2011-05-26 Tristan Gingold <gingold@adacore.com>
[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 static int pagesize;
123
124 /* When set, the file that we're processing is known to have debugging
125    info for C++ namespaces.  GCC 3.3.x did not produce this information,
126    but later versions do.  */
127
128 static int processing_has_namespace_info;
129
130 static const struct objfile_data *dwarf2_objfile_data_key;
131
132 struct dwarf2_section_info
133 {
134   asection *asection;
135   gdb_byte *buffer;
136   bfd_size_type size;
137   int was_mmapped;
138   /* True if we have tried to read this section.  */
139   int readin;
140 };
141
142 /* All offsets in the index are of this type.  It must be
143    architecture-independent.  */
144 typedef uint32_t offset_type;
145
146 DEF_VEC_I (offset_type);
147
148 /* A description of the mapped index.  The file format is described in
149    a comment by the code that writes the index.  */
150 struct mapped_index
151 {
152   /* Index data format version.  */
153   int version;
154
155   /* The total length of the buffer.  */
156   off_t total_size;
157
158   /* A pointer to the address table data.  */
159   const gdb_byte *address_table;
160
161   /* Size of the address table data in bytes.  */
162   offset_type address_table_size;
163
164   /* The symbol table, implemented as a hash table.  */
165   const offset_type *symbol_table;
166
167   /* Size in slots, each slot is 2 offset_types.  */
168   offset_type symbol_table_slots;
169
170   /* A pointer to the constant pool.  */
171   const char *constant_pool;
172 };
173
174 struct dwarf2_per_objfile
175 {
176   struct dwarf2_section_info info;
177   struct dwarf2_section_info abbrev;
178   struct dwarf2_section_info line;
179   struct dwarf2_section_info loc;
180   struct dwarf2_section_info macinfo;
181   struct dwarf2_section_info str;
182   struct dwarf2_section_info ranges;
183   struct dwarf2_section_info types;
184   struct dwarf2_section_info frame;
185   struct dwarf2_section_info eh_frame;
186   struct dwarf2_section_info gdb_index;
187
188   /* Back link.  */
189   struct objfile *objfile;
190
191   /* A list of all the compilation units.  This is used to locate
192      the target compilation unit of a particular reference.  */
193   struct dwarf2_per_cu_data **all_comp_units;
194
195   /* The number of compilation units in ALL_COMP_UNITS.  */
196   int n_comp_units;
197
198   /* The number of .debug_types-related CUs.  */
199   int n_type_comp_units;
200
201   /* The .debug_types-related CUs.  */
202   struct dwarf2_per_cu_data **type_comp_units;
203
204   /* A chain of compilation units that are currently read in, so that
205      they can be freed later.  */
206   struct dwarf2_per_cu_data *read_in_chain;
207
208   /* A table mapping .debug_types signatures to its signatured_type entry.
209      This is NULL if the .debug_types section hasn't been read in yet.  */
210   htab_t signatured_types;
211
212   /* A flag indicating wether this objfile has a section loaded at a
213      VMA of 0.  */
214   int has_section_at_zero;
215
216   /* True if we are using the mapped index,
217      or we are faking it for OBJF_READNOW's sake.  */
218   unsigned char using_index;
219
220   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
221   struct mapped_index *index_table;
222
223   /* When using index_table, this keeps track of all quick_file_names entries.
224      TUs can share line table entries with CUs or other TUs, and there can be
225      a lot more TUs than unique line tables, so we maintain a separate table
226      of all line table entries to support the sharing.  */
227   htab_t quick_file_names_table;
228
229   /* Set during partial symbol reading, to prevent queueing of full
230      symbols.  */
231   int reading_partial_symbols;
232
233   /* Table mapping type .debug_info DIE offsets to types.
234      This is NULL if not allocated yet.
235      It (currently) makes sense to allocate debug_types_type_hash lazily.
236      To keep things simple we allocate both lazily.  */
237   htab_t debug_info_type_hash;
238
239   /* Table mapping type .debug_types DIE offsets to types.
240      This is NULL if not allocated yet.  */
241   htab_t debug_types_type_hash;
242 };
243
244 static struct dwarf2_per_objfile *dwarf2_per_objfile;
245
246 /* Default names of the debugging sections.  */
247
248 /* Note that if the debugging section has been compressed, it might
249    have a name like .zdebug_info.  */
250
251 static const struct dwarf2_debug_sections dwarf2_elf_names = {
252   { ".debug_info", ".zdebug_info" },
253   { ".debug_abbrev", ".zdebug_abbrev" },
254   { ".debug_line", ".zdebug_line" },
255   { ".debug_loc", ".zdebug_loc" },
256   { ".debug_macinfo", ".zdebug_macinfo" },
257   { ".debug_str", ".zdebug_str" },
258   { ".debug_ranges", ".zdebug_ranges" },
259   { ".debug_types", ".zdebug_types" },
260   { ".debug_frame", ".zdebug_frame" },
261   { ".eh_frame", NULL },
262   { ".gdb_index", ".zgdb_index" }
263 };
264
265 /* local data types */
266
267 /* We hold several abbreviation tables in memory at the same time.  */
268 #ifndef ABBREV_HASH_SIZE
269 #define ABBREV_HASH_SIZE 121
270 #endif
271
272 /* The data in a compilation unit header, after target2host
273    translation, looks like this.  */
274 struct comp_unit_head
275 {
276   unsigned int length;
277   short version;
278   unsigned char addr_size;
279   unsigned char signed_addr_p;
280   unsigned int abbrev_offset;
281
282   /* Size of file offsets; either 4 or 8.  */
283   unsigned int offset_size;
284
285   /* Size of the length field; either 4 or 12.  */
286   unsigned int initial_length_size;
287
288   /* Offset to the first byte of this compilation unit header in the
289      .debug_info section, for resolving relative reference dies.  */
290   unsigned int offset;
291
292   /* Offset to first die in this cu from the start of the cu.
293      This will be the first byte following the compilation unit header.  */
294   unsigned int first_die_offset;
295 };
296
297 /* Type used for delaying computation of method physnames.
298    See comments for compute_delayed_physnames.  */
299 struct delayed_method_info
300 {
301   /* The type to which the method is attached, i.e., its parent class.  */
302   struct type *type;
303
304   /* The index of the method in the type's function fieldlists.  */
305   int fnfield_index;
306
307   /* The index of the method in the fieldlist.  */
308   int index;
309
310   /* The name of the DIE.  */
311   const char *name;
312
313   /*  The DIE associated with this method.  */
314   struct die_info *die;
315 };
316
317 typedef struct delayed_method_info delayed_method_info;
318 DEF_VEC_O (delayed_method_info);
319
320 /* Internal state when decoding a particular compilation unit.  */
321 struct dwarf2_cu
322 {
323   /* The objfile containing this compilation unit.  */
324   struct objfile *objfile;
325
326   /* The header of the compilation unit.  */
327   struct comp_unit_head header;
328
329   /* Base address of this compilation unit.  */
330   CORE_ADDR base_address;
331
332   /* Non-zero if base_address has been set.  */
333   int base_known;
334
335   struct function_range *first_fn, *last_fn, *cached_fn;
336
337   /* The language we are debugging.  */
338   enum language language;
339   const struct language_defn *language_defn;
340
341   const char *producer;
342
343   /* The generic symbol table building routines have separate lists for
344      file scope symbols and all all other scopes (local scopes).  So
345      we need to select the right one to pass to add_symbol_to_list().
346      We do it by keeping a pointer to the correct list in list_in_scope.
347
348      FIXME: The original dwarf code just treated the file scope as the
349      first local scope, and all other local scopes as nested local
350      scopes, and worked fine.  Check to see if we really need to
351      distinguish these in buildsym.c.  */
352   struct pending **list_in_scope;
353
354   /* DWARF abbreviation table associated with this compilation unit.  */
355   struct abbrev_info **dwarf2_abbrevs;
356
357   /* Storage for the abbrev table.  */
358   struct obstack abbrev_obstack;
359
360   /* Hash table holding all the loaded partial DIEs.  */
361   htab_t partial_dies;
362
363   /* Storage for things with the same lifetime as this read-in compilation
364      unit, including partial DIEs.  */
365   struct obstack comp_unit_obstack;
366
367   /* When multiple dwarf2_cu structures are living in memory, this field
368      chains them all together, so that they can be released efficiently.
369      We will probably also want a generation counter so that most-recently-used
370      compilation units are cached...  */
371   struct dwarf2_per_cu_data *read_in_chain;
372
373   /* Backchain to our per_cu entry if the tree has been built.  */
374   struct dwarf2_per_cu_data *per_cu;
375
376   /* How many compilation units ago was this CU last referenced?  */
377   int last_used;
378
379   /* A hash table of die offsets for following references.  */
380   htab_t die_hash;
381
382   /* Full DIEs if read in.  */
383   struct die_info *dies;
384
385   /* A set of pointers to dwarf2_per_cu_data objects for compilation
386      units referenced by this one.  Only set during full symbol processing;
387      partial symbol tables do not have dependencies.  */
388   htab_t dependencies;
389
390   /* Header data from the line table, during full symbol processing.  */
391   struct line_header *line_header;
392
393   /* A list of methods which need to have physnames computed
394      after all type information has been read.  */
395   VEC (delayed_method_info) *method_list;
396
397   /* Mark used when releasing cached dies.  */
398   unsigned int mark : 1;
399
400   /* This flag will be set if this compilation unit might include
401      inter-compilation-unit references.  */
402   unsigned int has_form_ref_addr : 1;
403
404   /* This flag will be set if this compilation unit includes any
405      DW_TAG_namespace DIEs.  If we know that there are explicit
406      DIEs for namespaces, we don't need to try to infer them
407      from mangled names.  */
408   unsigned int has_namespace_info : 1;
409
410   /* This CU references .debug_loc.  See the symtab->locations_valid field.
411      This test is imperfect as there may exist optimized debug code not using
412      any location list and still facing inlining issues if handled as
413      unoptimized code.  For a future better test see GCC PR other/32998.  */
414
415   unsigned int has_loclist : 1;
416 };
417
418 /* Persistent data held for a compilation unit, even when not
419    processing it.  We put a pointer to this structure in the
420    read_symtab_private field of the psymtab.  If we encounter
421    inter-compilation-unit references, we also maintain a sorted
422    list of all compilation units.  */
423
424 struct dwarf2_per_cu_data
425 {
426   /* The start offset and length of this compilation unit.  2**29-1
427      bytes should suffice to store the length of any compilation unit
428      - if it doesn't, GDB will fall over anyway.
429      NOTE: Unlike comp_unit_head.length, this length includes
430      initial_length_size.  */
431   unsigned int offset;
432   unsigned int length : 29;
433
434   /* Flag indicating this compilation unit will be read in before
435      any of the current compilation units are processed.  */
436   unsigned int queued : 1;
437
438   /* This flag will be set if we need to load absolutely all DIEs
439      for this compilation unit, instead of just the ones we think
440      are interesting.  It gets set if we look for a DIE in the
441      hash table and don't find it.  */
442   unsigned int load_all_dies : 1;
443
444   /* Non-zero if this CU is from .debug_types.
445      Otherwise it's from .debug_info.  */
446   unsigned int from_debug_types : 1;
447
448   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
449      of the CU cache it gets reset to NULL again.  */
450   struct dwarf2_cu *cu;
451
452   /* The corresponding objfile.  */
453   struct objfile *objfile;
454
455   /* When using partial symbol tables, the 'psymtab' field is active.
456      Otherwise the 'quick' field is active.  */
457   union
458   {
459     /* The partial symbol table associated with this compilation unit,
460        or NULL for partial units (which do not have an associated
461        symtab).  */
462     struct partial_symtab *psymtab;
463
464     /* Data needed by the "quick" functions.  */
465     struct dwarf2_per_cu_quick_data *quick;
466   } v;
467 };
468
469 /* Entry in the signatured_types hash table.  */
470
471 struct signatured_type
472 {
473   ULONGEST signature;
474
475   /* Offset in .debug_types of the type defined by this TU.  */
476   unsigned int type_offset;
477
478   /* The CU(/TU) of this type.  */
479   struct dwarf2_per_cu_data per_cu;
480 };
481
482 /* Struct used to pass misc. parameters to read_die_and_children, et
483    al.  which are used for both .debug_info and .debug_types dies.
484    All parameters here are unchanging for the life of the call.  This
485    struct exists to abstract away the constant parameters of die
486    reading.  */
487
488 struct die_reader_specs
489 {
490   /* The bfd of this objfile.  */
491   bfd* abfd;
492
493   /* The CU of the DIE we are parsing.  */
494   struct dwarf2_cu *cu;
495
496   /* Pointer to start of section buffer.
497      This is either the start of .debug_info or .debug_types.  */
498   const gdb_byte *buffer;
499 };
500
501 /* The line number information for a compilation unit (found in the
502    .debug_line section) begins with a "statement program header",
503    which contains the following information.  */
504 struct line_header
505 {
506   unsigned int total_length;
507   unsigned short version;
508   unsigned int header_length;
509   unsigned char minimum_instruction_length;
510   unsigned char maximum_ops_per_instruction;
511   unsigned char default_is_stmt;
512   int line_base;
513   unsigned char line_range;
514   unsigned char opcode_base;
515
516   /* standard_opcode_lengths[i] is the number of operands for the
517      standard opcode whose value is i.  This means that
518      standard_opcode_lengths[0] is unused, and the last meaningful
519      element is standard_opcode_lengths[opcode_base - 1].  */
520   unsigned char *standard_opcode_lengths;
521
522   /* The include_directories table.  NOTE!  These strings are not
523      allocated with xmalloc; instead, they are pointers into
524      debug_line_buffer.  If you try to free them, `free' will get
525      indigestion.  */
526   unsigned int num_include_dirs, include_dirs_size;
527   char **include_dirs;
528
529   /* The file_names table.  NOTE!  These strings are not allocated
530      with xmalloc; instead, they are pointers into debug_line_buffer.
531      Don't try to free them directly.  */
532   unsigned int num_file_names, file_names_size;
533   struct file_entry
534   {
535     char *name;
536     unsigned int dir_index;
537     unsigned int mod_time;
538     unsigned int length;
539     int included_p; /* Non-zero if referenced by the Line Number Program.  */
540     struct symtab *symtab; /* The associated symbol table, if any.  */
541   } *file_names;
542
543   /* The start and end of the statement program following this
544      header.  These point into dwarf2_per_objfile->line_buffer.  */
545   gdb_byte *statement_program_start, *statement_program_end;
546 };
547
548 /* When we construct a partial symbol table entry we only
549    need this much information.  */
550 struct partial_die_info
551   {
552     /* Offset of this DIE.  */
553     unsigned int offset;
554
555     /* DWARF-2 tag for this DIE.  */
556     ENUM_BITFIELD(dwarf_tag) tag : 16;
557
558     /* Assorted flags describing the data found in this DIE.  */
559     unsigned int has_children : 1;
560     unsigned int is_external : 1;
561     unsigned int is_declaration : 1;
562     unsigned int has_type : 1;
563     unsigned int has_specification : 1;
564     unsigned int has_pc_info : 1;
565
566     /* Flag set if the SCOPE field of this structure has been
567        computed.  */
568     unsigned int scope_set : 1;
569
570     /* Flag set if the DIE has a byte_size attribute.  */
571     unsigned int has_byte_size : 1;
572
573     /* Flag set if any of the DIE's children are template arguments.  */
574     unsigned int has_template_arguments : 1;
575
576     /* Flag set if fixup_partial_die has been called on this die.  */
577     unsigned int fixup_called : 1;
578
579     /* The name of this DIE.  Normally the value of DW_AT_name, but
580        sometimes a default name for unnamed DIEs.  */
581     char *name;
582
583     /* The linkage name, if present.  */
584     const char *linkage_name;
585
586     /* The scope to prepend to our children.  This is generally
587        allocated on the comp_unit_obstack, so will disappear
588        when this compilation unit leaves the cache.  */
589     char *scope;
590
591     /* The location description associated with this DIE, if any.  */
592     struct dwarf_block *locdesc;
593
594     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
595     CORE_ADDR lowpc;
596     CORE_ADDR highpc;
597
598     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
599        DW_AT_sibling, if any.  */
600     /* NOTE: This member isn't strictly necessary, read_partial_die could
601        return DW_AT_sibling values to its caller load_partial_dies.  */
602     gdb_byte *sibling;
603
604     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
605        DW_AT_specification (or DW_AT_abstract_origin or
606        DW_AT_extension).  */
607     unsigned int spec_offset;
608
609     /* Pointers to this DIE's parent, first child, and next sibling,
610        if any.  */
611     struct partial_die_info *die_parent, *die_child, *die_sibling;
612   };
613
614 /* This data structure holds the information of an abbrev.  */
615 struct abbrev_info
616   {
617     unsigned int number;        /* number identifying abbrev */
618     enum dwarf_tag tag;         /* dwarf tag */
619     unsigned short has_children;                /* boolean */
620     unsigned short num_attrs;   /* number of attributes */
621     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
622     struct abbrev_info *next;   /* next in chain */
623   };
624
625 struct attr_abbrev
626   {
627     ENUM_BITFIELD(dwarf_attribute) name : 16;
628     ENUM_BITFIELD(dwarf_form) form : 16;
629   };
630
631 /* Attributes have a name and a value.  */
632 struct attribute
633   {
634     ENUM_BITFIELD(dwarf_attribute) name : 16;
635     ENUM_BITFIELD(dwarf_form) form : 15;
636
637     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
638        field should be in u.str (existing only for DW_STRING) but it is kept
639        here for better struct attribute alignment.  */
640     unsigned int string_is_canonical : 1;
641
642     union
643       {
644         char *str;
645         struct dwarf_block *blk;
646         ULONGEST unsnd;
647         LONGEST snd;
648         CORE_ADDR addr;
649         struct signatured_type *signatured_type;
650       }
651     u;
652   };
653
654 /* This data structure holds a complete die structure.  */
655 struct die_info
656   {
657     /* DWARF-2 tag for this DIE.  */
658     ENUM_BITFIELD(dwarf_tag) tag : 16;
659
660     /* Number of attributes */
661     unsigned char num_attrs;
662
663     /* True if we're presently building the full type name for the
664        type derived from this DIE.  */
665     unsigned char building_fullname : 1;
666
667     /* Abbrev number */
668     unsigned int abbrev;
669
670     /* Offset in .debug_info or .debug_types section.  */
671     unsigned int offset;
672
673     /* The dies in a compilation unit form an n-ary tree.  PARENT
674        points to this die's parent; CHILD points to the first child of
675        this node; and all the children of a given node are chained
676        together via their SIBLING fields.  */
677     struct die_info *child;     /* Its first child, if any.  */
678     struct die_info *sibling;   /* Its next sibling, if any.  */
679     struct die_info *parent;    /* Its parent, if any.  */
680
681     /* An array of attributes, with NUM_ATTRS elements.  There may be
682        zero, but it's not common and zero-sized arrays are not
683        sufficiently portable C.  */
684     struct attribute attrs[1];
685   };
686
687 struct function_range
688 {
689   const char *name;
690   CORE_ADDR lowpc, highpc;
691   int seen_line;
692   struct function_range *next;
693 };
694
695 /* Get at parts of an attribute structure.  */
696
697 #define DW_STRING(attr)    ((attr)->u.str)
698 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
699 #define DW_UNSND(attr)     ((attr)->u.unsnd)
700 #define DW_BLOCK(attr)     ((attr)->u.blk)
701 #define DW_SND(attr)       ((attr)->u.snd)
702 #define DW_ADDR(attr)      ((attr)->u.addr)
703 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
704
705 /* Blocks are a bunch of untyped bytes.  */
706 struct dwarf_block
707   {
708     unsigned int size;
709     gdb_byte *data;
710   };
711
712 #ifndef ATTR_ALLOC_CHUNK
713 #define ATTR_ALLOC_CHUNK 4
714 #endif
715
716 /* Allocate fields for structs, unions and enums in this size.  */
717 #ifndef DW_FIELD_ALLOC_CHUNK
718 #define DW_FIELD_ALLOC_CHUNK 4
719 #endif
720
721 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
722    but this would require a corresponding change in unpack_field_as_long
723    and friends.  */
724 static int bits_per_byte = 8;
725
726 /* The routines that read and process dies for a C struct or C++ class
727    pass lists of data member fields and lists of member function fields
728    in an instance of a field_info structure, as defined below.  */
729 struct field_info
730   {
731     /* List of data member and baseclasses fields.  */
732     struct nextfield
733       {
734         struct nextfield *next;
735         int accessibility;
736         int virtuality;
737         struct field field;
738       }
739      *fields, *baseclasses;
740
741     /* Number of fields (including baseclasses).  */
742     int nfields;
743
744     /* Number of baseclasses.  */
745     int nbaseclasses;
746
747     /* Set if the accesibility of one of the fields is not public.  */
748     int non_public_fields;
749
750     /* Member function fields array, entries are allocated in the order they
751        are encountered in the object file.  */
752     struct nextfnfield
753       {
754         struct nextfnfield *next;
755         struct fn_field fnfield;
756       }
757      *fnfields;
758
759     /* Member function fieldlist array, contains name of possibly overloaded
760        member function, number of overloaded member functions and a pointer
761        to the head of the member function field chain.  */
762     struct fnfieldlist
763       {
764         char *name;
765         int length;
766         struct nextfnfield *head;
767       }
768      *fnfieldlists;
769
770     /* Number of entries in the fnfieldlists array.  */
771     int nfnfields;
772
773     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
774        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
775     struct typedef_field_list
776       {
777         struct typedef_field field;
778         struct typedef_field_list *next;
779       }
780     *typedef_field_list;
781     unsigned typedef_field_list_count;
782   };
783
784 /* One item on the queue of compilation units to read in full symbols
785    for.  */
786 struct dwarf2_queue_item
787 {
788   struct dwarf2_per_cu_data *per_cu;
789   struct dwarf2_queue_item *next;
790 };
791
792 /* The current queue.  */
793 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
794
795 /* Loaded secondary compilation units are kept in memory until they
796    have not been referenced for the processing of this many
797    compilation units.  Set this to zero to disable caching.  Cache
798    sizes of up to at least twenty will improve startup time for
799    typical inter-CU-reference binaries, at an obvious memory cost.  */
800 static int dwarf2_max_cache_age = 5;
801 static void
802 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
803                            struct cmd_list_element *c, const char *value)
804 {
805   fprintf_filtered (file, _("The upper bound on the age of cached "
806                             "dwarf2 compilation units is %s.\n"),
807                     value);
808 }
809
810
811 /* Various complaints about symbol reading that don't abort the process.  */
812
813 static void
814 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
815 {
816   complaint (&symfile_complaints,
817              _("statement list doesn't fit in .debug_line section"));
818 }
819
820 static void
821 dwarf2_debug_line_missing_file_complaint (void)
822 {
823   complaint (&symfile_complaints,
824              _(".debug_line section has line data without a file"));
825 }
826
827 static void
828 dwarf2_debug_line_missing_end_sequence_complaint (void)
829 {
830   complaint (&symfile_complaints,
831              _(".debug_line section has line "
832                "program sequence without an end"));
833 }
834
835 static void
836 dwarf2_complex_location_expr_complaint (void)
837 {
838   complaint (&symfile_complaints, _("location expression too complex"));
839 }
840
841 static void
842 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
843                                               int arg3)
844 {
845   complaint (&symfile_complaints,
846              _("const value length mismatch for '%s', got %d, expected %d"),
847              arg1, arg2, arg3);
848 }
849
850 static void
851 dwarf2_macros_too_long_complaint (void)
852 {
853   complaint (&symfile_complaints,
854              _("macro info runs off end of `.debug_macinfo' section"));
855 }
856
857 static void
858 dwarf2_macro_malformed_definition_complaint (const char *arg1)
859 {
860   complaint (&symfile_complaints,
861              _("macro debug info contains a "
862                "malformed macro definition:\n`%s'"),
863              arg1);
864 }
865
866 static void
867 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
868 {
869   complaint (&symfile_complaints,
870              _("invalid attribute class or form for '%s' in '%s'"),
871              arg1, arg2);
872 }
873
874 /* local function prototypes */
875
876 static void dwarf2_locate_sections (bfd *, asection *, void *);
877
878 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
879                                            struct objfile *);
880
881 static void dwarf2_build_psymtabs_hard (struct objfile *);
882
883 static void scan_partial_symbols (struct partial_die_info *,
884                                   CORE_ADDR *, CORE_ADDR *,
885                                   int, struct dwarf2_cu *);
886
887 static void add_partial_symbol (struct partial_die_info *,
888                                 struct dwarf2_cu *);
889
890 static void add_partial_namespace (struct partial_die_info *pdi,
891                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
892                                    int need_pc, struct dwarf2_cu *cu);
893
894 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
895                                 CORE_ADDR *highpc, int need_pc,
896                                 struct dwarf2_cu *cu);
897
898 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
899                                      struct dwarf2_cu *cu);
900
901 static void add_partial_subprogram (struct partial_die_info *pdi,
902                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
903                                     int need_pc, struct dwarf2_cu *cu);
904
905 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
906                                      gdb_byte *buffer, gdb_byte *info_ptr,
907                                      bfd *abfd, struct dwarf2_cu *cu);
908
909 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
910
911 static void psymtab_to_symtab_1 (struct partial_symtab *);
912
913 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
914
915 static void dwarf2_free_abbrev_table (void *);
916
917 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
918                                             struct dwarf2_cu *);
919
920 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
921                                                  struct dwarf2_cu *);
922
923 static struct partial_die_info *load_partial_dies (bfd *,
924                                                    gdb_byte *, gdb_byte *,
925                                                    int, struct dwarf2_cu *);
926
927 static gdb_byte *read_partial_die (struct partial_die_info *,
928                                    struct abbrev_info *abbrev,
929                                    unsigned int, bfd *,
930                                    gdb_byte *, gdb_byte *,
931                                    struct dwarf2_cu *);
932
933 static struct partial_die_info *find_partial_die (unsigned int,
934                                                   struct dwarf2_cu *);
935
936 static void fixup_partial_die (struct partial_die_info *,
937                                struct dwarf2_cu *);
938
939 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
940                                  bfd *, gdb_byte *, struct dwarf2_cu *);
941
942 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
943                                        bfd *, gdb_byte *, struct dwarf2_cu *);
944
945 static unsigned int read_1_byte (bfd *, gdb_byte *);
946
947 static int read_1_signed_byte (bfd *, gdb_byte *);
948
949 static unsigned int read_2_bytes (bfd *, gdb_byte *);
950
951 static unsigned int read_4_bytes (bfd *, gdb_byte *);
952
953 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
954
955 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
956                                unsigned int *);
957
958 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
959
960 static LONGEST read_checked_initial_length_and_offset
961   (bfd *, gdb_byte *, const struct comp_unit_head *,
962    unsigned int *, unsigned int *);
963
964 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
965                             unsigned int *);
966
967 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
968
969 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
970
971 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
972
973 static char *read_indirect_string (bfd *, gdb_byte *,
974                                    const struct comp_unit_head *,
975                                    unsigned int *);
976
977 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
978
979 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
980
981 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
982
983 static void set_cu_language (unsigned int, struct dwarf2_cu *);
984
985 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
986                                       struct dwarf2_cu *);
987
988 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
989                                                 unsigned int,
990                                                 struct dwarf2_cu *);
991
992 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
993                                struct dwarf2_cu *cu);
994
995 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
996
997 static struct die_info *die_specification (struct die_info *die,
998                                            struct dwarf2_cu **);
999
1000 static void free_line_header (struct line_header *lh);
1001
1002 static void add_file_name (struct line_header *, char *, unsigned int,
1003                            unsigned int, unsigned int);
1004
1005 static struct line_header *(dwarf_decode_line_header
1006                             (unsigned int offset,
1007                              bfd *abfd, struct dwarf2_cu *cu));
1008
1009 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1010                                 struct dwarf2_cu *, struct partial_symtab *);
1011
1012 static void dwarf2_start_subfile (char *, const char *, const char *);
1013
1014 static struct symbol *new_symbol (struct die_info *, struct type *,
1015                                   struct dwarf2_cu *);
1016
1017 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1018                                        struct dwarf2_cu *, struct symbol *);
1019
1020 static void dwarf2_const_value (struct attribute *, struct symbol *,
1021                                 struct dwarf2_cu *);
1022
1023 static void dwarf2_const_value_attr (struct attribute *attr,
1024                                      struct type *type,
1025                                      const char *name,
1026                                      struct obstack *obstack,
1027                                      struct dwarf2_cu *cu, long *value,
1028                                      gdb_byte **bytes,
1029                                      struct dwarf2_locexpr_baton **baton);
1030
1031 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1032
1033 static int need_gnat_info (struct dwarf2_cu *);
1034
1035 static struct type *die_descriptive_type (struct die_info *,
1036                                           struct dwarf2_cu *);
1037
1038 static void set_descriptive_type (struct type *, struct die_info *,
1039                                   struct dwarf2_cu *);
1040
1041 static struct type *die_containing_type (struct die_info *,
1042                                          struct dwarf2_cu *);
1043
1044 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045                                      struct dwarf2_cu *);
1046
1047 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1048
1049 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
1051 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1052
1053 static char *typename_concat (struct obstack *obs, const char *prefix,
1054                               const char *suffix, int physname,
1055                               struct dwarf2_cu *cu);
1056
1057 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1062
1063 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1064
1065 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066                                struct dwarf2_cu *, struct partial_symtab *);
1067
1068 static int dwarf2_get_pc_bounds (struct die_info *,
1069                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070                                  struct partial_symtab *);
1071
1072 static void get_scope_pc_bounds (struct die_info *,
1073                                  CORE_ADDR *, CORE_ADDR *,
1074                                  struct dwarf2_cu *);
1075
1076 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077                                         CORE_ADDR, struct dwarf2_cu *);
1078
1079 static void dwarf2_add_field (struct field_info *, struct die_info *,
1080                               struct dwarf2_cu *);
1081
1082 static void dwarf2_attach_fields_to_type (struct field_info *,
1083                                           struct type *, struct dwarf2_cu *);
1084
1085 static void dwarf2_add_member_fn (struct field_info *,
1086                                   struct die_info *, struct type *,
1087                                   struct dwarf2_cu *);
1088
1089 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1090                                              struct type *,
1091                                              struct dwarf2_cu *);
1092
1093 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1094
1095 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1096
1097 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1098
1099 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1100
1101 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1102
1103 static struct type *read_module_type (struct die_info *die,
1104                                       struct dwarf2_cu *cu);
1105
1106 static const char *namespace_name (struct die_info *die,
1107                                    int *is_anonymous, struct dwarf2_cu *);
1108
1109 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1110
1111 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1112
1113 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1114                                                        struct dwarf2_cu *);
1115
1116 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1117
1118 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1119                                                  gdb_byte *info_ptr,
1120                                                  gdb_byte **new_info_ptr,
1121                                                  struct die_info *parent);
1122
1123 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1124                                                gdb_byte *info_ptr,
1125                                                gdb_byte **new_info_ptr,
1126                                                struct die_info *parent);
1127
1128 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1129                                                gdb_byte *info_ptr,
1130                                                gdb_byte **new_info_ptr,
1131                                                struct die_info *parent);
1132
1133 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1134                                 struct die_info **, gdb_byte *,
1135                                 int *);
1136
1137 static void process_die (struct die_info *, struct dwarf2_cu *);
1138
1139 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1140                                        struct obstack *);
1141
1142 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1143
1144 static const char *dwarf2_full_name (char *name,
1145                                      struct die_info *die,
1146                                      struct dwarf2_cu *cu);
1147
1148 static struct die_info *dwarf2_extension (struct die_info *die,
1149                                           struct dwarf2_cu **);
1150
1151 static char *dwarf_tag_name (unsigned int);
1152
1153 static char *dwarf_attr_name (unsigned int);
1154
1155 static char *dwarf_form_name (unsigned int);
1156
1157 static char *dwarf_bool_name (unsigned int);
1158
1159 static char *dwarf_type_encoding_name (unsigned int);
1160
1161 #if 0
1162 static char *dwarf_cfi_name (unsigned int);
1163 #endif
1164
1165 static struct die_info *sibling_die (struct die_info *);
1166
1167 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1168
1169 static void dump_die_for_error (struct die_info *);
1170
1171 static void dump_die_1 (struct ui_file *, int level, int max_level,
1172                         struct die_info *);
1173
1174 /*static*/ void dump_die (struct die_info *, int max_level);
1175
1176 static void store_in_ref_table (struct die_info *,
1177                                 struct dwarf2_cu *);
1178
1179 static int is_ref_attr (struct attribute *);
1180
1181 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1182
1183 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1184
1185 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1186                                                struct attribute *,
1187                                                struct dwarf2_cu **);
1188
1189 static struct die_info *follow_die_ref (struct die_info *,
1190                                         struct attribute *,
1191                                         struct dwarf2_cu **);
1192
1193 static struct die_info *follow_die_sig (struct die_info *,
1194                                         struct attribute *,
1195                                         struct dwarf2_cu **);
1196
1197 static void read_signatured_type_at_offset (struct objfile *objfile,
1198                                             unsigned int offset);
1199
1200 static void read_signatured_type (struct objfile *,
1201                                   struct signatured_type *type_sig);
1202
1203 /* memory allocation interface */
1204
1205 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1206
1207 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1208
1209 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1210
1211 static void initialize_cu_func_list (struct dwarf2_cu *);
1212
1213 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1214                                  struct dwarf2_cu *);
1215
1216 static void dwarf_decode_macros (struct line_header *, unsigned int,
1217                                  char *, bfd *, struct dwarf2_cu *);
1218
1219 static int attr_form_is_block (struct attribute *);
1220
1221 static int attr_form_is_section_offset (struct attribute *);
1222
1223 static int attr_form_is_constant (struct attribute *);
1224
1225 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1226                                    struct dwarf2_loclist_baton *baton,
1227                                    struct attribute *attr);
1228
1229 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1230                                          struct symbol *sym,
1231                                          struct dwarf2_cu *cu);
1232
1233 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1234                                struct abbrev_info *abbrev,
1235                                struct dwarf2_cu *cu);
1236
1237 static void free_stack_comp_unit (void *);
1238
1239 static hashval_t partial_die_hash (const void *item);
1240
1241 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1242
1243 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1244   (unsigned int offset, struct objfile *objfile);
1245
1246 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1247   (unsigned int offset, struct objfile *objfile);
1248
1249 static void init_one_comp_unit (struct dwarf2_cu *cu,
1250                                 struct objfile *objfile);
1251
1252 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1253                                    struct die_info *comp_unit_die);
1254
1255 static void free_one_comp_unit (void *);
1256
1257 static void free_cached_comp_units (void *);
1258
1259 static void age_cached_comp_units (void);
1260
1261 static void free_one_cached_comp_unit (void *);
1262
1263 static struct type *set_die_type (struct die_info *, struct type *,
1264                                   struct dwarf2_cu *);
1265
1266 static void create_all_comp_units (struct objfile *);
1267
1268 static int create_debug_types_hash_table (struct objfile *objfile);
1269
1270 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1271                                  struct objfile *);
1272
1273 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275 static void dwarf2_add_dependence (struct dwarf2_cu *,
1276                                    struct dwarf2_per_cu_data *);
1277
1278 static void dwarf2_mark (struct dwarf2_cu *);
1279
1280 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
1282 static struct type *get_die_type_at_offset (unsigned int,
1283                                             struct dwarf2_per_cu_data *per_cu);
1284
1285 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static void dwarf2_release_queue (void *dummy);
1288
1289 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1290                              struct objfile *objfile);
1291
1292 static void process_queue (struct objfile *objfile);
1293
1294 static void find_file_and_directory (struct die_info *die,
1295                                      struct dwarf2_cu *cu,
1296                                      char **name, char **comp_dir);
1297
1298 static char *file_full_name (int file, struct line_header *lh,
1299                              const char *comp_dir);
1300
1301 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1302                                               gdb_byte *info_ptr,
1303                                               gdb_byte *buffer,
1304                                               unsigned int buffer_size,
1305                                               bfd *abfd);
1306
1307 static void init_cu_die_reader (struct die_reader_specs *reader,
1308                                 struct dwarf2_cu *cu);
1309
1310 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1311
1312 #if WORDS_BIGENDIAN
1313
1314 /* Convert VALUE between big- and little-endian.  */
1315 static offset_type
1316 byte_swap (offset_type value)
1317 {
1318   offset_type result;
1319
1320   result = (value & 0xff) << 24;
1321   result |= (value & 0xff00) << 8;
1322   result |= (value & 0xff0000) >> 8;
1323   result |= (value & 0xff000000) >> 24;
1324   return result;
1325 }
1326
1327 #define MAYBE_SWAP(V)  byte_swap (V)
1328
1329 #else
1330 #define MAYBE_SWAP(V) (V)
1331 #endif /* WORDS_BIGENDIAN */
1332
1333 /* The suffix for an index file.  */
1334 #define INDEX_SUFFIX ".gdb-index"
1335
1336 static const char *dwarf2_physname (char *name, struct die_info *die,
1337                                     struct dwarf2_cu *cu);
1338
1339 /* Try to locate the sections we need for DWARF 2 debugging
1340    information and return true if we have enough to do something.
1341    NAMES points to the dwarf2 section names, or is NULL if the standard
1342    ELF names are used.  */
1343
1344 int
1345 dwarf2_has_info (struct objfile *objfile,
1346                  const struct dwarf2_debug_sections *names)
1347 {
1348   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1349   if (!dwarf2_per_objfile)
1350     {
1351       /* Initialize per-objfile state.  */
1352       struct dwarf2_per_objfile *data
1353         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1354
1355       memset (data, 0, sizeof (*data));
1356       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1357       dwarf2_per_objfile = data;
1358
1359       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1360                              (void *) names);
1361       dwarf2_per_objfile->objfile = objfile;
1362     }
1363   return (dwarf2_per_objfile->info.asection != NULL
1364           && dwarf2_per_objfile->abbrev.asection != NULL);
1365 }
1366
1367 /* When loading sections, we look either for uncompressed section or for
1368    compressed section names.  */
1369
1370 static int
1371 section_is_p (const char *section_name,
1372               const struct dwarf2_section_names *names)
1373 {
1374   if (names->normal != NULL
1375       && strcmp (section_name, names->normal) == 0)
1376     return 1;
1377   if (names->compressed != NULL
1378       && strcmp (section_name, names->compressed) == 0)
1379     return 1;
1380   return 0;
1381 }
1382
1383 /* This function is mapped across the sections and remembers the
1384    offset and size of each of the debugging sections we are interested
1385    in.  */
1386
1387 static void
1388 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1389 {
1390   const struct dwarf2_debug_sections *names;
1391
1392   if (vnames == NULL)
1393     names = &dwarf2_elf_names;
1394   else
1395     names = (const struct dwarf2_debug_sections *) vnames;
1396
1397   if (section_is_p (sectp->name, &names->info))
1398     {
1399       dwarf2_per_objfile->info.asection = sectp;
1400       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1401     }
1402   else if (section_is_p (sectp->name, &names->abbrev))
1403     {
1404       dwarf2_per_objfile->abbrev.asection = sectp;
1405       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1406     }
1407   else if (section_is_p (sectp->name, &names->line))
1408     {
1409       dwarf2_per_objfile->line.asection = sectp;
1410       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1411     }
1412   else if (section_is_p (sectp->name, &names->loc))
1413     {
1414       dwarf2_per_objfile->loc.asection = sectp;
1415       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1416     }
1417   else if (section_is_p (sectp->name, &names->macinfo))
1418     {
1419       dwarf2_per_objfile->macinfo.asection = sectp;
1420       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1421     }
1422   else if (section_is_p (sectp->name, &names->str))
1423     {
1424       dwarf2_per_objfile->str.asection = sectp;
1425       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1426     }
1427   else if (section_is_p (sectp->name, &names->frame))
1428     {
1429       dwarf2_per_objfile->frame.asection = sectp;
1430       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1431     }
1432   else if (section_is_p (sectp->name, &names->eh_frame))
1433     {
1434       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1435
1436       if (aflag & SEC_HAS_CONTENTS)
1437         {
1438           dwarf2_per_objfile->eh_frame.asection = sectp;
1439           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1440         }
1441     }
1442   else if (section_is_p (sectp->name, &names->ranges))
1443     {
1444       dwarf2_per_objfile->ranges.asection = sectp;
1445       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1446     }
1447   else if (section_is_p (sectp->name, &names->types))
1448     {
1449       dwarf2_per_objfile->types.asection = sectp;
1450       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1451     }
1452   else if (section_is_p (sectp->name, &names->gdb_index))
1453     {
1454       dwarf2_per_objfile->gdb_index.asection = sectp;
1455       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1456     }
1457
1458   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1459       && bfd_section_vma (abfd, sectp) == 0)
1460     dwarf2_per_objfile->has_section_at_zero = 1;
1461 }
1462
1463 /* Decompress a section that was compressed using zlib.  Store the
1464    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1465
1466 static void
1467 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1468                          gdb_byte **outbuf, bfd_size_type *outsize)
1469 {
1470   bfd *abfd = objfile->obfd;
1471 #ifndef HAVE_ZLIB_H
1472   error (_("Support for zlib-compressed DWARF data (from '%s') "
1473            "is disabled in this copy of GDB"),
1474          bfd_get_filename (abfd));
1475 #else
1476   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1477   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1478   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1479   bfd_size_type uncompressed_size;
1480   gdb_byte *uncompressed_buffer;
1481   z_stream strm;
1482   int rc;
1483   int header_size = 12;
1484
1485   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1486       || bfd_bread (compressed_buffer,
1487                     compressed_size, abfd) != compressed_size)
1488     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1489            bfd_get_filename (abfd));
1490
1491   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1492      by the uncompressed section size, 8 bytes in big-endian order.  */
1493   if (compressed_size < header_size
1494       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1495     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1496            bfd_get_filename (abfd));
1497   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1498   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1499   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1500   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1501   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1502   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1503   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1504   uncompressed_size += compressed_buffer[11];
1505
1506   /* It is possible the section consists of several compressed
1507      buffers concatenated together, so we uncompress in a loop.  */
1508   strm.zalloc = NULL;
1509   strm.zfree = NULL;
1510   strm.opaque = NULL;
1511   strm.avail_in = compressed_size - header_size;
1512   strm.next_in = (Bytef*) compressed_buffer + header_size;
1513   strm.avail_out = uncompressed_size;
1514   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1515                                        uncompressed_size);
1516   rc = inflateInit (&strm);
1517   while (strm.avail_in > 0)
1518     {
1519       if (rc != Z_OK)
1520         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1521                bfd_get_filename (abfd), rc);
1522       strm.next_out = ((Bytef*) uncompressed_buffer
1523                        + (uncompressed_size - strm.avail_out));
1524       rc = inflate (&strm, Z_FINISH);
1525       if (rc != Z_STREAM_END)
1526         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1527                bfd_get_filename (abfd), rc);
1528       rc = inflateReset (&strm);
1529     }
1530   rc = inflateEnd (&strm);
1531   if (rc != Z_OK
1532       || strm.avail_out != 0)
1533     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1534            bfd_get_filename (abfd), rc);
1535
1536   do_cleanups (cleanup);
1537   *outbuf = uncompressed_buffer;
1538   *outsize = uncompressed_size;
1539 #endif
1540 }
1541
1542 /* A helper function that decides whether a section is empty.  */
1543
1544 static int
1545 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1546 {
1547   return info->asection == NULL || info->size == 0;
1548 }
1549
1550 /* Read the contents of the section SECTP from object file specified by
1551    OBJFILE, store info about the section into INFO.
1552    If the section is compressed, uncompress it before returning.  */
1553
1554 static void
1555 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1556 {
1557   bfd *abfd = objfile->obfd;
1558   asection *sectp = info->asection;
1559   gdb_byte *buf, *retbuf;
1560   unsigned char header[4];
1561
1562   if (info->readin)
1563     return;
1564   info->buffer = NULL;
1565   info->was_mmapped = 0;
1566   info->readin = 1;
1567
1568   if (dwarf2_section_empty_p (info))
1569     return;
1570
1571   /* Check if the file has a 4-byte header indicating compression.  */
1572   if (info->size > sizeof (header)
1573       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1574       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1575     {
1576       /* Upon decompression, update the buffer and its size.  */
1577       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1578         {
1579           zlib_decompress_section (objfile, sectp, &info->buffer,
1580                                    &info->size);
1581           return;
1582         }
1583     }
1584
1585 #ifdef HAVE_MMAP
1586   if (pagesize == 0)
1587     pagesize = getpagesize ();
1588
1589   /* Only try to mmap sections which are large enough: we don't want to
1590      waste space due to fragmentation.  Also, only try mmap for sections
1591      without relocations.  */
1592
1593   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1594     {
1595       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1596       size_t map_length = info->size + sectp->filepos - pg_offset;
1597       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1598                                  MAP_PRIVATE, pg_offset);
1599
1600       if (retbuf != MAP_FAILED)
1601         {
1602           info->was_mmapped = 1;
1603           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1604 #if HAVE_POSIX_MADVISE
1605           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1606 #endif
1607           return;
1608         }
1609     }
1610 #endif
1611
1612   /* If we get here, we are a normal, not-compressed section.  */
1613   info->buffer = buf
1614     = obstack_alloc (&objfile->objfile_obstack, info->size);
1615
1616   /* When debugging .o files, we may need to apply relocations; see
1617      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1618      We never compress sections in .o files, so we only need to
1619      try this when the section is not compressed.  */
1620   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1621   if (retbuf != NULL)
1622     {
1623       info->buffer = retbuf;
1624       return;
1625     }
1626
1627   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1628       || bfd_bread (buf, info->size, abfd) != info->size)
1629     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1630            bfd_get_filename (abfd));
1631 }
1632
1633 /* A helper function that returns the size of a section in a safe way.
1634    If you are positive that the section has been read before using the
1635    size, then it is safe to refer to the dwarf2_section_info object's
1636    "size" field directly.  In other cases, you must call this
1637    function, because for compressed sections the size field is not set
1638    correctly until the section has been read.  */
1639
1640 static bfd_size_type
1641 dwarf2_section_size (struct objfile *objfile,
1642                      struct dwarf2_section_info *info)
1643 {
1644   if (!info->readin)
1645     dwarf2_read_section (objfile, info);
1646   return info->size;
1647 }
1648
1649 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1650    SECTION_NAME.  */
1651
1652 void
1653 dwarf2_get_section_info (struct objfile *objfile,
1654                          enum dwarf2_section_enum sect,
1655                          asection **sectp, gdb_byte **bufp,
1656                          bfd_size_type *sizep)
1657 {
1658   struct dwarf2_per_objfile *data
1659     = objfile_data (objfile, dwarf2_objfile_data_key);
1660   struct dwarf2_section_info *info;
1661
1662   /* We may see an objfile without any DWARF, in which case we just
1663      return nothing.  */
1664   if (data == NULL)
1665     {
1666       *sectp = NULL;
1667       *bufp = NULL;
1668       *sizep = 0;
1669       return;
1670     }
1671   switch (sect)
1672     {
1673     case DWARF2_DEBUG_FRAME:
1674       info = &data->frame;
1675       break;
1676     case DWARF2_EH_FRAME:
1677       info = &data->eh_frame;
1678       break;
1679     default:
1680       gdb_assert_not_reached ("unexpected section");
1681     }
1682
1683   dwarf2_read_section (objfile, info);
1684
1685   *sectp = info->asection;
1686   *bufp = info->buffer;
1687   *sizep = info->size;
1688 }
1689
1690 \f
1691 /* DWARF quick_symbols_functions support.  */
1692
1693 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1694    unique line tables, so we maintain a separate table of all .debug_line
1695    derived entries to support the sharing.
1696    All the quick functions need is the list of file names.  We discard the
1697    line_header when we're done and don't need to record it here.  */
1698 struct quick_file_names
1699 {
1700   /* The offset in .debug_line of the line table.  We hash on this.  */
1701   unsigned int offset;
1702
1703   /* The number of entries in file_names, real_names.  */
1704   unsigned int num_file_names;
1705
1706   /* The file names from the line table, after being run through
1707      file_full_name.  */
1708   const char **file_names;
1709
1710   /* The file names from the line table after being run through
1711      gdb_realpath.  These are computed lazily.  */
1712   const char **real_names;
1713 };
1714
1715 /* When using the index (and thus not using psymtabs), each CU has an
1716    object of this type.  This is used to hold information needed by
1717    the various "quick" methods.  */
1718 struct dwarf2_per_cu_quick_data
1719 {
1720   /* The file table.  This can be NULL if there was no file table
1721      or it's currently not read in.
1722      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1723   struct quick_file_names *file_names;
1724
1725   /* The corresponding symbol table.  This is NULL if symbols for this
1726      CU have not yet been read.  */
1727   struct symtab *symtab;
1728
1729   /* A temporary mark bit used when iterating over all CUs in
1730      expand_symtabs_matching.  */
1731   unsigned int mark : 1;
1732
1733   /* True if we've tried to read the file table and found there isn't one.
1734      There will be no point in trying to read it again next time.  */
1735   unsigned int no_file_data : 1;
1736 };
1737
1738 /* Hash function for a quick_file_names.  */
1739
1740 static hashval_t
1741 hash_file_name_entry (const void *e)
1742 {
1743   const struct quick_file_names *file_data = e;
1744
1745   return file_data->offset;
1746 }
1747
1748 /* Equality function for a quick_file_names.  */
1749
1750 static int
1751 eq_file_name_entry (const void *a, const void *b)
1752 {
1753   const struct quick_file_names *ea = a;
1754   const struct quick_file_names *eb = b;
1755
1756   return ea->offset == eb->offset;
1757 }
1758
1759 /* Delete function for a quick_file_names.  */
1760
1761 static void
1762 delete_file_name_entry (void *e)
1763 {
1764   struct quick_file_names *file_data = e;
1765   int i;
1766
1767   for (i = 0; i < file_data->num_file_names; ++i)
1768     {
1769       xfree ((void*) file_data->file_names[i]);
1770       if (file_data->real_names)
1771         xfree ((void*) file_data->real_names[i]);
1772     }
1773
1774   /* The space for the struct itself lives on objfile_obstack,
1775      so we don't free it here.  */
1776 }
1777
1778 /* Create a quick_file_names hash table.  */
1779
1780 static htab_t
1781 create_quick_file_names_table (unsigned int nr_initial_entries)
1782 {
1783   return htab_create_alloc (nr_initial_entries,
1784                             hash_file_name_entry, eq_file_name_entry,
1785                             delete_file_name_entry, xcalloc, xfree);
1786 }
1787
1788 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1789    this CU came.  */
1790
1791 static void
1792 dw2_do_instantiate_symtab (struct objfile *objfile,
1793                            struct dwarf2_per_cu_data *per_cu)
1794 {
1795   struct cleanup *back_to;
1796
1797   back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799   queue_comp_unit (per_cu, objfile);
1800
1801   if (per_cu->from_debug_types)
1802     read_signatured_type_at_offset (objfile, per_cu->offset);
1803   else
1804     load_full_comp_unit (per_cu, objfile);
1805
1806   process_queue (objfile);
1807
1808   /* Age the cache, releasing compilation units that have not
1809      been used recently.  */
1810   age_cached_comp_units ();
1811
1812   do_cleanups (back_to);
1813 }
1814
1815 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1816    the objfile from which this CU came.  Returns the resulting symbol
1817    table.  */
1818
1819 static struct symtab *
1820 dw2_instantiate_symtab (struct objfile *objfile,
1821                         struct dwarf2_per_cu_data *per_cu)
1822 {
1823   if (!per_cu->v.quick->symtab)
1824     {
1825       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1826       increment_reading_symtab ();
1827       dw2_do_instantiate_symtab (objfile, per_cu);
1828       do_cleanups (back_to);
1829     }
1830   return per_cu->v.quick->symtab;
1831 }
1832
1833 /* Return the CU given its index.  */
1834
1835 static struct dwarf2_per_cu_data *
1836 dw2_get_cu (int index)
1837 {
1838   if (index >= dwarf2_per_objfile->n_comp_units)
1839     {
1840       index -= dwarf2_per_objfile->n_comp_units;
1841       return dwarf2_per_objfile->type_comp_units[index];
1842     }
1843   return dwarf2_per_objfile->all_comp_units[index];
1844 }
1845
1846 /* A helper function that knows how to read a 64-bit value in a way
1847    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1848    otherwise.  */
1849
1850 static int
1851 extract_cu_value (const char *bytes, ULONGEST *result)
1852 {
1853   if (sizeof (ULONGEST) < 8)
1854     {
1855       int i;
1856
1857       /* Ignore the upper 4 bytes if they are all zero.  */
1858       for (i = 0; i < 4; ++i)
1859         if (bytes[i + 4] != 0)
1860           return 0;
1861
1862       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1863     }
1864   else
1865     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1866   return 1;
1867 }
1868
1869 /* Read the CU list from the mapped index, and use it to create all
1870    the CU objects for this objfile.  Return 0 if something went wrong,
1871    1 if everything went ok.  */
1872
1873 static int
1874 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1875                        offset_type cu_list_elements)
1876 {
1877   offset_type i;
1878
1879   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1880   dwarf2_per_objfile->all_comp_units
1881     = obstack_alloc (&objfile->objfile_obstack,
1882                      dwarf2_per_objfile->n_comp_units
1883                      * sizeof (struct dwarf2_per_cu_data *));
1884
1885   for (i = 0; i < cu_list_elements; i += 2)
1886     {
1887       struct dwarf2_per_cu_data *the_cu;
1888       ULONGEST offset, length;
1889
1890       if (!extract_cu_value (cu_list, &offset)
1891           || !extract_cu_value (cu_list + 8, &length))
1892         return 0;
1893       cu_list += 2 * 8;
1894
1895       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1896                                struct dwarf2_per_cu_data);
1897       the_cu->offset = offset;
1898       the_cu->length = length;
1899       the_cu->objfile = objfile;
1900       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1901                                         struct dwarf2_per_cu_quick_data);
1902       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1903     }
1904
1905   return 1;
1906 }
1907
1908 /* Create the signatured type hash table from the index.  */
1909
1910 static int
1911 create_signatured_type_table_from_index (struct objfile *objfile,
1912                                          const gdb_byte *bytes,
1913                                          offset_type elements)
1914 {
1915   offset_type i;
1916   htab_t sig_types_hash;
1917
1918   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1919   dwarf2_per_objfile->type_comp_units
1920     = obstack_alloc (&objfile->objfile_obstack,
1921                      dwarf2_per_objfile->n_type_comp_units
1922                      * sizeof (struct dwarf2_per_cu_data *));
1923
1924   sig_types_hash = allocate_signatured_type_table (objfile);
1925
1926   for (i = 0; i < elements; i += 3)
1927     {
1928       struct signatured_type *type_sig;
1929       ULONGEST offset, type_offset, signature;
1930       void **slot;
1931
1932       if (!extract_cu_value (bytes, &offset)
1933           || !extract_cu_value (bytes + 8, &type_offset))
1934         return 0;
1935       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936       bytes += 3 * 8;
1937
1938       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939                                  struct signatured_type);
1940       type_sig->signature = signature;
1941       type_sig->type_offset = type_offset;
1942       type_sig->per_cu.from_debug_types = 1;
1943       type_sig->per_cu.offset = offset;
1944       type_sig->per_cu.objfile = objfile;
1945       type_sig->per_cu.v.quick
1946         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947                           struct dwarf2_per_cu_quick_data);
1948
1949       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1950       *slot = type_sig;
1951
1952       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1953     }
1954
1955   dwarf2_per_objfile->signatured_types = sig_types_hash;
1956
1957   return 1;
1958 }
1959
1960 /* Read the address map data from the mapped index, and use it to
1961    populate the objfile's psymtabs_addrmap.  */
1962
1963 static void
1964 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965 {
1966   const gdb_byte *iter, *end;
1967   struct obstack temp_obstack;
1968   struct addrmap *mutable_map;
1969   struct cleanup *cleanup;
1970   CORE_ADDR baseaddr;
1971
1972   obstack_init (&temp_obstack);
1973   cleanup = make_cleanup_obstack_free (&temp_obstack);
1974   mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976   iter = index->address_table;
1977   end = iter + index->address_table_size;
1978
1979   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981   while (iter < end)
1982     {
1983       ULONGEST hi, lo, cu_index;
1984       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985       iter += 8;
1986       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987       iter += 8;
1988       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989       iter += 4;
1990       
1991       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1992                          dw2_get_cu (cu_index));
1993     }
1994
1995   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996                                                     &objfile->objfile_obstack);
1997   do_cleanups (cleanup);
1998 }
1999
2000 /* The hash function for strings in the mapped index.  This is the same as
2001    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002    implementation.  This is necessary because the hash function is tied to the
2003    format of the mapped index file.  The hash values do not have to match with
2004    SYMBOL_HASH_NEXT.
2005    
2006    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2007
2008 static hashval_t
2009 mapped_index_string_hash (int index_version, const void *p)
2010 {
2011   const unsigned char *str = (const unsigned char *) p;
2012   hashval_t r = 0;
2013   unsigned char c;
2014
2015   while ((c = *str++) != 0)
2016     {
2017       if (index_version >= 5)
2018         c = tolower (c);
2019       r = r * 67 + c - 113;
2020     }
2021
2022   return r;
2023 }
2024
2025 /* Find a slot in the mapped index INDEX for the object named NAME.
2026    If NAME is found, set *VEC_OUT to point to the CU vector in the
2027    constant pool and return 1.  If NAME cannot be found, return 0.  */
2028
2029 static int
2030 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031                           offset_type **vec_out)
2032 {
2033   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034   offset_type hash;
2035   offset_type slot, step;
2036   int (*cmp) (const char *, const char *);
2037
2038   if (current_language->la_language == language_cplus
2039       || current_language->la_language == language_java
2040       || current_language->la_language == language_fortran)
2041     {
2042       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2043          not contain any.  */
2044       const char *paren = strchr (name, '(');
2045
2046       if (paren)
2047         {
2048           char *dup;
2049
2050           dup = xmalloc (paren - name + 1);
2051           memcpy (dup, name, paren - name);
2052           dup[paren - name] = 0;
2053
2054           make_cleanup (xfree, dup);
2055           name = dup;
2056         }
2057     }
2058
2059   /* Index version 4 did not support case insensitive searches.  But the
2060      indexes for case insensitive languages are built in lowercase, therefore
2061      simulate our NAME being searched is also lowercased.  */
2062   hash = mapped_index_string_hash ((index->version == 4
2063                                     && case_sensitivity == case_sensitive_off
2064                                     ? 5 : index->version),
2065                                    name);
2066
2067   slot = hash & (index->symbol_table_slots - 1);
2068   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2069   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2070
2071   for (;;)
2072     {
2073       /* Convert a slot number to an offset into the table.  */
2074       offset_type i = 2 * slot;
2075       const char *str;
2076       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2077         {
2078           do_cleanups (back_to);
2079           return 0;
2080         }
2081
2082       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2083       if (!cmp (name, str))
2084         {
2085           *vec_out = (offset_type *) (index->constant_pool
2086                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2087           do_cleanups (back_to);
2088           return 1;
2089         }
2090
2091       slot = (slot + step) & (index->symbol_table_slots - 1);
2092     }
2093 }
2094
2095 /* Read the index file.  If everything went ok, initialize the "quick"
2096    elements of all the CUs and return 1.  Otherwise, return 0.  */
2097
2098 static int
2099 dwarf2_read_index (struct objfile *objfile)
2100 {
2101   char *addr;
2102   struct mapped_index *map;
2103   offset_type *metadata;
2104   const gdb_byte *cu_list;
2105   const gdb_byte *types_list = NULL;
2106   offset_type version, cu_list_elements;
2107   offset_type types_list_elements = 0;
2108   int i;
2109
2110   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2111     return 0;
2112
2113   /* Older elfutils strip versions could keep the section in the main
2114      executable while splitting it for the separate debug info file.  */
2115   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116        & SEC_HAS_CONTENTS) == 0)
2117     return 0;
2118
2119   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121   addr = dwarf2_per_objfile->gdb_index.buffer;
2122   /* Version check.  */
2123   version = MAYBE_SWAP (*(offset_type *) addr);
2124   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2125      causes the index to behave very poorly for certain requests.  Version 3
2126      contained incomplete addrmap.  So, it seems better to just ignore such
2127      indices.  Index version 4 uses a different hash function than index
2128      version 5 and later.  */
2129   if (version < 4)
2130     return 0;
2131   /* Indexes with higher version than the one supported by GDB may be no
2132      longer backward compatible.  */
2133   if (version > 5)
2134     return 0;
2135
2136   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2137   map->version = version;
2138   map->total_size = dwarf2_per_objfile->gdb_index.size;
2139
2140   metadata = (offset_type *) (addr + sizeof (offset_type));
2141
2142   i = 0;
2143   cu_list = addr + MAYBE_SWAP (metadata[i]);
2144   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2145                       / 8);
2146   ++i;
2147
2148   types_list = addr + MAYBE_SWAP (metadata[i]);
2149   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2150                           - MAYBE_SWAP (metadata[i]))
2151                          / 8);
2152   ++i;
2153
2154   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2155   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2156                              - MAYBE_SWAP (metadata[i]));
2157   ++i;
2158
2159   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2160   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2161                               - MAYBE_SWAP (metadata[i]))
2162                              / (2 * sizeof (offset_type)));
2163   ++i;
2164
2165   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2166
2167   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2168     return 0;
2169
2170   if (types_list_elements
2171       && !create_signatured_type_table_from_index (objfile, types_list,
2172                                                    types_list_elements))
2173     return 0;
2174
2175   create_addrmap_from_index (objfile, map);
2176
2177   dwarf2_per_objfile->index_table = map;
2178   dwarf2_per_objfile->using_index = 1;
2179   dwarf2_per_objfile->quick_file_names_table =
2180     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2181
2182   return 1;
2183 }
2184
2185 /* A helper for the "quick" functions which sets the global
2186    dwarf2_per_objfile according to OBJFILE.  */
2187
2188 static void
2189 dw2_setup (struct objfile *objfile)
2190 {
2191   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2192   gdb_assert (dwarf2_per_objfile);
2193 }
2194
2195 /* A helper for the "quick" functions which attempts to read the line
2196    table for THIS_CU.  */
2197
2198 static struct quick_file_names *
2199 dw2_get_file_names (struct objfile *objfile,
2200                     struct dwarf2_per_cu_data *this_cu)
2201 {
2202   bfd *abfd = objfile->obfd;
2203   struct line_header *lh;
2204   struct attribute *attr;
2205   struct cleanup *cleanups;
2206   struct die_info *comp_unit_die;
2207   struct dwarf2_section_info* sec;
2208   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2209   int has_children, i;
2210   struct dwarf2_cu cu;
2211   unsigned int bytes_read, buffer_size;
2212   struct die_reader_specs reader_specs;
2213   char *name, *comp_dir;
2214   void **slot;
2215   struct quick_file_names *qfn;
2216   unsigned int line_offset;
2217
2218   if (this_cu->v.quick->file_names != NULL)
2219     return this_cu->v.quick->file_names;
2220   /* If we know there is no line data, no point in looking again.  */
2221   if (this_cu->v.quick->no_file_data)
2222     return NULL;
2223
2224   init_one_comp_unit (&cu, objfile);
2225   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2226
2227   if (this_cu->from_debug_types)
2228     sec = &dwarf2_per_objfile->types;
2229   else
2230     sec = &dwarf2_per_objfile->info;
2231   dwarf2_read_section (objfile, sec);
2232   buffer_size = sec->size;
2233   buffer = sec->buffer;
2234   info_ptr = buffer + this_cu->offset;
2235   beg_of_comp_unit = info_ptr;
2236
2237   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2238                                           buffer, buffer_size,
2239                                           abfd);
2240
2241   /* Complete the cu_header.  */
2242   cu.header.offset = beg_of_comp_unit - buffer;
2243   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2244
2245   this_cu->cu = &cu;
2246   cu.per_cu = this_cu;
2247
2248   dwarf2_read_abbrevs (abfd, &cu);
2249   make_cleanup (dwarf2_free_abbrev_table, &cu);
2250
2251   if (this_cu->from_debug_types)
2252     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2253   init_cu_die_reader (&reader_specs, &cu);
2254   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2255                  &has_children);
2256
2257   lh = NULL;
2258   slot = NULL;
2259   line_offset = 0;
2260   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2261   if (attr)
2262     {
2263       struct quick_file_names find_entry;
2264
2265       line_offset = DW_UNSND (attr);
2266
2267       /* We may have already read in this line header (TU line header sharing).
2268          If we have we're done.  */
2269       find_entry.offset = line_offset;
2270       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2271                              &find_entry, INSERT);
2272       if (*slot != NULL)
2273         {
2274           do_cleanups (cleanups);
2275           this_cu->v.quick->file_names = *slot;
2276           return *slot;
2277         }
2278
2279       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2280     }
2281   if (lh == NULL)
2282     {
2283       do_cleanups (cleanups);
2284       this_cu->v.quick->no_file_data = 1;
2285       return NULL;
2286     }
2287
2288   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2289   qfn->offset = line_offset;
2290   gdb_assert (slot != NULL);
2291   *slot = qfn;
2292
2293   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2294
2295   qfn->num_file_names = lh->num_file_names;
2296   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2297                                    lh->num_file_names * sizeof (char *));
2298   for (i = 0; i < lh->num_file_names; ++i)
2299     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2300   qfn->real_names = NULL;
2301
2302   free_line_header (lh);
2303   do_cleanups (cleanups);
2304
2305   this_cu->v.quick->file_names = qfn;
2306   return qfn;
2307 }
2308
2309 /* A helper for the "quick" functions which computes and caches the
2310    real path for a given file name from the line table.  */
2311
2312 static const char *
2313 dw2_get_real_path (struct objfile *objfile,
2314                    struct quick_file_names *qfn, int index)
2315 {
2316   if (qfn->real_names == NULL)
2317     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2318                                       qfn->num_file_names, sizeof (char *));
2319
2320   if (qfn->real_names[index] == NULL)
2321     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2322
2323   return qfn->real_names[index];
2324 }
2325
2326 static struct symtab *
2327 dw2_find_last_source_symtab (struct objfile *objfile)
2328 {
2329   int index;
2330
2331   dw2_setup (objfile);
2332   index = dwarf2_per_objfile->n_comp_units - 1;
2333   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2334 }
2335
2336 /* Traversal function for dw2_forget_cached_source_info.  */
2337
2338 static int
2339 dw2_free_cached_file_names (void **slot, void *info)
2340 {
2341   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2342
2343   if (file_data->real_names)
2344     {
2345       int i;
2346
2347       for (i = 0; i < file_data->num_file_names; ++i)
2348         {
2349           xfree ((void*) file_data->real_names[i]);
2350           file_data->real_names[i] = NULL;
2351         }
2352     }
2353
2354   return 1;
2355 }
2356
2357 static void
2358 dw2_forget_cached_source_info (struct objfile *objfile)
2359 {
2360   dw2_setup (objfile);
2361
2362   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2363                           dw2_free_cached_file_names, NULL);
2364 }
2365
2366 static int
2367 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2368                    const char *full_path, const char *real_path,
2369                    struct symtab **result)
2370 {
2371   int i;
2372   int check_basename = lbasename (name) == name;
2373   struct dwarf2_per_cu_data *base_cu = NULL;
2374
2375   dw2_setup (objfile);
2376
2377   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2378                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2379     {
2380       int j;
2381       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2382       struct quick_file_names *file_data;
2383
2384       if (per_cu->v.quick->symtab)
2385         continue;
2386
2387       file_data = dw2_get_file_names (objfile, per_cu);
2388       if (file_data == NULL)
2389         continue;
2390
2391       for (j = 0; j < file_data->num_file_names; ++j)
2392         {
2393           const char *this_name = file_data->file_names[j];
2394
2395           if (FILENAME_CMP (name, this_name) == 0)
2396             {
2397               *result = dw2_instantiate_symtab (objfile, per_cu);
2398               return 1;
2399             }
2400
2401           if (check_basename && ! base_cu
2402               && FILENAME_CMP (lbasename (this_name), name) == 0)
2403             base_cu = per_cu;
2404
2405           if (full_path != NULL)
2406             {
2407               const char *this_real_name = dw2_get_real_path (objfile,
2408                                                               file_data, j);
2409
2410               if (this_real_name != NULL
2411                   && FILENAME_CMP (full_path, this_real_name) == 0)
2412                 {
2413                   *result = dw2_instantiate_symtab (objfile, per_cu);
2414                   return 1;
2415                 }
2416             }
2417
2418           if (real_path != NULL)
2419             {
2420               const char *this_real_name = dw2_get_real_path (objfile,
2421                                                               file_data, j);
2422
2423               if (this_real_name != NULL
2424                   && FILENAME_CMP (real_path, this_real_name) == 0)
2425                 {
2426                   *result = dw2_instantiate_symtab (objfile, per_cu);
2427                   return 1;
2428                 }
2429             }
2430         }
2431     }
2432
2433   if (base_cu)
2434     {
2435       *result = dw2_instantiate_symtab (objfile, base_cu);
2436       return 1;
2437     }
2438
2439   return 0;
2440 }
2441
2442 static struct symtab *
2443 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2444                    const char *name, domain_enum domain)
2445 {
2446   /* We do all the work in the pre_expand_symtabs_matching hook
2447      instead.  */
2448   return NULL;
2449 }
2450
2451 /* A helper function that expands all symtabs that hold an object
2452    named NAME.  */
2453
2454 static void
2455 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2456 {
2457   dw2_setup (objfile);
2458
2459   /* index_table is NULL if OBJF_READNOW.  */
2460   if (dwarf2_per_objfile->index_table)
2461     {
2462       offset_type *vec;
2463
2464       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2465                                     name, &vec))
2466         {
2467           offset_type i, len = MAYBE_SWAP (*vec);
2468           for (i = 0; i < len; ++i)
2469             {
2470               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2471               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2472
2473               dw2_instantiate_symtab (objfile, per_cu);
2474             }
2475         }
2476     }
2477 }
2478
2479 static void
2480 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2481                                  enum block_enum block_kind, const char *name,
2482                                  domain_enum domain)
2483 {
2484   dw2_do_expand_symtabs_matching (objfile, name);
2485 }
2486
2487 static void
2488 dw2_print_stats (struct objfile *objfile)
2489 {
2490   int i, count;
2491
2492   dw2_setup (objfile);
2493   count = 0;
2494   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2495                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2496     {
2497       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2498
2499       if (!per_cu->v.quick->symtab)
2500         ++count;
2501     }
2502   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2503 }
2504
2505 static void
2506 dw2_dump (struct objfile *objfile)
2507 {
2508   /* Nothing worth printing.  */
2509 }
2510
2511 static void
2512 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2513               struct section_offsets *delta)
2514 {
2515   /* There's nothing to relocate here.  */
2516 }
2517
2518 static void
2519 dw2_expand_symtabs_for_function (struct objfile *objfile,
2520                                  const char *func_name)
2521 {
2522   dw2_do_expand_symtabs_matching (objfile, func_name);
2523 }
2524
2525 static void
2526 dw2_expand_all_symtabs (struct objfile *objfile)
2527 {
2528   int i;
2529
2530   dw2_setup (objfile);
2531
2532   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2533                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2534     {
2535       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2536
2537       dw2_instantiate_symtab (objfile, per_cu);
2538     }
2539 }
2540
2541 static void
2542 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2543                                   const char *filename)
2544 {
2545   int i;
2546
2547   dw2_setup (objfile);
2548
2549   /* We don't need to consider type units here.
2550      This is only called for examining code, e.g. expand_line_sal.
2551      There can be an order of magnitude (or more) more type units
2552      than comp units, and we avoid them if we can.  */
2553
2554   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2555     {
2556       int j;
2557       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2558       struct quick_file_names *file_data;
2559
2560       if (per_cu->v.quick->symtab)
2561         continue;
2562
2563       file_data = dw2_get_file_names (objfile, per_cu);
2564       if (file_data == NULL)
2565         continue;
2566
2567       for (j = 0; j < file_data->num_file_names; ++j)
2568         {
2569           const char *this_name = file_data->file_names[j];
2570           if (FILENAME_CMP (this_name, filename) == 0)
2571             {
2572               dw2_instantiate_symtab (objfile, per_cu);
2573               break;
2574             }
2575         }
2576     }
2577 }
2578
2579 static const char *
2580 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2581 {
2582   struct dwarf2_per_cu_data *per_cu;
2583   offset_type *vec;
2584   struct quick_file_names *file_data;
2585
2586   dw2_setup (objfile);
2587
2588   /* index_table is NULL if OBJF_READNOW.  */
2589   if (!dwarf2_per_objfile->index_table)
2590     return NULL;
2591
2592   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2593                                  name, &vec))
2594     return NULL;
2595
2596   /* Note that this just looks at the very first one named NAME -- but
2597      actually we are looking for a function.  find_main_filename
2598      should be rewritten so that it doesn't require a custom hook.  It
2599      could just use the ordinary symbol tables.  */
2600   /* vec[0] is the length, which must always be >0.  */
2601   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2602
2603   file_data = dw2_get_file_names (objfile, per_cu);
2604   if (file_data == NULL)
2605     return NULL;
2606
2607   return file_data->file_names[file_data->num_file_names - 1];
2608 }
2609
2610 static void
2611 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2612                           struct objfile *objfile, int global,
2613                           int (*callback) (struct block *,
2614                                            struct symbol *, void *),
2615                           void *data, symbol_compare_ftype *match,
2616                           symbol_compare_ftype *ordered_compare)
2617 {
2618   /* Currently unimplemented; used for Ada.  The function can be called if the
2619      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2620      does not look for non-Ada symbols this function should just return.  */
2621 }
2622
2623 static void
2624 dw2_expand_symtabs_matching (struct objfile *objfile,
2625                              int (*file_matcher) (const char *, void *),
2626                              int (*name_matcher) (const char *, void *),
2627                              enum search_domain kind,
2628                              void *data)
2629 {
2630   int i;
2631   offset_type iter;
2632   struct mapped_index *index;
2633
2634   dw2_setup (objfile);
2635
2636   /* index_table is NULL if OBJF_READNOW.  */
2637   if (!dwarf2_per_objfile->index_table)
2638     return;
2639   index = dwarf2_per_objfile->index_table;
2640
2641   if (file_matcher != NULL)
2642     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2643                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2644       {
2645         int j;
2646         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647         struct quick_file_names *file_data;
2648
2649         per_cu->v.quick->mark = 0;
2650         if (per_cu->v.quick->symtab)
2651           continue;
2652
2653         file_data = dw2_get_file_names (objfile, per_cu);
2654         if (file_data == NULL)
2655           continue;
2656
2657         for (j = 0; j < file_data->num_file_names; ++j)
2658           {
2659             if (file_matcher (file_data->file_names[j], data))
2660               {
2661                 per_cu->v.quick->mark = 1;
2662                 break;
2663               }
2664           }
2665       }
2666
2667   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2668     {
2669       offset_type idx = 2 * iter;
2670       const char *name;
2671       offset_type *vec, vec_len, vec_idx;
2672
2673       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2674         continue;
2675
2676       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2677
2678       if (! (*name_matcher) (name, data))
2679         continue;
2680
2681       /* The name was matched, now expand corresponding CUs that were
2682          marked.  */
2683       vec = (offset_type *) (index->constant_pool
2684                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2685       vec_len = MAYBE_SWAP (vec[0]);
2686       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2687         {
2688           struct dwarf2_per_cu_data *per_cu;
2689
2690           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2691           if (file_matcher == NULL || per_cu->v.quick->mark)
2692             dw2_instantiate_symtab (objfile, per_cu);
2693         }
2694     }
2695 }
2696
2697 static struct symtab *
2698 dw2_find_pc_sect_symtab (struct objfile *objfile,
2699                          struct minimal_symbol *msymbol,
2700                          CORE_ADDR pc,
2701                          struct obj_section *section,
2702                          int warn_if_readin)
2703 {
2704   struct dwarf2_per_cu_data *data;
2705
2706   dw2_setup (objfile);
2707
2708   if (!objfile->psymtabs_addrmap)
2709     return NULL;
2710
2711   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2712   if (!data)
2713     return NULL;
2714
2715   if (warn_if_readin && data->v.quick->symtab)
2716     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2717              paddress (get_objfile_arch (objfile), pc));
2718
2719   return dw2_instantiate_symtab (objfile, data);
2720 }
2721
2722 static void
2723 dw2_map_symbol_filenames (struct objfile *objfile,
2724                           void (*fun) (const char *, const char *, void *),
2725                           void *data)
2726 {
2727   int i;
2728
2729   dw2_setup (objfile);
2730
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       if (per_cu->v.quick->symtab)
2739         continue;
2740
2741       file_data = dw2_get_file_names (objfile, per_cu);
2742       if (file_data == NULL)
2743         continue;
2744
2745       for (j = 0; j < file_data->num_file_names; ++j)
2746         {
2747           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2748                                                           j);
2749           (*fun) (file_data->file_names[j], this_real_name, data);
2750         }
2751     }
2752 }
2753
2754 static int
2755 dw2_has_symbols (struct objfile *objfile)
2756 {
2757   return 1;
2758 }
2759
2760 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2761 {
2762   dw2_has_symbols,
2763   dw2_find_last_source_symtab,
2764   dw2_forget_cached_source_info,
2765   dw2_lookup_symtab,
2766   dw2_lookup_symbol,
2767   dw2_pre_expand_symtabs_matching,
2768   dw2_print_stats,
2769   dw2_dump,
2770   dw2_relocate,
2771   dw2_expand_symtabs_for_function,
2772   dw2_expand_all_symtabs,
2773   dw2_expand_symtabs_with_filename,
2774   dw2_find_symbol_file,
2775   dw2_map_matching_symbols,
2776   dw2_expand_symtabs_matching,
2777   dw2_find_pc_sect_symtab,
2778   dw2_map_symbol_filenames
2779 };
2780
2781 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2782    file will use psymtabs, or 1 if using the GNU index.  */
2783
2784 int
2785 dwarf2_initialize_objfile (struct objfile *objfile)
2786 {
2787   /* If we're about to read full symbols, don't bother with the
2788      indices.  In this case we also don't care if some other debug
2789      format is making psymtabs, because they are all about to be
2790      expanded anyway.  */
2791   if ((objfile->flags & OBJF_READNOW))
2792     {
2793       int i;
2794
2795       dwarf2_per_objfile->using_index = 1;
2796       create_all_comp_units (objfile);
2797       create_debug_types_hash_table (objfile);
2798       dwarf2_per_objfile->quick_file_names_table =
2799         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2800
2801       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2802                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2803         {
2804           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2805
2806           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2807                                             struct dwarf2_per_cu_quick_data);
2808         }
2809
2810       /* Return 1 so that gdb sees the "quick" functions.  However,
2811          these functions will be no-ops because we will have expanded
2812          all symtabs.  */
2813       return 1;
2814     }
2815
2816   if (dwarf2_read_index (objfile))
2817     return 1;
2818
2819   return 0;
2820 }
2821
2822 \f
2823
2824 /* Build a partial symbol table.  */
2825
2826 void
2827 dwarf2_build_psymtabs (struct objfile *objfile)
2828 {
2829   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2830     {
2831       init_psymbol_list (objfile, 1024);
2832     }
2833
2834   dwarf2_build_psymtabs_hard (objfile);
2835 }
2836
2837 /* Return TRUE if OFFSET is within CU_HEADER.  */
2838
2839 static inline int
2840 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2841 {
2842   unsigned int bottom = cu_header->offset;
2843   unsigned int top = (cu_header->offset
2844                       + cu_header->length
2845                       + cu_header->initial_length_size);
2846
2847   return (offset >= bottom && offset < top);
2848 }
2849
2850 /* Read in the comp unit header information from the debug_info at info_ptr.
2851    NOTE: This leaves members offset, first_die_offset to be filled in
2852    by the caller.  */
2853
2854 static gdb_byte *
2855 read_comp_unit_head (struct comp_unit_head *cu_header,
2856                      gdb_byte *info_ptr, bfd *abfd)
2857 {
2858   int signed_addr;
2859   unsigned int bytes_read;
2860
2861   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2862   cu_header->initial_length_size = bytes_read;
2863   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2864   info_ptr += bytes_read;
2865   cu_header->version = read_2_bytes (abfd, info_ptr);
2866   info_ptr += 2;
2867   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2868                                           &bytes_read);
2869   info_ptr += bytes_read;
2870   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2871   info_ptr += 1;
2872   signed_addr = bfd_get_sign_extend_vma (abfd);
2873   if (signed_addr < 0)
2874     internal_error (__FILE__, __LINE__,
2875                     _("read_comp_unit_head: dwarf from non elf file"));
2876   cu_header->signed_addr_p = signed_addr;
2877
2878   return info_ptr;
2879 }
2880
2881 static gdb_byte *
2882 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2883                              gdb_byte *buffer, unsigned int buffer_size,
2884                              bfd *abfd)
2885 {
2886   gdb_byte *beg_of_comp_unit = info_ptr;
2887
2888   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2889
2890   if (header->version != 2 && header->version != 3 && header->version != 4)
2891     error (_("Dwarf Error: wrong version in compilation unit header "
2892            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2893            bfd_get_filename (abfd));
2894
2895   if (header->abbrev_offset
2896       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2897                               &dwarf2_per_objfile->abbrev))
2898     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2899            "(offset 0x%lx + 6) [in module %s]"),
2900            (long) header->abbrev_offset,
2901            (long) (beg_of_comp_unit - buffer),
2902            bfd_get_filename (abfd));
2903
2904   if (beg_of_comp_unit + header->length + header->initial_length_size
2905       > buffer + buffer_size)
2906     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2907            "(offset 0x%lx + 0) [in module %s]"),
2908            (long) header->length,
2909            (long) (beg_of_comp_unit - buffer),
2910            bfd_get_filename (abfd));
2911
2912   return info_ptr;
2913 }
2914
2915 /* Read in the types comp unit header information from .debug_types entry at
2916    types_ptr.  The result is a pointer to one past the end of the header.  */
2917
2918 static gdb_byte *
2919 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2920                           ULONGEST *signature,
2921                           gdb_byte *types_ptr, bfd *abfd)
2922 {
2923   gdb_byte *initial_types_ptr = types_ptr;
2924
2925   dwarf2_read_section (dwarf2_per_objfile->objfile,
2926                        &dwarf2_per_objfile->types);
2927   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2928
2929   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2930
2931   *signature = read_8_bytes (abfd, types_ptr);
2932   types_ptr += 8;
2933   types_ptr += cu_header->offset_size;
2934   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2935
2936   return types_ptr;
2937 }
2938
2939 /* Allocate a new partial symtab for file named NAME and mark this new
2940    partial symtab as being an include of PST.  */
2941
2942 static void
2943 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2944                                struct objfile *objfile)
2945 {
2946   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2947
2948   subpst->section_offsets = pst->section_offsets;
2949   subpst->textlow = 0;
2950   subpst->texthigh = 0;
2951
2952   subpst->dependencies = (struct partial_symtab **)
2953     obstack_alloc (&objfile->objfile_obstack,
2954                    sizeof (struct partial_symtab *));
2955   subpst->dependencies[0] = pst;
2956   subpst->number_of_dependencies = 1;
2957
2958   subpst->globals_offset = 0;
2959   subpst->n_global_syms = 0;
2960   subpst->statics_offset = 0;
2961   subpst->n_static_syms = 0;
2962   subpst->symtab = NULL;
2963   subpst->read_symtab = pst->read_symtab;
2964   subpst->readin = 0;
2965
2966   /* No private part is necessary for include psymtabs.  This property
2967      can be used to differentiate between such include psymtabs and
2968      the regular ones.  */
2969   subpst->read_symtab_private = NULL;
2970 }
2971
2972 /* Read the Line Number Program data and extract the list of files
2973    included by the source file represented by PST.  Build an include
2974    partial symtab for each of these included files.  */
2975
2976 static void
2977 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2978                                struct die_info *die,
2979                                struct partial_symtab *pst)
2980 {
2981   struct objfile *objfile = cu->objfile;
2982   bfd *abfd = objfile->obfd;
2983   struct line_header *lh = NULL;
2984   struct attribute *attr;
2985
2986   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2987   if (attr)
2988     {
2989       unsigned int line_offset = DW_UNSND (attr);
2990
2991       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2992     }
2993   if (lh == NULL)
2994     return;  /* No linetable, so no includes.  */
2995
2996   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2997   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2998
2999   free_line_header (lh);
3000 }
3001
3002 static hashval_t
3003 hash_type_signature (const void *item)
3004 {
3005   const struct signatured_type *type_sig = item;
3006
3007   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3008   return type_sig->signature;
3009 }
3010
3011 static int
3012 eq_type_signature (const void *item_lhs, const void *item_rhs)
3013 {
3014   const struct signatured_type *lhs = item_lhs;
3015   const struct signatured_type *rhs = item_rhs;
3016
3017   return lhs->signature == rhs->signature;
3018 }
3019
3020 /* Allocate a hash table for signatured types.  */
3021
3022 static htab_t
3023 allocate_signatured_type_table (struct objfile *objfile)
3024 {
3025   return htab_create_alloc_ex (41,
3026                                hash_type_signature,
3027                                eq_type_signature,
3028                                NULL,
3029                                &objfile->objfile_obstack,
3030                                hashtab_obstack_allocate,
3031                                dummy_obstack_deallocate);
3032 }
3033
3034 /* A helper function to add a signatured type CU to a list.  */
3035
3036 static int
3037 add_signatured_type_cu_to_list (void **slot, void *datum)
3038 {
3039   struct signatured_type *sigt = *slot;
3040   struct dwarf2_per_cu_data ***datap = datum;
3041
3042   **datap = &sigt->per_cu;
3043   ++*datap;
3044
3045   return 1;
3046 }
3047
3048 /* Create the hash table of all entries in the .debug_types section.
3049    The result is zero if there is an error (e.g. missing .debug_types section),
3050    otherwise non-zero.  */
3051
3052 static int
3053 create_debug_types_hash_table (struct objfile *objfile)
3054 {
3055   gdb_byte *info_ptr;
3056   htab_t types_htab;
3057   struct dwarf2_per_cu_data **iter;
3058
3059   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3060   info_ptr = dwarf2_per_objfile->types.buffer;
3061
3062   if (info_ptr == NULL)
3063     {
3064       dwarf2_per_objfile->signatured_types = NULL;
3065       return 0;
3066     }
3067
3068   types_htab = allocate_signatured_type_table (objfile);
3069
3070   if (dwarf2_die_debug)
3071     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3072
3073   while (info_ptr < dwarf2_per_objfile->types.buffer
3074          + dwarf2_per_objfile->types.size)
3075     {
3076       unsigned int offset;
3077       unsigned int offset_size;
3078       unsigned int type_offset;
3079       unsigned int length, initial_length_size;
3080       unsigned short version;
3081       ULONGEST signature;
3082       struct signatured_type *type_sig;
3083       void **slot;
3084       gdb_byte *ptr = info_ptr;
3085
3086       offset = ptr - dwarf2_per_objfile->types.buffer;
3087
3088       /* We need to read the type's signature in order to build the hash
3089          table, but we don't need to read anything else just yet.  */
3090
3091       /* Sanity check to ensure entire cu is present.  */
3092       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3093       if (ptr + length + initial_length_size
3094           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3095         {
3096           complaint (&symfile_complaints,
3097                      _("debug type entry runs off end "
3098                        "of `.debug_types' section, ignored"));
3099           break;
3100         }
3101
3102       offset_size = initial_length_size == 4 ? 4 : 8;
3103       ptr += initial_length_size;
3104       version = bfd_get_16 (objfile->obfd, ptr);
3105       ptr += 2;
3106       ptr += offset_size; /* abbrev offset */
3107       ptr += 1; /* address size */
3108       signature = bfd_get_64 (objfile->obfd, ptr);
3109       ptr += 8;
3110       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3111
3112       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3113       memset (type_sig, 0, sizeof (*type_sig));
3114       type_sig->signature = signature;
3115       type_sig->type_offset = type_offset;
3116       type_sig->per_cu.objfile = objfile;
3117       type_sig->per_cu.from_debug_types = 1;
3118       type_sig->per_cu.offset = offset;
3119
3120       slot = htab_find_slot (types_htab, type_sig, INSERT);
3121       gdb_assert (slot != NULL);
3122       if (*slot != NULL)
3123         {
3124           const struct signatured_type *dup_sig = *slot;
3125
3126           complaint (&symfile_complaints,
3127                      _("debug type entry at offset 0x%x is duplicate to the "
3128                        "entry at offset 0x%x, signature 0x%s"),
3129                      offset, dup_sig->per_cu.offset,
3130                      phex (signature, sizeof (signature)));
3131           gdb_assert (signature == dup_sig->signature);
3132         }
3133       *slot = type_sig;
3134
3135       if (dwarf2_die_debug)
3136         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3137                             offset, phex (signature, sizeof (signature)));
3138
3139       info_ptr = info_ptr + initial_length_size + length;
3140     }
3141
3142   dwarf2_per_objfile->signatured_types = types_htab;
3143
3144   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3145   dwarf2_per_objfile->type_comp_units
3146     = obstack_alloc (&objfile->objfile_obstack,
3147                      dwarf2_per_objfile->n_type_comp_units
3148                      * sizeof (struct dwarf2_per_cu_data *));
3149   iter = &dwarf2_per_objfile->type_comp_units[0];
3150   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3151   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3152               == dwarf2_per_objfile->n_type_comp_units);
3153
3154   return 1;
3155 }
3156
3157 /* Lookup a signature based type.
3158    Returns NULL if SIG is not present in the table.  */
3159
3160 static struct signatured_type *
3161 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3162 {
3163   struct signatured_type find_entry, *entry;
3164
3165   if (dwarf2_per_objfile->signatured_types == NULL)
3166     {
3167       complaint (&symfile_complaints,
3168                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3169       return 0;
3170     }
3171
3172   find_entry.signature = sig;
3173   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3174   return entry;
3175 }
3176
3177 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3178
3179 static void
3180 init_cu_die_reader (struct die_reader_specs *reader,
3181                     struct dwarf2_cu *cu)
3182 {
3183   reader->abfd = cu->objfile->obfd;
3184   reader->cu = cu;
3185   if (cu->per_cu->from_debug_types)
3186     {
3187       gdb_assert (dwarf2_per_objfile->types.readin);
3188       reader->buffer = dwarf2_per_objfile->types.buffer;
3189     }
3190   else
3191     {
3192       gdb_assert (dwarf2_per_objfile->info.readin);
3193       reader->buffer = dwarf2_per_objfile->info.buffer;
3194     }
3195 }
3196
3197 /* Find the base address of the compilation unit for range lists and
3198    location lists.  It will normally be specified by DW_AT_low_pc.
3199    In DWARF-3 draft 4, the base address could be overridden by
3200    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3201    compilation units with discontinuous ranges.  */
3202
3203 static void
3204 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3205 {
3206   struct attribute *attr;
3207
3208   cu->base_known = 0;
3209   cu->base_address = 0;
3210
3211   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3212   if (attr)
3213     {
3214       cu->base_address = DW_ADDR (attr);
3215       cu->base_known = 1;
3216     }
3217   else
3218     {
3219       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3220       if (attr)
3221         {
3222           cu->base_address = DW_ADDR (attr);
3223           cu->base_known = 1;
3224         }
3225     }
3226 }
3227
3228 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3229    to combine the common parts.
3230    Process a compilation unit for a psymtab.
3231    BUFFER is a pointer to the beginning of the dwarf section buffer,
3232    either .debug_info or debug_types.
3233    INFO_PTR is a pointer to the start of the CU.
3234    Returns a pointer to the next CU.  */
3235
3236 static gdb_byte *
3237 process_psymtab_comp_unit (struct objfile *objfile,
3238                            struct dwarf2_per_cu_data *this_cu,
3239                            gdb_byte *buffer, gdb_byte *info_ptr,
3240                            unsigned int buffer_size)
3241 {
3242   bfd *abfd = objfile->obfd;
3243   gdb_byte *beg_of_comp_unit = info_ptr;
3244   struct die_info *comp_unit_die;
3245   struct partial_symtab *pst;
3246   CORE_ADDR baseaddr;
3247   struct cleanup *back_to_inner;
3248   struct dwarf2_cu cu;
3249   int has_children, has_pc_info;
3250   struct attribute *attr;
3251   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3252   struct die_reader_specs reader_specs;
3253   const char *filename;
3254
3255   init_one_comp_unit (&cu, objfile);
3256   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3257
3258   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3259                                           buffer, buffer_size,
3260                                           abfd);
3261
3262   /* Complete the cu_header.  */
3263   cu.header.offset = beg_of_comp_unit - buffer;
3264   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3265
3266   cu.list_in_scope = &file_symbols;
3267
3268   /* If this compilation unit was already read in, free the
3269      cached copy in order to read it in again.  This is
3270      necessary because we skipped some symbols when we first
3271      read in the compilation unit (see load_partial_dies).
3272      This problem could be avoided, but the benefit is
3273      unclear.  */
3274   if (this_cu->cu != NULL)
3275     free_one_cached_comp_unit (this_cu->cu);
3276
3277   /* Note that this is a pointer to our stack frame, being
3278      added to a global data structure.  It will be cleaned up
3279      in free_stack_comp_unit when we finish with this
3280      compilation unit.  */
3281   this_cu->cu = &cu;
3282   cu.per_cu = this_cu;
3283
3284   /* Read the abbrevs for this compilation unit into a table.  */
3285   dwarf2_read_abbrevs (abfd, &cu);
3286   make_cleanup (dwarf2_free_abbrev_table, &cu);
3287
3288   /* Read the compilation unit die.  */
3289   if (this_cu->from_debug_types)
3290     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3291   init_cu_die_reader (&reader_specs, &cu);
3292   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3293                             &has_children);
3294
3295   if (this_cu->from_debug_types)
3296     {
3297       /* LENGTH has not been set yet for type units.  */
3298       gdb_assert (this_cu->offset == cu.header.offset);
3299       this_cu->length = cu.header.length + cu.header.initial_length_size;
3300     }
3301   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3302     {
3303       info_ptr = (beg_of_comp_unit + cu.header.length
3304                   + cu.header.initial_length_size);
3305       do_cleanups (back_to_inner);
3306       return info_ptr;
3307     }
3308
3309   prepare_one_comp_unit (&cu, comp_unit_die);
3310
3311   /* Allocate a new partial symbol table structure.  */
3312   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3313   if (attr == NULL || !DW_STRING (attr))
3314     filename = "";
3315   else
3316     filename = DW_STRING (attr);
3317   pst = start_psymtab_common (objfile, objfile->section_offsets,
3318                               filename,
3319                               /* TEXTLOW and TEXTHIGH are set below.  */
3320                               0,
3321                               objfile->global_psymbols.next,
3322                               objfile->static_psymbols.next);
3323
3324   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3325   if (attr != NULL)
3326     pst->dirname = DW_STRING (attr);
3327
3328   pst->read_symtab_private = this_cu;
3329
3330   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3331
3332   /* Store the function that reads in the rest of the symbol table.  */
3333   pst->read_symtab = dwarf2_psymtab_to_symtab;
3334
3335   this_cu->v.psymtab = pst;
3336
3337   dwarf2_find_base_address (comp_unit_die, &cu);
3338
3339   /* Possibly set the default values of LOWPC and HIGHPC from
3340      `DW_AT_ranges'.  */
3341   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3342                                       &best_highpc, &cu, pst);
3343   if (has_pc_info == 1 && best_lowpc < best_highpc)
3344     /* Store the contiguous range if it is not empty; it can be empty for
3345        CUs with no code.  */
3346     addrmap_set_empty (objfile->psymtabs_addrmap,
3347                        best_lowpc + baseaddr,
3348                        best_highpc + baseaddr - 1, pst);
3349
3350   /* Check if comp unit has_children.
3351      If so, read the rest of the partial symbols from this comp unit.
3352      If not, there's no more debug_info for this comp unit.  */
3353   if (has_children)
3354     {
3355       struct partial_die_info *first_die;
3356       CORE_ADDR lowpc, highpc;
3357
3358       lowpc = ((CORE_ADDR) -1);
3359       highpc = ((CORE_ADDR) 0);
3360
3361       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3362
3363       scan_partial_symbols (first_die, &lowpc, &highpc,
3364                             ! has_pc_info, &cu);
3365
3366       /* If we didn't find a lowpc, set it to highpc to avoid
3367          complaints from `maint check'.  */
3368       if (lowpc == ((CORE_ADDR) -1))
3369         lowpc = highpc;
3370
3371       /* If the compilation unit didn't have an explicit address range,
3372          then use the information extracted from its child dies.  */
3373       if (! has_pc_info)
3374         {
3375           best_lowpc = lowpc;
3376           best_highpc = highpc;
3377         }
3378     }
3379   pst->textlow = best_lowpc + baseaddr;
3380   pst->texthigh = best_highpc + baseaddr;
3381
3382   pst->n_global_syms = objfile->global_psymbols.next -
3383     (objfile->global_psymbols.list + pst->globals_offset);
3384   pst->n_static_syms = objfile->static_psymbols.next -
3385     (objfile->static_psymbols.list + pst->statics_offset);
3386   sort_pst_symbols (pst);
3387
3388   info_ptr = (beg_of_comp_unit + cu.header.length
3389               + cu.header.initial_length_size);
3390
3391   if (this_cu->from_debug_types)
3392     {
3393       /* It's not clear we want to do anything with stmt lists here.
3394          Waiting to see what gcc ultimately does.  */
3395     }
3396   else
3397     {
3398       /* Get the list of files included in the current compilation unit,
3399          and build a psymtab for each of them.  */
3400       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3401     }
3402
3403   do_cleanups (back_to_inner);
3404
3405   return info_ptr;
3406 }
3407
3408 /* Traversal function for htab_traverse_noresize.
3409    Process one .debug_types comp-unit.  */
3410
3411 static int
3412 process_type_comp_unit (void **slot, void *info)
3413 {
3414   struct signatured_type *entry = (struct signatured_type *) *slot;
3415   struct objfile *objfile = (struct objfile *) info;
3416   struct dwarf2_per_cu_data *this_cu;
3417
3418   this_cu = &entry->per_cu;
3419
3420   gdb_assert (dwarf2_per_objfile->types.readin);
3421   process_psymtab_comp_unit (objfile, this_cu,
3422                              dwarf2_per_objfile->types.buffer,
3423                              dwarf2_per_objfile->types.buffer + this_cu->offset,
3424                              dwarf2_per_objfile->types.size);
3425
3426   return 1;
3427 }
3428
3429 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3430    Build partial symbol tables for the .debug_types comp-units.  */
3431
3432 static void
3433 build_type_psymtabs (struct objfile *objfile)
3434 {
3435   if (! create_debug_types_hash_table (objfile))
3436     return;
3437
3438   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3439                           process_type_comp_unit, objfile);
3440 }
3441
3442 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3443
3444 static void
3445 psymtabs_addrmap_cleanup (void *o)
3446 {
3447   struct objfile *objfile = o;
3448
3449   objfile->psymtabs_addrmap = NULL;
3450 }
3451
3452 /* Build the partial symbol table by doing a quick pass through the
3453    .debug_info and .debug_abbrev sections.  */
3454
3455 static void
3456 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3457 {
3458   gdb_byte *info_ptr;
3459   struct cleanup *back_to, *addrmap_cleanup;
3460   struct obstack temp_obstack;
3461
3462   dwarf2_per_objfile->reading_partial_symbols = 1;
3463
3464   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3465   info_ptr = dwarf2_per_objfile->info.buffer;
3466
3467   /* Any cached compilation units will be linked by the per-objfile
3468      read_in_chain.  Make sure to free them when we're done.  */
3469   back_to = make_cleanup (free_cached_comp_units, NULL);
3470
3471   build_type_psymtabs (objfile);
3472
3473   create_all_comp_units (objfile);
3474
3475   /* Create a temporary address map on a temporary obstack.  We later
3476      copy this to the final obstack.  */
3477   obstack_init (&temp_obstack);
3478   make_cleanup_obstack_free (&temp_obstack);
3479   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3480   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3481
3482   /* Since the objects we're extracting from .debug_info vary in
3483      length, only the individual functions to extract them (like
3484      read_comp_unit_head and load_partial_die) can really know whether
3485      the buffer is large enough to hold another complete object.
3486
3487      At the moment, they don't actually check that.  If .debug_info
3488      holds just one extra byte after the last compilation unit's dies,
3489      then read_comp_unit_head will happily read off the end of the
3490      buffer.  read_partial_die is similarly casual.  Those functions
3491      should be fixed.
3492
3493      For this loop condition, simply checking whether there's any data
3494      left at all should be sufficient.  */
3495
3496   while (info_ptr < (dwarf2_per_objfile->info.buffer
3497                      + dwarf2_per_objfile->info.size))
3498     {
3499       struct dwarf2_per_cu_data *this_cu;
3500
3501       this_cu = dwarf2_find_comp_unit (info_ptr
3502                                        - dwarf2_per_objfile->info.buffer,
3503                                        objfile);
3504
3505       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3506                                             dwarf2_per_objfile->info.buffer,
3507                                             info_ptr,
3508                                             dwarf2_per_objfile->info.size);
3509     }
3510
3511   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3512                                                     &objfile->objfile_obstack);
3513   discard_cleanups (addrmap_cleanup);
3514
3515   do_cleanups (back_to);
3516 }
3517
3518 /* Load the partial DIEs for a secondary CU into memory.  */
3519
3520 static void
3521 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3522                         struct objfile *objfile)
3523 {
3524   bfd *abfd = objfile->obfd;
3525   gdb_byte *info_ptr, *beg_of_comp_unit;
3526   struct die_info *comp_unit_die;
3527   struct dwarf2_cu *cu;
3528   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3529   int has_children;
3530   struct die_reader_specs reader_specs;
3531   int read_cu = 0;
3532
3533   gdb_assert (! this_cu->from_debug_types);
3534
3535   gdb_assert (dwarf2_per_objfile->info.readin);
3536   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3537   beg_of_comp_unit = info_ptr;
3538
3539   if (this_cu->cu == NULL)
3540     {
3541       cu = xmalloc (sizeof (*cu));
3542       init_one_comp_unit (cu, objfile);
3543
3544       read_cu = 1;
3545
3546       /* If an error occurs while loading, release our storage.  */
3547       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3548
3549       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3550                                               dwarf2_per_objfile->info.buffer,
3551                                               dwarf2_per_objfile->info.size,
3552                                               abfd);
3553
3554       /* Complete the cu_header.  */
3555       cu->header.offset = this_cu->offset;
3556       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3557
3558       /* Link this compilation unit into the compilation unit tree.  */
3559       this_cu->cu = cu;
3560       cu->per_cu = this_cu;
3561
3562       /* Link this CU into read_in_chain.  */
3563       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3564       dwarf2_per_objfile->read_in_chain = this_cu;
3565     }
3566   else
3567     {
3568       cu = this_cu->cu;
3569       info_ptr += cu->header.first_die_offset;
3570     }
3571
3572   /* Read the abbrevs for this compilation unit into a table.  */
3573   gdb_assert (cu->dwarf2_abbrevs == NULL);
3574   dwarf2_read_abbrevs (abfd, cu);
3575   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3576
3577   /* Read the compilation unit die.  */
3578   init_cu_die_reader (&reader_specs, cu);
3579   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3580                             &has_children);
3581
3582   prepare_one_comp_unit (cu, comp_unit_die);
3583
3584   /* Check if comp unit has_children.
3585      If so, read the rest of the partial symbols from this comp unit.
3586      If not, there's no more debug_info for this comp unit.  */
3587   if (has_children)
3588     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3589
3590   do_cleanups (free_abbrevs_cleanup);
3591
3592   if (read_cu)
3593     {
3594       /* We've successfully allocated this compilation unit.  Let our
3595          caller clean it up when finished with it.  */
3596       discard_cleanups (free_cu_cleanup);
3597     }
3598 }
3599
3600 /* Create a list of all compilation units in OBJFILE.  We do this only
3601    if an inter-comp-unit reference is found; presumably if there is one,
3602    there will be many, and one will occur early in the .debug_info section.
3603    So there's no point in building this list incrementally.  */
3604
3605 static void
3606 create_all_comp_units (struct objfile *objfile)
3607 {
3608   int n_allocated;
3609   int n_comp_units;
3610   struct dwarf2_per_cu_data **all_comp_units;
3611   gdb_byte *info_ptr;
3612
3613   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3614   info_ptr = dwarf2_per_objfile->info.buffer;
3615
3616   n_comp_units = 0;
3617   n_allocated = 10;
3618   all_comp_units = xmalloc (n_allocated
3619                             * sizeof (struct dwarf2_per_cu_data *));
3620
3621   while (info_ptr < dwarf2_per_objfile->info.buffer
3622          + dwarf2_per_objfile->info.size)
3623     {
3624       unsigned int length, initial_length_size;
3625       struct dwarf2_per_cu_data *this_cu;
3626       unsigned int offset;
3627
3628       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3629
3630       /* Read just enough information to find out where the next
3631          compilation unit is.  */
3632       length = read_initial_length (objfile->obfd, info_ptr,
3633                                     &initial_length_size);
3634
3635       /* Save the compilation unit for later lookup.  */
3636       this_cu = obstack_alloc (&objfile->objfile_obstack,
3637                                sizeof (struct dwarf2_per_cu_data));
3638       memset (this_cu, 0, sizeof (*this_cu));
3639       this_cu->offset = offset;
3640       this_cu->length = length + initial_length_size;
3641       this_cu->objfile = objfile;
3642
3643       if (n_comp_units == n_allocated)
3644         {
3645           n_allocated *= 2;
3646           all_comp_units = xrealloc (all_comp_units,
3647                                      n_allocated
3648                                      * sizeof (struct dwarf2_per_cu_data *));
3649         }
3650       all_comp_units[n_comp_units++] = this_cu;
3651
3652       info_ptr = info_ptr + this_cu->length;
3653     }
3654
3655   dwarf2_per_objfile->all_comp_units
3656     = obstack_alloc (&objfile->objfile_obstack,
3657                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3658   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3659           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3660   xfree (all_comp_units);
3661   dwarf2_per_objfile->n_comp_units = n_comp_units;
3662 }
3663
3664 /* Process all loaded DIEs for compilation unit CU, starting at
3665    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3666    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3667    DW_AT_ranges).  If NEED_PC is set, then this function will set
3668    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3669    and record the covered ranges in the addrmap.  */
3670
3671 static void
3672 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3673                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3674 {
3675   struct partial_die_info *pdi;
3676
3677   /* Now, march along the PDI's, descending into ones which have
3678      interesting children but skipping the children of the other ones,
3679      until we reach the end of the compilation unit.  */
3680
3681   pdi = first_die;
3682
3683   while (pdi != NULL)
3684     {
3685       fixup_partial_die (pdi, cu);
3686
3687       /* Anonymous namespaces or modules have no name but have interesting
3688          children, so we need to look at them.  Ditto for anonymous
3689          enums.  */
3690
3691       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3692           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3693         {
3694           switch (pdi->tag)
3695             {
3696             case DW_TAG_subprogram:
3697               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3698               break;
3699             case DW_TAG_constant:
3700             case DW_TAG_variable:
3701             case DW_TAG_typedef:
3702             case DW_TAG_union_type:
3703               if (!pdi->is_declaration)
3704                 {
3705                   add_partial_symbol (pdi, cu);
3706                 }
3707               break;
3708             case DW_TAG_class_type:
3709             case DW_TAG_interface_type:
3710             case DW_TAG_structure_type:
3711               if (!pdi->is_declaration)
3712                 {
3713                   add_partial_symbol (pdi, cu);
3714                 }
3715               break;
3716             case DW_TAG_enumeration_type:
3717               if (!pdi->is_declaration)
3718                 add_partial_enumeration (pdi, cu);
3719               break;
3720             case DW_TAG_base_type:
3721             case DW_TAG_subrange_type:
3722               /* File scope base type definitions are added to the partial
3723                  symbol table.  */
3724               add_partial_symbol (pdi, cu);
3725               break;
3726             case DW_TAG_namespace:
3727               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3728               break;
3729             case DW_TAG_module:
3730               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3731               break;
3732             default:
3733               break;
3734             }
3735         }
3736
3737       /* If the die has a sibling, skip to the sibling.  */
3738
3739       pdi = pdi->die_sibling;
3740     }
3741 }
3742
3743 /* Functions used to compute the fully scoped name of a partial DIE.
3744
3745    Normally, this is simple.  For C++, the parent DIE's fully scoped
3746    name is concatenated with "::" and the partial DIE's name.  For
3747    Java, the same thing occurs except that "." is used instead of "::".
3748    Enumerators are an exception; they use the scope of their parent
3749    enumeration type, i.e. the name of the enumeration type is not
3750    prepended to the enumerator.
3751
3752    There are two complexities.  One is DW_AT_specification; in this
3753    case "parent" means the parent of the target of the specification,
3754    instead of the direct parent of the DIE.  The other is compilers
3755    which do not emit DW_TAG_namespace; in this case we try to guess
3756    the fully qualified name of structure types from their members'
3757    linkage names.  This must be done using the DIE's children rather
3758    than the children of any DW_AT_specification target.  We only need
3759    to do this for structures at the top level, i.e. if the target of
3760    any DW_AT_specification (if any; otherwise the DIE itself) does not
3761    have a parent.  */
3762
3763 /* Compute the scope prefix associated with PDI's parent, in
3764    compilation unit CU.  The result will be allocated on CU's
3765    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3766    field.  NULL is returned if no prefix is necessary.  */
3767 static char *
3768 partial_die_parent_scope (struct partial_die_info *pdi,
3769                           struct dwarf2_cu *cu)
3770 {
3771   char *grandparent_scope;
3772   struct partial_die_info *parent, *real_pdi;
3773
3774   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3775      then this means the parent of the specification DIE.  */
3776
3777   real_pdi = pdi;
3778   while (real_pdi->has_specification)
3779     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3780
3781   parent = real_pdi->die_parent;
3782   if (parent == NULL)
3783     return NULL;
3784
3785   if (parent->scope_set)
3786     return parent->scope;
3787
3788   fixup_partial_die (parent, cu);
3789
3790   grandparent_scope = partial_die_parent_scope (parent, cu);
3791
3792   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3793      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3794      Work around this problem here.  */
3795   if (cu->language == language_cplus
3796       && parent->tag == DW_TAG_namespace
3797       && strcmp (parent->name, "::") == 0
3798       && grandparent_scope == NULL)
3799     {
3800       parent->scope = NULL;
3801       parent->scope_set = 1;
3802       return NULL;
3803     }
3804
3805   if (parent->tag == DW_TAG_namespace
3806       || parent->tag == DW_TAG_module
3807       || parent->tag == DW_TAG_structure_type
3808       || parent->tag == DW_TAG_class_type
3809       || parent->tag == DW_TAG_interface_type
3810       || parent->tag == DW_TAG_union_type
3811       || parent->tag == DW_TAG_enumeration_type)
3812     {
3813       if (grandparent_scope == NULL)
3814         parent->scope = parent->name;
3815       else
3816         parent->scope = typename_concat (&cu->comp_unit_obstack,
3817                                          grandparent_scope,
3818                                          parent->name, 0, cu);
3819     }
3820   else if (parent->tag == DW_TAG_enumerator)
3821     /* Enumerators should not get the name of the enumeration as a prefix.  */
3822     parent->scope = grandparent_scope;
3823   else
3824     {
3825       /* FIXME drow/2004-04-01: What should we be doing with
3826          function-local names?  For partial symbols, we should probably be
3827          ignoring them.  */
3828       complaint (&symfile_complaints,
3829                  _("unhandled containing DIE tag %d for DIE at %d"),
3830                  parent->tag, pdi->offset);
3831       parent->scope = grandparent_scope;
3832     }
3833
3834   parent->scope_set = 1;
3835   return parent->scope;
3836 }
3837
3838 /* Return the fully scoped name associated with PDI, from compilation unit
3839    CU.  The result will be allocated with malloc.  */
3840 static char *
3841 partial_die_full_name (struct partial_die_info *pdi,
3842                        struct dwarf2_cu *cu)
3843 {
3844   char *parent_scope;
3845
3846   /* If this is a template instantiation, we can not work out the
3847      template arguments from partial DIEs.  So, unfortunately, we have
3848      to go through the full DIEs.  At least any work we do building
3849      types here will be reused if full symbols are loaded later.  */
3850   if (pdi->has_template_arguments)
3851     {
3852       fixup_partial_die (pdi, cu);
3853
3854       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3855         {
3856           struct die_info *die;
3857           struct attribute attr;
3858           struct dwarf2_cu *ref_cu = cu;
3859
3860           attr.name = 0;
3861           attr.form = DW_FORM_ref_addr;
3862           attr.u.addr = pdi->offset;
3863           die = follow_die_ref (NULL, &attr, &ref_cu);
3864
3865           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3866         }
3867     }
3868
3869   parent_scope = partial_die_parent_scope (pdi, cu);
3870   if (parent_scope == NULL)
3871     return NULL;
3872   else
3873     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3874 }
3875
3876 static void
3877 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3878 {
3879   struct objfile *objfile = cu->objfile;
3880   CORE_ADDR addr = 0;
3881   char *actual_name = NULL;
3882   const struct partial_symbol *psym = NULL;
3883   CORE_ADDR baseaddr;
3884   int built_actual_name = 0;
3885
3886   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3887
3888   actual_name = partial_die_full_name (pdi, cu);
3889   if (actual_name)
3890     built_actual_name = 1;
3891
3892   if (actual_name == NULL)
3893     actual_name = pdi->name;
3894
3895   switch (pdi->tag)
3896     {
3897     case DW_TAG_subprogram:
3898       if (pdi->is_external || cu->language == language_ada)
3899         {
3900           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3901              of the global scope.  But in Ada, we want to be able to access
3902              nested procedures globally.  So all Ada subprograms are stored
3903              in the global scope.  */
3904           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3905              mst_text, objfile); */
3906           add_psymbol_to_list (actual_name, strlen (actual_name),
3907                                built_actual_name,
3908                                VAR_DOMAIN, LOC_BLOCK,
3909                                &objfile->global_psymbols,
3910                                0, pdi->lowpc + baseaddr,
3911                                cu->language, objfile);
3912         }
3913       else
3914         {
3915           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3916              mst_file_text, objfile); */
3917           add_psymbol_to_list (actual_name, strlen (actual_name),
3918                                built_actual_name,
3919                                VAR_DOMAIN, LOC_BLOCK,
3920                                &objfile->static_psymbols,
3921                                0, pdi->lowpc + baseaddr,
3922                                cu->language, objfile);
3923         }
3924       break;
3925     case DW_TAG_constant:
3926       {
3927         struct psymbol_allocation_list *list;
3928
3929         if (pdi->is_external)
3930           list = &objfile->global_psymbols;
3931         else
3932           list = &objfile->static_psymbols;
3933         add_psymbol_to_list (actual_name, strlen (actual_name),
3934                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3935                              list, 0, 0, cu->language, objfile);
3936       }
3937       break;
3938     case DW_TAG_variable:
3939       if (pdi->locdesc)
3940         addr = decode_locdesc (pdi->locdesc, cu);
3941
3942       if (pdi->locdesc
3943           && addr == 0
3944           && !dwarf2_per_objfile->has_section_at_zero)
3945         {
3946           /* A global or static variable may also have been stripped
3947              out by the linker if unused, in which case its address
3948              will be nullified; do not add such variables into partial
3949              symbol table then.  */
3950         }
3951       else if (pdi->is_external)
3952         {
3953           /* Global Variable.
3954              Don't enter into the minimal symbol tables as there is
3955              a minimal symbol table entry from the ELF symbols already.
3956              Enter into partial symbol table if it has a location
3957              descriptor or a type.
3958              If the location descriptor is missing, new_symbol will create
3959              a LOC_UNRESOLVED symbol, the address of the variable will then
3960              be determined from the minimal symbol table whenever the variable
3961              is referenced.
3962              The address for the partial symbol table entry is not
3963              used by GDB, but it comes in handy for debugging partial symbol
3964              table building.  */
3965
3966           if (pdi->locdesc || pdi->has_type)
3967             add_psymbol_to_list (actual_name, strlen (actual_name),
3968                                  built_actual_name,
3969                                  VAR_DOMAIN, LOC_STATIC,
3970                                  &objfile->global_psymbols,
3971                                  0, addr + baseaddr,
3972                                  cu->language, objfile);
3973         }
3974       else
3975         {
3976           /* Static Variable.  Skip symbols without location descriptors.  */
3977           if (pdi->locdesc == NULL)
3978             {
3979               if (built_actual_name)
3980                 xfree (actual_name);
3981               return;
3982             }
3983           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3984              mst_file_data, objfile); */
3985           add_psymbol_to_list (actual_name, strlen (actual_name),
3986                                built_actual_name,
3987                                VAR_DOMAIN, LOC_STATIC,
3988                                &objfile->static_psymbols,
3989                                0, addr + baseaddr,
3990                                cu->language, objfile);
3991         }
3992       break;
3993     case DW_TAG_typedef:
3994     case DW_TAG_base_type:
3995     case DW_TAG_subrange_type:
3996       add_psymbol_to_list (actual_name, strlen (actual_name),
3997                            built_actual_name,
3998                            VAR_DOMAIN, LOC_TYPEDEF,
3999                            &objfile->static_psymbols,
4000                            0, (CORE_ADDR) 0, cu->language, objfile);
4001       break;
4002     case DW_TAG_namespace:
4003       add_psymbol_to_list (actual_name, strlen (actual_name),
4004                            built_actual_name,
4005                            VAR_DOMAIN, LOC_TYPEDEF,
4006                            &objfile->global_psymbols,
4007                            0, (CORE_ADDR) 0, cu->language, objfile);
4008       break;
4009     case DW_TAG_class_type:
4010     case DW_TAG_interface_type:
4011     case DW_TAG_structure_type:
4012     case DW_TAG_union_type:
4013     case DW_TAG_enumeration_type:
4014       /* Skip external references.  The DWARF standard says in the section
4015          about "Structure, Union, and Class Type Entries": "An incomplete
4016          structure, union or class type is represented by a structure,
4017          union or class entry that does not have a byte size attribute
4018          and that has a DW_AT_declaration attribute."  */
4019       if (!pdi->has_byte_size && pdi->is_declaration)
4020         {
4021           if (built_actual_name)
4022             xfree (actual_name);
4023           return;
4024         }
4025
4026       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4027          static vs. global.  */
4028       add_psymbol_to_list (actual_name, strlen (actual_name),
4029                            built_actual_name,
4030                            STRUCT_DOMAIN, LOC_TYPEDEF,
4031                            (cu->language == language_cplus
4032                             || cu->language == language_java)
4033                            ? &objfile->global_psymbols
4034                            : &objfile->static_psymbols,
4035                            0, (CORE_ADDR) 0, cu->language, objfile);
4036
4037       break;
4038     case DW_TAG_enumerator:
4039       add_psymbol_to_list (actual_name, strlen (actual_name),
4040                            built_actual_name,
4041                            VAR_DOMAIN, LOC_CONST,
4042                            (cu->language == language_cplus
4043                             || cu->language == language_java)
4044                            ? &objfile->global_psymbols
4045                            : &objfile->static_psymbols,
4046                            0, (CORE_ADDR) 0, cu->language, objfile);
4047       break;
4048     default:
4049       break;
4050     }
4051
4052   if (built_actual_name)
4053     xfree (actual_name);
4054 }
4055
4056 /* Read a partial die corresponding to a namespace; also, add a symbol
4057    corresponding to that namespace to the symbol table.  NAMESPACE is
4058    the name of the enclosing namespace.  */
4059
4060 static void
4061 add_partial_namespace (struct partial_die_info *pdi,
4062                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4063                        int need_pc, struct dwarf2_cu *cu)
4064 {
4065   /* Add a symbol for the namespace.  */
4066
4067   add_partial_symbol (pdi, cu);
4068
4069   /* Now scan partial symbols in that namespace.  */
4070
4071   if (pdi->has_children)
4072     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4073 }
4074
4075 /* Read a partial die corresponding to a Fortran module.  */
4076
4077 static void
4078 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4079                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4080 {
4081   /* Now scan partial symbols in that module.  */
4082
4083   if (pdi->has_children)
4084     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4085 }
4086
4087 /* Read a partial die corresponding to a subprogram and create a partial
4088    symbol for that subprogram.  When the CU language allows it, this
4089    routine also defines a partial symbol for each nested subprogram
4090    that this subprogram contains.
4091
4092    DIE my also be a lexical block, in which case we simply search
4093    recursively for suprograms defined inside that lexical block.
4094    Again, this is only performed when the CU language allows this
4095    type of definitions.  */
4096
4097 static void
4098 add_partial_subprogram (struct partial_die_info *pdi,
4099                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4100                         int need_pc, struct dwarf2_cu *cu)
4101 {
4102   if (pdi->tag == DW_TAG_subprogram)
4103     {
4104       if (pdi->has_pc_info)
4105         {
4106           if (pdi->lowpc < *lowpc)
4107             *lowpc = pdi->lowpc;
4108           if (pdi->highpc > *highpc)
4109             *highpc = pdi->highpc;
4110           if (need_pc)
4111             {
4112               CORE_ADDR baseaddr;
4113               struct objfile *objfile = cu->objfile;
4114
4115               baseaddr = ANOFFSET (objfile->section_offsets,
4116                                    SECT_OFF_TEXT (objfile));
4117               addrmap_set_empty (objfile->psymtabs_addrmap,
4118                                  pdi->lowpc + baseaddr,
4119                                  pdi->highpc - 1 + baseaddr,
4120                                  cu->per_cu->v.psymtab);
4121             }
4122           if (!pdi->is_declaration)
4123             /* Ignore subprogram DIEs that do not have a name, they are
4124                illegal.  Do not emit a complaint at this point, we will
4125                do so when we convert this psymtab into a symtab.  */
4126             if (pdi->name)
4127               add_partial_symbol (pdi, cu);
4128         }
4129     }
4130
4131   if (! pdi->has_children)
4132     return;
4133
4134   if (cu->language == language_ada)
4135     {
4136       pdi = pdi->die_child;
4137       while (pdi != NULL)
4138         {
4139           fixup_partial_die (pdi, cu);
4140           if (pdi->tag == DW_TAG_subprogram
4141               || pdi->tag == DW_TAG_lexical_block)
4142             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4143           pdi = pdi->die_sibling;
4144         }
4145     }
4146 }
4147
4148 /* Read a partial die corresponding to an enumeration type.  */
4149
4150 static void
4151 add_partial_enumeration (struct partial_die_info *enum_pdi,
4152                          struct dwarf2_cu *cu)
4153 {
4154   struct partial_die_info *pdi;
4155
4156   if (enum_pdi->name != NULL)
4157     add_partial_symbol (enum_pdi, cu);
4158
4159   pdi = enum_pdi->die_child;
4160   while (pdi)
4161     {
4162       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4163         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4164       else
4165         add_partial_symbol (pdi, cu);
4166       pdi = pdi->die_sibling;
4167     }
4168 }
4169
4170 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4171    Return the corresponding abbrev, or NULL if the number is zero (indicating
4172    an empty DIE).  In either case *BYTES_READ will be set to the length of
4173    the initial number.  */
4174
4175 static struct abbrev_info *
4176 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4177                  struct dwarf2_cu *cu)
4178 {
4179   bfd *abfd = cu->objfile->obfd;
4180   unsigned int abbrev_number;
4181   struct abbrev_info *abbrev;
4182
4183   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4184
4185   if (abbrev_number == 0)
4186     return NULL;
4187
4188   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4189   if (!abbrev)
4190     {
4191       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4192              abbrev_number, bfd_get_filename (abfd));
4193     }
4194
4195   return abbrev;
4196 }
4197
4198 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4199    Returns a pointer to the end of a series of DIEs, terminated by an empty
4200    DIE.  Any children of the skipped DIEs will also be skipped.  */
4201
4202 static gdb_byte *
4203 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4204 {
4205   struct abbrev_info *abbrev;
4206   unsigned int bytes_read;
4207
4208   while (1)
4209     {
4210       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4211       if (abbrev == NULL)
4212         return info_ptr + bytes_read;
4213       else
4214         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4215     }
4216 }
4217
4218 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4219    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4220    abbrev corresponding to that skipped uleb128 should be passed in
4221    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4222    children.  */
4223
4224 static gdb_byte *
4225 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4226               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4227 {
4228   unsigned int bytes_read;
4229   struct attribute attr;
4230   bfd *abfd = cu->objfile->obfd;
4231   unsigned int form, i;
4232
4233   for (i = 0; i < abbrev->num_attrs; i++)
4234     {
4235       /* The only abbrev we care about is DW_AT_sibling.  */
4236       if (abbrev->attrs[i].name == DW_AT_sibling)
4237         {
4238           read_attribute (&attr, &abbrev->attrs[i],
4239                           abfd, info_ptr, cu);
4240           if (attr.form == DW_FORM_ref_addr)
4241             complaint (&symfile_complaints,
4242                        _("ignoring absolute DW_AT_sibling"));
4243           else
4244             return buffer + dwarf2_get_ref_die_offset (&attr);
4245         }
4246
4247       /* If it isn't DW_AT_sibling, skip this attribute.  */
4248       form = abbrev->attrs[i].form;
4249     skip_attribute:
4250       switch (form)
4251         {
4252         case DW_FORM_ref_addr:
4253           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4254              and later it is offset sized.  */
4255           if (cu->header.version == 2)
4256             info_ptr += cu->header.addr_size;
4257           else
4258             info_ptr += cu->header.offset_size;
4259           break;
4260         case DW_FORM_addr:
4261           info_ptr += cu->header.addr_size;
4262           break;
4263         case DW_FORM_data1:
4264         case DW_FORM_ref1:
4265         case DW_FORM_flag:
4266           info_ptr += 1;
4267           break;
4268         case DW_FORM_flag_present:
4269           break;
4270         case DW_FORM_data2:
4271         case DW_FORM_ref2:
4272           info_ptr += 2;
4273           break;
4274         case DW_FORM_data4:
4275         case DW_FORM_ref4:
4276           info_ptr += 4;
4277           break;
4278         case DW_FORM_data8:
4279         case DW_FORM_ref8:
4280         case DW_FORM_ref_sig8:
4281           info_ptr += 8;
4282           break;
4283         case DW_FORM_string:
4284           read_direct_string (abfd, info_ptr, &bytes_read);
4285           info_ptr += bytes_read;
4286           break;
4287         case DW_FORM_sec_offset:
4288         case DW_FORM_strp:
4289           info_ptr += cu->header.offset_size;
4290           break;
4291         case DW_FORM_exprloc:
4292         case DW_FORM_block:
4293           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4294           info_ptr += bytes_read;
4295           break;
4296         case DW_FORM_block1:
4297           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4298           break;
4299         case DW_FORM_block2:
4300           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4301           break;
4302         case DW_FORM_block4:
4303           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4304           break;
4305         case DW_FORM_sdata:
4306         case DW_FORM_udata:
4307         case DW_FORM_ref_udata:
4308           info_ptr = skip_leb128 (abfd, info_ptr);
4309           break;
4310         case DW_FORM_indirect:
4311           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4312           info_ptr += bytes_read;
4313           /* We need to continue parsing from here, so just go back to
4314              the top.  */
4315           goto skip_attribute;
4316
4317         default:
4318           error (_("Dwarf Error: Cannot handle %s "
4319                    "in DWARF reader [in module %s]"),
4320                  dwarf_form_name (form),
4321                  bfd_get_filename (abfd));
4322         }
4323     }
4324
4325   if (abbrev->has_children)
4326     return skip_children (buffer, info_ptr, cu);
4327   else
4328     return info_ptr;
4329 }
4330
4331 /* Locate ORIG_PDI's sibling.
4332    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4333    in BUFFER.  */
4334
4335 static gdb_byte *
4336 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4337                     gdb_byte *buffer, gdb_byte *info_ptr,
4338                     bfd *abfd, struct dwarf2_cu *cu)
4339 {
4340   /* Do we know the sibling already?  */
4341
4342   if (orig_pdi->sibling)
4343     return orig_pdi->sibling;
4344
4345   /* Are there any children to deal with?  */
4346
4347   if (!orig_pdi->has_children)
4348     return info_ptr;
4349
4350   /* Skip the children the long way.  */
4351
4352   return skip_children (buffer, info_ptr, cu);
4353 }
4354
4355 /* Expand this partial symbol table into a full symbol table.  */
4356
4357 static void
4358 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4359 {
4360   if (pst != NULL)
4361     {
4362       if (pst->readin)
4363         {
4364           warning (_("bug: psymtab for %s is already read in."),
4365                    pst->filename);
4366         }
4367       else
4368         {
4369           if (info_verbose)
4370             {
4371               printf_filtered (_("Reading in symbols for %s..."),
4372                                pst->filename);
4373               gdb_flush (gdb_stdout);
4374             }
4375
4376           /* Restore our global data.  */
4377           dwarf2_per_objfile = objfile_data (pst->objfile,
4378                                              dwarf2_objfile_data_key);
4379
4380           /* If this psymtab is constructed from a debug-only objfile, the
4381              has_section_at_zero flag will not necessarily be correct.  We
4382              can get the correct value for this flag by looking at the data
4383              associated with the (presumably stripped) associated objfile.  */
4384           if (pst->objfile->separate_debug_objfile_backlink)
4385             {
4386               struct dwarf2_per_objfile *dpo_backlink
4387                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4388                                 dwarf2_objfile_data_key);
4389
4390               dwarf2_per_objfile->has_section_at_zero
4391                 = dpo_backlink->has_section_at_zero;
4392             }
4393
4394           dwarf2_per_objfile->reading_partial_symbols = 0;
4395
4396           psymtab_to_symtab_1 (pst);
4397
4398           /* Finish up the debug error message.  */
4399           if (info_verbose)
4400             printf_filtered (_("done.\n"));
4401         }
4402     }
4403 }
4404
4405 /* Add PER_CU to the queue.  */
4406
4407 static void
4408 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4409 {
4410   struct dwarf2_queue_item *item;
4411
4412   per_cu->queued = 1;
4413   item = xmalloc (sizeof (*item));
4414   item->per_cu = per_cu;
4415   item->next = NULL;
4416
4417   if (dwarf2_queue == NULL)
4418     dwarf2_queue = item;
4419   else
4420     dwarf2_queue_tail->next = item;
4421
4422   dwarf2_queue_tail = item;
4423 }
4424
4425 /* Process the queue.  */
4426
4427 static void
4428 process_queue (struct objfile *objfile)
4429 {
4430   struct dwarf2_queue_item *item, *next_item;
4431
4432   /* The queue starts out with one item, but following a DIE reference
4433      may load a new CU, adding it to the end of the queue.  */
4434   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4435     {
4436       if (dwarf2_per_objfile->using_index
4437           ? !item->per_cu->v.quick->symtab
4438           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4439         process_full_comp_unit (item->per_cu);
4440
4441       item->per_cu->queued = 0;
4442       next_item = item->next;
4443       xfree (item);
4444     }
4445
4446   dwarf2_queue_tail = NULL;
4447 }
4448
4449 /* Free all allocated queue entries.  This function only releases anything if
4450    an error was thrown; if the queue was processed then it would have been
4451    freed as we went along.  */
4452
4453 static void
4454 dwarf2_release_queue (void *dummy)
4455 {
4456   struct dwarf2_queue_item *item, *last;
4457
4458   item = dwarf2_queue;
4459   while (item)
4460     {
4461       /* Anything still marked queued is likely to be in an
4462          inconsistent state, so discard it.  */
4463       if (item->per_cu->queued)
4464         {
4465           if (item->per_cu->cu != NULL)
4466             free_one_cached_comp_unit (item->per_cu->cu);
4467           item->per_cu->queued = 0;
4468         }
4469
4470       last = item;
4471       item = item->next;
4472       xfree (last);
4473     }
4474
4475   dwarf2_queue = dwarf2_queue_tail = NULL;
4476 }
4477
4478 /* Read in full symbols for PST, and anything it depends on.  */
4479
4480 static void
4481 psymtab_to_symtab_1 (struct partial_symtab *pst)
4482 {
4483   struct dwarf2_per_cu_data *per_cu;
4484   struct cleanup *back_to;
4485   int i;
4486
4487   for (i = 0; i < pst->number_of_dependencies; i++)
4488     if (!pst->dependencies[i]->readin)
4489       {
4490         /* Inform about additional files that need to be read in.  */
4491         if (info_verbose)
4492           {
4493             /* FIXME: i18n: Need to make this a single string.  */
4494             fputs_filtered (" ", gdb_stdout);
4495             wrap_here ("");
4496             fputs_filtered ("and ", gdb_stdout);
4497             wrap_here ("");
4498             printf_filtered ("%s...", pst->dependencies[i]->filename);
4499             wrap_here ("");     /* Flush output.  */
4500             gdb_flush (gdb_stdout);
4501           }
4502         psymtab_to_symtab_1 (pst->dependencies[i]);
4503       }
4504
4505   per_cu = pst->read_symtab_private;
4506
4507   if (per_cu == NULL)
4508     {
4509       /* It's an include file, no symbols to read for it.
4510          Everything is in the parent symtab.  */
4511       pst->readin = 1;
4512       return;
4513     }
4514
4515   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4516 }
4517
4518 /* Load the DIEs associated with PER_CU into memory.  */
4519
4520 static void
4521 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4522                      struct objfile *objfile)
4523 {
4524   bfd *abfd = objfile->obfd;
4525   struct dwarf2_cu *cu;
4526   unsigned int offset;
4527   gdb_byte *info_ptr, *beg_of_comp_unit;
4528   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4529   struct attribute *attr;
4530   int read_cu = 0;
4531
4532   gdb_assert (! per_cu->from_debug_types);
4533
4534   /* Set local variables from the partial symbol table info.  */
4535   offset = per_cu->offset;
4536
4537   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4538   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4539   beg_of_comp_unit = info_ptr;
4540
4541   if (per_cu->cu == NULL)
4542     {
4543       cu = xmalloc (sizeof (*cu));
4544       init_one_comp_unit (cu, objfile);
4545
4546       read_cu = 1;
4547
4548       /* If an error occurs while loading, release our storage.  */
4549       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4550
4551       /* Read in the comp_unit header.  */
4552       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4553
4554       /* Complete the cu_header.  */
4555       cu->header.offset = offset;
4556       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4557
4558       /* Read the abbrevs for this compilation unit.  */
4559       dwarf2_read_abbrevs (abfd, cu);
4560       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4561
4562       /* Link this compilation unit into the compilation unit tree.  */
4563       per_cu->cu = cu;
4564       cu->per_cu = per_cu;
4565
4566       /* Link this CU into read_in_chain.  */
4567       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4568       dwarf2_per_objfile->read_in_chain = per_cu;
4569     }
4570   else
4571     {
4572       cu = per_cu->cu;
4573       info_ptr += cu->header.first_die_offset;
4574     }
4575
4576   cu->dies = read_comp_unit (info_ptr, cu);
4577
4578   /* We try not to read any attributes in this function, because not
4579      all objfiles needed for references have been loaded yet, and symbol
4580      table processing isn't initialized.  But we have to set the CU language,
4581      or we won't be able to build types correctly.  */
4582   prepare_one_comp_unit (cu, cu->dies);
4583
4584   /* Similarly, if we do not read the producer, we can not apply
4585      producer-specific interpretation.  */
4586   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4587   if (attr)
4588     cu->producer = DW_STRING (attr);
4589
4590   if (read_cu)
4591     {
4592       do_cleanups (free_abbrevs_cleanup);
4593
4594       /* We've successfully allocated this compilation unit.  Let our
4595          caller clean it up when finished with it.  */
4596       discard_cleanups (free_cu_cleanup);
4597     }
4598 }
4599
4600 /* Add a DIE to the delayed physname list.  */
4601
4602 static void
4603 add_to_method_list (struct type *type, int fnfield_index, int index,
4604                     const char *name, struct die_info *die,
4605                     struct dwarf2_cu *cu)
4606 {
4607   struct delayed_method_info mi;
4608   mi.type = type;
4609   mi.fnfield_index = fnfield_index;
4610   mi.index = index;
4611   mi.name = name;
4612   mi.die = die;
4613   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4614 }
4615
4616 /* A cleanup for freeing the delayed method list.  */
4617
4618 static void
4619 free_delayed_list (void *ptr)
4620 {
4621   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4622   if (cu->method_list != NULL)
4623     {
4624       VEC_free (delayed_method_info, cu->method_list);
4625       cu->method_list = NULL;
4626     }
4627 }
4628
4629 /* Compute the physnames of any methods on the CU's method list.
4630
4631    The computation of method physnames is delayed in order to avoid the
4632    (bad) condition that one of the method's formal parameters is of an as yet
4633    incomplete type.  */
4634
4635 static void
4636 compute_delayed_physnames (struct dwarf2_cu *cu)
4637 {
4638   int i;
4639   struct delayed_method_info *mi;
4640   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4641     {
4642       const char *physname;
4643       struct fn_fieldlist *fn_flp
4644         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4645       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4646       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4647     }
4648 }
4649
4650 /* Check for GCC >= 4.0.  */
4651
4652 static int
4653 producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4654 {
4655   const char *cs;
4656   int major, minor;
4657
4658   if (cu->producer == NULL)
4659     {
4660       /* For unknown compilers expect their behavior is not compliant.  For GCC
4661          this case can also happen for -gdwarf-4 type units supported since
4662          gcc-4.5.  */
4663
4664       return 0;
4665     }
4666
4667   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
4668
4669   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4670     {
4671       /* For non-GCC compilers expect their behavior is not compliant.  */
4672
4673       return 0;
4674     }
4675   cs = &cu->producer[strlen ("GNU ")];
4676   while (*cs && !isdigit (*cs))
4677     cs++;
4678   if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4679     {
4680       /* Not recognized as GCC.  */
4681
4682       return 0;
4683     }
4684
4685   return major >= 4;
4686 }
4687
4688 /* Generate full symbol information for PST and CU, whose DIEs have
4689    already been loaded into memory.  */
4690
4691 static void
4692 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4693 {
4694   struct dwarf2_cu *cu = per_cu->cu;
4695   struct objfile *objfile = per_cu->objfile;
4696   CORE_ADDR lowpc, highpc;
4697   struct symtab *symtab;
4698   struct cleanup *back_to, *delayed_list_cleanup;
4699   CORE_ADDR baseaddr;
4700
4701   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4702
4703   buildsym_init ();
4704   back_to = make_cleanup (really_free_pendings, NULL);
4705   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4706
4707   cu->list_in_scope = &file_symbols;
4708
4709   dwarf2_find_base_address (cu->dies, cu);
4710
4711   /* Do line number decoding in read_file_scope () */
4712   process_die (cu->dies, cu);
4713
4714   /* Now that we have processed all the DIEs in the CU, all the types 
4715      should be complete, and it should now be safe to compute all of the
4716      physnames.  */
4717   compute_delayed_physnames (cu);
4718   do_cleanups (delayed_list_cleanup);
4719
4720   /* Some compilers don't define a DW_AT_high_pc attribute for the
4721      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4722      it, by scanning the DIE's below the compilation unit.  */
4723   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4724
4725   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4726
4727   if (symtab != NULL)
4728     {
4729       /* Set symtab language to language from DW_AT_language.  If the
4730          compilation is from a C file generated by language preprocessors, do
4731          not set the language if it was already deduced by start_subfile.  */
4732       if (!(cu->language == language_c && symtab->language != language_c))
4733         symtab->language = cu->language;
4734
4735       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4736          produce DW_AT_location with location lists but it can be possibly
4737          invalid without -fvar-tracking.
4738
4739          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4740          needed, it would be wrong due to missing DW_AT_producer there.
4741
4742          Still one can confuse GDB by using non-standard GCC compilation
4743          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4744          */ 
4745       if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4746         symtab->locations_valid = 1;
4747     }
4748
4749   if (dwarf2_per_objfile->using_index)
4750     per_cu->v.quick->symtab = symtab;
4751   else
4752     {
4753       struct partial_symtab *pst = per_cu->v.psymtab;
4754       pst->symtab = symtab;
4755       pst->readin = 1;
4756     }
4757
4758   do_cleanups (back_to);
4759 }
4760
4761 /* Process a die and its children.  */
4762
4763 static void
4764 process_die (struct die_info *die, struct dwarf2_cu *cu)
4765 {
4766   switch (die->tag)
4767     {
4768     case DW_TAG_padding:
4769       break;
4770     case DW_TAG_compile_unit:
4771       read_file_scope (die, cu);
4772       break;
4773     case DW_TAG_type_unit:
4774       read_type_unit_scope (die, cu);
4775       break;
4776     case DW_TAG_subprogram:
4777     case DW_TAG_inlined_subroutine:
4778       read_func_scope (die, cu);
4779       break;
4780     case DW_TAG_lexical_block:
4781     case DW_TAG_try_block:
4782     case DW_TAG_catch_block:
4783       read_lexical_block_scope (die, cu);
4784       break;
4785     case DW_TAG_class_type:
4786     case DW_TAG_interface_type:
4787     case DW_TAG_structure_type:
4788     case DW_TAG_union_type:
4789       process_structure_scope (die, cu);
4790       break;
4791     case DW_TAG_enumeration_type:
4792       process_enumeration_scope (die, cu);
4793       break;
4794
4795     /* These dies have a type, but processing them does not create
4796        a symbol or recurse to process the children.  Therefore we can
4797        read them on-demand through read_type_die.  */
4798     case DW_TAG_subroutine_type:
4799     case DW_TAG_set_type:
4800     case DW_TAG_array_type:
4801     case DW_TAG_pointer_type:
4802     case DW_TAG_ptr_to_member_type:
4803     case DW_TAG_reference_type:
4804     case DW_TAG_string_type:
4805       break;
4806
4807     case DW_TAG_base_type:
4808     case DW_TAG_subrange_type:
4809     case DW_TAG_typedef:
4810       /* Add a typedef symbol for the type definition, if it has a
4811          DW_AT_name.  */
4812       new_symbol (die, read_type_die (die, cu), cu);
4813       break;
4814     case DW_TAG_common_block:
4815       read_common_block (die, cu);
4816       break;
4817     case DW_TAG_common_inclusion:
4818       break;
4819     case DW_TAG_namespace:
4820       processing_has_namespace_info = 1;
4821       read_namespace (die, cu);
4822       break;
4823     case DW_TAG_module:
4824       processing_has_namespace_info = 1;
4825       read_module (die, cu);
4826       break;
4827     case DW_TAG_imported_declaration:
4828     case DW_TAG_imported_module:
4829       processing_has_namespace_info = 1;
4830       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4831                                  || cu->language != language_fortran))
4832         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4833                    dwarf_tag_name (die->tag));
4834       read_import_statement (die, cu);
4835       break;
4836     default:
4837       new_symbol (die, NULL, cu);
4838       break;
4839     }
4840 }
4841
4842 /* A helper function for dwarf2_compute_name which determines whether DIE
4843    needs to have the name of the scope prepended to the name listed in the
4844    die.  */
4845
4846 static int
4847 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4848 {
4849   struct attribute *attr;
4850
4851   switch (die->tag)
4852     {
4853     case DW_TAG_namespace:
4854     case DW_TAG_typedef:
4855     case DW_TAG_class_type:
4856     case DW_TAG_interface_type:
4857     case DW_TAG_structure_type:
4858     case DW_TAG_union_type:
4859     case DW_TAG_enumeration_type:
4860     case DW_TAG_enumerator:
4861     case DW_TAG_subprogram:
4862     case DW_TAG_member:
4863       return 1;
4864
4865     case DW_TAG_variable:
4866     case DW_TAG_constant:
4867       /* We only need to prefix "globally" visible variables.  These include
4868          any variable marked with DW_AT_external or any variable that
4869          lives in a namespace.  [Variables in anonymous namespaces
4870          require prefixing, but they are not DW_AT_external.]  */
4871
4872       if (dwarf2_attr (die, DW_AT_specification, cu))
4873         {
4874           struct dwarf2_cu *spec_cu = cu;
4875
4876           return die_needs_namespace (die_specification (die, &spec_cu),
4877                                       spec_cu);
4878         }
4879
4880       attr = dwarf2_attr (die, DW_AT_external, cu);
4881       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4882           && die->parent->tag != DW_TAG_module)
4883         return 0;
4884       /* A variable in a lexical block of some kind does not need a
4885          namespace, even though in C++ such variables may be external
4886          and have a mangled name.  */
4887       if (die->parent->tag ==  DW_TAG_lexical_block
4888           || die->parent->tag ==  DW_TAG_try_block
4889           || die->parent->tag ==  DW_TAG_catch_block
4890           || die->parent->tag == DW_TAG_subprogram)
4891         return 0;
4892       return 1;
4893
4894     default:
4895       return 0;
4896     }
4897 }
4898
4899 /* Retrieve the last character from a mem_file.  */
4900
4901 static void
4902 do_ui_file_peek_last (void *object, const char *buffer, long length)
4903 {
4904   char *last_char_p = (char *) object;
4905
4906   if (length > 0)
4907     *last_char_p = buffer[length - 1];
4908 }
4909
4910 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4911    compute the physname for the object, which include a method's
4912    formal parameters (C++/Java) and return type (Java).
4913
4914    For Ada, return the DIE's linkage name rather than the fully qualified
4915    name.  PHYSNAME is ignored..
4916
4917    The result is allocated on the objfile_obstack and canonicalized.  */
4918
4919 static const char *
4920 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4921                      int physname)
4922 {
4923   if (name == NULL)
4924     name = dwarf2_name (die, cu);
4925
4926   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4927      compute it by typename_concat inside GDB.  */
4928   if (cu->language == language_ada
4929       || (cu->language == language_fortran && physname))
4930     {
4931       /* For Ada unit, we prefer the linkage name over the name, as
4932          the former contains the exported name, which the user expects
4933          to be able to reference.  Ideally, we want the user to be able
4934          to reference this entity using either natural or linkage name,
4935          but we haven't started looking at this enhancement yet.  */
4936       struct attribute *attr;
4937
4938       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4939       if (attr == NULL)
4940         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4941       if (attr && DW_STRING (attr))
4942         return DW_STRING (attr);
4943     }
4944
4945   /* These are the only languages we know how to qualify names in.  */
4946   if (name != NULL
4947       && (cu->language == language_cplus || cu->language == language_java
4948           || cu->language == language_fortran))
4949     {
4950       if (die_needs_namespace (die, cu))
4951         {
4952           long length;
4953           char *prefix;
4954           struct ui_file *buf;
4955
4956           prefix = determine_prefix (die, cu);
4957           buf = mem_fileopen ();
4958           if (*prefix != '\0')
4959             {
4960               char *prefixed_name = typename_concat (NULL, prefix, name,
4961                                                      physname, cu);
4962
4963               fputs_unfiltered (prefixed_name, buf);
4964               xfree (prefixed_name);
4965             }
4966           else
4967             fputs_unfiltered (name, buf);
4968
4969           /* Template parameters may be specified in the DIE's DW_AT_name, or
4970              as children with DW_TAG_template_type_param or
4971              DW_TAG_value_type_param.  If the latter, add them to the name
4972              here.  If the name already has template parameters, then
4973              skip this step; some versions of GCC emit both, and
4974              it is more efficient to use the pre-computed name.
4975
4976              Something to keep in mind about this process: it is very
4977              unlikely, or in some cases downright impossible, to produce
4978              something that will match the mangled name of a function.
4979              If the definition of the function has the same debug info,
4980              we should be able to match up with it anyway.  But fallbacks
4981              using the minimal symbol, for instance to find a method
4982              implemented in a stripped copy of libstdc++, will not work.
4983              If we do not have debug info for the definition, we will have to
4984              match them up some other way.
4985
4986              When we do name matching there is a related problem with function
4987              templates; two instantiated function templates are allowed to
4988              differ only by their return types, which we do not add here.  */
4989
4990           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4991             {
4992               struct attribute *attr;
4993               struct die_info *child;
4994               int first = 1;
4995
4996               die->building_fullname = 1;
4997
4998               for (child = die->child; child != NULL; child = child->sibling)
4999                 {
5000                   struct type *type;
5001                   long value;
5002                   gdb_byte *bytes;
5003                   struct dwarf2_locexpr_baton *baton;
5004                   struct value *v;
5005
5006                   if (child->tag != DW_TAG_template_type_param
5007                       && child->tag != DW_TAG_template_value_param)
5008                     continue;
5009
5010                   if (first)
5011                     {
5012                       fputs_unfiltered ("<", buf);
5013                       first = 0;
5014                     }
5015                   else
5016                     fputs_unfiltered (", ", buf);
5017
5018                   attr = dwarf2_attr (child, DW_AT_type, cu);
5019                   if (attr == NULL)
5020                     {
5021                       complaint (&symfile_complaints,
5022                                  _("template parameter missing DW_AT_type"));
5023                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5024                       continue;
5025                     }
5026                   type = die_type (child, cu);
5027
5028                   if (child->tag == DW_TAG_template_type_param)
5029                     {
5030                       c_print_type (type, "", buf, -1, 0);
5031                       continue;
5032                     }
5033
5034                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5035                   if (attr == NULL)
5036                     {
5037                       complaint (&symfile_complaints,
5038                                  _("template parameter missing "
5039                                    "DW_AT_const_value"));
5040                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5041                       continue;
5042                     }
5043
5044                   dwarf2_const_value_attr (attr, type, name,
5045                                            &cu->comp_unit_obstack, cu,
5046                                            &value, &bytes, &baton);
5047
5048                   if (TYPE_NOSIGN (type))
5049                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5050                        changed, this can use value_print instead.  */
5051                     c_printchar (value, type, buf);
5052                   else
5053                     {
5054                       struct value_print_options opts;
5055
5056                       if (baton != NULL)
5057                         v = dwarf2_evaluate_loc_desc (type, NULL,
5058                                                       baton->data,
5059                                                       baton->size,
5060                                                       baton->per_cu);
5061                       else if (bytes != NULL)
5062                         {
5063                           v = allocate_value (type);
5064                           memcpy (value_contents_writeable (v), bytes,
5065                                   TYPE_LENGTH (type));
5066                         }
5067                       else
5068                         v = value_from_longest (type, value);
5069
5070                       /* Specify decimal so that we do not depend on
5071                          the radix.  */
5072                       get_formatted_print_options (&opts, 'd');
5073                       opts.raw = 1;
5074                       value_print (v, buf, &opts);
5075                       release_value (v);
5076                       value_free (v);
5077                     }
5078                 }
5079
5080               die->building_fullname = 0;
5081
5082               if (!first)
5083                 {
5084                   /* Close the argument list, with a space if necessary
5085                      (nested templates).  */
5086                   char last_char = '\0';
5087                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5088                   if (last_char == '>')
5089                     fputs_unfiltered (" >", buf);
5090                   else
5091                     fputs_unfiltered (">", buf);
5092                 }
5093             }
5094
5095           /* For Java and C++ methods, append formal parameter type
5096              information, if PHYSNAME.  */
5097
5098           if (physname && die->tag == DW_TAG_subprogram
5099               && (cu->language == language_cplus
5100                   || cu->language == language_java))
5101             {
5102               struct type *type = read_type_die (die, cu);
5103
5104               c_type_print_args (type, buf, 1, cu->language);
5105
5106               if (cu->language == language_java)
5107                 {
5108                   /* For java, we must append the return type to method
5109                      names.  */
5110                   if (die->tag == DW_TAG_subprogram)
5111                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5112                                      0, 0);
5113                 }
5114               else if (cu->language == language_cplus)
5115                 {
5116                   /* Assume that an artificial first parameter is
5117                      "this", but do not crash if it is not.  RealView
5118                      marks unnamed (and thus unused) parameters as
5119                      artificial; there is no way to differentiate
5120                      the two cases.  */
5121                   if (TYPE_NFIELDS (type) > 0
5122                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5123                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5124                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5125                                                                         0))))
5126                     fputs_unfiltered (" const", buf);
5127                 }
5128             }
5129
5130           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5131                                        &length);
5132           ui_file_delete (buf);
5133
5134           if (cu->language == language_cplus)
5135             {
5136               char *cname
5137                 = dwarf2_canonicalize_name (name, cu,
5138                                             &cu->objfile->objfile_obstack);
5139
5140               if (cname != NULL)
5141                 name = cname;
5142             }
5143         }
5144     }
5145
5146   return name;
5147 }
5148
5149 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5150    If scope qualifiers are appropriate they will be added.  The result
5151    will be allocated on the objfile_obstack, or NULL if the DIE does
5152    not have a name.  NAME may either be from a previous call to
5153    dwarf2_name or NULL.
5154
5155    The output string will be canonicalized (if C++/Java).  */
5156
5157 static const char *
5158 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5159 {
5160   return dwarf2_compute_name (name, die, cu, 0);
5161 }
5162
5163 /* Construct a physname for the given DIE in CU.  NAME may either be
5164    from a previous call to dwarf2_name or NULL.  The result will be
5165    allocated on the objfile_objstack or NULL if the DIE does not have a
5166    name.
5167
5168    The output string will be canonicalized (if C++/Java).  */
5169
5170 static const char *
5171 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5172 {
5173   return dwarf2_compute_name (name, die, cu, 1);
5174 }
5175
5176 /* Read the import statement specified by the given die and record it.  */
5177
5178 static void
5179 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5180 {
5181   struct attribute *import_attr;
5182   struct die_info *imported_die;
5183   struct dwarf2_cu *imported_cu;
5184   const char *imported_name;
5185   const char *imported_name_prefix;
5186   const char *canonical_name;
5187   const char *import_alias;
5188   const char *imported_declaration = NULL;
5189   const char *import_prefix;
5190
5191   char *temp;
5192
5193   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5194   if (import_attr == NULL)
5195     {
5196       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5197                  dwarf_tag_name (die->tag));
5198       return;
5199     }
5200
5201   imported_cu = cu;
5202   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5203   imported_name = dwarf2_name (imported_die, imported_cu);
5204   if (imported_name == NULL)
5205     {
5206       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5207
5208         The import in the following code:
5209         namespace A
5210           {
5211             typedef int B;
5212           }
5213
5214         int main ()
5215           {
5216             using A::B;
5217             B b;
5218             return b;
5219           }
5220
5221         ...
5222          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5223             <52>   DW_AT_decl_file   : 1
5224             <53>   DW_AT_decl_line   : 6
5225             <54>   DW_AT_import      : <0x75>
5226          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5227             <59>   DW_AT_name        : B
5228             <5b>   DW_AT_decl_file   : 1
5229             <5c>   DW_AT_decl_line   : 2
5230             <5d>   DW_AT_type        : <0x6e>
5231         ...
5232          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5233             <76>   DW_AT_byte_size   : 4
5234             <77>   DW_AT_encoding    : 5        (signed)
5235
5236         imports the wrong die ( 0x75 instead of 0x58 ).
5237         This case will be ignored until the gcc bug is fixed.  */
5238       return;
5239     }
5240
5241   /* Figure out the local name after import.  */
5242   import_alias = dwarf2_name (die, cu);
5243
5244   /* Figure out where the statement is being imported to.  */
5245   import_prefix = determine_prefix (die, cu);
5246
5247   /* Figure out what the scope of the imported die is and prepend it
5248      to the name of the imported die.  */
5249   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5250
5251   if (imported_die->tag != DW_TAG_namespace
5252       && imported_die->tag != DW_TAG_module)
5253     {
5254       imported_declaration = imported_name;
5255       canonical_name = imported_name_prefix;
5256     }
5257   else if (strlen (imported_name_prefix) > 0)
5258     {
5259       temp = alloca (strlen (imported_name_prefix)
5260                      + 2 + strlen (imported_name) + 1);
5261       strcpy (temp, imported_name_prefix);
5262       strcat (temp, "::");
5263       strcat (temp, imported_name);
5264       canonical_name = temp;
5265     }
5266   else
5267     canonical_name = imported_name;
5268
5269   cp_add_using_directive (import_prefix,
5270                           canonical_name,
5271                           import_alias,
5272                           imported_declaration,
5273                           &cu->objfile->objfile_obstack);
5274 }
5275
5276 static void
5277 initialize_cu_func_list (struct dwarf2_cu *cu)
5278 {
5279   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5280 }
5281
5282 /* Cleanup function for read_file_scope.  */
5283
5284 static void
5285 free_cu_line_header (void *arg)
5286 {
5287   struct dwarf2_cu *cu = arg;
5288
5289   free_line_header (cu->line_header);
5290   cu->line_header = NULL;
5291 }
5292
5293 static void
5294 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5295                          char **name, char **comp_dir)
5296 {
5297   struct attribute *attr;
5298
5299   *name = NULL;
5300   *comp_dir = NULL;
5301
5302   /* Find the filename.  Do not use dwarf2_name here, since the filename
5303      is not a source language identifier.  */
5304   attr = dwarf2_attr (die, DW_AT_name, cu);
5305   if (attr)
5306     {
5307       *name = DW_STRING (attr);
5308     }
5309
5310   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5311   if (attr)
5312     *comp_dir = DW_STRING (attr);
5313   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5314     {
5315       *comp_dir = ldirname (*name);
5316       if (*comp_dir != NULL)
5317         make_cleanup (xfree, *comp_dir);
5318     }
5319   if (*comp_dir != NULL)
5320     {
5321       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5322          directory, get rid of it.  */
5323       char *cp = strchr (*comp_dir, ':');
5324
5325       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5326         *comp_dir = cp + 1;
5327     }
5328
5329   if (*name == NULL)
5330     *name = "<unknown>";
5331 }
5332
5333 /* Process DW_TAG_compile_unit.  */
5334
5335 static void
5336 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5337 {
5338   struct objfile *objfile = cu->objfile;
5339   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5340   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5341   CORE_ADDR highpc = ((CORE_ADDR) 0);
5342   struct attribute *attr;
5343   char *name = NULL;
5344   char *comp_dir = NULL;
5345   struct die_info *child_die;
5346   bfd *abfd = objfile->obfd;
5347   struct line_header *line_header = 0;
5348   CORE_ADDR baseaddr;
5349
5350   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5351
5352   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5353
5354   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5355      from finish_block.  */
5356   if (lowpc == ((CORE_ADDR) -1))
5357     lowpc = highpc;
5358   lowpc += baseaddr;
5359   highpc += baseaddr;
5360
5361   find_file_and_directory (die, cu, &name, &comp_dir);
5362
5363   attr = dwarf2_attr (die, DW_AT_language, cu);
5364   if (attr)
5365     {
5366       set_cu_language (DW_UNSND (attr), cu);
5367     }
5368
5369   attr = dwarf2_attr (die, DW_AT_producer, cu);
5370   if (attr)
5371     cu->producer = DW_STRING (attr);
5372
5373   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5374      standardised yet.  As a workaround for the language detection we fall
5375      back to the DW_AT_producer string.  */
5376   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5377     cu->language = language_opencl;
5378
5379   /* We assume that we're processing GCC output.  */
5380   processing_gcc_compilation = 2;
5381
5382   processing_has_namespace_info = 0;
5383
5384   start_symtab (name, comp_dir, lowpc);
5385   record_debugformat ("DWARF 2");
5386   record_producer (cu->producer);
5387
5388   initialize_cu_func_list (cu);
5389
5390   /* Decode line number information if present.  We do this before
5391      processing child DIEs, so that the line header table is available
5392      for DW_AT_decl_file.  */
5393   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5394   if (attr)
5395     {
5396       unsigned int line_offset = DW_UNSND (attr);
5397       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5398       if (line_header)
5399         {
5400           cu->line_header = line_header;
5401           make_cleanup (free_cu_line_header, cu);
5402           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5403         }
5404     }
5405
5406   /* Process all dies in compilation unit.  */
5407   if (die->child != NULL)
5408     {
5409       child_die = die->child;
5410       while (child_die && child_die->tag)
5411         {
5412           process_die (child_die, cu);
5413           child_die = sibling_die (child_die);
5414         }
5415     }
5416
5417   /* Decode macro information, if present.  Dwarf 2 macro information
5418      refers to information in the line number info statement program
5419      header, so we can only read it if we've read the header
5420      successfully.  */
5421   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5422   if (attr && line_header)
5423     {
5424       unsigned int macro_offset = DW_UNSND (attr);
5425
5426       dwarf_decode_macros (line_header, macro_offset,
5427                            comp_dir, abfd, cu);
5428     }
5429   do_cleanups (back_to);
5430 }
5431
5432 /* Process DW_TAG_type_unit.
5433    For TUs we want to skip the first top level sibling if it's not the
5434    actual type being defined by this TU.  In this case the first top
5435    level sibling is there to provide context only.  */
5436
5437 static void
5438 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5439 {
5440   struct objfile *objfile = cu->objfile;
5441   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5442   CORE_ADDR lowpc;
5443   struct attribute *attr;
5444   char *name = NULL;
5445   char *comp_dir = NULL;
5446   struct die_info *child_die;
5447   bfd *abfd = objfile->obfd;
5448
5449   /* start_symtab needs a low pc, but we don't really have one.
5450      Do what read_file_scope would do in the absence of such info.  */
5451   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5452
5453   /* Find the filename.  Do not use dwarf2_name here, since the filename
5454      is not a source language identifier.  */
5455   attr = dwarf2_attr (die, DW_AT_name, cu);
5456   if (attr)
5457     name = DW_STRING (attr);
5458
5459   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5460   if (attr)
5461     comp_dir = DW_STRING (attr);
5462   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5463     {
5464       comp_dir = ldirname (name);
5465       if (comp_dir != NULL)
5466         make_cleanup (xfree, comp_dir);
5467     }
5468
5469   if (name == NULL)
5470     name = "<unknown>";
5471
5472   attr = dwarf2_attr (die, DW_AT_language, cu);
5473   if (attr)
5474     set_cu_language (DW_UNSND (attr), cu);
5475
5476   /* This isn't technically needed today.  It is done for symmetry
5477      with read_file_scope.  */
5478   attr = dwarf2_attr (die, DW_AT_producer, cu);
5479   if (attr)
5480     cu->producer = DW_STRING (attr);
5481
5482   /* We assume that we're processing GCC output.  */
5483   processing_gcc_compilation = 2;
5484
5485   processing_has_namespace_info = 0;
5486
5487   start_symtab (name, comp_dir, lowpc);
5488   record_debugformat ("DWARF 2");
5489   record_producer (cu->producer);
5490
5491   /* Process the dies in the type unit.  */
5492   if (die->child == NULL)
5493     {
5494       dump_die_for_error (die);
5495       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5496              bfd_get_filename (abfd));
5497     }
5498
5499   child_die = die->child;
5500
5501   while (child_die && child_die->tag)
5502     {
5503       process_die (child_die, cu);
5504
5505       child_die = sibling_die (child_die);
5506     }
5507
5508   do_cleanups (back_to);
5509 }
5510
5511 static void
5512 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5513                      struct dwarf2_cu *cu)
5514 {
5515   struct function_range *thisfn;
5516
5517   thisfn = (struct function_range *)
5518     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5519   thisfn->name = name;
5520   thisfn->lowpc = lowpc;
5521   thisfn->highpc = highpc;
5522   thisfn->seen_line = 0;
5523   thisfn->next = NULL;
5524
5525   if (cu->last_fn == NULL)
5526       cu->first_fn = thisfn;
5527   else
5528       cu->last_fn->next = thisfn;
5529
5530   cu->last_fn = thisfn;
5531 }
5532
5533 /* qsort helper for inherit_abstract_dies.  */
5534
5535 static int
5536 unsigned_int_compar (const void *ap, const void *bp)
5537 {
5538   unsigned int a = *(unsigned int *) ap;
5539   unsigned int b = *(unsigned int *) bp;
5540
5541   return (a > b) - (b > a);
5542 }
5543
5544 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5545    Inherit only the children of the DW_AT_abstract_origin DIE not being
5546    already referenced by DW_AT_abstract_origin from the children of the
5547    current DIE.  */
5548
5549 static void
5550 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5551 {
5552   struct die_info *child_die;
5553   unsigned die_children_count;
5554   /* CU offsets which were referenced by children of the current DIE.  */
5555   unsigned *offsets;
5556   unsigned *offsets_end, *offsetp;
5557   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5558   struct die_info *origin_die;
5559   /* Iterator of the ORIGIN_DIE children.  */
5560   struct die_info *origin_child_die;
5561   struct cleanup *cleanups;
5562   struct attribute *attr;
5563   struct dwarf2_cu *origin_cu;
5564   struct pending **origin_previous_list_in_scope;
5565
5566   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5567   if (!attr)
5568     return;
5569
5570   /* Note that following die references may follow to a die in a
5571      different cu.  */
5572
5573   origin_cu = cu;
5574   origin_die = follow_die_ref (die, attr, &origin_cu);
5575
5576   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5577      symbols in.  */
5578   origin_previous_list_in_scope = origin_cu->list_in_scope;
5579   origin_cu->list_in_scope = cu->list_in_scope;
5580
5581   if (die->tag != origin_die->tag
5582       && !(die->tag == DW_TAG_inlined_subroutine
5583            && origin_die->tag == DW_TAG_subprogram))
5584     complaint (&symfile_complaints,
5585                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5586                die->offset, origin_die->offset);
5587
5588   child_die = die->child;
5589   die_children_count = 0;
5590   while (child_die && child_die->tag)
5591     {
5592       child_die = sibling_die (child_die);
5593       die_children_count++;
5594     }
5595   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5596   cleanups = make_cleanup (xfree, offsets);
5597
5598   offsets_end = offsets;
5599   child_die = die->child;
5600   while (child_die && child_die->tag)
5601     {
5602       /* For each CHILD_DIE, find the corresponding child of
5603          ORIGIN_DIE.  If there is more than one layer of
5604          DW_AT_abstract_origin, follow them all; there shouldn't be,
5605          but GCC versions at least through 4.4 generate this (GCC PR
5606          40573).  */
5607       struct die_info *child_origin_die = child_die;
5608       struct dwarf2_cu *child_origin_cu = cu;
5609
5610       while (1)
5611         {
5612           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5613                               child_origin_cu);
5614           if (attr == NULL)
5615             break;
5616           child_origin_die = follow_die_ref (child_origin_die, attr,
5617                                              &child_origin_cu);
5618         }
5619
5620       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5621          counterpart may exist.  */
5622       if (child_origin_die != child_die)
5623         {
5624           if (child_die->tag != child_origin_die->tag
5625               && !(child_die->tag == DW_TAG_inlined_subroutine
5626                    && child_origin_die->tag == DW_TAG_subprogram))
5627             complaint (&symfile_complaints,
5628                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5629                          "different tags"), child_die->offset,
5630                        child_origin_die->offset);
5631           if (child_origin_die->parent != origin_die)
5632             complaint (&symfile_complaints,
5633                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5634                          "different parents"), child_die->offset,
5635                        child_origin_die->offset);
5636           else
5637             *offsets_end++ = child_origin_die->offset;
5638         }
5639       child_die = sibling_die (child_die);
5640     }
5641   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5642          unsigned_int_compar);
5643   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5644     if (offsetp[-1] == *offsetp)
5645       complaint (&symfile_complaints,
5646                  _("Multiple children of DIE 0x%x refer "
5647                    "to DIE 0x%x as their abstract origin"),
5648                  die->offset, *offsetp);
5649
5650   offsetp = offsets;
5651   origin_child_die = origin_die->child;
5652   while (origin_child_die && origin_child_die->tag)
5653     {
5654       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5655       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5656         offsetp++;
5657       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5658         {
5659           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5660           process_die (origin_child_die, origin_cu);
5661         }
5662       origin_child_die = sibling_die (origin_child_die);
5663     }
5664   origin_cu->list_in_scope = origin_previous_list_in_scope;
5665
5666   do_cleanups (cleanups);
5667 }
5668
5669 static void
5670 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5671 {
5672   struct objfile *objfile = cu->objfile;
5673   struct context_stack *new;
5674   CORE_ADDR lowpc;
5675   CORE_ADDR highpc;
5676   struct die_info *child_die;
5677   struct attribute *attr, *call_line, *call_file;
5678   char *name;
5679   CORE_ADDR baseaddr;
5680   struct block *block;
5681   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5682   VEC (symbolp) *template_args = NULL;
5683   struct template_symbol *templ_func = NULL;
5684
5685   if (inlined_func)
5686     {
5687       /* If we do not have call site information, we can't show the
5688          caller of this inlined function.  That's too confusing, so
5689          only use the scope for local variables.  */
5690       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5691       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5692       if (call_line == NULL || call_file == NULL)
5693         {
5694           read_lexical_block_scope (die, cu);
5695           return;
5696         }
5697     }
5698
5699   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5700
5701   name = dwarf2_name (die, cu);
5702
5703   /* Ignore functions with missing or empty names.  These are actually
5704      illegal according to the DWARF standard.  */
5705   if (name == NULL)
5706     {
5707       complaint (&symfile_complaints,
5708                  _("missing name for subprogram DIE at %d"), die->offset);
5709       return;
5710     }
5711
5712   /* Ignore functions with missing or invalid low and high pc attributes.  */
5713   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5714     {
5715       attr = dwarf2_attr (die, DW_AT_external, cu);
5716       if (!attr || !DW_UNSND (attr))
5717         complaint (&symfile_complaints,
5718                    _("cannot get low and high bounds "
5719                      "for subprogram DIE at %d"),
5720                    die->offset);
5721       return;
5722     }
5723
5724   lowpc += baseaddr;
5725   highpc += baseaddr;
5726
5727   /* Record the function range for dwarf_decode_lines.  */
5728   add_to_cu_func_list (name, lowpc, highpc, cu);
5729
5730   /* If we have any template arguments, then we must allocate a
5731      different sort of symbol.  */
5732   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5733     {
5734       if (child_die->tag == DW_TAG_template_type_param
5735           || child_die->tag == DW_TAG_template_value_param)
5736         {
5737           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5738                                        struct template_symbol);
5739           templ_func->base.is_cplus_template_function = 1;
5740           break;
5741         }
5742     }
5743
5744   new = push_context (0, lowpc);
5745   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5746                                (struct symbol *) templ_func);
5747
5748   /* If there is a location expression for DW_AT_frame_base, record
5749      it.  */
5750   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5751   if (attr)
5752     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5753        expression is being recorded directly in the function's symbol
5754        and not in a separate frame-base object.  I guess this hack is
5755        to avoid adding some sort of frame-base adjunct/annex to the
5756        function's symbol :-(.  The problem with doing this is that it
5757        results in a function symbol with a location expression that
5758        has nothing to do with the location of the function, ouch!  The
5759        relationship should be: a function's symbol has-a frame base; a
5760        frame-base has-a location expression.  */
5761     dwarf2_symbol_mark_computed (attr, new->name, cu);
5762
5763   cu->list_in_scope = &local_symbols;
5764
5765   if (die->child != NULL)
5766     {
5767       child_die = die->child;
5768       while (child_die && child_die->tag)
5769         {
5770           if (child_die->tag == DW_TAG_template_type_param
5771               || child_die->tag == DW_TAG_template_value_param)
5772             {
5773               struct symbol *arg = new_symbol (child_die, NULL, cu);
5774
5775               if (arg != NULL)
5776                 VEC_safe_push (symbolp, template_args, arg);
5777             }
5778           else
5779             process_die (child_die, cu);
5780           child_die = sibling_die (child_die);
5781         }
5782     }
5783
5784   inherit_abstract_dies (die, cu);
5785
5786   /* If we have a DW_AT_specification, we might need to import using
5787      directives from the context of the specification DIE.  See the
5788      comment in determine_prefix.  */
5789   if (cu->language == language_cplus
5790       && dwarf2_attr (die, DW_AT_specification, cu))
5791     {
5792       struct dwarf2_cu *spec_cu = cu;
5793       struct die_info *spec_die = die_specification (die, &spec_cu);
5794
5795       while (spec_die)
5796         {
5797           child_die = spec_die->child;
5798           while (child_die && child_die->tag)
5799             {
5800               if (child_die->tag == DW_TAG_imported_module)
5801                 process_die (child_die, spec_cu);
5802               child_die = sibling_die (child_die);
5803             }
5804
5805           /* In some cases, GCC generates specification DIEs that
5806              themselves contain DW_AT_specification attributes.  */
5807           spec_die = die_specification (spec_die, &spec_cu);
5808         }
5809     }
5810
5811   new = pop_context ();
5812   /* Make a block for the local symbols within.  */
5813   block = finish_block (new->name, &local_symbols, new->old_blocks,
5814                         lowpc, highpc, objfile);
5815
5816   /* For C++, set the block's scope.  */
5817   if (cu->language == language_cplus || cu->language == language_fortran)
5818     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5819                         determine_prefix (die, cu),
5820                         processing_has_namespace_info);
5821
5822   /* If we have address ranges, record them.  */
5823   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5824
5825   /* Attach template arguments to function.  */
5826   if (! VEC_empty (symbolp, template_args))
5827     {
5828       gdb_assert (templ_func != NULL);
5829
5830       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5831       templ_func->template_arguments
5832         = obstack_alloc (&objfile->objfile_obstack,
5833                          (templ_func->n_template_arguments
5834                           * sizeof (struct symbol *)));
5835       memcpy (templ_func->template_arguments,
5836               VEC_address (symbolp, template_args),
5837               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5838       VEC_free (symbolp, template_args);
5839     }
5840
5841   /* In C++, we can have functions nested inside functions (e.g., when
5842      a function declares a class that has methods).  This means that
5843      when we finish processing a function scope, we may need to go
5844      back to building a containing block's symbol lists.  */
5845   local_symbols = new->locals;
5846   param_symbols = new->params;
5847   using_directives = new->using_directives;
5848
5849   /* If we've finished processing a top-level function, subsequent
5850      symbols go in the file symbol list.  */
5851   if (outermost_context_p ())
5852     cu->list_in_scope = &file_symbols;
5853 }
5854
5855 /* Process all the DIES contained within a lexical block scope.  Start
5856    a new scope, process the dies, and then close the scope.  */
5857
5858 static void
5859 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5860 {
5861   struct objfile *objfile = cu->objfile;
5862   struct context_stack *new;
5863   CORE_ADDR lowpc, highpc;
5864   struct die_info *child_die;
5865   CORE_ADDR baseaddr;
5866
5867   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5868
5869   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5870   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5871      as multiple lexical blocks?  Handling children in a sane way would
5872      be nasty.  Might be easier to properly extend generic blocks to
5873      describe ranges.  */
5874   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5875     return;
5876   lowpc += baseaddr;
5877   highpc += baseaddr;
5878
5879   push_context (0, lowpc);
5880   if (die->child != NULL)
5881     {
5882       child_die = die->child;
5883       while (child_die && child_die->tag)
5884         {
5885           process_die (child_die, cu);
5886           child_die = sibling_die (child_die);
5887         }
5888     }
5889   new = pop_context ();
5890
5891   if (local_symbols != NULL || using_directives != NULL)
5892     {
5893       struct block *block
5894         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5895                         highpc, objfile);
5896
5897       /* Note that recording ranges after traversing children, as we
5898          do here, means that recording a parent's ranges entails
5899          walking across all its children's ranges as they appear in
5900          the address map, which is quadratic behavior.
5901
5902          It would be nicer to record the parent's ranges before
5903          traversing its children, simply overriding whatever you find
5904          there.  But since we don't even decide whether to create a
5905          block until after we've traversed its children, that's hard
5906          to do.  */
5907       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5908     }
5909   local_symbols = new->locals;
5910   using_directives = new->using_directives;
5911 }
5912
5913 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5914    Return 1 if the attributes are present and valid, otherwise, return 0.
5915    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5916
5917 static int
5918 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5919                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5920                     struct partial_symtab *ranges_pst)
5921 {
5922   struct objfile *objfile = cu->objfile;
5923   struct comp_unit_head *cu_header = &cu->header;
5924   bfd *obfd = objfile->obfd;
5925   unsigned int addr_size = cu_header->addr_size;
5926   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5927   /* Base address selection entry.  */
5928   CORE_ADDR base;
5929   int found_base;
5930   unsigned int dummy;
5931   gdb_byte *buffer;
5932   CORE_ADDR marker;
5933   int low_set;
5934   CORE_ADDR low = 0;
5935   CORE_ADDR high = 0;
5936   CORE_ADDR baseaddr;
5937
5938   found_base = cu->base_known;
5939   base = cu->base_address;
5940
5941   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5942   if (offset >= dwarf2_per_objfile->ranges.size)
5943     {
5944       complaint (&symfile_complaints,
5945                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5946                  offset);
5947       return 0;
5948     }
5949   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5950
5951   /* Read in the largest possible address.  */
5952   marker = read_address (obfd, buffer, cu, &dummy);
5953   if ((marker & mask) == mask)
5954     {
5955       /* If we found the largest possible address, then
5956          read the base address.  */
5957       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5958       buffer += 2 * addr_size;
5959       offset += 2 * addr_size;
5960       found_base = 1;
5961     }
5962
5963   low_set = 0;
5964
5965   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5966
5967   while (1)
5968     {
5969       CORE_ADDR range_beginning, range_end;
5970
5971       range_beginning = read_address (obfd, buffer, cu, &dummy);
5972       buffer += addr_size;
5973       range_end = read_address (obfd, buffer, cu, &dummy);
5974       buffer += addr_size;
5975       offset += 2 * addr_size;
5976
5977       /* An end of list marker is a pair of zero addresses.  */
5978       if (range_beginning == 0 && range_end == 0)
5979         /* Found the end of list entry.  */
5980         break;
5981
5982       /* Each base address selection entry is a pair of 2 values.
5983          The first is the largest possible address, the second is
5984          the base address.  Check for a base address here.  */
5985       if ((range_beginning & mask) == mask)
5986         {
5987           /* If we found the largest possible address, then
5988              read the base address.  */
5989           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5990           found_base = 1;
5991           continue;
5992         }
5993
5994       if (!found_base)
5995         {
5996           /* We have no valid base address for the ranges
5997              data.  */
5998           complaint (&symfile_complaints,
5999                      _("Invalid .debug_ranges data (no base address)"));
6000           return 0;
6001         }
6002
6003       if (range_beginning > range_end)
6004         {
6005           /* Inverted range entries are invalid.  */
6006           complaint (&symfile_complaints,
6007                      _("Invalid .debug_ranges data (inverted range)"));
6008           return 0;
6009         }
6010
6011       /* Empty range entries have no effect.  */
6012       if (range_beginning == range_end)
6013         continue;
6014
6015       range_beginning += base;
6016       range_end += base;
6017
6018       if (ranges_pst != NULL)
6019         addrmap_set_empty (objfile->psymtabs_addrmap,
6020                            range_beginning + baseaddr,
6021                            range_end - 1 + baseaddr,
6022                            ranges_pst);
6023
6024       /* FIXME: This is recording everything as a low-high
6025          segment of consecutive addresses.  We should have a
6026          data structure for discontiguous block ranges
6027          instead.  */
6028       if (! low_set)
6029         {
6030           low = range_beginning;
6031           high = range_end;
6032           low_set = 1;
6033         }
6034       else
6035         {
6036           if (range_beginning < low)
6037             low = range_beginning;
6038           if (range_end > high)
6039             high = range_end;
6040         }
6041     }
6042
6043   if (! low_set)
6044     /* If the first entry is an end-of-list marker, the range
6045        describes an empty scope, i.e. no instructions.  */
6046     return 0;
6047
6048   if (low_return)
6049     *low_return = low;
6050   if (high_return)
6051     *high_return = high;
6052   return 1;
6053 }
6054
6055 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6056    are present and valid, otherwise, return 0.  Return -1 if the range is
6057    discontinuous, i.e. derived from DW_AT_ranges information.  */
6058 static int
6059 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6060                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6061                       struct partial_symtab *pst)
6062 {
6063   struct attribute *attr;
6064   CORE_ADDR low = 0;
6065   CORE_ADDR high = 0;
6066   int ret = 0;
6067
6068   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6069   if (attr)
6070     {
6071       high = DW_ADDR (attr);
6072       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6073       if (attr)
6074         low = DW_ADDR (attr);
6075       else
6076         /* Found high w/o low attribute.  */
6077         return 0;
6078
6079       /* Found consecutive range of addresses.  */
6080       ret = 1;
6081     }
6082   else
6083     {
6084       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6085       if (attr != NULL)
6086         {
6087           /* Value of the DW_AT_ranges attribute is the offset in the
6088              .debug_ranges section.  */
6089           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6090             return 0;
6091           /* Found discontinuous range of addresses.  */
6092           ret = -1;
6093         }
6094     }
6095
6096   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6097   if (high <= low)
6098     return 0;
6099
6100   /* When using the GNU linker, .gnu.linkonce. sections are used to
6101      eliminate duplicate copies of functions and vtables and such.
6102      The linker will arbitrarily choose one and discard the others.
6103      The AT_*_pc values for such functions refer to local labels in
6104      these sections.  If the section from that file was discarded, the
6105      labels are not in the output, so the relocs get a value of 0.
6106      If this is a discarded function, mark the pc bounds as invalid,
6107      so that GDB will ignore it.  */
6108   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6109     return 0;
6110
6111   *lowpc = low;
6112   *highpc = high;
6113   return ret;
6114 }
6115
6116 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6117    its low and high PC addresses.  Do nothing if these addresses could not
6118    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6119    and HIGHPC to the high address if greater than HIGHPC.  */
6120
6121 static void
6122 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6123                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6124                                  struct dwarf2_cu *cu)
6125 {
6126   CORE_ADDR low, high;
6127   struct die_info *child = die->child;
6128
6129   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6130     {
6131       *lowpc = min (*lowpc, low);
6132       *highpc = max (*highpc, high);
6133     }
6134
6135   /* If the language does not allow nested subprograms (either inside
6136      subprograms or lexical blocks), we're done.  */
6137   if (cu->language != language_ada)
6138     return;
6139
6140   /* Check all the children of the given DIE.  If it contains nested
6141      subprograms, then check their pc bounds.  Likewise, we need to
6142      check lexical blocks as well, as they may also contain subprogram
6143      definitions.  */
6144   while (child && child->tag)
6145     {
6146       if (child->tag == DW_TAG_subprogram
6147           || child->tag == DW_TAG_lexical_block)
6148         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6149       child = sibling_die (child);
6150     }
6151 }
6152
6153 /* Get the low and high pc's represented by the scope DIE, and store
6154    them in *LOWPC and *HIGHPC.  If the correct values can't be
6155    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6156
6157 static void
6158 get_scope_pc_bounds (struct die_info *die,
6159                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6160                      struct dwarf2_cu *cu)
6161 {
6162   CORE_ADDR best_low = (CORE_ADDR) -1;
6163   CORE_ADDR best_high = (CORE_ADDR) 0;
6164   CORE_ADDR current_low, current_high;
6165
6166   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6167     {
6168       best_low = current_low;
6169       best_high = current_high;
6170     }
6171   else
6172     {
6173       struct die_info *child = die->child;
6174
6175       while (child && child->tag)
6176         {
6177           switch (child->tag) {
6178           case DW_TAG_subprogram:
6179             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6180             break;
6181           case DW_TAG_namespace:
6182           case DW_TAG_module:
6183             /* FIXME: carlton/2004-01-16: Should we do this for
6184                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6185                that current GCC's always emit the DIEs corresponding
6186                to definitions of methods of classes as children of a
6187                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6188                the DIEs giving the declarations, which could be
6189                anywhere).  But I don't see any reason why the
6190                standards says that they have to be there.  */
6191             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6192
6193             if (current_low != ((CORE_ADDR) -1))
6194               {
6195                 best_low = min (best_low, current_low);
6196                 best_high = max (best_high, current_high);
6197               }
6198             break;
6199           default:
6200             /* Ignore.  */
6201             break;
6202           }
6203
6204           child = sibling_die (child);
6205         }
6206     }
6207
6208   *lowpc = best_low;
6209   *highpc = best_high;
6210 }
6211
6212 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6213    in DIE.  */
6214 static void
6215 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6216                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6217 {
6218   struct attribute *attr;
6219
6220   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6221   if (attr)
6222     {
6223       CORE_ADDR high = DW_ADDR (attr);
6224
6225       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6226       if (attr)
6227         {
6228           CORE_ADDR low = DW_ADDR (attr);
6229
6230           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6231         }
6232     }
6233
6234   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6235   if (attr)
6236     {
6237       bfd *obfd = cu->objfile->obfd;
6238
6239       /* The value of the DW_AT_ranges attribute is the offset of the
6240          address range list in the .debug_ranges section.  */
6241       unsigned long offset = DW_UNSND (attr);
6242       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6243
6244       /* For some target architectures, but not others, the
6245          read_address function sign-extends the addresses it returns.
6246          To recognize base address selection entries, we need a
6247          mask.  */
6248       unsigned int addr_size = cu->header.addr_size;
6249       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6250
6251       /* The base address, to which the next pair is relative.  Note
6252          that this 'base' is a DWARF concept: most entries in a range
6253          list are relative, to reduce the number of relocs against the
6254          debugging information.  This is separate from this function's
6255          'baseaddr' argument, which GDB uses to relocate debugging
6256          information from a shared library based on the address at
6257          which the library was loaded.  */
6258       CORE_ADDR base = cu->base_address;
6259       int base_known = cu->base_known;
6260
6261       gdb_assert (dwarf2_per_objfile->ranges.readin);
6262       if (offset >= dwarf2_per_objfile->ranges.size)
6263         {
6264           complaint (&symfile_complaints,
6265                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6266                      offset);
6267           return;
6268         }
6269
6270       for (;;)
6271         {
6272           unsigned int bytes_read;
6273           CORE_ADDR start, end;
6274
6275           start = read_address (obfd, buffer, cu, &bytes_read);
6276           buffer += bytes_read;
6277           end = read_address (obfd, buffer, cu, &bytes_read);
6278           buffer += bytes_read;
6279
6280           /* Did we find the end of the range list?  */
6281           if (start == 0 && end == 0)
6282             break;
6283
6284           /* Did we find a base address selection entry?  */
6285           else if ((start & base_select_mask) == base_select_mask)
6286             {
6287               base = end;
6288               base_known = 1;
6289             }
6290
6291           /* We found an ordinary address range.  */
6292           else
6293             {
6294               if (!base_known)
6295                 {
6296                   complaint (&symfile_complaints,
6297                              _("Invalid .debug_ranges data "
6298                                "(no base address)"));
6299                   return;
6300                 }
6301
6302               if (start > end)
6303                 {
6304                   /* Inverted range entries are invalid.  */
6305                   complaint (&symfile_complaints,
6306                              _("Invalid .debug_ranges data "
6307                                "(inverted range)"));
6308                   return;
6309                 }
6310
6311               /* Empty range entries have no effect.  */
6312               if (start == end)
6313                 continue;
6314
6315               record_block_range (block,
6316                                   baseaddr + base + start,
6317                                   baseaddr + base + end - 1);
6318             }
6319         }
6320     }
6321 }
6322
6323 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6324    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6325    during 4.6.0 experimental.  */
6326
6327 static int
6328 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6329 {
6330   const char *cs;
6331   int major, minor, release;
6332
6333   if (cu->producer == NULL)
6334     {
6335       /* For unknown compilers expect their behavior is DWARF version
6336          compliant.
6337
6338          GCC started to support .debug_types sections by -gdwarf-4 since
6339          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6340          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6341          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6342          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6343
6344       return 0;
6345     }
6346
6347   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6348
6349   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6350     {
6351       /* For non-GCC compilers expect their behavior is DWARF version
6352          compliant.  */
6353
6354       return 0;
6355     }
6356   cs = &cu->producer[strlen ("GNU ")];
6357   while (*cs && !isdigit (*cs))
6358     cs++;
6359   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6360     {
6361       /* Not recognized as GCC.  */
6362
6363       return 0;
6364     }
6365
6366   return major < 4 || (major == 4 && minor < 6);
6367 }
6368
6369 /* Return the default accessibility type if it is not overriden by
6370    DW_AT_accessibility.  */
6371
6372 static enum dwarf_access_attribute
6373 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6374 {
6375   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6376     {
6377       /* The default DWARF 2 accessibility for members is public, the default
6378          accessibility for inheritance is private.  */
6379
6380       if (die->tag != DW_TAG_inheritance)
6381         return DW_ACCESS_public;
6382       else
6383         return DW_ACCESS_private;
6384     }
6385   else
6386     {
6387       /* DWARF 3+ defines the default accessibility a different way.  The same
6388          rules apply now for DW_TAG_inheritance as for the members and it only
6389          depends on the container kind.  */
6390
6391       if (die->parent->tag == DW_TAG_class_type)
6392         return DW_ACCESS_private;
6393       else
6394         return DW_ACCESS_public;
6395     }
6396 }
6397
6398 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6399    offset.  If the attribute was not found return 0, otherwise return
6400    1.  If it was found but could not properly be handled, set *OFFSET
6401    to 0.  */
6402
6403 static int
6404 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6405                              LONGEST *offset)
6406 {
6407   struct attribute *attr;
6408
6409   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6410   if (attr != NULL)
6411     {
6412       *offset = 0;
6413
6414       /* Note that we do not check for a section offset first here.
6415          This is because DW_AT_data_member_location is new in DWARF 4,
6416          so if we see it, we can assume that a constant form is really
6417          a constant and not a section offset.  */
6418       if (attr_form_is_constant (attr))
6419         *offset = dwarf2_get_attr_constant_value (attr, 0);
6420       else if (attr_form_is_section_offset (attr))
6421         dwarf2_complex_location_expr_complaint ();
6422       else if (attr_form_is_block (attr))
6423         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6424       else
6425         dwarf2_complex_location_expr_complaint ();
6426
6427       return 1;
6428     }
6429
6430   return 0;
6431 }
6432
6433 /* Add an aggregate field to the field list.  */
6434
6435 static void
6436 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6437                   struct dwarf2_cu *cu)
6438 {
6439   struct objfile *objfile = cu->objfile;
6440   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6441   struct nextfield *new_field;
6442   struct attribute *attr;
6443   struct field *fp;
6444   char *fieldname = "";
6445
6446   /* Allocate a new field list entry and link it in.  */
6447   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6448   make_cleanup (xfree, new_field);
6449   memset (new_field, 0, sizeof (struct nextfield));
6450
6451   if (die->tag == DW_TAG_inheritance)
6452     {
6453       new_field->next = fip->baseclasses;
6454       fip->baseclasses = new_field;
6455     }
6456   else
6457     {
6458       new_field->next = fip->fields;
6459       fip->fields = new_field;
6460     }
6461   fip->nfields++;
6462
6463   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6464   if (attr)
6465     new_field->accessibility = DW_UNSND (attr);
6466   else
6467     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6468   if (new_field->accessibility != DW_ACCESS_public)
6469     fip->non_public_fields = 1;
6470
6471   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6472   if (attr)
6473     new_field->virtuality = DW_UNSND (attr);
6474   else
6475     new_field->virtuality = DW_VIRTUALITY_none;
6476
6477   fp = &new_field->field;
6478
6479   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6480     {
6481       LONGEST offset;
6482
6483       /* Data member other than a C++ static data member.  */
6484
6485       /* Get type of field.  */
6486       fp->type = die_type (die, cu);
6487
6488       SET_FIELD_BITPOS (*fp, 0);
6489
6490       /* Get bit size of field (zero if none).  */
6491       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6492       if (attr)
6493         {
6494           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6495         }
6496       else
6497         {
6498           FIELD_BITSIZE (*fp) = 0;
6499         }
6500
6501       /* Get bit offset of field.  */
6502       if (handle_data_member_location (die, cu, &offset))
6503         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6504       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6505       if (attr)
6506         {
6507           if (gdbarch_bits_big_endian (gdbarch))
6508             {
6509               /* For big endian bits, the DW_AT_bit_offset gives the
6510                  additional bit offset from the MSB of the containing
6511                  anonymous object to the MSB of the field.  We don't
6512                  have to do anything special since we don't need to
6513                  know the size of the anonymous object.  */
6514               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6515             }
6516           else
6517             {
6518               /* For little endian bits, compute the bit offset to the
6519                  MSB of the anonymous object, subtract off the number of
6520                  bits from the MSB of the field to the MSB of the
6521                  object, and then subtract off the number of bits of
6522                  the field itself.  The result is the bit offset of
6523                  the LSB of the field.  */
6524               int anonymous_size;
6525               int bit_offset = DW_UNSND (attr);
6526
6527               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6528               if (attr)
6529                 {
6530                   /* The size of the anonymous object containing
6531                      the bit field is explicit, so use the
6532                      indicated size (in bytes).  */
6533                   anonymous_size = DW_UNSND (attr);
6534                 }
6535               else
6536                 {
6537                   /* The size of the anonymous object containing
6538                      the bit field must be inferred from the type
6539                      attribute of the data member containing the
6540                      bit field.  */
6541                   anonymous_size = TYPE_LENGTH (fp->type);
6542                 }
6543               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6544                 - bit_offset - FIELD_BITSIZE (*fp);
6545             }
6546         }
6547
6548       /* Get name of field.  */
6549       fieldname = dwarf2_name (die, cu);
6550       if (fieldname == NULL)
6551         fieldname = "";
6552
6553       /* The name is already allocated along with this objfile, so we don't
6554          need to duplicate it for the type.  */
6555       fp->name = fieldname;
6556
6557       /* Change accessibility for artificial fields (e.g. virtual table
6558          pointer or virtual base class pointer) to private.  */
6559       if (dwarf2_attr (die, DW_AT_artificial, cu))
6560         {
6561           FIELD_ARTIFICIAL (*fp) = 1;
6562           new_field->accessibility = DW_ACCESS_private;
6563           fip->non_public_fields = 1;
6564         }
6565     }
6566   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6567     {
6568       /* C++ static member.  */
6569
6570       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6571          is a declaration, but all versions of G++ as of this writing
6572          (so through at least 3.2.1) incorrectly generate
6573          DW_TAG_variable tags.  */
6574
6575       const char *physname;
6576
6577       /* Get name of field.  */
6578       fieldname = dwarf2_name (die, cu);
6579       if (fieldname == NULL)
6580         return;
6581
6582       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6583       if (attr
6584           /* Only create a symbol if this is an external value.
6585              new_symbol checks this and puts the value in the global symbol
6586              table, which we want.  If it is not external, new_symbol
6587              will try to put the value in cu->list_in_scope which is wrong.  */
6588           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6589         {
6590           /* A static const member, not much different than an enum as far as
6591              we're concerned, except that we can support more types.  */
6592           new_symbol (die, NULL, cu);
6593         }
6594
6595       /* Get physical name.  */
6596       physname = dwarf2_physname (fieldname, die, cu);
6597
6598       /* The name is already allocated along with this objfile, so we don't
6599          need to duplicate it for the type.  */
6600       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6601       FIELD_TYPE (*fp) = die_type (die, cu);
6602       FIELD_NAME (*fp) = fieldname;
6603     }
6604   else if (die->tag == DW_TAG_inheritance)
6605     {
6606       LONGEST offset;
6607
6608       /* C++ base class field.  */
6609       if (handle_data_member_location (die, cu, &offset))
6610         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6611       FIELD_BITSIZE (*fp) = 0;
6612       FIELD_TYPE (*fp) = die_type (die, cu);
6613       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6614       fip->nbaseclasses++;
6615     }
6616 }
6617
6618 /* Add a typedef defined in the scope of the FIP's class.  */
6619
6620 static void
6621 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6622                     struct dwarf2_cu *cu)
6623 {
6624   struct objfile *objfile = cu->objfile;
6625   struct typedef_field_list *new_field;
6626   struct attribute *attr;
6627   struct typedef_field *fp;
6628   char *fieldname = "";
6629
6630   /* Allocate a new field list entry and link it in.  */
6631   new_field = xzalloc (sizeof (*new_field));
6632   make_cleanup (xfree, new_field);
6633
6634   gdb_assert (die->tag == DW_TAG_typedef);
6635
6636   fp = &new_field->field;
6637
6638   /* Get name of field.  */
6639   fp->name = dwarf2_name (die, cu);
6640   if (fp->name == NULL)
6641     return;
6642
6643   fp->type = read_type_die (die, cu);
6644
6645   new_field->next = fip->typedef_field_list;
6646   fip->typedef_field_list = new_field;
6647   fip->typedef_field_list_count++;
6648 }
6649
6650 /* Create the vector of fields, and attach it to the type.  */
6651
6652 static void
6653 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6654                               struct dwarf2_cu *cu)
6655 {
6656   int nfields = fip->nfields;
6657
6658   /* Record the field count, allocate space for the array of fields,
6659      and create blank accessibility bitfields if necessary.  */
6660   TYPE_NFIELDS (type) = nfields;
6661   TYPE_FIELDS (type) = (struct field *)
6662     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6663   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6664
6665   if (fip->non_public_fields && cu->language != language_ada)
6666     {
6667       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6668
6669       TYPE_FIELD_PRIVATE_BITS (type) =
6670         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6671       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6672
6673       TYPE_FIELD_PROTECTED_BITS (type) =
6674         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6675       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6676
6677       TYPE_FIELD_IGNORE_BITS (type) =
6678         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6679       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6680     }
6681
6682   /* If the type has baseclasses, allocate and clear a bit vector for
6683      TYPE_FIELD_VIRTUAL_BITS.  */
6684   if (fip->nbaseclasses && cu->language != language_ada)
6685     {
6686       int num_bytes = B_BYTES (fip->nbaseclasses);
6687       unsigned char *pointer;
6688
6689       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6690       pointer = TYPE_ALLOC (type, num_bytes);
6691       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6692       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6693       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6694     }
6695
6696   /* Copy the saved-up fields into the field vector.  Start from the head of
6697      the list, adding to the tail of the field array, so that they end up in
6698      the same order in the array in which they were added to the list.  */
6699   while (nfields-- > 0)
6700     {
6701       struct nextfield *fieldp;
6702
6703       if (fip->fields)
6704         {
6705           fieldp = fip->fields;
6706           fip->fields = fieldp->next;
6707         }
6708       else
6709         {
6710           fieldp = fip->baseclasses;
6711           fip->baseclasses = fieldp->next;
6712         }
6713
6714       TYPE_FIELD (type, nfields) = fieldp->field;
6715       switch (fieldp->accessibility)
6716         {
6717         case DW_ACCESS_private:
6718           if (cu->language != language_ada)
6719             SET_TYPE_FIELD_PRIVATE (type, nfields);
6720           break;
6721
6722         case DW_ACCESS_protected:
6723           if (cu->language != language_ada)
6724             SET_TYPE_FIELD_PROTECTED (type, nfields);
6725           break;
6726
6727         case DW_ACCESS_public:
6728           break;
6729
6730         default:
6731           /* Unknown accessibility.  Complain and treat it as public.  */
6732           {
6733             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6734                        fieldp->accessibility);
6735           }
6736           break;
6737         }
6738       if (nfields < fip->nbaseclasses)
6739         {
6740           switch (fieldp->virtuality)
6741             {
6742             case DW_VIRTUALITY_virtual:
6743             case DW_VIRTUALITY_pure_virtual:
6744               if (cu->language == language_ada)
6745                 error (_("unexpected virtuality in component of Ada type"));
6746               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6747               break;
6748             }
6749         }
6750     }
6751 }
6752
6753 /* Add a member function to the proper fieldlist.  */
6754
6755 static void
6756 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6757                       struct type *type, struct dwarf2_cu *cu)
6758 {
6759   struct objfile *objfile = cu->objfile;
6760   struct attribute *attr;
6761   struct fnfieldlist *flp;
6762   int i;
6763   struct fn_field *fnp;
6764   char *fieldname;
6765   struct nextfnfield *new_fnfield;
6766   struct type *this_type;
6767   enum dwarf_access_attribute accessibility;
6768
6769   if (cu->language == language_ada)
6770     error (_("unexpected member function in Ada type"));
6771
6772   /* Get name of member function.  */
6773   fieldname = dwarf2_name (die, cu);
6774   if (fieldname == NULL)
6775     return;
6776
6777   /* Look up member function name in fieldlist.  */
6778   for (i = 0; i < fip->nfnfields; i++)
6779     {
6780       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6781         break;
6782     }
6783
6784   /* Create new list element if necessary.  */
6785   if (i < fip->nfnfields)
6786     flp = &fip->fnfieldlists[i];
6787   else
6788     {
6789       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6790         {
6791           fip->fnfieldlists = (struct fnfieldlist *)
6792             xrealloc (fip->fnfieldlists,
6793                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6794                       * sizeof (struct fnfieldlist));
6795           if (fip->nfnfields == 0)
6796             make_cleanup (free_current_contents, &fip->fnfieldlists);
6797         }
6798       flp = &fip->fnfieldlists[fip->nfnfields];
6799       flp->name = fieldname;
6800       flp->length = 0;
6801       flp->head = NULL;
6802       i = fip->nfnfields++;
6803     }
6804
6805   /* Create a new member function field and chain it to the field list
6806      entry.  */
6807   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6808   make_cleanup (xfree, new_fnfield);
6809   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6810   new_fnfield->next = flp->head;
6811   flp->head = new_fnfield;
6812   flp->length++;
6813
6814   /* Fill in the member function field info.  */
6815   fnp = &new_fnfield->fnfield;
6816
6817   /* Delay processing of the physname until later.  */
6818   if (cu->language == language_cplus || cu->language == language_java)
6819     {
6820       add_to_method_list (type, i, flp->length - 1, fieldname,
6821                           die, cu);
6822     }
6823   else
6824     {
6825       const char *physname = dwarf2_physname (fieldname, die, cu);
6826       fnp->physname = physname ? physname : "";
6827     }
6828
6829   fnp->type = alloc_type (objfile);
6830   this_type = read_type_die (die, cu);
6831   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6832     {
6833       int nparams = TYPE_NFIELDS (this_type);
6834
6835       /* TYPE is the domain of this method, and THIS_TYPE is the type
6836            of the method itself (TYPE_CODE_METHOD).  */
6837       smash_to_method_type (fnp->type, type,
6838                             TYPE_TARGET_TYPE (this_type),
6839                             TYPE_FIELDS (this_type),
6840                             TYPE_NFIELDS (this_type),
6841                             TYPE_VARARGS (this_type));
6842
6843       /* Handle static member functions.
6844          Dwarf2 has no clean way to discern C++ static and non-static
6845          member functions.  G++ helps GDB by marking the first
6846          parameter for non-static member functions (which is the this
6847          pointer) as artificial.  We obtain this information from
6848          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6849       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6850         fnp->voffset = VOFFSET_STATIC;
6851     }
6852   else
6853     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6854                dwarf2_full_name (fieldname, die, cu));
6855
6856   /* Get fcontext from DW_AT_containing_type if present.  */
6857   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6858     fnp->fcontext = die_containing_type (die, cu);
6859
6860   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6861      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6862
6863   /* Get accessibility.  */
6864   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6865   if (attr)
6866     accessibility = DW_UNSND (attr);
6867   else
6868     accessibility = dwarf2_default_access_attribute (die, cu);
6869   switch (accessibility)
6870     {
6871     case DW_ACCESS_private:
6872       fnp->is_private = 1;
6873       break;
6874     case DW_ACCESS_protected:
6875       fnp->is_protected = 1;
6876       break;
6877     }
6878
6879   /* Check for artificial methods.  */
6880   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6881   if (attr && DW_UNSND (attr) != 0)
6882     fnp->is_artificial = 1;
6883
6884   /* Get index in virtual function table if it is a virtual member
6885      function.  For older versions of GCC, this is an offset in the
6886      appropriate virtual table, as specified by DW_AT_containing_type.
6887      For everyone else, it is an expression to be evaluated relative
6888      to the object address.  */
6889
6890   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6891   if (attr)
6892     {
6893       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6894         {
6895           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6896             {
6897               /* Old-style GCC.  */
6898               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6899             }
6900           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6901                    || (DW_BLOCK (attr)->size > 1
6902                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6903                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6904             {
6905               struct dwarf_block blk;
6906               int offset;
6907
6908               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6909                         ? 1 : 2);
6910               blk.size = DW_BLOCK (attr)->size - offset;
6911               blk.data = DW_BLOCK (attr)->data + offset;
6912               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6913               if ((fnp->voffset % cu->header.addr_size) != 0)
6914                 dwarf2_complex_location_expr_complaint ();
6915               else
6916                 fnp->voffset /= cu->header.addr_size;
6917               fnp->voffset += 2;
6918             }
6919           else
6920             dwarf2_complex_location_expr_complaint ();
6921
6922           if (!fnp->fcontext)
6923             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6924         }
6925       else if (attr_form_is_section_offset (attr))
6926         {
6927           dwarf2_complex_location_expr_complaint ();
6928         }
6929       else
6930         {
6931           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6932                                                  fieldname);
6933         }
6934     }
6935   else
6936     {
6937       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6938       if (attr && DW_UNSND (attr))
6939         {
6940           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6941           complaint (&symfile_complaints,
6942                      _("Member function \"%s\" (offset %d) is virtual "
6943                        "but the vtable offset is not specified"),
6944                      fieldname, die->offset);
6945           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6946           TYPE_CPLUS_DYNAMIC (type) = 1;
6947         }
6948     }
6949 }
6950
6951 /* Create the vector of member function fields, and attach it to the type.  */
6952
6953 static void
6954 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6955                                  struct dwarf2_cu *cu)
6956 {
6957   struct fnfieldlist *flp;
6958   int total_length = 0;
6959   int i;
6960
6961   if (cu->language == language_ada)
6962     error (_("unexpected member functions in Ada type"));
6963
6964   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6965   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6966     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6967
6968   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6969     {
6970       struct nextfnfield *nfp = flp->head;
6971       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6972       int k;
6973
6974       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6975       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6976       fn_flp->fn_fields = (struct fn_field *)
6977         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6978       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6979         fn_flp->fn_fields[k] = nfp->fnfield;
6980
6981       total_length += flp->length;
6982     }
6983
6984   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6985   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6986 }
6987
6988 /* Returns non-zero if NAME is the name of a vtable member in CU's
6989    language, zero otherwise.  */
6990 static int
6991 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6992 {
6993   static const char vptr[] = "_vptr";
6994   static const char vtable[] = "vtable";
6995
6996   /* Look for the C++ and Java forms of the vtable.  */
6997   if ((cu->language == language_java
6998        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6999        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7000        && is_cplus_marker (name[sizeof (vptr) - 1])))
7001     return 1;
7002
7003   return 0;
7004 }
7005
7006 /* GCC outputs unnamed structures that are really pointers to member
7007    functions, with the ABI-specified layout.  If TYPE describes
7008    such a structure, smash it into a member function type.
7009
7010    GCC shouldn't do this; it should just output pointer to member DIEs.
7011    This is GCC PR debug/28767.  */
7012
7013 static void
7014 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7015 {
7016   struct type *pfn_type, *domain_type, *new_type;
7017
7018   /* Check for a structure with no name and two children.  */
7019   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7020     return;
7021
7022   /* Check for __pfn and __delta members.  */
7023   if (TYPE_FIELD_NAME (type, 0) == NULL
7024       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7025       || TYPE_FIELD_NAME (type, 1) == NULL
7026       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7027     return;
7028
7029   /* Find the type of the method.  */
7030   pfn_type = TYPE_FIELD_TYPE (type, 0);
7031   if (pfn_type == NULL
7032       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7033       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7034     return;
7035
7036   /* Look for the "this" argument.  */
7037   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7038   if (TYPE_NFIELDS (pfn_type) == 0
7039       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7040       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7041     return;
7042
7043   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7044   new_type = alloc_type (objfile);
7045   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7046                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7047                         TYPE_VARARGS (pfn_type));
7048   smash_to_methodptr_type (type, new_type);
7049 }
7050
7051 /* Called when we find the DIE that starts a structure or union scope
7052    (definition) to create a type for the structure or union.  Fill in
7053    the type's name and general properties; the members will not be
7054    processed until process_structure_type.
7055
7056    NOTE: we need to call these functions regardless of whether or not the
7057    DIE has a DW_AT_name attribute, since it might be an anonymous
7058    structure or union.  This gets the type entered into our set of
7059    user defined types.
7060
7061    However, if the structure is incomplete (an opaque struct/union)
7062    then suppress creating a symbol table entry for it since gdb only
7063    wants to find the one with the complete definition.  Note that if
7064    it is complete, we just call new_symbol, which does it's own
7065    checking about whether the struct/union is anonymous or not (and
7066    suppresses creating a symbol table entry itself).  */
7067
7068 static struct type *
7069 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7070 {
7071   struct objfile *objfile = cu->objfile;
7072   struct type *type;
7073   struct attribute *attr;
7074   char *name;
7075
7076   /* If the definition of this type lives in .debug_types, read that type.
7077      Don't follow DW_AT_specification though, that will take us back up
7078      the chain and we want to go down.  */
7079   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7080   if (attr)
7081     {
7082       struct dwarf2_cu *type_cu = cu;
7083       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7084
7085       /* We could just recurse on read_structure_type, but we need to call
7086          get_die_type to ensure only one type for this DIE is created.
7087          This is important, for example, because for c++ classes we need
7088          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7089       type = read_type_die (type_die, type_cu);
7090
7091       /* TYPE_CU may not be the same as CU.
7092          Ensure TYPE is recorded in CU's type_hash table.  */
7093       return set_die_type (die, type, cu);
7094     }
7095
7096   type = alloc_type (objfile);
7097   INIT_CPLUS_SPECIFIC (type);
7098
7099   name = dwarf2_name (die, cu);
7100   if (name != NULL)
7101     {
7102       if (cu->language == language_cplus
7103           || cu->language == language_java)
7104         {
7105           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7106
7107           /* dwarf2_full_name might have already finished building the DIE's
7108              type.  If so, there is no need to continue.  */
7109           if (get_die_type (die, cu) != NULL)
7110             return get_die_type (die, cu);
7111
7112           TYPE_TAG_NAME (type) = full_name;
7113           if (die->tag == DW_TAG_structure_type
7114               || die->tag == DW_TAG_class_type)
7115             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7116         }
7117       else
7118         {
7119           /* The name is already allocated along with this objfile, so
7120              we don't need to duplicate it for the type.  */
7121           TYPE_TAG_NAME (type) = (char *) name;
7122           if (die->tag == DW_TAG_class_type)
7123             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7124         }
7125     }
7126
7127   if (die->tag == DW_TAG_structure_type)
7128     {
7129       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7130     }
7131   else if (die->tag == DW_TAG_union_type)
7132     {
7133       TYPE_CODE (type) = TYPE_CODE_UNION;
7134     }
7135   else
7136     {
7137       TYPE_CODE (type) = TYPE_CODE_CLASS;
7138     }
7139
7140   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7141     TYPE_DECLARED_CLASS (type) = 1;
7142
7143   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7144   if (attr)
7145     {
7146       TYPE_LENGTH (type) = DW_UNSND (attr);
7147     }
7148   else
7149     {
7150       TYPE_LENGTH (type) = 0;
7151     }
7152
7153   TYPE_STUB_SUPPORTED (type) = 1;
7154   if (die_is_declaration (die, cu))
7155     TYPE_STUB (type) = 1;
7156   else if (attr == NULL && die->child == NULL
7157            && producer_is_realview (cu->producer))
7158     /* RealView does not output the required DW_AT_declaration
7159        on incomplete types.  */
7160     TYPE_STUB (type) = 1;
7161
7162   /* We need to add the type field to the die immediately so we don't
7163      infinitely recurse when dealing with pointers to the structure
7164      type within the structure itself.  */
7165   set_die_type (die, type, cu);
7166
7167   /* set_die_type should be already done.  */
7168   set_descriptive_type (type, die, cu);
7169
7170   return type;
7171 }
7172
7173 /* Finish creating a structure or union type, including filling in
7174    its members and creating a symbol for it.  */
7175
7176 static void
7177 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7178 {
7179   struct objfile *objfile = cu->objfile;
7180   struct die_info *child_die = die->child;
7181   struct type *type;
7182
7183   type = get_die_type (die, cu);
7184   if (type == NULL)
7185     type = read_structure_type (die, cu);
7186
7187   if (die->child != NULL && ! die_is_declaration (die, cu))
7188     {
7189       struct field_info fi;
7190       struct die_info *child_die;
7191       VEC (symbolp) *template_args = NULL;
7192       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7193
7194       memset (&fi, 0, sizeof (struct field_info));
7195
7196       child_die = die->child;
7197
7198       while (child_die && child_die->tag)
7199         {
7200           if (child_die->tag == DW_TAG_member
7201               || child_die->tag == DW_TAG_variable)
7202             {
7203               /* NOTE: carlton/2002-11-05: A C++ static data member
7204                  should be a DW_TAG_member that is a declaration, but
7205                  all versions of G++ as of this writing (so through at
7206                  least 3.2.1) incorrectly generate DW_TAG_variable
7207                  tags for them instead.  */
7208               dwarf2_add_field (&fi, child_die, cu);
7209             }
7210           else if (child_die->tag == DW_TAG_subprogram)
7211             {
7212               /* C++ member function.  */
7213               dwarf2_add_member_fn (&fi, child_die, type, cu);
7214             }
7215           else if (child_die->tag == DW_TAG_inheritance)
7216             {
7217               /* C++ base class field.  */
7218               dwarf2_add_field (&fi, child_die, cu);
7219             }
7220           else if (child_die->tag == DW_TAG_typedef)
7221             dwarf2_add_typedef (&fi, child_die, cu);
7222           else if (child_die->tag == DW_TAG_template_type_param
7223                    || child_die->tag == DW_TAG_template_value_param)
7224             {
7225               struct symbol *arg = new_symbol (child_die, NULL, cu);
7226
7227               if (arg != NULL)
7228                 VEC_safe_push (symbolp, template_args, arg);
7229             }
7230
7231           child_die = sibling_die (child_die);
7232         }
7233
7234       /* Attach template arguments to type.  */
7235       if (! VEC_empty (symbolp, template_args))
7236         {
7237           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7238           TYPE_N_TEMPLATE_ARGUMENTS (type)
7239             = VEC_length (symbolp, template_args);
7240           TYPE_TEMPLATE_ARGUMENTS (type)
7241             = obstack_alloc (&objfile->objfile_obstack,
7242                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7243                               * sizeof (struct symbol *)));
7244           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7245                   VEC_address (symbolp, template_args),
7246                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7247                    * sizeof (struct symbol *)));
7248           VEC_free (symbolp, template_args);
7249         }
7250
7251       /* Attach fields and member functions to the type.  */
7252       if (fi.nfields)
7253         dwarf2_attach_fields_to_type (&fi, type, cu);
7254       if (fi.nfnfields)
7255         {
7256           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7257
7258           /* Get the type which refers to the base class (possibly this
7259              class itself) which contains the vtable pointer for the current
7260              class from the DW_AT_containing_type attribute.  This use of
7261              DW_AT_containing_type is a GNU extension.  */
7262
7263           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7264             {
7265               struct type *t = die_containing_type (die, cu);
7266
7267               TYPE_VPTR_BASETYPE (type) = t;
7268               if (type == t)
7269                 {
7270                   int i;
7271
7272                   /* Our own class provides vtbl ptr.  */
7273                   for (i = TYPE_NFIELDS (t) - 1;
7274                        i >= TYPE_N_BASECLASSES (t);
7275                        --i)
7276                     {
7277                       char *fieldname = TYPE_FIELD_NAME (t, i);
7278
7279                       if (is_vtable_name (fieldname, cu))
7280                         {
7281                           TYPE_VPTR_FIELDNO (type) = i;
7282                           break;
7283                         }
7284                     }
7285
7286                   /* Complain if virtual function table field not found.  */
7287                   if (i < TYPE_N_BASECLASSES (t))
7288                     complaint (&symfile_complaints,
7289                                _("virtual function table pointer "
7290                                  "not found when defining class '%s'"),
7291                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7292                                "");
7293                 }
7294               else
7295                 {
7296                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7297                 }
7298             }
7299           else if (cu->producer
7300                    && strncmp (cu->producer,
7301                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7302             {
7303               /* The IBM XLC compiler does not provide direct indication
7304                  of the containing type, but the vtable pointer is
7305                  always named __vfp.  */
7306
7307               int i;
7308
7309               for (i = TYPE_NFIELDS (type) - 1;
7310                    i >= TYPE_N_BASECLASSES (type);
7311                    --i)
7312                 {
7313                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7314                     {
7315                       TYPE_VPTR_FIELDNO (type) = i;
7316                       TYPE_VPTR_BASETYPE (type) = type;
7317                       break;
7318                     }
7319                 }
7320             }
7321         }
7322
7323       /* Copy fi.typedef_field_list linked list elements content into the
7324          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7325       if (fi.typedef_field_list)
7326         {
7327           int i = fi.typedef_field_list_count;
7328
7329           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7330           TYPE_TYPEDEF_FIELD_ARRAY (type)
7331             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7332           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7333
7334           /* Reverse the list order to keep the debug info elements order.  */
7335           while (--i >= 0)
7336             {
7337               struct typedef_field *dest, *src;
7338
7339               dest = &TYPE_TYPEDEF_FIELD (type, i);
7340               src = &fi.typedef_field_list->field;
7341               fi.typedef_field_list = fi.typedef_field_list->next;
7342               *dest = *src;
7343             }
7344         }
7345
7346       do_cleanups (back_to);
7347     }
7348
7349   quirk_gcc_member_function_pointer (type, cu->objfile);
7350
7351   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7352      snapshots) has been known to create a die giving a declaration
7353      for a class that has, as a child, a die giving a definition for a
7354      nested class.  So we have to process our children even if the
7355      current die is a declaration.  Normally, of course, a declaration
7356      won't have any children at all.  */
7357
7358   while (child_die != NULL && child_die->tag)
7359     {
7360       if (child_die->tag == DW_TAG_member
7361           || child_die->tag == DW_TAG_variable
7362           || child_die->tag == DW_TAG_inheritance
7363           || child_die->tag == DW_TAG_template_value_param
7364           || child_die->tag == DW_TAG_template_type_param)
7365         {
7366           /* Do nothing.  */
7367         }
7368       else
7369         process_die (child_die, cu);
7370
7371       child_die = sibling_die (child_die);
7372     }
7373
7374   /* Do not consider external references.  According to the DWARF standard,
7375      these DIEs are identified by the fact that they have no byte_size
7376      attribute, and a declaration attribute.  */
7377   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7378       || !die_is_declaration (die, cu))
7379     new_symbol (die, type, cu);
7380 }
7381
7382 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7383    complete the type's fields yet, or create any symbols.  */
7384
7385 static struct type *
7386 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7387 {
7388   struct objfile *objfile = cu->objfile;
7389   struct type *type;
7390   struct attribute *attr;
7391   const char *name;
7392
7393   /* If the definition of this type lives in .debug_types, read that type.
7394      Don't follow DW_AT_specification though, that will take us back up
7395      the chain and we want to go down.  */
7396   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7397   if (attr)
7398     {
7399       struct dwarf2_cu *type_cu = cu;
7400       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7401
7402       type = read_type_die (type_die, type_cu);
7403
7404       /* TYPE_CU may not be the same as CU.
7405          Ensure TYPE is recorded in CU's type_hash table.  */
7406       return set_die_type (die, type, cu);
7407     }
7408
7409   type = alloc_type (objfile);
7410
7411   TYPE_CODE (type) = TYPE_CODE_ENUM;
7412   name = dwarf2_full_name (NULL, die, cu);
7413   if (name != NULL)
7414     TYPE_TAG_NAME (type) = (char *) name;
7415
7416   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7417   if (attr)
7418     {
7419       TYPE_LENGTH (type) = DW_UNSND (attr);
7420     }
7421   else
7422     {
7423       TYPE_LENGTH (type) = 0;
7424     }
7425
7426   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7427      declared as private in the package spec, and then defined only
7428      inside the package body.  Such types are known as Taft Amendment
7429      Types.  When another package uses such a type, an incomplete DIE
7430      may be generated by the compiler.  */
7431   if (die_is_declaration (die, cu))
7432     TYPE_STUB (type) = 1;
7433
7434   return set_die_type (die, type, cu);
7435 }
7436
7437 /* Given a pointer to a die which begins an enumeration, process all
7438    the dies that define the members of the enumeration, and create the
7439    symbol for the enumeration type.
7440
7441    NOTE: We reverse the order of the element list.  */
7442
7443 static void
7444 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7445 {
7446   struct type *this_type;
7447
7448   this_type = get_die_type (die, cu);
7449   if (this_type == NULL)
7450     this_type = read_enumeration_type (die, cu);
7451
7452   if (die->child != NULL)
7453     {
7454       struct die_info *child_die;
7455       struct symbol *sym;
7456       struct field *fields = NULL;
7457       int num_fields = 0;
7458       int unsigned_enum = 1;
7459       char *name;
7460
7461       child_die = die->child;
7462       while (child_die && child_die->tag)
7463         {
7464           if (child_die->tag != DW_TAG_enumerator)
7465             {
7466               process_die (child_die, cu);
7467             }
7468           else
7469             {
7470               name = dwarf2_name (child_die, cu);
7471               if (name)
7472                 {
7473                   sym = new_symbol (child_die, this_type, cu);
7474                   if (SYMBOL_VALUE (sym) < 0)
7475                     unsigned_enum = 0;
7476
7477                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7478                     {
7479                       fields = (struct field *)
7480                         xrealloc (fields,
7481                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7482                                   * sizeof (struct field));
7483                     }
7484
7485                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7486                   FIELD_TYPE (fields[num_fields]) = NULL;
7487                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7488                   FIELD_BITSIZE (fields[num_fields]) = 0;
7489
7490                   num_fields++;
7491                 }
7492             }
7493
7494           child_die = sibling_die (child_die);
7495         }
7496
7497       if (num_fields)
7498         {
7499           TYPE_NFIELDS (this_type) = num_fields;
7500           TYPE_FIELDS (this_type) = (struct field *)
7501             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7502           memcpy (TYPE_FIELDS (this_type), fields,
7503                   sizeof (struct field) * num_fields);
7504           xfree (fields);
7505         }
7506       if (unsigned_enum)
7507         TYPE_UNSIGNED (this_type) = 1;
7508     }
7509
7510   new_symbol (die, this_type, cu);
7511 }
7512
7513 /* Extract all information from a DW_TAG_array_type DIE and put it in
7514    the DIE's type field.  For now, this only handles one dimensional
7515    arrays.  */
7516
7517 static struct type *
7518 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7519 {
7520   struct objfile *objfile = cu->objfile;
7521   struct die_info *child_die;
7522   struct type *type;
7523   struct type *element_type, *range_type, *index_type;
7524   struct type **range_types = NULL;
7525   struct attribute *attr;
7526   int ndim = 0;
7527   struct cleanup *back_to;
7528   char *name;
7529
7530   element_type = die_type (die, cu);
7531
7532   /* The die_type call above may have already set the type for this DIE.  */
7533   type = get_die_type (die, cu);
7534   if (type)
7535     return type;
7536
7537   /* Irix 6.2 native cc creates array types without children for
7538      arrays with unspecified length.  */
7539   if (die->child == NULL)
7540     {
7541       index_type = objfile_type (objfile)->builtin_int;
7542       range_type = create_range_type (NULL, index_type, 0, -1);
7543       type = create_array_type (NULL, element_type, range_type);
7544       return set_die_type (die, type, cu);
7545     }
7546
7547   back_to = make_cleanup (null_cleanup, NULL);
7548   child_die = die->child;
7549   while (child_die && child_die->tag)
7550     {
7551       if (child_die->tag == DW_TAG_subrange_type)
7552         {
7553           struct type *child_type = read_type_die (child_die, cu);
7554
7555           if (child_type != NULL)
7556             {
7557               /* The range type was succesfully read.  Save it for the
7558                  array type creation.  */
7559               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7560                 {
7561                   range_types = (struct type **)
7562                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7563                               * sizeof (struct type *));
7564                   if (ndim == 0)
7565                     make_cleanup (free_current_contents, &range_types);
7566                 }
7567               range_types[ndim++] = child_type;
7568             }
7569         }
7570       child_die = sibling_die (child_die);
7571     }
7572
7573   /* Dwarf2 dimensions are output from left to right, create the
7574      necessary array types in backwards order.  */
7575
7576   type = element_type;
7577
7578   if (read_array_order (die, cu) == DW_ORD_col_major)
7579     {
7580       int i = 0;
7581
7582       while (i < ndim)
7583         type = create_array_type (NULL, type, range_types[i++]);
7584     }
7585   else
7586     {
7587       while (ndim-- > 0)
7588         type = create_array_type (NULL, type, range_types[ndim]);
7589     }
7590
7591   /* Understand Dwarf2 support for vector types (like they occur on
7592      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7593      array type.  This is not part of the Dwarf2/3 standard yet, but a
7594      custom vendor extension.  The main difference between a regular
7595      array and the vector variant is that vectors are passed by value
7596      to functions.  */
7597   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7598   if (attr)
7599     make_vector_type (type);
7600
7601   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7602      implementation may choose to implement triple vectors using this
7603      attribute.  */
7604   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7605   if (attr)
7606     {
7607       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7608         TYPE_LENGTH (type) = DW_UNSND (attr);
7609       else
7610         complaint (&symfile_complaints,
7611                    _("DW_AT_byte_size for array type smaller "
7612                      "than the total size of elements"));
7613     }
7614
7615   name = dwarf2_name (die, cu);
7616   if (name)
7617     TYPE_NAME (type) = name;
7618
7619   /* Install the type in the die.  */
7620   set_die_type (die, type, cu);
7621
7622   /* set_die_type should be already done.  */
7623   set_descriptive_type (type, die, cu);
7624
7625   do_cleanups (back_to);
7626
7627   return type;
7628 }
7629
7630 static enum dwarf_array_dim_ordering
7631 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7632 {
7633   struct attribute *attr;
7634
7635   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7636
7637   if (attr) return DW_SND (attr);
7638
7639   /* GNU F77 is a special case, as at 08/2004 array type info is the
7640      opposite order to the dwarf2 specification, but data is still
7641      laid out as per normal fortran.
7642
7643      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7644      version checking.  */
7645
7646   if (cu->language == language_fortran
7647       && cu->producer && strstr (cu->producer, "GNU F77"))
7648     {
7649       return DW_ORD_row_major;
7650     }
7651
7652   switch (cu->language_defn->la_array_ordering)
7653     {
7654     case array_column_major:
7655       return DW_ORD_col_major;
7656     case array_row_major:
7657     default:
7658       return DW_ORD_row_major;
7659     };
7660 }
7661
7662 /* Extract all information from a DW_TAG_set_type DIE and put it in
7663    the DIE's type field.  */
7664
7665 static struct type *
7666 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7667 {
7668   struct type *domain_type, *set_type;
7669   struct attribute *attr;
7670
7671   domain_type = die_type (die, cu);
7672
7673   /* The die_type call above may have already set the type for this DIE.  */
7674   set_type = get_die_type (die, cu);
7675   if (set_type)
7676     return set_type;
7677
7678   set_type = create_set_type (NULL, domain_type);
7679
7680   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7681   if (attr)
7682     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7683
7684   return set_die_type (die, set_type, cu);
7685 }
7686
7687 /* First cut: install each common block member as a global variable.  */
7688
7689 static void
7690 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7691 {
7692   struct die_info *child_die;
7693   struct attribute *attr;
7694   struct symbol *sym;
7695   CORE_ADDR base = (CORE_ADDR) 0;
7696
7697   attr = dwarf2_attr (die, DW_AT_location, cu);
7698   if (attr)
7699     {
7700       /* Support the .debug_loc offsets.  */
7701       if (attr_form_is_block (attr))
7702         {
7703           base = decode_locdesc (DW_BLOCK (attr), cu);
7704         }
7705       else if (attr_form_is_section_offset (attr))
7706         {
7707           dwarf2_complex_location_expr_complaint ();
7708         }
7709       else
7710         {
7711           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7712                                                  "common block member");
7713         }
7714     }
7715   if (die->child != NULL)
7716     {
7717       child_die = die->child;
7718       while (child_die && child_die->tag)
7719         {
7720           LONGEST offset;
7721
7722           sym = new_symbol (child_die, NULL, cu);
7723           if (sym != NULL
7724               && handle_data_member_location (child_die, cu, &offset))
7725             {
7726               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7727               add_symbol_to_list (sym, &global_symbols);
7728             }
7729           child_die = sibling_die (child_die);
7730         }
7731     }
7732 }
7733
7734 /* Create a type for a C++ namespace.  */
7735
7736 static struct type *
7737 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7738 {
7739   struct objfile *objfile = cu->objfile;
7740   const char *previous_prefix, *name;
7741   int is_anonymous;
7742   struct type *type;
7743
7744   /* For extensions, reuse the type of the original namespace.  */
7745   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7746     {
7747       struct die_info *ext_die;
7748       struct dwarf2_cu *ext_cu = cu;
7749
7750       ext_die = dwarf2_extension (die, &ext_cu);
7751       type = read_type_die (ext_die, ext_cu);
7752
7753       /* EXT_CU may not be the same as CU.
7754          Ensure TYPE is recorded in CU's type_hash table.  */
7755       return set_die_type (die, type, cu);
7756     }
7757
7758   name = namespace_name (die, &is_anonymous, cu);
7759
7760   /* Now build the name of the current namespace.  */
7761
7762   previous_prefix = determine_prefix (die, cu);
7763   if (previous_prefix[0] != '\0')
7764     name = typename_concat (&objfile->objfile_obstack,
7765                             previous_prefix, name, 0, cu);
7766
7767   /* Create the type.  */
7768   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7769                     objfile);
7770   TYPE_NAME (type) = (char *) name;
7771   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7772
7773   return set_die_type (die, type, cu);
7774 }
7775
7776 /* Read a C++ namespace.  */
7777
7778 static void
7779 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7780 {
7781   struct objfile *objfile = cu->objfile;
7782   int is_anonymous;
7783
7784   /* Add a symbol associated to this if we haven't seen the namespace
7785      before.  Also, add a using directive if it's an anonymous
7786      namespace.  */
7787
7788   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7789     {
7790       struct type *type;
7791
7792       type = read_type_die (die, cu);
7793       new_symbol (die, type, cu);
7794
7795       namespace_name (die, &is_anonymous, cu);
7796       if (is_anonymous)
7797         {
7798           const char *previous_prefix = determine_prefix (die, cu);
7799
7800           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7801                                   NULL, &objfile->objfile_obstack);
7802         }
7803     }
7804
7805   if (die->child != NULL)
7806     {
7807       struct die_info *child_die = die->child;
7808
7809       while (child_die && child_die->tag)
7810         {
7811           process_die (child_die, cu);
7812           child_die = sibling_die (child_die);
7813         }
7814     }
7815 }
7816
7817 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7818    imported module.  Still we need that type as local Fortran "use ... only"
7819    declaration imports depend on the created type in determine_prefix.  */
7820
7821 static struct type *
7822 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7823 {
7824   struct objfile *objfile = cu->objfile;
7825   char *module_name;
7826   struct type *type;
7827
7828   module_name = dwarf2_name (die, cu);
7829   if (!module_name)
7830     complaint (&symfile_complaints,
7831                _("DW_TAG_module has no name, offset 0x%x"),
7832                die->offset);
7833   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7834
7835   /* determine_prefix uses TYPE_TAG_NAME.  */
7836   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7837
7838   return set_die_type (die, type, cu);
7839 }
7840
7841 /* Read a Fortran module.  */
7842
7843 static void
7844 read_module (struct die_info *die, struct dwarf2_cu *cu)
7845 {
7846   struct die_info *child_die = die->child;
7847
7848   while (child_die && child_die->tag)
7849     {
7850       process_die (child_die, cu);
7851       child_die = sibling_die (child_die);
7852     }
7853 }
7854
7855 /* Return the name of the namespace represented by DIE.  Set
7856    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7857    namespace.  */
7858
7859 static const char *
7860 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7861 {
7862   struct die_info *current_die;
7863   const char *name = NULL;
7864
7865   /* Loop through the extensions until we find a name.  */
7866
7867   for (current_die = die;
7868        current_die != NULL;
7869        current_die = dwarf2_extension (die, &cu))
7870     {
7871       name = dwarf2_name (current_die, cu);
7872       if (name != NULL)
7873         break;
7874     }
7875
7876   /* Is it an anonymous namespace?  */
7877
7878   *is_anonymous = (name == NULL);
7879   if (*is_anonymous)
7880     name = "(anonymous namespace)";
7881
7882   return name;
7883 }
7884
7885 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7886    the user defined type vector.  */
7887
7888 static struct type *
7889 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7890 {
7891   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7892   struct comp_unit_head *cu_header = &cu->header;
7893   struct type *type;
7894   struct attribute *attr_byte_size;
7895   struct attribute *attr_address_class;
7896   int byte_size, addr_class;
7897   struct type *target_type;
7898
7899   target_type = die_type (die, cu);
7900
7901   /* The die_type call above may have already set the type for this DIE.  */
7902   type = get_die_type (die, cu);
7903   if (type)
7904     return type;
7905
7906   type = lookup_pointer_type (target_type);
7907
7908   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7909   if (attr_byte_size)
7910     byte_size = DW_UNSND (attr_byte_size);
7911   else
7912     byte_size = cu_header->addr_size;
7913
7914   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7915   if (attr_address_class)
7916     addr_class = DW_UNSND (attr_address_class);
7917   else
7918     addr_class = DW_ADDR_none;
7919
7920   /* If the pointer size or address class is different than the
7921      default, create a type variant marked as such and set the
7922      length accordingly.  */
7923   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7924     {
7925       if (gdbarch_address_class_type_flags_p (gdbarch))
7926         {
7927           int type_flags;
7928
7929           type_flags = gdbarch_address_class_type_flags
7930                          (gdbarch, byte_size, addr_class);
7931           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7932                       == 0);
7933           type = make_type_with_address_space (type, type_flags);
7934         }
7935       else if (TYPE_LENGTH (type) != byte_size)
7936         {
7937           complaint (&symfile_complaints,
7938                      _("invalid pointer size %d"), byte_size);
7939         }
7940       else
7941         {
7942           /* Should we also complain about unhandled address classes?  */
7943         }
7944     }
7945
7946   TYPE_LENGTH (type) = byte_size;
7947   return set_die_type (die, type, cu);
7948 }
7949
7950 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7951    the user defined type vector.  */
7952
7953 static struct type *
7954 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7955 {
7956   struct type *type;
7957   struct type *to_type;
7958   struct type *domain;
7959
7960   to_type = die_type (die, cu);
7961   domain = die_containing_type (die, cu);
7962
7963   /* The calls above may have already set the type for this DIE.  */
7964   type = get_die_type (die, cu);
7965   if (type)
7966     return type;
7967
7968   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7969     type = lookup_methodptr_type (to_type);
7970   else
7971     type = lookup_memberptr_type (to_type, domain);
7972
7973   return set_die_type (die, type, cu);
7974 }
7975
7976 /* Extract all information from a DW_TAG_reference_type DIE and add to
7977    the user defined type vector.  */
7978
7979 static struct type *
7980 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7981 {
7982   struct comp_unit_head *cu_header = &cu->header;
7983   struct type *type, *target_type;
7984   struct attribute *attr;
7985
7986   target_type = die_type (die, cu);
7987
7988   /* The die_type call above may have already set the type for this DIE.  */
7989   type = get_die_type (die, cu);
7990   if (type)
7991     return type;
7992
7993   type = lookup_reference_type (target_type);
7994   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7995   if (attr)
7996     {
7997       TYPE_LENGTH (type) = DW_UNSND (attr);
7998     }
7999   else
8000     {
8001       TYPE_LENGTH (type) = cu_header->addr_size;
8002     }
8003   return set_die_type (die, type, cu);
8004 }
8005
8006 static struct type *
8007 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8008 {
8009   struct type *base_type, *cv_type;
8010
8011   base_type = die_type (die, cu);
8012
8013   /* The die_type call above may have already set the type for this DIE.  */
8014   cv_type = get_die_type (die, cu);
8015   if (cv_type)
8016     return cv_type;
8017
8018   /* In case the const qualifier is applied to an array type, the element type
8019      is so qualified, not the array type (section 6.7.3 of C99).  */
8020   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8021     {
8022       struct type *el_type, *inner_array;
8023
8024       base_type = copy_type (base_type);
8025       inner_array = base_type;
8026
8027       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8028         {
8029           TYPE_TARGET_TYPE (inner_array) =
8030             copy_type (TYPE_TARGET_TYPE (inner_array));
8031           inner_array = TYPE_TARGET_TYPE (inner_array);
8032         }
8033
8034       el_type = TYPE_TARGET_TYPE (inner_array);
8035       TYPE_TARGET_TYPE (inner_array) =
8036         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8037
8038       return set_die_type (die, base_type, cu);
8039     }
8040
8041   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8042   return set_die_type (die, cv_type, cu);
8043 }
8044
8045 static struct type *
8046 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8047 {
8048   struct type *base_type, *cv_type;
8049
8050   base_type = die_type (die, cu);
8051
8052   /* The die_type call above may have already set the type for this DIE.  */
8053   cv_type = get_die_type (die, cu);
8054   if (cv_type)
8055     return cv_type;
8056
8057   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8058   return set_die_type (die, cv_type, cu);
8059 }
8060
8061 /* Extract all information from a DW_TAG_string_type DIE and add to
8062    the user defined type vector.  It isn't really a user defined type,
8063    but it behaves like one, with other DIE's using an AT_user_def_type
8064    attribute to reference it.  */
8065
8066 static struct type *
8067 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8068 {
8069   struct objfile *objfile = cu->objfile;
8070   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8071   struct type *type, *range_type, *index_type, *char_type;
8072   struct attribute *attr;
8073   unsigned int length;
8074
8075   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8076   if (attr)
8077     {
8078       length = DW_UNSND (attr);
8079     }
8080   else
8081     {
8082       /* Check for the DW_AT_byte_size attribute.  */
8083       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8084       if (attr)
8085         {
8086           length = DW_UNSND (attr);
8087         }
8088       else
8089         {
8090           length = 1;
8091         }
8092     }
8093
8094   index_type = objfile_type (objfile)->builtin_int;
8095   range_type = create_range_type (NULL, index_type, 1, length);
8096   char_type = language_string_char_type (cu->language_defn, gdbarch);
8097   type = create_string_type (NULL, char_type, range_type);
8098
8099   return set_die_type (die, type, cu);
8100 }
8101
8102 /* Handle DIES due to C code like:
8103
8104    struct foo
8105    {
8106    int (*funcp)(int a, long l);
8107    int b;
8108    };
8109
8110    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8111
8112 static struct type *
8113 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8114 {
8115   struct type *type;            /* Type that this function returns.  */
8116   struct type *ftype;           /* Function that returns above type.  */
8117   struct attribute *attr;
8118
8119   type = die_type (die, cu);
8120
8121   /* The die_type call above may have already set the type for this DIE.  */
8122   ftype = get_die_type (die, cu);
8123   if (ftype)
8124     return ftype;
8125
8126   ftype = lookup_function_type (type);
8127
8128   /* All functions in C++, Pascal and Java have prototypes.  */
8129   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8130   if ((attr && (DW_UNSND (attr) != 0))
8131       || cu->language == language_cplus
8132       || cu->language == language_java
8133       || cu->language == language_pascal)
8134     TYPE_PROTOTYPED (ftype) = 1;
8135   else if (producer_is_realview (cu->producer))
8136     /* RealView does not emit DW_AT_prototyped.  We can not
8137        distinguish prototyped and unprototyped functions; default to
8138        prototyped, since that is more common in modern code (and
8139        RealView warns about unprototyped functions).  */
8140     TYPE_PROTOTYPED (ftype) = 1;
8141
8142   /* Store the calling convention in the type if it's available in
8143      the subroutine die.  Otherwise set the calling convention to
8144      the default value DW_CC_normal.  */
8145   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8146   if (attr)
8147     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8148   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8149     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8150   else
8151     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8152
8153   /* We need to add the subroutine type to the die immediately so
8154      we don't infinitely recurse when dealing with parameters
8155      declared as the same subroutine type.  */
8156   set_die_type (die, ftype, cu);
8157
8158   if (die->child != NULL)
8159     {
8160       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8161       struct die_info *child_die;
8162       int nparams, iparams;
8163
8164       /* Count the number of parameters.
8165          FIXME: GDB currently ignores vararg functions, but knows about
8166          vararg member functions.  */
8167       nparams = 0;
8168       child_die = die->child;
8169       while (child_die && child_die->tag)
8170         {
8171           if (child_die->tag == DW_TAG_formal_parameter)
8172             nparams++;
8173           else if (child_die->tag == DW_TAG_unspecified_parameters)
8174             TYPE_VARARGS (ftype) = 1;
8175           child_die = sibling_die (child_die);
8176         }
8177
8178       /* Allocate storage for parameters and fill them in.  */
8179       TYPE_NFIELDS (ftype) = nparams;
8180       TYPE_FIELDS (ftype) = (struct field *)
8181         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8182
8183       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8184          even if we error out during the parameters reading below.  */
8185       for (iparams = 0; iparams < nparams; iparams++)
8186         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8187
8188       iparams = 0;
8189       child_die = die->child;
8190       while (child_die && child_die->tag)
8191         {
8192           if (child_die->tag == DW_TAG_formal_parameter)
8193             {
8194               struct type *arg_type;
8195
8196               /* DWARF version 2 has no clean way to discern C++
8197                  static and non-static member functions.  G++ helps
8198                  GDB by marking the first parameter for non-static
8199                  member functions (which is the this pointer) as
8200                  artificial.  We pass this information to
8201                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8202
8203                  DWARF version 3 added DW_AT_object_pointer, which GCC
8204                  4.5 does not yet generate.  */
8205               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8206               if (attr)
8207                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8208               else
8209                 {
8210                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8211
8212                   /* GCC/43521: In java, the formal parameter
8213                      "this" is sometimes not marked with DW_AT_artificial.  */
8214                   if (cu->language == language_java)
8215                     {
8216                       const char *name = dwarf2_name (child_die, cu);
8217
8218                       if (name && !strcmp (name, "this"))
8219                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8220                     }
8221                 }
8222               arg_type = die_type (child_die, cu);
8223
8224               /* RealView does not mark THIS as const, which the testsuite
8225                  expects.  GCC marks THIS as const in method definitions,
8226                  but not in the class specifications (GCC PR 43053).  */
8227               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8228                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8229                 {
8230                   int is_this = 0;
8231                   struct dwarf2_cu *arg_cu = cu;
8232                   const char *name = dwarf2_name (child_die, cu);
8233
8234                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8235                   if (attr)
8236                     {
8237                       /* If the compiler emits this, use it.  */
8238                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8239                         is_this = 1;
8240                     }
8241                   else if (name && strcmp (name, "this") == 0)
8242                     /* Function definitions will have the argument names.  */
8243                     is_this = 1;
8244                   else if (name == NULL && iparams == 0)
8245                     /* Declarations may not have the names, so like
8246                        elsewhere in GDB, assume an artificial first
8247                        argument is "this".  */
8248                     is_this = 1;
8249
8250                   if (is_this)
8251                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8252                                              arg_type, 0);
8253                 }
8254
8255               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8256               iparams++;
8257             }
8258           child_die = sibling_die (child_die);
8259         }
8260     }
8261
8262   return ftype;
8263 }
8264
8265 static struct type *
8266 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8267 {
8268   struct objfile *objfile = cu->objfile;
8269   const char *name = NULL;
8270   struct type *this_type;
8271
8272   name = dwarf2_full_name (NULL, die, cu);
8273   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8274                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8275   TYPE_NAME (this_type) = (char *) name;
8276   set_die_type (die, this_type, cu);
8277   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8278   return this_type;
8279 }
8280
8281 /* Find a representation of a given base type and install
8282    it in the TYPE field of the die.  */
8283
8284 static struct type *
8285 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8286 {
8287   struct objfile *objfile = cu->objfile;
8288   struct type *type;
8289   struct attribute *attr;
8290   int encoding = 0, size = 0;
8291   char *name;
8292   enum type_code code = TYPE_CODE_INT;
8293   int type_flags = 0;
8294   struct type *target_type = NULL;
8295
8296   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8297   if (attr)
8298     {
8299       encoding = DW_UNSND (attr);
8300     }
8301   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8302   if (attr)
8303     {
8304       size = DW_UNSND (attr);
8305     }
8306   name = dwarf2_name (die, cu);
8307   if (!name)
8308     {
8309       complaint (&symfile_complaints,
8310                  _("DW_AT_name missing from DW_TAG_base_type"));
8311     }
8312
8313   switch (encoding)
8314     {
8315       case DW_ATE_address:
8316         /* Turn DW_ATE_address into a void * pointer.  */
8317         code = TYPE_CODE_PTR;
8318         type_flags |= TYPE_FLAG_UNSIGNED;
8319         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8320         break;
8321       case DW_ATE_boolean:
8322         code = TYPE_CODE_BOOL;
8323         type_flags |= TYPE_FLAG_UNSIGNED;
8324         break;
8325       case DW_ATE_complex_float:
8326         code = TYPE_CODE_COMPLEX;
8327         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8328         break;
8329       case DW_ATE_decimal_float:
8330         code = TYPE_CODE_DECFLOAT;
8331         break;
8332       case DW_ATE_float:
8333         code = TYPE_CODE_FLT;
8334         break;
8335       case DW_ATE_signed:
8336         break;
8337       case DW_ATE_unsigned:
8338         type_flags |= TYPE_FLAG_UNSIGNED;
8339         break;
8340       case DW_ATE_signed_char:
8341         if (cu->language == language_ada || cu->language == language_m2
8342             || cu->language == language_pascal)
8343           code = TYPE_CODE_CHAR;
8344         break;
8345       case DW_ATE_unsigned_char:
8346         if (cu->language == language_ada || cu->language == language_m2
8347             || cu->language == language_pascal)
8348           code = TYPE_CODE_CHAR;
8349         type_flags |= TYPE_FLAG_UNSIGNED;
8350         break;
8351       case DW_ATE_UTF:
8352         /* We just treat this as an integer and then recognize the
8353            type by name elsewhere.  */
8354         break;
8355
8356       default:
8357         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8358                    dwarf_type_encoding_name (encoding));
8359         break;
8360     }
8361
8362   type = init_type (code, size, type_flags, NULL, objfile);
8363   TYPE_NAME (type) = name;
8364   TYPE_TARGET_TYPE (type) = target_type;
8365
8366   if (name && strcmp (name, "char") == 0)
8367     TYPE_NOSIGN (type) = 1;
8368
8369   return set_die_type (die, type, cu);
8370 }
8371
8372 /* Read the given DW_AT_subrange DIE.  */
8373
8374 static struct type *
8375 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8376 {
8377   struct type *base_type;
8378   struct type *range_type;
8379   struct attribute *attr;
8380   LONGEST low = 0;
8381   LONGEST high = -1;
8382   char *name;
8383   LONGEST negative_mask;
8384
8385   base_type = die_type (die, cu);
8386   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8387   check_typedef (base_type);
8388
8389   /* The die_type call above may have already set the type for this DIE.  */
8390   range_type = get_die_type (die, cu);
8391   if (range_type)
8392     return range_type;
8393
8394   if (cu->language == language_fortran)
8395     {
8396       /* FORTRAN implies a lower bound of 1, if not given.  */
8397       low = 1;
8398     }
8399
8400   /* FIXME: For variable sized arrays either of these could be
8401      a variable rather than a constant value.  We'll allow it,
8402      but we don't know how to handle it.  */
8403   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8404   if (attr)
8405     low = dwarf2_get_attr_constant_value (attr, 0);
8406
8407   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8408   if (attr)
8409     {
8410       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8411         {
8412           /* GCC encodes arrays with unspecified or dynamic length
8413              with a DW_FORM_block1 attribute or a reference attribute.
8414              FIXME: GDB does not yet know how to handle dynamic
8415              arrays properly, treat them as arrays with unspecified
8416              length for now.
8417
8418              FIXME: jimb/2003-09-22: GDB does not really know
8419              how to handle arrays of unspecified length
8420              either; we just represent them as zero-length
8421              arrays.  Choose an appropriate upper bound given
8422              the lower bound we've computed above.  */
8423           high = low - 1;
8424         }
8425       else
8426         high = dwarf2_get_attr_constant_value (attr, 1);
8427     }
8428   else
8429     {
8430       attr = dwarf2_attr (die, DW_AT_count, cu);
8431       if (attr)
8432         {
8433           int count = dwarf2_get_attr_constant_value (attr, 1);
8434           high = low + count - 1;
8435         }
8436       else
8437         {
8438           /* Unspecified array length.  */
8439           high = low - 1;
8440         }
8441     }
8442
8443   /* Dwarf-2 specifications explicitly allows to create subrange types
8444      without specifying a base type.
8445      In that case, the base type must be set to the type of
8446      the lower bound, upper bound or count, in that order, if any of these
8447      three attributes references an object that has a type.
8448      If no base type is found, the Dwarf-2 specifications say that
8449      a signed integer type of size equal to the size of an address should
8450      be used.
8451      For the following C code: `extern char gdb_int [];'
8452      GCC produces an empty range DIE.
8453      FIXME: muller/2010-05-28: Possible references to object for low bound,
8454      high bound or count are not yet handled by this code.  */
8455   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8456     {
8457       struct objfile *objfile = cu->objfile;
8458       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8459       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8460       struct type *int_type = objfile_type (objfile)->builtin_int;
8461
8462       /* Test "int", "long int", and "long long int" objfile types,
8463          and select the first one having a size above or equal to the
8464          architecture address size.  */
8465       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8466         base_type = int_type;
8467       else
8468         {
8469           int_type = objfile_type (objfile)->builtin_long;
8470           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8471             base_type = int_type;
8472           else
8473             {
8474               int_type = objfile_type (objfile)->builtin_long_long;
8475               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8476                 base_type = int_type;
8477             }
8478         }
8479     }
8480
8481   negative_mask =
8482     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8483   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8484     low |= negative_mask;
8485   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8486     high |= negative_mask;
8487
8488   range_type = create_range_type (NULL, base_type, low, high);
8489
8490   /* Mark arrays with dynamic length at least as an array of unspecified
8491      length.  GDB could check the boundary but before it gets implemented at
8492      least allow accessing the array elements.  */
8493   if (attr && attr->form == DW_FORM_block1)
8494     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8495
8496   /* Ada expects an empty array on no boundary attributes.  */
8497   if (attr == NULL && cu->language != language_ada)
8498     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8499
8500   name = dwarf2_name (die, cu);
8501   if (name)
8502     TYPE_NAME (range_type) = name;
8503
8504   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8505   if (attr)
8506     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8507
8508   set_die_type (die, range_type, cu);
8509
8510   /* set_die_type should be already done.  */
8511   set_descriptive_type (range_type, die, cu);
8512
8513   return range_type;
8514 }
8515
8516 static struct type *
8517 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8518 {
8519   struct type *type;
8520
8521   /* For now, we only support the C meaning of an unspecified type: void.  */
8522
8523   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8524   TYPE_NAME (type) = dwarf2_name (die, cu);
8525
8526   return set_die_type (die, type, cu);
8527 }
8528
8529 /* Trivial hash function for die_info: the hash value of a DIE
8530    is its offset in .debug_info for this objfile.  */
8531
8532 static hashval_t
8533 die_hash (const void *item)
8534 {
8535   const struct die_info *die = item;
8536
8537   return die->offset;
8538 }
8539
8540 /* Trivial comparison function for die_info structures: two DIEs
8541    are equal if they have the same offset.  */
8542
8543 static int
8544 die_eq (const void *item_lhs, const void *item_rhs)
8545 {
8546   const struct die_info *die_lhs = item_lhs;
8547   const struct die_info *die_rhs = item_rhs;
8548
8549   return die_lhs->offset == die_rhs->offset;
8550 }
8551
8552 /* Read a whole compilation unit into a linked list of dies.  */
8553
8554 static struct die_info *
8555 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8556 {
8557   struct die_reader_specs reader_specs;
8558   int read_abbrevs = 0;
8559   struct cleanup *back_to = NULL;
8560   struct die_info *die;
8561
8562   if (cu->dwarf2_abbrevs == NULL)
8563     {
8564       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8565       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8566       read_abbrevs = 1;
8567     }
8568
8569   gdb_assert (cu->die_hash == NULL);
8570   cu->die_hash
8571     = htab_create_alloc_ex (cu->header.length / 12,
8572                             die_hash,
8573                             die_eq,
8574                             NULL,
8575                             &cu->comp_unit_obstack,
8576                             hashtab_obstack_allocate,
8577                             dummy_obstack_deallocate);
8578
8579   init_cu_die_reader (&reader_specs, cu);
8580
8581   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8582
8583   if (read_abbrevs)
8584     do_cleanups (back_to);
8585
8586   return die;
8587 }
8588
8589 /* Main entry point for reading a DIE and all children.
8590    Read the DIE and dump it if requested.  */
8591
8592 static struct die_info *
8593 read_die_and_children (const struct die_reader_specs *reader,
8594                        gdb_byte *info_ptr,
8595                        gdb_byte **new_info_ptr,
8596                        struct die_info *parent)
8597 {
8598   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8599                                                      new_info_ptr, parent);
8600
8601   if (dwarf2_die_debug)
8602     {
8603       fprintf_unfiltered (gdb_stdlog,
8604                           "\nRead die from %s of %s:\n",
8605                           reader->buffer == dwarf2_per_objfile->info.buffer
8606                           ? ".debug_info"
8607                           : reader->buffer == dwarf2_per_objfile->types.buffer
8608                           ? ".debug_types"
8609                           : "unknown section",
8610                           reader->abfd->filename);
8611       dump_die (result, dwarf2_die_debug);
8612     }
8613
8614   return result;
8615 }
8616
8617 /* Read a single die and all its descendents.  Set the die's sibling
8618    field to NULL; set other fields in the die correctly, and set all
8619    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8620    location of the info_ptr after reading all of those dies.  PARENT
8621    is the parent of the die in question.  */
8622
8623 static struct die_info *
8624 read_die_and_children_1 (const struct die_reader_specs *reader,
8625                          gdb_byte *info_ptr,
8626                          gdb_byte **new_info_ptr,
8627                          struct die_info *parent)
8628 {
8629   struct die_info *die;
8630   gdb_byte *cur_ptr;
8631   int has_children;
8632
8633   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8634   if (die == NULL)
8635     {
8636       *new_info_ptr = cur_ptr;
8637       return NULL;
8638     }
8639   store_in_ref_table (die, reader->cu);
8640
8641   if (has_children)
8642     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8643   else
8644     {
8645       die->child = NULL;
8646       *new_info_ptr = cur_ptr;
8647     }
8648
8649   die->sibling = NULL;
8650   die->parent = parent;
8651   return die;
8652 }
8653
8654 /* Read a die, all of its descendents, and all of its siblings; set
8655    all of the fields of all of the dies correctly.  Arguments are as
8656    in read_die_and_children.  */
8657
8658 static struct die_info *
8659 read_die_and_siblings (const struct die_reader_specs *reader,
8660                        gdb_byte *info_ptr,
8661                        gdb_byte **new_info_ptr,
8662                        struct die_info *parent)
8663 {
8664   struct die_info *first_die, *last_sibling;
8665   gdb_byte *cur_ptr;
8666
8667   cur_ptr = info_ptr;
8668   first_die = last_sibling = NULL;
8669
8670   while (1)
8671     {
8672       struct die_info *die
8673         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8674
8675       if (die == NULL)
8676         {
8677           *new_info_ptr = cur_ptr;
8678           return first_die;
8679         }
8680
8681       if (!first_die)
8682         first_die = die;
8683       else
8684         last_sibling->sibling = die;
8685
8686       last_sibling = die;
8687     }
8688 }
8689
8690 /* Read the die from the .debug_info section buffer.  Set DIEP to
8691    point to a newly allocated die with its information, except for its
8692    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8693    whether the die has children or not.  */
8694
8695 static gdb_byte *
8696 read_full_die (const struct die_reader_specs *reader,
8697                struct die_info **diep, gdb_byte *info_ptr,
8698                int *has_children)
8699 {
8700   unsigned int abbrev_number, bytes_read, i, offset;
8701   struct abbrev_info *abbrev;
8702   struct die_info *die;
8703   struct dwarf2_cu *cu = reader->cu;
8704   bfd *abfd = reader->abfd;
8705
8706   offset = info_ptr - reader->buffer;
8707   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8708   info_ptr += bytes_read;
8709   if (!abbrev_number)
8710     {
8711       *diep = NULL;
8712       *has_children = 0;
8713       return info_ptr;
8714     }
8715
8716   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8717   if (!abbrev)
8718     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8719            abbrev_number,
8720            bfd_get_filename (abfd));
8721
8722   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8723   die->offset = offset;
8724   die->tag = abbrev->tag;
8725   die->abbrev = abbrev_number;
8726
8727   die->num_attrs = abbrev->num_attrs;
8728
8729   for (i = 0; i < abbrev->num_attrs; ++i)
8730     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8731                                abfd, info_ptr, cu);
8732
8733   *diep = die;
8734   *has_children = abbrev->has_children;
8735   return info_ptr;
8736 }
8737
8738 /* In DWARF version 2, the description of the debugging information is
8739    stored in a separate .debug_abbrev section.  Before we read any
8740    dies from a section we read in all abbreviations and install them
8741    in a hash table.  This function also sets flags in CU describing
8742    the data found in the abbrev table.  */
8743
8744 static void
8745 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8746 {
8747   struct comp_unit_head *cu_header = &cu->header;
8748   gdb_byte *abbrev_ptr;
8749   struct abbrev_info *cur_abbrev;
8750   unsigned int abbrev_number, bytes_read, abbrev_name;
8751   unsigned int abbrev_form, hash_number;
8752   struct attr_abbrev *cur_attrs;
8753   unsigned int allocated_attrs;
8754
8755   /* Initialize dwarf2 abbrevs.  */
8756   obstack_init (&cu->abbrev_obstack);
8757   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8758                                       (ABBREV_HASH_SIZE
8759                                        * sizeof (struct abbrev_info *)));
8760   memset (cu->dwarf2_abbrevs, 0,
8761           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8762
8763   dwarf2_read_section (dwarf2_per_objfile->objfile,
8764                        &dwarf2_per_objfile->abbrev);
8765   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8766   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8767   abbrev_ptr += bytes_read;
8768
8769   allocated_attrs = ATTR_ALLOC_CHUNK;
8770   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8771
8772   /* Loop until we reach an abbrev number of 0.  */
8773   while (abbrev_number)
8774     {
8775       cur_abbrev = dwarf_alloc_abbrev (cu);
8776
8777       /* read in abbrev header */
8778       cur_abbrev->number = abbrev_number;
8779       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8780       abbrev_ptr += bytes_read;
8781       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8782       abbrev_ptr += 1;
8783
8784       if (cur_abbrev->tag == DW_TAG_namespace)
8785         cu->has_namespace_info = 1;
8786
8787       /* now read in declarations */
8788       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8789       abbrev_ptr += bytes_read;
8790       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8791       abbrev_ptr += bytes_read;
8792       while (abbrev_name)
8793         {
8794           if (cur_abbrev->num_attrs == allocated_attrs)
8795             {
8796               allocated_attrs += ATTR_ALLOC_CHUNK;
8797               cur_attrs
8798                 = xrealloc (cur_attrs, (allocated_attrs
8799                                         * sizeof (struct attr_abbrev)));
8800             }
8801
8802           /* Record whether this compilation unit might have
8803              inter-compilation-unit references.  If we don't know what form
8804              this attribute will have, then it might potentially be a
8805              DW_FORM_ref_addr, so we conservatively expect inter-CU
8806              references.  */
8807
8808           if (abbrev_form == DW_FORM_ref_addr
8809               || abbrev_form == DW_FORM_indirect)
8810             cu->has_form_ref_addr = 1;
8811
8812           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8813           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8814           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8815           abbrev_ptr += bytes_read;
8816           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8817           abbrev_ptr += bytes_read;
8818         }
8819
8820       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8821                                          (cur_abbrev->num_attrs
8822                                           * sizeof (struct attr_abbrev)));
8823       memcpy (cur_abbrev->attrs, cur_attrs,
8824               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8825
8826       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8827       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8828       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8829
8830       /* Get next abbreviation.
8831          Under Irix6 the abbreviations for a compilation unit are not
8832          always properly terminated with an abbrev number of 0.
8833          Exit loop if we encounter an abbreviation which we have
8834          already read (which means we are about to read the abbreviations
8835          for the next compile unit) or if the end of the abbreviation
8836          table is reached.  */
8837       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8838           >= dwarf2_per_objfile->abbrev.size)
8839         break;
8840       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8841       abbrev_ptr += bytes_read;
8842       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8843         break;
8844     }
8845
8846   xfree (cur_attrs);
8847 }
8848
8849 /* Release the memory used by the abbrev table for a compilation unit.  */
8850
8851 static void
8852 dwarf2_free_abbrev_table (void *ptr_to_cu)
8853 {
8854   struct dwarf2_cu *cu = ptr_to_cu;
8855
8856   obstack_free (&cu->abbrev_obstack, NULL);
8857   cu->dwarf2_abbrevs = NULL;
8858 }
8859
8860 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8861
8862 static struct abbrev_info *
8863 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8864 {
8865   unsigned int hash_number;
8866   struct abbrev_info *abbrev;
8867
8868   hash_number = number % ABBREV_HASH_SIZE;
8869   abbrev = cu->dwarf2_abbrevs[hash_number];
8870
8871   while (abbrev)
8872     {
8873       if (abbrev->number == number)
8874         return abbrev;
8875       else
8876         abbrev = abbrev->next;
8877     }
8878   return NULL;
8879 }
8880
8881 /* Returns nonzero if TAG represents a type that we might generate a partial
8882    symbol for.  */
8883
8884 static int
8885 is_type_tag_for_partial (int tag)
8886 {
8887   switch (tag)
8888     {
8889 #if 0
8890     /* Some types that would be reasonable to generate partial symbols for,
8891        that we don't at present.  */
8892     case DW_TAG_array_type:
8893     case DW_TAG_file_type:
8894     case DW_TAG_ptr_to_member_type:
8895     case DW_TAG_set_type:
8896     case DW_TAG_string_type:
8897     case DW_TAG_subroutine_type:
8898 #endif
8899     case DW_TAG_base_type:
8900     case DW_TAG_class_type:
8901     case DW_TAG_interface_type:
8902     case DW_TAG_enumeration_type:
8903     case DW_TAG_structure_type:
8904     case DW_TAG_subrange_type:
8905     case DW_TAG_typedef:
8906     case DW_TAG_union_type:
8907       return 1;
8908     default:
8909       return 0;
8910     }
8911 }
8912
8913 /* Load all DIEs that are interesting for partial symbols into memory.  */
8914
8915 static struct partial_die_info *
8916 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8917                    int building_psymtab, struct dwarf2_cu *cu)
8918 {
8919   struct partial_die_info *part_die;
8920   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8921   struct abbrev_info *abbrev;
8922   unsigned int bytes_read;
8923   unsigned int load_all = 0;
8924
8925   int nesting_level = 1;
8926
8927   parent_die = NULL;
8928   last_die = NULL;
8929
8930   if (cu->per_cu && cu->per_cu->load_all_dies)
8931     load_all = 1;
8932
8933   cu->partial_dies
8934     = htab_create_alloc_ex (cu->header.length / 12,
8935                             partial_die_hash,
8936                             partial_die_eq,
8937                             NULL,
8938                             &cu->comp_unit_obstack,
8939                             hashtab_obstack_allocate,
8940                             dummy_obstack_deallocate);
8941
8942   part_die = obstack_alloc (&cu->comp_unit_obstack,
8943                             sizeof (struct partial_die_info));
8944
8945   while (1)
8946     {
8947       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8948
8949       /* A NULL abbrev means the end of a series of children.  */
8950       if (abbrev == NULL)
8951         {
8952           if (--nesting_level == 0)
8953             {
8954               /* PART_DIE was probably the last thing allocated on the
8955                  comp_unit_obstack, so we could call obstack_free
8956                  here.  We don't do that because the waste is small,
8957                  and will be cleaned up when we're done with this
8958                  compilation unit.  This way, we're also more robust
8959                  against other users of the comp_unit_obstack.  */
8960               return first_die;
8961             }
8962           info_ptr += bytes_read;
8963           last_die = parent_die;
8964           parent_die = parent_die->die_parent;
8965           continue;
8966         }
8967
8968       /* Check for template arguments.  We never save these; if
8969          they're seen, we just mark the parent, and go on our way.  */
8970       if (parent_die != NULL
8971           && cu->language == language_cplus
8972           && (abbrev->tag == DW_TAG_template_type_param
8973               || abbrev->tag == DW_TAG_template_value_param))
8974         {
8975           parent_die->has_template_arguments = 1;
8976
8977           if (!load_all)
8978             {
8979               /* We don't need a partial DIE for the template argument.  */
8980               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8981                                        cu);
8982               continue;
8983             }
8984         }
8985
8986       /* We only recurse into subprograms looking for template arguments.
8987          Skip their other children.  */
8988       if (!load_all
8989           && cu->language == language_cplus
8990           && parent_die != NULL
8991           && parent_die->tag == DW_TAG_subprogram)
8992         {
8993           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8994           continue;
8995         }
8996
8997       /* Check whether this DIE is interesting enough to save.  Normally
8998          we would not be interested in members here, but there may be
8999          later variables referencing them via DW_AT_specification (for
9000          static members).  */
9001       if (!load_all
9002           && !is_type_tag_for_partial (abbrev->tag)
9003           && abbrev->tag != DW_TAG_constant
9004           && abbrev->tag != DW_TAG_enumerator
9005           && abbrev->tag != DW_TAG_subprogram
9006           && abbrev->tag != DW_TAG_lexical_block
9007           && abbrev->tag != DW_TAG_variable
9008           && abbrev->tag != DW_TAG_namespace
9009           && abbrev->tag != DW_TAG_module
9010           && abbrev->tag != DW_TAG_member)
9011         {
9012           /* Otherwise we skip to the next sibling, if any.  */
9013           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9014           continue;
9015         }
9016
9017       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9018                                    buffer, info_ptr, cu);
9019
9020       /* This two-pass algorithm for processing partial symbols has a
9021          high cost in cache pressure.  Thus, handle some simple cases
9022          here which cover the majority of C partial symbols.  DIEs
9023          which neither have specification tags in them, nor could have
9024          specification tags elsewhere pointing at them, can simply be
9025          processed and discarded.
9026
9027          This segment is also optional; scan_partial_symbols and
9028          add_partial_symbol will handle these DIEs if we chain
9029          them in normally.  When compilers which do not emit large
9030          quantities of duplicate debug information are more common,
9031          this code can probably be removed.  */
9032
9033       /* Any complete simple types at the top level (pretty much all
9034          of them, for a language without namespaces), can be processed
9035          directly.  */
9036       if (parent_die == NULL
9037           && part_die->has_specification == 0
9038           && part_die->is_declaration == 0
9039           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9040               || part_die->tag == DW_TAG_base_type
9041               || part_die->tag == DW_TAG_subrange_type))
9042         {
9043           if (building_psymtab && part_die->name != NULL)
9044             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9045                                  VAR_DOMAIN, LOC_TYPEDEF,
9046                                  &cu->objfile->static_psymbols,
9047                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9048           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9049           continue;
9050         }
9051
9052       /* The exception for DW_TAG_typedef with has_children above is
9053          a workaround of GCC PR debug/47510.  In the case of this complaint
9054          type_name_no_tag_or_error will error on such types later.
9055
9056          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9057          it could not find the child DIEs referenced later, this is checked
9058          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9059
9060       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9061         complaint (&symfile_complaints,
9062                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9063                      "- DIE at 0x%x [in module %s]"),
9064                    part_die->offset, cu->objfile->name);
9065
9066       /* If we're at the second level, and we're an enumerator, and
9067          our parent has no specification (meaning possibly lives in a
9068          namespace elsewhere), then we can add the partial symbol now
9069          instead of queueing it.  */
9070       if (part_die->tag == DW_TAG_enumerator
9071           && parent_die != NULL
9072           && parent_die->die_parent == NULL
9073           && parent_die->tag == DW_TAG_enumeration_type
9074           && parent_die->has_specification == 0)
9075         {
9076           if (part_die->name == NULL)
9077             complaint (&symfile_complaints,
9078                        _("malformed enumerator DIE ignored"));
9079           else if (building_psymtab)
9080             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9081                                  VAR_DOMAIN, LOC_CONST,
9082                                  (cu->language == language_cplus
9083                                   || cu->language == language_java)
9084                                  ? &cu->objfile->global_psymbols
9085                                  : &cu->objfile->static_psymbols,
9086                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9087
9088           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9089           continue;
9090         }
9091
9092       /* We'll save this DIE so link it in.  */
9093       part_die->die_parent = parent_die;
9094       part_die->die_sibling = NULL;
9095       part_die->die_child = NULL;
9096
9097       if (last_die && last_die == parent_die)
9098         last_die->die_child = part_die;
9099       else if (last_die)
9100         last_die->die_sibling = part_die;
9101
9102       last_die = part_die;
9103
9104       if (first_die == NULL)
9105         first_die = part_die;
9106
9107       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9108          find interesting need to be in the hash table, because we
9109          also have the parent/sibling/child chains; only those that we
9110          might refer to by offset later during partial symbol reading.
9111
9112          For now this means things that might have be the target of a
9113          DW_AT_specification, DW_AT_abstract_origin, or
9114          DW_AT_extension.  DW_AT_extension will refer only to
9115          namespaces; DW_AT_abstract_origin refers to functions (and
9116          many things under the function DIE, but we do not recurse
9117          into function DIEs during partial symbol reading) and
9118          possibly variables as well; DW_AT_specification refers to
9119          declarations.  Declarations ought to have the DW_AT_declaration
9120          flag.  It happens that GCC forgets to put it in sometimes, but
9121          only for functions, not for types.
9122
9123          Adding more things than necessary to the hash table is harmless
9124          except for the performance cost.  Adding too few will result in
9125          wasted time in find_partial_die, when we reread the compilation
9126          unit with load_all_dies set.  */
9127
9128       if (load_all
9129           || abbrev->tag == DW_TAG_constant
9130           || abbrev->tag == DW_TAG_subprogram
9131           || abbrev->tag == DW_TAG_variable
9132           || abbrev->tag == DW_TAG_namespace
9133           || part_die->is_declaration)
9134         {
9135           void **slot;
9136
9137           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9138                                            part_die->offset, INSERT);
9139           *slot = part_die;
9140         }
9141
9142       part_die = obstack_alloc (&cu->comp_unit_obstack,
9143                                 sizeof (struct partial_die_info));
9144
9145       /* For some DIEs we want to follow their children (if any).  For C
9146          we have no reason to follow the children of structures; for other
9147          languages we have to, so that we can get at method physnames
9148          to infer fully qualified class names, for DW_AT_specification,
9149          and for C++ template arguments.  For C++, we also look one level
9150          inside functions to find template arguments (if the name of the
9151          function does not already contain the template arguments).
9152
9153          For Ada, we need to scan the children of subprograms and lexical
9154          blocks as well because Ada allows the definition of nested
9155          entities that could be interesting for the debugger, such as
9156          nested subprograms for instance.  */
9157       if (last_die->has_children
9158           && (load_all
9159               || last_die->tag == DW_TAG_namespace
9160               || last_die->tag == DW_TAG_module
9161               || last_die->tag == DW_TAG_enumeration_type
9162               || (cu->language == language_cplus
9163                   && last_die->tag == DW_TAG_subprogram
9164                   && (last_die->name == NULL
9165                       || strchr (last_die->name, '<') == NULL))
9166               || (cu->language != language_c
9167                   && (last_die->tag == DW_TAG_class_type
9168                       || last_die->tag == DW_TAG_interface_type
9169                       || last_die->tag == DW_TAG_structure_type
9170                       || last_die->tag == DW_TAG_union_type))
9171               || (cu->language == language_ada
9172                   && (last_die->tag == DW_TAG_subprogram
9173                       || last_die->tag == DW_TAG_lexical_block))))
9174         {
9175           nesting_level++;
9176           parent_die = last_die;
9177           continue;
9178         }
9179
9180       /* Otherwise we skip to the next sibling, if any.  */
9181       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9182
9183       /* Back to the top, do it again.  */
9184     }
9185 }
9186
9187 /* Read a minimal amount of information into the minimal die structure.  */
9188
9189 static gdb_byte *
9190 read_partial_die (struct partial_die_info *part_die,
9191                   struct abbrev_info *abbrev,
9192                   unsigned int abbrev_len, bfd *abfd,
9193                   gdb_byte *buffer, gdb_byte *info_ptr,
9194                   struct dwarf2_cu *cu)
9195 {
9196   unsigned int i;
9197   struct attribute attr;
9198   int has_low_pc_attr = 0;
9199   int has_high_pc_attr = 0;
9200
9201   memset (part_die, 0, sizeof (struct partial_die_info));
9202
9203   part_die->offset = info_ptr - buffer;
9204
9205   info_ptr += abbrev_len;
9206
9207   if (abbrev == NULL)
9208     return info_ptr;
9209
9210   part_die->tag = abbrev->tag;
9211   part_die->has_children = abbrev->has_children;
9212
9213   for (i = 0; i < abbrev->num_attrs; ++i)
9214     {
9215       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9216
9217       /* Store the data if it is of an attribute we want to keep in a
9218          partial symbol table.  */
9219       switch (attr.name)
9220         {
9221         case DW_AT_name:
9222           switch (part_die->tag)
9223             {
9224             case DW_TAG_compile_unit:
9225             case DW_TAG_type_unit:
9226               /* Compilation units have a DW_AT_name that is a filename, not
9227                  a source language identifier.  */
9228             case DW_TAG_enumeration_type:
9229             case DW_TAG_enumerator:
9230               /* These tags always have simple identifiers already; no need
9231                  to canonicalize them.  */
9232               part_die->name = DW_STRING (&attr);
9233               break;
9234             default:
9235               part_die->name
9236                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9237                                             &cu->objfile->objfile_obstack);
9238               break;
9239             }
9240           break;
9241         case DW_AT_linkage_name:
9242         case DW_AT_MIPS_linkage_name:
9243           /* Note that both forms of linkage name might appear.  We
9244              assume they will be the same, and we only store the last
9245              one we see.  */
9246           if (cu->language == language_ada)
9247             part_die->name = DW_STRING (&attr);
9248           part_die->linkage_name = DW_STRING (&attr);
9249           break;
9250         case DW_AT_low_pc:
9251           has_low_pc_attr = 1;
9252           part_die->lowpc = DW_ADDR (&attr);
9253           break;
9254         case DW_AT_high_pc:
9255           has_high_pc_attr = 1;
9256           part_die->highpc = DW_ADDR (&attr);
9257           break;
9258         case DW_AT_location:
9259           /* Support the .debug_loc offsets.  */
9260           if (attr_form_is_block (&attr))
9261             {
9262                part_die->locdesc = DW_BLOCK (&attr);
9263             }
9264           else if (attr_form_is_section_offset (&attr))
9265             {
9266               dwarf2_complex_location_expr_complaint ();
9267             }
9268           else
9269             {
9270               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9271                                                      "partial symbol information");
9272             }
9273           break;
9274         case DW_AT_external:
9275           part_die->is_external = DW_UNSND (&attr);
9276           break;
9277         case DW_AT_declaration:
9278           part_die->is_declaration = DW_UNSND (&attr);
9279           break;
9280         case DW_AT_type:
9281           part_die->has_type = 1;
9282           break;
9283         case DW_AT_abstract_origin:
9284         case DW_AT_specification:
9285         case DW_AT_extension:
9286           part_die->has_specification = 1;
9287           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9288           break;
9289         case DW_AT_sibling:
9290           /* Ignore absolute siblings, they might point outside of
9291              the current compile unit.  */
9292           if (attr.form == DW_FORM_ref_addr)
9293             complaint (&symfile_complaints,
9294                        _("ignoring absolute DW_AT_sibling"));
9295           else
9296             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9297           break;
9298         case DW_AT_byte_size:
9299           part_die->has_byte_size = 1;
9300           break;
9301         case DW_AT_calling_convention:
9302           /* DWARF doesn't provide a way to identify a program's source-level
9303              entry point.  DW_AT_calling_convention attributes are only meant
9304              to describe functions' calling conventions.
9305
9306              However, because it's a necessary piece of information in
9307              Fortran, and because DW_CC_program is the only piece of debugging
9308              information whose definition refers to a 'main program' at all,
9309              several compilers have begun marking Fortran main programs with
9310              DW_CC_program --- even when those functions use the standard
9311              calling conventions.
9312
9313              So until DWARF specifies a way to provide this information and
9314              compilers pick up the new representation, we'll support this
9315              practice.  */
9316           if (DW_UNSND (&attr) == DW_CC_program
9317               && cu->language == language_fortran)
9318             {
9319               set_main_name (part_die->name);
9320
9321               /* As this DIE has a static linkage the name would be difficult
9322                  to look up later.  */
9323               language_of_main = language_fortran;
9324             }
9325           break;
9326         default:
9327           break;
9328         }
9329     }
9330
9331   if (has_low_pc_attr && has_high_pc_attr)
9332     {
9333       /* When using the GNU linker, .gnu.linkonce. sections are used to
9334          eliminate duplicate copies of functions and vtables and such.
9335          The linker will arbitrarily choose one and discard the others.
9336          The AT_*_pc values for such functions refer to local labels in
9337          these sections.  If the section from that file was discarded, the
9338          labels are not in the output, so the relocs get a value of 0.
9339          If this is a discarded function, mark the pc bounds as invalid,
9340          so that GDB will ignore it.  */
9341       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9342         {
9343           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9344
9345           complaint (&symfile_complaints,
9346                      _("DW_AT_low_pc %s is zero "
9347                        "for DIE at 0x%x [in module %s]"),
9348                      paddress (gdbarch, part_die->lowpc),
9349                      part_die->offset, cu->objfile->name);
9350         }
9351       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9352       else if (part_die->lowpc >= part_die->highpc)
9353         {
9354           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9355
9356           complaint (&symfile_complaints,
9357                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9358                        "for DIE at 0x%x [in module %s]"),
9359                      paddress (gdbarch, part_die->lowpc),
9360                      paddress (gdbarch, part_die->highpc),
9361                      part_die->offset, cu->objfile->name);
9362         }
9363       else
9364         part_die->has_pc_info = 1;
9365     }
9366
9367   return info_ptr;
9368 }
9369
9370 /* Find a cached partial DIE at OFFSET in CU.  */
9371
9372 static struct partial_die_info *
9373 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9374 {
9375   struct partial_die_info *lookup_die = NULL;
9376   struct partial_die_info part_die;
9377
9378   part_die.offset = offset;
9379   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9380
9381   return lookup_die;
9382 }
9383
9384 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9385    except in the case of .debug_types DIEs which do not reference
9386    outside their CU (they do however referencing other types via
9387    DW_FORM_ref_sig8).  */
9388
9389 static struct partial_die_info *
9390 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9391 {
9392   struct dwarf2_per_cu_data *per_cu = NULL;
9393   struct partial_die_info *pd = NULL;
9394
9395   if (cu->per_cu->from_debug_types)
9396     {
9397       pd = find_partial_die_in_comp_unit (offset, cu);
9398       if (pd != NULL)
9399         return pd;
9400       goto not_found;
9401     }
9402
9403   if (offset_in_cu_p (&cu->header, offset))
9404     {
9405       pd = find_partial_die_in_comp_unit (offset, cu);
9406       if (pd != NULL)
9407         return pd;
9408     }
9409
9410   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9411
9412   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9413     load_partial_comp_unit (per_cu, cu->objfile);
9414
9415   per_cu->cu->last_used = 0;
9416   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9417
9418   if (pd == NULL && per_cu->load_all_dies == 0)
9419     {
9420       struct cleanup *back_to;
9421       struct partial_die_info comp_unit_die;
9422       struct abbrev_info *abbrev;
9423       unsigned int bytes_read;
9424       char *info_ptr;
9425
9426       per_cu->load_all_dies = 1;
9427
9428       /* Re-read the DIEs.  */
9429       back_to = make_cleanup (null_cleanup, 0);
9430       if (per_cu->cu->dwarf2_abbrevs == NULL)
9431         {
9432           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9433           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9434         }
9435       info_ptr = (dwarf2_per_objfile->info.buffer
9436                   + per_cu->cu->header.offset
9437                   + per_cu->cu->header.first_die_offset);
9438       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9439       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9440                                    per_cu->cu->objfile->obfd,
9441                                    dwarf2_per_objfile->info.buffer, info_ptr,
9442                                    per_cu->cu);
9443       if (comp_unit_die.has_children)
9444         load_partial_dies (per_cu->cu->objfile->obfd,
9445                            dwarf2_per_objfile->info.buffer, info_ptr,
9446                            0, per_cu->cu);
9447       do_cleanups (back_to);
9448
9449       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9450     }
9451
9452  not_found:
9453
9454   if (pd == NULL)
9455     internal_error (__FILE__, __LINE__,
9456                     _("could not find partial DIE 0x%x "
9457                       "in cache [from module %s]\n"),
9458                     offset, bfd_get_filename (cu->objfile->obfd));
9459   return pd;
9460 }
9461
9462 /* See if we can figure out if the class lives in a namespace.  We do
9463    this by looking for a member function; its demangled name will
9464    contain namespace info, if there is any.  */
9465
9466 static void
9467 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9468                                   struct dwarf2_cu *cu)
9469 {
9470   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9471      what template types look like, because the demangler
9472      frequently doesn't give the same name as the debug info.  We
9473      could fix this by only using the demangled name to get the
9474      prefix (but see comment in read_structure_type).  */
9475
9476   struct partial_die_info *real_pdi;
9477   struct partial_die_info *child_pdi;
9478
9479   /* If this DIE (this DIE's specification, if any) has a parent, then
9480      we should not do this.  We'll prepend the parent's fully qualified
9481      name when we create the partial symbol.  */
9482
9483   real_pdi = struct_pdi;
9484   while (real_pdi->has_specification)
9485     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9486
9487   if (real_pdi->die_parent != NULL)
9488     return;
9489
9490   for (child_pdi = struct_pdi->die_child;
9491        child_pdi != NULL;
9492        child_pdi = child_pdi->die_sibling)
9493     {
9494       if (child_pdi->tag == DW_TAG_subprogram
9495           && child_pdi->linkage_name != NULL)
9496         {
9497           char *actual_class_name
9498             = language_class_name_from_physname (cu->language_defn,
9499                                                  child_pdi->linkage_name);
9500           if (actual_class_name != NULL)
9501             {
9502               struct_pdi->name
9503                 = obsavestring (actual_class_name,
9504                                 strlen (actual_class_name),
9505                                 &cu->objfile->objfile_obstack);
9506               xfree (actual_class_name);
9507             }
9508           break;
9509         }
9510     }
9511 }
9512
9513 /* Adjust PART_DIE before generating a symbol for it.  This function
9514    may set the is_external flag or change the DIE's name.  */
9515
9516 static void
9517 fixup_partial_die (struct partial_die_info *part_die,
9518                    struct dwarf2_cu *cu)
9519 {
9520   /* Once we've fixed up a die, there's no point in doing so again.
9521      This also avoids a memory leak if we were to call
9522      guess_partial_die_structure_name multiple times.  */
9523   if (part_die->fixup_called)
9524     return;
9525
9526   /* If we found a reference attribute and the DIE has no name, try
9527      to find a name in the referred to DIE.  */
9528
9529   if (part_die->name == NULL && part_die->has_specification)
9530     {
9531       struct partial_die_info *spec_die;
9532
9533       spec_die = find_partial_die (part_die->spec_offset, cu);
9534
9535       fixup_partial_die (spec_die, cu);
9536
9537       if (spec_die->name)
9538         {
9539           part_die->name = spec_die->name;
9540
9541           /* Copy DW_AT_external attribute if it is set.  */
9542           if (spec_die->is_external)
9543             part_die->is_external = spec_die->is_external;
9544         }
9545     }
9546
9547   /* Set default names for some unnamed DIEs.  */
9548
9549   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9550     part_die->name = "(anonymous namespace)";
9551
9552   /* If there is no parent die to provide a namespace, and there are
9553      children, see if we can determine the namespace from their linkage
9554      name.
9555      NOTE: We need to do this even if cu->has_namespace_info != 0.
9556      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9557   if (cu->language == language_cplus
9558       && dwarf2_per_objfile->types.asection != NULL
9559       && part_die->die_parent == NULL
9560       && part_die->has_children
9561       && (part_die->tag == DW_TAG_class_type
9562           || part_die->tag == DW_TAG_structure_type
9563           || part_die->tag == DW_TAG_union_type))
9564     guess_partial_die_structure_name (part_die, cu);
9565
9566   /* GCC might emit a nameless struct or union that has a linkage
9567      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
9568   if (part_die->name == NULL
9569       && (part_die->tag == DW_TAG_structure_type
9570           || part_die->tag == DW_TAG_union_type
9571           || part_die->tag == DW_TAG_class_type)
9572       && part_die->linkage_name != NULL)
9573     {
9574       char *demangled;
9575
9576       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9577       if (demangled)
9578         {
9579           part_die->name = obsavestring (demangled, strlen (demangled),
9580                                          &cu->objfile->objfile_obstack);
9581           xfree (demangled);
9582         }
9583     }
9584
9585   part_die->fixup_called = 1;
9586 }
9587
9588 /* Read an attribute value described by an attribute form.  */
9589
9590 static gdb_byte *
9591 read_attribute_value (struct attribute *attr, unsigned form,
9592                       bfd *abfd, gdb_byte *info_ptr,
9593                       struct dwarf2_cu *cu)
9594 {
9595   struct comp_unit_head *cu_header = &cu->header;
9596   unsigned int bytes_read;
9597   struct dwarf_block *blk;
9598
9599   attr->form = form;
9600   switch (form)
9601     {
9602     case DW_FORM_ref_addr:
9603       if (cu->header.version == 2)
9604         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9605       else
9606         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9607                                       &cu->header, &bytes_read);
9608       info_ptr += bytes_read;
9609       break;
9610     case DW_FORM_addr:
9611       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9612       info_ptr += bytes_read;
9613       break;
9614     case DW_FORM_block2:
9615       blk = dwarf_alloc_block (cu);
9616       blk->size = read_2_bytes (abfd, info_ptr);
9617       info_ptr += 2;
9618       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9619       info_ptr += blk->size;
9620       DW_BLOCK (attr) = blk;
9621       break;
9622     case DW_FORM_block4:
9623       blk = dwarf_alloc_block (cu);
9624       blk->size = read_4_bytes (abfd, info_ptr);
9625       info_ptr += 4;
9626       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9627       info_ptr += blk->size;
9628       DW_BLOCK (attr) = blk;
9629       break;
9630     case DW_FORM_data2:
9631       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9632       info_ptr += 2;
9633       break;
9634     case DW_FORM_data4:
9635       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9636       info_ptr += 4;
9637       break;
9638     case DW_FORM_data8:
9639       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9640       info_ptr += 8;
9641       break;
9642     case DW_FORM_sec_offset:
9643       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9644       info_ptr += bytes_read;
9645       break;
9646     case DW_FORM_string:
9647       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9648       DW_STRING_IS_CANONICAL (attr) = 0;
9649       info_ptr += bytes_read;
9650       break;
9651     case DW_FORM_strp:
9652       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9653                                                &bytes_read);
9654       DW_STRING_IS_CANONICAL (attr) = 0;
9655       info_ptr += bytes_read;
9656       break;
9657     case DW_FORM_exprloc:
9658     case DW_FORM_block:
9659       blk = dwarf_alloc_block (cu);
9660       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9661       info_ptr += bytes_read;
9662       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9663       info_ptr += blk->size;
9664       DW_BLOCK (attr) = blk;
9665       break;
9666     case DW_FORM_block1:
9667       blk = dwarf_alloc_block (cu);
9668       blk->size = read_1_byte (abfd, info_ptr);
9669       info_ptr += 1;
9670       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9671       info_ptr += blk->size;
9672       DW_BLOCK (attr) = blk;
9673       break;
9674     case DW_FORM_data1:
9675       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9676       info_ptr += 1;
9677       break;
9678     case DW_FORM_flag:
9679       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9680       info_ptr += 1;
9681       break;
9682     case DW_FORM_flag_present:
9683       DW_UNSND (attr) = 1;
9684       break;
9685     case DW_FORM_sdata:
9686       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9687       info_ptr += bytes_read;
9688       break;
9689     case DW_FORM_udata:
9690       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9691       info_ptr += bytes_read;
9692       break;
9693     case DW_FORM_ref1:
9694       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9695       info_ptr += 1;
9696       break;
9697     case DW_FORM_ref2:
9698       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9699       info_ptr += 2;
9700       break;
9701     case DW_FORM_ref4:
9702       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9703       info_ptr += 4;
9704       break;
9705     case DW_FORM_ref8:
9706       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9707       info_ptr += 8;
9708       break;
9709     case DW_FORM_ref_sig8:
9710       /* Convert the signature to something we can record in DW_UNSND
9711          for later lookup.
9712          NOTE: This is NULL if the type wasn't found.  */
9713       DW_SIGNATURED_TYPE (attr) =
9714         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9715       info_ptr += 8;
9716       break;
9717     case DW_FORM_ref_udata:
9718       DW_ADDR (attr) = (cu->header.offset
9719                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9720       info_ptr += bytes_read;
9721       break;
9722     case DW_FORM_indirect:
9723       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9724       info_ptr += bytes_read;
9725       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9726       break;
9727     default:
9728       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9729              dwarf_form_name (form),
9730              bfd_get_filename (abfd));
9731     }
9732
9733   /* We have seen instances where the compiler tried to emit a byte
9734      size attribute of -1 which ended up being encoded as an unsigned
9735      0xffffffff.  Although 0xffffffff is technically a valid size value,
9736      an object of this size seems pretty unlikely so we can relatively
9737      safely treat these cases as if the size attribute was invalid and
9738      treat them as zero by default.  */
9739   if (attr->name == DW_AT_byte_size
9740       && form == DW_FORM_data4
9741       && DW_UNSND (attr) >= 0xffffffff)
9742     {
9743       complaint
9744         (&symfile_complaints,
9745          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9746          hex_string (DW_UNSND (attr)));
9747       DW_UNSND (attr) = 0;
9748     }
9749
9750   return info_ptr;
9751 }
9752
9753 /* Read an attribute described by an abbreviated attribute.  */
9754
9755 static gdb_byte *
9756 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9757                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9758 {
9759   attr->name = abbrev->name;
9760   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9761 }
9762
9763 /* Read dwarf information from a buffer.  */
9764
9765 static unsigned int
9766 read_1_byte (bfd *abfd, gdb_byte *buf)
9767 {
9768   return bfd_get_8 (abfd, buf);
9769 }
9770
9771 static int
9772 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9773 {
9774   return bfd_get_signed_8 (abfd, buf);
9775 }
9776
9777 static unsigned int
9778 read_2_bytes (bfd *abfd, gdb_byte *buf)
9779 {
9780   return bfd_get_16 (abfd, buf);
9781 }
9782
9783 static int
9784 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9785 {
9786   return bfd_get_signed_16 (abfd, buf);
9787 }
9788
9789 static unsigned int
9790 read_4_bytes (bfd *abfd, gdb_byte *buf)
9791 {
9792   return bfd_get_32 (abfd, buf);
9793 }
9794
9795 static int
9796 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9797 {
9798   return bfd_get_signed_32 (abfd, buf);
9799 }
9800
9801 static ULONGEST
9802 read_8_bytes (bfd *abfd, gdb_byte *buf)
9803 {
9804   return bfd_get_64 (abfd, buf);
9805 }
9806
9807 static CORE_ADDR
9808 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9809               unsigned int *bytes_read)
9810 {
9811   struct comp_unit_head *cu_header = &cu->header;
9812   CORE_ADDR retval = 0;
9813
9814   if (cu_header->signed_addr_p)
9815     {
9816       switch (cu_header->addr_size)
9817         {
9818         case 2:
9819           retval = bfd_get_signed_16 (abfd, buf);
9820           break;
9821         case 4:
9822           retval = bfd_get_signed_32 (abfd, buf);
9823           break;
9824         case 8:
9825           retval = bfd_get_signed_64 (abfd, buf);
9826           break;
9827         default:
9828           internal_error (__FILE__, __LINE__,
9829                           _("read_address: bad switch, signed [in module %s]"),
9830                           bfd_get_filename (abfd));
9831         }
9832     }
9833   else
9834     {
9835       switch (cu_header->addr_size)
9836         {
9837         case 2:
9838           retval = bfd_get_16 (abfd, buf);
9839           break;
9840         case 4:
9841           retval = bfd_get_32 (abfd, buf);
9842           break;
9843         case 8:
9844           retval = bfd_get_64 (abfd, buf);
9845           break;
9846         default:
9847           internal_error (__FILE__, __LINE__,
9848                           _("read_address: bad switch, "
9849                             "unsigned [in module %s]"),
9850                           bfd_get_filename (abfd));
9851         }
9852     }
9853
9854   *bytes_read = cu_header->addr_size;
9855   return retval;
9856 }
9857
9858 /* Read the initial length from a section.  The (draft) DWARF 3
9859    specification allows the initial length to take up either 4 bytes
9860    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9861    bytes describe the length and all offsets will be 8 bytes in length
9862    instead of 4.
9863
9864    An older, non-standard 64-bit format is also handled by this
9865    function.  The older format in question stores the initial length
9866    as an 8-byte quantity without an escape value.  Lengths greater
9867    than 2^32 aren't very common which means that the initial 4 bytes
9868    is almost always zero.  Since a length value of zero doesn't make
9869    sense for the 32-bit format, this initial zero can be considered to
9870    be an escape value which indicates the presence of the older 64-bit
9871    format.  As written, the code can't detect (old format) lengths
9872    greater than 4GB.  If it becomes necessary to handle lengths
9873    somewhat larger than 4GB, we could allow other small values (such
9874    as the non-sensical values of 1, 2, and 3) to also be used as
9875    escape values indicating the presence of the old format.
9876
9877    The value returned via bytes_read should be used to increment the
9878    relevant pointer after calling read_initial_length().
9879
9880    [ Note:  read_initial_length() and read_offset() are based on the
9881      document entitled "DWARF Debugging Information Format", revision
9882      3, draft 8, dated November 19, 2001.  This document was obtained
9883      from:
9884
9885         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9886
9887      This document is only a draft and is subject to change.  (So beware.)
9888
9889      Details regarding the older, non-standard 64-bit format were
9890      determined empirically by examining 64-bit ELF files produced by
9891      the SGI toolchain on an IRIX 6.5 machine.
9892
9893      - Kevin, July 16, 2002
9894    ] */
9895
9896 static LONGEST
9897 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9898 {
9899   LONGEST length = bfd_get_32 (abfd, buf);
9900
9901   if (length == 0xffffffff)
9902     {
9903       length = bfd_get_64 (abfd, buf + 4);
9904       *bytes_read = 12;
9905     }
9906   else if (length == 0)
9907     {
9908       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9909       length = bfd_get_64 (abfd, buf);
9910       *bytes_read = 8;
9911     }
9912   else
9913     {
9914       *bytes_read = 4;
9915     }
9916
9917   return length;
9918 }
9919
9920 /* Cover function for read_initial_length.
9921    Returns the length of the object at BUF, and stores the size of the
9922    initial length in *BYTES_READ and stores the size that offsets will be in
9923    *OFFSET_SIZE.
9924    If the initial length size is not equivalent to that specified in
9925    CU_HEADER then issue a complaint.
9926    This is useful when reading non-comp-unit headers.  */
9927
9928 static LONGEST
9929 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9930                                         const struct comp_unit_head *cu_header,
9931                                         unsigned int *bytes_read,
9932                                         unsigned int *offset_size)
9933 {
9934   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9935
9936   gdb_assert (cu_header->initial_length_size == 4
9937               || cu_header->initial_length_size == 8
9938               || cu_header->initial_length_size == 12);
9939
9940   if (cu_header->initial_length_size != *bytes_read)
9941     complaint (&symfile_complaints,
9942                _("intermixed 32-bit and 64-bit DWARF sections"));
9943
9944   *offset_size = (*bytes_read == 4) ? 4 : 8;
9945   return length;
9946 }
9947
9948 /* Read an offset from the data stream.  The size of the offset is
9949    given by cu_header->offset_size.  */
9950
9951 static LONGEST
9952 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9953              unsigned int *bytes_read)
9954 {
9955   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9956
9957   *bytes_read = cu_header->offset_size;
9958   return offset;
9959 }
9960
9961 /* Read an offset from the data stream.  */
9962
9963 static LONGEST
9964 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9965 {
9966   LONGEST retval = 0;
9967
9968   switch (offset_size)
9969     {
9970     case 4:
9971       retval = bfd_get_32 (abfd, buf);
9972       break;
9973     case 8:
9974       retval = bfd_get_64 (abfd, buf);
9975       break;
9976     default:
9977       internal_error (__FILE__, __LINE__,
9978                       _("read_offset_1: bad switch [in module %s]"),
9979                       bfd_get_filename (abfd));
9980     }
9981
9982   return retval;
9983 }
9984
9985 static gdb_byte *
9986 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9987 {
9988   /* If the size of a host char is 8 bits, we can return a pointer
9989      to the buffer, otherwise we have to copy the data to a buffer
9990      allocated on the temporary obstack.  */
9991   gdb_assert (HOST_CHAR_BIT == 8);
9992   return buf;
9993 }
9994
9995 static char *
9996 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9997 {
9998   /* If the size of a host char is 8 bits, we can return a pointer
9999      to the string, otherwise we have to copy the string to a buffer
10000      allocated on the temporary obstack.  */
10001   gdb_assert (HOST_CHAR_BIT == 8);
10002   if (*buf == '\0')
10003     {
10004       *bytes_read_ptr = 1;
10005       return NULL;
10006     }
10007   *bytes_read_ptr = strlen ((char *) buf) + 1;
10008   return (char *) buf;
10009 }
10010
10011 static char *
10012 read_indirect_string (bfd *abfd, gdb_byte *buf,
10013                       const struct comp_unit_head *cu_header,
10014                       unsigned int *bytes_read_ptr)
10015 {
10016   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10017
10018   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10019   if (dwarf2_per_objfile->str.buffer == NULL)
10020     {
10021       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10022                       bfd_get_filename (abfd));
10023       return NULL;
10024     }
10025   if (str_offset >= dwarf2_per_objfile->str.size)
10026     {
10027       error (_("DW_FORM_strp pointing outside of "
10028                ".debug_str section [in module %s]"),
10029              bfd_get_filename (abfd));
10030       return NULL;
10031     }
10032   gdb_assert (HOST_CHAR_BIT == 8);
10033   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10034     return NULL;
10035   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10036 }
10037
10038 static unsigned long
10039 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10040 {
10041   unsigned long result;
10042   unsigned int num_read;
10043   int i, shift;
10044   unsigned char byte;
10045
10046   result = 0;
10047   shift = 0;
10048   num_read = 0;
10049   i = 0;
10050   while (1)
10051     {
10052       byte = bfd_get_8 (abfd, buf);
10053       buf++;
10054       num_read++;
10055       result |= ((unsigned long)(byte & 127) << shift);
10056       if ((byte & 128) == 0)
10057         {
10058           break;
10059         }
10060       shift += 7;
10061     }
10062   *bytes_read_ptr = num_read;
10063   return result;
10064 }
10065
10066 static long
10067 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10068 {
10069   long result;
10070   int i, shift, num_read;
10071   unsigned char byte;
10072
10073   result = 0;
10074   shift = 0;
10075   num_read = 0;
10076   i = 0;
10077   while (1)
10078     {
10079       byte = bfd_get_8 (abfd, buf);
10080       buf++;
10081       num_read++;
10082       result |= ((long)(byte & 127) << shift);
10083       shift += 7;
10084       if ((byte & 128) == 0)
10085         {
10086           break;
10087         }
10088     }
10089   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10090     result |= -(((long)1) << shift);
10091   *bytes_read_ptr = num_read;
10092   return result;
10093 }
10094
10095 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10096
10097 static gdb_byte *
10098 skip_leb128 (bfd *abfd, gdb_byte *buf)
10099 {
10100   int byte;
10101
10102   while (1)
10103     {
10104       byte = bfd_get_8 (abfd, buf);
10105       buf++;
10106       if ((byte & 128) == 0)
10107         return buf;
10108     }
10109 }
10110
10111 static void
10112 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10113 {
10114   switch (lang)
10115     {
10116     case DW_LANG_C89:
10117     case DW_LANG_C99:
10118     case DW_LANG_C:
10119       cu->language = language_c;
10120       break;
10121     case DW_LANG_C_plus_plus:
10122       cu->language = language_cplus;
10123       break;
10124     case DW_LANG_D:
10125       cu->language = language_d;
10126       break;
10127     case DW_LANG_Fortran77:
10128     case DW_LANG_Fortran90:
10129     case DW_LANG_Fortran95:
10130       cu->language = language_fortran;
10131       break;
10132     case DW_LANG_Mips_Assembler:
10133       cu->language = language_asm;
10134       break;
10135     case DW_LANG_Java:
10136       cu->language = language_java;
10137       break;
10138     case DW_LANG_Ada83:
10139     case DW_LANG_Ada95:
10140       cu->language = language_ada;
10141       break;
10142     case DW_LANG_Modula2:
10143       cu->language = language_m2;
10144       break;
10145     case DW_LANG_Pascal83:
10146       cu->language = language_pascal;
10147       break;
10148     case DW_LANG_ObjC:
10149       cu->language = language_objc;
10150       break;
10151     case DW_LANG_Cobol74:
10152     case DW_LANG_Cobol85:
10153     default:
10154       cu->language = language_minimal;
10155       break;
10156     }
10157   cu->language_defn = language_def (cu->language);
10158 }
10159
10160 /* Return the named attribute or NULL if not there.  */
10161
10162 static struct attribute *
10163 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10164 {
10165   unsigned int i;
10166   struct attribute *spec = NULL;
10167
10168   for (i = 0; i < die->num_attrs; ++i)
10169     {
10170       if (die->attrs[i].name == name)
10171         return &die->attrs[i];
10172       if (die->attrs[i].name == DW_AT_specification
10173           || die->attrs[i].name == DW_AT_abstract_origin)
10174         spec = &die->attrs[i];
10175     }
10176
10177   if (spec)
10178     {
10179       die = follow_die_ref (die, spec, &cu);
10180       return dwarf2_attr (die, name, cu);
10181     }
10182
10183   return NULL;
10184 }
10185
10186 /* Return the named attribute or NULL if not there,
10187    but do not follow DW_AT_specification, etc.
10188    This is for use in contexts where we're reading .debug_types dies.
10189    Following DW_AT_specification, DW_AT_abstract_origin will take us
10190    back up the chain, and we want to go down.  */
10191
10192 static struct attribute *
10193 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10194                        struct dwarf2_cu *cu)
10195 {
10196   unsigned int i;
10197
10198   for (i = 0; i < die->num_attrs; ++i)
10199     if (die->attrs[i].name == name)
10200       return &die->attrs[i];
10201
10202   return NULL;
10203 }
10204
10205 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10206    and holds a non-zero value.  This function should only be used for
10207    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10208
10209 static int
10210 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10211 {
10212   struct attribute *attr = dwarf2_attr (die, name, cu);
10213
10214   return (attr && DW_UNSND (attr));
10215 }
10216
10217 static int
10218 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10219 {
10220   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10221      which value is non-zero.  However, we have to be careful with
10222      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10223      (via dwarf2_flag_true_p) follows this attribute.  So we may
10224      end up accidently finding a declaration attribute that belongs
10225      to a different DIE referenced by the specification attribute,
10226      even though the given DIE does not have a declaration attribute.  */
10227   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10228           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10229 }
10230
10231 /* Return the die giving the specification for DIE, if there is
10232    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10233    containing the return value on output.  If there is no
10234    specification, but there is an abstract origin, that is
10235    returned.  */
10236
10237 static struct die_info *
10238 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10239 {
10240   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10241                                              *spec_cu);
10242
10243   if (spec_attr == NULL)
10244     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10245
10246   if (spec_attr == NULL)
10247     return NULL;
10248   else
10249     return follow_die_ref (die, spec_attr, spec_cu);
10250 }
10251
10252 /* Free the line_header structure *LH, and any arrays and strings it
10253    refers to.
10254    NOTE: This is also used as a "cleanup" function.  */
10255
10256 static void
10257 free_line_header (struct line_header *lh)
10258 {
10259   if (lh->standard_opcode_lengths)
10260     xfree (lh->standard_opcode_lengths);
10261
10262   /* Remember that all the lh->file_names[i].name pointers are
10263      pointers into debug_line_buffer, and don't need to be freed.  */
10264   if (lh->file_names)
10265     xfree (lh->file_names);
10266
10267   /* Similarly for the include directory names.  */
10268   if (lh->include_dirs)
10269     xfree (lh->include_dirs);
10270
10271   xfree (lh);
10272 }
10273
10274 /* Add an entry to LH's include directory table.  */
10275
10276 static void
10277 add_include_dir (struct line_header *lh, char *include_dir)
10278 {
10279   /* Grow the array if necessary.  */
10280   if (lh->include_dirs_size == 0)
10281     {
10282       lh->include_dirs_size = 1; /* for testing */
10283       lh->include_dirs = xmalloc (lh->include_dirs_size
10284                                   * sizeof (*lh->include_dirs));
10285     }
10286   else if (lh->num_include_dirs >= lh->include_dirs_size)
10287     {
10288       lh->include_dirs_size *= 2;
10289       lh->include_dirs = xrealloc (lh->include_dirs,
10290                                    (lh->include_dirs_size
10291                                     * sizeof (*lh->include_dirs)));
10292     }
10293
10294   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10295 }
10296
10297 /* Add an entry to LH's file name table.  */
10298
10299 static void
10300 add_file_name (struct line_header *lh,
10301                char *name,
10302                unsigned int dir_index,
10303                unsigned int mod_time,
10304                unsigned int length)
10305 {
10306   struct file_entry *fe;
10307
10308   /* Grow the array if necessary.  */
10309   if (lh->file_names_size == 0)
10310     {
10311       lh->file_names_size = 1; /* for testing */
10312       lh->file_names = xmalloc (lh->file_names_size
10313                                 * sizeof (*lh->file_names));
10314     }
10315   else if (lh->num_file_names >= lh->file_names_size)
10316     {
10317       lh->file_names_size *= 2;
10318       lh->file_names = xrealloc (lh->file_names,
10319                                  (lh->file_names_size
10320                                   * sizeof (*lh->file_names)));
10321     }
10322
10323   fe = &lh->file_names[lh->num_file_names++];
10324   fe->name = name;
10325   fe->dir_index = dir_index;
10326   fe->mod_time = mod_time;
10327   fe->length = length;
10328   fe->included_p = 0;
10329   fe->symtab = NULL;
10330 }
10331
10332 /* Read the statement program header starting at OFFSET in
10333    .debug_line, according to the endianness of ABFD.  Return a pointer
10334    to a struct line_header, allocated using xmalloc.
10335
10336    NOTE: the strings in the include directory and file name tables of
10337    the returned object point into debug_line_buffer, and must not be
10338    freed.  */
10339
10340 static struct line_header *
10341 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10342                           struct dwarf2_cu *cu)
10343 {
10344   struct cleanup *back_to;
10345   struct line_header *lh;
10346   gdb_byte *line_ptr;
10347   unsigned int bytes_read, offset_size;
10348   int i;
10349   char *cur_dir, *cur_file;
10350
10351   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10352   if (dwarf2_per_objfile->line.buffer == NULL)
10353     {
10354       complaint (&symfile_complaints, _("missing .debug_line section"));
10355       return 0;
10356     }
10357
10358   /* Make sure that at least there's room for the total_length field.
10359      That could be 12 bytes long, but we're just going to fudge that.  */
10360   if (offset + 4 >= dwarf2_per_objfile->line.size)
10361     {
10362       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10363       return 0;
10364     }
10365
10366   lh = xmalloc (sizeof (*lh));
10367   memset (lh, 0, sizeof (*lh));
10368   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10369                           (void *) lh);
10370
10371   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10372
10373   /* Read in the header.  */
10374   lh->total_length =
10375     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10376                                             &bytes_read, &offset_size);
10377   line_ptr += bytes_read;
10378   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10379                                      + dwarf2_per_objfile->line.size))
10380     {
10381       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10382       return 0;
10383     }
10384   lh->statement_program_end = line_ptr + lh->total_length;
10385   lh->version = read_2_bytes (abfd, line_ptr);
10386   line_ptr += 2;
10387   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10388   line_ptr += offset_size;
10389   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10390   line_ptr += 1;
10391   if (lh->version >= 4)
10392     {
10393       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10394       line_ptr += 1;
10395     }
10396   else
10397     lh->maximum_ops_per_instruction = 1;
10398
10399   if (lh->maximum_ops_per_instruction == 0)
10400     {
10401       lh->maximum_ops_per_instruction = 1;
10402       complaint (&symfile_complaints,
10403                  _("invalid maximum_ops_per_instruction "
10404                    "in `.debug_line' section"));
10405     }
10406
10407   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10408   line_ptr += 1;
10409   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10410   line_ptr += 1;
10411   lh->line_range = read_1_byte (abfd, line_ptr);
10412   line_ptr += 1;
10413   lh->opcode_base = read_1_byte (abfd, line_ptr);
10414   line_ptr += 1;
10415   lh->standard_opcode_lengths
10416     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10417
10418   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10419   for (i = 1; i < lh->opcode_base; ++i)
10420     {
10421       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10422       line_ptr += 1;
10423     }
10424
10425   /* Read directory table.  */
10426   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10427     {
10428       line_ptr += bytes_read;
10429       add_include_dir (lh, cur_dir);
10430     }
10431   line_ptr += bytes_read;
10432
10433   /* Read file name table.  */
10434   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10435     {
10436       unsigned int dir_index, mod_time, length;
10437
10438       line_ptr += bytes_read;
10439       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10440       line_ptr += bytes_read;
10441       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10442       line_ptr += bytes_read;
10443       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10444       line_ptr += bytes_read;
10445
10446       add_file_name (lh, cur_file, dir_index, mod_time, length);
10447     }
10448   line_ptr += bytes_read;
10449   lh->statement_program_start = line_ptr;
10450
10451   if (line_ptr > (dwarf2_per_objfile->line.buffer
10452                   + dwarf2_per_objfile->line.size))
10453     complaint (&symfile_complaints,
10454                _("line number info header doesn't "
10455                  "fit in `.debug_line' section"));
10456
10457   discard_cleanups (back_to);
10458   return lh;
10459 }
10460
10461 /* This function exists to work around a bug in certain compilers
10462    (particularly GCC 2.95), in which the first line number marker of a
10463    function does not show up until after the prologue, right before
10464    the second line number marker.  This function shifts ADDRESS down
10465    to the beginning of the function if necessary, and is called on
10466    addresses passed to record_line.  */
10467
10468 static CORE_ADDR
10469 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10470 {
10471   struct function_range *fn;
10472
10473   /* Find the function_range containing address.  */
10474   if (!cu->first_fn)
10475     return address;
10476
10477   if (!cu->cached_fn)
10478     cu->cached_fn = cu->first_fn;
10479
10480   fn = cu->cached_fn;
10481   while (fn)
10482     if (fn->lowpc <= address && fn->highpc > address)
10483       goto found;
10484     else
10485       fn = fn->next;
10486
10487   fn = cu->first_fn;
10488   while (fn && fn != cu->cached_fn)
10489     if (fn->lowpc <= address && fn->highpc > address)
10490       goto found;
10491     else
10492       fn = fn->next;
10493
10494   return address;
10495
10496  found:
10497   if (fn->seen_line)
10498     return address;
10499   if (address != fn->lowpc)
10500     complaint (&symfile_complaints,
10501                _("misplaced first line number at 0x%lx for '%s'"),
10502                (unsigned long) address, fn->name);
10503   fn->seen_line = 1;
10504   return fn->lowpc;
10505 }
10506
10507 /* Subroutine of dwarf_decode_lines to simplify it.
10508    Return the file name of the psymtab for included file FILE_INDEX
10509    in line header LH of PST.
10510    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10511    If space for the result is malloc'd, it will be freed by a cleanup.
10512    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10513
10514 static char *
10515 psymtab_include_file_name (const struct line_header *lh, int file_index,
10516                            const struct partial_symtab *pst,
10517                            const char *comp_dir)
10518 {
10519   const struct file_entry fe = lh->file_names [file_index];
10520   char *include_name = fe.name;
10521   char *include_name_to_compare = include_name;
10522   char *dir_name = NULL;
10523   const char *pst_filename;
10524   char *copied_name = NULL;
10525   int file_is_pst;
10526
10527   if (fe.dir_index)
10528     dir_name = lh->include_dirs[fe.dir_index - 1];
10529
10530   if (!IS_ABSOLUTE_PATH (include_name)
10531       && (dir_name != NULL || comp_dir != NULL))
10532     {
10533       /* Avoid creating a duplicate psymtab for PST.
10534          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10535          Before we do the comparison, however, we need to account
10536          for DIR_NAME and COMP_DIR.
10537          First prepend dir_name (if non-NULL).  If we still don't
10538          have an absolute path prepend comp_dir (if non-NULL).
10539          However, the directory we record in the include-file's
10540          psymtab does not contain COMP_DIR (to match the
10541          corresponding symtab(s)).
10542
10543          Example:
10544
10545          bash$ cd /tmp
10546          bash$ gcc -g ./hello.c
10547          include_name = "hello.c"
10548          dir_name = "."
10549          DW_AT_comp_dir = comp_dir = "/tmp"
10550          DW_AT_name = "./hello.c"  */
10551
10552       if (dir_name != NULL)
10553         {
10554           include_name = concat (dir_name, SLASH_STRING,
10555                                  include_name, (char *)NULL);
10556           include_name_to_compare = include_name;
10557           make_cleanup (xfree, include_name);
10558         }
10559       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10560         {
10561           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10562                                             include_name, (char *)NULL);
10563         }
10564     }
10565
10566   pst_filename = pst->filename;
10567   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10568     {
10569       copied_name = concat (pst->dirname, SLASH_STRING,
10570                             pst_filename, (char *)NULL);
10571       pst_filename = copied_name;
10572     }
10573
10574   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10575
10576   if (include_name_to_compare != include_name)
10577     xfree (include_name_to_compare);
10578   if (copied_name != NULL)
10579     xfree (copied_name);
10580
10581   if (file_is_pst)
10582     return NULL;
10583   return include_name;
10584 }
10585
10586 /* Ignore this record_line request.  */
10587
10588 static void
10589 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10590 {
10591   return;
10592 }
10593
10594 /* Decode the Line Number Program (LNP) for the given line_header
10595    structure and CU.  The actual information extracted and the type
10596    of structures created from the LNP depends on the value of PST.
10597
10598    1. If PST is NULL, then this procedure uses the data from the program
10599       to create all necessary symbol tables, and their linetables.
10600
10601    2. If PST is not NULL, this procedure reads the program to determine
10602       the list of files included by the unit represented by PST, and
10603       builds all the associated partial symbol tables.
10604
10605    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10606    It is used for relative paths in the line table.
10607    NOTE: When processing partial symtabs (pst != NULL),
10608    comp_dir == pst->dirname.
10609
10610    NOTE: It is important that psymtabs have the same file name (via strcmp)
10611    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10612    symtab we don't use it in the name of the psymtabs we create.
10613    E.g. expand_line_sal requires this when finding psymtabs to expand.
10614    A good testcase for this is mb-inline.exp.  */
10615
10616 static void
10617 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10618                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10619 {
10620   gdb_byte *line_ptr, *extended_end;
10621   gdb_byte *line_end;
10622   unsigned int bytes_read, extended_len;
10623   unsigned char op_code, extended_op, adj_opcode;
10624   CORE_ADDR baseaddr;
10625   struct objfile *objfile = cu->objfile;
10626   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10627   const int decode_for_pst_p = (pst != NULL);
10628   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10629   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10630     = record_line;
10631
10632   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10633
10634   line_ptr = lh->statement_program_start;
10635   line_end = lh->statement_program_end;
10636
10637   /* Read the statement sequences until there's nothing left.  */
10638   while (line_ptr < line_end)
10639     {
10640       /* state machine registers  */
10641       CORE_ADDR address = 0;
10642       unsigned int file = 1;
10643       unsigned int line = 1;
10644       unsigned int column = 0;
10645       int is_stmt = lh->default_is_stmt;
10646       int basic_block = 0;
10647       int end_sequence = 0;
10648       CORE_ADDR addr;
10649       unsigned char op_index = 0;
10650
10651       if (!decode_for_pst_p && lh->num_file_names >= file)
10652         {
10653           /* Start a subfile for the current file of the state machine.  */
10654           /* lh->include_dirs and lh->file_names are 0-based, but the
10655              directory and file name numbers in the statement program
10656              are 1-based.  */
10657           struct file_entry *fe = &lh->file_names[file - 1];
10658           char *dir = NULL;
10659
10660           if (fe->dir_index)
10661             dir = lh->include_dirs[fe->dir_index - 1];
10662
10663           dwarf2_start_subfile (fe->name, dir, comp_dir);
10664         }
10665
10666       /* Decode the table.  */
10667       while (!end_sequence)
10668         {
10669           op_code = read_1_byte (abfd, line_ptr);
10670           line_ptr += 1;
10671           if (line_ptr > line_end)
10672             {
10673               dwarf2_debug_line_missing_end_sequence_complaint ();
10674               break;
10675             }
10676
10677           if (op_code >= lh->opcode_base)
10678             {
10679               /* Special operand.  */
10680               adj_opcode = op_code - lh->opcode_base;
10681               address += (((op_index + (adj_opcode / lh->line_range))
10682                            / lh->maximum_ops_per_instruction)
10683                           * lh->minimum_instruction_length);
10684               op_index = ((op_index + (adj_opcode / lh->line_range))
10685                           % lh->maximum_ops_per_instruction);
10686               line += lh->line_base + (adj_opcode % lh->line_range);
10687               if (lh->num_file_names < file || file == 0)
10688                 dwarf2_debug_line_missing_file_complaint ();
10689               /* For now we ignore lines not starting on an
10690                  instruction boundary.  */
10691               else if (op_index == 0)
10692                 {
10693                   lh->file_names[file - 1].included_p = 1;
10694                   if (!decode_for_pst_p && is_stmt)
10695                     {
10696                       if (last_subfile != current_subfile)
10697                         {
10698                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10699                           if (last_subfile)
10700                             (*p_record_line) (last_subfile, 0, addr);
10701                           last_subfile = current_subfile;
10702                         }
10703                       /* Append row to matrix using current values.  */
10704                       addr = check_cu_functions (address, cu);
10705                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10706                       (*p_record_line) (current_subfile, line, addr);
10707                     }
10708                 }
10709               basic_block = 0;
10710             }
10711           else switch (op_code)
10712             {
10713             case DW_LNS_extended_op:
10714               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10715                                                    &bytes_read);
10716               line_ptr += bytes_read;
10717               extended_end = line_ptr + extended_len;
10718               extended_op = read_1_byte (abfd, line_ptr);
10719               line_ptr += 1;
10720               switch (extended_op)
10721                 {
10722                 case DW_LNE_end_sequence:
10723                   p_record_line = record_line;
10724                   end_sequence = 1;
10725                   break;
10726                 case DW_LNE_set_address:
10727                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10728
10729                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10730                     {
10731                       /* This line table is for a function which has been
10732                          GCd by the linker.  Ignore it.  PR gdb/12528 */
10733
10734                       long line_offset
10735                         = line_ptr - dwarf2_per_objfile->line.buffer;
10736
10737                       complaint (&symfile_complaints,
10738                                  _(".debug_line address at offset 0x%lx is 0 "
10739                                    "[in module %s]"),
10740                                  line_offset, cu->objfile->name);
10741                       p_record_line = noop_record_line;
10742                     }
10743
10744                   op_index = 0;
10745                   line_ptr += bytes_read;
10746                   address += baseaddr;
10747                   break;
10748                 case DW_LNE_define_file:
10749                   {
10750                     char *cur_file;
10751                     unsigned int dir_index, mod_time, length;
10752
10753                     cur_file = read_direct_string (abfd, line_ptr,
10754                                                    &bytes_read);
10755                     line_ptr += bytes_read;
10756                     dir_index =
10757                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10758                     line_ptr += bytes_read;
10759                     mod_time =
10760                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10761                     line_ptr += bytes_read;
10762                     length =
10763                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10764                     line_ptr += bytes_read;
10765                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10766                   }
10767                   break;
10768                 case DW_LNE_set_discriminator:
10769                   /* The discriminator is not interesting to the debugger;
10770                      just ignore it.  */
10771                   line_ptr = extended_end;
10772                   break;
10773                 default:
10774                   complaint (&symfile_complaints,
10775                              _("mangled .debug_line section"));
10776                   return;
10777                 }
10778               /* Make sure that we parsed the extended op correctly.  If e.g.
10779                  we expected a different address size than the producer used,
10780                  we may have read the wrong number of bytes.  */
10781               if (line_ptr != extended_end)
10782                 {
10783                   complaint (&symfile_complaints,
10784                              _("mangled .debug_line section"));
10785                   return;
10786                 }
10787               break;
10788             case DW_LNS_copy:
10789               if (lh->num_file_names < file || file == 0)
10790                 dwarf2_debug_line_missing_file_complaint ();
10791               else
10792                 {
10793                   lh->file_names[file - 1].included_p = 1;
10794                   if (!decode_for_pst_p && is_stmt)
10795                     {
10796                       if (last_subfile != current_subfile)
10797                         {
10798                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10799                           if (last_subfile)
10800                             (*p_record_line) (last_subfile, 0, addr);
10801                           last_subfile = current_subfile;
10802                         }
10803                       addr = check_cu_functions (address, cu);
10804                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10805                       (*p_record_line) (current_subfile, line, addr);
10806                     }
10807                 }
10808               basic_block = 0;
10809               break;
10810             case DW_LNS_advance_pc:
10811               {
10812                 CORE_ADDR adjust
10813                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10814
10815                 address += (((op_index + adjust)
10816                              / lh->maximum_ops_per_instruction)
10817                             * lh->minimum_instruction_length);
10818                 op_index = ((op_index + adjust)
10819                             % lh->maximum_ops_per_instruction);
10820                 line_ptr += bytes_read;
10821               }
10822               break;
10823             case DW_LNS_advance_line:
10824               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10825               line_ptr += bytes_read;
10826               break;
10827             case DW_LNS_set_file:
10828               {
10829                 /* The arrays lh->include_dirs and lh->file_names are
10830                    0-based, but the directory and file name numbers in
10831                    the statement program are 1-based.  */
10832                 struct file_entry *fe;
10833                 char *dir = NULL;
10834
10835                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10836                 line_ptr += bytes_read;
10837                 if (lh->num_file_names < file || file == 0)
10838                   dwarf2_debug_line_missing_file_complaint ();
10839                 else
10840                   {
10841                     fe = &lh->file_names[file - 1];
10842                     if (fe->dir_index)
10843                       dir = lh->include_dirs[fe->dir_index - 1];
10844                     if (!decode_for_pst_p)
10845                       {
10846                         last_subfile = current_subfile;
10847                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10848                       }
10849                   }
10850               }
10851               break;
10852             case DW_LNS_set_column:
10853               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10854               line_ptr += bytes_read;
10855               break;
10856             case DW_LNS_negate_stmt:
10857               is_stmt = (!is_stmt);
10858               break;
10859             case DW_LNS_set_basic_block:
10860               basic_block = 1;
10861               break;
10862             /* Add to the address register of the state machine the
10863                address increment value corresponding to special opcode
10864                255.  I.e., this value is scaled by the minimum
10865                instruction length since special opcode 255 would have
10866                scaled the increment.  */
10867             case DW_LNS_const_add_pc:
10868               {
10869                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10870
10871                 address += (((op_index + adjust)
10872                              / lh->maximum_ops_per_instruction)
10873                             * lh->minimum_instruction_length);
10874                 op_index = ((op_index + adjust)
10875                             % lh->maximum_ops_per_instruction);
10876               }
10877               break;
10878             case DW_LNS_fixed_advance_pc:
10879               address += read_2_bytes (abfd, line_ptr);
10880               op_index = 0;
10881               line_ptr += 2;
10882               break;
10883             default:
10884               {
10885                 /* Unknown standard opcode, ignore it.  */
10886                 int i;
10887
10888                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10889                   {
10890                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10891                     line_ptr += bytes_read;
10892                   }
10893               }
10894             }
10895         }
10896       if (lh->num_file_names < file || file == 0)
10897         dwarf2_debug_line_missing_file_complaint ();
10898       else
10899         {
10900           lh->file_names[file - 1].included_p = 1;
10901           if (!decode_for_pst_p)
10902             {
10903               addr = gdbarch_addr_bits_remove (gdbarch, address);
10904               (*p_record_line) (current_subfile, 0, addr);
10905             }
10906         }
10907     }
10908
10909   if (decode_for_pst_p)
10910     {
10911       int file_index;
10912
10913       /* Now that we're done scanning the Line Header Program, we can
10914          create the psymtab of each included file.  */
10915       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10916         if (lh->file_names[file_index].included_p == 1)
10917           {
10918             char *include_name =
10919               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10920             if (include_name != NULL)
10921               dwarf2_create_include_psymtab (include_name, pst, objfile);
10922           }
10923     }
10924   else
10925     {
10926       /* Make sure a symtab is created for every file, even files
10927          which contain only variables (i.e. no code with associated
10928          line numbers).  */
10929
10930       int i;
10931       struct file_entry *fe;
10932
10933       for (i = 0; i < lh->num_file_names; i++)
10934         {
10935           char *dir = NULL;
10936
10937           fe = &lh->file_names[i];
10938           if (fe->dir_index)
10939             dir = lh->include_dirs[fe->dir_index - 1];
10940           dwarf2_start_subfile (fe->name, dir, comp_dir);
10941
10942           /* Skip the main file; we don't need it, and it must be
10943              allocated last, so that it will show up before the
10944              non-primary symtabs in the objfile's symtab list.  */
10945           if (current_subfile == first_subfile)
10946             continue;
10947
10948           if (current_subfile->symtab == NULL)
10949             current_subfile->symtab = allocate_symtab (current_subfile->name,
10950                                                        cu->objfile);
10951           fe->symtab = current_subfile->symtab;
10952         }
10953     }
10954 }
10955
10956 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10957    DIRNAME the name of the source directory which contains FILENAME
10958    or NULL if not known.  COMP_DIR is the compilation directory for the
10959    linetable's compilation unit or NULL if not known.
10960    This routine tries to keep line numbers from identical absolute and
10961    relative file names in a common subfile.
10962
10963    Using the `list' example from the GDB testsuite, which resides in
10964    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10965    of /srcdir/list0.c yields the following debugging information for list0.c:
10966
10967    DW_AT_name:          /srcdir/list0.c
10968    DW_AT_comp_dir:              /compdir
10969    files.files[0].name: list0.h
10970    files.files[0].dir:  /srcdir
10971    files.files[1].name: list0.c
10972    files.files[1].dir:  /srcdir
10973
10974    The line number information for list0.c has to end up in a single
10975    subfile, so that `break /srcdir/list0.c:1' works as expected.
10976    start_subfile will ensure that this happens provided that we pass the
10977    concatenation of files.files[1].dir and files.files[1].name as the
10978    subfile's name.  */
10979
10980 static void
10981 dwarf2_start_subfile (char *filename, const char *dirname,
10982                       const char *comp_dir)
10983 {
10984   char *fullname;
10985
10986   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10987      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10988      second argument to start_subfile.  To be consistent, we do the
10989      same here.  In order not to lose the line information directory,
10990      we concatenate it to the filename when it makes sense.
10991      Note that the Dwarf3 standard says (speaking of filenames in line
10992      information): ``The directory index is ignored for file names
10993      that represent full path names''.  Thus ignoring dirname in the
10994      `else' branch below isn't an issue.  */
10995
10996   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10997     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10998   else
10999     fullname = filename;
11000
11001   start_subfile (fullname, comp_dir);
11002
11003   if (fullname != filename)
11004     xfree (fullname);
11005 }
11006
11007 static void
11008 var_decode_location (struct attribute *attr, struct symbol *sym,
11009                      struct dwarf2_cu *cu)
11010 {
11011   struct objfile *objfile = cu->objfile;
11012   struct comp_unit_head *cu_header = &cu->header;
11013
11014   /* NOTE drow/2003-01-30: There used to be a comment and some special
11015      code here to turn a symbol with DW_AT_external and a
11016      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11017      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11018      with some versions of binutils) where shared libraries could have
11019      relocations against symbols in their debug information - the
11020      minimal symbol would have the right address, but the debug info
11021      would not.  It's no longer necessary, because we will explicitly
11022      apply relocations when we read in the debug information now.  */
11023
11024   /* A DW_AT_location attribute with no contents indicates that a
11025      variable has been optimized away.  */
11026   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11027     {
11028       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11029       return;
11030     }
11031
11032   /* Handle one degenerate form of location expression specially, to
11033      preserve GDB's previous behavior when section offsets are
11034      specified.  If this is just a DW_OP_addr then mark this symbol
11035      as LOC_STATIC.  */
11036
11037   if (attr_form_is_block (attr)
11038       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11039       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11040     {
11041       unsigned int dummy;
11042
11043       SYMBOL_VALUE_ADDRESS (sym) =
11044         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11045       SYMBOL_CLASS (sym) = LOC_STATIC;
11046       fixup_symbol_section (sym, objfile);
11047       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11048                                               SYMBOL_SECTION (sym));
11049       return;
11050     }
11051
11052   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11053      expression evaluator, and use LOC_COMPUTED only when necessary
11054      (i.e. when the value of a register or memory location is
11055      referenced, or a thread-local block, etc.).  Then again, it might
11056      not be worthwhile.  I'm assuming that it isn't unless performance
11057      or memory numbers show me otherwise.  */
11058
11059   dwarf2_symbol_mark_computed (attr, sym, cu);
11060   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11061
11062   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11063     cu->has_loclist = 1;
11064 }
11065
11066 /* Given a pointer to a DWARF information entry, figure out if we need
11067    to make a symbol table entry for it, and if so, create a new entry
11068    and return a pointer to it.
11069    If TYPE is NULL, determine symbol type from the die, otherwise
11070    used the passed type.
11071    If SPACE is not NULL, use it to hold the new symbol.  If it is
11072    NULL, allocate a new symbol on the objfile's obstack.  */
11073
11074 static struct symbol *
11075 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11076                  struct symbol *space)
11077 {
11078   struct objfile *objfile = cu->objfile;
11079   struct symbol *sym = NULL;
11080   char *name;
11081   struct attribute *attr = NULL;
11082   struct attribute *attr2 = NULL;
11083   CORE_ADDR baseaddr;
11084   struct pending **list_to_add = NULL;
11085
11086   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11087
11088   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11089
11090   name = dwarf2_name (die, cu);
11091   if (name)
11092     {
11093       const char *linkagename;
11094       int suppress_add = 0;
11095
11096       if (space)
11097         sym = space;
11098       else
11099         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11100       OBJSTAT (objfile, n_syms++);
11101
11102       /* Cache this symbol's name and the name's demangled form (if any).  */
11103       SYMBOL_SET_LANGUAGE (sym, cu->language);
11104       linkagename = dwarf2_physname (name, die, cu);
11105       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11106
11107       /* Fortran does not have mangling standard and the mangling does differ
11108          between gfortran, iFort etc.  */
11109       if (cu->language == language_fortran
11110           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11111         symbol_set_demangled_name (&(sym->ginfo),
11112                                    (char *) dwarf2_full_name (name, die, cu),
11113                                    NULL);
11114
11115       /* Default assumptions.
11116          Use the passed type or decode it from the die.  */
11117       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11118       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11119       if (type != NULL)
11120         SYMBOL_TYPE (sym) = type;
11121       else
11122         SYMBOL_TYPE (sym) = die_type (die, cu);
11123       attr = dwarf2_attr (die,
11124                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11125                           cu);
11126       if (attr)
11127         {
11128           SYMBOL_LINE (sym) = DW_UNSND (attr);
11129         }
11130
11131       attr = dwarf2_attr (die,
11132                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11133                           cu);
11134       if (attr)
11135         {
11136           int file_index = DW_UNSND (attr);
11137
11138           if (cu->line_header == NULL
11139               || file_index > cu->line_header->num_file_names)
11140             complaint (&symfile_complaints,
11141                        _("file index out of range"));
11142           else if (file_index > 0)
11143             {
11144               struct file_entry *fe;
11145
11146               fe = &cu->line_header->file_names[file_index - 1];
11147               SYMBOL_SYMTAB (sym) = fe->symtab;
11148             }
11149         }
11150
11151       switch (die->tag)
11152         {
11153         case DW_TAG_label:
11154           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11155           if (attr)
11156             {
11157               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11158             }
11159           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11160           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11161           SYMBOL_CLASS (sym) = LOC_LABEL;
11162           add_symbol_to_list (sym, cu->list_in_scope);
11163           break;
11164         case DW_TAG_subprogram:
11165           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11166              finish_block.  */
11167           SYMBOL_CLASS (sym) = LOC_BLOCK;
11168           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11169           if ((attr2 && (DW_UNSND (attr2) != 0))
11170               || cu->language == language_ada)
11171             {
11172               /* Subprograms marked external are stored as a global symbol.
11173                  Ada subprograms, whether marked external or not, are always
11174                  stored as a global symbol, because we want to be able to
11175                  access them globally.  For instance, we want to be able
11176                  to break on a nested subprogram without having to
11177                  specify the context.  */
11178               list_to_add = &global_symbols;
11179             }
11180           else
11181             {
11182               list_to_add = cu->list_in_scope;
11183             }
11184           break;
11185         case DW_TAG_inlined_subroutine:
11186           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11187              finish_block.  */
11188           SYMBOL_CLASS (sym) = LOC_BLOCK;
11189           SYMBOL_INLINED (sym) = 1;
11190           /* Do not add the symbol to any lists.  It will be found via
11191              BLOCK_FUNCTION from the blockvector.  */
11192           break;
11193         case DW_TAG_template_value_param:
11194           suppress_add = 1;
11195           /* Fall through.  */
11196         case DW_TAG_constant:
11197         case DW_TAG_variable:
11198         case DW_TAG_member:
11199           /* Compilation with minimal debug info may result in
11200              variables with missing type entries.  Change the
11201              misleading `void' type to something sensible.  */
11202           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11203             SYMBOL_TYPE (sym)
11204               = objfile_type (objfile)->nodebug_data_symbol;
11205
11206           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11207           /* In the case of DW_TAG_member, we should only be called for
11208              static const members.  */
11209           if (die->tag == DW_TAG_member)
11210             {
11211               /* dwarf2_add_field uses die_is_declaration,
11212                  so we do the same.  */
11213               gdb_assert (die_is_declaration (die, cu));
11214               gdb_assert (attr);
11215             }
11216           if (attr)
11217             {
11218               dwarf2_const_value (attr, sym, cu);
11219               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11220               if (!suppress_add)
11221                 {
11222                   if (attr2 && (DW_UNSND (attr2) != 0))
11223                     list_to_add = &global_symbols;
11224                   else
11225                     list_to_add = cu->list_in_scope;
11226                 }
11227               break;
11228             }
11229           attr = dwarf2_attr (die, DW_AT_location, cu);
11230           if (attr)
11231             {
11232               var_decode_location (attr, sym, cu);
11233               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11234               if (SYMBOL_CLASS (sym) == LOC_STATIC
11235                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11236                   && !dwarf2_per_objfile->has_section_at_zero)
11237                 {
11238                   /* When a static variable is eliminated by the linker,
11239                      the corresponding debug information is not stripped
11240                      out, but the variable address is set to null;
11241                      do not add such variables into symbol table.  */
11242                 }
11243               else if (attr2 && (DW_UNSND (attr2) != 0))
11244                 {
11245                   /* Workaround gfortran PR debug/40040 - it uses
11246                      DW_AT_location for variables in -fPIC libraries which may
11247                      get overriden by other libraries/executable and get
11248                      a different address.  Resolve it by the minimal symbol
11249                      which may come from inferior's executable using copy
11250                      relocation.  Make this workaround only for gfortran as for
11251                      other compilers GDB cannot guess the minimal symbol
11252                      Fortran mangling kind.  */
11253                   if (cu->language == language_fortran && die->parent
11254                       && die->parent->tag == DW_TAG_module
11255                       && cu->producer
11256                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11257                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11258
11259                   /* A variable with DW_AT_external is never static,
11260                      but it may be block-scoped.  */
11261                   list_to_add = (cu->list_in_scope == &file_symbols
11262                                  ? &global_symbols : cu->list_in_scope);
11263                 }
11264               else
11265                 list_to_add = cu->list_in_scope;
11266             }
11267           else
11268             {
11269               /* We do not know the address of this symbol.
11270                  If it is an external symbol and we have type information
11271                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11272                  The address of the variable will then be determined from
11273                  the minimal symbol table whenever the variable is
11274                  referenced.  */
11275               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11276               if (attr2 && (DW_UNSND (attr2) != 0)
11277                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11278                 {
11279                   /* A variable with DW_AT_external is never static, but it
11280                      may be block-scoped.  */
11281                   list_to_add = (cu->list_in_scope == &file_symbols
11282                                  ? &global_symbols : cu->list_in_scope);
11283
11284                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11285                 }
11286               else if (!die_is_declaration (die, cu))
11287                 {
11288                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11289                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11290                   if (!suppress_add)
11291                     list_to_add = cu->list_in_scope;
11292                 }
11293             }
11294           break;
11295         case DW_TAG_formal_parameter:
11296           /* If we are inside a function, mark this as an argument.  If
11297              not, we might be looking at an argument to an inlined function
11298              when we do not have enough information to show inlined frames;
11299              pretend it's a local variable in that case so that the user can
11300              still see it.  */
11301           if (context_stack_depth > 0
11302               && context_stack[context_stack_depth - 1].name != NULL)
11303             SYMBOL_IS_ARGUMENT (sym) = 1;
11304           attr = dwarf2_attr (die, DW_AT_location, cu);
11305           if (attr)
11306             {
11307               var_decode_location (attr, sym, cu);
11308             }
11309           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11310           if (attr)
11311             {
11312               dwarf2_const_value (attr, sym, cu);
11313             }
11314           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11315           if (attr && DW_UNSND (attr))
11316             {
11317               struct type *ref_type;
11318
11319               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11320               SYMBOL_TYPE (sym) = ref_type;
11321             }
11322
11323           list_to_add = cu->list_in_scope;
11324           break;
11325         case DW_TAG_unspecified_parameters:
11326           /* From varargs functions; gdb doesn't seem to have any
11327              interest in this information, so just ignore it for now.
11328              (FIXME?) */
11329           break;
11330         case DW_TAG_template_type_param:
11331           suppress_add = 1;
11332           /* Fall through.  */
11333         case DW_TAG_class_type:
11334         case DW_TAG_interface_type:
11335         case DW_TAG_structure_type:
11336         case DW_TAG_union_type:
11337         case DW_TAG_set_type:
11338         case DW_TAG_enumeration_type:
11339           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11340           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11341
11342           {
11343             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11344                really ever be static objects: otherwise, if you try
11345                to, say, break of a class's method and you're in a file
11346                which doesn't mention that class, it won't work unless
11347                the check for all static symbols in lookup_symbol_aux
11348                saves you.  See the OtherFileClass tests in
11349                gdb.c++/namespace.exp.  */
11350
11351             if (!suppress_add)
11352               {
11353                 list_to_add = (cu->list_in_scope == &file_symbols
11354                                && (cu->language == language_cplus
11355                                    || cu->language == language_java)
11356                                ? &global_symbols : cu->list_in_scope);
11357
11358                 /* The semantics of C++ state that "struct foo {
11359                    ... }" also defines a typedef for "foo".  A Java
11360                    class declaration also defines a typedef for the
11361                    class.  */
11362                 if (cu->language == language_cplus
11363                     || cu->language == language_java
11364                     || cu->language == language_ada)
11365                   {
11366                     /* The symbol's name is already allocated along
11367                        with this objfile, so we don't need to
11368                        duplicate it for the type.  */
11369                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11370                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11371                   }
11372               }
11373           }
11374           break;
11375         case DW_TAG_typedef:
11376           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11377           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11378           list_to_add = cu->list_in_scope;
11379           break;
11380         case DW_TAG_base_type:
11381         case DW_TAG_subrange_type:
11382           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11383           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11384           list_to_add = cu->list_in_scope;
11385           break;
11386         case DW_TAG_enumerator:
11387           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11388           if (attr)
11389             {
11390               dwarf2_const_value (attr, sym, cu);
11391             }
11392           {
11393             /* NOTE: carlton/2003-11-10: See comment above in the
11394                DW_TAG_class_type, etc. block.  */
11395
11396             list_to_add = (cu->list_in_scope == &file_symbols
11397                            && (cu->language == language_cplus
11398                                || cu->language == language_java)
11399                            ? &global_symbols : cu->list_in_scope);
11400           }
11401           break;
11402         case DW_TAG_namespace:
11403           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11404           list_to_add = &global_symbols;
11405           break;
11406         default:
11407           /* Not a tag we recognize.  Hopefully we aren't processing
11408              trash data, but since we must specifically ignore things
11409              we don't recognize, there is nothing else we should do at
11410              this point.  */
11411           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11412                      dwarf_tag_name (die->tag));
11413           break;
11414         }
11415
11416       if (suppress_add)
11417         {
11418           sym->hash_next = objfile->template_symbols;
11419           objfile->template_symbols = sym;
11420           list_to_add = NULL;
11421         }
11422
11423       if (list_to_add != NULL)
11424         add_symbol_to_list (sym, list_to_add);
11425
11426       /* For the benefit of old versions of GCC, check for anonymous
11427          namespaces based on the demangled name.  */
11428       if (!processing_has_namespace_info
11429           && cu->language == language_cplus)
11430         cp_scan_for_anonymous_namespaces (sym);
11431     }
11432   return (sym);
11433 }
11434
11435 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11436
11437 static struct symbol *
11438 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11439 {
11440   return new_symbol_full (die, type, cu, NULL);
11441 }
11442
11443 /* Given an attr with a DW_FORM_dataN value in host byte order,
11444    zero-extend it as appropriate for the symbol's type.  The DWARF
11445    standard (v4) is not entirely clear about the meaning of using
11446    DW_FORM_dataN for a constant with a signed type, where the type is
11447    wider than the data.  The conclusion of a discussion on the DWARF
11448    list was that this is unspecified.  We choose to always zero-extend
11449    because that is the interpretation long in use by GCC.  */
11450
11451 static gdb_byte *
11452 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11453                          const char *name, struct obstack *obstack,
11454                          struct dwarf2_cu *cu, long *value, int bits)
11455 {
11456   struct objfile *objfile = cu->objfile;
11457   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11458                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11459   LONGEST l = DW_UNSND (attr);
11460
11461   if (bits < sizeof (*value) * 8)
11462     {
11463       l &= ((LONGEST) 1 << bits) - 1;
11464       *value = l;
11465     }
11466   else if (bits == sizeof (*value) * 8)
11467     *value = l;
11468   else
11469     {
11470       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11471       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11472       return bytes;
11473     }
11474
11475   return NULL;
11476 }
11477
11478 /* Read a constant value from an attribute.  Either set *VALUE, or if
11479    the value does not fit in *VALUE, set *BYTES - either already
11480    allocated on the objfile obstack, or newly allocated on OBSTACK,
11481    or, set *BATON, if we translated the constant to a location
11482    expression.  */
11483
11484 static void
11485 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11486                          const char *name, struct obstack *obstack,
11487                          struct dwarf2_cu *cu,
11488                          long *value, gdb_byte **bytes,
11489                          struct dwarf2_locexpr_baton **baton)
11490 {
11491   struct objfile *objfile = cu->objfile;
11492   struct comp_unit_head *cu_header = &cu->header;
11493   struct dwarf_block *blk;
11494   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11495                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11496
11497   *value = 0;
11498   *bytes = NULL;
11499   *baton = NULL;
11500
11501   switch (attr->form)
11502     {
11503     case DW_FORM_addr:
11504       {
11505         gdb_byte *data;
11506
11507         if (TYPE_LENGTH (type) != cu_header->addr_size)
11508           dwarf2_const_value_length_mismatch_complaint (name,
11509                                                         cu_header->addr_size,
11510                                                         TYPE_LENGTH (type));
11511         /* Symbols of this form are reasonably rare, so we just
11512            piggyback on the existing location code rather than writing
11513            a new implementation of symbol_computed_ops.  */
11514         *baton = obstack_alloc (&objfile->objfile_obstack,
11515                                 sizeof (struct dwarf2_locexpr_baton));
11516         (*baton)->per_cu = cu->per_cu;
11517         gdb_assert ((*baton)->per_cu);
11518
11519         (*baton)->size = 2 + cu_header->addr_size;
11520         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11521         (*baton)->data = data;
11522
11523         data[0] = DW_OP_addr;
11524         store_unsigned_integer (&data[1], cu_header->addr_size,
11525                                 byte_order, DW_ADDR (attr));
11526         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11527       }
11528       break;
11529     case DW_FORM_string:
11530     case DW_FORM_strp:
11531       /* DW_STRING is already allocated on the objfile obstack, point
11532          directly to it.  */
11533       *bytes = (gdb_byte *) DW_STRING (attr);
11534       break;
11535     case DW_FORM_block1:
11536     case DW_FORM_block2:
11537     case DW_FORM_block4:
11538     case DW_FORM_block:
11539     case DW_FORM_exprloc:
11540       blk = DW_BLOCK (attr);
11541       if (TYPE_LENGTH (type) != blk->size)
11542         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11543                                                       TYPE_LENGTH (type));
11544       *bytes = blk->data;
11545       break;
11546
11547       /* The DW_AT_const_value attributes are supposed to carry the
11548          symbol's value "represented as it would be on the target
11549          architecture."  By the time we get here, it's already been
11550          converted to host endianness, so we just need to sign- or
11551          zero-extend it as appropriate.  */
11552     case DW_FORM_data1:
11553       *bytes = dwarf2_const_value_data (attr, type, name,
11554                                         obstack, cu, value, 8);
11555       break;
11556     case DW_FORM_data2:
11557       *bytes = dwarf2_const_value_data (attr, type, name,
11558                                         obstack, cu, value, 16);
11559       break;
11560     case DW_FORM_data4:
11561       *bytes = dwarf2_const_value_data (attr, type, name,
11562                                         obstack, cu, value, 32);
11563       break;
11564     case DW_FORM_data8:
11565       *bytes = dwarf2_const_value_data (attr, type, name,
11566                                         obstack, cu, value, 64);
11567       break;
11568
11569     case DW_FORM_sdata:
11570       *value = DW_SND (attr);
11571       break;
11572
11573     case DW_FORM_udata:
11574       *value = DW_UNSND (attr);
11575       break;
11576
11577     default:
11578       complaint (&symfile_complaints,
11579                  _("unsupported const value attribute form: '%s'"),
11580                  dwarf_form_name (attr->form));
11581       *value = 0;
11582       break;
11583     }
11584 }
11585
11586
11587 /* Copy constant value from an attribute to a symbol.  */
11588
11589 static void
11590 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11591                     struct dwarf2_cu *cu)
11592 {
11593   struct objfile *objfile = cu->objfile;
11594   struct comp_unit_head *cu_header = &cu->header;
11595   long value;
11596   gdb_byte *bytes;
11597   struct dwarf2_locexpr_baton *baton;
11598
11599   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11600                            SYMBOL_PRINT_NAME (sym),
11601                            &objfile->objfile_obstack, cu,
11602                            &value, &bytes, &baton);
11603
11604   if (baton != NULL)
11605     {
11606       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11607       SYMBOL_LOCATION_BATON (sym) = baton;
11608       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11609     }
11610   else if (bytes != NULL)
11611      {
11612       SYMBOL_VALUE_BYTES (sym) = bytes;
11613       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11614     }
11615   else
11616     {
11617       SYMBOL_VALUE (sym) = value;
11618       SYMBOL_CLASS (sym) = LOC_CONST;
11619     }
11620 }
11621
11622 /* Return the type of the die in question using its DW_AT_type attribute.  */
11623
11624 static struct type *
11625 die_type (struct die_info *die, struct dwarf2_cu *cu)
11626 {
11627   struct attribute *type_attr;
11628
11629   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11630   if (!type_attr)
11631     {
11632       /* A missing DW_AT_type represents a void type.  */
11633       return objfile_type (cu->objfile)->builtin_void;
11634     }
11635
11636   return lookup_die_type (die, type_attr, cu);
11637 }
11638
11639 /* True iff CU's producer generates GNAT Ada auxiliary information
11640    that allows to find parallel types through that information instead
11641    of having to do expensive parallel lookups by type name.  */
11642
11643 static int
11644 need_gnat_info (struct dwarf2_cu *cu)
11645 {
11646   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11647      of GNAT produces this auxiliary information, without any indication
11648      that it is produced.  Part of enhancing the FSF version of GNAT
11649      to produce that information will be to put in place an indicator
11650      that we can use in order to determine whether the descriptive type
11651      info is available or not.  One suggestion that has been made is
11652      to use a new attribute, attached to the CU die.  For now, assume
11653      that the descriptive type info is not available.  */
11654   return 0;
11655 }
11656
11657 /* Return the auxiliary type of the die in question using its
11658    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11659    attribute is not present.  */
11660
11661 static struct type *
11662 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11663 {
11664   struct attribute *type_attr;
11665
11666   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11667   if (!type_attr)
11668     return NULL;
11669
11670   return lookup_die_type (die, type_attr, cu);
11671 }
11672
11673 /* If DIE has a descriptive_type attribute, then set the TYPE's
11674    descriptive type accordingly.  */
11675
11676 static void
11677 set_descriptive_type (struct type *type, struct die_info *die,
11678                       struct dwarf2_cu *cu)
11679 {
11680   struct type *descriptive_type = die_descriptive_type (die, cu);
11681
11682   if (descriptive_type)
11683     {
11684       ALLOCATE_GNAT_AUX_TYPE (type);
11685       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11686     }
11687 }
11688
11689 /* Return the containing type of the die in question using its
11690    DW_AT_containing_type attribute.  */
11691
11692 static struct type *
11693 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11694 {
11695   struct attribute *type_attr;
11696
11697   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11698   if (!type_attr)
11699     error (_("Dwarf Error: Problem turning containing type into gdb type "
11700              "[in module %s]"), cu->objfile->name);
11701
11702   return lookup_die_type (die, type_attr, cu);
11703 }
11704
11705 /* Look up the type of DIE in CU using its type attribute ATTR.
11706    If there is no type substitute an error marker.  */
11707
11708 static struct type *
11709 lookup_die_type (struct die_info *die, struct attribute *attr,
11710                  struct dwarf2_cu *cu)
11711 {
11712   struct type *this_type;
11713
11714   /* First see if we have it cached.  */
11715
11716   if (is_ref_attr (attr))
11717     {
11718       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11719
11720       this_type = get_die_type_at_offset (offset, cu->per_cu);
11721     }
11722   else if (attr->form == DW_FORM_ref_sig8)
11723     {
11724       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11725       struct dwarf2_cu *sig_cu;
11726       unsigned int offset;
11727
11728       /* sig_type will be NULL if the signatured type is missing from
11729          the debug info.  */
11730       if (sig_type == NULL)
11731         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11732                  "at 0x%x [in module %s]"),
11733                die->offset, cu->objfile->name);
11734
11735       gdb_assert (sig_type->per_cu.from_debug_types);
11736       offset = sig_type->per_cu.offset + sig_type->type_offset;
11737       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11738     }
11739   else
11740     {
11741       dump_die_for_error (die);
11742       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11743              dwarf_attr_name (attr->name), cu->objfile->name);
11744     }
11745
11746   /* If not cached we need to read it in.  */
11747
11748   if (this_type == NULL)
11749     {
11750       struct die_info *type_die;
11751       struct dwarf2_cu *type_cu = cu;
11752
11753       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11754       /* If the type is cached, we should have found it above.  */
11755       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11756       this_type = read_type_die_1 (type_die, type_cu);
11757     }
11758
11759   /* If we still don't have a type use an error marker.  */
11760
11761   if (this_type == NULL)
11762     {
11763       char *message, *saved;
11764
11765       /* read_type_die already issued a complaint.  */
11766       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11767                             cu->objfile->name,
11768                             cu->header.offset,
11769                             die->offset);
11770       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11771                              message, strlen (message));
11772       xfree (message);
11773
11774       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11775     }
11776
11777   return this_type;
11778 }
11779
11780 /* Return the type in DIE, CU.
11781    Returns NULL for invalid types.
11782
11783    This first does a lookup in the appropriate type_hash table,
11784    and only reads the die in if necessary.
11785
11786    NOTE: This can be called when reading in partial or full symbols.  */
11787
11788 static struct type *
11789 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11790 {
11791   struct type *this_type;
11792
11793   this_type = get_die_type (die, cu);
11794   if (this_type)
11795     return this_type;
11796
11797   return read_type_die_1 (die, cu);
11798 }
11799
11800 /* Read the type in DIE, CU.
11801    Returns NULL for invalid types.  */
11802
11803 static struct type *
11804 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11805 {
11806   struct type *this_type = NULL;
11807
11808   switch (die->tag)
11809     {
11810     case DW_TAG_class_type:
11811     case DW_TAG_interface_type:
11812     case DW_TAG_structure_type:
11813     case DW_TAG_union_type:
11814       this_type = read_structure_type (die, cu);
11815       break;
11816     case DW_TAG_enumeration_type:
11817       this_type = read_enumeration_type (die, cu);
11818       break;
11819     case DW_TAG_subprogram:
11820     case DW_TAG_subroutine_type:
11821     case DW_TAG_inlined_subroutine:
11822       this_type = read_subroutine_type (die, cu);
11823       break;
11824     case DW_TAG_array_type:
11825       this_type = read_array_type (die, cu);
11826       break;
11827     case DW_TAG_set_type:
11828       this_type = read_set_type (die, cu);
11829       break;
11830     case DW_TAG_pointer_type:
11831       this_type = read_tag_pointer_type (die, cu);
11832       break;
11833     case DW_TAG_ptr_to_member_type:
11834       this_type = read_tag_ptr_to_member_type (die, cu);
11835       break;
11836     case DW_TAG_reference_type:
11837       this_type = read_tag_reference_type (die, cu);
11838       break;
11839     case DW_TAG_const_type:
11840       this_type = read_tag_const_type (die, cu);
11841       break;
11842     case DW_TAG_volatile_type:
11843       this_type = read_tag_volatile_type (die, cu);
11844       break;
11845     case DW_TAG_string_type:
11846       this_type = read_tag_string_type (die, cu);
11847       break;
11848     case DW_TAG_typedef:
11849       this_type = read_typedef (die, cu);
11850       break;
11851     case DW_TAG_subrange_type:
11852       this_type = read_subrange_type (die, cu);
11853       break;
11854     case DW_TAG_base_type:
11855       this_type = read_base_type (die, cu);
11856       break;
11857     case DW_TAG_unspecified_type:
11858       this_type = read_unspecified_type (die, cu);
11859       break;
11860     case DW_TAG_namespace:
11861       this_type = read_namespace_type (die, cu);
11862       break;
11863     case DW_TAG_module:
11864       this_type = read_module_type (die, cu);
11865       break;
11866     default:
11867       complaint (&symfile_complaints,
11868                  _("unexpected tag in read_type_die: '%s'"),
11869                  dwarf_tag_name (die->tag));
11870       break;
11871     }
11872
11873   return this_type;
11874 }
11875
11876 /* See if we can figure out if the class lives in a namespace.  We do
11877    this by looking for a member function; its demangled name will
11878    contain namespace info, if there is any.
11879    Return the computed name or NULL.
11880    Space for the result is allocated on the objfile's obstack.
11881    This is the full-die version of guess_partial_die_structure_name.
11882    In this case we know DIE has no useful parent.  */
11883
11884 static char *
11885 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11886 {
11887   struct die_info *spec_die;
11888   struct dwarf2_cu *spec_cu;
11889   struct die_info *child;
11890
11891   spec_cu = cu;
11892   spec_die = die_specification (die, &spec_cu);
11893   if (spec_die != NULL)
11894     {
11895       die = spec_die;
11896       cu = spec_cu;
11897     }
11898
11899   for (child = die->child;
11900        child != NULL;
11901        child = child->sibling)
11902     {
11903       if (child->tag == DW_TAG_subprogram)
11904         {
11905           struct attribute *attr;
11906
11907           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11908           if (attr == NULL)
11909             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11910           if (attr != NULL)
11911             {
11912               char *actual_name
11913                 = language_class_name_from_physname (cu->language_defn,
11914                                                      DW_STRING (attr));
11915               char *name = NULL;
11916
11917               if (actual_name != NULL)
11918                 {
11919                   char *die_name = dwarf2_name (die, cu);
11920
11921                   if (die_name != NULL
11922                       && strcmp (die_name, actual_name) != 0)
11923                     {
11924                       /* Strip off the class name from the full name.
11925                          We want the prefix.  */
11926                       int die_name_len = strlen (die_name);
11927                       int actual_name_len = strlen (actual_name);
11928
11929                       /* Test for '::' as a sanity check.  */
11930                       if (actual_name_len > die_name_len + 2
11931                           && actual_name[actual_name_len
11932                                          - die_name_len - 1] == ':')
11933                         name =
11934                           obsavestring (actual_name,
11935                                         actual_name_len - die_name_len - 2,
11936                                         &cu->objfile->objfile_obstack);
11937                     }
11938                 }
11939               xfree (actual_name);
11940               return name;
11941             }
11942         }
11943     }
11944
11945   return NULL;
11946 }
11947
11948 /* Return the name of the namespace/class that DIE is defined within,
11949    or "" if we can't tell.  The caller should not xfree the result.
11950
11951    For example, if we're within the method foo() in the following
11952    code:
11953
11954    namespace N {
11955      class C {
11956        void foo () {
11957        }
11958      };
11959    }
11960
11961    then determine_prefix on foo's die will return "N::C".  */
11962
11963 static char *
11964 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11965 {
11966   struct die_info *parent, *spec_die;
11967   struct dwarf2_cu *spec_cu;
11968   struct type *parent_type;
11969
11970   if (cu->language != language_cplus && cu->language != language_java
11971       && cu->language != language_fortran)
11972     return "";
11973
11974   /* We have to be careful in the presence of DW_AT_specification.
11975      For example, with GCC 3.4, given the code
11976
11977      namespace N {
11978        void foo() {
11979          // Definition of N::foo.
11980        }
11981      }
11982
11983      then we'll have a tree of DIEs like this:
11984
11985      1: DW_TAG_compile_unit
11986        2: DW_TAG_namespace        // N
11987          3: DW_TAG_subprogram     // declaration of N::foo
11988        4: DW_TAG_subprogram       // definition of N::foo
11989             DW_AT_specification   // refers to die #3
11990
11991      Thus, when processing die #4, we have to pretend that we're in
11992      the context of its DW_AT_specification, namely the contex of die
11993      #3.  */
11994   spec_cu = cu;
11995   spec_die = die_specification (die, &spec_cu);
11996   if (spec_die == NULL)
11997     parent = die->parent;
11998   else
11999     {
12000       parent = spec_die->parent;
12001       cu = spec_cu;
12002     }
12003
12004   if (parent == NULL)
12005     return "";
12006   else if (parent->building_fullname)
12007     {
12008       const char *name;
12009       const char *parent_name;
12010
12011       /* It has been seen on RealView 2.2 built binaries,
12012          DW_TAG_template_type_param types actually _defined_ as
12013          children of the parent class:
12014
12015          enum E {};
12016          template class <class Enum> Class{};
12017          Class<enum E> class_e;
12018
12019          1: DW_TAG_class_type (Class)
12020            2: DW_TAG_enumeration_type (E)
12021              3: DW_TAG_enumerator (enum1:0)
12022              3: DW_TAG_enumerator (enum2:1)
12023              ...
12024            2: DW_TAG_template_type_param
12025               DW_AT_type  DW_FORM_ref_udata (E)
12026
12027          Besides being broken debug info, it can put GDB into an
12028          infinite loop.  Consider:
12029
12030          When we're building the full name for Class<E>, we'll start
12031          at Class, and go look over its template type parameters,
12032          finding E.  We'll then try to build the full name of E, and
12033          reach here.  We're now trying to build the full name of E,
12034          and look over the parent DIE for containing scope.  In the
12035          broken case, if we followed the parent DIE of E, we'd again
12036          find Class, and once again go look at its template type
12037          arguments, etc., etc.  Simply don't consider such parent die
12038          as source-level parent of this die (it can't be, the language
12039          doesn't allow it), and break the loop here.  */
12040       name = dwarf2_name (die, cu);
12041       parent_name = dwarf2_name (parent, cu);
12042       complaint (&symfile_complaints,
12043                  _("template param type '%s' defined within parent '%s'"),
12044                  name ? name : "<unknown>",
12045                  parent_name ? parent_name : "<unknown>");
12046       return "";
12047     }
12048   else
12049     switch (parent->tag)
12050       {
12051       case DW_TAG_namespace:
12052         parent_type = read_type_die (parent, cu);
12053         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12054            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12055            Work around this problem here.  */
12056         if (cu->language == language_cplus
12057             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12058           return "";
12059         /* We give a name to even anonymous namespaces.  */
12060         return TYPE_TAG_NAME (parent_type);
12061       case DW_TAG_class_type:
12062       case DW_TAG_interface_type:
12063       case DW_TAG_structure_type:
12064       case DW_TAG_union_type:
12065       case DW_TAG_module:
12066         parent_type = read_type_die (parent, cu);
12067         if (TYPE_TAG_NAME (parent_type) != NULL)
12068           return TYPE_TAG_NAME (parent_type);
12069         else
12070           /* An anonymous structure is only allowed non-static data
12071              members; no typedefs, no member functions, et cetera.
12072              So it does not need a prefix.  */
12073           return "";
12074       case DW_TAG_compile_unit:
12075         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12076         if (cu->language == language_cplus
12077             && dwarf2_per_objfile->types.asection != NULL
12078             && die->child != NULL
12079             && (die->tag == DW_TAG_class_type
12080                 || die->tag == DW_TAG_structure_type
12081                 || die->tag == DW_TAG_union_type))
12082           {
12083             char *name = guess_full_die_structure_name (die, cu);
12084             if (name != NULL)
12085               return name;
12086           }
12087         return "";
12088       default:
12089         return determine_prefix (parent, cu);
12090       }
12091 }
12092
12093 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12094    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12095    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12096    an obconcat, otherwise allocate storage for the result.  The CU argument is
12097    used to determine the language and hence, the appropriate separator.  */
12098
12099 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12100
12101 static char *
12102 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12103                  int physname, struct dwarf2_cu *cu)
12104 {
12105   const char *lead = "";
12106   const char *sep;
12107
12108   if (suffix == NULL || suffix[0] == '\0'
12109       || prefix == NULL || prefix[0] == '\0')
12110     sep = "";
12111   else if (cu->language == language_java)
12112     sep = ".";
12113   else if (cu->language == language_fortran && physname)
12114     {
12115       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12116          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12117
12118       lead = "__";
12119       sep = "_MOD_";
12120     }
12121   else
12122     sep = "::";
12123
12124   if (prefix == NULL)
12125     prefix = "";
12126   if (suffix == NULL)
12127     suffix = "";
12128
12129   if (obs == NULL)
12130     {
12131       char *retval
12132         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12133
12134       strcpy (retval, lead);
12135       strcat (retval, prefix);
12136       strcat (retval, sep);
12137       strcat (retval, suffix);
12138       return retval;
12139     }
12140   else
12141     {
12142       /* We have an obstack.  */
12143       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12144     }
12145 }
12146
12147 /* Return sibling of die, NULL if no sibling.  */
12148
12149 static struct die_info *
12150 sibling_die (struct die_info *die)
12151 {
12152   return die->sibling;
12153 }
12154
12155 /* Get name of a die, return NULL if not found.  */
12156
12157 static char *
12158 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12159                           struct obstack *obstack)
12160 {
12161   if (name && cu->language == language_cplus)
12162     {
12163       char *canon_name = cp_canonicalize_string (name);
12164
12165       if (canon_name != NULL)
12166         {
12167           if (strcmp (canon_name, name) != 0)
12168             name = obsavestring (canon_name, strlen (canon_name),
12169                                  obstack);
12170           xfree (canon_name);
12171         }
12172     }
12173
12174   return name;
12175 }
12176
12177 /* Get name of a die, return NULL if not found.  */
12178
12179 static char *
12180 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12181 {
12182   struct attribute *attr;
12183
12184   attr = dwarf2_attr (die, DW_AT_name, cu);
12185   if ((!attr || !DW_STRING (attr))
12186       && die->tag != DW_TAG_class_type
12187       && die->tag != DW_TAG_interface_type
12188       && die->tag != DW_TAG_structure_type
12189       && die->tag != DW_TAG_union_type)
12190     return NULL;
12191
12192   switch (die->tag)
12193     {
12194     case DW_TAG_compile_unit:
12195       /* Compilation units have a DW_AT_name that is a filename, not
12196          a source language identifier.  */
12197     case DW_TAG_enumeration_type:
12198     case DW_TAG_enumerator:
12199       /* These tags always have simple identifiers already; no need
12200          to canonicalize them.  */
12201       return DW_STRING (attr);
12202
12203     case DW_TAG_subprogram:
12204       /* Java constructors will all be named "<init>", so return
12205          the class name when we see this special case.  */
12206       if (cu->language == language_java
12207           && DW_STRING (attr) != NULL
12208           && strcmp (DW_STRING (attr), "<init>") == 0)
12209         {
12210           struct dwarf2_cu *spec_cu = cu;
12211           struct die_info *spec_die;
12212
12213           /* GCJ will output '<init>' for Java constructor names.
12214              For this special case, return the name of the parent class.  */
12215
12216           /* GCJ may output suprogram DIEs with AT_specification set.
12217              If so, use the name of the specified DIE.  */
12218           spec_die = die_specification (die, &spec_cu);
12219           if (spec_die != NULL)
12220             return dwarf2_name (spec_die, spec_cu);
12221
12222           do
12223             {
12224               die = die->parent;
12225               if (die->tag == DW_TAG_class_type)
12226                 return dwarf2_name (die, cu);
12227             }
12228           while (die->tag != DW_TAG_compile_unit);
12229         }
12230       break;
12231
12232     case DW_TAG_class_type:
12233     case DW_TAG_interface_type:
12234     case DW_TAG_structure_type:
12235     case DW_TAG_union_type:
12236       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12237          structures or unions.  These were of the form "._%d" in GCC 4.1,
12238          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12239          and GCC 4.4.  We work around this problem by ignoring these.  */
12240       if (attr && DW_STRING (attr)
12241           && (strncmp (DW_STRING (attr), "._", 2) == 0
12242               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12243         return NULL;
12244
12245       /* GCC might emit a nameless typedef that has a linkage name.  See
12246          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12247       if (!attr || DW_STRING (attr) == NULL)
12248         {
12249           char *demangled = NULL;
12250
12251           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12252           if (attr == NULL)
12253             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12254
12255           if (attr == NULL || DW_STRING (attr) == NULL)
12256             return NULL;
12257
12258           /* Avoid demangling DW_STRING (attr) the second time on a second
12259              call for the same DIE.  */
12260           if (!DW_STRING_IS_CANONICAL (attr))
12261             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12262
12263           if (demangled)
12264             {
12265               /* FIXME: we already did this for the partial symbol... */
12266               DW_STRING (attr)
12267                 = obsavestring (demangled, strlen (demangled),
12268                                 &cu->objfile->objfile_obstack);
12269               DW_STRING_IS_CANONICAL (attr) = 1;
12270               xfree (demangled);
12271             }
12272         }
12273       break;
12274
12275     default:
12276       break;
12277     }
12278
12279   if (!DW_STRING_IS_CANONICAL (attr))
12280     {
12281       DW_STRING (attr)
12282         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12283                                     &cu->objfile->objfile_obstack);
12284       DW_STRING_IS_CANONICAL (attr) = 1;
12285     }
12286   return DW_STRING (attr);
12287 }
12288
12289 /* Return the die that this die in an extension of, or NULL if there
12290    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12291    containing the return value on output.  */
12292
12293 static struct die_info *
12294 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12295 {
12296   struct attribute *attr;
12297
12298   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12299   if (attr == NULL)
12300     return NULL;
12301
12302   return follow_die_ref (die, attr, ext_cu);
12303 }
12304
12305 /* Convert a DIE tag into its string name.  */
12306
12307 static char *
12308 dwarf_tag_name (unsigned tag)
12309 {
12310   switch (tag)
12311     {
12312     case DW_TAG_padding:
12313       return "DW_TAG_padding";
12314     case DW_TAG_array_type:
12315       return "DW_TAG_array_type";
12316     case DW_TAG_class_type:
12317       return "DW_TAG_class_type";
12318     case DW_TAG_entry_point:
12319       return "DW_TAG_entry_point";
12320     case DW_TAG_enumeration_type:
12321       return "DW_TAG_enumeration_type";
12322     case DW_TAG_formal_parameter:
12323       return "DW_TAG_formal_parameter";
12324     case DW_TAG_imported_declaration:
12325       return "DW_TAG_imported_declaration";
12326     case DW_TAG_label:
12327       return "DW_TAG_label";
12328     case DW_TAG_lexical_block:
12329       return "DW_TAG_lexical_block";
12330     case DW_TAG_member:
12331       return "DW_TAG_member";
12332     case DW_TAG_pointer_type:
12333       return "DW_TAG_pointer_type";
12334     case DW_TAG_reference_type:
12335       return "DW_TAG_reference_type";
12336     case DW_TAG_compile_unit:
12337       return "DW_TAG_compile_unit";
12338     case DW_TAG_string_type:
12339       return "DW_TAG_string_type";
12340     case DW_TAG_structure_type:
12341       return "DW_TAG_structure_type";
12342     case DW_TAG_subroutine_type:
12343       return "DW_TAG_subroutine_type";
12344     case DW_TAG_typedef:
12345       return "DW_TAG_typedef";
12346     case DW_TAG_union_type:
12347       return "DW_TAG_union_type";
12348     case DW_TAG_unspecified_parameters:
12349       return "DW_TAG_unspecified_parameters";
12350     case DW_TAG_variant:
12351       return "DW_TAG_variant";
12352     case DW_TAG_common_block:
12353       return "DW_TAG_common_block";
12354     case DW_TAG_common_inclusion:
12355       return "DW_TAG_common_inclusion";
12356     case DW_TAG_inheritance:
12357       return "DW_TAG_inheritance";
12358     case DW_TAG_inlined_subroutine:
12359       return "DW_TAG_inlined_subroutine";
12360     case DW_TAG_module:
12361       return "DW_TAG_module";
12362     case DW_TAG_ptr_to_member_type:
12363       return "DW_TAG_ptr_to_member_type";
12364     case DW_TAG_set_type:
12365       return "DW_TAG_set_type";
12366     case DW_TAG_subrange_type:
12367       return "DW_TAG_subrange_type";
12368     case DW_TAG_with_stmt:
12369       return "DW_TAG_with_stmt";
12370     case DW_TAG_access_declaration:
12371       return "DW_TAG_access_declaration";
12372     case DW_TAG_base_type:
12373       return "DW_TAG_base_type";
12374     case DW_TAG_catch_block:
12375       return "DW_TAG_catch_block";
12376     case DW_TAG_const_type:
12377       return "DW_TAG_const_type";
12378     case DW_TAG_constant:
12379       return "DW_TAG_constant";
12380     case DW_TAG_enumerator:
12381       return "DW_TAG_enumerator";
12382     case DW_TAG_file_type:
12383       return "DW_TAG_file_type";
12384     case DW_TAG_friend:
12385       return "DW_TAG_friend";
12386     case DW_TAG_namelist:
12387       return "DW_TAG_namelist";
12388     case DW_TAG_namelist_item:
12389       return "DW_TAG_namelist_item";
12390     case DW_TAG_packed_type:
12391       return "DW_TAG_packed_type";
12392     case DW_TAG_subprogram:
12393       return "DW_TAG_subprogram";
12394     case DW_TAG_template_type_param:
12395       return "DW_TAG_template_type_param";
12396     case DW_TAG_template_value_param:
12397       return "DW_TAG_template_value_param";
12398     case DW_TAG_thrown_type:
12399       return "DW_TAG_thrown_type";
12400     case DW_TAG_try_block:
12401       return "DW_TAG_try_block";
12402     case DW_TAG_variant_part:
12403       return "DW_TAG_variant_part";
12404     case DW_TAG_variable:
12405       return "DW_TAG_variable";
12406     case DW_TAG_volatile_type:
12407       return "DW_TAG_volatile_type";
12408     case DW_TAG_dwarf_procedure:
12409       return "DW_TAG_dwarf_procedure";
12410     case DW_TAG_restrict_type:
12411       return "DW_TAG_restrict_type";
12412     case DW_TAG_interface_type:
12413       return "DW_TAG_interface_type";
12414     case DW_TAG_namespace:
12415       return "DW_TAG_namespace";
12416     case DW_TAG_imported_module:
12417       return "DW_TAG_imported_module";
12418     case DW_TAG_unspecified_type:
12419       return "DW_TAG_unspecified_type";
12420     case DW_TAG_partial_unit:
12421       return "DW_TAG_partial_unit";
12422     case DW_TAG_imported_unit:
12423       return "DW_TAG_imported_unit";
12424     case DW_TAG_condition:
12425       return "DW_TAG_condition";
12426     case DW_TAG_shared_type:
12427       return "DW_TAG_shared_type";
12428     case DW_TAG_type_unit:
12429       return "DW_TAG_type_unit";
12430     case DW_TAG_MIPS_loop:
12431       return "DW_TAG_MIPS_loop";
12432     case DW_TAG_HP_array_descriptor:
12433       return "DW_TAG_HP_array_descriptor";
12434     case DW_TAG_format_label:
12435       return "DW_TAG_format_label";
12436     case DW_TAG_function_template:
12437       return "DW_TAG_function_template";
12438     case DW_TAG_class_template:
12439       return "DW_TAG_class_template";
12440     case DW_TAG_GNU_BINCL:
12441       return "DW_TAG_GNU_BINCL";
12442     case DW_TAG_GNU_EINCL:
12443       return "DW_TAG_GNU_EINCL";
12444     case DW_TAG_upc_shared_type:
12445       return "DW_TAG_upc_shared_type";
12446     case DW_TAG_upc_strict_type:
12447       return "DW_TAG_upc_strict_type";
12448     case DW_TAG_upc_relaxed_type:
12449       return "DW_TAG_upc_relaxed_type";
12450     case DW_TAG_PGI_kanji_type:
12451       return "DW_TAG_PGI_kanji_type";
12452     case DW_TAG_PGI_interface_block:
12453       return "DW_TAG_PGI_interface_block";
12454     default:
12455       return "DW_TAG_<unknown>";
12456     }
12457 }
12458
12459 /* Convert a DWARF attribute code into its string name.  */
12460
12461 static char *
12462 dwarf_attr_name (unsigned attr)
12463 {
12464   switch (attr)
12465     {
12466     case DW_AT_sibling:
12467       return "DW_AT_sibling";
12468     case DW_AT_location:
12469       return "DW_AT_location";
12470     case DW_AT_name:
12471       return "DW_AT_name";
12472     case DW_AT_ordering:
12473       return "DW_AT_ordering";
12474     case DW_AT_subscr_data:
12475       return "DW_AT_subscr_data";
12476     case DW_AT_byte_size:
12477       return "DW_AT_byte_size";
12478     case DW_AT_bit_offset:
12479       return "DW_AT_bit_offset";
12480     case DW_AT_bit_size:
12481       return "DW_AT_bit_size";
12482     case DW_AT_element_list:
12483       return "DW_AT_element_list";
12484     case DW_AT_stmt_list:
12485       return "DW_AT_stmt_list";
12486     case DW_AT_low_pc:
12487       return "DW_AT_low_pc";
12488     case DW_AT_high_pc:
12489       return "DW_AT_high_pc";
12490     case DW_AT_language:
12491       return "DW_AT_language";
12492     case DW_AT_member:
12493       return "DW_AT_member";
12494     case DW_AT_discr:
12495       return "DW_AT_discr";
12496     case DW_AT_discr_value:
12497       return "DW_AT_discr_value";
12498     case DW_AT_visibility:
12499       return "DW_AT_visibility";
12500     case DW_AT_import:
12501       return "DW_AT_import";
12502     case DW_AT_string_length:
12503       return "DW_AT_string_length";
12504     case DW_AT_common_reference:
12505       return "DW_AT_common_reference";
12506     case DW_AT_comp_dir:
12507       return "DW_AT_comp_dir";
12508     case DW_AT_const_value:
12509       return "DW_AT_const_value";
12510     case DW_AT_containing_type:
12511       return "DW_AT_containing_type";
12512     case DW_AT_default_value:
12513       return "DW_AT_default_value";
12514     case DW_AT_inline:
12515       return "DW_AT_inline";
12516     case DW_AT_is_optional:
12517       return "DW_AT_is_optional";
12518     case DW_AT_lower_bound:
12519       return "DW_AT_lower_bound";
12520     case DW_AT_producer:
12521       return "DW_AT_producer";
12522     case DW_AT_prototyped:
12523       return "DW_AT_prototyped";
12524     case DW_AT_return_addr:
12525       return "DW_AT_return_addr";
12526     case DW_AT_start_scope:
12527       return "DW_AT_start_scope";
12528     case DW_AT_bit_stride:
12529       return "DW_AT_bit_stride";
12530     case DW_AT_upper_bound:
12531       return "DW_AT_upper_bound";
12532     case DW_AT_abstract_origin:
12533       return "DW_AT_abstract_origin";
12534     case DW_AT_accessibility:
12535       return "DW_AT_accessibility";
12536     case DW_AT_address_class:
12537       return "DW_AT_address_class";
12538     case DW_AT_artificial:
12539       return "DW_AT_artificial";
12540     case DW_AT_base_types:
12541       return "DW_AT_base_types";
12542     case DW_AT_calling_convention:
12543       return "DW_AT_calling_convention";
12544     case DW_AT_count:
12545       return "DW_AT_count";
12546     case DW_AT_data_member_location:
12547       return "DW_AT_data_member_location";
12548     case DW_AT_decl_column:
12549       return "DW_AT_decl_column";
12550     case DW_AT_decl_file:
12551       return "DW_AT_decl_file";
12552     case DW_AT_decl_line:
12553       return "DW_AT_decl_line";
12554     case DW_AT_declaration:
12555       return "DW_AT_declaration";
12556     case DW_AT_discr_list:
12557       return "DW_AT_discr_list";
12558     case DW_AT_encoding:
12559       return "DW_AT_encoding";
12560     case DW_AT_external:
12561       return "DW_AT_external";
12562     case DW_AT_frame_base:
12563       return "DW_AT_frame_base";
12564     case DW_AT_friend:
12565       return "DW_AT_friend";
12566     case DW_AT_identifier_case:
12567       return "DW_AT_identifier_case";
12568     case DW_AT_macro_info:
12569       return "DW_AT_macro_info";
12570     case DW_AT_namelist_items:
12571       return "DW_AT_namelist_items";
12572     case DW_AT_priority:
12573       return "DW_AT_priority";
12574     case DW_AT_segment:
12575       return "DW_AT_segment";
12576     case DW_AT_specification:
12577       return "DW_AT_specification";
12578     case DW_AT_static_link:
12579       return "DW_AT_static_link";
12580     case DW_AT_type:
12581       return "DW_AT_type";
12582     case DW_AT_use_location:
12583       return "DW_AT_use_location";
12584     case DW_AT_variable_parameter:
12585       return "DW_AT_variable_parameter";
12586     case DW_AT_virtuality:
12587       return "DW_AT_virtuality";
12588     case DW_AT_vtable_elem_location:
12589       return "DW_AT_vtable_elem_location";
12590     /* DWARF 3 values.  */
12591     case DW_AT_allocated:
12592       return "DW_AT_allocated";
12593     case DW_AT_associated:
12594       return "DW_AT_associated";
12595     case DW_AT_data_location:
12596       return "DW_AT_data_location";
12597     case DW_AT_byte_stride:
12598       return "DW_AT_byte_stride";
12599     case DW_AT_entry_pc:
12600       return "DW_AT_entry_pc";
12601     case DW_AT_use_UTF8:
12602       return "DW_AT_use_UTF8";
12603     case DW_AT_extension:
12604       return "DW_AT_extension";
12605     case DW_AT_ranges:
12606       return "DW_AT_ranges";
12607     case DW_AT_trampoline:
12608       return "DW_AT_trampoline";
12609     case DW_AT_call_column:
12610       return "DW_AT_call_column";
12611     case DW_AT_call_file:
12612       return "DW_AT_call_file";
12613     case DW_AT_call_line:
12614       return "DW_AT_call_line";
12615     case DW_AT_description:
12616       return "DW_AT_description";
12617     case DW_AT_binary_scale:
12618       return "DW_AT_binary_scale";
12619     case DW_AT_decimal_scale:
12620       return "DW_AT_decimal_scale";
12621     case DW_AT_small:
12622       return "DW_AT_small";
12623     case DW_AT_decimal_sign:
12624       return "DW_AT_decimal_sign";
12625     case DW_AT_digit_count:
12626       return "DW_AT_digit_count";
12627     case DW_AT_picture_string:
12628       return "DW_AT_picture_string";
12629     case DW_AT_mutable:
12630       return "DW_AT_mutable";
12631     case DW_AT_threads_scaled:
12632       return "DW_AT_threads_scaled";
12633     case DW_AT_explicit:
12634       return "DW_AT_explicit";
12635     case DW_AT_object_pointer:
12636       return "DW_AT_object_pointer";
12637     case DW_AT_endianity:
12638       return "DW_AT_endianity";
12639     case DW_AT_elemental:
12640       return "DW_AT_elemental";
12641     case DW_AT_pure:
12642       return "DW_AT_pure";
12643     case DW_AT_recursive:
12644       return "DW_AT_recursive";
12645     /* DWARF 4 values.  */
12646     case DW_AT_signature:
12647       return "DW_AT_signature";
12648     case DW_AT_linkage_name:
12649       return "DW_AT_linkage_name";
12650     /* SGI/MIPS extensions.  */
12651 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12652     case DW_AT_MIPS_fde:
12653       return "DW_AT_MIPS_fde";
12654 #endif
12655     case DW_AT_MIPS_loop_begin:
12656       return "DW_AT_MIPS_loop_begin";
12657     case DW_AT_MIPS_tail_loop_begin:
12658       return "DW_AT_MIPS_tail_loop_begin";
12659     case DW_AT_MIPS_epilog_begin:
12660       return "DW_AT_MIPS_epilog_begin";
12661     case DW_AT_MIPS_loop_unroll_factor:
12662       return "DW_AT_MIPS_loop_unroll_factor";
12663     case DW_AT_MIPS_software_pipeline_depth:
12664       return "DW_AT_MIPS_software_pipeline_depth";
12665     case DW_AT_MIPS_linkage_name:
12666       return "DW_AT_MIPS_linkage_name";
12667     case DW_AT_MIPS_stride:
12668       return "DW_AT_MIPS_stride";
12669     case DW_AT_MIPS_abstract_name:
12670       return "DW_AT_MIPS_abstract_name";
12671     case DW_AT_MIPS_clone_origin:
12672       return "DW_AT_MIPS_clone_origin";
12673     case DW_AT_MIPS_has_inlines:
12674       return "DW_AT_MIPS_has_inlines";
12675     /* HP extensions.  */
12676 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12677     case DW_AT_HP_block_index:
12678       return "DW_AT_HP_block_index";
12679 #endif
12680     case DW_AT_HP_unmodifiable:
12681       return "DW_AT_HP_unmodifiable";
12682     case DW_AT_HP_actuals_stmt_list:
12683       return "DW_AT_HP_actuals_stmt_list";
12684     case DW_AT_HP_proc_per_section:
12685       return "DW_AT_HP_proc_per_section";
12686     case DW_AT_HP_raw_data_ptr:
12687       return "DW_AT_HP_raw_data_ptr";
12688     case DW_AT_HP_pass_by_reference:
12689       return "DW_AT_HP_pass_by_reference";
12690     case DW_AT_HP_opt_level:
12691       return "DW_AT_HP_opt_level";
12692     case DW_AT_HP_prof_version_id:
12693       return "DW_AT_HP_prof_version_id";
12694     case DW_AT_HP_opt_flags:
12695       return "DW_AT_HP_opt_flags";
12696     case DW_AT_HP_cold_region_low_pc:
12697       return "DW_AT_HP_cold_region_low_pc";
12698     case DW_AT_HP_cold_region_high_pc:
12699       return "DW_AT_HP_cold_region_high_pc";
12700     case DW_AT_HP_all_variables_modifiable:
12701       return "DW_AT_HP_all_variables_modifiable";
12702     case DW_AT_HP_linkage_name:
12703       return "DW_AT_HP_linkage_name";
12704     case DW_AT_HP_prof_flags:
12705       return "DW_AT_HP_prof_flags";
12706     /* GNU extensions.  */
12707     case DW_AT_sf_names:
12708       return "DW_AT_sf_names";
12709     case DW_AT_src_info:
12710       return "DW_AT_src_info";
12711     case DW_AT_mac_info:
12712       return "DW_AT_mac_info";
12713     case DW_AT_src_coords:
12714       return "DW_AT_src_coords";
12715     case DW_AT_body_begin:
12716       return "DW_AT_body_begin";
12717     case DW_AT_body_end:
12718       return "DW_AT_body_end";
12719     case DW_AT_GNU_vector:
12720       return "DW_AT_GNU_vector";
12721     case DW_AT_GNU_odr_signature:
12722       return "DW_AT_GNU_odr_signature";
12723     /* VMS extensions.  */
12724     case DW_AT_VMS_rtnbeg_pd_address:
12725       return "DW_AT_VMS_rtnbeg_pd_address";
12726     /* UPC extension.  */
12727     case DW_AT_upc_threads_scaled:
12728       return "DW_AT_upc_threads_scaled";
12729     /* PGI (STMicroelectronics) extensions.  */
12730     case DW_AT_PGI_lbase:
12731       return "DW_AT_PGI_lbase";
12732     case DW_AT_PGI_soffset:
12733       return "DW_AT_PGI_soffset";
12734     case DW_AT_PGI_lstride:
12735       return "DW_AT_PGI_lstride";
12736     default:
12737       return "DW_AT_<unknown>";
12738     }
12739 }
12740
12741 /* Convert a DWARF value form code into its string name.  */
12742
12743 static char *
12744 dwarf_form_name (unsigned form)
12745 {
12746   switch (form)
12747     {
12748     case DW_FORM_addr:
12749       return "DW_FORM_addr";
12750     case DW_FORM_block2:
12751       return "DW_FORM_block2";
12752     case DW_FORM_block4:
12753       return "DW_FORM_block4";
12754     case DW_FORM_data2:
12755       return "DW_FORM_data2";
12756     case DW_FORM_data4:
12757       return "DW_FORM_data4";
12758     case DW_FORM_data8:
12759       return "DW_FORM_data8";
12760     case DW_FORM_string:
12761       return "DW_FORM_string";
12762     case DW_FORM_block:
12763       return "DW_FORM_block";
12764     case DW_FORM_block1:
12765       return "DW_FORM_block1";
12766     case DW_FORM_data1:
12767       return "DW_FORM_data1";
12768     case DW_FORM_flag:
12769       return "DW_FORM_flag";
12770     case DW_FORM_sdata:
12771       return "DW_FORM_sdata";
12772     case DW_FORM_strp:
12773       return "DW_FORM_strp";
12774     case DW_FORM_udata:
12775       return "DW_FORM_udata";
12776     case DW_FORM_ref_addr:
12777       return "DW_FORM_ref_addr";
12778     case DW_FORM_ref1:
12779       return "DW_FORM_ref1";
12780     case DW_FORM_ref2:
12781       return "DW_FORM_ref2";
12782     case DW_FORM_ref4:
12783       return "DW_FORM_ref4";
12784     case DW_FORM_ref8:
12785       return "DW_FORM_ref8";
12786     case DW_FORM_ref_udata:
12787       return "DW_FORM_ref_udata";
12788     case DW_FORM_indirect:
12789       return "DW_FORM_indirect";
12790     case DW_FORM_sec_offset:
12791       return "DW_FORM_sec_offset";
12792     case DW_FORM_exprloc:
12793       return "DW_FORM_exprloc";
12794     case DW_FORM_flag_present:
12795       return "DW_FORM_flag_present";
12796     case DW_FORM_ref_sig8:
12797       return "DW_FORM_ref_sig8";
12798     default:
12799       return "DW_FORM_<unknown>";
12800     }
12801 }
12802
12803 /* Convert a DWARF stack opcode into its string name.  */
12804
12805 const char *
12806 dwarf_stack_op_name (unsigned op)
12807 {
12808   switch (op)
12809     {
12810     case DW_OP_addr:
12811       return "DW_OP_addr";
12812     case DW_OP_deref:
12813       return "DW_OP_deref";
12814     case DW_OP_const1u:
12815       return "DW_OP_const1u";
12816     case DW_OP_const1s:
12817       return "DW_OP_const1s";
12818     case DW_OP_const2u:
12819       return "DW_OP_const2u";
12820     case DW_OP_const2s:
12821       return "DW_OP_const2s";
12822     case DW_OP_const4u:
12823       return "DW_OP_const4u";
12824     case DW_OP_const4s:
12825       return "DW_OP_const4s";
12826     case DW_OP_const8u:
12827       return "DW_OP_const8u";
12828     case DW_OP_const8s:
12829       return "DW_OP_const8s";
12830     case DW_OP_constu:
12831       return "DW_OP_constu";
12832     case DW_OP_consts:
12833       return "DW_OP_consts";
12834     case DW_OP_dup:
12835       return "DW_OP_dup";
12836     case DW_OP_drop:
12837       return "DW_OP_drop";
12838     case DW_OP_over:
12839       return "DW_OP_over";
12840     case DW_OP_pick:
12841       return "DW_OP_pick";
12842     case DW_OP_swap:
12843       return "DW_OP_swap";
12844     case DW_OP_rot:
12845       return "DW_OP_rot";
12846     case DW_OP_xderef:
12847       return "DW_OP_xderef";
12848     case DW_OP_abs:
12849       return "DW_OP_abs";
12850     case DW_OP_and:
12851       return "DW_OP_and";
12852     case DW_OP_div:
12853       return "DW_OP_div";
12854     case DW_OP_minus:
12855       return "DW_OP_minus";
12856     case DW_OP_mod:
12857       return "DW_OP_mod";
12858     case DW_OP_mul:
12859       return "DW_OP_mul";
12860     case DW_OP_neg:
12861       return "DW_OP_neg";
12862     case DW_OP_not:
12863       return "DW_OP_not";
12864     case DW_OP_or:
12865       return "DW_OP_or";
12866     case DW_OP_plus:
12867       return "DW_OP_plus";
12868     case DW_OP_plus_uconst:
12869       return "DW_OP_plus_uconst";
12870     case DW_OP_shl:
12871       return "DW_OP_shl";
12872     case DW_OP_shr:
12873       return "DW_OP_shr";
12874     case DW_OP_shra:
12875       return "DW_OP_shra";
12876     case DW_OP_xor:
12877       return "DW_OP_xor";
12878     case DW_OP_bra:
12879       return "DW_OP_bra";
12880     case DW_OP_eq:
12881       return "DW_OP_eq";
12882     case DW_OP_ge:
12883       return "DW_OP_ge";
12884     case DW_OP_gt:
12885       return "DW_OP_gt";
12886     case DW_OP_le:
12887       return "DW_OP_le";
12888     case DW_OP_lt:
12889       return "DW_OP_lt";
12890     case DW_OP_ne:
12891       return "DW_OP_ne";
12892     case DW_OP_skip:
12893       return "DW_OP_skip";
12894     case DW_OP_lit0:
12895       return "DW_OP_lit0";
12896     case DW_OP_lit1:
12897       return "DW_OP_lit1";
12898     case DW_OP_lit2:
12899       return "DW_OP_lit2";
12900     case DW_OP_lit3:
12901       return "DW_OP_lit3";
12902     case DW_OP_lit4:
12903       return "DW_OP_lit4";
12904     case DW_OP_lit5:
12905       return "DW_OP_lit5";
12906     case DW_OP_lit6:
12907       return "DW_OP_lit6";
12908     case DW_OP_lit7:
12909       return "DW_OP_lit7";
12910     case DW_OP_lit8:
12911       return "DW_OP_lit8";
12912     case DW_OP_lit9:
12913       return "DW_OP_lit9";
12914     case DW_OP_lit10:
12915       return "DW_OP_lit10";
12916     case DW_OP_lit11:
12917       return "DW_OP_lit11";
12918     case DW_OP_lit12:
12919       return "DW_OP_lit12";
12920     case DW_OP_lit13:
12921       return "DW_OP_lit13";
12922     case DW_OP_lit14:
12923       return "DW_OP_lit14";
12924     case DW_OP_lit15:
12925       return "DW_OP_lit15";
12926     case DW_OP_lit16:
12927       return "DW_OP_lit16";
12928     case DW_OP_lit17:
12929       return "DW_OP_lit17";
12930     case DW_OP_lit18:
12931       return "DW_OP_lit18";
12932     case DW_OP_lit19:
12933       return "DW_OP_lit19";
12934     case DW_OP_lit20:
12935       return "DW_OP_lit20";
12936     case DW_OP_lit21:
12937       return "DW_OP_lit21";
12938     case DW_OP_lit22:
12939       return "DW_OP_lit22";
12940     case DW_OP_lit23:
12941       return "DW_OP_lit23";
12942     case DW_OP_lit24:
12943       return "DW_OP_lit24";
12944     case DW_OP_lit25:
12945       return "DW_OP_lit25";
12946     case DW_OP_lit26:
12947       return "DW_OP_lit26";
12948     case DW_OP_lit27:
12949       return "DW_OP_lit27";
12950     case DW_OP_lit28:
12951       return "DW_OP_lit28";
12952     case DW_OP_lit29:
12953       return "DW_OP_lit29";
12954     case DW_OP_lit30:
12955       return "DW_OP_lit30";
12956     case DW_OP_lit31:
12957       return "DW_OP_lit31";
12958     case DW_OP_reg0:
12959       return "DW_OP_reg0";
12960     case DW_OP_reg1:
12961       return "DW_OP_reg1";
12962     case DW_OP_reg2:
12963       return "DW_OP_reg2";
12964     case DW_OP_reg3:
12965       return "DW_OP_reg3";
12966     case DW_OP_reg4:
12967       return "DW_OP_reg4";
12968     case DW_OP_reg5:
12969       return "DW_OP_reg5";
12970     case DW_OP_reg6:
12971       return "DW_OP_reg6";
12972     case DW_OP_reg7:
12973       return "DW_OP_reg7";
12974     case DW_OP_reg8:
12975       return "DW_OP_reg8";
12976     case DW_OP_reg9:
12977       return "DW_OP_reg9";
12978     case DW_OP_reg10:
12979       return "DW_OP_reg10";
12980     case DW_OP_reg11:
12981       return "DW_OP_reg11";
12982     case DW_OP_reg12:
12983       return "DW_OP_reg12";
12984     case DW_OP_reg13:
12985       return "DW_OP_reg13";
12986     case DW_OP_reg14:
12987       return "DW_OP_reg14";
12988     case DW_OP_reg15:
12989       return "DW_OP_reg15";
12990     case DW_OP_reg16:
12991       return "DW_OP_reg16";
12992     case DW_OP_reg17:
12993       return "DW_OP_reg17";
12994     case DW_OP_reg18:
12995       return "DW_OP_reg18";
12996     case DW_OP_reg19:
12997       return "DW_OP_reg19";
12998     case DW_OP_reg20:
12999       return "DW_OP_reg20";
13000     case DW_OP_reg21:
13001       return "DW_OP_reg21";
13002     case DW_OP_reg22:
13003       return "DW_OP_reg22";
13004     case DW_OP_reg23:
13005       return "DW_OP_reg23";
13006     case DW_OP_reg24:
13007       return "DW_OP_reg24";
13008     case DW_OP_reg25:
13009       return "DW_OP_reg25";
13010     case DW_OP_reg26:
13011       return "DW_OP_reg26";
13012     case DW_OP_reg27:
13013       return "DW_OP_reg27";
13014     case DW_OP_reg28:
13015       return "DW_OP_reg28";
13016     case DW_OP_reg29:
13017       return "DW_OP_reg29";
13018     case DW_OP_reg30:
13019       return "DW_OP_reg30";
13020     case DW_OP_reg31:
13021       return "DW_OP_reg31";
13022     case DW_OP_breg0:
13023       return "DW_OP_breg0";
13024     case DW_OP_breg1:
13025       return "DW_OP_breg1";
13026     case DW_OP_breg2:
13027       return "DW_OP_breg2";
13028     case DW_OP_breg3:
13029       return "DW_OP_breg3";
13030     case DW_OP_breg4:
13031       return "DW_OP_breg4";
13032     case DW_OP_breg5:
13033       return "DW_OP_breg5";
13034     case DW_OP_breg6:
13035       return "DW_OP_breg6";
13036     case DW_OP_breg7:
13037       return "DW_OP_breg7";
13038     case DW_OP_breg8:
13039       return "DW_OP_breg8";
13040     case DW_OP_breg9:
13041       return "DW_OP_breg9";
13042     case DW_OP_breg10:
13043       return "DW_OP_breg10";
13044     case DW_OP_breg11:
13045       return "DW_OP_breg11";
13046     case DW_OP_breg12:
13047       return "DW_OP_breg12";
13048     case DW_OP_breg13:
13049       return "DW_OP_breg13";
13050     case DW_OP_breg14:
13051       return "DW_OP_breg14";
13052     case DW_OP_breg15:
13053       return "DW_OP_breg15";
13054     case DW_OP_breg16:
13055       return "DW_OP_breg16";
13056     case DW_OP_breg17:
13057       return "DW_OP_breg17";
13058     case DW_OP_breg18:
13059       return "DW_OP_breg18";
13060     case DW_OP_breg19:
13061       return "DW_OP_breg19";
13062     case DW_OP_breg20:
13063       return "DW_OP_breg20";
13064     case DW_OP_breg21:
13065       return "DW_OP_breg21";
13066     case DW_OP_breg22:
13067       return "DW_OP_breg22";
13068     case DW_OP_breg23:
13069       return "DW_OP_breg23";
13070     case DW_OP_breg24:
13071       return "DW_OP_breg24";
13072     case DW_OP_breg25:
13073       return "DW_OP_breg25";
13074     case DW_OP_breg26:
13075       return "DW_OP_breg26";
13076     case DW_OP_breg27:
13077       return "DW_OP_breg27";
13078     case DW_OP_breg28:
13079       return "DW_OP_breg28";
13080     case DW_OP_breg29:
13081       return "DW_OP_breg29";
13082     case DW_OP_breg30:
13083       return "DW_OP_breg30";
13084     case DW_OP_breg31:
13085       return "DW_OP_breg31";
13086     case DW_OP_regx:
13087       return "DW_OP_regx";
13088     case DW_OP_fbreg:
13089       return "DW_OP_fbreg";
13090     case DW_OP_bregx:
13091       return "DW_OP_bregx";
13092     case DW_OP_piece:
13093       return "DW_OP_piece";
13094     case DW_OP_deref_size:
13095       return "DW_OP_deref_size";
13096     case DW_OP_xderef_size:
13097       return "DW_OP_xderef_size";
13098     case DW_OP_nop:
13099       return "DW_OP_nop";
13100     /* DWARF 3 extensions.  */
13101     case DW_OP_push_object_address:
13102       return "DW_OP_push_object_address";
13103     case DW_OP_call2:
13104       return "DW_OP_call2";
13105     case DW_OP_call4:
13106       return "DW_OP_call4";
13107     case DW_OP_call_ref:
13108       return "DW_OP_call_ref";
13109     case DW_OP_form_tls_address:
13110       return "DW_OP_form_tls_address";
13111     case DW_OP_call_frame_cfa:
13112       return "DW_OP_call_frame_cfa";
13113     case DW_OP_bit_piece:
13114       return "DW_OP_bit_piece";
13115     /* DWARF 4 extensions.  */
13116     case DW_OP_implicit_value:
13117       return "DW_OP_implicit_value";
13118     case DW_OP_stack_value:
13119       return "DW_OP_stack_value";
13120     /* GNU extensions.  */
13121     case DW_OP_GNU_push_tls_address:
13122       return "DW_OP_GNU_push_tls_address";
13123     case DW_OP_GNU_uninit:
13124       return "DW_OP_GNU_uninit";
13125     case DW_OP_GNU_implicit_pointer:
13126       return "DW_OP_GNU_implicit_pointer";
13127     case DW_OP_GNU_entry_value:
13128       return "DW_OP_GNU_entry_value";
13129     case DW_OP_GNU_const_type:
13130       return "DW_OP_GNU_const_type";
13131     case DW_OP_GNU_regval_type:
13132       return "DW_OP_GNU_regval_type";
13133     case DW_OP_GNU_deref_type:
13134       return "DW_OP_GNU_deref_type";
13135     case DW_OP_GNU_convert:
13136       return "DW_OP_GNU_convert";
13137     case DW_OP_GNU_reinterpret:
13138       return "DW_OP_GNU_reinterpret";
13139     default:
13140       return NULL;
13141     }
13142 }
13143
13144 static char *
13145 dwarf_bool_name (unsigned mybool)
13146 {
13147   if (mybool)
13148     return "TRUE";
13149   else
13150     return "FALSE";
13151 }
13152
13153 /* Convert a DWARF type code into its string name.  */
13154
13155 static char *
13156 dwarf_type_encoding_name (unsigned enc)
13157 {
13158   switch (enc)
13159     {
13160     case DW_ATE_void:
13161       return "DW_ATE_void";
13162     case DW_ATE_address:
13163       return "DW_ATE_address";
13164     case DW_ATE_boolean:
13165       return "DW_ATE_boolean";
13166     case DW_ATE_complex_float:
13167       return "DW_ATE_complex_float";
13168     case DW_ATE_float:
13169       return "DW_ATE_float";
13170     case DW_ATE_signed:
13171       return "DW_ATE_signed";
13172     case DW_ATE_signed_char:
13173       return "DW_ATE_signed_char";
13174     case DW_ATE_unsigned:
13175       return "DW_ATE_unsigned";
13176     case DW_ATE_unsigned_char:
13177       return "DW_ATE_unsigned_char";
13178     /* DWARF 3.  */
13179     case DW_ATE_imaginary_float:
13180       return "DW_ATE_imaginary_float";
13181     case DW_ATE_packed_decimal:
13182       return "DW_ATE_packed_decimal";
13183     case DW_ATE_numeric_string:
13184       return "DW_ATE_numeric_string";
13185     case DW_ATE_edited:
13186       return "DW_ATE_edited";
13187     case DW_ATE_signed_fixed:
13188       return "DW_ATE_signed_fixed";
13189     case DW_ATE_unsigned_fixed:
13190       return "DW_ATE_unsigned_fixed";
13191     case DW_ATE_decimal_float:
13192       return "DW_ATE_decimal_float";
13193     /* DWARF 4.  */
13194     case DW_ATE_UTF:
13195       return "DW_ATE_UTF";
13196     /* HP extensions.  */
13197     case DW_ATE_HP_float80:
13198       return "DW_ATE_HP_float80";
13199     case DW_ATE_HP_complex_float80:
13200       return "DW_ATE_HP_complex_float80";
13201     case DW_ATE_HP_float128:
13202       return "DW_ATE_HP_float128";
13203     case DW_ATE_HP_complex_float128:
13204       return "DW_ATE_HP_complex_float128";
13205     case DW_ATE_HP_floathpintel:
13206       return "DW_ATE_HP_floathpintel";
13207     case DW_ATE_HP_imaginary_float80:
13208       return "DW_ATE_HP_imaginary_float80";
13209     case DW_ATE_HP_imaginary_float128:
13210       return "DW_ATE_HP_imaginary_float128";
13211     default:
13212       return "DW_ATE_<unknown>";
13213     }
13214 }
13215
13216 /* Convert a DWARF call frame info operation to its string name.  */
13217
13218 #if 0
13219 static char *
13220 dwarf_cfi_name (unsigned cfi_opc)
13221 {
13222   switch (cfi_opc)
13223     {
13224     case DW_CFA_advance_loc:
13225       return "DW_CFA_advance_loc";
13226     case DW_CFA_offset:
13227       return "DW_CFA_offset";
13228     case DW_CFA_restore:
13229       return "DW_CFA_restore";
13230     case DW_CFA_nop:
13231       return "DW_CFA_nop";
13232     case DW_CFA_set_loc:
13233       return "DW_CFA_set_loc";
13234     case DW_CFA_advance_loc1:
13235       return "DW_CFA_advance_loc1";
13236     case DW_CFA_advance_loc2:
13237       return "DW_CFA_advance_loc2";
13238     case DW_CFA_advance_loc4:
13239       return "DW_CFA_advance_loc4";
13240     case DW_CFA_offset_extended:
13241       return "DW_CFA_offset_extended";
13242     case DW_CFA_restore_extended:
13243       return "DW_CFA_restore_extended";
13244     case DW_CFA_undefined:
13245       return "DW_CFA_undefined";
13246     case DW_CFA_same_value:
13247       return "DW_CFA_same_value";
13248     case DW_CFA_register:
13249       return "DW_CFA_register";
13250     case DW_CFA_remember_state:
13251       return "DW_CFA_remember_state";
13252     case DW_CFA_restore_state:
13253       return "DW_CFA_restore_state";
13254     case DW_CFA_def_cfa:
13255       return "DW_CFA_def_cfa";
13256     case DW_CFA_def_cfa_register:
13257       return "DW_CFA_def_cfa_register";
13258     case DW_CFA_def_cfa_offset:
13259       return "DW_CFA_def_cfa_offset";
13260     /* DWARF 3.  */
13261     case DW_CFA_def_cfa_expression:
13262       return "DW_CFA_def_cfa_expression";
13263     case DW_CFA_expression:
13264       return "DW_CFA_expression";
13265     case DW_CFA_offset_extended_sf:
13266       return "DW_CFA_offset_extended_sf";
13267     case DW_CFA_def_cfa_sf:
13268       return "DW_CFA_def_cfa_sf";
13269     case DW_CFA_def_cfa_offset_sf:
13270       return "DW_CFA_def_cfa_offset_sf";
13271     case DW_CFA_val_offset:
13272       return "DW_CFA_val_offset";
13273     case DW_CFA_val_offset_sf:
13274       return "DW_CFA_val_offset_sf";
13275     case DW_CFA_val_expression:
13276       return "DW_CFA_val_expression";
13277     /* SGI/MIPS specific.  */
13278     case DW_CFA_MIPS_advance_loc8:
13279       return "DW_CFA_MIPS_advance_loc8";
13280     /* GNU extensions.  */
13281     case DW_CFA_GNU_window_save:
13282       return "DW_CFA_GNU_window_save";
13283     case DW_CFA_GNU_args_size:
13284       return "DW_CFA_GNU_args_size";
13285     case DW_CFA_GNU_negative_offset_extended:
13286       return "DW_CFA_GNU_negative_offset_extended";
13287     default:
13288       return "DW_CFA_<unknown>";
13289     }
13290 }
13291 #endif
13292
13293 static void
13294 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13295 {
13296   unsigned int i;
13297
13298   print_spaces (indent, f);
13299   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13300            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13301
13302   if (die->parent != NULL)
13303     {
13304       print_spaces (indent, f);
13305       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13306                           die->parent->offset);
13307     }
13308
13309   print_spaces (indent, f);
13310   fprintf_unfiltered (f, "  has children: %s\n",
13311            dwarf_bool_name (die->child != NULL));
13312
13313   print_spaces (indent, f);
13314   fprintf_unfiltered (f, "  attributes:\n");
13315
13316   for (i = 0; i < die->num_attrs; ++i)
13317     {
13318       print_spaces (indent, f);
13319       fprintf_unfiltered (f, "    %s (%s) ",
13320                dwarf_attr_name (die->attrs[i].name),
13321                dwarf_form_name (die->attrs[i].form));
13322
13323       switch (die->attrs[i].form)
13324         {
13325         case DW_FORM_ref_addr:
13326         case DW_FORM_addr:
13327           fprintf_unfiltered (f, "address: ");
13328           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13329           break;
13330         case DW_FORM_block2:
13331         case DW_FORM_block4:
13332         case DW_FORM_block:
13333         case DW_FORM_block1:
13334           fprintf_unfiltered (f, "block: size %d",
13335                               DW_BLOCK (&die->attrs[i])->size);
13336           break;
13337         case DW_FORM_exprloc:
13338           fprintf_unfiltered (f, "expression: size %u",
13339                               DW_BLOCK (&die->attrs[i])->size);
13340           break;
13341         case DW_FORM_ref1:
13342         case DW_FORM_ref2:
13343         case DW_FORM_ref4:
13344           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13345                               (long) (DW_ADDR (&die->attrs[i])));
13346           break;
13347         case DW_FORM_data1:
13348         case DW_FORM_data2:
13349         case DW_FORM_data4:
13350         case DW_FORM_data8:
13351         case DW_FORM_udata:
13352         case DW_FORM_sdata:
13353           fprintf_unfiltered (f, "constant: %s",
13354                               pulongest (DW_UNSND (&die->attrs[i])));
13355           break;
13356         case DW_FORM_sec_offset:
13357           fprintf_unfiltered (f, "section offset: %s",
13358                               pulongest (DW_UNSND (&die->attrs[i])));
13359           break;
13360         case DW_FORM_ref_sig8:
13361           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13362             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13363                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13364           else
13365             fprintf_unfiltered (f, "signatured type, offset: unknown");
13366           break;
13367         case DW_FORM_string:
13368         case DW_FORM_strp:
13369           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13370                    DW_STRING (&die->attrs[i])
13371                    ? DW_STRING (&die->attrs[i]) : "",
13372                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13373           break;
13374         case DW_FORM_flag:
13375           if (DW_UNSND (&die->attrs[i]))
13376             fprintf_unfiltered (f, "flag: TRUE");
13377           else
13378             fprintf_unfiltered (f, "flag: FALSE");
13379           break;
13380         case DW_FORM_flag_present:
13381           fprintf_unfiltered (f, "flag: TRUE");
13382           break;
13383         case DW_FORM_indirect:
13384           /* The reader will have reduced the indirect form to
13385              the "base form" so this form should not occur.  */
13386           fprintf_unfiltered (f, 
13387                               "unexpected attribute form: DW_FORM_indirect");
13388           break;
13389         default:
13390           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13391                    die->attrs[i].form);
13392           break;
13393         }
13394       fprintf_unfiltered (f, "\n");
13395     }
13396 }
13397
13398 static void
13399 dump_die_for_error (struct die_info *die)
13400 {
13401   dump_die_shallow (gdb_stderr, 0, die);
13402 }
13403
13404 static void
13405 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13406 {
13407   int indent = level * 4;
13408
13409   gdb_assert (die != NULL);
13410
13411   if (level >= max_level)
13412     return;
13413
13414   dump_die_shallow (f, indent, die);
13415
13416   if (die->child != NULL)
13417     {
13418       print_spaces (indent, f);
13419       fprintf_unfiltered (f, "  Children:");
13420       if (level + 1 < max_level)
13421         {
13422           fprintf_unfiltered (f, "\n");
13423           dump_die_1 (f, level + 1, max_level, die->child);
13424         }
13425       else
13426         {
13427           fprintf_unfiltered (f,
13428                               " [not printed, max nesting level reached]\n");
13429         }
13430     }
13431
13432   if (die->sibling != NULL && level > 0)
13433     {
13434       dump_die_1 (f, level, max_level, die->sibling);
13435     }
13436 }
13437
13438 /* This is called from the pdie macro in gdbinit.in.
13439    It's not static so gcc will keep a copy callable from gdb.  */
13440
13441 void
13442 dump_die (struct die_info *die, int max_level)
13443 {
13444   dump_die_1 (gdb_stdlog, 0, max_level, die);
13445 }
13446
13447 static void
13448 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13449 {
13450   void **slot;
13451
13452   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13453
13454   *slot = die;
13455 }
13456
13457 static int
13458 is_ref_attr (struct attribute *attr)
13459 {
13460   switch (attr->form)
13461     {
13462     case DW_FORM_ref_addr:
13463     case DW_FORM_ref1:
13464     case DW_FORM_ref2:
13465     case DW_FORM_ref4:
13466     case DW_FORM_ref8:
13467     case DW_FORM_ref_udata:
13468       return 1;
13469     default:
13470       return 0;
13471     }
13472 }
13473
13474 static unsigned int
13475 dwarf2_get_ref_die_offset (struct attribute *attr)
13476 {
13477   if (is_ref_attr (attr))
13478     return DW_ADDR (attr);
13479
13480   complaint (&symfile_complaints,
13481              _("unsupported die ref attribute form: '%s'"),
13482              dwarf_form_name (attr->form));
13483   return 0;
13484 }
13485
13486 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13487  * the value held by the attribute is not constant.  */
13488
13489 static LONGEST
13490 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13491 {
13492   if (attr->form == DW_FORM_sdata)
13493     return DW_SND (attr);
13494   else if (attr->form == DW_FORM_udata
13495            || attr->form == DW_FORM_data1
13496            || attr->form == DW_FORM_data2
13497            || attr->form == DW_FORM_data4
13498            || attr->form == DW_FORM_data8)
13499     return DW_UNSND (attr);
13500   else
13501     {
13502       complaint (&symfile_complaints,
13503                  _("Attribute value is not a constant (%s)"),
13504                  dwarf_form_name (attr->form));
13505       return default_value;
13506     }
13507 }
13508
13509 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13510    unit and add it to our queue.
13511    The result is non-zero if PER_CU was queued, otherwise the result is zero
13512    meaning either PER_CU is already queued or it is already loaded.  */
13513
13514 static int
13515 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13516                        struct dwarf2_per_cu_data *per_cu)
13517 {
13518   /* We may arrive here during partial symbol reading, if we need full
13519      DIEs to process an unusual case (e.g. template arguments).  Do
13520      not queue PER_CU, just tell our caller to load its DIEs.  */
13521   if (dwarf2_per_objfile->reading_partial_symbols)
13522     {
13523       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13524         return 1;
13525       return 0;
13526     }
13527
13528   /* Mark the dependence relation so that we don't flush PER_CU
13529      too early.  */
13530   dwarf2_add_dependence (this_cu, per_cu);
13531
13532   /* If it's already on the queue, we have nothing to do.  */
13533   if (per_cu->queued)
13534     return 0;
13535
13536   /* If the compilation unit is already loaded, just mark it as
13537      used.  */
13538   if (per_cu->cu != NULL)
13539     {
13540       per_cu->cu->last_used = 0;
13541       return 0;
13542     }
13543
13544   /* Add it to the queue.  */
13545   queue_comp_unit (per_cu, this_cu->objfile);
13546
13547   return 1;
13548 }
13549
13550 /* Follow reference or signature attribute ATTR of SRC_DIE.
13551    On entry *REF_CU is the CU of SRC_DIE.
13552    On exit *REF_CU is the CU of the result.  */
13553
13554 static struct die_info *
13555 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13556                        struct dwarf2_cu **ref_cu)
13557 {
13558   struct die_info *die;
13559
13560   if (is_ref_attr (attr))
13561     die = follow_die_ref (src_die, attr, ref_cu);
13562   else if (attr->form == DW_FORM_ref_sig8)
13563     die = follow_die_sig (src_die, attr, ref_cu);
13564   else
13565     {
13566       dump_die_for_error (src_die);
13567       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13568              (*ref_cu)->objfile->name);
13569     }
13570
13571   return die;
13572 }
13573
13574 /* Follow reference OFFSET.
13575    On entry *REF_CU is the CU of the source die referencing OFFSET.
13576    On exit *REF_CU is the CU of the result.
13577    Returns NULL if OFFSET is invalid.  */
13578
13579 static struct die_info *
13580 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13581 {
13582   struct die_info temp_die;
13583   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13584
13585   gdb_assert (cu->per_cu != NULL);
13586
13587   target_cu = cu;
13588
13589   if (cu->per_cu->from_debug_types)
13590     {
13591       /* .debug_types CUs cannot reference anything outside their CU.
13592          If they need to, they have to reference a signatured type via
13593          DW_FORM_ref_sig8.  */
13594       if (! offset_in_cu_p (&cu->header, offset))
13595         return NULL;
13596     }
13597   else if (! offset_in_cu_p (&cu->header, offset))
13598     {
13599       struct dwarf2_per_cu_data *per_cu;
13600
13601       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13602
13603       /* If necessary, add it to the queue and load its DIEs.  */
13604       if (maybe_queue_comp_unit (cu, per_cu))
13605         load_full_comp_unit (per_cu, cu->objfile);
13606
13607       target_cu = per_cu->cu;
13608     }
13609   else if (cu->dies == NULL)
13610     {
13611       /* We're loading full DIEs during partial symbol reading.  */
13612       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13613       load_full_comp_unit (cu->per_cu, cu->objfile);
13614     }
13615
13616   *ref_cu = target_cu;
13617   temp_die.offset = offset;
13618   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13619 }
13620
13621 /* Follow reference attribute ATTR of SRC_DIE.
13622    On entry *REF_CU is the CU of SRC_DIE.
13623    On exit *REF_CU is the CU of the result.  */
13624
13625 static struct die_info *
13626 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13627                 struct dwarf2_cu **ref_cu)
13628 {
13629   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13630   struct dwarf2_cu *cu = *ref_cu;
13631   struct die_info *die;
13632
13633   die = follow_die_offset (offset, ref_cu);
13634   if (!die)
13635     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13636            "at 0x%x [in module %s]"),
13637            offset, src_die->offset, cu->objfile->name);
13638
13639   return die;
13640 }
13641
13642 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13643    value is intended for DW_OP_call*.  */
13644
13645 struct dwarf2_locexpr_baton
13646 dwarf2_fetch_die_location_block (unsigned int offset,
13647                                  struct dwarf2_per_cu_data *per_cu,
13648                                  CORE_ADDR (*get_frame_pc) (void *baton),
13649                                  void *baton)
13650 {
13651   struct dwarf2_cu *cu = per_cu->cu;
13652   struct die_info *die;
13653   struct attribute *attr;
13654   struct dwarf2_locexpr_baton retval;
13655
13656   dw2_setup (per_cu->objfile);
13657
13658   die = follow_die_offset (offset, &cu);
13659   if (!die)
13660     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13661            offset, per_cu->cu->objfile->name);
13662
13663   attr = dwarf2_attr (die, DW_AT_location, cu);
13664   if (!attr)
13665     {
13666       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13667
13668       retval.data = NULL;
13669       retval.size = 0;
13670     }
13671   else if (attr_form_is_section_offset (attr))
13672     {
13673       struct dwarf2_loclist_baton loclist_baton;
13674       CORE_ADDR pc = (*get_frame_pc) (baton);
13675       size_t size;
13676
13677       fill_in_loclist_baton (cu, &loclist_baton, attr);
13678
13679       retval.data = dwarf2_find_location_expression (&loclist_baton,
13680                                                      &size, pc);
13681       retval.size = size;
13682     }
13683   else
13684     {
13685       if (!attr_form_is_block (attr))
13686         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13687                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13688                offset, per_cu->cu->objfile->name);
13689
13690       retval.data = DW_BLOCK (attr)->data;
13691       retval.size = DW_BLOCK (attr)->size;
13692     }
13693   retval.per_cu = cu->per_cu;
13694   return retval;
13695 }
13696
13697 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13698    PER_CU.  */
13699
13700 struct type *
13701 dwarf2_get_die_type (unsigned int die_offset,
13702                      struct dwarf2_per_cu_data *per_cu)
13703 {
13704   dw2_setup (per_cu->objfile);
13705   return get_die_type_at_offset (die_offset, per_cu);
13706 }
13707
13708 /* Follow the signature attribute ATTR in SRC_DIE.
13709    On entry *REF_CU is the CU of SRC_DIE.
13710    On exit *REF_CU is the CU of the result.  */
13711
13712 static struct die_info *
13713 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13714                 struct dwarf2_cu **ref_cu)
13715 {
13716   struct objfile *objfile = (*ref_cu)->objfile;
13717   struct die_info temp_die;
13718   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13719   struct dwarf2_cu *sig_cu;
13720   struct die_info *die;
13721
13722   /* sig_type will be NULL if the signatured type is missing from
13723      the debug info.  */
13724   if (sig_type == NULL)
13725     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13726              "at 0x%x [in module %s]"),
13727            src_die->offset, objfile->name);
13728
13729   /* If necessary, add it to the queue and load its DIEs.  */
13730
13731   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13732     read_signatured_type (objfile, sig_type);
13733
13734   gdb_assert (sig_type->per_cu.cu != NULL);
13735
13736   sig_cu = sig_type->per_cu.cu;
13737   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13738   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13739   if (die)
13740     {
13741       *ref_cu = sig_cu;
13742       return die;
13743     }
13744
13745   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13746          "from DIE at 0x%x [in module %s]"),
13747          sig_type->type_offset, src_die->offset, objfile->name);
13748 }
13749
13750 /* Given an offset of a signatured type, return its signatured_type.  */
13751
13752 static struct signatured_type *
13753 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13754 {
13755   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13756   unsigned int length, initial_length_size;
13757   unsigned int sig_offset;
13758   struct signatured_type find_entry, *type_sig;
13759
13760   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13761   sig_offset = (initial_length_size
13762                 + 2 /*version*/
13763                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13764                 + 1 /*address_size*/);
13765   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13766   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13767
13768   /* This is only used to lookup previously recorded types.
13769      If we didn't find it, it's our bug.  */
13770   gdb_assert (type_sig != NULL);
13771   gdb_assert (offset == type_sig->per_cu.offset);
13772
13773   return type_sig;
13774 }
13775
13776 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13777
13778 static void
13779 read_signatured_type_at_offset (struct objfile *objfile,
13780                                 unsigned int offset)
13781 {
13782   struct signatured_type *type_sig;
13783
13784   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13785
13786   /* We have the section offset, but we need the signature to do the
13787      hash table lookup.  */
13788   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13789
13790   gdb_assert (type_sig->per_cu.cu == NULL);
13791
13792   read_signatured_type (objfile, type_sig);
13793
13794   gdb_assert (type_sig->per_cu.cu != NULL);
13795 }
13796
13797 /* Read in a signatured type and build its CU and DIEs.  */
13798
13799 static void
13800 read_signatured_type (struct objfile *objfile,
13801                       struct signatured_type *type_sig)
13802 {
13803   gdb_byte *types_ptr;
13804   struct die_reader_specs reader_specs;
13805   struct dwarf2_cu *cu;
13806   ULONGEST signature;
13807   struct cleanup *back_to, *free_cu_cleanup;
13808
13809   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13810   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
13811
13812   gdb_assert (type_sig->per_cu.cu == NULL);
13813
13814   cu = xmalloc (sizeof (*cu));
13815   init_one_comp_unit (cu, objfile);
13816
13817   type_sig->per_cu.cu = cu;
13818   cu->per_cu = &type_sig->per_cu;
13819
13820   /* If an error occurs while loading, release our storage.  */
13821   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13822
13823   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13824                                         types_ptr, objfile->obfd);
13825   gdb_assert (signature == type_sig->signature);
13826
13827   cu->die_hash
13828     = htab_create_alloc_ex (cu->header.length / 12,
13829                             die_hash,
13830                             die_eq,
13831                             NULL,
13832                             &cu->comp_unit_obstack,
13833                             hashtab_obstack_allocate,
13834                             dummy_obstack_deallocate);
13835
13836   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13837   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13838
13839   init_cu_die_reader (&reader_specs, cu);
13840
13841   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13842                                     NULL /*parent*/);
13843
13844   /* We try not to read any attributes in this function, because not
13845      all objfiles needed for references have been loaded yet, and symbol
13846      table processing isn't initialized.  But we have to set the CU language,
13847      or we won't be able to build types correctly.  */
13848   prepare_one_comp_unit (cu, cu->dies);
13849
13850   do_cleanups (back_to);
13851
13852   /* We've successfully allocated this compilation unit.  Let our caller
13853      clean it up when finished with it.  */
13854   discard_cleanups (free_cu_cleanup);
13855
13856   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13857   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13858 }
13859
13860 /* Decode simple location descriptions.
13861    Given a pointer to a dwarf block that defines a location, compute
13862    the location and return the value.
13863
13864    NOTE drow/2003-11-18: This function is called in two situations
13865    now: for the address of static or global variables (partial symbols
13866    only) and for offsets into structures which are expected to be
13867    (more or less) constant.  The partial symbol case should go away,
13868    and only the constant case should remain.  That will let this
13869    function complain more accurately.  A few special modes are allowed
13870    without complaint for global variables (for instance, global
13871    register values and thread-local values).
13872
13873    A location description containing no operations indicates that the
13874    object is optimized out.  The return value is 0 for that case.
13875    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13876    callers will only want a very basic result and this can become a
13877    complaint.
13878
13879    Note that stack[0] is unused except as a default error return.  */
13880
13881 static CORE_ADDR
13882 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13883 {
13884   struct objfile *objfile = cu->objfile;
13885   int i;
13886   int size = blk->size;
13887   gdb_byte *data = blk->data;
13888   CORE_ADDR stack[64];
13889   int stacki;
13890   unsigned int bytes_read, unsnd;
13891   gdb_byte op;
13892
13893   i = 0;
13894   stacki = 0;
13895   stack[stacki] = 0;
13896   stack[++stacki] = 0;
13897
13898   while (i < size)
13899     {
13900       op = data[i++];
13901       switch (op)
13902         {
13903         case DW_OP_lit0:
13904         case DW_OP_lit1:
13905         case DW_OP_lit2:
13906         case DW_OP_lit3:
13907         case DW_OP_lit4:
13908         case DW_OP_lit5:
13909         case DW_OP_lit6:
13910         case DW_OP_lit7:
13911         case DW_OP_lit8:
13912         case DW_OP_lit9:
13913         case DW_OP_lit10:
13914         case DW_OP_lit11:
13915         case DW_OP_lit12:
13916         case DW_OP_lit13:
13917         case DW_OP_lit14:
13918         case DW_OP_lit15:
13919         case DW_OP_lit16:
13920         case DW_OP_lit17:
13921         case DW_OP_lit18:
13922         case DW_OP_lit19:
13923         case DW_OP_lit20:
13924         case DW_OP_lit21:
13925         case DW_OP_lit22:
13926         case DW_OP_lit23:
13927         case DW_OP_lit24:
13928         case DW_OP_lit25:
13929         case DW_OP_lit26:
13930         case DW_OP_lit27:
13931         case DW_OP_lit28:
13932         case DW_OP_lit29:
13933         case DW_OP_lit30:
13934         case DW_OP_lit31:
13935           stack[++stacki] = op - DW_OP_lit0;
13936           break;
13937
13938         case DW_OP_reg0:
13939         case DW_OP_reg1:
13940         case DW_OP_reg2:
13941         case DW_OP_reg3:
13942         case DW_OP_reg4:
13943         case DW_OP_reg5:
13944         case DW_OP_reg6:
13945         case DW_OP_reg7:
13946         case DW_OP_reg8:
13947         case DW_OP_reg9:
13948         case DW_OP_reg10:
13949         case DW_OP_reg11:
13950         case DW_OP_reg12:
13951         case DW_OP_reg13:
13952         case DW_OP_reg14:
13953         case DW_OP_reg15:
13954         case DW_OP_reg16:
13955         case DW_OP_reg17:
13956         case DW_OP_reg18:
13957         case DW_OP_reg19:
13958         case DW_OP_reg20:
13959         case DW_OP_reg21:
13960         case DW_OP_reg22:
13961         case DW_OP_reg23:
13962         case DW_OP_reg24:
13963         case DW_OP_reg25:
13964         case DW_OP_reg26:
13965         case DW_OP_reg27:
13966         case DW_OP_reg28:
13967         case DW_OP_reg29:
13968         case DW_OP_reg30:
13969         case DW_OP_reg31:
13970           stack[++stacki] = op - DW_OP_reg0;
13971           if (i < size)
13972             dwarf2_complex_location_expr_complaint ();
13973           break;
13974
13975         case DW_OP_regx:
13976           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13977           i += bytes_read;
13978           stack[++stacki] = unsnd;
13979           if (i < size)
13980             dwarf2_complex_location_expr_complaint ();
13981           break;
13982
13983         case DW_OP_addr:
13984           stack[++stacki] = read_address (objfile->obfd, &data[i],
13985                                           cu, &bytes_read);
13986           i += bytes_read;
13987           break;
13988
13989         case DW_OP_const1u:
13990           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13991           i += 1;
13992           break;
13993
13994         case DW_OP_const1s:
13995           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13996           i += 1;
13997           break;
13998
13999         case DW_OP_const2u:
14000           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14001           i += 2;
14002           break;
14003
14004         case DW_OP_const2s:
14005           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14006           i += 2;
14007           break;
14008
14009         case DW_OP_const4u:
14010           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14011           i += 4;
14012           break;
14013
14014         case DW_OP_const4s:
14015           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14016           i += 4;
14017           break;
14018
14019         case DW_OP_constu:
14020           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14021                                                   &bytes_read);
14022           i += bytes_read;
14023           break;
14024
14025         case DW_OP_consts:
14026           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14027           i += bytes_read;
14028           break;
14029
14030         case DW_OP_dup:
14031           stack[stacki + 1] = stack[stacki];
14032           stacki++;
14033           break;
14034
14035         case DW_OP_plus:
14036           stack[stacki - 1] += stack[stacki];
14037           stacki--;
14038           break;
14039
14040         case DW_OP_plus_uconst:
14041           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14042                                                  &bytes_read);
14043           i += bytes_read;
14044           break;
14045
14046         case DW_OP_minus:
14047           stack[stacki - 1] -= stack[stacki];
14048           stacki--;
14049           break;
14050
14051         case DW_OP_deref:
14052           /* If we're not the last op, then we definitely can't encode
14053              this using GDB's address_class enum.  This is valid for partial
14054              global symbols, although the variable's address will be bogus
14055              in the psymtab.  */
14056           if (i < size)
14057             dwarf2_complex_location_expr_complaint ();
14058           break;
14059
14060         case DW_OP_GNU_push_tls_address:
14061           /* The top of the stack has the offset from the beginning
14062              of the thread control block at which the variable is located.  */
14063           /* Nothing should follow this operator, so the top of stack would
14064              be returned.  */
14065           /* This is valid for partial global symbols, but the variable's
14066              address will be bogus in the psymtab.  */
14067           if (i < size)
14068             dwarf2_complex_location_expr_complaint ();
14069           break;
14070
14071         case DW_OP_GNU_uninit:
14072           break;
14073
14074         default:
14075           {
14076             const char *name = dwarf_stack_op_name (op);
14077
14078             if (name)
14079               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14080                          name);
14081             else
14082               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14083                          op);
14084           }
14085
14086           return (stack[stacki]);
14087         }
14088
14089       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14090          outside of the allocated space.  Also enforce minimum>0.  */
14091       if (stacki >= ARRAY_SIZE (stack) - 1)
14092         {
14093           complaint (&symfile_complaints,
14094                      _("location description stack overflow"));
14095           return 0;
14096         }
14097
14098       if (stacki <= 0)
14099         {
14100           complaint (&symfile_complaints,
14101                      _("location description stack underflow"));
14102           return 0;
14103         }
14104     }
14105   return (stack[stacki]);
14106 }
14107
14108 /* memory allocation interface */
14109
14110 static struct dwarf_block *
14111 dwarf_alloc_block (struct dwarf2_cu *cu)
14112 {
14113   struct dwarf_block *blk;
14114
14115   blk = (struct dwarf_block *)
14116     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14117   return (blk);
14118 }
14119
14120 static struct abbrev_info *
14121 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14122 {
14123   struct abbrev_info *abbrev;
14124
14125   abbrev = (struct abbrev_info *)
14126     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14127   memset (abbrev, 0, sizeof (struct abbrev_info));
14128   return (abbrev);
14129 }
14130
14131 static struct die_info *
14132 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14133 {
14134   struct die_info *die;
14135   size_t size = sizeof (struct die_info);
14136
14137   if (num_attrs > 1)
14138     size += (num_attrs - 1) * sizeof (struct attribute);
14139
14140   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14141   memset (die, 0, sizeof (struct die_info));
14142   return (die);
14143 }
14144
14145 \f
14146 /* Macro support.  */
14147
14148 /* Return the full name of file number I in *LH's file name table.
14149    Use COMP_DIR as the name of the current directory of the
14150    compilation.  The result is allocated using xmalloc; the caller is
14151    responsible for freeing it.  */
14152 static char *
14153 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14154 {
14155   /* Is the file number a valid index into the line header's file name
14156      table?  Remember that file numbers start with one, not zero.  */
14157   if (1 <= file && file <= lh->num_file_names)
14158     {
14159       struct file_entry *fe = &lh->file_names[file - 1];
14160
14161       if (IS_ABSOLUTE_PATH (fe->name))
14162         return xstrdup (fe->name);
14163       else
14164         {
14165           const char *dir;
14166           int dir_len;
14167           char *full_name;
14168
14169           if (fe->dir_index)
14170             dir = lh->include_dirs[fe->dir_index - 1];
14171           else
14172             dir = comp_dir;
14173
14174           if (dir)
14175             {
14176               dir_len = strlen (dir);
14177               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14178               strcpy (full_name, dir);
14179               full_name[dir_len] = '/';
14180               strcpy (full_name + dir_len + 1, fe->name);
14181               return full_name;
14182             }
14183           else
14184             return xstrdup (fe->name);
14185         }
14186     }
14187   else
14188     {
14189       /* The compiler produced a bogus file number.  We can at least
14190          record the macro definitions made in the file, even if we
14191          won't be able to find the file by name.  */
14192       char fake_name[80];
14193
14194       sprintf (fake_name, "<bad macro file number %d>", file);
14195
14196       complaint (&symfile_complaints,
14197                  _("bad file number in macro information (%d)"),
14198                  file);
14199
14200       return xstrdup (fake_name);
14201     }
14202 }
14203
14204
14205 static struct macro_source_file *
14206 macro_start_file (int file, int line,
14207                   struct macro_source_file *current_file,
14208                   const char *comp_dir,
14209                   struct line_header *lh, struct objfile *objfile)
14210 {
14211   /* The full name of this source file.  */
14212   char *full_name = file_full_name (file, lh, comp_dir);
14213
14214   /* We don't create a macro table for this compilation unit
14215      at all until we actually get a filename.  */
14216   if (! pending_macros)
14217     pending_macros = new_macro_table (&objfile->objfile_obstack,
14218                                       objfile->macro_cache);
14219
14220   if (! current_file)
14221     /* If we have no current file, then this must be the start_file
14222        directive for the compilation unit's main source file.  */
14223     current_file = macro_set_main (pending_macros, full_name);
14224   else
14225     current_file = macro_include (current_file, line, full_name);
14226
14227   xfree (full_name);
14228
14229   return current_file;
14230 }
14231
14232
14233 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14234    followed by a null byte.  */
14235 static char *
14236 copy_string (const char *buf, int len)
14237 {
14238   char *s = xmalloc (len + 1);
14239
14240   memcpy (s, buf, len);
14241   s[len] = '\0';
14242   return s;
14243 }
14244
14245
14246 static const char *
14247 consume_improper_spaces (const char *p, const char *body)
14248 {
14249   if (*p == ' ')
14250     {
14251       complaint (&symfile_complaints,
14252                  _("macro definition contains spaces "
14253                    "in formal argument list:\n`%s'"),
14254                  body);
14255
14256       while (*p == ' ')
14257         p++;
14258     }
14259
14260   return p;
14261 }
14262
14263
14264 static void
14265 parse_macro_definition (struct macro_source_file *file, int line,
14266                         const char *body)
14267 {
14268   const char *p;
14269
14270   /* The body string takes one of two forms.  For object-like macro
14271      definitions, it should be:
14272
14273         <macro name> " " <definition>
14274
14275      For function-like macro definitions, it should be:
14276
14277         <macro name> "() " <definition>
14278      or
14279         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14280
14281      Spaces may appear only where explicitly indicated, and in the
14282      <definition>.
14283
14284      The Dwarf 2 spec says that an object-like macro's name is always
14285      followed by a space, but versions of GCC around March 2002 omit
14286      the space when the macro's definition is the empty string.
14287
14288      The Dwarf 2 spec says that there should be no spaces between the
14289      formal arguments in a function-like macro's formal argument list,
14290      but versions of GCC around March 2002 include spaces after the
14291      commas.  */
14292
14293
14294   /* Find the extent of the macro name.  The macro name is terminated
14295      by either a space or null character (for an object-like macro) or
14296      an opening paren (for a function-like macro).  */
14297   for (p = body; *p; p++)
14298     if (*p == ' ' || *p == '(')
14299       break;
14300
14301   if (*p == ' ' || *p == '\0')
14302     {
14303       /* It's an object-like macro.  */
14304       int name_len = p - body;
14305       char *name = copy_string (body, name_len);
14306       const char *replacement;
14307
14308       if (*p == ' ')
14309         replacement = body + name_len + 1;
14310       else
14311         {
14312           dwarf2_macro_malformed_definition_complaint (body);
14313           replacement = body + name_len;
14314         }
14315
14316       macro_define_object (file, line, name, replacement);
14317
14318       xfree (name);
14319     }
14320   else if (*p == '(')
14321     {
14322       /* It's a function-like macro.  */
14323       char *name = copy_string (body, p - body);
14324       int argc = 0;
14325       int argv_size = 1;
14326       char **argv = xmalloc (argv_size * sizeof (*argv));
14327
14328       p++;
14329
14330       p = consume_improper_spaces (p, body);
14331
14332       /* Parse the formal argument list.  */
14333       while (*p && *p != ')')
14334         {
14335           /* Find the extent of the current argument name.  */
14336           const char *arg_start = p;
14337
14338           while (*p && *p != ',' && *p != ')' && *p != ' ')
14339             p++;
14340
14341           if (! *p || p == arg_start)
14342             dwarf2_macro_malformed_definition_complaint (body);
14343           else
14344             {
14345               /* Make sure argv has room for the new argument.  */
14346               if (argc >= argv_size)
14347                 {
14348                   argv_size *= 2;
14349                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14350                 }
14351
14352               argv[argc++] = copy_string (arg_start, p - arg_start);
14353             }
14354
14355           p = consume_improper_spaces (p, body);
14356
14357           /* Consume the comma, if present.  */
14358           if (*p == ',')
14359             {
14360               p++;
14361
14362               p = consume_improper_spaces (p, body);
14363             }
14364         }
14365
14366       if (*p == ')')
14367         {
14368           p++;
14369
14370           if (*p == ' ')
14371             /* Perfectly formed definition, no complaints.  */
14372             macro_define_function (file, line, name,
14373                                    argc, (const char **) argv,
14374                                    p + 1);
14375           else if (*p == '\0')
14376             {
14377               /* Complain, but do define it.  */
14378               dwarf2_macro_malformed_definition_complaint (body);
14379               macro_define_function (file, line, name,
14380                                      argc, (const char **) argv,
14381                                      p);
14382             }
14383           else
14384             /* Just complain.  */
14385             dwarf2_macro_malformed_definition_complaint (body);
14386         }
14387       else
14388         /* Just complain.  */
14389         dwarf2_macro_malformed_definition_complaint (body);
14390
14391       xfree (name);
14392       {
14393         int i;
14394
14395         for (i = 0; i < argc; i++)
14396           xfree (argv[i]);
14397       }
14398       xfree (argv);
14399     }
14400   else
14401     dwarf2_macro_malformed_definition_complaint (body);
14402 }
14403
14404
14405 static void
14406 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14407                      char *comp_dir, bfd *abfd,
14408                      struct dwarf2_cu *cu)
14409 {
14410   gdb_byte *mac_ptr, *mac_end;
14411   struct macro_source_file *current_file = 0;
14412   enum dwarf_macinfo_record_type macinfo_type;
14413   int at_commandline;
14414
14415   dwarf2_read_section (dwarf2_per_objfile->objfile,
14416                        &dwarf2_per_objfile->macinfo);
14417   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14418     {
14419       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14420       return;
14421     }
14422
14423   /* First pass: Find the name of the base filename.
14424      This filename is needed in order to process all macros whose definition
14425      (or undefinition) comes from the command line.  These macros are defined
14426      before the first DW_MACINFO_start_file entry, and yet still need to be
14427      associated to the base file.
14428
14429      To determine the base file name, we scan the macro definitions until we
14430      reach the first DW_MACINFO_start_file entry.  We then initialize
14431      CURRENT_FILE accordingly so that any macro definition found before the
14432      first DW_MACINFO_start_file can still be associated to the base file.  */
14433
14434   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14435   mac_end = dwarf2_per_objfile->macinfo.buffer
14436     + dwarf2_per_objfile->macinfo.size;
14437
14438   do
14439     {
14440       /* Do we at least have room for a macinfo type byte?  */
14441       if (mac_ptr >= mac_end)
14442         {
14443           /* Complaint is printed during the second pass as GDB will probably
14444              stop the first pass earlier upon finding
14445              DW_MACINFO_start_file.  */
14446           break;
14447         }
14448
14449       macinfo_type = read_1_byte (abfd, mac_ptr);
14450       mac_ptr++;
14451
14452       switch (macinfo_type)
14453         {
14454           /* A zero macinfo type indicates the end of the macro
14455              information.  */
14456         case 0:
14457           break;
14458
14459         case DW_MACINFO_define:
14460         case DW_MACINFO_undef:
14461           /* Only skip the data by MAC_PTR.  */
14462           {
14463             unsigned int bytes_read;
14464
14465             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14466             mac_ptr += bytes_read;
14467             read_direct_string (abfd, mac_ptr, &bytes_read);
14468             mac_ptr += bytes_read;
14469           }
14470           break;
14471
14472         case DW_MACINFO_start_file:
14473           {
14474             unsigned int bytes_read;
14475             int line, file;
14476
14477             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14478             mac_ptr += bytes_read;
14479             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14480             mac_ptr += bytes_read;
14481
14482             current_file = macro_start_file (file, line, current_file,
14483                                              comp_dir, lh, cu->objfile);
14484           }
14485           break;
14486
14487         case DW_MACINFO_end_file:
14488           /* No data to skip by MAC_PTR.  */
14489           break;
14490
14491         case DW_MACINFO_vendor_ext:
14492           /* Only skip the data by MAC_PTR.  */
14493           {
14494             unsigned int bytes_read;
14495
14496             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14497             mac_ptr += bytes_read;
14498             read_direct_string (abfd, mac_ptr, &bytes_read);
14499             mac_ptr += bytes_read;
14500           }
14501           break;
14502
14503         default:
14504           break;
14505         }
14506     } while (macinfo_type != 0 && current_file == NULL);
14507
14508   /* Second pass: Process all entries.
14509
14510      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14511      command-line macro definitions/undefinitions.  This flag is unset when we
14512      reach the first DW_MACINFO_start_file entry.  */
14513
14514   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14515
14516   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14517      GDB is still reading the definitions from command line.  First
14518      DW_MACINFO_start_file will need to be ignored as it was already executed
14519      to create CURRENT_FILE for the main source holding also the command line
14520      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14521      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14522
14523   at_commandline = 1;
14524
14525   do
14526     {
14527       /* Do we at least have room for a macinfo type byte?  */
14528       if (mac_ptr >= mac_end)
14529         {
14530           dwarf2_macros_too_long_complaint ();
14531           break;
14532         }
14533
14534       macinfo_type = read_1_byte (abfd, mac_ptr);
14535       mac_ptr++;
14536
14537       switch (macinfo_type)
14538         {
14539           /* A zero macinfo type indicates the end of the macro
14540              information.  */
14541         case 0:
14542           break;
14543
14544         case DW_MACINFO_define:
14545         case DW_MACINFO_undef:
14546           {
14547             unsigned int bytes_read;
14548             int line;
14549             char *body;
14550
14551             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14552             mac_ptr += bytes_read;
14553             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14554             mac_ptr += bytes_read;
14555
14556             if (! current_file)
14557               {
14558                 /* DWARF violation as no main source is present.  */
14559                 complaint (&symfile_complaints,
14560                            _("debug info with no main source gives macro %s "
14561                              "on line %d: %s"),
14562                            macinfo_type == DW_MACINFO_define ?
14563                              _("definition") :
14564                                macinfo_type == DW_MACINFO_undef ?
14565                                  _("undefinition") :
14566                                  _("something-or-other"), line, body);
14567                 break;
14568               }
14569             if ((line == 0 && !at_commandline)
14570                 || (line != 0 && at_commandline))
14571               complaint (&symfile_complaints,
14572                          _("debug info gives %s macro %s with %s line %d: %s"),
14573                          at_commandline ? _("command-line") : _("in-file"),
14574                          macinfo_type == DW_MACINFO_define ?
14575                            _("definition") :
14576                              macinfo_type == DW_MACINFO_undef ?
14577                                _("undefinition") :
14578                                _("something-or-other"),
14579                          line == 0 ? _("zero") : _("non-zero"), line, body);
14580
14581             if (macinfo_type == DW_MACINFO_define)
14582               parse_macro_definition (current_file, line, body);
14583             else if (macinfo_type == DW_MACINFO_undef)
14584               macro_undef (current_file, line, body);
14585           }
14586           break;
14587
14588         case DW_MACINFO_start_file:
14589           {
14590             unsigned int bytes_read;
14591             int line, file;
14592
14593             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14594             mac_ptr += bytes_read;
14595             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14596             mac_ptr += bytes_read;
14597
14598             if ((line == 0 && !at_commandline)
14599                 || (line != 0 && at_commandline))
14600               complaint (&symfile_complaints,
14601                          _("debug info gives source %d included "
14602                            "from %s at %s line %d"),
14603                          file, at_commandline ? _("command-line") : _("file"),
14604                          line == 0 ? _("zero") : _("non-zero"), line);
14605
14606             if (at_commandline)
14607               {
14608                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14609                 at_commandline = 0;
14610               }
14611             else
14612               current_file = macro_start_file (file, line,
14613                                                current_file, comp_dir,
14614                                                lh, cu->objfile);
14615           }
14616           break;
14617
14618         case DW_MACINFO_end_file:
14619           if (! current_file)
14620             complaint (&symfile_complaints,
14621                        _("macro debug info has an unmatched "
14622                          "`close_file' directive"));
14623           else
14624             {
14625               current_file = current_file->included_by;
14626               if (! current_file)
14627                 {
14628                   enum dwarf_macinfo_record_type next_type;
14629
14630                   /* GCC circa March 2002 doesn't produce the zero
14631                      type byte marking the end of the compilation
14632                      unit.  Complain if it's not there, but exit no
14633                      matter what.  */
14634
14635                   /* Do we at least have room for a macinfo type byte?  */
14636                   if (mac_ptr >= mac_end)
14637                     {
14638                       dwarf2_macros_too_long_complaint ();
14639                       return;
14640                     }
14641
14642                   /* We don't increment mac_ptr here, so this is just
14643                      a look-ahead.  */
14644                   next_type = read_1_byte (abfd, mac_ptr);
14645                   if (next_type != 0)
14646                     complaint (&symfile_complaints,
14647                                _("no terminating 0-type entry for "
14648                                  "macros in `.debug_macinfo' section"));
14649
14650                   return;
14651                 }
14652             }
14653           break;
14654
14655         case DW_MACINFO_vendor_ext:
14656           {
14657             unsigned int bytes_read;
14658             int constant;
14659
14660             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14661             mac_ptr += bytes_read;
14662             read_direct_string (abfd, mac_ptr, &bytes_read);
14663             mac_ptr += bytes_read;
14664
14665             /* We don't recognize any vendor extensions.  */
14666           }
14667           break;
14668         }
14669     } while (macinfo_type != 0);
14670 }
14671
14672 /* Check if the attribute's form is a DW_FORM_block*
14673    if so return true else false.  */
14674 static int
14675 attr_form_is_block (struct attribute *attr)
14676 {
14677   return (attr == NULL ? 0 :
14678       attr->form == DW_FORM_block1
14679       || attr->form == DW_FORM_block2
14680       || attr->form == DW_FORM_block4
14681       || attr->form == DW_FORM_block
14682       || attr->form == DW_FORM_exprloc);
14683 }
14684
14685 /* Return non-zero if ATTR's value is a section offset --- classes
14686    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14687    You may use DW_UNSND (attr) to retrieve such offsets.
14688
14689    Section 7.5.4, "Attribute Encodings", explains that no attribute
14690    may have a value that belongs to more than one of these classes; it
14691    would be ambiguous if we did, because we use the same forms for all
14692    of them.  */
14693 static int
14694 attr_form_is_section_offset (struct attribute *attr)
14695 {
14696   return (attr->form == DW_FORM_data4
14697           || attr->form == DW_FORM_data8
14698           || attr->form == DW_FORM_sec_offset);
14699 }
14700
14701
14702 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14703    zero otherwise.  When this function returns true, you can apply
14704    dwarf2_get_attr_constant_value to it.
14705
14706    However, note that for some attributes you must check
14707    attr_form_is_section_offset before using this test.  DW_FORM_data4
14708    and DW_FORM_data8 are members of both the constant class, and of
14709    the classes that contain offsets into other debug sections
14710    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14711    that, if an attribute's can be either a constant or one of the
14712    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14713    taken as section offsets, not constants.  */
14714 static int
14715 attr_form_is_constant (struct attribute *attr)
14716 {
14717   switch (attr->form)
14718     {
14719     case DW_FORM_sdata:
14720     case DW_FORM_udata:
14721     case DW_FORM_data1:
14722     case DW_FORM_data2:
14723     case DW_FORM_data4:
14724     case DW_FORM_data8:
14725       return 1;
14726     default:
14727       return 0;
14728     }
14729 }
14730
14731 /* A helper function that fills in a dwarf2_loclist_baton.  */
14732
14733 static void
14734 fill_in_loclist_baton (struct dwarf2_cu *cu,
14735                        struct dwarf2_loclist_baton *baton,
14736                        struct attribute *attr)
14737 {
14738   dwarf2_read_section (dwarf2_per_objfile->objfile,
14739                        &dwarf2_per_objfile->loc);
14740
14741   baton->per_cu = cu->per_cu;
14742   gdb_assert (baton->per_cu);
14743   /* We don't know how long the location list is, but make sure we
14744      don't run off the edge of the section.  */
14745   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14746   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14747   baton->base_address = cu->base_address;
14748 }
14749
14750 static void
14751 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14752                              struct dwarf2_cu *cu)
14753 {
14754   if (attr_form_is_section_offset (attr)
14755       /* ".debug_loc" may not exist at all, or the offset may be outside
14756          the section.  If so, fall through to the complaint in the
14757          other branch.  */
14758       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14759                                                 &dwarf2_per_objfile->loc))
14760     {
14761       struct dwarf2_loclist_baton *baton;
14762
14763       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14764                              sizeof (struct dwarf2_loclist_baton));
14765
14766       fill_in_loclist_baton (cu, baton, attr);
14767
14768       if (cu->base_known == 0)
14769         complaint (&symfile_complaints,
14770                    _("Location list used without "
14771                      "specifying the CU base address."));
14772
14773       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14774       SYMBOL_LOCATION_BATON (sym) = baton;
14775     }
14776   else
14777     {
14778       struct dwarf2_locexpr_baton *baton;
14779
14780       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14781                              sizeof (struct dwarf2_locexpr_baton));
14782       baton->per_cu = cu->per_cu;
14783       gdb_assert (baton->per_cu);
14784
14785       if (attr_form_is_block (attr))
14786         {
14787           /* Note that we're just copying the block's data pointer
14788              here, not the actual data.  We're still pointing into the
14789              info_buffer for SYM's objfile; right now we never release
14790              that buffer, but when we do clean up properly this may
14791              need to change.  */
14792           baton->size = DW_BLOCK (attr)->size;
14793           baton->data = DW_BLOCK (attr)->data;
14794         }
14795       else
14796         {
14797           dwarf2_invalid_attrib_class_complaint ("location description",
14798                                                  SYMBOL_NATURAL_NAME (sym));
14799           baton->size = 0;
14800           baton->data = NULL;
14801         }
14802
14803       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14804       SYMBOL_LOCATION_BATON (sym) = baton;
14805     }
14806 }
14807
14808 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14809    came from a separate debuginfo file, then the master objfile is
14810    returned.  */
14811
14812 struct objfile *
14813 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14814 {
14815   struct objfile *objfile = per_cu->objfile;
14816
14817   /* Return the master objfile, so that we can report and look up the
14818      correct file containing this variable.  */
14819   if (objfile->separate_debug_objfile_backlink)
14820     objfile = objfile->separate_debug_objfile_backlink;
14821
14822   return objfile;
14823 }
14824
14825 /* Return the address size given in the compilation unit header for CU.  */
14826
14827 CORE_ADDR
14828 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14829 {
14830   if (per_cu->cu)
14831     return per_cu->cu->header.addr_size;
14832   else
14833     {
14834       /* If the CU is not currently read in, we re-read its header.  */
14835       struct objfile *objfile = per_cu->objfile;
14836       struct dwarf2_per_objfile *per_objfile
14837         = objfile_data (objfile, dwarf2_objfile_data_key);
14838       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14839       struct comp_unit_head cu_header;
14840
14841       memset (&cu_header, 0, sizeof cu_header);
14842       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14843       return cu_header.addr_size;
14844     }
14845 }
14846
14847 /* Return the offset size given in the compilation unit header for CU.  */
14848
14849 int
14850 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14851 {
14852   if (per_cu->cu)
14853     return per_cu->cu->header.offset_size;
14854   else
14855     {
14856       /* If the CU is not currently read in, we re-read its header.  */
14857       struct objfile *objfile = per_cu->objfile;
14858       struct dwarf2_per_objfile *per_objfile
14859         = objfile_data (objfile, dwarf2_objfile_data_key);
14860       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14861       struct comp_unit_head cu_header;
14862
14863       memset (&cu_header, 0, sizeof cu_header);
14864       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14865       return cu_header.offset_size;
14866     }
14867 }
14868
14869 /* Return the text offset of the CU.  The returned offset comes from
14870    this CU's objfile.  If this objfile came from a separate debuginfo
14871    file, then the offset may be different from the corresponding
14872    offset in the parent objfile.  */
14873
14874 CORE_ADDR
14875 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14876 {
14877   struct objfile *objfile = per_cu->objfile;
14878
14879   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14880 }
14881
14882 /* Locate the .debug_info compilation unit from CU's objfile which contains
14883    the DIE at OFFSET.  Raises an error on failure.  */
14884
14885 static struct dwarf2_per_cu_data *
14886 dwarf2_find_containing_comp_unit (unsigned int offset,
14887                                   struct objfile *objfile)
14888 {
14889   struct dwarf2_per_cu_data *this_cu;
14890   int low, high;
14891
14892   low = 0;
14893   high = dwarf2_per_objfile->n_comp_units - 1;
14894   while (high > low)
14895     {
14896       int mid = low + (high - low) / 2;
14897
14898       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14899         high = mid;
14900       else
14901         low = mid + 1;
14902     }
14903   gdb_assert (low == high);
14904   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14905     {
14906       if (low == 0)
14907         error (_("Dwarf Error: could not find partial DIE containing "
14908                "offset 0x%lx [in module %s]"),
14909                (long) offset, bfd_get_filename (objfile->obfd));
14910
14911       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14912       return dwarf2_per_objfile->all_comp_units[low-1];
14913     }
14914   else
14915     {
14916       this_cu = dwarf2_per_objfile->all_comp_units[low];
14917       if (low == dwarf2_per_objfile->n_comp_units - 1
14918           && offset >= this_cu->offset + this_cu->length)
14919         error (_("invalid dwarf2 offset %u"), offset);
14920       gdb_assert (offset < this_cu->offset + this_cu->length);
14921       return this_cu;
14922     }
14923 }
14924
14925 /* Locate the compilation unit from OBJFILE which is located at exactly
14926    OFFSET.  Raises an error on failure.  */
14927
14928 static struct dwarf2_per_cu_data *
14929 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14930 {
14931   struct dwarf2_per_cu_data *this_cu;
14932
14933   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14934   if (this_cu->offset != offset)
14935     error (_("no compilation unit with offset %u."), offset);
14936   return this_cu;
14937 }
14938
14939 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14940
14941 static void
14942 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14943 {
14944   memset (cu, 0, sizeof (*cu));
14945   cu->objfile = objfile;
14946   obstack_init (&cu->comp_unit_obstack);
14947 }
14948
14949 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14950
14951 static void
14952 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14953 {
14954   struct attribute *attr;
14955
14956   /* Set the language we're debugging.  */
14957   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14958   if (attr)
14959     set_cu_language (DW_UNSND (attr), cu);
14960   else
14961     {
14962       cu->language = language_minimal;
14963       cu->language_defn = language_def (cu->language);
14964     }
14965 }
14966
14967 /* Release one cached compilation unit, CU.  We unlink it from the tree
14968    of compilation units, but we don't remove it from the read_in_chain;
14969    the caller is responsible for that.
14970    NOTE: DATA is a void * because this function is also used as a
14971    cleanup routine.  */
14972
14973 static void
14974 free_one_comp_unit (void *data)
14975 {
14976   struct dwarf2_cu *cu = data;
14977
14978   if (cu->per_cu != NULL)
14979     cu->per_cu->cu = NULL;
14980   cu->per_cu = NULL;
14981
14982   obstack_free (&cu->comp_unit_obstack, NULL);
14983
14984   xfree (cu);
14985 }
14986
14987 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14988    when we're finished with it.  We can't free the pointer itself, but be
14989    sure to unlink it from the cache.  Also release any associated storage
14990    and perform cache maintenance.
14991
14992    Only used during partial symbol parsing.  */
14993
14994 static void
14995 free_stack_comp_unit (void *data)
14996 {
14997   struct dwarf2_cu *cu = data;
14998
14999   obstack_free (&cu->comp_unit_obstack, NULL);
15000   cu->partial_dies = NULL;
15001
15002   if (cu->per_cu != NULL)
15003     {
15004       /* This compilation unit is on the stack in our caller, so we
15005          should not xfree it.  Just unlink it.  */
15006       cu->per_cu->cu = NULL;
15007       cu->per_cu = NULL;
15008
15009       /* If we had a per-cu pointer, then we may have other compilation
15010          units loaded, so age them now.  */
15011       age_cached_comp_units ();
15012     }
15013 }
15014
15015 /* Free all cached compilation units.  */
15016
15017 static void
15018 free_cached_comp_units (void *data)
15019 {
15020   struct dwarf2_per_cu_data *per_cu, **last_chain;
15021
15022   per_cu = dwarf2_per_objfile->read_in_chain;
15023   last_chain = &dwarf2_per_objfile->read_in_chain;
15024   while (per_cu != NULL)
15025     {
15026       struct dwarf2_per_cu_data *next_cu;
15027
15028       next_cu = per_cu->cu->read_in_chain;
15029
15030       free_one_comp_unit (per_cu->cu);
15031       *last_chain = next_cu;
15032
15033       per_cu = next_cu;
15034     }
15035 }
15036
15037 /* Increase the age counter on each cached compilation unit, and free
15038    any that are too old.  */
15039
15040 static void
15041 age_cached_comp_units (void)
15042 {
15043   struct dwarf2_per_cu_data *per_cu, **last_chain;
15044
15045   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15046   per_cu = dwarf2_per_objfile->read_in_chain;
15047   while (per_cu != NULL)
15048     {
15049       per_cu->cu->last_used ++;
15050       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15051         dwarf2_mark (per_cu->cu);
15052       per_cu = per_cu->cu->read_in_chain;
15053     }
15054
15055   per_cu = dwarf2_per_objfile->read_in_chain;
15056   last_chain = &dwarf2_per_objfile->read_in_chain;
15057   while (per_cu != NULL)
15058     {
15059       struct dwarf2_per_cu_data *next_cu;
15060
15061       next_cu = per_cu->cu->read_in_chain;
15062
15063       if (!per_cu->cu->mark)
15064         {
15065           free_one_comp_unit (per_cu->cu);
15066           *last_chain = next_cu;
15067         }
15068       else
15069         last_chain = &per_cu->cu->read_in_chain;
15070
15071       per_cu = next_cu;
15072     }
15073 }
15074
15075 /* Remove a single compilation unit from the cache.  */
15076
15077 static void
15078 free_one_cached_comp_unit (void *target_cu)
15079 {
15080   struct dwarf2_per_cu_data *per_cu, **last_chain;
15081
15082   per_cu = dwarf2_per_objfile->read_in_chain;
15083   last_chain = &dwarf2_per_objfile->read_in_chain;
15084   while (per_cu != NULL)
15085     {
15086       struct dwarf2_per_cu_data *next_cu;
15087
15088       next_cu = per_cu->cu->read_in_chain;
15089
15090       if (per_cu->cu == target_cu)
15091         {
15092           free_one_comp_unit (per_cu->cu);
15093           *last_chain = next_cu;
15094           break;
15095         }
15096       else
15097         last_chain = &per_cu->cu->read_in_chain;
15098
15099       per_cu = next_cu;
15100     }
15101 }
15102
15103 /* Release all extra memory associated with OBJFILE.  */
15104
15105 void
15106 dwarf2_free_objfile (struct objfile *objfile)
15107 {
15108   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15109
15110   if (dwarf2_per_objfile == NULL)
15111     return;
15112
15113   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
15114   free_cached_comp_units (NULL);
15115
15116   if (dwarf2_per_objfile->quick_file_names_table)
15117     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15118
15119   /* Everything else should be on the objfile obstack.  */
15120 }
15121
15122 /* A pair of DIE offset and GDB type pointer.  We store these
15123    in a hash table separate from the DIEs, and preserve them
15124    when the DIEs are flushed out of cache.  */
15125
15126 struct dwarf2_offset_and_type
15127 {
15128   unsigned int offset;
15129   struct type *type;
15130 };
15131
15132 /* Hash function for a dwarf2_offset_and_type.  */
15133
15134 static hashval_t
15135 offset_and_type_hash (const void *item)
15136 {
15137   const struct dwarf2_offset_and_type *ofs = item;
15138
15139   return ofs->offset;
15140 }
15141
15142 /* Equality function for a dwarf2_offset_and_type.  */
15143
15144 static int
15145 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15146 {
15147   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15148   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15149
15150   return ofs_lhs->offset == ofs_rhs->offset;
15151 }
15152
15153 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15154    table if necessary.  For convenience, return TYPE.
15155
15156    The DIEs reading must have careful ordering to:
15157     * Not cause infite loops trying to read in DIEs as a prerequisite for
15158       reading current DIE.
15159     * Not trying to dereference contents of still incompletely read in types
15160       while reading in other DIEs.
15161     * Enable referencing still incompletely read in types just by a pointer to
15162       the type without accessing its fields.
15163
15164    Therefore caller should follow these rules:
15165      * Try to fetch any prerequisite types we may need to build this DIE type
15166        before building the type and calling set_die_type.
15167      * After building type call set_die_type for current DIE as soon as
15168        possible before fetching more types to complete the current type.
15169      * Make the type as complete as possible before fetching more types.  */
15170
15171 static struct type *
15172 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15173 {
15174   struct dwarf2_offset_and_type **slot, ofs;
15175   struct objfile *objfile = cu->objfile;
15176   htab_t *type_hash_ptr;
15177
15178   /* For Ada types, make sure that the gnat-specific data is always
15179      initialized (if not already set).  There are a few types where
15180      we should not be doing so, because the type-specific area is
15181      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15182      where the type-specific area is used to store the floatformat).
15183      But this is not a problem, because the gnat-specific information
15184      is actually not needed for these types.  */
15185   if (need_gnat_info (cu)
15186       && TYPE_CODE (type) != TYPE_CODE_FUNC
15187       && TYPE_CODE (type) != TYPE_CODE_FLT
15188       && !HAVE_GNAT_AUX_INFO (type))
15189     INIT_GNAT_SPECIFIC (type);
15190
15191   if (cu->per_cu->from_debug_types)
15192     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15193   else
15194     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15195
15196   if (*type_hash_ptr == NULL)
15197     {
15198       *type_hash_ptr
15199         = htab_create_alloc_ex (127,
15200                                 offset_and_type_hash,
15201                                 offset_and_type_eq,
15202                                 NULL,
15203                                 &objfile->objfile_obstack,
15204                                 hashtab_obstack_allocate,
15205                                 dummy_obstack_deallocate);
15206     }
15207
15208   ofs.offset = die->offset;
15209   ofs.type = type;
15210   slot = (struct dwarf2_offset_and_type **)
15211     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15212   if (*slot)
15213     complaint (&symfile_complaints,
15214                _("A problem internal to GDB: DIE 0x%x has type already set"),
15215                die->offset);
15216   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15217   **slot = ofs;
15218   return type;
15219 }
15220
15221 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15222    table, or return NULL if the die does not have a saved type.  */
15223
15224 static struct type *
15225 get_die_type_at_offset (unsigned int offset,
15226                         struct dwarf2_per_cu_data *per_cu)
15227 {
15228   struct dwarf2_offset_and_type *slot, ofs;
15229   htab_t type_hash;
15230
15231   if (per_cu->from_debug_types)
15232     type_hash = dwarf2_per_objfile->debug_types_type_hash;
15233   else
15234     type_hash = dwarf2_per_objfile->debug_info_type_hash;
15235   if (type_hash == NULL)
15236     return NULL;
15237
15238   ofs.offset = offset;
15239   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15240   if (slot)
15241     return slot->type;
15242   else
15243     return NULL;
15244 }
15245
15246 /* Look up the type for DIE in the appropriate type_hash table,
15247    or return NULL if DIE does not have a saved type.  */
15248
15249 static struct type *
15250 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15251 {
15252   return get_die_type_at_offset (die->offset, cu->per_cu);
15253 }
15254
15255 /* Add a dependence relationship from CU to REF_PER_CU.  */
15256
15257 static void
15258 dwarf2_add_dependence (struct dwarf2_cu *cu,
15259                        struct dwarf2_per_cu_data *ref_per_cu)
15260 {
15261   void **slot;
15262
15263   if (cu->dependencies == NULL)
15264     cu->dependencies
15265       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15266                               NULL, &cu->comp_unit_obstack,
15267                               hashtab_obstack_allocate,
15268                               dummy_obstack_deallocate);
15269
15270   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15271   if (*slot == NULL)
15272     *slot = ref_per_cu;
15273 }
15274
15275 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15276    Set the mark field in every compilation unit in the
15277    cache that we must keep because we are keeping CU.  */
15278
15279 static int
15280 dwarf2_mark_helper (void **slot, void *data)
15281 {
15282   struct dwarf2_per_cu_data *per_cu;
15283
15284   per_cu = (struct dwarf2_per_cu_data *) *slot;
15285   if (per_cu->cu->mark)
15286     return 1;
15287   per_cu->cu->mark = 1;
15288
15289   if (per_cu->cu->dependencies != NULL)
15290     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15291
15292   return 1;
15293 }
15294
15295 /* Set the mark field in CU and in every other compilation unit in the
15296    cache that we must keep because we are keeping CU.  */
15297
15298 static void
15299 dwarf2_mark (struct dwarf2_cu *cu)
15300 {
15301   if (cu->mark)
15302     return;
15303   cu->mark = 1;
15304   if (cu->dependencies != NULL)
15305     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15306 }
15307
15308 static void
15309 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15310 {
15311   while (per_cu)
15312     {
15313       per_cu->cu->mark = 0;
15314       per_cu = per_cu->cu->read_in_chain;
15315     }
15316 }
15317
15318 /* Trivial hash function for partial_die_info: the hash value of a DIE
15319    is its offset in .debug_info for this objfile.  */
15320
15321 static hashval_t
15322 partial_die_hash (const void *item)
15323 {
15324   const struct partial_die_info *part_die = item;
15325
15326   return part_die->offset;
15327 }
15328
15329 /* Trivial comparison function for partial_die_info structures: two DIEs
15330    are equal if they have the same offset.  */
15331
15332 static int
15333 partial_die_eq (const void *item_lhs, const void *item_rhs)
15334 {
15335   const struct partial_die_info *part_die_lhs = item_lhs;
15336   const struct partial_die_info *part_die_rhs = item_rhs;
15337
15338   return part_die_lhs->offset == part_die_rhs->offset;
15339 }
15340
15341 static struct cmd_list_element *set_dwarf2_cmdlist;
15342 static struct cmd_list_element *show_dwarf2_cmdlist;
15343
15344 static void
15345 set_dwarf2_cmd (char *args, int from_tty)
15346 {
15347   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15348 }
15349
15350 static void
15351 show_dwarf2_cmd (char *args, int from_tty)
15352 {
15353   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15354 }
15355
15356 /* If section described by INFO was mmapped, munmap it now.  */
15357
15358 static void
15359 munmap_section_buffer (struct dwarf2_section_info *info)
15360 {
15361   if (info->was_mmapped)
15362     {
15363 #ifdef HAVE_MMAP
15364       intptr_t begin = (intptr_t) info->buffer;
15365       intptr_t map_begin = begin & ~(pagesize - 1);
15366       size_t map_length = info->size + begin - map_begin;
15367
15368       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15369 #else
15370       /* Without HAVE_MMAP, we should never be here to begin with.  */
15371       gdb_assert_not_reached ("no mmap support");
15372 #endif
15373     }
15374 }
15375
15376 /* munmap debug sections for OBJFILE, if necessary.  */
15377
15378 static void
15379 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15380 {
15381   struct dwarf2_per_objfile *data = d;
15382
15383   /* This is sorted according to the order they're defined in to make it easier
15384      to keep in sync.  */
15385   munmap_section_buffer (&data->info);
15386   munmap_section_buffer (&data->abbrev);
15387   munmap_section_buffer (&data->line);
15388   munmap_section_buffer (&data->loc);
15389   munmap_section_buffer (&data->macinfo);
15390   munmap_section_buffer (&data->str);
15391   munmap_section_buffer (&data->ranges);
15392   munmap_section_buffer (&data->types);
15393   munmap_section_buffer (&data->frame);
15394   munmap_section_buffer (&data->eh_frame);
15395   munmap_section_buffer (&data->gdb_index);
15396 }
15397
15398 \f
15399 /* The "save gdb-index" command.  */
15400
15401 /* The contents of the hash table we create when building the string
15402    table.  */
15403 struct strtab_entry
15404 {
15405   offset_type offset;
15406   const char *str;
15407 };
15408
15409 /* Hash function for a strtab_entry.
15410
15411    Function is used only during write_hash_table so no index format backward
15412    compatibility is needed.  */
15413
15414 static hashval_t
15415 hash_strtab_entry (const void *e)
15416 {
15417   const struct strtab_entry *entry = e;
15418   return mapped_index_string_hash (INT_MAX, entry->str);
15419 }
15420
15421 /* Equality function for a strtab_entry.  */
15422
15423 static int
15424 eq_strtab_entry (const void *a, const void *b)
15425 {
15426   const struct strtab_entry *ea = a;
15427   const struct strtab_entry *eb = b;
15428   return !strcmp (ea->str, eb->str);
15429 }
15430
15431 /* Create a strtab_entry hash table.  */
15432
15433 static htab_t
15434 create_strtab (void)
15435 {
15436   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15437                             xfree, xcalloc, xfree);
15438 }
15439
15440 /* Add a string to the constant pool.  Return the string's offset in
15441    host order.  */
15442
15443 static offset_type
15444 add_string (htab_t table, struct obstack *cpool, const char *str)
15445 {
15446   void **slot;
15447   struct strtab_entry entry;
15448   struct strtab_entry *result;
15449
15450   entry.str = str;
15451   slot = htab_find_slot (table, &entry, INSERT);
15452   if (*slot)
15453     result = *slot;
15454   else
15455     {
15456       result = XNEW (struct strtab_entry);
15457       result->offset = obstack_object_size (cpool);
15458       result->str = str;
15459       obstack_grow_str0 (cpool, str);
15460       *slot = result;
15461     }
15462   return result->offset;
15463 }
15464
15465 /* An entry in the symbol table.  */
15466 struct symtab_index_entry
15467 {
15468   /* The name of the symbol.  */
15469   const char *name;
15470   /* The offset of the name in the constant pool.  */
15471   offset_type index_offset;
15472   /* A sorted vector of the indices of all the CUs that hold an object
15473      of this name.  */
15474   VEC (offset_type) *cu_indices;
15475 };
15476
15477 /* The symbol table.  This is a power-of-2-sized hash table.  */
15478 struct mapped_symtab
15479 {
15480   offset_type n_elements;
15481   offset_type size;
15482   struct symtab_index_entry **data;
15483 };
15484
15485 /* Hash function for a symtab_index_entry.  */
15486
15487 static hashval_t
15488 hash_symtab_entry (const void *e)
15489 {
15490   const struct symtab_index_entry *entry = e;
15491   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15492                          sizeof (offset_type) * VEC_length (offset_type,
15493                                                             entry->cu_indices),
15494                          0);
15495 }
15496
15497 /* Equality function for a symtab_index_entry.  */
15498
15499 static int
15500 eq_symtab_entry (const void *a, const void *b)
15501 {
15502   const struct symtab_index_entry *ea = a;
15503   const struct symtab_index_entry *eb = b;
15504   int len = VEC_length (offset_type, ea->cu_indices);
15505   if (len != VEC_length (offset_type, eb->cu_indices))
15506     return 0;
15507   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15508                   VEC_address (offset_type, eb->cu_indices),
15509                   sizeof (offset_type) * len);
15510 }
15511
15512 /* Destroy a symtab_index_entry.  */
15513
15514 static void
15515 delete_symtab_entry (void *p)
15516 {
15517   struct symtab_index_entry *entry = p;
15518   VEC_free (offset_type, entry->cu_indices);
15519   xfree (entry);
15520 }
15521
15522 /* Create a hash table holding symtab_index_entry objects.  */
15523
15524 static htab_t
15525 create_symbol_hash_table (void)
15526 {
15527   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15528                             delete_symtab_entry, xcalloc, xfree);
15529 }
15530
15531 /* Create a new mapped symtab object.  */
15532
15533 static struct mapped_symtab *
15534 create_mapped_symtab (void)
15535 {
15536   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15537   symtab->n_elements = 0;
15538   symtab->size = 1024;
15539   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15540   return symtab;
15541 }
15542
15543 /* Destroy a mapped_symtab.  */
15544
15545 static void
15546 cleanup_mapped_symtab (void *p)
15547 {
15548   struct mapped_symtab *symtab = p;
15549   /* The contents of the array are freed when the other hash table is
15550      destroyed.  */
15551   xfree (symtab->data);
15552   xfree (symtab);
15553 }
15554
15555 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15556    the slot.
15557    
15558    Function is used only during write_hash_table so no index format backward
15559    compatibility is needed.  */
15560
15561 static struct symtab_index_entry **
15562 find_slot (struct mapped_symtab *symtab, const char *name)
15563 {
15564   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15565
15566   index = hash & (symtab->size - 1);
15567   step = ((hash * 17) & (symtab->size - 1)) | 1;
15568
15569   for (;;)
15570     {
15571       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15572         return &symtab->data[index];
15573       index = (index + step) & (symtab->size - 1);
15574     }
15575 }
15576
15577 /* Expand SYMTAB's hash table.  */
15578
15579 static void
15580 hash_expand (struct mapped_symtab *symtab)
15581 {
15582   offset_type old_size = symtab->size;
15583   offset_type i;
15584   struct symtab_index_entry **old_entries = symtab->data;
15585
15586   symtab->size *= 2;
15587   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15588
15589   for (i = 0; i < old_size; ++i)
15590     {
15591       if (old_entries[i])
15592         {
15593           struct symtab_index_entry **slot = find_slot (symtab,
15594                                                         old_entries[i]->name);
15595           *slot = old_entries[i];
15596         }
15597     }
15598
15599   xfree (old_entries);
15600 }
15601
15602 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15603    is the index of the CU in which the symbol appears.  */
15604
15605 static void
15606 add_index_entry (struct mapped_symtab *symtab, const char *name,
15607                  offset_type cu_index)
15608 {
15609   struct symtab_index_entry **slot;
15610
15611   ++symtab->n_elements;
15612   if (4 * symtab->n_elements / 3 >= symtab->size)
15613     hash_expand (symtab);
15614
15615   slot = find_slot (symtab, name);
15616   if (!*slot)
15617     {
15618       *slot = XNEW (struct symtab_index_entry);
15619       (*slot)->name = name;
15620       (*slot)->cu_indices = NULL;
15621     }
15622   /* Don't push an index twice.  Due to how we add entries we only
15623      have to check the last one.  */ 
15624   if (VEC_empty (offset_type, (*slot)->cu_indices)
15625       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15626     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15627 }
15628
15629 /* Add a vector of indices to the constant pool.  */
15630
15631 static offset_type
15632 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15633                       struct symtab_index_entry *entry)
15634 {
15635   void **slot;
15636
15637   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15638   if (!*slot)
15639     {
15640       offset_type len = VEC_length (offset_type, entry->cu_indices);
15641       offset_type val = MAYBE_SWAP (len);
15642       offset_type iter;
15643       int i;
15644
15645       *slot = entry;
15646       entry->index_offset = obstack_object_size (cpool);
15647
15648       obstack_grow (cpool, &val, sizeof (val));
15649       for (i = 0;
15650            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15651            ++i)
15652         {
15653           val = MAYBE_SWAP (iter);
15654           obstack_grow (cpool, &val, sizeof (val));
15655         }
15656     }
15657   else
15658     {
15659       struct symtab_index_entry *old_entry = *slot;
15660       entry->index_offset = old_entry->index_offset;
15661       entry = old_entry;
15662     }
15663   return entry->index_offset;
15664 }
15665
15666 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15667    constant pool entries going into the obstack CPOOL.  */
15668
15669 static void
15670 write_hash_table (struct mapped_symtab *symtab,
15671                   struct obstack *output, struct obstack *cpool)
15672 {
15673   offset_type i;
15674   htab_t symbol_hash_table;
15675   htab_t str_table;
15676
15677   symbol_hash_table = create_symbol_hash_table ();
15678   str_table = create_strtab ();
15679
15680   /* We add all the index vectors to the constant pool first, to
15681      ensure alignment is ok.  */
15682   for (i = 0; i < symtab->size; ++i)
15683     {
15684       if (symtab->data[i])
15685         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15686     }
15687
15688   /* Now write out the hash table.  */
15689   for (i = 0; i < symtab->size; ++i)
15690     {
15691       offset_type str_off, vec_off;
15692
15693       if (symtab->data[i])
15694         {
15695           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15696           vec_off = symtab->data[i]->index_offset;
15697         }
15698       else
15699         {
15700           /* While 0 is a valid constant pool index, it is not valid
15701              to have 0 for both offsets.  */
15702           str_off = 0;
15703           vec_off = 0;
15704         }
15705
15706       str_off = MAYBE_SWAP (str_off);
15707       vec_off = MAYBE_SWAP (vec_off);
15708
15709       obstack_grow (output, &str_off, sizeof (str_off));
15710       obstack_grow (output, &vec_off, sizeof (vec_off));
15711     }
15712
15713   htab_delete (str_table);
15714   htab_delete (symbol_hash_table);
15715 }
15716
15717 /* Struct to map psymtab to CU index in the index file.  */
15718 struct psymtab_cu_index_map
15719 {
15720   struct partial_symtab *psymtab;
15721   unsigned int cu_index;
15722 };
15723
15724 static hashval_t
15725 hash_psymtab_cu_index (const void *item)
15726 {
15727   const struct psymtab_cu_index_map *map = item;
15728
15729   return htab_hash_pointer (map->psymtab);
15730 }
15731
15732 static int
15733 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15734 {
15735   const struct psymtab_cu_index_map *lhs = item_lhs;
15736   const struct psymtab_cu_index_map *rhs = item_rhs;
15737
15738   return lhs->psymtab == rhs->psymtab;
15739 }
15740
15741 /* Helper struct for building the address table.  */
15742 struct addrmap_index_data
15743 {
15744   struct objfile *objfile;
15745   struct obstack *addr_obstack;
15746   htab_t cu_index_htab;
15747
15748   /* Non-zero if the previous_* fields are valid.
15749      We can't write an entry until we see the next entry (since it is only then
15750      that we know the end of the entry).  */
15751   int previous_valid;
15752   /* Index of the CU in the table of all CUs in the index file.  */
15753   unsigned int previous_cu_index;
15754   /* Start address of the CU.  */
15755   CORE_ADDR previous_cu_start;
15756 };
15757
15758 /* Write an address entry to OBSTACK.  */
15759
15760 static void
15761 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15762                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15763 {
15764   offset_type cu_index_to_write;
15765   char addr[8];
15766   CORE_ADDR baseaddr;
15767
15768   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15769
15770   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15771   obstack_grow (obstack, addr, 8);
15772   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15773   obstack_grow (obstack, addr, 8);
15774   cu_index_to_write = MAYBE_SWAP (cu_index);
15775   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15776 }
15777
15778 /* Worker function for traversing an addrmap to build the address table.  */
15779
15780 static int
15781 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15782 {
15783   struct addrmap_index_data *data = datap;
15784   struct partial_symtab *pst = obj;
15785   offset_type cu_index;
15786   void **slot;
15787
15788   if (data->previous_valid)
15789     add_address_entry (data->objfile, data->addr_obstack,
15790                        data->previous_cu_start, start_addr,
15791                        data->previous_cu_index);
15792
15793   data->previous_cu_start = start_addr;
15794   if (pst != NULL)
15795     {
15796       struct psymtab_cu_index_map find_map, *map;
15797       find_map.psymtab = pst;
15798       map = htab_find (data->cu_index_htab, &find_map);
15799       gdb_assert (map != NULL);
15800       data->previous_cu_index = map->cu_index;
15801       data->previous_valid = 1;
15802     }
15803   else
15804       data->previous_valid = 0;
15805
15806   return 0;
15807 }
15808
15809 /* Write OBJFILE's address map to OBSTACK.
15810    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15811    in the index file.  */
15812
15813 static void
15814 write_address_map (struct objfile *objfile, struct obstack *obstack,
15815                    htab_t cu_index_htab)
15816 {
15817   struct addrmap_index_data addrmap_index_data;
15818
15819   /* When writing the address table, we have to cope with the fact that
15820      the addrmap iterator only provides the start of a region; we have to
15821      wait until the next invocation to get the start of the next region.  */
15822
15823   addrmap_index_data.objfile = objfile;
15824   addrmap_index_data.addr_obstack = obstack;
15825   addrmap_index_data.cu_index_htab = cu_index_htab;
15826   addrmap_index_data.previous_valid = 0;
15827
15828   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15829                    &addrmap_index_data);
15830
15831   /* It's highly unlikely the last entry (end address = 0xff...ff)
15832      is valid, but we should still handle it.
15833      The end address is recorded as the start of the next region, but that
15834      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15835      anyway.  */
15836   if (addrmap_index_data.previous_valid)
15837     add_address_entry (objfile, obstack,
15838                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15839                        addrmap_index_data.previous_cu_index);
15840 }
15841
15842 /* Add a list of partial symbols to SYMTAB.  */
15843
15844 static void
15845 write_psymbols (struct mapped_symtab *symtab,
15846                 htab_t psyms_seen,
15847                 struct partial_symbol **psymp,
15848                 int count,
15849                 offset_type cu_index,
15850                 int is_static)
15851 {
15852   for (; count-- > 0; ++psymp)
15853     {
15854       void **slot, *lookup;
15855
15856       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15857         error (_("Ada is not currently supported by the index"));
15858
15859       /* We only want to add a given psymbol once.  However, we also
15860          want to account for whether it is global or static.  So, we
15861          may add it twice, using slightly different values.  */
15862       if (is_static)
15863         {
15864           uintptr_t val = 1 | (uintptr_t) *psymp;
15865
15866           lookup = (void *) val;
15867         }
15868       else
15869         lookup = *psymp;
15870
15871       /* Only add a given psymbol once.  */
15872       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15873       if (!*slot)
15874         {
15875           *slot = lookup;
15876           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15877         }
15878     }
15879 }
15880
15881 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15882    exception if there is an error.  */
15883
15884 static void
15885 write_obstack (FILE *file, struct obstack *obstack)
15886 {
15887   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15888               file)
15889       != obstack_object_size (obstack))
15890     error (_("couldn't data write to file"));
15891 }
15892
15893 /* Unlink a file if the argument is not NULL.  */
15894
15895 static void
15896 unlink_if_set (void *p)
15897 {
15898   char **filename = p;
15899   if (*filename)
15900     unlink (*filename);
15901 }
15902
15903 /* A helper struct used when iterating over debug_types.  */
15904 struct signatured_type_index_data
15905 {
15906   struct objfile *objfile;
15907   struct mapped_symtab *symtab;
15908   struct obstack *types_list;
15909   htab_t psyms_seen;
15910   int cu_index;
15911 };
15912
15913 /* A helper function that writes a single signatured_type to an
15914    obstack.  */
15915
15916 static int
15917 write_one_signatured_type (void **slot, void *d)
15918 {
15919   struct signatured_type_index_data *info = d;
15920   struct signatured_type *entry = (struct signatured_type *) *slot;
15921   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15922   struct partial_symtab *psymtab = per_cu->v.psymtab;
15923   gdb_byte val[8];
15924
15925   write_psymbols (info->symtab,
15926                   info->psyms_seen,
15927                   info->objfile->global_psymbols.list
15928                   + psymtab->globals_offset,
15929                   psymtab->n_global_syms, info->cu_index,
15930                   0);
15931   write_psymbols (info->symtab,
15932                   info->psyms_seen,
15933                   info->objfile->static_psymbols.list
15934                   + psymtab->statics_offset,
15935                   psymtab->n_static_syms, info->cu_index,
15936                   1);
15937
15938   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
15939   obstack_grow (info->types_list, val, 8);
15940   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15941   obstack_grow (info->types_list, val, 8);
15942   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15943   obstack_grow (info->types_list, val, 8);
15944
15945   ++info->cu_index;
15946
15947   return 1;
15948 }
15949
15950 /* A cleanup function for an htab_t.  */
15951
15952 static void
15953 cleanup_htab (void *arg)
15954 {
15955   htab_delete (arg);
15956 }
15957
15958 /* Create an index file for OBJFILE in the directory DIR.  */
15959
15960 static void
15961 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15962 {
15963   struct cleanup *cleanup;
15964   char *filename, *cleanup_filename;
15965   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15966   struct obstack cu_list, types_cu_list;
15967   int i;
15968   FILE *out_file;
15969   struct mapped_symtab *symtab;
15970   offset_type val, size_of_contents, total_len;
15971   struct stat st;
15972   char buf[8];
15973   htab_t psyms_seen;
15974   htab_t cu_index_htab;
15975   struct psymtab_cu_index_map *psymtab_cu_index_map;
15976
15977   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15978     return;
15979
15980   if (dwarf2_per_objfile->using_index)
15981     error (_("Cannot use an index to create the index"));
15982
15983   if (stat (objfile->name, &st) < 0)
15984     perror_with_name (objfile->name);
15985
15986   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15987                      INDEX_SUFFIX, (char *) NULL);
15988   cleanup = make_cleanup (xfree, filename);
15989
15990   out_file = fopen (filename, "wb");
15991   if (!out_file)
15992     error (_("Can't open `%s' for writing"), filename);
15993
15994   cleanup_filename = filename;
15995   make_cleanup (unlink_if_set, &cleanup_filename);
15996
15997   symtab = create_mapped_symtab ();
15998   make_cleanup (cleanup_mapped_symtab, symtab);
15999
16000   obstack_init (&addr_obstack);
16001   make_cleanup_obstack_free (&addr_obstack);
16002
16003   obstack_init (&cu_list);
16004   make_cleanup_obstack_free (&cu_list);
16005
16006   obstack_init (&types_cu_list);
16007   make_cleanup_obstack_free (&types_cu_list);
16008
16009   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16010                                   NULL, xcalloc, xfree);
16011   make_cleanup (cleanup_htab, psyms_seen);
16012
16013   /* While we're scanning CU's create a table that maps a psymtab pointer
16014      (which is what addrmap records) to its index (which is what is recorded
16015      in the index file).  This will later be needed to write the address
16016      table.  */
16017   cu_index_htab = htab_create_alloc (100,
16018                                      hash_psymtab_cu_index,
16019                                      eq_psymtab_cu_index,
16020                                      NULL, xcalloc, xfree);
16021   make_cleanup (cleanup_htab, cu_index_htab);
16022   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16023     xmalloc (sizeof (struct psymtab_cu_index_map)
16024              * dwarf2_per_objfile->n_comp_units);
16025   make_cleanup (xfree, psymtab_cu_index_map);
16026
16027   /* The CU list is already sorted, so we don't need to do additional
16028      work here.  Also, the debug_types entries do not appear in
16029      all_comp_units, but only in their own hash table.  */
16030   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16031     {
16032       struct dwarf2_per_cu_data *per_cu
16033         = dwarf2_per_objfile->all_comp_units[i];
16034       struct partial_symtab *psymtab = per_cu->v.psymtab;
16035       gdb_byte val[8];
16036       struct psymtab_cu_index_map *map;
16037       void **slot;
16038
16039       write_psymbols (symtab,
16040                       psyms_seen,
16041                       objfile->global_psymbols.list + psymtab->globals_offset,
16042                       psymtab->n_global_syms, i,
16043                       0);
16044       write_psymbols (symtab,
16045                       psyms_seen,
16046                       objfile->static_psymbols.list + psymtab->statics_offset,
16047                       psymtab->n_static_syms, i,
16048                       1);
16049
16050       map = &psymtab_cu_index_map[i];
16051       map->psymtab = psymtab;
16052       map->cu_index = i;
16053       slot = htab_find_slot (cu_index_htab, map, INSERT);
16054       gdb_assert (slot != NULL);
16055       gdb_assert (*slot == NULL);
16056       *slot = map;
16057
16058       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16059       obstack_grow (&cu_list, val, 8);
16060       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16061       obstack_grow (&cu_list, val, 8);
16062     }
16063
16064   /* Dump the address map.  */
16065   write_address_map (objfile, &addr_obstack, cu_index_htab);
16066
16067   /* Write out the .debug_type entries, if any.  */
16068   if (dwarf2_per_objfile->signatured_types)
16069     {
16070       struct signatured_type_index_data sig_data;
16071
16072       sig_data.objfile = objfile;
16073       sig_data.symtab = symtab;
16074       sig_data.types_list = &types_cu_list;
16075       sig_data.psyms_seen = psyms_seen;
16076       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16077       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16078                               write_one_signatured_type, &sig_data);
16079     }
16080
16081   obstack_init (&constant_pool);
16082   make_cleanup_obstack_free (&constant_pool);
16083   obstack_init (&symtab_obstack);
16084   make_cleanup_obstack_free (&symtab_obstack);
16085   write_hash_table (symtab, &symtab_obstack, &constant_pool);
16086
16087   obstack_init (&contents);
16088   make_cleanup_obstack_free (&contents);
16089   size_of_contents = 6 * sizeof (offset_type);
16090   total_len = size_of_contents;
16091
16092   /* The version number.  */
16093   val = MAYBE_SWAP (5);
16094   obstack_grow (&contents, &val, sizeof (val));
16095
16096   /* The offset of the CU list from the start of the file.  */
16097   val = MAYBE_SWAP (total_len);
16098   obstack_grow (&contents, &val, sizeof (val));
16099   total_len += obstack_object_size (&cu_list);
16100
16101   /* The offset of the types CU list from the start of the file.  */
16102   val = MAYBE_SWAP (total_len);
16103   obstack_grow (&contents, &val, sizeof (val));
16104   total_len += obstack_object_size (&types_cu_list);
16105
16106   /* The offset of the address table from the start of the file.  */
16107   val = MAYBE_SWAP (total_len);
16108   obstack_grow (&contents, &val, sizeof (val));
16109   total_len += obstack_object_size (&addr_obstack);
16110
16111   /* The offset of the symbol table from the start of the file.  */
16112   val = MAYBE_SWAP (total_len);
16113   obstack_grow (&contents, &val, sizeof (val));
16114   total_len += obstack_object_size (&symtab_obstack);
16115
16116   /* The offset of the constant pool from the start of the file.  */
16117   val = MAYBE_SWAP (total_len);
16118   obstack_grow (&contents, &val, sizeof (val));
16119   total_len += obstack_object_size (&constant_pool);
16120
16121   gdb_assert (obstack_object_size (&contents) == size_of_contents);
16122
16123   write_obstack (out_file, &contents);
16124   write_obstack (out_file, &cu_list);
16125   write_obstack (out_file, &types_cu_list);
16126   write_obstack (out_file, &addr_obstack);
16127   write_obstack (out_file, &symtab_obstack);
16128   write_obstack (out_file, &constant_pool);
16129
16130   fclose (out_file);
16131
16132   /* We want to keep the file, so we set cleanup_filename to NULL
16133      here.  See unlink_if_set.  */
16134   cleanup_filename = NULL;
16135
16136   do_cleanups (cleanup);
16137 }
16138
16139 /* Implementation of the `save gdb-index' command.
16140    
16141    Note that the file format used by this command is documented in the
16142    GDB manual.  Any changes here must be documented there.  */
16143
16144 static void
16145 save_gdb_index_command (char *arg, int from_tty)
16146 {
16147   struct objfile *objfile;
16148
16149   if (!arg || !*arg)
16150     error (_("usage: save gdb-index DIRECTORY"));
16151
16152   ALL_OBJFILES (objfile)
16153   {
16154     struct stat st;
16155
16156     /* If the objfile does not correspond to an actual file, skip it.  */
16157     if (stat (objfile->name, &st) < 0)
16158       continue;
16159
16160     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16161     if (dwarf2_per_objfile)
16162       {
16163         volatile struct gdb_exception except;
16164
16165         TRY_CATCH (except, RETURN_MASK_ERROR)
16166           {
16167             write_psymtabs_to_index (objfile, arg);
16168           }
16169         if (except.reason < 0)
16170           exception_fprintf (gdb_stderr, except,
16171                              _("Error while writing index for `%s': "),
16172                              objfile->name);
16173       }
16174   }
16175 }
16176
16177 \f
16178
16179 int dwarf2_always_disassemble;
16180
16181 static void
16182 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16183                                 struct cmd_list_element *c, const char *value)
16184 {
16185   fprintf_filtered (file,
16186                     _("Whether to always disassemble "
16187                       "DWARF expressions is %s.\n"),
16188                     value);
16189 }
16190
16191 void _initialize_dwarf2_read (void);
16192
16193 void
16194 _initialize_dwarf2_read (void)
16195 {
16196   struct cmd_list_element *c;
16197
16198   dwarf2_objfile_data_key
16199     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16200
16201   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16202 Set DWARF 2 specific variables.\n\
16203 Configure DWARF 2 variables such as the cache size"),
16204                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16205                   0/*allow-unknown*/, &maintenance_set_cmdlist);
16206
16207   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16208 Show DWARF 2 specific variables\n\
16209 Show DWARF 2 variables such as the cache size"),
16210                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16211                   0/*allow-unknown*/, &maintenance_show_cmdlist);
16212
16213   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16214                             &dwarf2_max_cache_age, _("\
16215 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16216 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16217 A higher limit means that cached compilation units will be stored\n\
16218 in memory longer, and more total memory will be used.  Zero disables\n\
16219 caching, which can slow down startup."),
16220                             NULL,
16221                             show_dwarf2_max_cache_age,
16222                             &set_dwarf2_cmdlist,
16223                             &show_dwarf2_cmdlist);
16224
16225   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16226                            &dwarf2_always_disassemble, _("\
16227 Set whether `info address' always disassembles DWARF expressions."), _("\
16228 Show whether `info address' always disassembles DWARF expressions."), _("\
16229 When enabled, DWARF expressions are always printed in an assembly-like\n\
16230 syntax.  When disabled, expressions will be printed in a more\n\
16231 conversational style, when possible."),
16232                            NULL,
16233                            show_dwarf2_always_disassemble,
16234                            &set_dwarf2_cmdlist,
16235                            &show_dwarf2_cmdlist);
16236
16237   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16238 Set debugging of the dwarf2 DIE reader."), _("\
16239 Show debugging of the dwarf2 DIE reader."), _("\
16240 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16241 The value is the maximum depth to print."),
16242                             NULL,
16243                             NULL,
16244                             &setdebuglist, &showdebuglist);
16245
16246   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16247                _("\
16248 Save a gdb-index file.\n\
16249 Usage: save gdb-index DIRECTORY"),
16250                &save_cmdlist);
16251   set_cmd_completer (c, filename_completer);
16252 }