gdb/doc/
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "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 /* 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 #define INFO_SECTION     "debug_info"
252 #define ABBREV_SECTION   "debug_abbrev"
253 #define LINE_SECTION     "debug_line"
254 #define LOC_SECTION      "debug_loc"
255 #define MACINFO_SECTION  "debug_macinfo"
256 #define STR_SECTION      "debug_str"
257 #define RANGES_SECTION   "debug_ranges"
258 #define TYPES_SECTION    "debug_types"
259 #define FRAME_SECTION    "debug_frame"
260 #define EH_FRAME_SECTION "eh_frame"
261 #define GDB_INDEX_SECTION "gdb_index"
262
263 /* local data types */
264
265 /* We hold several abbreviation tables in memory at the same time.  */
266 #ifndef ABBREV_HASH_SIZE
267 #define ABBREV_HASH_SIZE 121
268 #endif
269
270 /* The data in a compilation unit header, after target2host
271    translation, looks like this.  */
272 struct comp_unit_head
273 {
274   unsigned int length;
275   short version;
276   unsigned char addr_size;
277   unsigned char signed_addr_p;
278   unsigned int abbrev_offset;
279
280   /* Size of file offsets; either 4 or 8.  */
281   unsigned int offset_size;
282
283   /* Size of the length field; either 4 or 12.  */
284   unsigned int initial_length_size;
285
286   /* Offset to the first byte of this compilation unit header in the
287      .debug_info section, for resolving relative reference dies.  */
288   unsigned int offset;
289
290   /* Offset to first die in this cu from the start of the cu.
291      This will be the first byte following the compilation unit header.  */
292   unsigned int first_die_offset;
293 };
294
295 /* Type used for delaying computation of method physnames.
296    See comments for compute_delayed_physnames.  */
297 struct delayed_method_info
298 {
299   /* The type to which the method is attached, i.e., its parent class.  */
300   struct type *type;
301
302   /* The index of the method in the type's function fieldlists.  */
303   int fnfield_index;
304
305   /* The index of the method in the fieldlist.  */
306   int index;
307
308   /* The name of the DIE.  */
309   const char *name;
310
311   /*  The DIE associated with this method.  */
312   struct die_info *die;
313 };
314
315 typedef struct delayed_method_info delayed_method_info;
316 DEF_VEC_O (delayed_method_info);
317
318 /* Internal state when decoding a particular compilation unit.  */
319 struct dwarf2_cu
320 {
321   /* The objfile containing this compilation unit.  */
322   struct objfile *objfile;
323
324   /* The header of the compilation unit.  */
325   struct comp_unit_head header;
326
327   /* Base address of this compilation unit.  */
328   CORE_ADDR base_address;
329
330   /* Non-zero if base_address has been set.  */
331   int base_known;
332
333   struct function_range *first_fn, *last_fn, *cached_fn;
334
335   /* The language we are debugging.  */
336   enum language language;
337   const struct language_defn *language_defn;
338
339   const char *producer;
340
341   /* The generic symbol table building routines have separate lists for
342      file scope symbols and all all other scopes (local scopes).  So
343      we need to select the right one to pass to add_symbol_to_list().
344      We do it by keeping a pointer to the correct list in list_in_scope.
345
346      FIXME: The original dwarf code just treated the file scope as the
347      first local scope, and all other local scopes as nested local
348      scopes, and worked fine.  Check to see if we really need to
349      distinguish these in buildsym.c.  */
350   struct pending **list_in_scope;
351
352   /* DWARF abbreviation table associated with this compilation unit.  */
353   struct abbrev_info **dwarf2_abbrevs;
354
355   /* Storage for the abbrev table.  */
356   struct obstack abbrev_obstack;
357
358   /* Hash table holding all the loaded partial DIEs.  */
359   htab_t partial_dies;
360
361   /* Storage for things with the same lifetime as this read-in compilation
362      unit, including partial DIEs.  */
363   struct obstack comp_unit_obstack;
364
365   /* When multiple dwarf2_cu structures are living in memory, this field
366      chains them all together, so that they can be released efficiently.
367      We will probably also want a generation counter so that most-recently-used
368      compilation units are cached...  */
369   struct dwarf2_per_cu_data *read_in_chain;
370
371   /* Backchain to our per_cu entry if the tree has been built.  */
372   struct dwarf2_per_cu_data *per_cu;
373
374   /* How many compilation units ago was this CU last referenced?  */
375   int last_used;
376
377   /* A hash table of die offsets for following references.  */
378   htab_t die_hash;
379
380   /* Full DIEs if read in.  */
381   struct die_info *dies;
382
383   /* A set of pointers to dwarf2_per_cu_data objects for compilation
384      units referenced by this one.  Only set during full symbol processing;
385      partial symbol tables do not have dependencies.  */
386   htab_t dependencies;
387
388   /* Header data from the line table, during full symbol processing.  */
389   struct line_header *line_header;
390
391   /* A list of methods which need to have physnames computed
392      after all type information has been read.  */
393   VEC (delayed_method_info) *method_list;
394
395   /* Mark used when releasing cached dies.  */
396   unsigned int mark : 1;
397
398   /* This flag will be set if this compilation unit might include
399      inter-compilation-unit references.  */
400   unsigned int has_form_ref_addr : 1;
401
402   /* This flag will be set if this compilation unit includes any
403      DW_TAG_namespace DIEs.  If we know that there are explicit
404      DIEs for namespaces, we don't need to try to infer them
405      from mangled names.  */
406   unsigned int has_namespace_info : 1;
407 };
408
409 /* Persistent data held for a compilation unit, even when not
410    processing it.  We put a pointer to this structure in the
411    read_symtab_private field of the psymtab.  If we encounter
412    inter-compilation-unit references, we also maintain a sorted
413    list of all compilation units.  */
414
415 struct dwarf2_per_cu_data
416 {
417   /* The start offset and length of this compilation unit.  2**29-1
418      bytes should suffice to store the length of any compilation unit
419      - if it doesn't, GDB will fall over anyway.
420      NOTE: Unlike comp_unit_head.length, this length includes
421      initial_length_size.  */
422   unsigned int offset;
423   unsigned int length : 29;
424
425   /* Flag indicating this compilation unit will be read in before
426      any of the current compilation units are processed.  */
427   unsigned int queued : 1;
428
429   /* This flag will be set if we need to load absolutely all DIEs
430      for this compilation unit, instead of just the ones we think
431      are interesting.  It gets set if we look for a DIE in the
432      hash table and don't find it.  */
433   unsigned int load_all_dies : 1;
434
435   /* Non-zero if this CU is from .debug_types.
436      Otherwise it's from .debug_info.  */
437   unsigned int from_debug_types : 1;
438
439   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
440      of the CU cache it gets reset to NULL again.  */
441   struct dwarf2_cu *cu;
442
443   /* The corresponding objfile.  */
444   struct objfile *objfile;
445
446   /* When using partial symbol tables, the 'psymtab' field is active.
447      Otherwise the 'quick' field is active.  */
448   union
449   {
450     /* The partial symbol table associated with this compilation unit,
451        or NULL for partial units (which do not have an associated
452        symtab).  */
453     struct partial_symtab *psymtab;
454
455     /* Data needed by the "quick" functions.  */
456     struct dwarf2_per_cu_quick_data *quick;
457   } v;
458 };
459
460 /* Entry in the signatured_types hash table.  */
461
462 struct signatured_type
463 {
464   ULONGEST signature;
465
466   /* Offset in .debug_types of the TU (type_unit) for this type.  */
467   unsigned int offset;
468
469   /* Offset in .debug_types of the type defined by this TU.  */
470   unsigned int type_offset;
471
472   /* The CU(/TU) of this type.  */
473   struct dwarf2_per_cu_data per_cu;
474 };
475
476 /* Struct used to pass misc. parameters to read_die_and_children, et
477    al.  which are used for both .debug_info and .debug_types dies.
478    All parameters here are unchanging for the life of the call.  This
479    struct exists to abstract away the constant parameters of die
480    reading.  */
481
482 struct die_reader_specs
483 {
484   /* The bfd of this objfile.  */
485   bfd* abfd;
486
487   /* The CU of the DIE we are parsing.  */
488   struct dwarf2_cu *cu;
489
490   /* Pointer to start of section buffer.
491      This is either the start of .debug_info or .debug_types.  */
492   const gdb_byte *buffer;
493 };
494
495 /* The line number information for a compilation unit (found in the
496    .debug_line section) begins with a "statement program header",
497    which contains the following information.  */
498 struct line_header
499 {
500   unsigned int total_length;
501   unsigned short version;
502   unsigned int header_length;
503   unsigned char minimum_instruction_length;
504   unsigned char maximum_ops_per_instruction;
505   unsigned char default_is_stmt;
506   int line_base;
507   unsigned char line_range;
508   unsigned char opcode_base;
509
510   /* standard_opcode_lengths[i] is the number of operands for the
511      standard opcode whose value is i.  This means that
512      standard_opcode_lengths[0] is unused, and the last meaningful
513      element is standard_opcode_lengths[opcode_base - 1].  */
514   unsigned char *standard_opcode_lengths;
515
516   /* The include_directories table.  NOTE!  These strings are not
517      allocated with xmalloc; instead, they are pointers into
518      debug_line_buffer.  If you try to free them, `free' will get
519      indigestion.  */
520   unsigned int num_include_dirs, include_dirs_size;
521   char **include_dirs;
522
523   /* The file_names table.  NOTE!  These strings are not allocated
524      with xmalloc; instead, they are pointers into debug_line_buffer.
525      Don't try to free them directly.  */
526   unsigned int num_file_names, file_names_size;
527   struct file_entry
528   {
529     char *name;
530     unsigned int dir_index;
531     unsigned int mod_time;
532     unsigned int length;
533     int included_p; /* Non-zero if referenced by the Line Number Program.  */
534     struct symtab *symtab; /* The associated symbol table, if any.  */
535   } *file_names;
536
537   /* The start and end of the statement program following this
538      header.  These point into dwarf2_per_objfile->line_buffer.  */
539   gdb_byte *statement_program_start, *statement_program_end;
540 };
541
542 /* When we construct a partial symbol table entry we only
543    need this much information.  */
544 struct partial_die_info
545   {
546     /* Offset of this DIE.  */
547     unsigned int offset;
548
549     /* DWARF-2 tag for this DIE.  */
550     ENUM_BITFIELD(dwarf_tag) tag : 16;
551
552     /* Assorted flags describing the data found in this DIE.  */
553     unsigned int has_children : 1;
554     unsigned int is_external : 1;
555     unsigned int is_declaration : 1;
556     unsigned int has_type : 1;
557     unsigned int has_specification : 1;
558     unsigned int has_pc_info : 1;
559
560     /* Flag set if the SCOPE field of this structure has been
561        computed.  */
562     unsigned int scope_set : 1;
563
564     /* Flag set if the DIE has a byte_size attribute.  */
565     unsigned int has_byte_size : 1;
566
567     /* Flag set if any of the DIE's children are template arguments.  */
568     unsigned int has_template_arguments : 1;
569
570     /* Flag set if fixup_partial_die has been called on this die.  */
571     unsigned int fixup_called : 1;
572
573     /* The name of this DIE.  Normally the value of DW_AT_name, but
574        sometimes a default name for unnamed DIEs.  */
575     char *name;
576
577     /* The linkage name, if present.  */
578     const char *linkage_name;
579
580     /* The scope to prepend to our children.  This is generally
581        allocated on the comp_unit_obstack, so will disappear
582        when this compilation unit leaves the cache.  */
583     char *scope;
584
585     /* The location description associated with this DIE, if any.  */
586     struct dwarf_block *locdesc;
587
588     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
589     CORE_ADDR lowpc;
590     CORE_ADDR highpc;
591
592     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
593        DW_AT_sibling, if any.  */
594     /* NOTE: This member isn't strictly necessary, read_partial_die could
595        return DW_AT_sibling values to its caller load_partial_dies.  */
596     gdb_byte *sibling;
597
598     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
599        DW_AT_specification (or DW_AT_abstract_origin or
600        DW_AT_extension).  */
601     unsigned int spec_offset;
602
603     /* Pointers to this DIE's parent, first child, and next sibling,
604        if any.  */
605     struct partial_die_info *die_parent, *die_child, *die_sibling;
606   };
607
608 /* This data structure holds the information of an abbrev.  */
609 struct abbrev_info
610   {
611     unsigned int number;        /* number identifying abbrev */
612     enum dwarf_tag tag;         /* dwarf tag */
613     unsigned short has_children;                /* boolean */
614     unsigned short num_attrs;   /* number of attributes */
615     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
616     struct abbrev_info *next;   /* next in chain */
617   };
618
619 struct attr_abbrev
620   {
621     ENUM_BITFIELD(dwarf_attribute) name : 16;
622     ENUM_BITFIELD(dwarf_form) form : 16;
623   };
624
625 /* Attributes have a name and a value.  */
626 struct attribute
627   {
628     ENUM_BITFIELD(dwarf_attribute) name : 16;
629     ENUM_BITFIELD(dwarf_form) form : 15;
630
631     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
632        field should be in u.str (existing only for DW_STRING) but it is kept
633        here for better struct attribute alignment.  */
634     unsigned int string_is_canonical : 1;
635
636     union
637       {
638         char *str;
639         struct dwarf_block *blk;
640         ULONGEST unsnd;
641         LONGEST snd;
642         CORE_ADDR addr;
643         struct signatured_type *signatured_type;
644       }
645     u;
646   };
647
648 /* This data structure holds a complete die structure.  */
649 struct die_info
650   {
651     /* DWARF-2 tag for this DIE.  */
652     ENUM_BITFIELD(dwarf_tag) tag : 16;
653
654     /* Number of attributes */
655     unsigned char num_attrs;
656
657     /* True if we're presently building the full type name for the
658        type derived from this DIE.  */
659     unsigned char building_fullname : 1;
660
661     /* Abbrev number */
662     unsigned int abbrev;
663
664     /* Offset in .debug_info or .debug_types section.  */
665     unsigned int offset;
666
667     /* The dies in a compilation unit form an n-ary tree.  PARENT
668        points to this die's parent; CHILD points to the first child of
669        this node; and all the children of a given node are chained
670        together via their SIBLING fields.  */
671     struct die_info *child;     /* Its first child, if any.  */
672     struct die_info *sibling;   /* Its next sibling, if any.  */
673     struct die_info *parent;    /* Its parent, if any.  */
674
675     /* An array of attributes, with NUM_ATTRS elements.  There may be
676        zero, but it's not common and zero-sized arrays are not
677        sufficiently portable C.  */
678     struct attribute attrs[1];
679   };
680
681 struct function_range
682 {
683   const char *name;
684   CORE_ADDR lowpc, highpc;
685   int seen_line;
686   struct function_range *next;
687 };
688
689 /* Get at parts of an attribute structure.  */
690
691 #define DW_STRING(attr)    ((attr)->u.str)
692 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
693 #define DW_UNSND(attr)     ((attr)->u.unsnd)
694 #define DW_BLOCK(attr)     ((attr)->u.blk)
695 #define DW_SND(attr)       ((attr)->u.snd)
696 #define DW_ADDR(attr)      ((attr)->u.addr)
697 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
698
699 /* Blocks are a bunch of untyped bytes.  */
700 struct dwarf_block
701   {
702     unsigned int size;
703     gdb_byte *data;
704   };
705
706 #ifndef ATTR_ALLOC_CHUNK
707 #define ATTR_ALLOC_CHUNK 4
708 #endif
709
710 /* Allocate fields for structs, unions and enums in this size.  */
711 #ifndef DW_FIELD_ALLOC_CHUNK
712 #define DW_FIELD_ALLOC_CHUNK 4
713 #endif
714
715 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
716    but this would require a corresponding change in unpack_field_as_long
717    and friends.  */
718 static int bits_per_byte = 8;
719
720 /* The routines that read and process dies for a C struct or C++ class
721    pass lists of data member fields and lists of member function fields
722    in an instance of a field_info structure, as defined below.  */
723 struct field_info
724   {
725     /* List of data member and baseclasses fields.  */
726     struct nextfield
727       {
728         struct nextfield *next;
729         int accessibility;
730         int virtuality;
731         struct field field;
732       }
733      *fields, *baseclasses;
734
735     /* Number of fields (including baseclasses).  */
736     int nfields;
737
738     /* Number of baseclasses.  */
739     int nbaseclasses;
740
741     /* Set if the accesibility of one of the fields is not public.  */
742     int non_public_fields;
743
744     /* Member function fields array, entries are allocated in the order they
745        are encountered in the object file.  */
746     struct nextfnfield
747       {
748         struct nextfnfield *next;
749         struct fn_field fnfield;
750       }
751      *fnfields;
752
753     /* Member function fieldlist array, contains name of possibly overloaded
754        member function, number of overloaded member functions and a pointer
755        to the head of the member function field chain.  */
756     struct fnfieldlist
757       {
758         char *name;
759         int length;
760         struct nextfnfield *head;
761       }
762      *fnfieldlists;
763
764     /* Number of entries in the fnfieldlists array.  */
765     int nfnfields;
766
767     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
768        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
769     struct typedef_field_list
770       {
771         struct typedef_field field;
772         struct typedef_field_list *next;
773       }
774     *typedef_field_list;
775     unsigned typedef_field_list_count;
776   };
777
778 /* One item on the queue of compilation units to read in full symbols
779    for.  */
780 struct dwarf2_queue_item
781 {
782   struct dwarf2_per_cu_data *per_cu;
783   struct dwarf2_queue_item *next;
784 };
785
786 /* The current queue.  */
787 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
788
789 /* Loaded secondary compilation units are kept in memory until they
790    have not been referenced for the processing of this many
791    compilation units.  Set this to zero to disable caching.  Cache
792    sizes of up to at least twenty will improve startup time for
793    typical inter-CU-reference binaries, at an obvious memory cost.  */
794 static int dwarf2_max_cache_age = 5;
795 static void
796 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
797                            struct cmd_list_element *c, const char *value)
798 {
799   fprintf_filtered (file, _("The upper bound on the age of cached "
800                             "dwarf2 compilation units is %s.\n"),
801                     value);
802 }
803
804
805 /* Various complaints about symbol reading that don't abort the process.  */
806
807 static void
808 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
809 {
810   complaint (&symfile_complaints,
811              _("statement list doesn't fit in .debug_line section"));
812 }
813
814 static void
815 dwarf2_debug_line_missing_file_complaint (void)
816 {
817   complaint (&symfile_complaints,
818              _(".debug_line section has line data without a file"));
819 }
820
821 static void
822 dwarf2_debug_line_missing_end_sequence_complaint (void)
823 {
824   complaint (&symfile_complaints,
825              _(".debug_line section has line "
826                "program sequence without an end"));
827 }
828
829 static void
830 dwarf2_complex_location_expr_complaint (void)
831 {
832   complaint (&symfile_complaints, _("location expression too complex"));
833 }
834
835 static void
836 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
837                                               int arg3)
838 {
839   complaint (&symfile_complaints,
840              _("const value length mismatch for '%s', got %d, expected %d"),
841              arg1, arg2, arg3);
842 }
843
844 static void
845 dwarf2_macros_too_long_complaint (void)
846 {
847   complaint (&symfile_complaints,
848              _("macro info runs off end of `.debug_macinfo' section"));
849 }
850
851 static void
852 dwarf2_macro_malformed_definition_complaint (const char *arg1)
853 {
854   complaint (&symfile_complaints,
855              _("macro debug info contains a "
856                "malformed macro definition:\n`%s'"),
857              arg1);
858 }
859
860 static void
861 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
862 {
863   complaint (&symfile_complaints,
864              _("invalid attribute class or form for '%s' in '%s'"),
865              arg1, arg2);
866 }
867
868 /* local function prototypes */
869
870 static void dwarf2_locate_sections (bfd *, asection *, void *);
871
872 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873                                            struct objfile *);
874
875 static void dwarf2_build_psymtabs_hard (struct objfile *);
876
877 static void scan_partial_symbols (struct partial_die_info *,
878                                   CORE_ADDR *, CORE_ADDR *,
879                                   int, struct dwarf2_cu *);
880
881 static void add_partial_symbol (struct partial_die_info *,
882                                 struct dwarf2_cu *);
883
884 static void add_partial_namespace (struct partial_die_info *pdi,
885                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
886                                    int need_pc, struct dwarf2_cu *cu);
887
888 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
889                                 CORE_ADDR *highpc, int need_pc,
890                                 struct dwarf2_cu *cu);
891
892 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
893                                      struct dwarf2_cu *cu);
894
895 static void add_partial_subprogram (struct partial_die_info *pdi,
896                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
897                                     int need_pc, struct dwarf2_cu *cu);
898
899 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
900                                      gdb_byte *buffer, gdb_byte *info_ptr,
901                                      bfd *abfd, struct dwarf2_cu *cu);
902
903 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
904
905 static void psymtab_to_symtab_1 (struct partial_symtab *);
906
907 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
908
909 static void dwarf2_free_abbrev_table (void *);
910
911 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
912                                             struct dwarf2_cu *);
913
914 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
915                                                  struct dwarf2_cu *);
916
917 static struct partial_die_info *load_partial_dies (bfd *,
918                                                    gdb_byte *, gdb_byte *,
919                                                    int, struct dwarf2_cu *);
920
921 static gdb_byte *read_partial_die (struct partial_die_info *,
922                                    struct abbrev_info *abbrev,
923                                    unsigned int, bfd *,
924                                    gdb_byte *, gdb_byte *,
925                                    struct dwarf2_cu *);
926
927 static struct partial_die_info *find_partial_die (unsigned int,
928                                                   struct dwarf2_cu *);
929
930 static void fixup_partial_die (struct partial_die_info *,
931                                struct dwarf2_cu *);
932
933 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
934                                  bfd *, gdb_byte *, struct dwarf2_cu *);
935
936 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
937                                        bfd *, gdb_byte *, struct dwarf2_cu *);
938
939 static unsigned int read_1_byte (bfd *, gdb_byte *);
940
941 static int read_1_signed_byte (bfd *, gdb_byte *);
942
943 static unsigned int read_2_bytes (bfd *, gdb_byte *);
944
945 static unsigned int read_4_bytes (bfd *, gdb_byte *);
946
947 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
948
949 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
950                                unsigned int *);
951
952 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
953
954 static LONGEST read_checked_initial_length_and_offset
955   (bfd *, gdb_byte *, const struct comp_unit_head *,
956    unsigned int *, unsigned int *);
957
958 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
959                             unsigned int *);
960
961 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
962
963 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
964
965 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
966
967 static char *read_indirect_string (bfd *, gdb_byte *,
968                                    const struct comp_unit_head *,
969                                    unsigned int *);
970
971 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
972
973 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
974
975 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
976
977 static void set_cu_language (unsigned int, struct dwarf2_cu *);
978
979 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
980                                       struct dwarf2_cu *);
981
982 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
983                                                 unsigned int,
984                                                 struct dwarf2_cu *);
985
986 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
987                                struct dwarf2_cu *cu);
988
989 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
990
991 static struct die_info *die_specification (struct die_info *die,
992                                            struct dwarf2_cu **);
993
994 static void free_line_header (struct line_header *lh);
995
996 static void add_file_name (struct line_header *, char *, unsigned int,
997                            unsigned int, unsigned int);
998
999 static struct line_header *(dwarf_decode_line_header
1000                             (unsigned int offset,
1001                              bfd *abfd, struct dwarf2_cu *cu));
1002
1003 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1004                                 struct dwarf2_cu *, struct partial_symtab *);
1005
1006 static void dwarf2_start_subfile (char *, const char *, const char *);
1007
1008 static struct symbol *new_symbol (struct die_info *, struct type *,
1009                                   struct dwarf2_cu *);
1010
1011 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1012                                        struct dwarf2_cu *, struct symbol *);
1013
1014 static void dwarf2_const_value (struct attribute *, struct symbol *,
1015                                 struct dwarf2_cu *);
1016
1017 static void dwarf2_const_value_attr (struct attribute *attr,
1018                                      struct type *type,
1019                                      const char *name,
1020                                      struct obstack *obstack,
1021                                      struct dwarf2_cu *cu, long *value,
1022                                      gdb_byte **bytes,
1023                                      struct dwarf2_locexpr_baton **baton);
1024
1025 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1026
1027 static int need_gnat_info (struct dwarf2_cu *);
1028
1029 static struct type *die_descriptive_type (struct die_info *,
1030                                           struct dwarf2_cu *);
1031
1032 static void set_descriptive_type (struct type *, struct die_info *,
1033                                   struct dwarf2_cu *);
1034
1035 static struct type *die_containing_type (struct die_info *,
1036                                          struct dwarf2_cu *);
1037
1038 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1039                                      struct dwarf2_cu *);
1040
1041 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1042
1043 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1044
1045 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1046
1047 static char *typename_concat (struct obstack *obs, const char *prefix,
1048                               const char *suffix, int physname,
1049                               struct dwarf2_cu *cu);
1050
1051 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1052
1053 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1054
1055 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1056
1057 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1060                                struct dwarf2_cu *, struct partial_symtab *);
1061
1062 static int dwarf2_get_pc_bounds (struct die_info *,
1063                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1064                                  struct partial_symtab *);
1065
1066 static void get_scope_pc_bounds (struct die_info *,
1067                                  CORE_ADDR *, CORE_ADDR *,
1068                                  struct dwarf2_cu *);
1069
1070 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1071                                         CORE_ADDR, struct dwarf2_cu *);
1072
1073 static void dwarf2_add_field (struct field_info *, struct die_info *,
1074                               struct dwarf2_cu *);
1075
1076 static void dwarf2_attach_fields_to_type (struct field_info *,
1077                                           struct type *, struct dwarf2_cu *);
1078
1079 static void dwarf2_add_member_fn (struct field_info *,
1080                                   struct die_info *, struct type *,
1081                                   struct dwarf2_cu *);
1082
1083 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1084                                              struct type *,
1085                                              struct dwarf2_cu *);
1086
1087 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1090
1091 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1092
1093 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1094
1095 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1096
1097 static struct type *read_module_type (struct die_info *die,
1098                                       struct dwarf2_cu *cu);
1099
1100 static const char *namespace_name (struct die_info *die,
1101                                    int *is_anonymous, struct dwarf2_cu *);
1102
1103 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1104
1105 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1106
1107 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1108                                                        struct dwarf2_cu *);
1109
1110 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1111
1112 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1113                                                  gdb_byte *info_ptr,
1114                                                  gdb_byte **new_info_ptr,
1115                                                  struct die_info *parent);
1116
1117 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1118                                                gdb_byte *info_ptr,
1119                                                gdb_byte **new_info_ptr,
1120                                                struct die_info *parent);
1121
1122 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1123                                                gdb_byte *info_ptr,
1124                                                gdb_byte **new_info_ptr,
1125                                                struct die_info *parent);
1126
1127 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1128                                 struct die_info **, gdb_byte *,
1129                                 int *);
1130
1131 static void process_die (struct die_info *, struct dwarf2_cu *);
1132
1133 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1134                                        struct obstack *);
1135
1136 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1137
1138 static const char *dwarf2_full_name (char *name,
1139                                      struct die_info *die,
1140                                      struct dwarf2_cu *cu);
1141
1142 static struct die_info *dwarf2_extension (struct die_info *die,
1143                                           struct dwarf2_cu **);
1144
1145 static char *dwarf_tag_name (unsigned int);
1146
1147 static char *dwarf_attr_name (unsigned int);
1148
1149 static char *dwarf_form_name (unsigned int);
1150
1151 static char *dwarf_bool_name (unsigned int);
1152
1153 static char *dwarf_type_encoding_name (unsigned int);
1154
1155 #if 0
1156 static char *dwarf_cfi_name (unsigned int);
1157 #endif
1158
1159 static struct die_info *sibling_die (struct die_info *);
1160
1161 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1162
1163 static void dump_die_for_error (struct die_info *);
1164
1165 static void dump_die_1 (struct ui_file *, int level, int max_level,
1166                         struct die_info *);
1167
1168 /*static*/ void dump_die (struct die_info *, int max_level);
1169
1170 static void store_in_ref_table (struct die_info *,
1171                                 struct dwarf2_cu *);
1172
1173 static int is_ref_attr (struct attribute *);
1174
1175 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1176
1177 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1178
1179 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1180                                                struct attribute *,
1181                                                struct dwarf2_cu **);
1182
1183 static struct die_info *follow_die_ref (struct die_info *,
1184                                         struct attribute *,
1185                                         struct dwarf2_cu **);
1186
1187 static struct die_info *follow_die_sig (struct die_info *,
1188                                         struct attribute *,
1189                                         struct dwarf2_cu **);
1190
1191 static void read_signatured_type_at_offset (struct objfile *objfile,
1192                                             unsigned int offset);
1193
1194 static void read_signatured_type (struct objfile *,
1195                                   struct signatured_type *type_sig);
1196
1197 /* memory allocation interface */
1198
1199 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1200
1201 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1202
1203 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1204
1205 static void initialize_cu_func_list (struct dwarf2_cu *);
1206
1207 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1208                                  struct dwarf2_cu *);
1209
1210 static void dwarf_decode_macros (struct line_header *, unsigned int,
1211                                  char *, bfd *, struct dwarf2_cu *);
1212
1213 static int attr_form_is_block (struct attribute *);
1214
1215 static int attr_form_is_section_offset (struct attribute *);
1216
1217 static int attr_form_is_constant (struct attribute *);
1218
1219 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1220                                    struct dwarf2_loclist_baton *baton,
1221                                    struct attribute *attr);
1222
1223 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1224                                          struct symbol *sym,
1225                                          struct dwarf2_cu *cu);
1226
1227 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1228                                struct abbrev_info *abbrev,
1229                                struct dwarf2_cu *cu);
1230
1231 static void free_stack_comp_unit (void *);
1232
1233 static hashval_t partial_die_hash (const void *item);
1234
1235 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1236
1237 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1238   (unsigned int offset, struct objfile *objfile);
1239
1240 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1241   (unsigned int offset, struct objfile *objfile);
1242
1243 static void init_one_comp_unit (struct dwarf2_cu *cu,
1244                                 struct objfile *objfile);
1245
1246 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1247                                    struct die_info *comp_unit_die);
1248
1249 static void free_one_comp_unit (void *);
1250
1251 static void free_cached_comp_units (void *);
1252
1253 static void age_cached_comp_units (void);
1254
1255 static void free_one_cached_comp_unit (void *);
1256
1257 static struct type *set_die_type (struct die_info *, struct type *,
1258                                   struct dwarf2_cu *);
1259
1260 static void create_all_comp_units (struct objfile *);
1261
1262 static int create_debug_types_hash_table (struct objfile *objfile);
1263
1264 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1265                                  struct objfile *);
1266
1267 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1268
1269 static void dwarf2_add_dependence (struct dwarf2_cu *,
1270                                    struct dwarf2_per_cu_data *);
1271
1272 static void dwarf2_mark (struct dwarf2_cu *);
1273
1274 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1275
1276 static struct type *get_die_type_at_offset (unsigned int,
1277                                             struct dwarf2_per_cu_data *per_cu);
1278
1279 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1280
1281 static void dwarf2_release_queue (void *dummy);
1282
1283 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1284                              struct objfile *objfile);
1285
1286 static void process_queue (struct objfile *objfile);
1287
1288 static void find_file_and_directory (struct die_info *die,
1289                                      struct dwarf2_cu *cu,
1290                                      char **name, char **comp_dir);
1291
1292 static char *file_full_name (int file, struct line_header *lh,
1293                              const char *comp_dir);
1294
1295 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1296                                               gdb_byte *info_ptr,
1297                                               gdb_byte *buffer,
1298                                               unsigned int buffer_size,
1299                                               bfd *abfd);
1300
1301 static void init_cu_die_reader (struct die_reader_specs *reader,
1302                                 struct dwarf2_cu *cu);
1303
1304 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1305
1306 #if WORDS_BIGENDIAN
1307
1308 /* Convert VALUE between big- and little-endian.  */
1309 static offset_type
1310 byte_swap (offset_type value)
1311 {
1312   offset_type result;
1313
1314   result = (value & 0xff) << 24;
1315   result |= (value & 0xff00) << 8;
1316   result |= (value & 0xff0000) >> 8;
1317   result |= (value & 0xff000000) >> 24;
1318   return result;
1319 }
1320
1321 #define MAYBE_SWAP(V)  byte_swap (V)
1322
1323 #else
1324 #define MAYBE_SWAP(V) (V)
1325 #endif /* WORDS_BIGENDIAN */
1326
1327 /* The suffix for an index file.  */
1328 #define INDEX_SUFFIX ".gdb-index"
1329
1330 static const char *dwarf2_physname (char *name, struct die_info *die,
1331                                     struct dwarf2_cu *cu);
1332
1333 /* Try to locate the sections we need for DWARF 2 debugging
1334    information and return true if we have enough to do something.  */
1335
1336 int
1337 dwarf2_has_info (struct objfile *objfile)
1338 {
1339   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1340   if (!dwarf2_per_objfile)
1341     {
1342       /* Initialize per-objfile state.  */
1343       struct dwarf2_per_objfile *data
1344         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1345
1346       memset (data, 0, sizeof (*data));
1347       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1348       dwarf2_per_objfile = data;
1349
1350       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1351       dwarf2_per_objfile->objfile = objfile;
1352     }
1353   return (dwarf2_per_objfile->info.asection != NULL
1354           && dwarf2_per_objfile->abbrev.asection != NULL);
1355 }
1356
1357 /* When loading sections, we can either look for ".<name>", or for
1358  * ".z<name>", which indicates a compressed section.  */
1359
1360 static int
1361 section_is_p (const char *section_name, const char *name)
1362 {
1363   return (section_name[0] == '.'
1364           && (strcmp (section_name + 1, name) == 0
1365               || (section_name[1] == 'z'
1366                   && strcmp (section_name + 2, name) == 0)));
1367 }
1368
1369 /* This function is mapped across the sections and remembers the
1370    offset and size of each of the debugging sections we are interested
1371    in.  */
1372
1373 static void
1374 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1375 {
1376   if (section_is_p (sectp->name, INFO_SECTION))
1377     {
1378       dwarf2_per_objfile->info.asection = sectp;
1379       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1380     }
1381   else if (section_is_p (sectp->name, ABBREV_SECTION))
1382     {
1383       dwarf2_per_objfile->abbrev.asection = sectp;
1384       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1385     }
1386   else if (section_is_p (sectp->name, LINE_SECTION))
1387     {
1388       dwarf2_per_objfile->line.asection = sectp;
1389       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1390     }
1391   else if (section_is_p (sectp->name, LOC_SECTION))
1392     {
1393       dwarf2_per_objfile->loc.asection = sectp;
1394       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1395     }
1396   else if (section_is_p (sectp->name, MACINFO_SECTION))
1397     {
1398       dwarf2_per_objfile->macinfo.asection = sectp;
1399       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1400     }
1401   else if (section_is_p (sectp->name, STR_SECTION))
1402     {
1403       dwarf2_per_objfile->str.asection = sectp;
1404       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1405     }
1406   else if (section_is_p (sectp->name, FRAME_SECTION))
1407     {
1408       dwarf2_per_objfile->frame.asection = sectp;
1409       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1410     }
1411   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1412     {
1413       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1414
1415       if (aflag & SEC_HAS_CONTENTS)
1416         {
1417           dwarf2_per_objfile->eh_frame.asection = sectp;
1418           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1419         }
1420     }
1421   else if (section_is_p (sectp->name, RANGES_SECTION))
1422     {
1423       dwarf2_per_objfile->ranges.asection = sectp;
1424       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1425     }
1426   else if (section_is_p (sectp->name, TYPES_SECTION))
1427     {
1428       dwarf2_per_objfile->types.asection = sectp;
1429       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1430     }
1431   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1432     {
1433       dwarf2_per_objfile->gdb_index.asection = sectp;
1434       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1435     }
1436
1437   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1438       && bfd_section_vma (abfd, sectp) == 0)
1439     dwarf2_per_objfile->has_section_at_zero = 1;
1440 }
1441
1442 /* Decompress a section that was compressed using zlib.  Store the
1443    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1444
1445 static void
1446 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1447                          gdb_byte **outbuf, bfd_size_type *outsize)
1448 {
1449   bfd *abfd = objfile->obfd;
1450 #ifndef HAVE_ZLIB_H
1451   error (_("Support for zlib-compressed DWARF data (from '%s') "
1452            "is disabled in this copy of GDB"),
1453          bfd_get_filename (abfd));
1454 #else
1455   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1456   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1457   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1458   bfd_size_type uncompressed_size;
1459   gdb_byte *uncompressed_buffer;
1460   z_stream strm;
1461   int rc;
1462   int header_size = 12;
1463
1464   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1465       || bfd_bread (compressed_buffer,
1466                     compressed_size, abfd) != compressed_size)
1467     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1468            bfd_get_filename (abfd));
1469
1470   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1471      by the uncompressed section size, 8 bytes in big-endian order.  */
1472   if (compressed_size < header_size
1473       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1474     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1475            bfd_get_filename (abfd));
1476   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1477   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1478   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1479   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1480   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1481   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1482   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1483   uncompressed_size += compressed_buffer[11];
1484
1485   /* It is possible the section consists of several compressed
1486      buffers concatenated together, so we uncompress in a loop.  */
1487   strm.zalloc = NULL;
1488   strm.zfree = NULL;
1489   strm.opaque = NULL;
1490   strm.avail_in = compressed_size - header_size;
1491   strm.next_in = (Bytef*) compressed_buffer + header_size;
1492   strm.avail_out = uncompressed_size;
1493   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1494                                        uncompressed_size);
1495   rc = inflateInit (&strm);
1496   while (strm.avail_in > 0)
1497     {
1498       if (rc != Z_OK)
1499         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1500                bfd_get_filename (abfd), rc);
1501       strm.next_out = ((Bytef*) uncompressed_buffer
1502                        + (uncompressed_size - strm.avail_out));
1503       rc = inflate (&strm, Z_FINISH);
1504       if (rc != Z_STREAM_END)
1505         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1506                bfd_get_filename (abfd), rc);
1507       rc = inflateReset (&strm);
1508     }
1509   rc = inflateEnd (&strm);
1510   if (rc != Z_OK
1511       || strm.avail_out != 0)
1512     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1513            bfd_get_filename (abfd), rc);
1514
1515   do_cleanups (cleanup);
1516   *outbuf = uncompressed_buffer;
1517   *outsize = uncompressed_size;
1518 #endif
1519 }
1520
1521 /* A helper function that decides whether a section is empty.  */
1522
1523 static int
1524 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1525 {
1526   return info->asection == NULL || info->size == 0;
1527 }
1528
1529 /* Read the contents of the section SECTP from object file specified by
1530    OBJFILE, store info about the section into INFO.
1531    If the section is compressed, uncompress it before returning.  */
1532
1533 static void
1534 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1535 {
1536   bfd *abfd = objfile->obfd;
1537   asection *sectp = info->asection;
1538   gdb_byte *buf, *retbuf;
1539   unsigned char header[4];
1540
1541   if (info->readin)
1542     return;
1543   info->buffer = NULL;
1544   info->was_mmapped = 0;
1545   info->readin = 1;
1546
1547   if (dwarf2_section_empty_p (info))
1548     return;
1549
1550   /* Check if the file has a 4-byte header indicating compression.  */
1551   if (info->size > sizeof (header)
1552       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1553       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1554     {
1555       /* Upon decompression, update the buffer and its size.  */
1556       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1557         {
1558           zlib_decompress_section (objfile, sectp, &info->buffer,
1559                                    &info->size);
1560           return;
1561         }
1562     }
1563
1564 #ifdef HAVE_MMAP
1565   if (pagesize == 0)
1566     pagesize = getpagesize ();
1567
1568   /* Only try to mmap sections which are large enough: we don't want to
1569      waste space due to fragmentation.  Also, only try mmap for sections
1570      without relocations.  */
1571
1572   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1573     {
1574       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1575       size_t map_length = info->size + sectp->filepos - pg_offset;
1576       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1577                                  MAP_PRIVATE, pg_offset);
1578
1579       if (retbuf != MAP_FAILED)
1580         {
1581           info->was_mmapped = 1;
1582           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1583 #if HAVE_POSIX_MADVISE
1584           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1585 #endif
1586           return;
1587         }
1588     }
1589 #endif
1590
1591   /* If we get here, we are a normal, not-compressed section.  */
1592   info->buffer = buf
1593     = obstack_alloc (&objfile->objfile_obstack, info->size);
1594
1595   /* When debugging .o files, we may need to apply relocations; see
1596      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1597      We never compress sections in .o files, so we only need to
1598      try this when the section is not compressed.  */
1599   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1600   if (retbuf != NULL)
1601     {
1602       info->buffer = retbuf;
1603       return;
1604     }
1605
1606   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1607       || bfd_bread (buf, info->size, abfd) != info->size)
1608     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1609            bfd_get_filename (abfd));
1610 }
1611
1612 /* A helper function that returns the size of a section in a safe way.
1613    If you are positive that the section has been read before using the
1614    size, then it is safe to refer to the dwarf2_section_info object's
1615    "size" field directly.  In other cases, you must call this
1616    function, because for compressed sections the size field is not set
1617    correctly until the section has been read.  */
1618
1619 static bfd_size_type
1620 dwarf2_section_size (struct objfile *objfile,
1621                      struct dwarf2_section_info *info)
1622 {
1623   if (!info->readin)
1624     dwarf2_read_section (objfile, info);
1625   return info->size;
1626 }
1627
1628 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1629    SECTION_NAME.  */
1630
1631 void
1632 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1633                          asection **sectp, gdb_byte **bufp,
1634                          bfd_size_type *sizep)
1635 {
1636   struct dwarf2_per_objfile *data
1637     = objfile_data (objfile, dwarf2_objfile_data_key);
1638   struct dwarf2_section_info *info;
1639
1640   /* We may see an objfile without any DWARF, in which case we just
1641      return nothing.  */
1642   if (data == NULL)
1643     {
1644       *sectp = NULL;
1645       *bufp = NULL;
1646       *sizep = 0;
1647       return;
1648     }
1649   if (section_is_p (section_name, EH_FRAME_SECTION))
1650     info = &data->eh_frame;
1651   else if (section_is_p (section_name, FRAME_SECTION))
1652     info = &data->frame;
1653   else
1654     gdb_assert_not_reached ("unexpected section");
1655
1656   dwarf2_read_section (objfile, info);
1657
1658   *sectp = info->asection;
1659   *bufp = info->buffer;
1660   *sizep = info->size;
1661 }
1662
1663 \f
1664 /* DWARF quick_symbols_functions support.  */
1665
1666 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1667    unique line tables, so we maintain a separate table of all .debug_line
1668    derived entries to support the sharing.
1669    All the quick functions need is the list of file names.  We discard the
1670    line_header when we're done and don't need to record it here.  */
1671 struct quick_file_names
1672 {
1673   /* The offset in .debug_line of the line table.  We hash on this.  */
1674   unsigned int offset;
1675
1676   /* The number of entries in file_names, real_names.  */
1677   unsigned int num_file_names;
1678
1679   /* The file names from the line table, after being run through
1680      file_full_name.  */
1681   const char **file_names;
1682
1683   /* The file names from the line table after being run through
1684      gdb_realpath.  These are computed lazily.  */
1685   const char **real_names;
1686 };
1687
1688 /* When using the index (and thus not using psymtabs), each CU has an
1689    object of this type.  This is used to hold information needed by
1690    the various "quick" methods.  */
1691 struct dwarf2_per_cu_quick_data
1692 {
1693   /* The file table.  This can be NULL if there was no file table
1694      or it's currently not read in.
1695      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1696   struct quick_file_names *file_names;
1697
1698   /* The corresponding symbol table.  This is NULL if symbols for this
1699      CU have not yet been read.  */
1700   struct symtab *symtab;
1701
1702   /* A temporary mark bit used when iterating over all CUs in
1703      expand_symtabs_matching.  */
1704   unsigned int mark : 1;
1705
1706   /* True if we've tried to read the file table and found there isn't one.
1707      There will be no point in trying to read it again next time.  */
1708   unsigned int no_file_data : 1;
1709 };
1710
1711 /* Hash function for a quick_file_names.  */
1712
1713 static hashval_t
1714 hash_file_name_entry (const void *e)
1715 {
1716   const struct quick_file_names *file_data = e;
1717
1718   return file_data->offset;
1719 }
1720
1721 /* Equality function for a quick_file_names.  */
1722
1723 static int
1724 eq_file_name_entry (const void *a, const void *b)
1725 {
1726   const struct quick_file_names *ea = a;
1727   const struct quick_file_names *eb = b;
1728
1729   return ea->offset == eb->offset;
1730 }
1731
1732 /* Delete function for a quick_file_names.  */
1733
1734 static void
1735 delete_file_name_entry (void *e)
1736 {
1737   struct quick_file_names *file_data = e;
1738   int i;
1739
1740   for (i = 0; i < file_data->num_file_names; ++i)
1741     {
1742       xfree ((void*) file_data->file_names[i]);
1743       if (file_data->real_names)
1744         xfree ((void*) file_data->real_names[i]);
1745     }
1746
1747   /* The space for the struct itself lives on objfile_obstack,
1748      so we don't free it here.  */
1749 }
1750
1751 /* Create a quick_file_names hash table.  */
1752
1753 static htab_t
1754 create_quick_file_names_table (unsigned int nr_initial_entries)
1755 {
1756   return htab_create_alloc (nr_initial_entries,
1757                             hash_file_name_entry, eq_file_name_entry,
1758                             delete_file_name_entry, xcalloc, xfree);
1759 }
1760
1761 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1762    this CU came.  */
1763
1764 static void
1765 dw2_do_instantiate_symtab (struct objfile *objfile,
1766                            struct dwarf2_per_cu_data *per_cu)
1767 {
1768   struct cleanup *back_to;
1769
1770   back_to = make_cleanup (dwarf2_release_queue, NULL);
1771
1772   queue_comp_unit (per_cu, objfile);
1773
1774   if (per_cu->from_debug_types)
1775     read_signatured_type_at_offset (objfile, per_cu->offset);
1776   else
1777     load_full_comp_unit (per_cu, objfile);
1778
1779   process_queue (objfile);
1780
1781   /* Age the cache, releasing compilation units that have not
1782      been used recently.  */
1783   age_cached_comp_units ();
1784
1785   do_cleanups (back_to);
1786 }
1787
1788 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1789    the objfile from which this CU came.  Returns the resulting symbol
1790    table.  */
1791
1792 static struct symtab *
1793 dw2_instantiate_symtab (struct objfile *objfile,
1794                         struct dwarf2_per_cu_data *per_cu)
1795 {
1796   if (!per_cu->v.quick->symtab)
1797     {
1798       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1799       increment_reading_symtab ();
1800       dw2_do_instantiate_symtab (objfile, per_cu);
1801       do_cleanups (back_to);
1802     }
1803   return per_cu->v.quick->symtab;
1804 }
1805
1806 /* Return the CU given its index.  */
1807
1808 static struct dwarf2_per_cu_data *
1809 dw2_get_cu (int index)
1810 {
1811   if (index >= dwarf2_per_objfile->n_comp_units)
1812     {
1813       index -= dwarf2_per_objfile->n_comp_units;
1814       return dwarf2_per_objfile->type_comp_units[index];
1815     }
1816   return dwarf2_per_objfile->all_comp_units[index];
1817 }
1818
1819 /* A helper function that knows how to read a 64-bit value in a way
1820    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1821    otherwise.  */
1822
1823 static int
1824 extract_cu_value (const char *bytes, ULONGEST *result)
1825 {
1826   if (sizeof (ULONGEST) < 8)
1827     {
1828       int i;
1829
1830       /* Ignore the upper 4 bytes if they are all zero.  */
1831       for (i = 0; i < 4; ++i)
1832         if (bytes[i + 4] != 0)
1833           return 0;
1834
1835       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1836     }
1837   else
1838     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1839   return 1;
1840 }
1841
1842 /* Read the CU list from the mapped index, and use it to create all
1843    the CU objects for this objfile.  Return 0 if something went wrong,
1844    1 if everything went ok.  */
1845
1846 static int
1847 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1848                        offset_type cu_list_elements)
1849 {
1850   offset_type i;
1851
1852   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1853   dwarf2_per_objfile->all_comp_units
1854     = obstack_alloc (&objfile->objfile_obstack,
1855                      dwarf2_per_objfile->n_comp_units
1856                      * sizeof (struct dwarf2_per_cu_data *));
1857
1858   for (i = 0; i < cu_list_elements; i += 2)
1859     {
1860       struct dwarf2_per_cu_data *the_cu;
1861       ULONGEST offset, length;
1862
1863       if (!extract_cu_value (cu_list, &offset)
1864           || !extract_cu_value (cu_list + 8, &length))
1865         return 0;
1866       cu_list += 2 * 8;
1867
1868       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1869                                struct dwarf2_per_cu_data);
1870       the_cu->offset = offset;
1871       the_cu->length = length;
1872       the_cu->objfile = objfile;
1873       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1874                                         struct dwarf2_per_cu_quick_data);
1875       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1876     }
1877
1878   return 1;
1879 }
1880
1881 /* Create the signatured type hash table from the index.  */
1882
1883 static int
1884 create_signatured_type_table_from_index (struct objfile *objfile,
1885                                          const gdb_byte *bytes,
1886                                          offset_type elements)
1887 {
1888   offset_type i;
1889   htab_t sig_types_hash;
1890
1891   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1892   dwarf2_per_objfile->type_comp_units
1893     = obstack_alloc (&objfile->objfile_obstack,
1894                      dwarf2_per_objfile->n_type_comp_units
1895                      * sizeof (struct dwarf2_per_cu_data *));
1896
1897   sig_types_hash = allocate_signatured_type_table (objfile);
1898
1899   for (i = 0; i < elements; i += 3)
1900     {
1901       struct signatured_type *type_sig;
1902       ULONGEST offset, type_offset, signature;
1903       void **slot;
1904
1905       if (!extract_cu_value (bytes, &offset)
1906           || !extract_cu_value (bytes + 8, &type_offset))
1907         return 0;
1908       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1909       bytes += 3 * 8;
1910
1911       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1912                                  struct signatured_type);
1913       type_sig->signature = signature;
1914       type_sig->offset = offset;
1915       type_sig->type_offset = type_offset;
1916       type_sig->per_cu.from_debug_types = 1;
1917       type_sig->per_cu.offset = offset;
1918       type_sig->per_cu.objfile = objfile;
1919       type_sig->per_cu.v.quick
1920         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1921                           struct dwarf2_per_cu_quick_data);
1922
1923       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1924       *slot = type_sig;
1925
1926       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1927     }
1928
1929   dwarf2_per_objfile->signatured_types = sig_types_hash;
1930
1931   return 1;
1932 }
1933
1934 /* Read the address map data from the mapped index, and use it to
1935    populate the objfile's psymtabs_addrmap.  */
1936
1937 static void
1938 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1939 {
1940   const gdb_byte *iter, *end;
1941   struct obstack temp_obstack;
1942   struct addrmap *mutable_map;
1943   struct cleanup *cleanup;
1944   CORE_ADDR baseaddr;
1945
1946   obstack_init (&temp_obstack);
1947   cleanup = make_cleanup_obstack_free (&temp_obstack);
1948   mutable_map = addrmap_create_mutable (&temp_obstack);
1949
1950   iter = index->address_table;
1951   end = iter + index->address_table_size;
1952
1953   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1954
1955   while (iter < end)
1956     {
1957       ULONGEST hi, lo, cu_index;
1958       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1959       iter += 8;
1960       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1961       iter += 8;
1962       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1963       iter += 4;
1964       
1965       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1966                          dw2_get_cu (cu_index));
1967     }
1968
1969   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1970                                                     &objfile->objfile_obstack);
1971   do_cleanups (cleanup);
1972 }
1973
1974 /* The hash function for strings in the mapped index.  This is the same as
1975    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1976    implementation.  This is necessary because the hash function is tied to the
1977    format of the mapped index file.  The hash values do not have to match with
1978    SYMBOL_HASH_NEXT.
1979    
1980    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
1981
1982 static hashval_t
1983 mapped_index_string_hash (int index_version, const void *p)
1984 {
1985   const unsigned char *str = (const unsigned char *) p;
1986   hashval_t r = 0;
1987   unsigned char c;
1988
1989   while ((c = *str++) != 0)
1990     {
1991       if (index_version >= 5)
1992         c = tolower (c);
1993       r = r * 67 + c - 113;
1994     }
1995
1996   return r;
1997 }
1998
1999 /* Find a slot in the mapped index INDEX for the object named NAME.
2000    If NAME is found, set *VEC_OUT to point to the CU vector in the
2001    constant pool and return 1.  If NAME cannot be found, return 0.  */
2002
2003 static int
2004 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2005                           offset_type **vec_out)
2006 {
2007   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2008   offset_type hash;
2009   offset_type slot, step;
2010   int (*cmp) (const char *, const char *);
2011
2012   if (current_language->la_language == language_cplus
2013       || current_language->la_language == language_java
2014       || current_language->la_language == language_fortran)
2015     {
2016       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2017          not contain any.  */
2018       const char *paren = strchr (name, '(');
2019
2020       if (paren)
2021         {
2022           char *dup;
2023
2024           dup = xmalloc (paren - name + 1);
2025           memcpy (dup, name, paren - name);
2026           dup[paren - name] = 0;
2027
2028           make_cleanup (xfree, dup);
2029           name = dup;
2030         }
2031     }
2032
2033   /* Index version 4 did not support case insensitive searches.  But the
2034      indexes for case insensitive languages are built in lowercase, therefore
2035      simulate our NAME being searched is also lowercased.  */
2036   hash = mapped_index_string_hash ((index->version == 4
2037                                     && case_sensitivity == case_sensitive_off
2038                                     ? 5 : index->version),
2039                                    name);
2040
2041   slot = hash & (index->symbol_table_slots - 1);
2042   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2043   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2044
2045   for (;;)
2046     {
2047       /* Convert a slot number to an offset into the table.  */
2048       offset_type i = 2 * slot;
2049       const char *str;
2050       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2051         {
2052           do_cleanups (back_to);
2053           return 0;
2054         }
2055
2056       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2057       if (!cmp (name, str))
2058         {
2059           *vec_out = (offset_type *) (index->constant_pool
2060                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2061           do_cleanups (back_to);
2062           return 1;
2063         }
2064
2065       slot = (slot + step) & (index->symbol_table_slots - 1);
2066     }
2067 }
2068
2069 /* Read the index file.  If everything went ok, initialize the "quick"
2070    elements of all the CUs and return 1.  Otherwise, return 0.  */
2071
2072 static int
2073 dwarf2_read_index (struct objfile *objfile)
2074 {
2075   char *addr;
2076   struct mapped_index *map;
2077   offset_type *metadata;
2078   const gdb_byte *cu_list;
2079   const gdb_byte *types_list = NULL;
2080   offset_type version, cu_list_elements;
2081   offset_type types_list_elements = 0;
2082   int i;
2083
2084   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2085     return 0;
2086
2087   /* Older elfutils strip versions could keep the section in the main
2088      executable while splitting it for the separate debug info file.  */
2089   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2090        & SEC_HAS_CONTENTS) == 0)
2091     return 0;
2092
2093   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2094
2095   addr = dwarf2_per_objfile->gdb_index.buffer;
2096   /* Version check.  */
2097   version = MAYBE_SWAP (*(offset_type *) addr);
2098   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2099      causes the index to behave very poorly for certain requests.  Version 3
2100      contained incomplete addrmap.  So, it seems better to just ignore such
2101      indices.  Index version 4 uses a different hash function than index
2102      version 5 and later.  */
2103   if (version < 4)
2104     return 0;
2105   /* Indexes with higher version than the one supported by GDB may be no
2106      longer backward compatible.  */
2107   if (version > 5)
2108     return 0;
2109
2110   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2111   map->version = version;
2112   map->total_size = dwarf2_per_objfile->gdb_index.size;
2113
2114   metadata = (offset_type *) (addr + sizeof (offset_type));
2115
2116   i = 0;
2117   cu_list = addr + MAYBE_SWAP (metadata[i]);
2118   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2119                       / 8);
2120   ++i;
2121
2122   types_list = addr + MAYBE_SWAP (metadata[i]);
2123   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2124                           - MAYBE_SWAP (metadata[i]))
2125                          / 8);
2126   ++i;
2127
2128   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2129   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2130                              - MAYBE_SWAP (metadata[i]));
2131   ++i;
2132
2133   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2134   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2135                               - MAYBE_SWAP (metadata[i]))
2136                              / (2 * sizeof (offset_type)));
2137   ++i;
2138
2139   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2140
2141   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2142     return 0;
2143
2144   if (types_list_elements
2145       && !create_signatured_type_table_from_index (objfile, types_list,
2146                                                    types_list_elements))
2147     return 0;
2148
2149   create_addrmap_from_index (objfile, map);
2150
2151   dwarf2_per_objfile->index_table = map;
2152   dwarf2_per_objfile->using_index = 1;
2153   dwarf2_per_objfile->quick_file_names_table =
2154     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2155
2156   return 1;
2157 }
2158
2159 /* A helper for the "quick" functions which sets the global
2160    dwarf2_per_objfile according to OBJFILE.  */
2161
2162 static void
2163 dw2_setup (struct objfile *objfile)
2164 {
2165   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2166   gdb_assert (dwarf2_per_objfile);
2167 }
2168
2169 /* A helper for the "quick" functions which attempts to read the line
2170    table for THIS_CU.  */
2171
2172 static struct quick_file_names *
2173 dw2_get_file_names (struct objfile *objfile,
2174                     struct dwarf2_per_cu_data *this_cu)
2175 {
2176   bfd *abfd = objfile->obfd;
2177   struct line_header *lh;
2178   struct attribute *attr;
2179   struct cleanup *cleanups;
2180   struct die_info *comp_unit_die;
2181   struct dwarf2_section_info* sec;
2182   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2183   int has_children, i;
2184   struct dwarf2_cu cu;
2185   unsigned int bytes_read, buffer_size;
2186   struct die_reader_specs reader_specs;
2187   char *name, *comp_dir;
2188   void **slot;
2189   struct quick_file_names *qfn;
2190   unsigned int line_offset;
2191
2192   if (this_cu->v.quick->file_names != NULL)
2193     return this_cu->v.quick->file_names;
2194   /* If we know there is no line data, no point in looking again.  */
2195   if (this_cu->v.quick->no_file_data)
2196     return NULL;
2197
2198   init_one_comp_unit (&cu, objfile);
2199   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2200
2201   if (this_cu->from_debug_types)
2202     sec = &dwarf2_per_objfile->types;
2203   else
2204     sec = &dwarf2_per_objfile->info;
2205   dwarf2_read_section (objfile, sec);
2206   buffer_size = sec->size;
2207   buffer = sec->buffer;
2208   info_ptr = buffer + this_cu->offset;
2209   beg_of_comp_unit = info_ptr;
2210
2211   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2212                                           buffer, buffer_size,
2213                                           abfd);
2214
2215   /* Complete the cu_header.  */
2216   cu.header.offset = beg_of_comp_unit - buffer;
2217   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2218
2219   this_cu->cu = &cu;
2220   cu.per_cu = this_cu;
2221
2222   dwarf2_read_abbrevs (abfd, &cu);
2223   make_cleanup (dwarf2_free_abbrev_table, &cu);
2224
2225   if (this_cu->from_debug_types)
2226     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2227   init_cu_die_reader (&reader_specs, &cu);
2228   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2229                  &has_children);
2230
2231   lh = NULL;
2232   slot = NULL;
2233   line_offset = 0;
2234   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2235   if (attr)
2236     {
2237       struct quick_file_names find_entry;
2238
2239       line_offset = DW_UNSND (attr);
2240
2241       /* We may have already read in this line header (TU line header sharing).
2242          If we have we're done.  */
2243       find_entry.offset = line_offset;
2244       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2245                              &find_entry, INSERT);
2246       if (*slot != NULL)
2247         {
2248           do_cleanups (cleanups);
2249           this_cu->v.quick->file_names = *slot;
2250           return *slot;
2251         }
2252
2253       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2254     }
2255   if (lh == NULL)
2256     {
2257       do_cleanups (cleanups);
2258       this_cu->v.quick->no_file_data = 1;
2259       return NULL;
2260     }
2261
2262   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2263   qfn->offset = line_offset;
2264   gdb_assert (slot != NULL);
2265   *slot = qfn;
2266
2267   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2268
2269   qfn->num_file_names = lh->num_file_names;
2270   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2271                                    lh->num_file_names * sizeof (char *));
2272   for (i = 0; i < lh->num_file_names; ++i)
2273     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2274   qfn->real_names = NULL;
2275
2276   free_line_header (lh);
2277   do_cleanups (cleanups);
2278
2279   this_cu->v.quick->file_names = qfn;
2280   return qfn;
2281 }
2282
2283 /* A helper for the "quick" functions which computes and caches the
2284    real path for a given file name from the line table.  */
2285
2286 static const char *
2287 dw2_get_real_path (struct objfile *objfile,
2288                    struct quick_file_names *qfn, int index)
2289 {
2290   if (qfn->real_names == NULL)
2291     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2292                                       qfn->num_file_names, sizeof (char *));
2293
2294   if (qfn->real_names[index] == NULL)
2295     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2296
2297   return qfn->real_names[index];
2298 }
2299
2300 static struct symtab *
2301 dw2_find_last_source_symtab (struct objfile *objfile)
2302 {
2303   int index;
2304
2305   dw2_setup (objfile);
2306   index = dwarf2_per_objfile->n_comp_units - 1;
2307   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2308 }
2309
2310 /* Traversal function for dw2_forget_cached_source_info.  */
2311
2312 static int
2313 dw2_free_cached_file_names (void **slot, void *info)
2314 {
2315   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2316
2317   if (file_data->real_names)
2318     {
2319       int i;
2320
2321       for (i = 0; i < file_data->num_file_names; ++i)
2322         {
2323           xfree ((void*) file_data->real_names[i]);
2324           file_data->real_names[i] = NULL;
2325         }
2326     }
2327
2328   return 1;
2329 }
2330
2331 static void
2332 dw2_forget_cached_source_info (struct objfile *objfile)
2333 {
2334   dw2_setup (objfile);
2335
2336   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2337                           dw2_free_cached_file_names, NULL);
2338 }
2339
2340 static int
2341 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2342                    const char *full_path, const char *real_path,
2343                    struct symtab **result)
2344 {
2345   int i;
2346   int check_basename = lbasename (name) == name;
2347   struct dwarf2_per_cu_data *base_cu = NULL;
2348
2349   dw2_setup (objfile);
2350
2351   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2352                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2353     {
2354       int j;
2355       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2356       struct quick_file_names *file_data;
2357
2358       if (per_cu->v.quick->symtab)
2359         continue;
2360
2361       file_data = dw2_get_file_names (objfile, per_cu);
2362       if (file_data == NULL)
2363         continue;
2364
2365       for (j = 0; j < file_data->num_file_names; ++j)
2366         {
2367           const char *this_name = file_data->file_names[j];
2368
2369           if (FILENAME_CMP (name, this_name) == 0)
2370             {
2371               *result = dw2_instantiate_symtab (objfile, per_cu);
2372               return 1;
2373             }
2374
2375           if (check_basename && ! base_cu
2376               && FILENAME_CMP (lbasename (this_name), name) == 0)
2377             base_cu = per_cu;
2378
2379           if (full_path != NULL)
2380             {
2381               const char *this_real_name = dw2_get_real_path (objfile,
2382                                                               file_data, j);
2383
2384               if (this_real_name != NULL
2385                   && FILENAME_CMP (full_path, this_real_name) == 0)
2386                 {
2387                   *result = dw2_instantiate_symtab (objfile, per_cu);
2388                   return 1;
2389                 }
2390             }
2391
2392           if (real_path != NULL)
2393             {
2394               const char *this_real_name = dw2_get_real_path (objfile,
2395                                                               file_data, j);
2396
2397               if (this_real_name != NULL
2398                   && FILENAME_CMP (real_path, this_real_name) == 0)
2399                 {
2400                   *result = dw2_instantiate_symtab (objfile, per_cu);
2401                   return 1;
2402                 }
2403             }
2404         }
2405     }
2406
2407   if (base_cu)
2408     {
2409       *result = dw2_instantiate_symtab (objfile, base_cu);
2410       return 1;
2411     }
2412
2413   return 0;
2414 }
2415
2416 static struct symtab *
2417 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2418                    const char *name, domain_enum domain)
2419 {
2420   /* We do all the work in the pre_expand_symtabs_matching hook
2421      instead.  */
2422   return NULL;
2423 }
2424
2425 /* A helper function that expands all symtabs that hold an object
2426    named NAME.  */
2427
2428 static void
2429 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2430 {
2431   dw2_setup (objfile);
2432
2433   /* index_table is NULL if OBJF_READNOW.  */
2434   if (dwarf2_per_objfile->index_table)
2435     {
2436       offset_type *vec;
2437
2438       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2439                                     name, &vec))
2440         {
2441           offset_type i, len = MAYBE_SWAP (*vec);
2442           for (i = 0; i < len; ++i)
2443             {
2444               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2445               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2446
2447               dw2_instantiate_symtab (objfile, per_cu);
2448             }
2449         }
2450     }
2451 }
2452
2453 static void
2454 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2455                                  enum block_enum block_kind, const char *name,
2456                                  domain_enum domain)
2457 {
2458   dw2_do_expand_symtabs_matching (objfile, name);
2459 }
2460
2461 static void
2462 dw2_print_stats (struct objfile *objfile)
2463 {
2464   int i, count;
2465
2466   dw2_setup (objfile);
2467   count = 0;
2468   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2469                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2470     {
2471       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2472
2473       if (!per_cu->v.quick->symtab)
2474         ++count;
2475     }
2476   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2477 }
2478
2479 static void
2480 dw2_dump (struct objfile *objfile)
2481 {
2482   /* Nothing worth printing.  */
2483 }
2484
2485 static void
2486 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2487               struct section_offsets *delta)
2488 {
2489   /* There's nothing to relocate here.  */
2490 }
2491
2492 static void
2493 dw2_expand_symtabs_for_function (struct objfile *objfile,
2494                                  const char *func_name)
2495 {
2496   dw2_do_expand_symtabs_matching (objfile, func_name);
2497 }
2498
2499 static void
2500 dw2_expand_all_symtabs (struct objfile *objfile)
2501 {
2502   int i;
2503
2504   dw2_setup (objfile);
2505
2506   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2507                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2508     {
2509       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2510
2511       dw2_instantiate_symtab (objfile, per_cu);
2512     }
2513 }
2514
2515 static void
2516 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2517                                   const char *filename)
2518 {
2519   int i;
2520
2521   dw2_setup (objfile);
2522
2523   /* We don't need to consider type units here.
2524      This is only called for examining code, e.g. expand_line_sal.
2525      There can be an order of magnitude (or more) more type units
2526      than comp units, and we avoid them if we can.  */
2527
2528   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2529     {
2530       int j;
2531       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2532       struct quick_file_names *file_data;
2533
2534       if (per_cu->v.quick->symtab)
2535         continue;
2536
2537       file_data = dw2_get_file_names (objfile, per_cu);
2538       if (file_data == NULL)
2539         continue;
2540
2541       for (j = 0; j < file_data->num_file_names; ++j)
2542         {
2543           const char *this_name = file_data->file_names[j];
2544           if (FILENAME_CMP (this_name, filename) == 0)
2545             {
2546               dw2_instantiate_symtab (objfile, per_cu);
2547               break;
2548             }
2549         }
2550     }
2551 }
2552
2553 static const char *
2554 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2555 {
2556   struct dwarf2_per_cu_data *per_cu;
2557   offset_type *vec;
2558   struct quick_file_names *file_data;
2559
2560   dw2_setup (objfile);
2561
2562   /* index_table is NULL if OBJF_READNOW.  */
2563   if (!dwarf2_per_objfile->index_table)
2564     return NULL;
2565
2566   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2567                                  name, &vec))
2568     return NULL;
2569
2570   /* Note that this just looks at the very first one named NAME -- but
2571      actually we are looking for a function.  find_main_filename
2572      should be rewritten so that it doesn't require a custom hook.  It
2573      could just use the ordinary symbol tables.  */
2574   /* vec[0] is the length, which must always be >0.  */
2575   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2576
2577   file_data = dw2_get_file_names (objfile, per_cu);
2578   if (file_data == NULL)
2579     return NULL;
2580
2581   return file_data->file_names[file_data->num_file_names - 1];
2582 }
2583
2584 static void
2585 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2586                           struct objfile *objfile, int global,
2587                           int (*callback) (struct block *,
2588                                            struct symbol *, void *),
2589                           void *data, symbol_compare_ftype *match,
2590                           symbol_compare_ftype *ordered_compare)
2591 {
2592   /* Currently unimplemented; used for Ada.  The function can be called if the
2593      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2594      does not look for non-Ada symbols this function should just return.  */
2595 }
2596
2597 static void
2598 dw2_expand_symtabs_matching (struct objfile *objfile,
2599                              int (*file_matcher) (const char *, void *),
2600                              int (*name_matcher) (const char *, void *),
2601                              enum search_domain kind,
2602                              void *data)
2603 {
2604   int i;
2605   offset_type iter;
2606   struct mapped_index *index;
2607
2608   dw2_setup (objfile);
2609
2610   /* index_table is NULL if OBJF_READNOW.  */
2611   if (!dwarf2_per_objfile->index_table)
2612     return;
2613   index = dwarf2_per_objfile->index_table;
2614
2615   if (file_matcher != NULL)
2616     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2617                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2618       {
2619         int j;
2620         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2621         struct quick_file_names *file_data;
2622
2623         per_cu->v.quick->mark = 0;
2624         if (per_cu->v.quick->symtab)
2625           continue;
2626
2627         file_data = dw2_get_file_names (objfile, per_cu);
2628         if (file_data == NULL)
2629           continue;
2630
2631         for (j = 0; j < file_data->num_file_names; ++j)
2632           {
2633             if (file_matcher (file_data->file_names[j], data))
2634               {
2635                 per_cu->v.quick->mark = 1;
2636                 break;
2637               }
2638           }
2639       }
2640
2641   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2642     {
2643       offset_type idx = 2 * iter;
2644       const char *name;
2645       offset_type *vec, vec_len, vec_idx;
2646
2647       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2648         continue;
2649
2650       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2651
2652       if (! (*name_matcher) (name, data))
2653         continue;
2654
2655       /* The name was matched, now expand corresponding CUs that were
2656          marked.  */
2657       vec = (offset_type *) (index->constant_pool
2658                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2659       vec_len = MAYBE_SWAP (vec[0]);
2660       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2661         {
2662           struct dwarf2_per_cu_data *per_cu;
2663
2664           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2665           if (file_matcher == NULL || per_cu->v.quick->mark)
2666             dw2_instantiate_symtab (objfile, per_cu);
2667         }
2668     }
2669 }
2670
2671 static struct symtab *
2672 dw2_find_pc_sect_symtab (struct objfile *objfile,
2673                          struct minimal_symbol *msymbol,
2674                          CORE_ADDR pc,
2675                          struct obj_section *section,
2676                          int warn_if_readin)
2677 {
2678   struct dwarf2_per_cu_data *data;
2679
2680   dw2_setup (objfile);
2681
2682   if (!objfile->psymtabs_addrmap)
2683     return NULL;
2684
2685   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2686   if (!data)
2687     return NULL;
2688
2689   if (warn_if_readin && data->v.quick->symtab)
2690     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2691              paddress (get_objfile_arch (objfile), pc));
2692
2693   return dw2_instantiate_symtab (objfile, data);
2694 }
2695
2696 static void
2697 dw2_map_symbol_filenames (struct objfile *objfile,
2698                           void (*fun) (const char *, const char *, void *),
2699                           void *data)
2700 {
2701   int i;
2702
2703   dw2_setup (objfile);
2704
2705   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2706                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2707     {
2708       int j;
2709       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2710       struct quick_file_names *file_data;
2711
2712       if (per_cu->v.quick->symtab)
2713         continue;
2714
2715       file_data = dw2_get_file_names (objfile, per_cu);
2716       if (file_data == NULL)
2717         continue;
2718
2719       for (j = 0; j < file_data->num_file_names; ++j)
2720         {
2721           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2722                                                           j);
2723           (*fun) (file_data->file_names[j], this_real_name, data);
2724         }
2725     }
2726 }
2727
2728 static int
2729 dw2_has_symbols (struct objfile *objfile)
2730 {
2731   return 1;
2732 }
2733
2734 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2735 {
2736   dw2_has_symbols,
2737   dw2_find_last_source_symtab,
2738   dw2_forget_cached_source_info,
2739   dw2_lookup_symtab,
2740   dw2_lookup_symbol,
2741   dw2_pre_expand_symtabs_matching,
2742   dw2_print_stats,
2743   dw2_dump,
2744   dw2_relocate,
2745   dw2_expand_symtabs_for_function,
2746   dw2_expand_all_symtabs,
2747   dw2_expand_symtabs_with_filename,
2748   dw2_find_symbol_file,
2749   dw2_map_matching_symbols,
2750   dw2_expand_symtabs_matching,
2751   dw2_find_pc_sect_symtab,
2752   dw2_map_symbol_filenames
2753 };
2754
2755 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2756    file will use psymtabs, or 1 if using the GNU index.  */
2757
2758 int
2759 dwarf2_initialize_objfile (struct objfile *objfile)
2760 {
2761   /* If we're about to read full symbols, don't bother with the
2762      indices.  In this case we also don't care if some other debug
2763      format is making psymtabs, because they are all about to be
2764      expanded anyway.  */
2765   if ((objfile->flags & OBJF_READNOW))
2766     {
2767       int i;
2768
2769       dwarf2_per_objfile->using_index = 1;
2770       create_all_comp_units (objfile);
2771       create_debug_types_hash_table (objfile);
2772       dwarf2_per_objfile->quick_file_names_table =
2773         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2774
2775       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2776                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2777         {
2778           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2779
2780           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2781                                             struct dwarf2_per_cu_quick_data);
2782         }
2783
2784       /* Return 1 so that gdb sees the "quick" functions.  However,
2785          these functions will be no-ops because we will have expanded
2786          all symtabs.  */
2787       return 1;
2788     }
2789
2790   if (dwarf2_read_index (objfile))
2791     return 1;
2792
2793   return 0;
2794 }
2795
2796 \f
2797
2798 /* Build a partial symbol table.  */
2799
2800 void
2801 dwarf2_build_psymtabs (struct objfile *objfile)
2802 {
2803   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2804     {
2805       init_psymbol_list (objfile, 1024);
2806     }
2807
2808   dwarf2_build_psymtabs_hard (objfile);
2809 }
2810
2811 /* Return TRUE if OFFSET is within CU_HEADER.  */
2812
2813 static inline int
2814 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2815 {
2816   unsigned int bottom = cu_header->offset;
2817   unsigned int top = (cu_header->offset
2818                       + cu_header->length
2819                       + cu_header->initial_length_size);
2820
2821   return (offset >= bottom && offset < top);
2822 }
2823
2824 /* Read in the comp unit header information from the debug_info at info_ptr.
2825    NOTE: This leaves members offset, first_die_offset to be filled in
2826    by the caller.  */
2827
2828 static gdb_byte *
2829 read_comp_unit_head (struct comp_unit_head *cu_header,
2830                      gdb_byte *info_ptr, bfd *abfd)
2831 {
2832   int signed_addr;
2833   unsigned int bytes_read;
2834
2835   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2836   cu_header->initial_length_size = bytes_read;
2837   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2838   info_ptr += bytes_read;
2839   cu_header->version = read_2_bytes (abfd, info_ptr);
2840   info_ptr += 2;
2841   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2842                                           &bytes_read);
2843   info_ptr += bytes_read;
2844   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2845   info_ptr += 1;
2846   signed_addr = bfd_get_sign_extend_vma (abfd);
2847   if (signed_addr < 0)
2848     internal_error (__FILE__, __LINE__,
2849                     _("read_comp_unit_head: dwarf from non elf file"));
2850   cu_header->signed_addr_p = signed_addr;
2851
2852   return info_ptr;
2853 }
2854
2855 static gdb_byte *
2856 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2857                              gdb_byte *buffer, unsigned int buffer_size,
2858                              bfd *abfd)
2859 {
2860   gdb_byte *beg_of_comp_unit = info_ptr;
2861
2862   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2863
2864   if (header->version != 2 && header->version != 3 && header->version != 4)
2865     error (_("Dwarf Error: wrong version in compilation unit header "
2866            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2867            bfd_get_filename (abfd));
2868
2869   if (header->abbrev_offset
2870       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2871                               &dwarf2_per_objfile->abbrev))
2872     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2873            "(offset 0x%lx + 6) [in module %s]"),
2874            (long) header->abbrev_offset,
2875            (long) (beg_of_comp_unit - buffer),
2876            bfd_get_filename (abfd));
2877
2878   if (beg_of_comp_unit + header->length + header->initial_length_size
2879       > buffer + buffer_size)
2880     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2881            "(offset 0x%lx + 0) [in module %s]"),
2882            (long) header->length,
2883            (long) (beg_of_comp_unit - buffer),
2884            bfd_get_filename (abfd));
2885
2886   return info_ptr;
2887 }
2888
2889 /* Read in the types comp unit header information from .debug_types entry at
2890    types_ptr.  The result is a pointer to one past the end of the header.  */
2891
2892 static gdb_byte *
2893 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2894                           ULONGEST *signature,
2895                           gdb_byte *types_ptr, bfd *abfd)
2896 {
2897   gdb_byte *initial_types_ptr = types_ptr;
2898
2899   dwarf2_read_section (dwarf2_per_objfile->objfile,
2900                        &dwarf2_per_objfile->types);
2901   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2902
2903   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2904
2905   *signature = read_8_bytes (abfd, types_ptr);
2906   types_ptr += 8;
2907   types_ptr += cu_header->offset_size;
2908   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2909
2910   return types_ptr;
2911 }
2912
2913 /* Allocate a new partial symtab for file named NAME and mark this new
2914    partial symtab as being an include of PST.  */
2915
2916 static void
2917 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2918                                struct objfile *objfile)
2919 {
2920   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2921
2922   subpst->section_offsets = pst->section_offsets;
2923   subpst->textlow = 0;
2924   subpst->texthigh = 0;
2925
2926   subpst->dependencies = (struct partial_symtab **)
2927     obstack_alloc (&objfile->objfile_obstack,
2928                    sizeof (struct partial_symtab *));
2929   subpst->dependencies[0] = pst;
2930   subpst->number_of_dependencies = 1;
2931
2932   subpst->globals_offset = 0;
2933   subpst->n_global_syms = 0;
2934   subpst->statics_offset = 0;
2935   subpst->n_static_syms = 0;
2936   subpst->symtab = NULL;
2937   subpst->read_symtab = pst->read_symtab;
2938   subpst->readin = 0;
2939
2940   /* No private part is necessary for include psymtabs.  This property
2941      can be used to differentiate between such include psymtabs and
2942      the regular ones.  */
2943   subpst->read_symtab_private = NULL;
2944 }
2945
2946 /* Read the Line Number Program data and extract the list of files
2947    included by the source file represented by PST.  Build an include
2948    partial symtab for each of these included files.  */
2949
2950 static void
2951 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2952                                struct die_info *die,
2953                                struct partial_symtab *pst)
2954 {
2955   struct objfile *objfile = cu->objfile;
2956   bfd *abfd = objfile->obfd;
2957   struct line_header *lh = NULL;
2958   struct attribute *attr;
2959
2960   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2961   if (attr)
2962     {
2963       unsigned int line_offset = DW_UNSND (attr);
2964
2965       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2966     }
2967   if (lh == NULL)
2968     return;  /* No linetable, so no includes.  */
2969
2970   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2971   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2972
2973   free_line_header (lh);
2974 }
2975
2976 static hashval_t
2977 hash_type_signature (const void *item)
2978 {
2979   const struct signatured_type *type_sig = item;
2980
2981   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2982   return type_sig->signature;
2983 }
2984
2985 static int
2986 eq_type_signature (const void *item_lhs, const void *item_rhs)
2987 {
2988   const struct signatured_type *lhs = item_lhs;
2989   const struct signatured_type *rhs = item_rhs;
2990
2991   return lhs->signature == rhs->signature;
2992 }
2993
2994 /* Allocate a hash table for signatured types.  */
2995
2996 static htab_t
2997 allocate_signatured_type_table (struct objfile *objfile)
2998 {
2999   return htab_create_alloc_ex (41,
3000                                hash_type_signature,
3001                                eq_type_signature,
3002                                NULL,
3003                                &objfile->objfile_obstack,
3004                                hashtab_obstack_allocate,
3005                                dummy_obstack_deallocate);
3006 }
3007
3008 /* A helper function to add a signatured type CU to a list.  */
3009
3010 static int
3011 add_signatured_type_cu_to_list (void **slot, void *datum)
3012 {
3013   struct signatured_type *sigt = *slot;
3014   struct dwarf2_per_cu_data ***datap = datum;
3015
3016   **datap = &sigt->per_cu;
3017   ++*datap;
3018
3019   return 1;
3020 }
3021
3022 /* Create the hash table of all entries in the .debug_types section.
3023    The result is zero if there is an error (e.g. missing .debug_types section),
3024    otherwise non-zero.  */
3025
3026 static int
3027 create_debug_types_hash_table (struct objfile *objfile)
3028 {
3029   gdb_byte *info_ptr;
3030   htab_t types_htab;
3031   struct dwarf2_per_cu_data **iter;
3032
3033   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3034   info_ptr = dwarf2_per_objfile->types.buffer;
3035
3036   if (info_ptr == NULL)
3037     {
3038       dwarf2_per_objfile->signatured_types = NULL;
3039       return 0;
3040     }
3041
3042   types_htab = allocate_signatured_type_table (objfile);
3043
3044   if (dwarf2_die_debug)
3045     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3046
3047   while (info_ptr < dwarf2_per_objfile->types.buffer
3048          + dwarf2_per_objfile->types.size)
3049     {
3050       unsigned int offset;
3051       unsigned int offset_size;
3052       unsigned int type_offset;
3053       unsigned int length, initial_length_size;
3054       unsigned short version;
3055       ULONGEST signature;
3056       struct signatured_type *type_sig;
3057       void **slot;
3058       gdb_byte *ptr = info_ptr;
3059
3060       offset = ptr - dwarf2_per_objfile->types.buffer;
3061
3062       /* We need to read the type's signature in order to build the hash
3063          table, but we don't need to read anything else just yet.  */
3064
3065       /* Sanity check to ensure entire cu is present.  */
3066       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3067       if (ptr + length + initial_length_size
3068           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3069         {
3070           complaint (&symfile_complaints,
3071                      _("debug type entry runs off end "
3072                        "of `.debug_types' section, ignored"));
3073           break;
3074         }
3075
3076       offset_size = initial_length_size == 4 ? 4 : 8;
3077       ptr += initial_length_size;
3078       version = bfd_get_16 (objfile->obfd, ptr);
3079       ptr += 2;
3080       ptr += offset_size; /* abbrev offset */
3081       ptr += 1; /* address size */
3082       signature = bfd_get_64 (objfile->obfd, ptr);
3083       ptr += 8;
3084       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3085
3086       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3087       memset (type_sig, 0, sizeof (*type_sig));
3088       type_sig->signature = signature;
3089       type_sig->offset = offset;
3090       type_sig->type_offset = type_offset;
3091       type_sig->per_cu.objfile = objfile;
3092       type_sig->per_cu.from_debug_types = 1;
3093
3094       slot = htab_find_slot (types_htab, type_sig, INSERT);
3095       gdb_assert (slot != NULL);
3096       *slot = type_sig;
3097
3098       if (dwarf2_die_debug)
3099         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3100                             offset, phex (signature, sizeof (signature)));
3101
3102       info_ptr = info_ptr + initial_length_size + length;
3103     }
3104
3105   dwarf2_per_objfile->signatured_types = types_htab;
3106
3107   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3108   dwarf2_per_objfile->type_comp_units
3109     = obstack_alloc (&objfile->objfile_obstack,
3110                      dwarf2_per_objfile->n_type_comp_units
3111                      * sizeof (struct dwarf2_per_cu_data *));
3112   iter = &dwarf2_per_objfile->type_comp_units[0];
3113   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3114   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3115               == dwarf2_per_objfile->n_type_comp_units);
3116
3117   return 1;
3118 }
3119
3120 /* Lookup a signature based type.
3121    Returns NULL if SIG is not present in the table.  */
3122
3123 static struct signatured_type *
3124 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3125 {
3126   struct signatured_type find_entry, *entry;
3127
3128   if (dwarf2_per_objfile->signatured_types == NULL)
3129     {
3130       complaint (&symfile_complaints,
3131                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3132       return 0;
3133     }
3134
3135   find_entry.signature = sig;
3136   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3137   return entry;
3138 }
3139
3140 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3141
3142 static void
3143 init_cu_die_reader (struct die_reader_specs *reader,
3144                     struct dwarf2_cu *cu)
3145 {
3146   reader->abfd = cu->objfile->obfd;
3147   reader->cu = cu;
3148   if (cu->per_cu->from_debug_types)
3149     {
3150       gdb_assert (dwarf2_per_objfile->types.readin);
3151       reader->buffer = dwarf2_per_objfile->types.buffer;
3152     }
3153   else
3154     {
3155       gdb_assert (dwarf2_per_objfile->info.readin);
3156       reader->buffer = dwarf2_per_objfile->info.buffer;
3157     }
3158 }
3159
3160 /* Find the base address of the compilation unit for range lists and
3161    location lists.  It will normally be specified by DW_AT_low_pc.
3162    In DWARF-3 draft 4, the base address could be overridden by
3163    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3164    compilation units with discontinuous ranges.  */
3165
3166 static void
3167 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3168 {
3169   struct attribute *attr;
3170
3171   cu->base_known = 0;
3172   cu->base_address = 0;
3173
3174   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3175   if (attr)
3176     {
3177       cu->base_address = DW_ADDR (attr);
3178       cu->base_known = 1;
3179     }
3180   else
3181     {
3182       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3183       if (attr)
3184         {
3185           cu->base_address = DW_ADDR (attr);
3186           cu->base_known = 1;
3187         }
3188     }
3189 }
3190
3191 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3192    to combine the common parts.
3193    Process a compilation unit for a psymtab.
3194    BUFFER is a pointer to the beginning of the dwarf section buffer,
3195    either .debug_info or debug_types.
3196    INFO_PTR is a pointer to the start of the CU.
3197    Returns a pointer to the next CU.  */
3198
3199 static gdb_byte *
3200 process_psymtab_comp_unit (struct objfile *objfile,
3201                            struct dwarf2_per_cu_data *this_cu,
3202                            gdb_byte *buffer, gdb_byte *info_ptr,
3203                            unsigned int buffer_size)
3204 {
3205   bfd *abfd = objfile->obfd;
3206   gdb_byte *beg_of_comp_unit = info_ptr;
3207   struct die_info *comp_unit_die;
3208   struct partial_symtab *pst;
3209   CORE_ADDR baseaddr;
3210   struct cleanup *back_to_inner;
3211   struct dwarf2_cu cu;
3212   int has_children, has_pc_info;
3213   struct attribute *attr;
3214   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3215   struct die_reader_specs reader_specs;
3216   const char *filename;
3217
3218   init_one_comp_unit (&cu, objfile);
3219   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3220
3221   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3222                                           buffer, buffer_size,
3223                                           abfd);
3224
3225   /* Complete the cu_header.  */
3226   cu.header.offset = beg_of_comp_unit - buffer;
3227   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3228
3229   cu.list_in_scope = &file_symbols;
3230
3231   /* If this compilation unit was already read in, free the
3232      cached copy in order to read it in again.  This is
3233      necessary because we skipped some symbols when we first
3234      read in the compilation unit (see load_partial_dies).
3235      This problem could be avoided, but the benefit is
3236      unclear.  */
3237   if (this_cu->cu != NULL)
3238     free_one_cached_comp_unit (this_cu->cu);
3239
3240   /* Note that this is a pointer to our stack frame, being
3241      added to a global data structure.  It will be cleaned up
3242      in free_stack_comp_unit when we finish with this
3243      compilation unit.  */
3244   this_cu->cu = &cu;
3245   cu.per_cu = this_cu;
3246
3247   /* Read the abbrevs for this compilation unit into a table.  */
3248   dwarf2_read_abbrevs (abfd, &cu);
3249   make_cleanup (dwarf2_free_abbrev_table, &cu);
3250
3251   /* Read the compilation unit die.  */
3252   if (this_cu->from_debug_types)
3253     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3254   init_cu_die_reader (&reader_specs, &cu);
3255   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3256                             &has_children);
3257
3258   if (this_cu->from_debug_types)
3259     {
3260       /* offset,length haven't been set yet for type units.  */
3261       this_cu->offset = cu.header.offset;
3262       this_cu->length = cu.header.length + cu.header.initial_length_size;
3263     }
3264   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3265     {
3266       info_ptr = (beg_of_comp_unit + cu.header.length
3267                   + cu.header.initial_length_size);
3268       do_cleanups (back_to_inner);
3269       return info_ptr;
3270     }
3271
3272   prepare_one_comp_unit (&cu, comp_unit_die);
3273
3274   /* Allocate a new partial symbol table structure.  */
3275   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3276   if (attr == NULL || !DW_STRING (attr))
3277     filename = "";
3278   else
3279     filename = DW_STRING (attr);
3280   pst = start_psymtab_common (objfile, objfile->section_offsets,
3281                               filename,
3282                               /* TEXTLOW and TEXTHIGH are set below.  */
3283                               0,
3284                               objfile->global_psymbols.next,
3285                               objfile->static_psymbols.next);
3286
3287   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3288   if (attr != NULL)
3289     pst->dirname = DW_STRING (attr);
3290
3291   pst->read_symtab_private = this_cu;
3292
3293   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3294
3295   /* Store the function that reads in the rest of the symbol table.  */
3296   pst->read_symtab = dwarf2_psymtab_to_symtab;
3297
3298   this_cu->v.psymtab = pst;
3299
3300   dwarf2_find_base_address (comp_unit_die, &cu);
3301
3302   /* Possibly set the default values of LOWPC and HIGHPC from
3303      `DW_AT_ranges'.  */
3304   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3305                                       &best_highpc, &cu, pst);
3306   if (has_pc_info == 1 && best_lowpc < best_highpc)
3307     /* Store the contiguous range if it is not empty; it can be empty for
3308        CUs with no code.  */
3309     addrmap_set_empty (objfile->psymtabs_addrmap,
3310                        best_lowpc + baseaddr,
3311                        best_highpc + baseaddr - 1, pst);
3312
3313   /* Check if comp unit has_children.
3314      If so, read the rest of the partial symbols from this comp unit.
3315      If not, there's no more debug_info for this comp unit.  */
3316   if (has_children)
3317     {
3318       struct partial_die_info *first_die;
3319       CORE_ADDR lowpc, highpc;
3320
3321       lowpc = ((CORE_ADDR) -1);
3322       highpc = ((CORE_ADDR) 0);
3323
3324       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3325
3326       scan_partial_symbols (first_die, &lowpc, &highpc,
3327                             ! has_pc_info, &cu);
3328
3329       /* If we didn't find a lowpc, set it to highpc to avoid
3330          complaints from `maint check'.  */
3331       if (lowpc == ((CORE_ADDR) -1))
3332         lowpc = highpc;
3333
3334       /* If the compilation unit didn't have an explicit address range,
3335          then use the information extracted from its child dies.  */
3336       if (! has_pc_info)
3337         {
3338           best_lowpc = lowpc;
3339           best_highpc = highpc;
3340         }
3341     }
3342   pst->textlow = best_lowpc + baseaddr;
3343   pst->texthigh = best_highpc + baseaddr;
3344
3345   pst->n_global_syms = objfile->global_psymbols.next -
3346     (objfile->global_psymbols.list + pst->globals_offset);
3347   pst->n_static_syms = objfile->static_psymbols.next -
3348     (objfile->static_psymbols.list + pst->statics_offset);
3349   sort_pst_symbols (pst);
3350
3351   info_ptr = (beg_of_comp_unit + cu.header.length
3352               + cu.header.initial_length_size);
3353
3354   if (this_cu->from_debug_types)
3355     {
3356       /* It's not clear we want to do anything with stmt lists here.
3357          Waiting to see what gcc ultimately does.  */
3358     }
3359   else
3360     {
3361       /* Get the list of files included in the current compilation unit,
3362          and build a psymtab for each of them.  */
3363       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3364     }
3365
3366   do_cleanups (back_to_inner);
3367
3368   return info_ptr;
3369 }
3370
3371 /* Traversal function for htab_traverse_noresize.
3372    Process one .debug_types comp-unit.  */
3373
3374 static int
3375 process_type_comp_unit (void **slot, void *info)
3376 {
3377   struct signatured_type *entry = (struct signatured_type *) *slot;
3378   struct objfile *objfile = (struct objfile *) info;
3379   struct dwarf2_per_cu_data *this_cu;
3380
3381   this_cu = &entry->per_cu;
3382
3383   gdb_assert (dwarf2_per_objfile->types.readin);
3384   process_psymtab_comp_unit (objfile, this_cu,
3385                              dwarf2_per_objfile->types.buffer,
3386                              dwarf2_per_objfile->types.buffer + entry->offset,
3387                              dwarf2_per_objfile->types.size);
3388
3389   return 1;
3390 }
3391
3392 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3393    Build partial symbol tables for the .debug_types comp-units.  */
3394
3395 static void
3396 build_type_psymtabs (struct objfile *objfile)
3397 {
3398   if (! create_debug_types_hash_table (objfile))
3399     return;
3400
3401   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3402                           process_type_comp_unit, objfile);
3403 }
3404
3405 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3406
3407 static void
3408 psymtabs_addrmap_cleanup (void *o)
3409 {
3410   struct objfile *objfile = o;
3411
3412   objfile->psymtabs_addrmap = NULL;
3413 }
3414
3415 /* Build the partial symbol table by doing a quick pass through the
3416    .debug_info and .debug_abbrev sections.  */
3417
3418 static void
3419 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3420 {
3421   gdb_byte *info_ptr;
3422   struct cleanup *back_to, *addrmap_cleanup;
3423   struct obstack temp_obstack;
3424
3425   dwarf2_per_objfile->reading_partial_symbols = 1;
3426
3427   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3428   info_ptr = dwarf2_per_objfile->info.buffer;
3429
3430   /* Any cached compilation units will be linked by the per-objfile
3431      read_in_chain.  Make sure to free them when we're done.  */
3432   back_to = make_cleanup (free_cached_comp_units, NULL);
3433
3434   build_type_psymtabs (objfile);
3435
3436   create_all_comp_units (objfile);
3437
3438   /* Create a temporary address map on a temporary obstack.  We later
3439      copy this to the final obstack.  */
3440   obstack_init (&temp_obstack);
3441   make_cleanup_obstack_free (&temp_obstack);
3442   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3443   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3444
3445   /* Since the objects we're extracting from .debug_info vary in
3446      length, only the individual functions to extract them (like
3447      read_comp_unit_head and load_partial_die) can really know whether
3448      the buffer is large enough to hold another complete object.
3449
3450      At the moment, they don't actually check that.  If .debug_info
3451      holds just one extra byte after the last compilation unit's dies,
3452      then read_comp_unit_head will happily read off the end of the
3453      buffer.  read_partial_die is similarly casual.  Those functions
3454      should be fixed.
3455
3456      For this loop condition, simply checking whether there's any data
3457      left at all should be sufficient.  */
3458
3459   while (info_ptr < (dwarf2_per_objfile->info.buffer
3460                      + dwarf2_per_objfile->info.size))
3461     {
3462       struct dwarf2_per_cu_data *this_cu;
3463
3464       this_cu = dwarf2_find_comp_unit (info_ptr
3465                                        - dwarf2_per_objfile->info.buffer,
3466                                        objfile);
3467
3468       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3469                                             dwarf2_per_objfile->info.buffer,
3470                                             info_ptr,
3471                                             dwarf2_per_objfile->info.size);
3472     }
3473
3474   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3475                                                     &objfile->objfile_obstack);
3476   discard_cleanups (addrmap_cleanup);
3477
3478   do_cleanups (back_to);
3479 }
3480
3481 /* Load the partial DIEs for a secondary CU into memory.  */
3482
3483 static void
3484 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3485                         struct objfile *objfile)
3486 {
3487   bfd *abfd = objfile->obfd;
3488   gdb_byte *info_ptr, *beg_of_comp_unit;
3489   struct die_info *comp_unit_die;
3490   struct dwarf2_cu *cu;
3491   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3492   int has_children;
3493   struct die_reader_specs reader_specs;
3494   int read_cu = 0;
3495
3496   gdb_assert (! this_cu->from_debug_types);
3497
3498   gdb_assert (dwarf2_per_objfile->info.readin);
3499   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3500   beg_of_comp_unit = info_ptr;
3501
3502   if (this_cu->cu == NULL)
3503     {
3504       cu = xmalloc (sizeof (*cu));
3505       init_one_comp_unit (cu, objfile);
3506
3507       read_cu = 1;
3508
3509       /* If an error occurs while loading, release our storage.  */
3510       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3511
3512       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3513                                               dwarf2_per_objfile->info.buffer,
3514                                               dwarf2_per_objfile->info.size,
3515                                               abfd);
3516
3517       /* Complete the cu_header.  */
3518       cu->header.offset = this_cu->offset;
3519       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3520
3521       /* Link this compilation unit into the compilation unit tree.  */
3522       this_cu->cu = cu;
3523       cu->per_cu = this_cu;
3524
3525       /* Link this CU into read_in_chain.  */
3526       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3527       dwarf2_per_objfile->read_in_chain = this_cu;
3528     }
3529   else
3530     {
3531       cu = this_cu->cu;
3532       info_ptr += cu->header.first_die_offset;
3533     }
3534
3535   /* Read the abbrevs for this compilation unit into a table.  */
3536   gdb_assert (cu->dwarf2_abbrevs == NULL);
3537   dwarf2_read_abbrevs (abfd, cu);
3538   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3539
3540   /* Read the compilation unit die.  */
3541   init_cu_die_reader (&reader_specs, cu);
3542   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3543                             &has_children);
3544
3545   prepare_one_comp_unit (cu, comp_unit_die);
3546
3547   /* Check if comp unit has_children.
3548      If so, read the rest of the partial symbols from this comp unit.
3549      If not, there's no more debug_info for this comp unit.  */
3550   if (has_children)
3551     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3552
3553   do_cleanups (free_abbrevs_cleanup);
3554
3555   if (read_cu)
3556     {
3557       /* We've successfully allocated this compilation unit.  Let our
3558          caller clean it up when finished with it.  */
3559       discard_cleanups (free_cu_cleanup);
3560     }
3561 }
3562
3563 /* Create a list of all compilation units in OBJFILE.  We do this only
3564    if an inter-comp-unit reference is found; presumably if there is one,
3565    there will be many, and one will occur early in the .debug_info section.
3566    So there's no point in building this list incrementally.  */
3567
3568 static void
3569 create_all_comp_units (struct objfile *objfile)
3570 {
3571   int n_allocated;
3572   int n_comp_units;
3573   struct dwarf2_per_cu_data **all_comp_units;
3574   gdb_byte *info_ptr;
3575
3576   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3577   info_ptr = dwarf2_per_objfile->info.buffer;
3578
3579   n_comp_units = 0;
3580   n_allocated = 10;
3581   all_comp_units = xmalloc (n_allocated
3582                             * sizeof (struct dwarf2_per_cu_data *));
3583
3584   while (info_ptr < dwarf2_per_objfile->info.buffer
3585          + dwarf2_per_objfile->info.size)
3586     {
3587       unsigned int length, initial_length_size;
3588       struct dwarf2_per_cu_data *this_cu;
3589       unsigned int offset;
3590
3591       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3592
3593       /* Read just enough information to find out where the next
3594          compilation unit is.  */
3595       length = read_initial_length (objfile->obfd, info_ptr,
3596                                     &initial_length_size);
3597
3598       /* Save the compilation unit for later lookup.  */
3599       this_cu = obstack_alloc (&objfile->objfile_obstack,
3600                                sizeof (struct dwarf2_per_cu_data));
3601       memset (this_cu, 0, sizeof (*this_cu));
3602       this_cu->offset = offset;
3603       this_cu->length = length + initial_length_size;
3604       this_cu->objfile = objfile;
3605
3606       if (n_comp_units == n_allocated)
3607         {
3608           n_allocated *= 2;
3609           all_comp_units = xrealloc (all_comp_units,
3610                                      n_allocated
3611                                      * sizeof (struct dwarf2_per_cu_data *));
3612         }
3613       all_comp_units[n_comp_units++] = this_cu;
3614
3615       info_ptr = info_ptr + this_cu->length;
3616     }
3617
3618   dwarf2_per_objfile->all_comp_units
3619     = obstack_alloc (&objfile->objfile_obstack,
3620                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3621   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3622           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3623   xfree (all_comp_units);
3624   dwarf2_per_objfile->n_comp_units = n_comp_units;
3625 }
3626
3627 /* Process all loaded DIEs for compilation unit CU, starting at
3628    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3629    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3630    DW_AT_ranges).  If NEED_PC is set, then this function will set
3631    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3632    and record the covered ranges in the addrmap.  */
3633
3634 static void
3635 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3636                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3637 {
3638   struct partial_die_info *pdi;
3639
3640   /* Now, march along the PDI's, descending into ones which have
3641      interesting children but skipping the children of the other ones,
3642      until we reach the end of the compilation unit.  */
3643
3644   pdi = first_die;
3645
3646   while (pdi != NULL)
3647     {
3648       fixup_partial_die (pdi, cu);
3649
3650       /* Anonymous namespaces or modules have no name but have interesting
3651          children, so we need to look at them.  Ditto for anonymous
3652          enums.  */
3653
3654       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3655           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3656         {
3657           switch (pdi->tag)
3658             {
3659             case DW_TAG_subprogram:
3660               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3661               break;
3662             case DW_TAG_constant:
3663             case DW_TAG_variable:
3664             case DW_TAG_typedef:
3665             case DW_TAG_union_type:
3666               if (!pdi->is_declaration)
3667                 {
3668                   add_partial_symbol (pdi, cu);
3669                 }
3670               break;
3671             case DW_TAG_class_type:
3672             case DW_TAG_interface_type:
3673             case DW_TAG_structure_type:
3674               if (!pdi->is_declaration)
3675                 {
3676                   add_partial_symbol (pdi, cu);
3677                 }
3678               break;
3679             case DW_TAG_enumeration_type:
3680               if (!pdi->is_declaration)
3681                 add_partial_enumeration (pdi, cu);
3682               break;
3683             case DW_TAG_base_type:
3684             case DW_TAG_subrange_type:
3685               /* File scope base type definitions are added to the partial
3686                  symbol table.  */
3687               add_partial_symbol (pdi, cu);
3688               break;
3689             case DW_TAG_namespace:
3690               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3691               break;
3692             case DW_TAG_module:
3693               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3694               break;
3695             default:
3696               break;
3697             }
3698         }
3699
3700       /* If the die has a sibling, skip to the sibling.  */
3701
3702       pdi = pdi->die_sibling;
3703     }
3704 }
3705
3706 /* Functions used to compute the fully scoped name of a partial DIE.
3707
3708    Normally, this is simple.  For C++, the parent DIE's fully scoped
3709    name is concatenated with "::" and the partial DIE's name.  For
3710    Java, the same thing occurs except that "." is used instead of "::".
3711    Enumerators are an exception; they use the scope of their parent
3712    enumeration type, i.e. the name of the enumeration type is not
3713    prepended to the enumerator.
3714
3715    There are two complexities.  One is DW_AT_specification; in this
3716    case "parent" means the parent of the target of the specification,
3717    instead of the direct parent of the DIE.  The other is compilers
3718    which do not emit DW_TAG_namespace; in this case we try to guess
3719    the fully qualified name of structure types from their members'
3720    linkage names.  This must be done using the DIE's children rather
3721    than the children of any DW_AT_specification target.  We only need
3722    to do this for structures at the top level, i.e. if the target of
3723    any DW_AT_specification (if any; otherwise the DIE itself) does not
3724    have a parent.  */
3725
3726 /* Compute the scope prefix associated with PDI's parent, in
3727    compilation unit CU.  The result will be allocated on CU's
3728    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3729    field.  NULL is returned if no prefix is necessary.  */
3730 static char *
3731 partial_die_parent_scope (struct partial_die_info *pdi,
3732                           struct dwarf2_cu *cu)
3733 {
3734   char *grandparent_scope;
3735   struct partial_die_info *parent, *real_pdi;
3736
3737   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3738      then this means the parent of the specification DIE.  */
3739
3740   real_pdi = pdi;
3741   while (real_pdi->has_specification)
3742     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3743
3744   parent = real_pdi->die_parent;
3745   if (parent == NULL)
3746     return NULL;
3747
3748   if (parent->scope_set)
3749     return parent->scope;
3750
3751   fixup_partial_die (parent, cu);
3752
3753   grandparent_scope = partial_die_parent_scope (parent, cu);
3754
3755   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3756      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3757      Work around this problem here.  */
3758   if (cu->language == language_cplus
3759       && parent->tag == DW_TAG_namespace
3760       && strcmp (parent->name, "::") == 0
3761       && grandparent_scope == NULL)
3762     {
3763       parent->scope = NULL;
3764       parent->scope_set = 1;
3765       return NULL;
3766     }
3767
3768   if (parent->tag == DW_TAG_namespace
3769       || parent->tag == DW_TAG_module
3770       || parent->tag == DW_TAG_structure_type
3771       || parent->tag == DW_TAG_class_type
3772       || parent->tag == DW_TAG_interface_type
3773       || parent->tag == DW_TAG_union_type
3774       || parent->tag == DW_TAG_enumeration_type)
3775     {
3776       if (grandparent_scope == NULL)
3777         parent->scope = parent->name;
3778       else
3779         parent->scope = typename_concat (&cu->comp_unit_obstack,
3780                                          grandparent_scope,
3781                                          parent->name, 0, cu);
3782     }
3783   else if (parent->tag == DW_TAG_enumerator)
3784     /* Enumerators should not get the name of the enumeration as a prefix.  */
3785     parent->scope = grandparent_scope;
3786   else
3787     {
3788       /* FIXME drow/2004-04-01: What should we be doing with
3789          function-local names?  For partial symbols, we should probably be
3790          ignoring them.  */
3791       complaint (&symfile_complaints,
3792                  _("unhandled containing DIE tag %d for DIE at %d"),
3793                  parent->tag, pdi->offset);
3794       parent->scope = grandparent_scope;
3795     }
3796
3797   parent->scope_set = 1;
3798   return parent->scope;
3799 }
3800
3801 /* Return the fully scoped name associated with PDI, from compilation unit
3802    CU.  The result will be allocated with malloc.  */
3803 static char *
3804 partial_die_full_name (struct partial_die_info *pdi,
3805                        struct dwarf2_cu *cu)
3806 {
3807   char *parent_scope;
3808
3809   /* If this is a template instantiation, we can not work out the
3810      template arguments from partial DIEs.  So, unfortunately, we have
3811      to go through the full DIEs.  At least any work we do building
3812      types here will be reused if full symbols are loaded later.  */
3813   if (pdi->has_template_arguments)
3814     {
3815       fixup_partial_die (pdi, cu);
3816
3817       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3818         {
3819           struct die_info *die;
3820           struct attribute attr;
3821           struct dwarf2_cu *ref_cu = cu;
3822
3823           attr.name = 0;
3824           attr.form = DW_FORM_ref_addr;
3825           attr.u.addr = pdi->offset;
3826           die = follow_die_ref (NULL, &attr, &ref_cu);
3827
3828           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3829         }
3830     }
3831
3832   parent_scope = partial_die_parent_scope (pdi, cu);
3833   if (parent_scope == NULL)
3834     return NULL;
3835   else
3836     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3837 }
3838
3839 static void
3840 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3841 {
3842   struct objfile *objfile = cu->objfile;
3843   CORE_ADDR addr = 0;
3844   char *actual_name = NULL;
3845   const struct partial_symbol *psym = NULL;
3846   CORE_ADDR baseaddr;
3847   int built_actual_name = 0;
3848
3849   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3850
3851   actual_name = partial_die_full_name (pdi, cu);
3852   if (actual_name)
3853     built_actual_name = 1;
3854
3855   if (actual_name == NULL)
3856     actual_name = pdi->name;
3857
3858   switch (pdi->tag)
3859     {
3860     case DW_TAG_subprogram:
3861       if (pdi->is_external || cu->language == language_ada)
3862         {
3863           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3864              of the global scope.  But in Ada, we want to be able to access
3865              nested procedures globally.  So all Ada subprograms are stored
3866              in the global scope.  */
3867           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3868              mst_text, objfile); */
3869           add_psymbol_to_list (actual_name, strlen (actual_name),
3870                                built_actual_name,
3871                                VAR_DOMAIN, LOC_BLOCK,
3872                                &objfile->global_psymbols,
3873                                0, pdi->lowpc + baseaddr,
3874                                cu->language, objfile);
3875         }
3876       else
3877         {
3878           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3879              mst_file_text, objfile); */
3880           add_psymbol_to_list (actual_name, strlen (actual_name),
3881                                built_actual_name,
3882                                VAR_DOMAIN, LOC_BLOCK,
3883                                &objfile->static_psymbols,
3884                                0, pdi->lowpc + baseaddr,
3885                                cu->language, objfile);
3886         }
3887       break;
3888     case DW_TAG_constant:
3889       {
3890         struct psymbol_allocation_list *list;
3891
3892         if (pdi->is_external)
3893           list = &objfile->global_psymbols;
3894         else
3895           list = &objfile->static_psymbols;
3896         add_psymbol_to_list (actual_name, strlen (actual_name),
3897                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3898                              list, 0, 0, cu->language, objfile);
3899       }
3900       break;
3901     case DW_TAG_variable:
3902       if (pdi->locdesc)
3903         addr = decode_locdesc (pdi->locdesc, cu);
3904
3905       if (pdi->locdesc
3906           && addr == 0
3907           && !dwarf2_per_objfile->has_section_at_zero)
3908         {
3909           /* A global or static variable may also have been stripped
3910              out by the linker if unused, in which case its address
3911              will be nullified; do not add such variables into partial
3912              symbol table then.  */
3913         }
3914       else if (pdi->is_external)
3915         {
3916           /* Global Variable.
3917              Don't enter into the minimal symbol tables as there is
3918              a minimal symbol table entry from the ELF symbols already.
3919              Enter into partial symbol table if it has a location
3920              descriptor or a type.
3921              If the location descriptor is missing, new_symbol will create
3922              a LOC_UNRESOLVED symbol, the address of the variable will then
3923              be determined from the minimal symbol table whenever the variable
3924              is referenced.
3925              The address for the partial symbol table entry is not
3926              used by GDB, but it comes in handy for debugging partial symbol
3927              table building.  */
3928
3929           if (pdi->locdesc || pdi->has_type)
3930             add_psymbol_to_list (actual_name, strlen (actual_name),
3931                                  built_actual_name,
3932                                  VAR_DOMAIN, LOC_STATIC,
3933                                  &objfile->global_psymbols,
3934                                  0, addr + baseaddr,
3935                                  cu->language, objfile);
3936         }
3937       else
3938         {
3939           /* Static Variable.  Skip symbols without location descriptors.  */
3940           if (pdi->locdesc == NULL)
3941             {
3942               if (built_actual_name)
3943                 xfree (actual_name);
3944               return;
3945             }
3946           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3947              mst_file_data, objfile); */
3948           add_psymbol_to_list (actual_name, strlen (actual_name),
3949                                built_actual_name,
3950                                VAR_DOMAIN, LOC_STATIC,
3951                                &objfile->static_psymbols,
3952                                0, addr + baseaddr,
3953                                cu->language, objfile);
3954         }
3955       break;
3956     case DW_TAG_typedef:
3957     case DW_TAG_base_type:
3958     case DW_TAG_subrange_type:
3959       add_psymbol_to_list (actual_name, strlen (actual_name),
3960                            built_actual_name,
3961                            VAR_DOMAIN, LOC_TYPEDEF,
3962                            &objfile->static_psymbols,
3963                            0, (CORE_ADDR) 0, cu->language, objfile);
3964       break;
3965     case DW_TAG_namespace:
3966       add_psymbol_to_list (actual_name, strlen (actual_name),
3967                            built_actual_name,
3968                            VAR_DOMAIN, LOC_TYPEDEF,
3969                            &objfile->global_psymbols,
3970                            0, (CORE_ADDR) 0, cu->language, objfile);
3971       break;
3972     case DW_TAG_class_type:
3973     case DW_TAG_interface_type:
3974     case DW_TAG_structure_type:
3975     case DW_TAG_union_type:
3976     case DW_TAG_enumeration_type:
3977       /* Skip external references.  The DWARF standard says in the section
3978          about "Structure, Union, and Class Type Entries": "An incomplete
3979          structure, union or class type is represented by a structure,
3980          union or class entry that does not have a byte size attribute
3981          and that has a DW_AT_declaration attribute."  */
3982       if (!pdi->has_byte_size && pdi->is_declaration)
3983         {
3984           if (built_actual_name)
3985             xfree (actual_name);
3986           return;
3987         }
3988
3989       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3990          static vs. global.  */
3991       add_psymbol_to_list (actual_name, strlen (actual_name),
3992                            built_actual_name,
3993                            STRUCT_DOMAIN, LOC_TYPEDEF,
3994                            (cu->language == language_cplus
3995                             || cu->language == language_java)
3996                            ? &objfile->global_psymbols
3997                            : &objfile->static_psymbols,
3998                            0, (CORE_ADDR) 0, cu->language, objfile);
3999
4000       break;
4001     case DW_TAG_enumerator:
4002       add_psymbol_to_list (actual_name, strlen (actual_name),
4003                            built_actual_name,
4004                            VAR_DOMAIN, LOC_CONST,
4005                            (cu->language == language_cplus
4006                             || cu->language == language_java)
4007                            ? &objfile->global_psymbols
4008                            : &objfile->static_psymbols,
4009                            0, (CORE_ADDR) 0, cu->language, objfile);
4010       break;
4011     default:
4012       break;
4013     }
4014
4015   if (built_actual_name)
4016     xfree (actual_name);
4017 }
4018
4019 /* Read a partial die corresponding to a namespace; also, add a symbol
4020    corresponding to that namespace to the symbol table.  NAMESPACE is
4021    the name of the enclosing namespace.  */
4022
4023 static void
4024 add_partial_namespace (struct partial_die_info *pdi,
4025                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4026                        int need_pc, struct dwarf2_cu *cu)
4027 {
4028   /* Add a symbol for the namespace.  */
4029
4030   add_partial_symbol (pdi, cu);
4031
4032   /* Now scan partial symbols in that namespace.  */
4033
4034   if (pdi->has_children)
4035     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4036 }
4037
4038 /* Read a partial die corresponding to a Fortran module.  */
4039
4040 static void
4041 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4042                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4043 {
4044   /* Now scan partial symbols in that module.  */
4045
4046   if (pdi->has_children)
4047     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4048 }
4049
4050 /* Read a partial die corresponding to a subprogram and create a partial
4051    symbol for that subprogram.  When the CU language allows it, this
4052    routine also defines a partial symbol for each nested subprogram
4053    that this subprogram contains.
4054
4055    DIE my also be a lexical block, in which case we simply search
4056    recursively for suprograms defined inside that lexical block.
4057    Again, this is only performed when the CU language allows this
4058    type of definitions.  */
4059
4060 static void
4061 add_partial_subprogram (struct partial_die_info *pdi,
4062                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4063                         int need_pc, struct dwarf2_cu *cu)
4064 {
4065   if (pdi->tag == DW_TAG_subprogram)
4066     {
4067       if (pdi->has_pc_info)
4068         {
4069           if (pdi->lowpc < *lowpc)
4070             *lowpc = pdi->lowpc;
4071           if (pdi->highpc > *highpc)
4072             *highpc = pdi->highpc;
4073           if (need_pc)
4074             {
4075               CORE_ADDR baseaddr;
4076               struct objfile *objfile = cu->objfile;
4077
4078               baseaddr = ANOFFSET (objfile->section_offsets,
4079                                    SECT_OFF_TEXT (objfile));
4080               addrmap_set_empty (objfile->psymtabs_addrmap,
4081                                  pdi->lowpc + baseaddr,
4082                                  pdi->highpc - 1 + baseaddr,
4083                                  cu->per_cu->v.psymtab);
4084             }
4085           if (!pdi->is_declaration)
4086             /* Ignore subprogram DIEs that do not have a name, they are
4087                illegal.  Do not emit a complaint at this point, we will
4088                do so when we convert this psymtab into a symtab.  */
4089             if (pdi->name)
4090               add_partial_symbol (pdi, cu);
4091         }
4092     }
4093
4094   if (! pdi->has_children)
4095     return;
4096
4097   if (cu->language == language_ada)
4098     {
4099       pdi = pdi->die_child;
4100       while (pdi != NULL)
4101         {
4102           fixup_partial_die (pdi, cu);
4103           if (pdi->tag == DW_TAG_subprogram
4104               || pdi->tag == DW_TAG_lexical_block)
4105             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4106           pdi = pdi->die_sibling;
4107         }
4108     }
4109 }
4110
4111 /* Read a partial die corresponding to an enumeration type.  */
4112
4113 static void
4114 add_partial_enumeration (struct partial_die_info *enum_pdi,
4115                          struct dwarf2_cu *cu)
4116 {
4117   struct partial_die_info *pdi;
4118
4119   if (enum_pdi->name != NULL)
4120     add_partial_symbol (enum_pdi, cu);
4121
4122   pdi = enum_pdi->die_child;
4123   while (pdi)
4124     {
4125       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4126         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4127       else
4128         add_partial_symbol (pdi, cu);
4129       pdi = pdi->die_sibling;
4130     }
4131 }
4132
4133 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4134    Return the corresponding abbrev, or NULL if the number is zero (indicating
4135    an empty DIE).  In either case *BYTES_READ will be set to the length of
4136    the initial number.  */
4137
4138 static struct abbrev_info *
4139 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4140                  struct dwarf2_cu *cu)
4141 {
4142   bfd *abfd = cu->objfile->obfd;
4143   unsigned int abbrev_number;
4144   struct abbrev_info *abbrev;
4145
4146   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4147
4148   if (abbrev_number == 0)
4149     return NULL;
4150
4151   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4152   if (!abbrev)
4153     {
4154       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4155              abbrev_number, bfd_get_filename (abfd));
4156     }
4157
4158   return abbrev;
4159 }
4160
4161 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4162    Returns a pointer to the end of a series of DIEs, terminated by an empty
4163    DIE.  Any children of the skipped DIEs will also be skipped.  */
4164
4165 static gdb_byte *
4166 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4167 {
4168   struct abbrev_info *abbrev;
4169   unsigned int bytes_read;
4170
4171   while (1)
4172     {
4173       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4174       if (abbrev == NULL)
4175         return info_ptr + bytes_read;
4176       else
4177         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4178     }
4179 }
4180
4181 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4182    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4183    abbrev corresponding to that skipped uleb128 should be passed in
4184    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4185    children.  */
4186
4187 static gdb_byte *
4188 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4189               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4190 {
4191   unsigned int bytes_read;
4192   struct attribute attr;
4193   bfd *abfd = cu->objfile->obfd;
4194   unsigned int form, i;
4195
4196   for (i = 0; i < abbrev->num_attrs; i++)
4197     {
4198       /* The only abbrev we care about is DW_AT_sibling.  */
4199       if (abbrev->attrs[i].name == DW_AT_sibling)
4200         {
4201           read_attribute (&attr, &abbrev->attrs[i],
4202                           abfd, info_ptr, cu);
4203           if (attr.form == DW_FORM_ref_addr)
4204             complaint (&symfile_complaints,
4205                        _("ignoring absolute DW_AT_sibling"));
4206           else
4207             return buffer + dwarf2_get_ref_die_offset (&attr);
4208         }
4209
4210       /* If it isn't DW_AT_sibling, skip this attribute.  */
4211       form = abbrev->attrs[i].form;
4212     skip_attribute:
4213       switch (form)
4214         {
4215         case DW_FORM_ref_addr:
4216           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4217              and later it is offset sized.  */
4218           if (cu->header.version == 2)
4219             info_ptr += cu->header.addr_size;
4220           else
4221             info_ptr += cu->header.offset_size;
4222           break;
4223         case DW_FORM_addr:
4224           info_ptr += cu->header.addr_size;
4225           break;
4226         case DW_FORM_data1:
4227         case DW_FORM_ref1:
4228         case DW_FORM_flag:
4229           info_ptr += 1;
4230           break;
4231         case DW_FORM_flag_present:
4232           break;
4233         case DW_FORM_data2:
4234         case DW_FORM_ref2:
4235           info_ptr += 2;
4236           break;
4237         case DW_FORM_data4:
4238         case DW_FORM_ref4:
4239           info_ptr += 4;
4240           break;
4241         case DW_FORM_data8:
4242         case DW_FORM_ref8:
4243         case DW_FORM_ref_sig8:
4244           info_ptr += 8;
4245           break;
4246         case DW_FORM_string:
4247           read_direct_string (abfd, info_ptr, &bytes_read);
4248           info_ptr += bytes_read;
4249           break;
4250         case DW_FORM_sec_offset:
4251         case DW_FORM_strp:
4252           info_ptr += cu->header.offset_size;
4253           break;
4254         case DW_FORM_exprloc:
4255         case DW_FORM_block:
4256           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4257           info_ptr += bytes_read;
4258           break;
4259         case DW_FORM_block1:
4260           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4261           break;
4262         case DW_FORM_block2:
4263           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4264           break;
4265         case DW_FORM_block4:
4266           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4267           break;
4268         case DW_FORM_sdata:
4269         case DW_FORM_udata:
4270         case DW_FORM_ref_udata:
4271           info_ptr = skip_leb128 (abfd, info_ptr);
4272           break;
4273         case DW_FORM_indirect:
4274           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4275           info_ptr += bytes_read;
4276           /* We need to continue parsing from here, so just go back to
4277              the top.  */
4278           goto skip_attribute;
4279
4280         default:
4281           error (_("Dwarf Error: Cannot handle %s "
4282                    "in DWARF reader [in module %s]"),
4283                  dwarf_form_name (form),
4284                  bfd_get_filename (abfd));
4285         }
4286     }
4287
4288   if (abbrev->has_children)
4289     return skip_children (buffer, info_ptr, cu);
4290   else
4291     return info_ptr;
4292 }
4293
4294 /* Locate ORIG_PDI's sibling.
4295    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4296    in BUFFER.  */
4297
4298 static gdb_byte *
4299 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4300                     gdb_byte *buffer, gdb_byte *info_ptr,
4301                     bfd *abfd, struct dwarf2_cu *cu)
4302 {
4303   /* Do we know the sibling already?  */
4304
4305   if (orig_pdi->sibling)
4306     return orig_pdi->sibling;
4307
4308   /* Are there any children to deal with?  */
4309
4310   if (!orig_pdi->has_children)
4311     return info_ptr;
4312
4313   /* Skip the children the long way.  */
4314
4315   return skip_children (buffer, info_ptr, cu);
4316 }
4317
4318 /* Expand this partial symbol table into a full symbol table.  */
4319
4320 static void
4321 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4322 {
4323   if (pst != NULL)
4324     {
4325       if (pst->readin)
4326         {
4327           warning (_("bug: psymtab for %s is already read in."),
4328                    pst->filename);
4329         }
4330       else
4331         {
4332           if (info_verbose)
4333             {
4334               printf_filtered (_("Reading in symbols for %s..."),
4335                                pst->filename);
4336               gdb_flush (gdb_stdout);
4337             }
4338
4339           /* Restore our global data.  */
4340           dwarf2_per_objfile = objfile_data (pst->objfile,
4341                                              dwarf2_objfile_data_key);
4342
4343           /* If this psymtab is constructed from a debug-only objfile, the
4344              has_section_at_zero flag will not necessarily be correct.  We
4345              can get the correct value for this flag by looking at the data
4346              associated with the (presumably stripped) associated objfile.  */
4347           if (pst->objfile->separate_debug_objfile_backlink)
4348             {
4349               struct dwarf2_per_objfile *dpo_backlink
4350                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4351                                 dwarf2_objfile_data_key);
4352
4353               dwarf2_per_objfile->has_section_at_zero
4354                 = dpo_backlink->has_section_at_zero;
4355             }
4356
4357           dwarf2_per_objfile->reading_partial_symbols = 0;
4358
4359           psymtab_to_symtab_1 (pst);
4360
4361           /* Finish up the debug error message.  */
4362           if (info_verbose)
4363             printf_filtered (_("done.\n"));
4364         }
4365     }
4366 }
4367
4368 /* Add PER_CU to the queue.  */
4369
4370 static void
4371 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4372 {
4373   struct dwarf2_queue_item *item;
4374
4375   per_cu->queued = 1;
4376   item = xmalloc (sizeof (*item));
4377   item->per_cu = per_cu;
4378   item->next = NULL;
4379
4380   if (dwarf2_queue == NULL)
4381     dwarf2_queue = item;
4382   else
4383     dwarf2_queue_tail->next = item;
4384
4385   dwarf2_queue_tail = item;
4386 }
4387
4388 /* Process the queue.  */
4389
4390 static void
4391 process_queue (struct objfile *objfile)
4392 {
4393   struct dwarf2_queue_item *item, *next_item;
4394
4395   /* The queue starts out with one item, but following a DIE reference
4396      may load a new CU, adding it to the end of the queue.  */
4397   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4398     {
4399       if (dwarf2_per_objfile->using_index
4400           ? !item->per_cu->v.quick->symtab
4401           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4402         process_full_comp_unit (item->per_cu);
4403
4404       item->per_cu->queued = 0;
4405       next_item = item->next;
4406       xfree (item);
4407     }
4408
4409   dwarf2_queue_tail = NULL;
4410 }
4411
4412 /* Free all allocated queue entries.  This function only releases anything if
4413    an error was thrown; if the queue was processed then it would have been
4414    freed as we went along.  */
4415
4416 static void
4417 dwarf2_release_queue (void *dummy)
4418 {
4419   struct dwarf2_queue_item *item, *last;
4420
4421   item = dwarf2_queue;
4422   while (item)
4423     {
4424       /* Anything still marked queued is likely to be in an
4425          inconsistent state, so discard it.  */
4426       if (item->per_cu->queued)
4427         {
4428           if (item->per_cu->cu != NULL)
4429             free_one_cached_comp_unit (item->per_cu->cu);
4430           item->per_cu->queued = 0;
4431         }
4432
4433       last = item;
4434       item = item->next;
4435       xfree (last);
4436     }
4437
4438   dwarf2_queue = dwarf2_queue_tail = NULL;
4439 }
4440
4441 /* Read in full symbols for PST, and anything it depends on.  */
4442
4443 static void
4444 psymtab_to_symtab_1 (struct partial_symtab *pst)
4445 {
4446   struct dwarf2_per_cu_data *per_cu;
4447   struct cleanup *back_to;
4448   int i;
4449
4450   for (i = 0; i < pst->number_of_dependencies; i++)
4451     if (!pst->dependencies[i]->readin)
4452       {
4453         /* Inform about additional files that need to be read in.  */
4454         if (info_verbose)
4455           {
4456             /* FIXME: i18n: Need to make this a single string.  */
4457             fputs_filtered (" ", gdb_stdout);
4458             wrap_here ("");
4459             fputs_filtered ("and ", gdb_stdout);
4460             wrap_here ("");
4461             printf_filtered ("%s...", pst->dependencies[i]->filename);
4462             wrap_here ("");     /* Flush output.  */
4463             gdb_flush (gdb_stdout);
4464           }
4465         psymtab_to_symtab_1 (pst->dependencies[i]);
4466       }
4467
4468   per_cu = pst->read_symtab_private;
4469
4470   if (per_cu == NULL)
4471     {
4472       /* It's an include file, no symbols to read for it.
4473          Everything is in the parent symtab.  */
4474       pst->readin = 1;
4475       return;
4476     }
4477
4478   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4479 }
4480
4481 /* Load the DIEs associated with PER_CU into memory.  */
4482
4483 static void
4484 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4485                      struct objfile *objfile)
4486 {
4487   bfd *abfd = objfile->obfd;
4488   struct dwarf2_cu *cu;
4489   unsigned int offset;
4490   gdb_byte *info_ptr, *beg_of_comp_unit;
4491   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4492   struct attribute *attr;
4493   int read_cu = 0;
4494
4495   gdb_assert (! per_cu->from_debug_types);
4496
4497   /* Set local variables from the partial symbol table info.  */
4498   offset = per_cu->offset;
4499
4500   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4501   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4502   beg_of_comp_unit = info_ptr;
4503
4504   if (per_cu->cu == NULL)
4505     {
4506       cu = xmalloc (sizeof (*cu));
4507       init_one_comp_unit (cu, objfile);
4508
4509       read_cu = 1;
4510
4511       /* If an error occurs while loading, release our storage.  */
4512       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4513
4514       /* Read in the comp_unit header.  */
4515       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4516
4517       /* Complete the cu_header.  */
4518       cu->header.offset = offset;
4519       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4520
4521       /* Read the abbrevs for this compilation unit.  */
4522       dwarf2_read_abbrevs (abfd, cu);
4523       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4524
4525       /* Link this compilation unit into the compilation unit tree.  */
4526       per_cu->cu = cu;
4527       cu->per_cu = per_cu;
4528
4529       /* Link this CU into read_in_chain.  */
4530       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4531       dwarf2_per_objfile->read_in_chain = per_cu;
4532     }
4533   else
4534     {
4535       cu = per_cu->cu;
4536       info_ptr += cu->header.first_die_offset;
4537     }
4538
4539   cu->dies = read_comp_unit (info_ptr, cu);
4540
4541   /* We try not to read any attributes in this function, because not
4542      all objfiles needed for references have been loaded yet, and symbol
4543      table processing isn't initialized.  But we have to set the CU language,
4544      or we won't be able to build types correctly.  */
4545   prepare_one_comp_unit (cu, cu->dies);
4546
4547   /* Similarly, if we do not read the producer, we can not apply
4548      producer-specific interpretation.  */
4549   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4550   if (attr)
4551     cu->producer = DW_STRING (attr);
4552
4553   if (read_cu)
4554     {
4555       do_cleanups (free_abbrevs_cleanup);
4556
4557       /* We've successfully allocated this compilation unit.  Let our
4558          caller clean it up when finished with it.  */
4559       discard_cleanups (free_cu_cleanup);
4560     }
4561 }
4562
4563 /* Add a DIE to the delayed physname list.  */
4564
4565 static void
4566 add_to_method_list (struct type *type, int fnfield_index, int index,
4567                     const char *name, struct die_info *die,
4568                     struct dwarf2_cu *cu)
4569 {
4570   struct delayed_method_info mi;
4571   mi.type = type;
4572   mi.fnfield_index = fnfield_index;
4573   mi.index = index;
4574   mi.name = name;
4575   mi.die = die;
4576   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4577 }
4578
4579 /* A cleanup for freeing the delayed method list.  */
4580
4581 static void
4582 free_delayed_list (void *ptr)
4583 {
4584   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4585   if (cu->method_list != NULL)
4586     {
4587       VEC_free (delayed_method_info, cu->method_list);
4588       cu->method_list = NULL;
4589     }
4590 }
4591
4592 /* Compute the physnames of any methods on the CU's method list.
4593
4594    The computation of method physnames is delayed in order to avoid the
4595    (bad) condition that one of the method's formal parameters is of an as yet
4596    incomplete type.  */
4597
4598 static void
4599 compute_delayed_physnames (struct dwarf2_cu *cu)
4600 {
4601   int i;
4602   struct delayed_method_info *mi;
4603   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4604     {
4605       char *physname;
4606       struct fn_fieldlist *fn_flp
4607         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4608       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4609       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4610     }
4611 }
4612
4613 /* Generate full symbol information for PST and CU, whose DIEs have
4614    already been loaded into memory.  */
4615
4616 static void
4617 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4618 {
4619   struct dwarf2_cu *cu = per_cu->cu;
4620   struct objfile *objfile = per_cu->objfile;
4621   CORE_ADDR lowpc, highpc;
4622   struct symtab *symtab;
4623   struct cleanup *back_to, *delayed_list_cleanup;
4624   CORE_ADDR baseaddr;
4625
4626   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4627
4628   buildsym_init ();
4629   back_to = make_cleanup (really_free_pendings, NULL);
4630   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4631
4632   cu->list_in_scope = &file_symbols;
4633
4634   dwarf2_find_base_address (cu->dies, cu);
4635
4636   /* Do line number decoding in read_file_scope () */
4637   process_die (cu->dies, cu);
4638
4639   /* Now that we have processed all the DIEs in the CU, all the types 
4640      should be complete, and it should now be safe to compute all of the
4641      physnames.  */
4642   compute_delayed_physnames (cu);
4643   do_cleanups (delayed_list_cleanup);
4644
4645   /* Some compilers don't define a DW_AT_high_pc attribute for the
4646      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4647      it, by scanning the DIE's below the compilation unit.  */
4648   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4649
4650   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4651
4652   /* Set symtab language to language from DW_AT_language.
4653      If the compilation is from a C file generated by language preprocessors,
4654      do not set the language if it was already deduced by start_subfile.  */
4655   if (symtab != NULL
4656       && !(cu->language == language_c && symtab->language != language_c))
4657     {
4658       symtab->language = cu->language;
4659     }
4660
4661   if (dwarf2_per_objfile->using_index)
4662     per_cu->v.quick->symtab = symtab;
4663   else
4664     {
4665       struct partial_symtab *pst = per_cu->v.psymtab;
4666       pst->symtab = symtab;
4667       pst->readin = 1;
4668     }
4669
4670   do_cleanups (back_to);
4671 }
4672
4673 /* Process a die and its children.  */
4674
4675 static void
4676 process_die (struct die_info *die, struct dwarf2_cu *cu)
4677 {
4678   switch (die->tag)
4679     {
4680     case DW_TAG_padding:
4681       break;
4682     case DW_TAG_compile_unit:
4683       read_file_scope (die, cu);
4684       break;
4685     case DW_TAG_type_unit:
4686       read_type_unit_scope (die, cu);
4687       break;
4688     case DW_TAG_subprogram:
4689     case DW_TAG_inlined_subroutine:
4690       read_func_scope (die, cu);
4691       break;
4692     case DW_TAG_lexical_block:
4693     case DW_TAG_try_block:
4694     case DW_TAG_catch_block:
4695       read_lexical_block_scope (die, cu);
4696       break;
4697     case DW_TAG_class_type:
4698     case DW_TAG_interface_type:
4699     case DW_TAG_structure_type:
4700     case DW_TAG_union_type:
4701       process_structure_scope (die, cu);
4702       break;
4703     case DW_TAG_enumeration_type:
4704       process_enumeration_scope (die, cu);
4705       break;
4706
4707     /* These dies have a type, but processing them does not create
4708        a symbol or recurse to process the children.  Therefore we can
4709        read them on-demand through read_type_die.  */
4710     case DW_TAG_subroutine_type:
4711     case DW_TAG_set_type:
4712     case DW_TAG_array_type:
4713     case DW_TAG_pointer_type:
4714     case DW_TAG_ptr_to_member_type:
4715     case DW_TAG_reference_type:
4716     case DW_TAG_string_type:
4717       break;
4718
4719     case DW_TAG_base_type:
4720     case DW_TAG_subrange_type:
4721     case DW_TAG_typedef:
4722       /* Add a typedef symbol for the type definition, if it has a
4723          DW_AT_name.  */
4724       new_symbol (die, read_type_die (die, cu), cu);
4725       break;
4726     case DW_TAG_common_block:
4727       read_common_block (die, cu);
4728       break;
4729     case DW_TAG_common_inclusion:
4730       break;
4731     case DW_TAG_namespace:
4732       processing_has_namespace_info = 1;
4733       read_namespace (die, cu);
4734       break;
4735     case DW_TAG_module:
4736       processing_has_namespace_info = 1;
4737       read_module (die, cu);
4738       break;
4739     case DW_TAG_imported_declaration:
4740     case DW_TAG_imported_module:
4741       processing_has_namespace_info = 1;
4742       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4743                                  || cu->language != language_fortran))
4744         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4745                    dwarf_tag_name (die->tag));
4746       read_import_statement (die, cu);
4747       break;
4748     default:
4749       new_symbol (die, NULL, cu);
4750       break;
4751     }
4752 }
4753
4754 /* A helper function for dwarf2_compute_name which determines whether DIE
4755    needs to have the name of the scope prepended to the name listed in the
4756    die.  */
4757
4758 static int
4759 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4760 {
4761   struct attribute *attr;
4762
4763   switch (die->tag)
4764     {
4765     case DW_TAG_namespace:
4766     case DW_TAG_typedef:
4767     case DW_TAG_class_type:
4768     case DW_TAG_interface_type:
4769     case DW_TAG_structure_type:
4770     case DW_TAG_union_type:
4771     case DW_TAG_enumeration_type:
4772     case DW_TAG_enumerator:
4773     case DW_TAG_subprogram:
4774     case DW_TAG_member:
4775       return 1;
4776
4777     case DW_TAG_variable:
4778     case DW_TAG_constant:
4779       /* We only need to prefix "globally" visible variables.  These include
4780          any variable marked with DW_AT_external or any variable that
4781          lives in a namespace.  [Variables in anonymous namespaces
4782          require prefixing, but they are not DW_AT_external.]  */
4783
4784       if (dwarf2_attr (die, DW_AT_specification, cu))
4785         {
4786           struct dwarf2_cu *spec_cu = cu;
4787
4788           return die_needs_namespace (die_specification (die, &spec_cu),
4789                                       spec_cu);
4790         }
4791
4792       attr = dwarf2_attr (die, DW_AT_external, cu);
4793       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4794           && die->parent->tag != DW_TAG_module)
4795         return 0;
4796       /* A variable in a lexical block of some kind does not need a
4797          namespace, even though in C++ such variables may be external
4798          and have a mangled name.  */
4799       if (die->parent->tag ==  DW_TAG_lexical_block
4800           || die->parent->tag ==  DW_TAG_try_block
4801           || die->parent->tag ==  DW_TAG_catch_block
4802           || die->parent->tag == DW_TAG_subprogram)
4803         return 0;
4804       return 1;
4805
4806     default:
4807       return 0;
4808     }
4809 }
4810
4811 /* Retrieve the last character from a mem_file.  */
4812
4813 static void
4814 do_ui_file_peek_last (void *object, const char *buffer, long length)
4815 {
4816   char *last_char_p = (char *) object;
4817
4818   if (length > 0)
4819     *last_char_p = buffer[length - 1];
4820 }
4821
4822 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4823    compute the physname for the object, which include a method's
4824    formal parameters (C++/Java) and return type (Java).
4825
4826    For Ada, return the DIE's linkage name rather than the fully qualified
4827    name.  PHYSNAME is ignored..
4828
4829    The result is allocated on the objfile_obstack and canonicalized.  */
4830
4831 static const char *
4832 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4833                      int physname)
4834 {
4835   if (name == NULL)
4836     name = dwarf2_name (die, cu);
4837
4838   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4839      compute it by typename_concat inside GDB.  */
4840   if (cu->language == language_ada
4841       || (cu->language == language_fortran && physname))
4842     {
4843       /* For Ada unit, we prefer the linkage name over the name, as
4844          the former contains the exported name, which the user expects
4845          to be able to reference.  Ideally, we want the user to be able
4846          to reference this entity using either natural or linkage name,
4847          but we haven't started looking at this enhancement yet.  */
4848       struct attribute *attr;
4849
4850       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4851       if (attr == NULL)
4852         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4853       if (attr && DW_STRING (attr))
4854         return DW_STRING (attr);
4855     }
4856
4857   /* These are the only languages we know how to qualify names in.  */
4858   if (name != NULL
4859       && (cu->language == language_cplus || cu->language == language_java
4860           || cu->language == language_fortran))
4861     {
4862       if (die_needs_namespace (die, cu))
4863         {
4864           long length;
4865           char *prefix;
4866           struct ui_file *buf;
4867
4868           prefix = determine_prefix (die, cu);
4869           buf = mem_fileopen ();
4870           if (*prefix != '\0')
4871             {
4872               char *prefixed_name = typename_concat (NULL, prefix, name,
4873                                                      physname, cu);
4874
4875               fputs_unfiltered (prefixed_name, buf);
4876               xfree (prefixed_name);
4877             }
4878           else
4879             fputs_unfiltered (name, buf);
4880
4881           /* Template parameters may be specified in the DIE's DW_AT_name, or
4882              as children with DW_TAG_template_type_param or
4883              DW_TAG_value_type_param.  If the latter, add them to the name
4884              here.  If the name already has template parameters, then
4885              skip this step; some versions of GCC emit both, and
4886              it is more efficient to use the pre-computed name.
4887
4888              Something to keep in mind about this process: it is very
4889              unlikely, or in some cases downright impossible, to produce
4890              something that will match the mangled name of a function.
4891              If the definition of the function has the same debug info,
4892              we should be able to match up with it anyway.  But fallbacks
4893              using the minimal symbol, for instance to find a method
4894              implemented in a stripped copy of libstdc++, will not work.
4895              If we do not have debug info for the definition, we will have to
4896              match them up some other way.
4897
4898              When we do name matching there is a related problem with function
4899              templates; two instantiated function templates are allowed to
4900              differ only by their return types, which we do not add here.  */
4901
4902           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4903             {
4904               struct attribute *attr;
4905               struct die_info *child;
4906               int first = 1;
4907
4908               die->building_fullname = 1;
4909
4910               for (child = die->child; child != NULL; child = child->sibling)
4911                 {
4912                   struct type *type;
4913                   long value;
4914                   gdb_byte *bytes;
4915                   struct dwarf2_locexpr_baton *baton;
4916                   struct value *v;
4917
4918                   if (child->tag != DW_TAG_template_type_param
4919                       && child->tag != DW_TAG_template_value_param)
4920                     continue;
4921
4922                   if (first)
4923                     {
4924                       fputs_unfiltered ("<", buf);
4925                       first = 0;
4926                     }
4927                   else
4928                     fputs_unfiltered (", ", buf);
4929
4930                   attr = dwarf2_attr (child, DW_AT_type, cu);
4931                   if (attr == NULL)
4932                     {
4933                       complaint (&symfile_complaints,
4934                                  _("template parameter missing DW_AT_type"));
4935                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4936                       continue;
4937                     }
4938                   type = die_type (child, cu);
4939
4940                   if (child->tag == DW_TAG_template_type_param)
4941                     {
4942                       c_print_type (type, "", buf, -1, 0);
4943                       continue;
4944                     }
4945
4946                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4947                   if (attr == NULL)
4948                     {
4949                       complaint (&symfile_complaints,
4950                                  _("template parameter missing "
4951                                    "DW_AT_const_value"));
4952                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4953                       continue;
4954                     }
4955
4956                   dwarf2_const_value_attr (attr, type, name,
4957                                            &cu->comp_unit_obstack, cu,
4958                                            &value, &bytes, &baton);
4959
4960                   if (TYPE_NOSIGN (type))
4961                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4962                        changed, this can use value_print instead.  */
4963                     c_printchar (value, type, buf);
4964                   else
4965                     {
4966                       struct value_print_options opts;
4967
4968                       if (baton != NULL)
4969                         v = dwarf2_evaluate_loc_desc (type, NULL,
4970                                                       baton->data,
4971                                                       baton->size,
4972                                                       baton->per_cu);
4973                       else if (bytes != NULL)
4974                         {
4975                           v = allocate_value (type);
4976                           memcpy (value_contents_writeable (v), bytes,
4977                                   TYPE_LENGTH (type));
4978                         }
4979                       else
4980                         v = value_from_longest (type, value);
4981
4982                       /* Specify decimal so that we do not depend on
4983                          the radix.  */
4984                       get_formatted_print_options (&opts, 'd');
4985                       opts.raw = 1;
4986                       value_print (v, buf, &opts);
4987                       release_value (v);
4988                       value_free (v);
4989                     }
4990                 }
4991
4992               die->building_fullname = 0;
4993
4994               if (!first)
4995                 {
4996                   /* Close the argument list, with a space if necessary
4997                      (nested templates).  */
4998                   char last_char = '\0';
4999                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5000                   if (last_char == '>')
5001                     fputs_unfiltered (" >", buf);
5002                   else
5003                     fputs_unfiltered (">", buf);
5004                 }
5005             }
5006
5007           /* For Java and C++ methods, append formal parameter type
5008              information, if PHYSNAME.  */
5009
5010           if (physname && die->tag == DW_TAG_subprogram
5011               && (cu->language == language_cplus
5012                   || cu->language == language_java))
5013             {
5014               struct type *type = read_type_die (die, cu);
5015
5016               c_type_print_args (type, buf, 1, cu->language);
5017
5018               if (cu->language == language_java)
5019                 {
5020                   /* For java, we must append the return type to method
5021                      names.  */
5022                   if (die->tag == DW_TAG_subprogram)
5023                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5024                                      0, 0);
5025                 }
5026               else if (cu->language == language_cplus)
5027                 {
5028                   /* Assume that an artificial first parameter is
5029                      "this", but do not crash if it is not.  RealView
5030                      marks unnamed (and thus unused) parameters as
5031                      artificial; there is no way to differentiate
5032                      the two cases.  */
5033                   if (TYPE_NFIELDS (type) > 0
5034                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5035                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5036                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5037                                                                         0))))
5038                     fputs_unfiltered (" const", buf);
5039                 }
5040             }
5041
5042           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5043                                        &length);
5044           ui_file_delete (buf);
5045
5046           if (cu->language == language_cplus)
5047             {
5048               char *cname
5049                 = dwarf2_canonicalize_name (name, cu,
5050                                             &cu->objfile->objfile_obstack);
5051
5052               if (cname != NULL)
5053                 name = cname;
5054             }
5055         }
5056     }
5057
5058   return name;
5059 }
5060
5061 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5062    If scope qualifiers are appropriate they will be added.  The result
5063    will be allocated on the objfile_obstack, or NULL if the DIE does
5064    not have a name.  NAME may either be from a previous call to
5065    dwarf2_name or NULL.
5066
5067    The output string will be canonicalized (if C++/Java).  */
5068
5069 static const char *
5070 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5071 {
5072   return dwarf2_compute_name (name, die, cu, 0);
5073 }
5074
5075 /* Construct a physname for the given DIE in CU.  NAME may either be
5076    from a previous call to dwarf2_name or NULL.  The result will be
5077    allocated on the objfile_objstack or NULL if the DIE does not have a
5078    name.
5079
5080    The output string will be canonicalized (if C++/Java).  */
5081
5082 static const char *
5083 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5084 {
5085   return dwarf2_compute_name (name, die, cu, 1);
5086 }
5087
5088 /* Read the import statement specified by the given die and record it.  */
5089
5090 static void
5091 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5092 {
5093   struct attribute *import_attr;
5094   struct die_info *imported_die;
5095   struct dwarf2_cu *imported_cu;
5096   const char *imported_name;
5097   const char *imported_name_prefix;
5098   const char *canonical_name;
5099   const char *import_alias;
5100   const char *imported_declaration = NULL;
5101   const char *import_prefix;
5102
5103   char *temp;
5104
5105   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5106   if (import_attr == NULL)
5107     {
5108       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5109                  dwarf_tag_name (die->tag));
5110       return;
5111     }
5112
5113   imported_cu = cu;
5114   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5115   imported_name = dwarf2_name (imported_die, imported_cu);
5116   if (imported_name == NULL)
5117     {
5118       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5119
5120         The import in the following code:
5121         namespace A
5122           {
5123             typedef int B;
5124           }
5125
5126         int main ()
5127           {
5128             using A::B;
5129             B b;
5130             return b;
5131           }
5132
5133         ...
5134          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5135             <52>   DW_AT_decl_file   : 1
5136             <53>   DW_AT_decl_line   : 6
5137             <54>   DW_AT_import      : <0x75>
5138          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5139             <59>   DW_AT_name        : B
5140             <5b>   DW_AT_decl_file   : 1
5141             <5c>   DW_AT_decl_line   : 2
5142             <5d>   DW_AT_type        : <0x6e>
5143         ...
5144          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5145             <76>   DW_AT_byte_size   : 4
5146             <77>   DW_AT_encoding    : 5        (signed)
5147
5148         imports the wrong die ( 0x75 instead of 0x58 ).
5149         This case will be ignored until the gcc bug is fixed.  */
5150       return;
5151     }
5152
5153   /* Figure out the local name after import.  */
5154   import_alias = dwarf2_name (die, cu);
5155
5156   /* Figure out where the statement is being imported to.  */
5157   import_prefix = determine_prefix (die, cu);
5158
5159   /* Figure out what the scope of the imported die is and prepend it
5160      to the name of the imported die.  */
5161   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5162
5163   if (imported_die->tag != DW_TAG_namespace
5164       && imported_die->tag != DW_TAG_module)
5165     {
5166       imported_declaration = imported_name;
5167       canonical_name = imported_name_prefix;
5168     }
5169   else if (strlen (imported_name_prefix) > 0)
5170     {
5171       temp = alloca (strlen (imported_name_prefix)
5172                      + 2 + strlen (imported_name) + 1);
5173       strcpy (temp, imported_name_prefix);
5174       strcat (temp, "::");
5175       strcat (temp, imported_name);
5176       canonical_name = temp;
5177     }
5178   else
5179     canonical_name = imported_name;
5180
5181   cp_add_using_directive (import_prefix,
5182                           canonical_name,
5183                           import_alias,
5184                           imported_declaration,
5185                           &cu->objfile->objfile_obstack);
5186 }
5187
5188 static void
5189 initialize_cu_func_list (struct dwarf2_cu *cu)
5190 {
5191   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5192 }
5193
5194 /* Cleanup function for read_file_scope.  */
5195
5196 static void
5197 free_cu_line_header (void *arg)
5198 {
5199   struct dwarf2_cu *cu = arg;
5200
5201   free_line_header (cu->line_header);
5202   cu->line_header = NULL;
5203 }
5204
5205 static void
5206 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5207                          char **name, char **comp_dir)
5208 {
5209   struct attribute *attr;
5210
5211   *name = NULL;
5212   *comp_dir = NULL;
5213
5214   /* Find the filename.  Do not use dwarf2_name here, since the filename
5215      is not a source language identifier.  */
5216   attr = dwarf2_attr (die, DW_AT_name, cu);
5217   if (attr)
5218     {
5219       *name = DW_STRING (attr);
5220     }
5221
5222   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5223   if (attr)
5224     *comp_dir = DW_STRING (attr);
5225   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5226     {
5227       *comp_dir = ldirname (*name);
5228       if (*comp_dir != NULL)
5229         make_cleanup (xfree, *comp_dir);
5230     }
5231   if (*comp_dir != NULL)
5232     {
5233       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5234          directory, get rid of it.  */
5235       char *cp = strchr (*comp_dir, ':');
5236
5237       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5238         *comp_dir = cp + 1;
5239     }
5240
5241   if (*name == NULL)
5242     *name = "<unknown>";
5243 }
5244
5245 /* Process DW_TAG_compile_unit.  */
5246
5247 static void
5248 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5249 {
5250   struct objfile *objfile = cu->objfile;
5251   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5252   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5253   CORE_ADDR highpc = ((CORE_ADDR) 0);
5254   struct attribute *attr;
5255   char *name = NULL;
5256   char *comp_dir = NULL;
5257   struct die_info *child_die;
5258   bfd *abfd = objfile->obfd;
5259   struct line_header *line_header = 0;
5260   CORE_ADDR baseaddr;
5261
5262   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5263
5264   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5265
5266   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5267      from finish_block.  */
5268   if (lowpc == ((CORE_ADDR) -1))
5269     lowpc = highpc;
5270   lowpc += baseaddr;
5271   highpc += baseaddr;
5272
5273   find_file_and_directory (die, cu, &name, &comp_dir);
5274
5275   attr = dwarf2_attr (die, DW_AT_language, cu);
5276   if (attr)
5277     {
5278       set_cu_language (DW_UNSND (attr), cu);
5279     }
5280
5281   attr = dwarf2_attr (die, DW_AT_producer, cu);
5282   if (attr)
5283     cu->producer = DW_STRING (attr);
5284
5285   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5286      standardised yet.  As a workaround for the language detection we fall
5287      back to the DW_AT_producer string.  */
5288   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5289     cu->language = language_opencl;
5290
5291   /* We assume that we're processing GCC output.  */
5292   processing_gcc_compilation = 2;
5293
5294   processing_has_namespace_info = 0;
5295
5296   start_symtab (name, comp_dir, lowpc);
5297   record_debugformat ("DWARF 2");
5298   record_producer (cu->producer);
5299
5300   initialize_cu_func_list (cu);
5301
5302   /* Decode line number information if present.  We do this before
5303      processing child DIEs, so that the line header table is available
5304      for DW_AT_decl_file.  */
5305   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5306   if (attr)
5307     {
5308       unsigned int line_offset = DW_UNSND (attr);
5309       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5310       if (line_header)
5311         {
5312           cu->line_header = line_header;
5313           make_cleanup (free_cu_line_header, cu);
5314           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5315         }
5316     }
5317
5318   /* Process all dies in compilation unit.  */
5319   if (die->child != NULL)
5320     {
5321       child_die = die->child;
5322       while (child_die && child_die->tag)
5323         {
5324           process_die (child_die, cu);
5325           child_die = sibling_die (child_die);
5326         }
5327     }
5328
5329   /* Decode macro information, if present.  Dwarf 2 macro information
5330      refers to information in the line number info statement program
5331      header, so we can only read it if we've read the header
5332      successfully.  */
5333   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5334   if (attr && line_header)
5335     {
5336       unsigned int macro_offset = DW_UNSND (attr);
5337
5338       dwarf_decode_macros (line_header, macro_offset,
5339                            comp_dir, abfd, cu);
5340     }
5341   do_cleanups (back_to);
5342 }
5343
5344 /* Process DW_TAG_type_unit.
5345    For TUs we want to skip the first top level sibling if it's not the
5346    actual type being defined by this TU.  In this case the first top
5347    level sibling is there to provide context only.  */
5348
5349 static void
5350 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5351 {
5352   struct objfile *objfile = cu->objfile;
5353   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5354   CORE_ADDR lowpc;
5355   struct attribute *attr;
5356   char *name = NULL;
5357   char *comp_dir = NULL;
5358   struct die_info *child_die;
5359   bfd *abfd = objfile->obfd;
5360
5361   /* start_symtab needs a low pc, but we don't really have one.
5362      Do what read_file_scope would do in the absence of such info.  */
5363   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5364
5365   /* Find the filename.  Do not use dwarf2_name here, since the filename
5366      is not a source language identifier.  */
5367   attr = dwarf2_attr (die, DW_AT_name, cu);
5368   if (attr)
5369     name = DW_STRING (attr);
5370
5371   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5372   if (attr)
5373     comp_dir = DW_STRING (attr);
5374   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5375     {
5376       comp_dir = ldirname (name);
5377       if (comp_dir != NULL)
5378         make_cleanup (xfree, comp_dir);
5379     }
5380
5381   if (name == NULL)
5382     name = "<unknown>";
5383
5384   attr = dwarf2_attr (die, DW_AT_language, cu);
5385   if (attr)
5386     set_cu_language (DW_UNSND (attr), cu);
5387
5388   /* This isn't technically needed today.  It is done for symmetry
5389      with read_file_scope.  */
5390   attr = dwarf2_attr (die, DW_AT_producer, cu);
5391   if (attr)
5392     cu->producer = DW_STRING (attr);
5393
5394   /* We assume that we're processing GCC output.  */
5395   processing_gcc_compilation = 2;
5396
5397   processing_has_namespace_info = 0;
5398
5399   start_symtab (name, comp_dir, lowpc);
5400   record_debugformat ("DWARF 2");
5401   record_producer (cu->producer);
5402
5403   /* Process the dies in the type unit.  */
5404   if (die->child == NULL)
5405     {
5406       dump_die_for_error (die);
5407       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5408              bfd_get_filename (abfd));
5409     }
5410
5411   child_die = die->child;
5412
5413   while (child_die && child_die->tag)
5414     {
5415       process_die (child_die, cu);
5416
5417       child_die = sibling_die (child_die);
5418     }
5419
5420   do_cleanups (back_to);
5421 }
5422
5423 static void
5424 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5425                      struct dwarf2_cu *cu)
5426 {
5427   struct function_range *thisfn;
5428
5429   thisfn = (struct function_range *)
5430     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5431   thisfn->name = name;
5432   thisfn->lowpc = lowpc;
5433   thisfn->highpc = highpc;
5434   thisfn->seen_line = 0;
5435   thisfn->next = NULL;
5436
5437   if (cu->last_fn == NULL)
5438       cu->first_fn = thisfn;
5439   else
5440       cu->last_fn->next = thisfn;
5441
5442   cu->last_fn = thisfn;
5443 }
5444
5445 /* qsort helper for inherit_abstract_dies.  */
5446
5447 static int
5448 unsigned_int_compar (const void *ap, const void *bp)
5449 {
5450   unsigned int a = *(unsigned int *) ap;
5451   unsigned int b = *(unsigned int *) bp;
5452
5453   return (a > b) - (b > a);
5454 }
5455
5456 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5457    Inherit only the children of the DW_AT_abstract_origin DIE not being
5458    already referenced by DW_AT_abstract_origin from the children of the
5459    current DIE.  */
5460
5461 static void
5462 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5463 {
5464   struct die_info *child_die;
5465   unsigned die_children_count;
5466   /* CU offsets which were referenced by children of the current DIE.  */
5467   unsigned *offsets;
5468   unsigned *offsets_end, *offsetp;
5469   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5470   struct die_info *origin_die;
5471   /* Iterator of the ORIGIN_DIE children.  */
5472   struct die_info *origin_child_die;
5473   struct cleanup *cleanups;
5474   struct attribute *attr;
5475   struct dwarf2_cu *origin_cu;
5476   struct pending **origin_previous_list_in_scope;
5477
5478   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5479   if (!attr)
5480     return;
5481
5482   /* Note that following die references may follow to a die in a
5483      different cu.  */
5484
5485   origin_cu = cu;
5486   origin_die = follow_die_ref (die, attr, &origin_cu);
5487
5488   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5489      symbols in.  */
5490   origin_previous_list_in_scope = origin_cu->list_in_scope;
5491   origin_cu->list_in_scope = cu->list_in_scope;
5492
5493   if (die->tag != origin_die->tag
5494       && !(die->tag == DW_TAG_inlined_subroutine
5495            && origin_die->tag == DW_TAG_subprogram))
5496     complaint (&symfile_complaints,
5497                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5498                die->offset, origin_die->offset);
5499
5500   child_die = die->child;
5501   die_children_count = 0;
5502   while (child_die && child_die->tag)
5503     {
5504       child_die = sibling_die (child_die);
5505       die_children_count++;
5506     }
5507   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5508   cleanups = make_cleanup (xfree, offsets);
5509
5510   offsets_end = offsets;
5511   child_die = die->child;
5512   while (child_die && child_die->tag)
5513     {
5514       /* For each CHILD_DIE, find the corresponding child of
5515          ORIGIN_DIE.  If there is more than one layer of
5516          DW_AT_abstract_origin, follow them all; there shouldn't be,
5517          but GCC versions at least through 4.4 generate this (GCC PR
5518          40573).  */
5519       struct die_info *child_origin_die = child_die;
5520       struct dwarf2_cu *child_origin_cu = cu;
5521
5522       while (1)
5523         {
5524           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5525                               child_origin_cu);
5526           if (attr == NULL)
5527             break;
5528           child_origin_die = follow_die_ref (child_origin_die, attr,
5529                                              &child_origin_cu);
5530         }
5531
5532       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5533          counterpart may exist.  */
5534       if (child_origin_die != child_die)
5535         {
5536           if (child_die->tag != child_origin_die->tag
5537               && !(child_die->tag == DW_TAG_inlined_subroutine
5538                    && child_origin_die->tag == DW_TAG_subprogram))
5539             complaint (&symfile_complaints,
5540                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5541                          "different tags"), child_die->offset,
5542                        child_origin_die->offset);
5543           if (child_origin_die->parent != origin_die)
5544             complaint (&symfile_complaints,
5545                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5546                          "different parents"), child_die->offset,
5547                        child_origin_die->offset);
5548           else
5549             *offsets_end++ = child_origin_die->offset;
5550         }
5551       child_die = sibling_die (child_die);
5552     }
5553   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5554          unsigned_int_compar);
5555   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5556     if (offsetp[-1] == *offsetp)
5557       complaint (&symfile_complaints,
5558                  _("Multiple children of DIE 0x%x refer "
5559                    "to DIE 0x%x as their abstract origin"),
5560                  die->offset, *offsetp);
5561
5562   offsetp = offsets;
5563   origin_child_die = origin_die->child;
5564   while (origin_child_die && origin_child_die->tag)
5565     {
5566       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5567       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5568         offsetp++;
5569       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5570         {
5571           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5572           process_die (origin_child_die, origin_cu);
5573         }
5574       origin_child_die = sibling_die (origin_child_die);
5575     }
5576   origin_cu->list_in_scope = origin_previous_list_in_scope;
5577
5578   do_cleanups (cleanups);
5579 }
5580
5581 static void
5582 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5583 {
5584   struct objfile *objfile = cu->objfile;
5585   struct context_stack *new;
5586   CORE_ADDR lowpc;
5587   CORE_ADDR highpc;
5588   struct die_info *child_die;
5589   struct attribute *attr, *call_line, *call_file;
5590   char *name;
5591   CORE_ADDR baseaddr;
5592   struct block *block;
5593   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5594   VEC (symbolp) *template_args = NULL;
5595   struct template_symbol *templ_func = NULL;
5596
5597   if (inlined_func)
5598     {
5599       /* If we do not have call site information, we can't show the
5600          caller of this inlined function.  That's too confusing, so
5601          only use the scope for local variables.  */
5602       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5603       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5604       if (call_line == NULL || call_file == NULL)
5605         {
5606           read_lexical_block_scope (die, cu);
5607           return;
5608         }
5609     }
5610
5611   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5612
5613   name = dwarf2_name (die, cu);
5614
5615   /* Ignore functions with missing or empty names.  These are actually
5616      illegal according to the DWARF standard.  */
5617   if (name == NULL)
5618     {
5619       complaint (&symfile_complaints,
5620                  _("missing name for subprogram DIE at %d"), die->offset);
5621       return;
5622     }
5623
5624   /* Ignore functions with missing or invalid low and high pc attributes.  */
5625   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5626     {
5627       attr = dwarf2_attr (die, DW_AT_external, cu);
5628       if (!attr || !DW_UNSND (attr))
5629         complaint (&symfile_complaints,
5630                    _("cannot get low and high bounds "
5631                      "for subprogram DIE at %d"),
5632                    die->offset);
5633       return;
5634     }
5635
5636   lowpc += baseaddr;
5637   highpc += baseaddr;
5638
5639   /* Record the function range for dwarf_decode_lines.  */
5640   add_to_cu_func_list (name, lowpc, highpc, cu);
5641
5642   /* If we have any template arguments, then we must allocate a
5643      different sort of symbol.  */
5644   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5645     {
5646       if (child_die->tag == DW_TAG_template_type_param
5647           || child_die->tag == DW_TAG_template_value_param)
5648         {
5649           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5650                                        struct template_symbol);
5651           templ_func->base.is_cplus_template_function = 1;
5652           break;
5653         }
5654     }
5655
5656   new = push_context (0, lowpc);
5657   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5658                                (struct symbol *) templ_func);
5659
5660   /* If there is a location expression for DW_AT_frame_base, record
5661      it.  */
5662   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5663   if (attr)
5664     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5665        expression is being recorded directly in the function's symbol
5666        and not in a separate frame-base object.  I guess this hack is
5667        to avoid adding some sort of frame-base adjunct/annex to the
5668        function's symbol :-(.  The problem with doing this is that it
5669        results in a function symbol with a location expression that
5670        has nothing to do with the location of the function, ouch!  The
5671        relationship should be: a function's symbol has-a frame base; a
5672        frame-base has-a location expression.  */
5673     dwarf2_symbol_mark_computed (attr, new->name, cu);
5674
5675   cu->list_in_scope = &local_symbols;
5676
5677   if (die->child != NULL)
5678     {
5679       child_die = die->child;
5680       while (child_die && child_die->tag)
5681         {
5682           if (child_die->tag == DW_TAG_template_type_param
5683               || child_die->tag == DW_TAG_template_value_param)
5684             {
5685               struct symbol *arg = new_symbol (child_die, NULL, cu);
5686
5687               if (arg != NULL)
5688                 VEC_safe_push (symbolp, template_args, arg);
5689             }
5690           else
5691             process_die (child_die, cu);
5692           child_die = sibling_die (child_die);
5693         }
5694     }
5695
5696   inherit_abstract_dies (die, cu);
5697
5698   /* If we have a DW_AT_specification, we might need to import using
5699      directives from the context of the specification DIE.  See the
5700      comment in determine_prefix.  */
5701   if (cu->language == language_cplus
5702       && dwarf2_attr (die, DW_AT_specification, cu))
5703     {
5704       struct dwarf2_cu *spec_cu = cu;
5705       struct die_info *spec_die = die_specification (die, &spec_cu);
5706
5707       while (spec_die)
5708         {
5709           child_die = spec_die->child;
5710           while (child_die && child_die->tag)
5711             {
5712               if (child_die->tag == DW_TAG_imported_module)
5713                 process_die (child_die, spec_cu);
5714               child_die = sibling_die (child_die);
5715             }
5716
5717           /* In some cases, GCC generates specification DIEs that
5718              themselves contain DW_AT_specification attributes.  */
5719           spec_die = die_specification (spec_die, &spec_cu);
5720         }
5721     }
5722
5723   new = pop_context ();
5724   /* Make a block for the local symbols within.  */
5725   block = finish_block (new->name, &local_symbols, new->old_blocks,
5726                         lowpc, highpc, objfile);
5727
5728   /* For C++, set the block's scope.  */
5729   if (cu->language == language_cplus || cu->language == language_fortran)
5730     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5731                         determine_prefix (die, cu),
5732                         processing_has_namespace_info);
5733
5734   /* If we have address ranges, record them.  */
5735   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5736
5737   /* Attach template arguments to function.  */
5738   if (! VEC_empty (symbolp, template_args))
5739     {
5740       gdb_assert (templ_func != NULL);
5741
5742       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5743       templ_func->template_arguments
5744         = obstack_alloc (&objfile->objfile_obstack,
5745                          (templ_func->n_template_arguments
5746                           * sizeof (struct symbol *)));
5747       memcpy (templ_func->template_arguments,
5748               VEC_address (symbolp, template_args),
5749               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5750       VEC_free (symbolp, template_args);
5751     }
5752
5753   /* In C++, we can have functions nested inside functions (e.g., when
5754      a function declares a class that has methods).  This means that
5755      when we finish processing a function scope, we may need to go
5756      back to building a containing block's symbol lists.  */
5757   local_symbols = new->locals;
5758   param_symbols = new->params;
5759   using_directives = new->using_directives;
5760
5761   /* If we've finished processing a top-level function, subsequent
5762      symbols go in the file symbol list.  */
5763   if (outermost_context_p ())
5764     cu->list_in_scope = &file_symbols;
5765 }
5766
5767 /* Process all the DIES contained within a lexical block scope.  Start
5768    a new scope, process the dies, and then close the scope.  */
5769
5770 static void
5771 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5772 {
5773   struct objfile *objfile = cu->objfile;
5774   struct context_stack *new;
5775   CORE_ADDR lowpc, highpc;
5776   struct die_info *child_die;
5777   CORE_ADDR baseaddr;
5778
5779   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5780
5781   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5782   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5783      as multiple lexical blocks?  Handling children in a sane way would
5784      be nasty.  Might be easier to properly extend generic blocks to
5785      describe ranges.  */
5786   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5787     return;
5788   lowpc += baseaddr;
5789   highpc += baseaddr;
5790
5791   push_context (0, lowpc);
5792   if (die->child != NULL)
5793     {
5794       child_die = die->child;
5795       while (child_die && child_die->tag)
5796         {
5797           process_die (child_die, cu);
5798           child_die = sibling_die (child_die);
5799         }
5800     }
5801   new = pop_context ();
5802
5803   if (local_symbols != NULL || using_directives != NULL)
5804     {
5805       struct block *block
5806         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5807                         highpc, objfile);
5808
5809       /* Note that recording ranges after traversing children, as we
5810          do here, means that recording a parent's ranges entails
5811          walking across all its children's ranges as they appear in
5812          the address map, which is quadratic behavior.
5813
5814          It would be nicer to record the parent's ranges before
5815          traversing its children, simply overriding whatever you find
5816          there.  But since we don't even decide whether to create a
5817          block until after we've traversed its children, that's hard
5818          to do.  */
5819       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5820     }
5821   local_symbols = new->locals;
5822   using_directives = new->using_directives;
5823 }
5824
5825 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5826    Return 1 if the attributes are present and valid, otherwise, return 0.
5827    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5828
5829 static int
5830 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5831                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5832                     struct partial_symtab *ranges_pst)
5833 {
5834   struct objfile *objfile = cu->objfile;
5835   struct comp_unit_head *cu_header = &cu->header;
5836   bfd *obfd = objfile->obfd;
5837   unsigned int addr_size = cu_header->addr_size;
5838   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5839   /* Base address selection entry.  */
5840   CORE_ADDR base;
5841   int found_base;
5842   unsigned int dummy;
5843   gdb_byte *buffer;
5844   CORE_ADDR marker;
5845   int low_set;
5846   CORE_ADDR low = 0;
5847   CORE_ADDR high = 0;
5848   CORE_ADDR baseaddr;
5849
5850   found_base = cu->base_known;
5851   base = cu->base_address;
5852
5853   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5854   if (offset >= dwarf2_per_objfile->ranges.size)
5855     {
5856       complaint (&symfile_complaints,
5857                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5858                  offset);
5859       return 0;
5860     }
5861   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5862
5863   /* Read in the largest possible address.  */
5864   marker = read_address (obfd, buffer, cu, &dummy);
5865   if ((marker & mask) == mask)
5866     {
5867       /* If we found the largest possible address, then
5868          read the base address.  */
5869       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5870       buffer += 2 * addr_size;
5871       offset += 2 * addr_size;
5872       found_base = 1;
5873     }
5874
5875   low_set = 0;
5876
5877   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5878
5879   while (1)
5880     {
5881       CORE_ADDR range_beginning, range_end;
5882
5883       range_beginning = read_address (obfd, buffer, cu, &dummy);
5884       buffer += addr_size;
5885       range_end = read_address (obfd, buffer, cu, &dummy);
5886       buffer += addr_size;
5887       offset += 2 * addr_size;
5888
5889       /* An end of list marker is a pair of zero addresses.  */
5890       if (range_beginning == 0 && range_end == 0)
5891         /* Found the end of list entry.  */
5892         break;
5893
5894       /* Each base address selection entry is a pair of 2 values.
5895          The first is the largest possible address, the second is
5896          the base address.  Check for a base address here.  */
5897       if ((range_beginning & mask) == mask)
5898         {
5899           /* If we found the largest possible address, then
5900              read the base address.  */
5901           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5902           found_base = 1;
5903           continue;
5904         }
5905
5906       if (!found_base)
5907         {
5908           /* We have no valid base address for the ranges
5909              data.  */
5910           complaint (&symfile_complaints,
5911                      _("Invalid .debug_ranges data (no base address)"));
5912           return 0;
5913         }
5914
5915       if (range_beginning > range_end)
5916         {
5917           /* Inverted range entries are invalid.  */
5918           complaint (&symfile_complaints,
5919                      _("Invalid .debug_ranges data (inverted range)"));
5920           return 0;
5921         }
5922
5923       /* Empty range entries have no effect.  */
5924       if (range_beginning == range_end)
5925         continue;
5926
5927       range_beginning += base;
5928       range_end += base;
5929
5930       if (ranges_pst != NULL)
5931         addrmap_set_empty (objfile->psymtabs_addrmap,
5932                            range_beginning + baseaddr,
5933                            range_end - 1 + baseaddr,
5934                            ranges_pst);
5935
5936       /* FIXME: This is recording everything as a low-high
5937          segment of consecutive addresses.  We should have a
5938          data structure for discontiguous block ranges
5939          instead.  */
5940       if (! low_set)
5941         {
5942           low = range_beginning;
5943           high = range_end;
5944           low_set = 1;
5945         }
5946       else
5947         {
5948           if (range_beginning < low)
5949             low = range_beginning;
5950           if (range_end > high)
5951             high = range_end;
5952         }
5953     }
5954
5955   if (! low_set)
5956     /* If the first entry is an end-of-list marker, the range
5957        describes an empty scope, i.e. no instructions.  */
5958     return 0;
5959
5960   if (low_return)
5961     *low_return = low;
5962   if (high_return)
5963     *high_return = high;
5964   return 1;
5965 }
5966
5967 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5968    are present and valid, otherwise, return 0.  Return -1 if the range is
5969    discontinuous, i.e. derived from DW_AT_ranges information.  */
5970 static int
5971 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5972                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5973                       struct partial_symtab *pst)
5974 {
5975   struct attribute *attr;
5976   CORE_ADDR low = 0;
5977   CORE_ADDR high = 0;
5978   int ret = 0;
5979
5980   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5981   if (attr)
5982     {
5983       high = DW_ADDR (attr);
5984       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5985       if (attr)
5986         low = DW_ADDR (attr);
5987       else
5988         /* Found high w/o low attribute.  */
5989         return 0;
5990
5991       /* Found consecutive range of addresses.  */
5992       ret = 1;
5993     }
5994   else
5995     {
5996       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5997       if (attr != NULL)
5998         {
5999           /* Value of the DW_AT_ranges attribute is the offset in the
6000              .debug_ranges section.  */
6001           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6002             return 0;
6003           /* Found discontinuous range of addresses.  */
6004           ret = -1;
6005         }
6006     }
6007
6008   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6009   if (high <= low)
6010     return 0;
6011
6012   /* When using the GNU linker, .gnu.linkonce. sections are used to
6013      eliminate duplicate copies of functions and vtables and such.
6014      The linker will arbitrarily choose one and discard the others.
6015      The AT_*_pc values for such functions refer to local labels in
6016      these sections.  If the section from that file was discarded, the
6017      labels are not in the output, so the relocs get a value of 0.
6018      If this is a discarded function, mark the pc bounds as invalid,
6019      so that GDB will ignore it.  */
6020   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6021     return 0;
6022
6023   *lowpc = low;
6024   *highpc = high;
6025   return ret;
6026 }
6027
6028 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6029    its low and high PC addresses.  Do nothing if these addresses could not
6030    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6031    and HIGHPC to the high address if greater than HIGHPC.  */
6032
6033 static void
6034 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6035                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6036                                  struct dwarf2_cu *cu)
6037 {
6038   CORE_ADDR low, high;
6039   struct die_info *child = die->child;
6040
6041   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6042     {
6043       *lowpc = min (*lowpc, low);
6044       *highpc = max (*highpc, high);
6045     }
6046
6047   /* If the language does not allow nested subprograms (either inside
6048      subprograms or lexical blocks), we're done.  */
6049   if (cu->language != language_ada)
6050     return;
6051
6052   /* Check all the children of the given DIE.  If it contains nested
6053      subprograms, then check their pc bounds.  Likewise, we need to
6054      check lexical blocks as well, as they may also contain subprogram
6055      definitions.  */
6056   while (child && child->tag)
6057     {
6058       if (child->tag == DW_TAG_subprogram
6059           || child->tag == DW_TAG_lexical_block)
6060         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6061       child = sibling_die (child);
6062     }
6063 }
6064
6065 /* Get the low and high pc's represented by the scope DIE, and store
6066    them in *LOWPC and *HIGHPC.  If the correct values can't be
6067    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6068
6069 static void
6070 get_scope_pc_bounds (struct die_info *die,
6071                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6072                      struct dwarf2_cu *cu)
6073 {
6074   CORE_ADDR best_low = (CORE_ADDR) -1;
6075   CORE_ADDR best_high = (CORE_ADDR) 0;
6076   CORE_ADDR current_low, current_high;
6077
6078   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6079     {
6080       best_low = current_low;
6081       best_high = current_high;
6082     }
6083   else
6084     {
6085       struct die_info *child = die->child;
6086
6087       while (child && child->tag)
6088         {
6089           switch (child->tag) {
6090           case DW_TAG_subprogram:
6091             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6092             break;
6093           case DW_TAG_namespace:
6094           case DW_TAG_module:
6095             /* FIXME: carlton/2004-01-16: Should we do this for
6096                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6097                that current GCC's always emit the DIEs corresponding
6098                to definitions of methods of classes as children of a
6099                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6100                the DIEs giving the declarations, which could be
6101                anywhere).  But I don't see any reason why the
6102                standards says that they have to be there.  */
6103             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6104
6105             if (current_low != ((CORE_ADDR) -1))
6106               {
6107                 best_low = min (best_low, current_low);
6108                 best_high = max (best_high, current_high);
6109               }
6110             break;
6111           default:
6112             /* Ignore.  */
6113             break;
6114           }
6115
6116           child = sibling_die (child);
6117         }
6118     }
6119
6120   *lowpc = best_low;
6121   *highpc = best_high;
6122 }
6123
6124 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6125    in DIE.  */
6126 static void
6127 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6128                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6129 {
6130   struct attribute *attr;
6131
6132   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6133   if (attr)
6134     {
6135       CORE_ADDR high = DW_ADDR (attr);
6136
6137       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6138       if (attr)
6139         {
6140           CORE_ADDR low = DW_ADDR (attr);
6141
6142           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6143         }
6144     }
6145
6146   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6147   if (attr)
6148     {
6149       bfd *obfd = cu->objfile->obfd;
6150
6151       /* The value of the DW_AT_ranges attribute is the offset of the
6152          address range list in the .debug_ranges section.  */
6153       unsigned long offset = DW_UNSND (attr);
6154       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6155
6156       /* For some target architectures, but not others, the
6157          read_address function sign-extends the addresses it returns.
6158          To recognize base address selection entries, we need a
6159          mask.  */
6160       unsigned int addr_size = cu->header.addr_size;
6161       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6162
6163       /* The base address, to which the next pair is relative.  Note
6164          that this 'base' is a DWARF concept: most entries in a range
6165          list are relative, to reduce the number of relocs against the
6166          debugging information.  This is separate from this function's
6167          'baseaddr' argument, which GDB uses to relocate debugging
6168          information from a shared library based on the address at
6169          which the library was loaded.  */
6170       CORE_ADDR base = cu->base_address;
6171       int base_known = cu->base_known;
6172
6173       gdb_assert (dwarf2_per_objfile->ranges.readin);
6174       if (offset >= dwarf2_per_objfile->ranges.size)
6175         {
6176           complaint (&symfile_complaints,
6177                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6178                      offset);
6179           return;
6180         }
6181
6182       for (;;)
6183         {
6184           unsigned int bytes_read;
6185           CORE_ADDR start, end;
6186
6187           start = read_address (obfd, buffer, cu, &bytes_read);
6188           buffer += bytes_read;
6189           end = read_address (obfd, buffer, cu, &bytes_read);
6190           buffer += bytes_read;
6191
6192           /* Did we find the end of the range list?  */
6193           if (start == 0 && end == 0)
6194             break;
6195
6196           /* Did we find a base address selection entry?  */
6197           else if ((start & base_select_mask) == base_select_mask)
6198             {
6199               base = end;
6200               base_known = 1;
6201             }
6202
6203           /* We found an ordinary address range.  */
6204           else
6205             {
6206               if (!base_known)
6207                 {
6208                   complaint (&symfile_complaints,
6209                              _("Invalid .debug_ranges data "
6210                                "(no base address)"));
6211                   return;
6212                 }
6213
6214               if (start > end)
6215                 {
6216                   /* Inverted range entries are invalid.  */
6217                   complaint (&symfile_complaints,
6218                              _("Invalid .debug_ranges data "
6219                                "(inverted range)"));
6220                   return;
6221                 }
6222
6223               /* Empty range entries have no effect.  */
6224               if (start == end)
6225                 continue;
6226
6227               record_block_range (block,
6228                                   baseaddr + base + start,
6229                                   baseaddr + base + end - 1);
6230             }
6231         }
6232     }
6233 }
6234
6235 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6236    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6237    during 4.6.0 experimental.  */
6238
6239 static int
6240 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6241 {
6242   const char *cs;
6243   int major, minor, release;
6244
6245   if (cu->producer == NULL)
6246     {
6247       /* For unknown compilers expect their behavior is DWARF version
6248          compliant.
6249
6250          GCC started to support .debug_types sections by -gdwarf-4 since
6251          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6252          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6253          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6254          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6255
6256       return 0;
6257     }
6258
6259   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6260
6261   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6262     {
6263       /* For non-GCC compilers expect their behavior is DWARF version
6264          compliant.  */
6265
6266       return 0;
6267     }
6268   cs = &cu->producer[strlen ("GNU ")];
6269   while (*cs && !isdigit (*cs))
6270     cs++;
6271   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6272     {
6273       /* Not recognized as GCC.  */
6274
6275       return 0;
6276     }
6277
6278   return major < 4 || (major == 4 && minor < 6);
6279 }
6280
6281 /* Return the default accessibility type if it is not overriden by
6282    DW_AT_accessibility.  */
6283
6284 static enum dwarf_access_attribute
6285 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6286 {
6287   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6288     {
6289       /* The default DWARF 2 accessibility for members is public, the default
6290          accessibility for inheritance is private.  */
6291
6292       if (die->tag != DW_TAG_inheritance)
6293         return DW_ACCESS_public;
6294       else
6295         return DW_ACCESS_private;
6296     }
6297   else
6298     {
6299       /* DWARF 3+ defines the default accessibility a different way.  The same
6300          rules apply now for DW_TAG_inheritance as for the members and it only
6301          depends on the container kind.  */
6302
6303       if (die->parent->tag == DW_TAG_class_type)
6304         return DW_ACCESS_private;
6305       else
6306         return DW_ACCESS_public;
6307     }
6308 }
6309
6310 /* Add an aggregate field to the field list.  */
6311
6312 static void
6313 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6314                   struct dwarf2_cu *cu)
6315 {
6316   struct objfile *objfile = cu->objfile;
6317   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6318   struct nextfield *new_field;
6319   struct attribute *attr;
6320   struct field *fp;
6321   char *fieldname = "";
6322
6323   /* Allocate a new field list entry and link it in.  */
6324   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6325   make_cleanup (xfree, new_field);
6326   memset (new_field, 0, sizeof (struct nextfield));
6327
6328   if (die->tag == DW_TAG_inheritance)
6329     {
6330       new_field->next = fip->baseclasses;
6331       fip->baseclasses = new_field;
6332     }
6333   else
6334     {
6335       new_field->next = fip->fields;
6336       fip->fields = new_field;
6337     }
6338   fip->nfields++;
6339
6340   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6341   if (attr)
6342     new_field->accessibility = DW_UNSND (attr);
6343   else
6344     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6345   if (new_field->accessibility != DW_ACCESS_public)
6346     fip->non_public_fields = 1;
6347
6348   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6349   if (attr)
6350     new_field->virtuality = DW_UNSND (attr);
6351   else
6352     new_field->virtuality = DW_VIRTUALITY_none;
6353
6354   fp = &new_field->field;
6355
6356   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6357     {
6358       /* Data member other than a C++ static data member.  */
6359
6360       /* Get type of field.  */
6361       fp->type = die_type (die, cu);
6362
6363       SET_FIELD_BITPOS (*fp, 0);
6364
6365       /* Get bit size of field (zero if none).  */
6366       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6367       if (attr)
6368         {
6369           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6370         }
6371       else
6372         {
6373           FIELD_BITSIZE (*fp) = 0;
6374         }
6375
6376       /* Get bit offset of field.  */
6377       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6378       if (attr)
6379         {
6380           int byte_offset = 0;
6381
6382           if (attr_form_is_section_offset (attr))
6383             dwarf2_complex_location_expr_complaint ();
6384           else if (attr_form_is_constant (attr))
6385             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6386           else if (attr_form_is_block (attr))
6387             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6388           else
6389             dwarf2_complex_location_expr_complaint ();
6390
6391           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6392         }
6393       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6394       if (attr)
6395         {
6396           if (gdbarch_bits_big_endian (gdbarch))
6397             {
6398               /* For big endian bits, the DW_AT_bit_offset gives the
6399                  additional bit offset from the MSB of the containing
6400                  anonymous object to the MSB of the field.  We don't
6401                  have to do anything special since we don't need to
6402                  know the size of the anonymous object.  */
6403               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6404             }
6405           else
6406             {
6407               /* For little endian bits, compute the bit offset to the
6408                  MSB of the anonymous object, subtract off the number of
6409                  bits from the MSB of the field to the MSB of the
6410                  object, and then subtract off the number of bits of
6411                  the field itself.  The result is the bit offset of
6412                  the LSB of the field.  */
6413               int anonymous_size;
6414               int bit_offset = DW_UNSND (attr);
6415
6416               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6417               if (attr)
6418                 {
6419                   /* The size of the anonymous object containing
6420                      the bit field is explicit, so use the
6421                      indicated size (in bytes).  */
6422                   anonymous_size = DW_UNSND (attr);
6423                 }
6424               else
6425                 {
6426                   /* The size of the anonymous object containing
6427                      the bit field must be inferred from the type
6428                      attribute of the data member containing the
6429                      bit field.  */
6430                   anonymous_size = TYPE_LENGTH (fp->type);
6431                 }
6432               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6433                 - bit_offset - FIELD_BITSIZE (*fp);
6434             }
6435         }
6436
6437       /* Get name of field.  */
6438       fieldname = dwarf2_name (die, cu);
6439       if (fieldname == NULL)
6440         fieldname = "";
6441
6442       /* The name is already allocated along with this objfile, so we don't
6443          need to duplicate it for the type.  */
6444       fp->name = fieldname;
6445
6446       /* Change accessibility for artificial fields (e.g. virtual table
6447          pointer or virtual base class pointer) to private.  */
6448       if (dwarf2_attr (die, DW_AT_artificial, cu))
6449         {
6450           FIELD_ARTIFICIAL (*fp) = 1;
6451           new_field->accessibility = DW_ACCESS_private;
6452           fip->non_public_fields = 1;
6453         }
6454     }
6455   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6456     {
6457       /* C++ static member.  */
6458
6459       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6460          is a declaration, but all versions of G++ as of this writing
6461          (so through at least 3.2.1) incorrectly generate
6462          DW_TAG_variable tags.  */
6463
6464       char *physname;
6465
6466       /* Get name of field.  */
6467       fieldname = dwarf2_name (die, cu);
6468       if (fieldname == NULL)
6469         return;
6470
6471       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6472       if (attr
6473           /* Only create a symbol if this is an external value.
6474              new_symbol checks this and puts the value in the global symbol
6475              table, which we want.  If it is not external, new_symbol
6476              will try to put the value in cu->list_in_scope which is wrong.  */
6477           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6478         {
6479           /* A static const member, not much different than an enum as far as
6480              we're concerned, except that we can support more types.  */
6481           new_symbol (die, NULL, cu);
6482         }
6483
6484       /* Get physical name.  */
6485       physname = (char *) dwarf2_physname (fieldname, die, cu);
6486
6487       /* The name is already allocated along with this objfile, so we don't
6488          need to duplicate it for the type.  */
6489       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6490       FIELD_TYPE (*fp) = die_type (die, cu);
6491       FIELD_NAME (*fp) = fieldname;
6492     }
6493   else if (die->tag == DW_TAG_inheritance)
6494     {
6495       /* C++ base class field.  */
6496       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6497       if (attr)
6498         {
6499           int byte_offset = 0;
6500
6501           if (attr_form_is_section_offset (attr))
6502             dwarf2_complex_location_expr_complaint ();
6503           else if (attr_form_is_constant (attr))
6504             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6505           else if (attr_form_is_block (attr))
6506             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6507           else
6508             dwarf2_complex_location_expr_complaint ();
6509
6510           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6511         }
6512       FIELD_BITSIZE (*fp) = 0;
6513       FIELD_TYPE (*fp) = die_type (die, cu);
6514       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6515       fip->nbaseclasses++;
6516     }
6517 }
6518
6519 /* Add a typedef defined in the scope of the FIP's class.  */
6520
6521 static void
6522 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6523                     struct dwarf2_cu *cu)
6524 {
6525   struct objfile *objfile = cu->objfile;
6526   struct typedef_field_list *new_field;
6527   struct attribute *attr;
6528   struct typedef_field *fp;
6529   char *fieldname = "";
6530
6531   /* Allocate a new field list entry and link it in.  */
6532   new_field = xzalloc (sizeof (*new_field));
6533   make_cleanup (xfree, new_field);
6534
6535   gdb_assert (die->tag == DW_TAG_typedef);
6536
6537   fp = &new_field->field;
6538
6539   /* Get name of field.  */
6540   fp->name = dwarf2_name (die, cu);
6541   if (fp->name == NULL)
6542     return;
6543
6544   fp->type = read_type_die (die, cu);
6545
6546   new_field->next = fip->typedef_field_list;
6547   fip->typedef_field_list = new_field;
6548   fip->typedef_field_list_count++;
6549 }
6550
6551 /* Create the vector of fields, and attach it to the type.  */
6552
6553 static void
6554 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6555                               struct dwarf2_cu *cu)
6556 {
6557   int nfields = fip->nfields;
6558
6559   /* Record the field count, allocate space for the array of fields,
6560      and create blank accessibility bitfields if necessary.  */
6561   TYPE_NFIELDS (type) = nfields;
6562   TYPE_FIELDS (type) = (struct field *)
6563     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6564   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6565
6566   if (fip->non_public_fields && cu->language != language_ada)
6567     {
6568       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6569
6570       TYPE_FIELD_PRIVATE_BITS (type) =
6571         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6572       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6573
6574       TYPE_FIELD_PROTECTED_BITS (type) =
6575         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6576       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6577
6578       TYPE_FIELD_IGNORE_BITS (type) =
6579         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6580       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6581     }
6582
6583   /* If the type has baseclasses, allocate and clear a bit vector for
6584      TYPE_FIELD_VIRTUAL_BITS.  */
6585   if (fip->nbaseclasses && cu->language != language_ada)
6586     {
6587       int num_bytes = B_BYTES (fip->nbaseclasses);
6588       unsigned char *pointer;
6589
6590       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6591       pointer = TYPE_ALLOC (type, num_bytes);
6592       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6593       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6594       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6595     }
6596
6597   /* Copy the saved-up fields into the field vector.  Start from the head of
6598      the list, adding to the tail of the field array, so that they end up in
6599      the same order in the array in which they were added to the list.  */
6600   while (nfields-- > 0)
6601     {
6602       struct nextfield *fieldp;
6603
6604       if (fip->fields)
6605         {
6606           fieldp = fip->fields;
6607           fip->fields = fieldp->next;
6608         }
6609       else
6610         {
6611           fieldp = fip->baseclasses;
6612           fip->baseclasses = fieldp->next;
6613         }
6614
6615       TYPE_FIELD (type, nfields) = fieldp->field;
6616       switch (fieldp->accessibility)
6617         {
6618         case DW_ACCESS_private:
6619           if (cu->language != language_ada)
6620             SET_TYPE_FIELD_PRIVATE (type, nfields);
6621           break;
6622
6623         case DW_ACCESS_protected:
6624           if (cu->language != language_ada)
6625             SET_TYPE_FIELD_PROTECTED (type, nfields);
6626           break;
6627
6628         case DW_ACCESS_public:
6629           break;
6630
6631         default:
6632           /* Unknown accessibility.  Complain and treat it as public.  */
6633           {
6634             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6635                        fieldp->accessibility);
6636           }
6637           break;
6638         }
6639       if (nfields < fip->nbaseclasses)
6640         {
6641           switch (fieldp->virtuality)
6642             {
6643             case DW_VIRTUALITY_virtual:
6644             case DW_VIRTUALITY_pure_virtual:
6645               if (cu->language == language_ada)
6646                 error (_("unexpected virtuality in component of Ada type"));
6647               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6648               break;
6649             }
6650         }
6651     }
6652 }
6653
6654 /* Add a member function to the proper fieldlist.  */
6655
6656 static void
6657 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6658                       struct type *type, struct dwarf2_cu *cu)
6659 {
6660   struct objfile *objfile = cu->objfile;
6661   struct attribute *attr;
6662   struct fnfieldlist *flp;
6663   int i;
6664   struct fn_field *fnp;
6665   char *fieldname;
6666   struct nextfnfield *new_fnfield;
6667   struct type *this_type;
6668   enum dwarf_access_attribute accessibility;
6669
6670   if (cu->language == language_ada)
6671     error (_("unexpected member function in Ada type"));
6672
6673   /* Get name of member function.  */
6674   fieldname = dwarf2_name (die, cu);
6675   if (fieldname == NULL)
6676     return;
6677
6678   /* Look up member function name in fieldlist.  */
6679   for (i = 0; i < fip->nfnfields; i++)
6680     {
6681       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6682         break;
6683     }
6684
6685   /* Create new list element if necessary.  */
6686   if (i < fip->nfnfields)
6687     flp = &fip->fnfieldlists[i];
6688   else
6689     {
6690       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6691         {
6692           fip->fnfieldlists = (struct fnfieldlist *)
6693             xrealloc (fip->fnfieldlists,
6694                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6695                       * sizeof (struct fnfieldlist));
6696           if (fip->nfnfields == 0)
6697             make_cleanup (free_current_contents, &fip->fnfieldlists);
6698         }
6699       flp = &fip->fnfieldlists[fip->nfnfields];
6700       flp->name = fieldname;
6701       flp->length = 0;
6702       flp->head = NULL;
6703       i = fip->nfnfields++;
6704     }
6705
6706   /* Create a new member function field and chain it to the field list
6707      entry.  */
6708   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6709   make_cleanup (xfree, new_fnfield);
6710   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6711   new_fnfield->next = flp->head;
6712   flp->head = new_fnfield;
6713   flp->length++;
6714
6715   /* Fill in the member function field info.  */
6716   fnp = &new_fnfield->fnfield;
6717
6718   /* Delay processing of the physname until later.  */
6719   if (cu->language == language_cplus || cu->language == language_java)
6720     {
6721       add_to_method_list (type, i, flp->length - 1, fieldname,
6722                           die, cu);
6723     }
6724   else
6725     {
6726       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6727       fnp->physname = physname ? physname : "";
6728     }
6729
6730   fnp->type = alloc_type (objfile);
6731   this_type = read_type_die (die, cu);
6732   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6733     {
6734       int nparams = TYPE_NFIELDS (this_type);
6735
6736       /* TYPE is the domain of this method, and THIS_TYPE is the type
6737            of the method itself (TYPE_CODE_METHOD).  */
6738       smash_to_method_type (fnp->type, type,
6739                             TYPE_TARGET_TYPE (this_type),
6740                             TYPE_FIELDS (this_type),
6741                             TYPE_NFIELDS (this_type),
6742                             TYPE_VARARGS (this_type));
6743
6744       /* Handle static member functions.
6745          Dwarf2 has no clean way to discern C++ static and non-static
6746          member functions.  G++ helps GDB by marking the first
6747          parameter for non-static member functions (which is the this
6748          pointer) as artificial.  We obtain this information from
6749          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6750       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6751         fnp->voffset = VOFFSET_STATIC;
6752     }
6753   else
6754     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6755                dwarf2_full_name (fieldname, die, cu));
6756
6757   /* Get fcontext from DW_AT_containing_type if present.  */
6758   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6759     fnp->fcontext = die_containing_type (die, cu);
6760
6761   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6762      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6763
6764   /* Get accessibility.  */
6765   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6766   if (attr)
6767     accessibility = DW_UNSND (attr);
6768   else
6769     accessibility = dwarf2_default_access_attribute (die, cu);
6770   switch (accessibility)
6771     {
6772     case DW_ACCESS_private:
6773       fnp->is_private = 1;
6774       break;
6775     case DW_ACCESS_protected:
6776       fnp->is_protected = 1;
6777       break;
6778     }
6779
6780   /* Check for artificial methods.  */
6781   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6782   if (attr && DW_UNSND (attr) != 0)
6783     fnp->is_artificial = 1;
6784
6785   /* Get index in virtual function table if it is a virtual member
6786      function.  For older versions of GCC, this is an offset in the
6787      appropriate virtual table, as specified by DW_AT_containing_type.
6788      For everyone else, it is an expression to be evaluated relative
6789      to the object address.  */
6790
6791   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6792   if (attr)
6793     {
6794       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6795         {
6796           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6797             {
6798               /* Old-style GCC.  */
6799               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6800             }
6801           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6802                    || (DW_BLOCK (attr)->size > 1
6803                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6804                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6805             {
6806               struct dwarf_block blk;
6807               int offset;
6808
6809               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6810                         ? 1 : 2);
6811               blk.size = DW_BLOCK (attr)->size - offset;
6812               blk.data = DW_BLOCK (attr)->data + offset;
6813               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6814               if ((fnp->voffset % cu->header.addr_size) != 0)
6815                 dwarf2_complex_location_expr_complaint ();
6816               else
6817                 fnp->voffset /= cu->header.addr_size;
6818               fnp->voffset += 2;
6819             }
6820           else
6821             dwarf2_complex_location_expr_complaint ();
6822
6823           if (!fnp->fcontext)
6824             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6825         }
6826       else if (attr_form_is_section_offset (attr))
6827         {
6828           dwarf2_complex_location_expr_complaint ();
6829         }
6830       else
6831         {
6832           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6833                                                  fieldname);
6834         }
6835     }
6836   else
6837     {
6838       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6839       if (attr && DW_UNSND (attr))
6840         {
6841           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6842           complaint (&symfile_complaints,
6843                      _("Member function \"%s\" (offset %d) is virtual "
6844                        "but the vtable offset is not specified"),
6845                      fieldname, die->offset);
6846           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6847           TYPE_CPLUS_DYNAMIC (type) = 1;
6848         }
6849     }
6850 }
6851
6852 /* Create the vector of member function fields, and attach it to the type.  */
6853
6854 static void
6855 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6856                                  struct dwarf2_cu *cu)
6857 {
6858   struct fnfieldlist *flp;
6859   int total_length = 0;
6860   int i;
6861
6862   if (cu->language == language_ada)
6863     error (_("unexpected member functions in Ada type"));
6864
6865   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6866   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6867     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6868
6869   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6870     {
6871       struct nextfnfield *nfp = flp->head;
6872       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6873       int k;
6874
6875       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6876       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6877       fn_flp->fn_fields = (struct fn_field *)
6878         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6879       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6880         fn_flp->fn_fields[k] = nfp->fnfield;
6881
6882       total_length += flp->length;
6883     }
6884
6885   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6886   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6887 }
6888
6889 /* Returns non-zero if NAME is the name of a vtable member in CU's
6890    language, zero otherwise.  */
6891 static int
6892 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6893 {
6894   static const char vptr[] = "_vptr";
6895   static const char vtable[] = "vtable";
6896
6897   /* Look for the C++ and Java forms of the vtable.  */
6898   if ((cu->language == language_java
6899        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6900        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6901        && is_cplus_marker (name[sizeof (vptr) - 1])))
6902     return 1;
6903
6904   return 0;
6905 }
6906
6907 /* GCC outputs unnamed structures that are really pointers to member
6908    functions, with the ABI-specified layout.  If TYPE describes
6909    such a structure, smash it into a member function type.
6910
6911    GCC shouldn't do this; it should just output pointer to member DIEs.
6912    This is GCC PR debug/28767.  */
6913
6914 static void
6915 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6916 {
6917   struct type *pfn_type, *domain_type, *new_type;
6918
6919   /* Check for a structure with no name and two children.  */
6920   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6921     return;
6922
6923   /* Check for __pfn and __delta members.  */
6924   if (TYPE_FIELD_NAME (type, 0) == NULL
6925       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6926       || TYPE_FIELD_NAME (type, 1) == NULL
6927       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6928     return;
6929
6930   /* Find the type of the method.  */
6931   pfn_type = TYPE_FIELD_TYPE (type, 0);
6932   if (pfn_type == NULL
6933       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6934       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6935     return;
6936
6937   /* Look for the "this" argument.  */
6938   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6939   if (TYPE_NFIELDS (pfn_type) == 0
6940       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6941       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6942     return;
6943
6944   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6945   new_type = alloc_type (objfile);
6946   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6947                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6948                         TYPE_VARARGS (pfn_type));
6949   smash_to_methodptr_type (type, new_type);
6950 }
6951
6952 /* Called when we find the DIE that starts a structure or union scope
6953    (definition) to create a type for the structure or union.  Fill in
6954    the type's name and general properties; the members will not be
6955    processed until process_structure_type.
6956
6957    NOTE: we need to call these functions regardless of whether or not the
6958    DIE has a DW_AT_name attribute, since it might be an anonymous
6959    structure or union.  This gets the type entered into our set of
6960    user defined types.
6961
6962    However, if the structure is incomplete (an opaque struct/union)
6963    then suppress creating a symbol table entry for it since gdb only
6964    wants to find the one with the complete definition.  Note that if
6965    it is complete, we just call new_symbol, which does it's own
6966    checking about whether the struct/union is anonymous or not (and
6967    suppresses creating a symbol table entry itself).  */
6968
6969 static struct type *
6970 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6971 {
6972   struct objfile *objfile = cu->objfile;
6973   struct type *type;
6974   struct attribute *attr;
6975   char *name;
6976
6977   /* If the definition of this type lives in .debug_types, read that type.
6978      Don't follow DW_AT_specification though, that will take us back up
6979      the chain and we want to go down.  */
6980   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6981   if (attr)
6982     {
6983       struct dwarf2_cu *type_cu = cu;
6984       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6985
6986       /* We could just recurse on read_structure_type, but we need to call
6987          get_die_type to ensure only one type for this DIE is created.
6988          This is important, for example, because for c++ classes we need
6989          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6990       type = read_type_die (type_die, type_cu);
6991
6992       /* TYPE_CU may not be the same as CU.
6993          Ensure TYPE is recorded in CU's type_hash table.  */
6994       return set_die_type (die, type, cu);
6995     }
6996
6997   type = alloc_type (objfile);
6998   INIT_CPLUS_SPECIFIC (type);
6999
7000   name = dwarf2_name (die, cu);
7001   if (name != NULL)
7002     {
7003       if (cu->language == language_cplus
7004           || cu->language == language_java)
7005         {
7006           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7007
7008           /* dwarf2_full_name might have already finished building the DIE's
7009              type.  If so, there is no need to continue.  */
7010           if (get_die_type (die, cu) != NULL)
7011             return get_die_type (die, cu);
7012
7013           TYPE_TAG_NAME (type) = full_name;
7014           if (die->tag == DW_TAG_structure_type
7015               || die->tag == DW_TAG_class_type)
7016             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7017         }
7018       else
7019         {
7020           /* The name is already allocated along with this objfile, so
7021              we don't need to duplicate it for the type.  */
7022           TYPE_TAG_NAME (type) = (char *) name;
7023           if (die->tag == DW_TAG_class_type)
7024             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7025         }
7026     }
7027
7028   if (die->tag == DW_TAG_structure_type)
7029     {
7030       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7031     }
7032   else if (die->tag == DW_TAG_union_type)
7033     {
7034       TYPE_CODE (type) = TYPE_CODE_UNION;
7035     }
7036   else
7037     {
7038       TYPE_CODE (type) = TYPE_CODE_CLASS;
7039     }
7040
7041   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7042     TYPE_DECLARED_CLASS (type) = 1;
7043
7044   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7045   if (attr)
7046     {
7047       TYPE_LENGTH (type) = DW_UNSND (attr);
7048     }
7049   else
7050     {
7051       TYPE_LENGTH (type) = 0;
7052     }
7053
7054   TYPE_STUB_SUPPORTED (type) = 1;
7055   if (die_is_declaration (die, cu))
7056     TYPE_STUB (type) = 1;
7057   else if (attr == NULL && die->child == NULL
7058            && producer_is_realview (cu->producer))
7059     /* RealView does not output the required DW_AT_declaration
7060        on incomplete types.  */
7061     TYPE_STUB (type) = 1;
7062
7063   /* We need to add the type field to the die immediately so we don't
7064      infinitely recurse when dealing with pointers to the structure
7065      type within the structure itself.  */
7066   set_die_type (die, type, cu);
7067
7068   /* set_die_type should be already done.  */
7069   set_descriptive_type (type, die, cu);
7070
7071   return type;
7072 }
7073
7074 /* Finish creating a structure or union type, including filling in
7075    its members and creating a symbol for it.  */
7076
7077 static void
7078 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7079 {
7080   struct objfile *objfile = cu->objfile;
7081   struct die_info *child_die = die->child;
7082   struct type *type;
7083
7084   type = get_die_type (die, cu);
7085   if (type == NULL)
7086     type = read_structure_type (die, cu);
7087
7088   if (die->child != NULL && ! die_is_declaration (die, cu))
7089     {
7090       struct field_info fi;
7091       struct die_info *child_die;
7092       VEC (symbolp) *template_args = NULL;
7093       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7094
7095       memset (&fi, 0, sizeof (struct field_info));
7096
7097       child_die = die->child;
7098
7099       while (child_die && child_die->tag)
7100         {
7101           if (child_die->tag == DW_TAG_member
7102               || child_die->tag == DW_TAG_variable)
7103             {
7104               /* NOTE: carlton/2002-11-05: A C++ static data member
7105                  should be a DW_TAG_member that is a declaration, but
7106                  all versions of G++ as of this writing (so through at
7107                  least 3.2.1) incorrectly generate DW_TAG_variable
7108                  tags for them instead.  */
7109               dwarf2_add_field (&fi, child_die, cu);
7110             }
7111           else if (child_die->tag == DW_TAG_subprogram)
7112             {
7113               /* C++ member function.  */
7114               dwarf2_add_member_fn (&fi, child_die, type, cu);
7115             }
7116           else if (child_die->tag == DW_TAG_inheritance)
7117             {
7118               /* C++ base class field.  */
7119               dwarf2_add_field (&fi, child_die, cu);
7120             }
7121           else if (child_die->tag == DW_TAG_typedef)
7122             dwarf2_add_typedef (&fi, child_die, cu);
7123           else if (child_die->tag == DW_TAG_template_type_param
7124                    || child_die->tag == DW_TAG_template_value_param)
7125             {
7126               struct symbol *arg = new_symbol (child_die, NULL, cu);
7127
7128               if (arg != NULL)
7129                 VEC_safe_push (symbolp, template_args, arg);
7130             }
7131
7132           child_die = sibling_die (child_die);
7133         }
7134
7135       /* Attach template arguments to type.  */
7136       if (! VEC_empty (symbolp, template_args))
7137         {
7138           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7139           TYPE_N_TEMPLATE_ARGUMENTS (type)
7140             = VEC_length (symbolp, template_args);
7141           TYPE_TEMPLATE_ARGUMENTS (type)
7142             = obstack_alloc (&objfile->objfile_obstack,
7143                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7144                               * sizeof (struct symbol *)));
7145           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7146                   VEC_address (symbolp, template_args),
7147                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7148                    * sizeof (struct symbol *)));
7149           VEC_free (symbolp, template_args);
7150         }
7151
7152       /* Attach fields and member functions to the type.  */
7153       if (fi.nfields)
7154         dwarf2_attach_fields_to_type (&fi, type, cu);
7155       if (fi.nfnfields)
7156         {
7157           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7158
7159           /* Get the type which refers to the base class (possibly this
7160              class itself) which contains the vtable pointer for the current
7161              class from the DW_AT_containing_type attribute.  This use of
7162              DW_AT_containing_type is a GNU extension.  */
7163
7164           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7165             {
7166               struct type *t = die_containing_type (die, cu);
7167
7168               TYPE_VPTR_BASETYPE (type) = t;
7169               if (type == t)
7170                 {
7171                   int i;
7172
7173                   /* Our own class provides vtbl ptr.  */
7174                   for (i = TYPE_NFIELDS (t) - 1;
7175                        i >= TYPE_N_BASECLASSES (t);
7176                        --i)
7177                     {
7178                       char *fieldname = TYPE_FIELD_NAME (t, i);
7179
7180                       if (is_vtable_name (fieldname, cu))
7181                         {
7182                           TYPE_VPTR_FIELDNO (type) = i;
7183                           break;
7184                         }
7185                     }
7186
7187                   /* Complain if virtual function table field not found.  */
7188                   if (i < TYPE_N_BASECLASSES (t))
7189                     complaint (&symfile_complaints,
7190                                _("virtual function table pointer "
7191                                  "not found when defining class '%s'"),
7192                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7193                                "");
7194                 }
7195               else
7196                 {
7197                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7198                 }
7199             }
7200           else if (cu->producer
7201                    && strncmp (cu->producer,
7202                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7203             {
7204               /* The IBM XLC compiler does not provide direct indication
7205                  of the containing type, but the vtable pointer is
7206                  always named __vfp.  */
7207
7208               int i;
7209
7210               for (i = TYPE_NFIELDS (type) - 1;
7211                    i >= TYPE_N_BASECLASSES (type);
7212                    --i)
7213                 {
7214                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7215                     {
7216                       TYPE_VPTR_FIELDNO (type) = i;
7217                       TYPE_VPTR_BASETYPE (type) = type;
7218                       break;
7219                     }
7220                 }
7221             }
7222         }
7223
7224       /* Copy fi.typedef_field_list linked list elements content into the
7225          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7226       if (fi.typedef_field_list)
7227         {
7228           int i = fi.typedef_field_list_count;
7229
7230           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7231           TYPE_TYPEDEF_FIELD_ARRAY (type)
7232             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7233           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7234
7235           /* Reverse the list order to keep the debug info elements order.  */
7236           while (--i >= 0)
7237             {
7238               struct typedef_field *dest, *src;
7239
7240               dest = &TYPE_TYPEDEF_FIELD (type, i);
7241               src = &fi.typedef_field_list->field;
7242               fi.typedef_field_list = fi.typedef_field_list->next;
7243               *dest = *src;
7244             }
7245         }
7246
7247       do_cleanups (back_to);
7248     }
7249
7250   quirk_gcc_member_function_pointer (type, cu->objfile);
7251
7252   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7253      snapshots) has been known to create a die giving a declaration
7254      for a class that has, as a child, a die giving a definition for a
7255      nested class.  So we have to process our children even if the
7256      current die is a declaration.  Normally, of course, a declaration
7257      won't have any children at all.  */
7258
7259   while (child_die != NULL && child_die->tag)
7260     {
7261       if (child_die->tag == DW_TAG_member
7262           || child_die->tag == DW_TAG_variable
7263           || child_die->tag == DW_TAG_inheritance
7264           || child_die->tag == DW_TAG_template_value_param
7265           || child_die->tag == DW_TAG_template_type_param)
7266         {
7267           /* Do nothing.  */
7268         }
7269       else
7270         process_die (child_die, cu);
7271
7272       child_die = sibling_die (child_die);
7273     }
7274
7275   /* Do not consider external references.  According to the DWARF standard,
7276      these DIEs are identified by the fact that they have no byte_size
7277      attribute, and a declaration attribute.  */
7278   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7279       || !die_is_declaration (die, cu))
7280     new_symbol (die, type, cu);
7281 }
7282
7283 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7284    complete the type's fields yet, or create any symbols.  */
7285
7286 static struct type *
7287 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7288 {
7289   struct objfile *objfile = cu->objfile;
7290   struct type *type;
7291   struct attribute *attr;
7292   const char *name;
7293
7294   /* If the definition of this type lives in .debug_types, read that type.
7295      Don't follow DW_AT_specification though, that will take us back up
7296      the chain and we want to go down.  */
7297   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7298   if (attr)
7299     {
7300       struct dwarf2_cu *type_cu = cu;
7301       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7302
7303       type = read_type_die (type_die, type_cu);
7304
7305       /* TYPE_CU may not be the same as CU.
7306          Ensure TYPE is recorded in CU's type_hash table.  */
7307       return set_die_type (die, type, cu);
7308     }
7309
7310   type = alloc_type (objfile);
7311
7312   TYPE_CODE (type) = TYPE_CODE_ENUM;
7313   name = dwarf2_full_name (NULL, die, cu);
7314   if (name != NULL)
7315     TYPE_TAG_NAME (type) = (char *) name;
7316
7317   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7318   if (attr)
7319     {
7320       TYPE_LENGTH (type) = DW_UNSND (attr);
7321     }
7322   else
7323     {
7324       TYPE_LENGTH (type) = 0;
7325     }
7326
7327   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7328      declared as private in the package spec, and then defined only
7329      inside the package body.  Such types are known as Taft Amendment
7330      Types.  When another package uses such a type, an incomplete DIE
7331      may be generated by the compiler.  */
7332   if (die_is_declaration (die, cu))
7333     TYPE_STUB (type) = 1;
7334
7335   return set_die_type (die, type, cu);
7336 }
7337
7338 /* Given a pointer to a die which begins an enumeration, process all
7339    the dies that define the members of the enumeration, and create the
7340    symbol for the enumeration type.
7341
7342    NOTE: We reverse the order of the element list.  */
7343
7344 static void
7345 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7346 {
7347   struct type *this_type;
7348
7349   this_type = get_die_type (die, cu);
7350   if (this_type == NULL)
7351     this_type = read_enumeration_type (die, cu);
7352
7353   if (die->child != NULL)
7354     {
7355       struct die_info *child_die;
7356       struct symbol *sym;
7357       struct field *fields = NULL;
7358       int num_fields = 0;
7359       int unsigned_enum = 1;
7360       char *name;
7361
7362       child_die = die->child;
7363       while (child_die && child_die->tag)
7364         {
7365           if (child_die->tag != DW_TAG_enumerator)
7366             {
7367               process_die (child_die, cu);
7368             }
7369           else
7370             {
7371               name = dwarf2_name (child_die, cu);
7372               if (name)
7373                 {
7374                   sym = new_symbol (child_die, this_type, cu);
7375                   if (SYMBOL_VALUE (sym) < 0)
7376                     unsigned_enum = 0;
7377
7378                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7379                     {
7380                       fields = (struct field *)
7381                         xrealloc (fields,
7382                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7383                                   * sizeof (struct field));
7384                     }
7385
7386                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7387                   FIELD_TYPE (fields[num_fields]) = NULL;
7388                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7389                   FIELD_BITSIZE (fields[num_fields]) = 0;
7390
7391                   num_fields++;
7392                 }
7393             }
7394
7395           child_die = sibling_die (child_die);
7396         }
7397
7398       if (num_fields)
7399         {
7400           TYPE_NFIELDS (this_type) = num_fields;
7401           TYPE_FIELDS (this_type) = (struct field *)
7402             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7403           memcpy (TYPE_FIELDS (this_type), fields,
7404                   sizeof (struct field) * num_fields);
7405           xfree (fields);
7406         }
7407       if (unsigned_enum)
7408         TYPE_UNSIGNED (this_type) = 1;
7409     }
7410
7411   new_symbol (die, this_type, cu);
7412 }
7413
7414 /* Extract all information from a DW_TAG_array_type DIE and put it in
7415    the DIE's type field.  For now, this only handles one dimensional
7416    arrays.  */
7417
7418 static struct type *
7419 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7420 {
7421   struct objfile *objfile = cu->objfile;
7422   struct die_info *child_die;
7423   struct type *type;
7424   struct type *element_type, *range_type, *index_type;
7425   struct type **range_types = NULL;
7426   struct attribute *attr;
7427   int ndim = 0;
7428   struct cleanup *back_to;
7429   char *name;
7430
7431   element_type = die_type (die, cu);
7432
7433   /* The die_type call above may have already set the type for this DIE.  */
7434   type = get_die_type (die, cu);
7435   if (type)
7436     return type;
7437
7438   /* Irix 6.2 native cc creates array types without children for
7439      arrays with unspecified length.  */
7440   if (die->child == NULL)
7441     {
7442       index_type = objfile_type (objfile)->builtin_int;
7443       range_type = create_range_type (NULL, index_type, 0, -1);
7444       type = create_array_type (NULL, element_type, range_type);
7445       return set_die_type (die, type, cu);
7446     }
7447
7448   back_to = make_cleanup (null_cleanup, NULL);
7449   child_die = die->child;
7450   while (child_die && child_die->tag)
7451     {
7452       if (child_die->tag == DW_TAG_subrange_type)
7453         {
7454           struct type *child_type = read_type_die (child_die, cu);
7455
7456           if (child_type != NULL)
7457             {
7458               /* The range type was succesfully read.  Save it for the
7459                  array type creation.  */
7460               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7461                 {
7462                   range_types = (struct type **)
7463                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7464                               * sizeof (struct type *));
7465                   if (ndim == 0)
7466                     make_cleanup (free_current_contents, &range_types);
7467                 }
7468               range_types[ndim++] = child_type;
7469             }
7470         }
7471       child_die = sibling_die (child_die);
7472     }
7473
7474   /* Dwarf2 dimensions are output from left to right, create the
7475      necessary array types in backwards order.  */
7476
7477   type = element_type;
7478
7479   if (read_array_order (die, cu) == DW_ORD_col_major)
7480     {
7481       int i = 0;
7482
7483       while (i < ndim)
7484         type = create_array_type (NULL, type, range_types[i++]);
7485     }
7486   else
7487     {
7488       while (ndim-- > 0)
7489         type = create_array_type (NULL, type, range_types[ndim]);
7490     }
7491
7492   /* Understand Dwarf2 support for vector types (like they occur on
7493      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7494      array type.  This is not part of the Dwarf2/3 standard yet, but a
7495      custom vendor extension.  The main difference between a regular
7496      array and the vector variant is that vectors are passed by value
7497      to functions.  */
7498   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7499   if (attr)
7500     make_vector_type (type);
7501
7502   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7503      implementation may choose to implement triple vectors using this
7504      attribute.  */
7505   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7506   if (attr)
7507     {
7508       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7509         TYPE_LENGTH (type) = DW_UNSND (attr);
7510       else
7511         complaint (&symfile_complaints,
7512                    _("DW_AT_byte_size for array type smaller "
7513                      "than the total size of elements"));
7514     }
7515
7516   name = dwarf2_name (die, cu);
7517   if (name)
7518     TYPE_NAME (type) = name;
7519
7520   /* Install the type in the die.  */
7521   set_die_type (die, type, cu);
7522
7523   /* set_die_type should be already done.  */
7524   set_descriptive_type (type, die, cu);
7525
7526   do_cleanups (back_to);
7527
7528   return type;
7529 }
7530
7531 static enum dwarf_array_dim_ordering
7532 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7533 {
7534   struct attribute *attr;
7535
7536   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7537
7538   if (attr) return DW_SND (attr);
7539
7540   /* GNU F77 is a special case, as at 08/2004 array type info is the
7541      opposite order to the dwarf2 specification, but data is still
7542      laid out as per normal fortran.
7543
7544      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7545      version checking.  */
7546
7547   if (cu->language == language_fortran
7548       && cu->producer && strstr (cu->producer, "GNU F77"))
7549     {
7550       return DW_ORD_row_major;
7551     }
7552
7553   switch (cu->language_defn->la_array_ordering)
7554     {
7555     case array_column_major:
7556       return DW_ORD_col_major;
7557     case array_row_major:
7558     default:
7559       return DW_ORD_row_major;
7560     };
7561 }
7562
7563 /* Extract all information from a DW_TAG_set_type DIE and put it in
7564    the DIE's type field.  */
7565
7566 static struct type *
7567 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7568 {
7569   struct type *domain_type, *set_type;
7570   struct attribute *attr;
7571
7572   domain_type = die_type (die, cu);
7573
7574   /* The die_type call above may have already set the type for this DIE.  */
7575   set_type = get_die_type (die, cu);
7576   if (set_type)
7577     return set_type;
7578
7579   set_type = create_set_type (NULL, domain_type);
7580
7581   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7582   if (attr)
7583     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7584
7585   return set_die_type (die, set_type, cu);
7586 }
7587
7588 /* First cut: install each common block member as a global variable.  */
7589
7590 static void
7591 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7592 {
7593   struct die_info *child_die;
7594   struct attribute *attr;
7595   struct symbol *sym;
7596   CORE_ADDR base = (CORE_ADDR) 0;
7597
7598   attr = dwarf2_attr (die, DW_AT_location, cu);
7599   if (attr)
7600     {
7601       /* Support the .debug_loc offsets.  */
7602       if (attr_form_is_block (attr))
7603         {
7604           base = decode_locdesc (DW_BLOCK (attr), cu);
7605         }
7606       else if (attr_form_is_section_offset (attr))
7607         {
7608           dwarf2_complex_location_expr_complaint ();
7609         }
7610       else
7611         {
7612           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7613                                                  "common block member");
7614         }
7615     }
7616   if (die->child != NULL)
7617     {
7618       child_die = die->child;
7619       while (child_die && child_die->tag)
7620         {
7621           sym = new_symbol (child_die, NULL, cu);
7622           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7623           if (sym != NULL && attr != NULL)
7624             {
7625               CORE_ADDR byte_offset = 0;
7626
7627               if (attr_form_is_section_offset (attr))
7628                 dwarf2_complex_location_expr_complaint ();
7629               else if (attr_form_is_constant (attr))
7630                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7631               else if (attr_form_is_block (attr))
7632                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7633               else
7634                 dwarf2_complex_location_expr_complaint ();
7635
7636               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7637               add_symbol_to_list (sym, &global_symbols);
7638             }
7639           child_die = sibling_die (child_die);
7640         }
7641     }
7642 }
7643
7644 /* Create a type for a C++ namespace.  */
7645
7646 static struct type *
7647 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7648 {
7649   struct objfile *objfile = cu->objfile;
7650   const char *previous_prefix, *name;
7651   int is_anonymous;
7652   struct type *type;
7653
7654   /* For extensions, reuse the type of the original namespace.  */
7655   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7656     {
7657       struct die_info *ext_die;
7658       struct dwarf2_cu *ext_cu = cu;
7659
7660       ext_die = dwarf2_extension (die, &ext_cu);
7661       type = read_type_die (ext_die, ext_cu);
7662
7663       /* EXT_CU may not be the same as CU.
7664          Ensure TYPE is recorded in CU's type_hash table.  */
7665       return set_die_type (die, type, cu);
7666     }
7667
7668   name = namespace_name (die, &is_anonymous, cu);
7669
7670   /* Now build the name of the current namespace.  */
7671
7672   previous_prefix = determine_prefix (die, cu);
7673   if (previous_prefix[0] != '\0')
7674     name = typename_concat (&objfile->objfile_obstack,
7675                             previous_prefix, name, 0, cu);
7676
7677   /* Create the type.  */
7678   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7679                     objfile);
7680   TYPE_NAME (type) = (char *) name;
7681   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7682
7683   return set_die_type (die, type, cu);
7684 }
7685
7686 /* Read a C++ namespace.  */
7687
7688 static void
7689 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7690 {
7691   struct objfile *objfile = cu->objfile;
7692   int is_anonymous;
7693
7694   /* Add a symbol associated to this if we haven't seen the namespace
7695      before.  Also, add a using directive if it's an anonymous
7696      namespace.  */
7697
7698   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7699     {
7700       struct type *type;
7701
7702       type = read_type_die (die, cu);
7703       new_symbol (die, type, cu);
7704
7705       namespace_name (die, &is_anonymous, cu);
7706       if (is_anonymous)
7707         {
7708           const char *previous_prefix = determine_prefix (die, cu);
7709
7710           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7711                                   NULL, &objfile->objfile_obstack);
7712         }
7713     }
7714
7715   if (die->child != NULL)
7716     {
7717       struct die_info *child_die = die->child;
7718
7719       while (child_die && child_die->tag)
7720         {
7721           process_die (child_die, cu);
7722           child_die = sibling_die (child_die);
7723         }
7724     }
7725 }
7726
7727 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7728    imported module.  Still we need that type as local Fortran "use ... only"
7729    declaration imports depend on the created type in determine_prefix.  */
7730
7731 static struct type *
7732 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7733 {
7734   struct objfile *objfile = cu->objfile;
7735   char *module_name;
7736   struct type *type;
7737
7738   module_name = dwarf2_name (die, cu);
7739   if (!module_name)
7740     complaint (&symfile_complaints,
7741                _("DW_TAG_module has no name, offset 0x%x"),
7742                die->offset);
7743   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7744
7745   /* determine_prefix uses TYPE_TAG_NAME.  */
7746   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7747
7748   return set_die_type (die, type, cu);
7749 }
7750
7751 /* Read a Fortran module.  */
7752
7753 static void
7754 read_module (struct die_info *die, struct dwarf2_cu *cu)
7755 {
7756   struct die_info *child_die = die->child;
7757
7758   while (child_die && child_die->tag)
7759     {
7760       process_die (child_die, cu);
7761       child_die = sibling_die (child_die);
7762     }
7763 }
7764
7765 /* Return the name of the namespace represented by DIE.  Set
7766    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7767    namespace.  */
7768
7769 static const char *
7770 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7771 {
7772   struct die_info *current_die;
7773   const char *name = NULL;
7774
7775   /* Loop through the extensions until we find a name.  */
7776
7777   for (current_die = die;
7778        current_die != NULL;
7779        current_die = dwarf2_extension (die, &cu))
7780     {
7781       name = dwarf2_name (current_die, cu);
7782       if (name != NULL)
7783         break;
7784     }
7785
7786   /* Is it an anonymous namespace?  */
7787
7788   *is_anonymous = (name == NULL);
7789   if (*is_anonymous)
7790     name = "(anonymous namespace)";
7791
7792   return name;
7793 }
7794
7795 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7796    the user defined type vector.  */
7797
7798 static struct type *
7799 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7800 {
7801   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7802   struct comp_unit_head *cu_header = &cu->header;
7803   struct type *type;
7804   struct attribute *attr_byte_size;
7805   struct attribute *attr_address_class;
7806   int byte_size, addr_class;
7807   struct type *target_type;
7808
7809   target_type = die_type (die, cu);
7810
7811   /* The die_type call above may have already set the type for this DIE.  */
7812   type = get_die_type (die, cu);
7813   if (type)
7814     return type;
7815
7816   type = lookup_pointer_type (target_type);
7817
7818   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7819   if (attr_byte_size)
7820     byte_size = DW_UNSND (attr_byte_size);
7821   else
7822     byte_size = cu_header->addr_size;
7823
7824   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7825   if (attr_address_class)
7826     addr_class = DW_UNSND (attr_address_class);
7827   else
7828     addr_class = DW_ADDR_none;
7829
7830   /* If the pointer size or address class is different than the
7831      default, create a type variant marked as such and set the
7832      length accordingly.  */
7833   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7834     {
7835       if (gdbarch_address_class_type_flags_p (gdbarch))
7836         {
7837           int type_flags;
7838
7839           type_flags = gdbarch_address_class_type_flags
7840                          (gdbarch, byte_size, addr_class);
7841           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7842                       == 0);
7843           type = make_type_with_address_space (type, type_flags);
7844         }
7845       else if (TYPE_LENGTH (type) != byte_size)
7846         {
7847           complaint (&symfile_complaints,
7848                      _("invalid pointer size %d"), byte_size);
7849         }
7850       else
7851         {
7852           /* Should we also complain about unhandled address classes?  */
7853         }
7854     }
7855
7856   TYPE_LENGTH (type) = byte_size;
7857   return set_die_type (die, type, cu);
7858 }
7859
7860 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7861    the user defined type vector.  */
7862
7863 static struct type *
7864 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7865 {
7866   struct type *type;
7867   struct type *to_type;
7868   struct type *domain;
7869
7870   to_type = die_type (die, cu);
7871   domain = die_containing_type (die, cu);
7872
7873   /* The calls above may have already set the type for this DIE.  */
7874   type = get_die_type (die, cu);
7875   if (type)
7876     return type;
7877
7878   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7879     type = lookup_methodptr_type (to_type);
7880   else
7881     type = lookup_memberptr_type (to_type, domain);
7882
7883   return set_die_type (die, type, cu);
7884 }
7885
7886 /* Extract all information from a DW_TAG_reference_type DIE and add to
7887    the user defined type vector.  */
7888
7889 static struct type *
7890 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7891 {
7892   struct comp_unit_head *cu_header = &cu->header;
7893   struct type *type, *target_type;
7894   struct attribute *attr;
7895
7896   target_type = die_type (die, cu);
7897
7898   /* The die_type call above may have already set the type for this DIE.  */
7899   type = get_die_type (die, cu);
7900   if (type)
7901     return type;
7902
7903   type = lookup_reference_type (target_type);
7904   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7905   if (attr)
7906     {
7907       TYPE_LENGTH (type) = DW_UNSND (attr);
7908     }
7909   else
7910     {
7911       TYPE_LENGTH (type) = cu_header->addr_size;
7912     }
7913   return set_die_type (die, type, cu);
7914 }
7915
7916 static struct type *
7917 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7918 {
7919   struct type *base_type, *cv_type;
7920
7921   base_type = die_type (die, cu);
7922
7923   /* The die_type call above may have already set the type for this DIE.  */
7924   cv_type = get_die_type (die, cu);
7925   if (cv_type)
7926     return cv_type;
7927
7928   /* In case the const qualifier is applied to an array type, the element type
7929      is so qualified, not the array type (section 6.7.3 of C99).  */
7930   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7931     {
7932       struct type *el_type, *inner_array;
7933
7934       base_type = copy_type (base_type);
7935       inner_array = base_type;
7936
7937       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7938         {
7939           TYPE_TARGET_TYPE (inner_array) =
7940             copy_type (TYPE_TARGET_TYPE (inner_array));
7941           inner_array = TYPE_TARGET_TYPE (inner_array);
7942         }
7943
7944       el_type = TYPE_TARGET_TYPE (inner_array);
7945       TYPE_TARGET_TYPE (inner_array) =
7946         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7947
7948       return set_die_type (die, base_type, cu);
7949     }
7950
7951   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7952   return set_die_type (die, cv_type, cu);
7953 }
7954
7955 static struct type *
7956 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7957 {
7958   struct type *base_type, *cv_type;
7959
7960   base_type = die_type (die, cu);
7961
7962   /* The die_type call above may have already set the type for this DIE.  */
7963   cv_type = get_die_type (die, cu);
7964   if (cv_type)
7965     return cv_type;
7966
7967   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7968   return set_die_type (die, cv_type, cu);
7969 }
7970
7971 /* Extract all information from a DW_TAG_string_type DIE and add to
7972    the user defined type vector.  It isn't really a user defined type,
7973    but it behaves like one, with other DIE's using an AT_user_def_type
7974    attribute to reference it.  */
7975
7976 static struct type *
7977 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7978 {
7979   struct objfile *objfile = cu->objfile;
7980   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7981   struct type *type, *range_type, *index_type, *char_type;
7982   struct attribute *attr;
7983   unsigned int length;
7984
7985   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7986   if (attr)
7987     {
7988       length = DW_UNSND (attr);
7989     }
7990   else
7991     {
7992       /* Check for the DW_AT_byte_size attribute.  */
7993       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7994       if (attr)
7995         {
7996           length = DW_UNSND (attr);
7997         }
7998       else
7999         {
8000           length = 1;
8001         }
8002     }
8003
8004   index_type = objfile_type (objfile)->builtin_int;
8005   range_type = create_range_type (NULL, index_type, 1, length);
8006   char_type = language_string_char_type (cu->language_defn, gdbarch);
8007   type = create_string_type (NULL, char_type, range_type);
8008
8009   return set_die_type (die, type, cu);
8010 }
8011
8012 /* Handle DIES due to C code like:
8013
8014    struct foo
8015    {
8016    int (*funcp)(int a, long l);
8017    int b;
8018    };
8019
8020    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8021
8022 static struct type *
8023 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8024 {
8025   struct type *type;            /* Type that this function returns.  */
8026   struct type *ftype;           /* Function that returns above type.  */
8027   struct attribute *attr;
8028
8029   type = die_type (die, cu);
8030
8031   /* The die_type call above may have already set the type for this DIE.  */
8032   ftype = get_die_type (die, cu);
8033   if (ftype)
8034     return ftype;
8035
8036   ftype = lookup_function_type (type);
8037
8038   /* All functions in C++, Pascal and Java have prototypes.  */
8039   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8040   if ((attr && (DW_UNSND (attr) != 0))
8041       || cu->language == language_cplus
8042       || cu->language == language_java
8043       || cu->language == language_pascal)
8044     TYPE_PROTOTYPED (ftype) = 1;
8045   else if (producer_is_realview (cu->producer))
8046     /* RealView does not emit DW_AT_prototyped.  We can not
8047        distinguish prototyped and unprototyped functions; default to
8048        prototyped, since that is more common in modern code (and
8049        RealView warns about unprototyped functions).  */
8050     TYPE_PROTOTYPED (ftype) = 1;
8051
8052   /* Store the calling convention in the type if it's available in
8053      the subroutine die.  Otherwise set the calling convention to
8054      the default value DW_CC_normal.  */
8055   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8056   if (attr)
8057     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8058   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8059     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8060   else
8061     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8062
8063   /* We need to add the subroutine type to the die immediately so
8064      we don't infinitely recurse when dealing with parameters
8065      declared as the same subroutine type.  */
8066   set_die_type (die, ftype, cu);
8067
8068   if (die->child != NULL)
8069     {
8070       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8071       struct die_info *child_die;
8072       int nparams, iparams;
8073
8074       /* Count the number of parameters.
8075          FIXME: GDB currently ignores vararg functions, but knows about
8076          vararg member functions.  */
8077       nparams = 0;
8078       child_die = die->child;
8079       while (child_die && child_die->tag)
8080         {
8081           if (child_die->tag == DW_TAG_formal_parameter)
8082             nparams++;
8083           else if (child_die->tag == DW_TAG_unspecified_parameters)
8084             TYPE_VARARGS (ftype) = 1;
8085           child_die = sibling_die (child_die);
8086         }
8087
8088       /* Allocate storage for parameters and fill them in.  */
8089       TYPE_NFIELDS (ftype) = nparams;
8090       TYPE_FIELDS (ftype) = (struct field *)
8091         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8092
8093       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8094          even if we error out during the parameters reading below.  */
8095       for (iparams = 0; iparams < nparams; iparams++)
8096         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8097
8098       iparams = 0;
8099       child_die = die->child;
8100       while (child_die && child_die->tag)
8101         {
8102           if (child_die->tag == DW_TAG_formal_parameter)
8103             {
8104               struct type *arg_type;
8105
8106               /* DWARF version 2 has no clean way to discern C++
8107                  static and non-static member functions.  G++ helps
8108                  GDB by marking the first parameter for non-static
8109                  member functions (which is the this pointer) as
8110                  artificial.  We pass this information to
8111                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8112
8113                  DWARF version 3 added DW_AT_object_pointer, which GCC
8114                  4.5 does not yet generate.  */
8115               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8116               if (attr)
8117                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8118               else
8119                 {
8120                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8121
8122                   /* GCC/43521: In java, the formal parameter
8123                      "this" is sometimes not marked with DW_AT_artificial.  */
8124                   if (cu->language == language_java)
8125                     {
8126                       const char *name = dwarf2_name (child_die, cu);
8127
8128                       if (name && !strcmp (name, "this"))
8129                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8130                     }
8131                 }
8132               arg_type = die_type (child_die, cu);
8133
8134               /* RealView does not mark THIS as const, which the testsuite
8135                  expects.  GCC marks THIS as const in method definitions,
8136                  but not in the class specifications (GCC PR 43053).  */
8137               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8138                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8139                 {
8140                   int is_this = 0;
8141                   struct dwarf2_cu *arg_cu = cu;
8142                   const char *name = dwarf2_name (child_die, cu);
8143
8144                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8145                   if (attr)
8146                     {
8147                       /* If the compiler emits this, use it.  */
8148                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8149                         is_this = 1;
8150                     }
8151                   else if (name && strcmp (name, "this") == 0)
8152                     /* Function definitions will have the argument names.  */
8153                     is_this = 1;
8154                   else if (name == NULL && iparams == 0)
8155                     /* Declarations may not have the names, so like
8156                        elsewhere in GDB, assume an artificial first
8157                        argument is "this".  */
8158                     is_this = 1;
8159
8160                   if (is_this)
8161                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8162                                              arg_type, 0);
8163                 }
8164
8165               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8166               iparams++;
8167             }
8168           child_die = sibling_die (child_die);
8169         }
8170     }
8171
8172   return ftype;
8173 }
8174
8175 static struct type *
8176 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8177 {
8178   struct objfile *objfile = cu->objfile;
8179   const char *name = NULL;
8180   struct type *this_type;
8181
8182   name = dwarf2_full_name (NULL, die, cu);
8183   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8184                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8185   TYPE_NAME (this_type) = (char *) name;
8186   set_die_type (die, this_type, cu);
8187   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8188   return this_type;
8189 }
8190
8191 /* Find a representation of a given base type and install
8192    it in the TYPE field of the die.  */
8193
8194 static struct type *
8195 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8196 {
8197   struct objfile *objfile = cu->objfile;
8198   struct type *type;
8199   struct attribute *attr;
8200   int encoding = 0, size = 0;
8201   char *name;
8202   enum type_code code = TYPE_CODE_INT;
8203   int type_flags = 0;
8204   struct type *target_type = NULL;
8205
8206   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8207   if (attr)
8208     {
8209       encoding = DW_UNSND (attr);
8210     }
8211   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8212   if (attr)
8213     {
8214       size = DW_UNSND (attr);
8215     }
8216   name = dwarf2_name (die, cu);
8217   if (!name)
8218     {
8219       complaint (&symfile_complaints,
8220                  _("DW_AT_name missing from DW_TAG_base_type"));
8221     }
8222
8223   switch (encoding)
8224     {
8225       case DW_ATE_address:
8226         /* Turn DW_ATE_address into a void * pointer.  */
8227         code = TYPE_CODE_PTR;
8228         type_flags |= TYPE_FLAG_UNSIGNED;
8229         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8230         break;
8231       case DW_ATE_boolean:
8232         code = TYPE_CODE_BOOL;
8233         type_flags |= TYPE_FLAG_UNSIGNED;
8234         break;
8235       case DW_ATE_complex_float:
8236         code = TYPE_CODE_COMPLEX;
8237         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8238         break;
8239       case DW_ATE_decimal_float:
8240         code = TYPE_CODE_DECFLOAT;
8241         break;
8242       case DW_ATE_float:
8243         code = TYPE_CODE_FLT;
8244         break;
8245       case DW_ATE_signed:
8246         break;
8247       case DW_ATE_unsigned:
8248         type_flags |= TYPE_FLAG_UNSIGNED;
8249         break;
8250       case DW_ATE_signed_char:
8251         if (cu->language == language_ada || cu->language == language_m2
8252             || cu->language == language_pascal)
8253           code = TYPE_CODE_CHAR;
8254         break;
8255       case DW_ATE_unsigned_char:
8256         if (cu->language == language_ada || cu->language == language_m2
8257             || cu->language == language_pascal)
8258           code = TYPE_CODE_CHAR;
8259         type_flags |= TYPE_FLAG_UNSIGNED;
8260         break;
8261       case DW_ATE_UTF:
8262         /* We just treat this as an integer and then recognize the
8263            type by name elsewhere.  */
8264         break;
8265
8266       default:
8267         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8268                    dwarf_type_encoding_name (encoding));
8269         break;
8270     }
8271
8272   type = init_type (code, size, type_flags, NULL, objfile);
8273   TYPE_NAME (type) = name;
8274   TYPE_TARGET_TYPE (type) = target_type;
8275
8276   if (name && strcmp (name, "char") == 0)
8277     TYPE_NOSIGN (type) = 1;
8278
8279   return set_die_type (die, type, cu);
8280 }
8281
8282 /* Read the given DW_AT_subrange DIE.  */
8283
8284 static struct type *
8285 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8286 {
8287   struct type *base_type;
8288   struct type *range_type;
8289   struct attribute *attr;
8290   LONGEST low = 0;
8291   LONGEST high = -1;
8292   char *name;
8293   LONGEST negative_mask;
8294
8295   base_type = die_type (die, cu);
8296   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8297   check_typedef (base_type);
8298
8299   /* The die_type call above may have already set the type for this DIE.  */
8300   range_type = get_die_type (die, cu);
8301   if (range_type)
8302     return range_type;
8303
8304   if (cu->language == language_fortran)
8305     {
8306       /* FORTRAN implies a lower bound of 1, if not given.  */
8307       low = 1;
8308     }
8309
8310   /* FIXME: For variable sized arrays either of these could be
8311      a variable rather than a constant value.  We'll allow it,
8312      but we don't know how to handle it.  */
8313   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8314   if (attr)
8315     low = dwarf2_get_attr_constant_value (attr, 0);
8316
8317   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8318   if (attr)
8319     {
8320       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8321         {
8322           /* GCC encodes arrays with unspecified or dynamic length
8323              with a DW_FORM_block1 attribute or a reference attribute.
8324              FIXME: GDB does not yet know how to handle dynamic
8325              arrays properly, treat them as arrays with unspecified
8326              length for now.
8327
8328              FIXME: jimb/2003-09-22: GDB does not really know
8329              how to handle arrays of unspecified length
8330              either; we just represent them as zero-length
8331              arrays.  Choose an appropriate upper bound given
8332              the lower bound we've computed above.  */
8333           high = low - 1;
8334         }
8335       else
8336         high = dwarf2_get_attr_constant_value (attr, 1);
8337     }
8338   else
8339     {
8340       attr = dwarf2_attr (die, DW_AT_count, cu);
8341       if (attr)
8342         {
8343           int count = dwarf2_get_attr_constant_value (attr, 1);
8344           high = low + count - 1;
8345         }
8346       else
8347         {
8348           /* Unspecified array length.  */
8349           high = low - 1;
8350         }
8351     }
8352
8353   /* Dwarf-2 specifications explicitly allows to create subrange types
8354      without specifying a base type.
8355      In that case, the base type must be set to the type of
8356      the lower bound, upper bound or count, in that order, if any of these
8357      three attributes references an object that has a type.
8358      If no base type is found, the Dwarf-2 specifications say that
8359      a signed integer type of size equal to the size of an address should
8360      be used.
8361      For the following C code: `extern char gdb_int [];'
8362      GCC produces an empty range DIE.
8363      FIXME: muller/2010-05-28: Possible references to object for low bound,
8364      high bound or count are not yet handled by this code.  */
8365   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8366     {
8367       struct objfile *objfile = cu->objfile;
8368       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8369       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8370       struct type *int_type = objfile_type (objfile)->builtin_int;
8371
8372       /* Test "int", "long int", and "long long int" objfile types,
8373          and select the first one having a size above or equal to the
8374          architecture address size.  */
8375       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8376         base_type = int_type;
8377       else
8378         {
8379           int_type = objfile_type (objfile)->builtin_long;
8380           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8381             base_type = int_type;
8382           else
8383             {
8384               int_type = objfile_type (objfile)->builtin_long_long;
8385               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8386                 base_type = int_type;
8387             }
8388         }
8389     }
8390
8391   negative_mask =
8392     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8393   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8394     low |= negative_mask;
8395   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8396     high |= negative_mask;
8397
8398   range_type = create_range_type (NULL, base_type, low, high);
8399
8400   /* Mark arrays with dynamic length at least as an array of unspecified
8401      length.  GDB could check the boundary but before it gets implemented at
8402      least allow accessing the array elements.  */
8403   if (attr && attr->form == DW_FORM_block1)
8404     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8405
8406   /* Ada expects an empty array on no boundary attributes.  */
8407   if (attr == NULL && cu->language != language_ada)
8408     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8409
8410   name = dwarf2_name (die, cu);
8411   if (name)
8412     TYPE_NAME (range_type) = name;
8413
8414   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8415   if (attr)
8416     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8417
8418   set_die_type (die, range_type, cu);
8419
8420   /* set_die_type should be already done.  */
8421   set_descriptive_type (range_type, die, cu);
8422
8423   return range_type;
8424 }
8425
8426 static struct type *
8427 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8428 {
8429   struct type *type;
8430
8431   /* For now, we only support the C meaning of an unspecified type: void.  */
8432
8433   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8434   TYPE_NAME (type) = dwarf2_name (die, cu);
8435
8436   return set_die_type (die, type, cu);
8437 }
8438
8439 /* Trivial hash function for die_info: the hash value of a DIE
8440    is its offset in .debug_info for this objfile.  */
8441
8442 static hashval_t
8443 die_hash (const void *item)
8444 {
8445   const struct die_info *die = item;
8446
8447   return die->offset;
8448 }
8449
8450 /* Trivial comparison function for die_info structures: two DIEs
8451    are equal if they have the same offset.  */
8452
8453 static int
8454 die_eq (const void *item_lhs, const void *item_rhs)
8455 {
8456   const struct die_info *die_lhs = item_lhs;
8457   const struct die_info *die_rhs = item_rhs;
8458
8459   return die_lhs->offset == die_rhs->offset;
8460 }
8461
8462 /* Read a whole compilation unit into a linked list of dies.  */
8463
8464 static struct die_info *
8465 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8466 {
8467   struct die_reader_specs reader_specs;
8468   int read_abbrevs = 0;
8469   struct cleanup *back_to = NULL;
8470   struct die_info *die;
8471
8472   if (cu->dwarf2_abbrevs == NULL)
8473     {
8474       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8475       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8476       read_abbrevs = 1;
8477     }
8478
8479   gdb_assert (cu->die_hash == NULL);
8480   cu->die_hash
8481     = htab_create_alloc_ex (cu->header.length / 12,
8482                             die_hash,
8483                             die_eq,
8484                             NULL,
8485                             &cu->comp_unit_obstack,
8486                             hashtab_obstack_allocate,
8487                             dummy_obstack_deallocate);
8488
8489   init_cu_die_reader (&reader_specs, cu);
8490
8491   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8492
8493   if (read_abbrevs)
8494     do_cleanups (back_to);
8495
8496   return die;
8497 }
8498
8499 /* Main entry point for reading a DIE and all children.
8500    Read the DIE and dump it if requested.  */
8501
8502 static struct die_info *
8503 read_die_and_children (const struct die_reader_specs *reader,
8504                        gdb_byte *info_ptr,
8505                        gdb_byte **new_info_ptr,
8506                        struct die_info *parent)
8507 {
8508   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8509                                                      new_info_ptr, parent);
8510
8511   if (dwarf2_die_debug)
8512     {
8513       fprintf_unfiltered (gdb_stdlog,
8514                           "\nRead die from %s of %s:\n",
8515                           reader->buffer == dwarf2_per_objfile->info.buffer
8516                           ? ".debug_info"
8517                           : reader->buffer == dwarf2_per_objfile->types.buffer
8518                           ? ".debug_types"
8519                           : "unknown section",
8520                           reader->abfd->filename);
8521       dump_die (result, dwarf2_die_debug);
8522     }
8523
8524   return result;
8525 }
8526
8527 /* Read a single die and all its descendents.  Set the die's sibling
8528    field to NULL; set other fields in the die correctly, and set all
8529    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8530    location of the info_ptr after reading all of those dies.  PARENT
8531    is the parent of the die in question.  */
8532
8533 static struct die_info *
8534 read_die_and_children_1 (const struct die_reader_specs *reader,
8535                          gdb_byte *info_ptr,
8536                          gdb_byte **new_info_ptr,
8537                          struct die_info *parent)
8538 {
8539   struct die_info *die;
8540   gdb_byte *cur_ptr;
8541   int has_children;
8542
8543   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8544   if (die == NULL)
8545     {
8546       *new_info_ptr = cur_ptr;
8547       return NULL;
8548     }
8549   store_in_ref_table (die, reader->cu);
8550
8551   if (has_children)
8552     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8553   else
8554     {
8555       die->child = NULL;
8556       *new_info_ptr = cur_ptr;
8557     }
8558
8559   die->sibling = NULL;
8560   die->parent = parent;
8561   return die;
8562 }
8563
8564 /* Read a die, all of its descendents, and all of its siblings; set
8565    all of the fields of all of the dies correctly.  Arguments are as
8566    in read_die_and_children.  */
8567
8568 static struct die_info *
8569 read_die_and_siblings (const struct die_reader_specs *reader,
8570                        gdb_byte *info_ptr,
8571                        gdb_byte **new_info_ptr,
8572                        struct die_info *parent)
8573 {
8574   struct die_info *first_die, *last_sibling;
8575   gdb_byte *cur_ptr;
8576
8577   cur_ptr = info_ptr;
8578   first_die = last_sibling = NULL;
8579
8580   while (1)
8581     {
8582       struct die_info *die
8583         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8584
8585       if (die == NULL)
8586         {
8587           *new_info_ptr = cur_ptr;
8588           return first_die;
8589         }
8590
8591       if (!first_die)
8592         first_die = die;
8593       else
8594         last_sibling->sibling = die;
8595
8596       last_sibling = die;
8597     }
8598 }
8599
8600 /* Read the die from the .debug_info section buffer.  Set DIEP to
8601    point to a newly allocated die with its information, except for its
8602    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8603    whether the die has children or not.  */
8604
8605 static gdb_byte *
8606 read_full_die (const struct die_reader_specs *reader,
8607                struct die_info **diep, gdb_byte *info_ptr,
8608                int *has_children)
8609 {
8610   unsigned int abbrev_number, bytes_read, i, offset;
8611   struct abbrev_info *abbrev;
8612   struct die_info *die;
8613   struct dwarf2_cu *cu = reader->cu;
8614   bfd *abfd = reader->abfd;
8615
8616   offset = info_ptr - reader->buffer;
8617   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8618   info_ptr += bytes_read;
8619   if (!abbrev_number)
8620     {
8621       *diep = NULL;
8622       *has_children = 0;
8623       return info_ptr;
8624     }
8625
8626   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8627   if (!abbrev)
8628     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8629            abbrev_number,
8630            bfd_get_filename (abfd));
8631
8632   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8633   die->offset = offset;
8634   die->tag = abbrev->tag;
8635   die->abbrev = abbrev_number;
8636
8637   die->num_attrs = abbrev->num_attrs;
8638
8639   for (i = 0; i < abbrev->num_attrs; ++i)
8640     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8641                                abfd, info_ptr, cu);
8642
8643   *diep = die;
8644   *has_children = abbrev->has_children;
8645   return info_ptr;
8646 }
8647
8648 /* In DWARF version 2, the description of the debugging information is
8649    stored in a separate .debug_abbrev section.  Before we read any
8650    dies from a section we read in all abbreviations and install them
8651    in a hash table.  This function also sets flags in CU describing
8652    the data found in the abbrev table.  */
8653
8654 static void
8655 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8656 {
8657   struct comp_unit_head *cu_header = &cu->header;
8658   gdb_byte *abbrev_ptr;
8659   struct abbrev_info *cur_abbrev;
8660   unsigned int abbrev_number, bytes_read, abbrev_name;
8661   unsigned int abbrev_form, hash_number;
8662   struct attr_abbrev *cur_attrs;
8663   unsigned int allocated_attrs;
8664
8665   /* Initialize dwarf2 abbrevs.  */
8666   obstack_init (&cu->abbrev_obstack);
8667   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8668                                       (ABBREV_HASH_SIZE
8669                                        * sizeof (struct abbrev_info *)));
8670   memset (cu->dwarf2_abbrevs, 0,
8671           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8672
8673   dwarf2_read_section (dwarf2_per_objfile->objfile,
8674                        &dwarf2_per_objfile->abbrev);
8675   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8676   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8677   abbrev_ptr += bytes_read;
8678
8679   allocated_attrs = ATTR_ALLOC_CHUNK;
8680   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8681
8682   /* Loop until we reach an abbrev number of 0.  */
8683   while (abbrev_number)
8684     {
8685       cur_abbrev = dwarf_alloc_abbrev (cu);
8686
8687       /* read in abbrev header */
8688       cur_abbrev->number = abbrev_number;
8689       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8690       abbrev_ptr += bytes_read;
8691       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8692       abbrev_ptr += 1;
8693
8694       if (cur_abbrev->tag == DW_TAG_namespace)
8695         cu->has_namespace_info = 1;
8696
8697       /* now read in declarations */
8698       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8699       abbrev_ptr += bytes_read;
8700       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8701       abbrev_ptr += bytes_read;
8702       while (abbrev_name)
8703         {
8704           if (cur_abbrev->num_attrs == allocated_attrs)
8705             {
8706               allocated_attrs += ATTR_ALLOC_CHUNK;
8707               cur_attrs
8708                 = xrealloc (cur_attrs, (allocated_attrs
8709                                         * sizeof (struct attr_abbrev)));
8710             }
8711
8712           /* Record whether this compilation unit might have
8713              inter-compilation-unit references.  If we don't know what form
8714              this attribute will have, then it might potentially be a
8715              DW_FORM_ref_addr, so we conservatively expect inter-CU
8716              references.  */
8717
8718           if (abbrev_form == DW_FORM_ref_addr
8719               || abbrev_form == DW_FORM_indirect)
8720             cu->has_form_ref_addr = 1;
8721
8722           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8723           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8724           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8725           abbrev_ptr += bytes_read;
8726           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8727           abbrev_ptr += bytes_read;
8728         }
8729
8730       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8731                                          (cur_abbrev->num_attrs
8732                                           * sizeof (struct attr_abbrev)));
8733       memcpy (cur_abbrev->attrs, cur_attrs,
8734               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8735
8736       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8737       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8738       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8739
8740       /* Get next abbreviation.
8741          Under Irix6 the abbreviations for a compilation unit are not
8742          always properly terminated with an abbrev number of 0.
8743          Exit loop if we encounter an abbreviation which we have
8744          already read (which means we are about to read the abbreviations
8745          for the next compile unit) or if the end of the abbreviation
8746          table is reached.  */
8747       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8748           >= dwarf2_per_objfile->abbrev.size)
8749         break;
8750       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8751       abbrev_ptr += bytes_read;
8752       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8753         break;
8754     }
8755
8756   xfree (cur_attrs);
8757 }
8758
8759 /* Release the memory used by the abbrev table for a compilation unit.  */
8760
8761 static void
8762 dwarf2_free_abbrev_table (void *ptr_to_cu)
8763 {
8764   struct dwarf2_cu *cu = ptr_to_cu;
8765
8766   obstack_free (&cu->abbrev_obstack, NULL);
8767   cu->dwarf2_abbrevs = NULL;
8768 }
8769
8770 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8771
8772 static struct abbrev_info *
8773 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8774 {
8775   unsigned int hash_number;
8776   struct abbrev_info *abbrev;
8777
8778   hash_number = number % ABBREV_HASH_SIZE;
8779   abbrev = cu->dwarf2_abbrevs[hash_number];
8780
8781   while (abbrev)
8782     {
8783       if (abbrev->number == number)
8784         return abbrev;
8785       else
8786         abbrev = abbrev->next;
8787     }
8788   return NULL;
8789 }
8790
8791 /* Returns nonzero if TAG represents a type that we might generate a partial
8792    symbol for.  */
8793
8794 static int
8795 is_type_tag_for_partial (int tag)
8796 {
8797   switch (tag)
8798     {
8799 #if 0
8800     /* Some types that would be reasonable to generate partial symbols for,
8801        that we don't at present.  */
8802     case DW_TAG_array_type:
8803     case DW_TAG_file_type:
8804     case DW_TAG_ptr_to_member_type:
8805     case DW_TAG_set_type:
8806     case DW_TAG_string_type:
8807     case DW_TAG_subroutine_type:
8808 #endif
8809     case DW_TAG_base_type:
8810     case DW_TAG_class_type:
8811     case DW_TAG_interface_type:
8812     case DW_TAG_enumeration_type:
8813     case DW_TAG_structure_type:
8814     case DW_TAG_subrange_type:
8815     case DW_TAG_typedef:
8816     case DW_TAG_union_type:
8817       return 1;
8818     default:
8819       return 0;
8820     }
8821 }
8822
8823 /* Load all DIEs that are interesting for partial symbols into memory.  */
8824
8825 static struct partial_die_info *
8826 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8827                    int building_psymtab, struct dwarf2_cu *cu)
8828 {
8829   struct partial_die_info *part_die;
8830   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8831   struct abbrev_info *abbrev;
8832   unsigned int bytes_read;
8833   unsigned int load_all = 0;
8834
8835   int nesting_level = 1;
8836
8837   parent_die = NULL;
8838   last_die = NULL;
8839
8840   if (cu->per_cu && cu->per_cu->load_all_dies)
8841     load_all = 1;
8842
8843   cu->partial_dies
8844     = htab_create_alloc_ex (cu->header.length / 12,
8845                             partial_die_hash,
8846                             partial_die_eq,
8847                             NULL,
8848                             &cu->comp_unit_obstack,
8849                             hashtab_obstack_allocate,
8850                             dummy_obstack_deallocate);
8851
8852   part_die = obstack_alloc (&cu->comp_unit_obstack,
8853                             sizeof (struct partial_die_info));
8854
8855   while (1)
8856     {
8857       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8858
8859       /* A NULL abbrev means the end of a series of children.  */
8860       if (abbrev == NULL)
8861         {
8862           if (--nesting_level == 0)
8863             {
8864               /* PART_DIE was probably the last thing allocated on the
8865                  comp_unit_obstack, so we could call obstack_free
8866                  here.  We don't do that because the waste is small,
8867                  and will be cleaned up when we're done with this
8868                  compilation unit.  This way, we're also more robust
8869                  against other users of the comp_unit_obstack.  */
8870               return first_die;
8871             }
8872           info_ptr += bytes_read;
8873           last_die = parent_die;
8874           parent_die = parent_die->die_parent;
8875           continue;
8876         }
8877
8878       /* Check for template arguments.  We never save these; if
8879          they're seen, we just mark the parent, and go on our way.  */
8880       if (parent_die != NULL
8881           && cu->language == language_cplus
8882           && (abbrev->tag == DW_TAG_template_type_param
8883               || abbrev->tag == DW_TAG_template_value_param))
8884         {
8885           parent_die->has_template_arguments = 1;
8886
8887           if (!load_all)
8888             {
8889               /* We don't need a partial DIE for the template argument.  */
8890               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8891                                        cu);
8892               continue;
8893             }
8894         }
8895
8896       /* We only recurse into subprograms looking for template arguments.
8897          Skip their other children.  */
8898       if (!load_all
8899           && cu->language == language_cplus
8900           && parent_die != NULL
8901           && parent_die->tag == DW_TAG_subprogram)
8902         {
8903           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8904           continue;
8905         }
8906
8907       /* Check whether this DIE is interesting enough to save.  Normally
8908          we would not be interested in members here, but there may be
8909          later variables referencing them via DW_AT_specification (for
8910          static members).  */
8911       if (!load_all
8912           && !is_type_tag_for_partial (abbrev->tag)
8913           && abbrev->tag != DW_TAG_constant
8914           && abbrev->tag != DW_TAG_enumerator
8915           && abbrev->tag != DW_TAG_subprogram
8916           && abbrev->tag != DW_TAG_lexical_block
8917           && abbrev->tag != DW_TAG_variable
8918           && abbrev->tag != DW_TAG_namespace
8919           && abbrev->tag != DW_TAG_module
8920           && abbrev->tag != DW_TAG_member)
8921         {
8922           /* Otherwise we skip to the next sibling, if any.  */
8923           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8924           continue;
8925         }
8926
8927       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8928                                    buffer, info_ptr, cu);
8929
8930       /* This two-pass algorithm for processing partial symbols has a
8931          high cost in cache pressure.  Thus, handle some simple cases
8932          here which cover the majority of C partial symbols.  DIEs
8933          which neither have specification tags in them, nor could have
8934          specification tags elsewhere pointing at them, can simply be
8935          processed and discarded.
8936
8937          This segment is also optional; scan_partial_symbols and
8938          add_partial_symbol will handle these DIEs if we chain
8939          them in normally.  When compilers which do not emit large
8940          quantities of duplicate debug information are more common,
8941          this code can probably be removed.  */
8942
8943       /* Any complete simple types at the top level (pretty much all
8944          of them, for a language without namespaces), can be processed
8945          directly.  */
8946       if (parent_die == NULL
8947           && part_die->has_specification == 0
8948           && part_die->is_declaration == 0
8949           && (part_die->tag == DW_TAG_typedef
8950               || part_die->tag == DW_TAG_base_type
8951               || part_die->tag == DW_TAG_subrange_type))
8952         {
8953           if (building_psymtab && part_die->name != NULL)
8954             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8955                                  VAR_DOMAIN, LOC_TYPEDEF,
8956                                  &cu->objfile->static_psymbols,
8957                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8958           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8959           continue;
8960         }
8961
8962       /* If we're at the second level, and we're an enumerator, and
8963          our parent has no specification (meaning possibly lives in a
8964          namespace elsewhere), then we can add the partial symbol now
8965          instead of queueing it.  */
8966       if (part_die->tag == DW_TAG_enumerator
8967           && parent_die != NULL
8968           && parent_die->die_parent == NULL
8969           && parent_die->tag == DW_TAG_enumeration_type
8970           && parent_die->has_specification == 0)
8971         {
8972           if (part_die->name == NULL)
8973             complaint (&symfile_complaints,
8974                        _("malformed enumerator DIE ignored"));
8975           else if (building_psymtab)
8976             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8977                                  VAR_DOMAIN, LOC_CONST,
8978                                  (cu->language == language_cplus
8979                                   || cu->language == language_java)
8980                                  ? &cu->objfile->global_psymbols
8981                                  : &cu->objfile->static_psymbols,
8982                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8983
8984           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8985           continue;
8986         }
8987
8988       /* We'll save this DIE so link it in.  */
8989       part_die->die_parent = parent_die;
8990       part_die->die_sibling = NULL;
8991       part_die->die_child = NULL;
8992
8993       if (last_die && last_die == parent_die)
8994         last_die->die_child = part_die;
8995       else if (last_die)
8996         last_die->die_sibling = part_die;
8997
8998       last_die = part_die;
8999
9000       if (first_die == NULL)
9001         first_die = part_die;
9002
9003       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9004          find interesting need to be in the hash table, because we
9005          also have the parent/sibling/child chains; only those that we
9006          might refer to by offset later during partial symbol reading.
9007
9008          For now this means things that might have be the target of a
9009          DW_AT_specification, DW_AT_abstract_origin, or
9010          DW_AT_extension.  DW_AT_extension will refer only to
9011          namespaces; DW_AT_abstract_origin refers to functions (and
9012          many things under the function DIE, but we do not recurse
9013          into function DIEs during partial symbol reading) and
9014          possibly variables as well; DW_AT_specification refers to
9015          declarations.  Declarations ought to have the DW_AT_declaration
9016          flag.  It happens that GCC forgets to put it in sometimes, but
9017          only for functions, not for types.
9018
9019          Adding more things than necessary to the hash table is harmless
9020          except for the performance cost.  Adding too few will result in
9021          wasted time in find_partial_die, when we reread the compilation
9022          unit with load_all_dies set.  */
9023
9024       if (load_all
9025           || abbrev->tag == DW_TAG_constant
9026           || abbrev->tag == DW_TAG_subprogram
9027           || abbrev->tag == DW_TAG_variable
9028           || abbrev->tag == DW_TAG_namespace
9029           || part_die->is_declaration)
9030         {
9031           void **slot;
9032
9033           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9034                                            part_die->offset, INSERT);
9035           *slot = part_die;
9036         }
9037
9038       part_die = obstack_alloc (&cu->comp_unit_obstack,
9039                                 sizeof (struct partial_die_info));
9040
9041       /* For some DIEs we want to follow their children (if any).  For C
9042          we have no reason to follow the children of structures; for other
9043          languages we have to, so that we can get at method physnames
9044          to infer fully qualified class names, for DW_AT_specification,
9045          and for C++ template arguments.  For C++, we also look one level
9046          inside functions to find template arguments (if the name of the
9047          function does not already contain the template arguments).
9048
9049          For Ada, we need to scan the children of subprograms and lexical
9050          blocks as well because Ada allows the definition of nested
9051          entities that could be interesting for the debugger, such as
9052          nested subprograms for instance.  */
9053       if (last_die->has_children
9054           && (load_all
9055               || last_die->tag == DW_TAG_namespace
9056               || last_die->tag == DW_TAG_module
9057               || last_die->tag == DW_TAG_enumeration_type
9058               || (cu->language == language_cplus
9059                   && last_die->tag == DW_TAG_subprogram
9060                   && (last_die->name == NULL
9061                       || strchr (last_die->name, '<') == NULL))
9062               || (cu->language != language_c
9063                   && (last_die->tag == DW_TAG_class_type
9064                       || last_die->tag == DW_TAG_interface_type
9065                       || last_die->tag == DW_TAG_structure_type
9066                       || last_die->tag == DW_TAG_union_type))
9067               || (cu->language == language_ada
9068                   && (last_die->tag == DW_TAG_subprogram
9069                       || last_die->tag == DW_TAG_lexical_block))))
9070         {
9071           nesting_level++;
9072           parent_die = last_die;
9073           continue;
9074         }
9075
9076       /* Otherwise we skip to the next sibling, if any.  */
9077       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9078
9079       /* Back to the top, do it again.  */
9080     }
9081 }
9082
9083 /* Read a minimal amount of information into the minimal die structure.  */
9084
9085 static gdb_byte *
9086 read_partial_die (struct partial_die_info *part_die,
9087                   struct abbrev_info *abbrev,
9088                   unsigned int abbrev_len, bfd *abfd,
9089                   gdb_byte *buffer, gdb_byte *info_ptr,
9090                   struct dwarf2_cu *cu)
9091 {
9092   unsigned int i;
9093   struct attribute attr;
9094   int has_low_pc_attr = 0;
9095   int has_high_pc_attr = 0;
9096
9097   memset (part_die, 0, sizeof (struct partial_die_info));
9098
9099   part_die->offset = info_ptr - buffer;
9100
9101   info_ptr += abbrev_len;
9102
9103   if (abbrev == NULL)
9104     return info_ptr;
9105
9106   part_die->tag = abbrev->tag;
9107   part_die->has_children = abbrev->has_children;
9108
9109   for (i = 0; i < abbrev->num_attrs; ++i)
9110     {
9111       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9112
9113       /* Store the data if it is of an attribute we want to keep in a
9114          partial symbol table.  */
9115       switch (attr.name)
9116         {
9117         case DW_AT_name:
9118           switch (part_die->tag)
9119             {
9120             case DW_TAG_compile_unit:
9121             case DW_TAG_type_unit:
9122               /* Compilation units have a DW_AT_name that is a filename, not
9123                  a source language identifier.  */
9124             case DW_TAG_enumeration_type:
9125             case DW_TAG_enumerator:
9126               /* These tags always have simple identifiers already; no need
9127                  to canonicalize them.  */
9128               part_die->name = DW_STRING (&attr);
9129               break;
9130             default:
9131               part_die->name
9132                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9133                                             &cu->objfile->objfile_obstack);
9134               break;
9135             }
9136           break;
9137         case DW_AT_linkage_name:
9138         case DW_AT_MIPS_linkage_name:
9139           /* Note that both forms of linkage name might appear.  We
9140              assume they will be the same, and we only store the last
9141              one we see.  */
9142           if (cu->language == language_ada)
9143             part_die->name = DW_STRING (&attr);
9144           part_die->linkage_name = DW_STRING (&attr);
9145           break;
9146         case DW_AT_low_pc:
9147           has_low_pc_attr = 1;
9148           part_die->lowpc = DW_ADDR (&attr);
9149           break;
9150         case DW_AT_high_pc:
9151           has_high_pc_attr = 1;
9152           part_die->highpc = DW_ADDR (&attr);
9153           break;
9154         case DW_AT_location:
9155           /* Support the .debug_loc offsets.  */
9156           if (attr_form_is_block (&attr))
9157             {
9158                part_die->locdesc = DW_BLOCK (&attr);
9159             }
9160           else if (attr_form_is_section_offset (&attr))
9161             {
9162               dwarf2_complex_location_expr_complaint ();
9163             }
9164           else
9165             {
9166               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9167                                                      "partial symbol information");
9168             }
9169           break;
9170         case DW_AT_external:
9171           part_die->is_external = DW_UNSND (&attr);
9172           break;
9173         case DW_AT_declaration:
9174           part_die->is_declaration = DW_UNSND (&attr);
9175           break;
9176         case DW_AT_type:
9177           part_die->has_type = 1;
9178           break;
9179         case DW_AT_abstract_origin:
9180         case DW_AT_specification:
9181         case DW_AT_extension:
9182           part_die->has_specification = 1;
9183           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9184           break;
9185         case DW_AT_sibling:
9186           /* Ignore absolute siblings, they might point outside of
9187              the current compile unit.  */
9188           if (attr.form == DW_FORM_ref_addr)
9189             complaint (&symfile_complaints,
9190                        _("ignoring absolute DW_AT_sibling"));
9191           else
9192             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9193           break;
9194         case DW_AT_byte_size:
9195           part_die->has_byte_size = 1;
9196           break;
9197         case DW_AT_calling_convention:
9198           /* DWARF doesn't provide a way to identify a program's source-level
9199              entry point.  DW_AT_calling_convention attributes are only meant
9200              to describe functions' calling conventions.
9201
9202              However, because it's a necessary piece of information in
9203              Fortran, and because DW_CC_program is the only piece of debugging
9204              information whose definition refers to a 'main program' at all,
9205              several compilers have begun marking Fortran main programs with
9206              DW_CC_program --- even when those functions use the standard
9207              calling conventions.
9208
9209              So until DWARF specifies a way to provide this information and
9210              compilers pick up the new representation, we'll support this
9211              practice.  */
9212           if (DW_UNSND (&attr) == DW_CC_program
9213               && cu->language == language_fortran)
9214             {
9215               set_main_name (part_die->name);
9216
9217               /* As this DIE has a static linkage the name would be difficult
9218                  to look up later.  */
9219               language_of_main = language_fortran;
9220             }
9221           break;
9222         default:
9223           break;
9224         }
9225     }
9226
9227   if (has_low_pc_attr && has_high_pc_attr)
9228     {
9229       /* When using the GNU linker, .gnu.linkonce. sections are used to
9230          eliminate duplicate copies of functions and vtables and such.
9231          The linker will arbitrarily choose one and discard the others.
9232          The AT_*_pc values for such functions refer to local labels in
9233          these sections.  If the section from that file was discarded, the
9234          labels are not in the output, so the relocs get a value of 0.
9235          If this is a discarded function, mark the pc bounds as invalid,
9236          so that GDB will ignore it.  */
9237       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9238         {
9239           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9240
9241           complaint (&symfile_complaints,
9242                      _("DW_AT_low_pc %s is zero "
9243                        "for DIE at 0x%x [in module %s]"),
9244                      paddress (gdbarch, part_die->lowpc),
9245                      part_die->offset, cu->objfile->name);
9246         }
9247       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9248       else if (part_die->lowpc >= part_die->highpc)
9249         {
9250           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9251
9252           complaint (&symfile_complaints,
9253                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9254                        "for DIE at 0x%x [in module %s]"),
9255                      paddress (gdbarch, part_die->lowpc),
9256                      paddress (gdbarch, part_die->highpc),
9257                      part_die->offset, cu->objfile->name);
9258         }
9259       else
9260         part_die->has_pc_info = 1;
9261     }
9262
9263   return info_ptr;
9264 }
9265
9266 /* Find a cached partial DIE at OFFSET in CU.  */
9267
9268 static struct partial_die_info *
9269 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9270 {
9271   struct partial_die_info *lookup_die = NULL;
9272   struct partial_die_info part_die;
9273
9274   part_die.offset = offset;
9275   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9276
9277   return lookup_die;
9278 }
9279
9280 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9281    except in the case of .debug_types DIEs which do not reference
9282    outside their CU (they do however referencing other types via
9283    DW_FORM_ref_sig8).  */
9284
9285 static struct partial_die_info *
9286 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9287 {
9288   struct dwarf2_per_cu_data *per_cu = NULL;
9289   struct partial_die_info *pd = NULL;
9290
9291   if (cu->per_cu->from_debug_types)
9292     {
9293       pd = find_partial_die_in_comp_unit (offset, cu);
9294       if (pd != NULL)
9295         return pd;
9296       goto not_found;
9297     }
9298
9299   if (offset_in_cu_p (&cu->header, offset))
9300     {
9301       pd = find_partial_die_in_comp_unit (offset, cu);
9302       if (pd != NULL)
9303         return pd;
9304     }
9305
9306   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9307
9308   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9309     load_partial_comp_unit (per_cu, cu->objfile);
9310
9311   per_cu->cu->last_used = 0;
9312   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9313
9314   if (pd == NULL && per_cu->load_all_dies == 0)
9315     {
9316       struct cleanup *back_to;
9317       struct partial_die_info comp_unit_die;
9318       struct abbrev_info *abbrev;
9319       unsigned int bytes_read;
9320       char *info_ptr;
9321
9322       per_cu->load_all_dies = 1;
9323
9324       /* Re-read the DIEs.  */
9325       back_to = make_cleanup (null_cleanup, 0);
9326       if (per_cu->cu->dwarf2_abbrevs == NULL)
9327         {
9328           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9329           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9330         }
9331       info_ptr = (dwarf2_per_objfile->info.buffer
9332                   + per_cu->cu->header.offset
9333                   + per_cu->cu->header.first_die_offset);
9334       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9335       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9336                                    per_cu->cu->objfile->obfd,
9337                                    dwarf2_per_objfile->info.buffer, info_ptr,
9338                                    per_cu->cu);
9339       if (comp_unit_die.has_children)
9340         load_partial_dies (per_cu->cu->objfile->obfd,
9341                            dwarf2_per_objfile->info.buffer, info_ptr,
9342                            0, per_cu->cu);
9343       do_cleanups (back_to);
9344
9345       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9346     }
9347
9348  not_found:
9349
9350   if (pd == NULL)
9351     internal_error (__FILE__, __LINE__,
9352                     _("could not find partial DIE 0x%x "
9353                       "in cache [from module %s]\n"),
9354                     offset, bfd_get_filename (cu->objfile->obfd));
9355   return pd;
9356 }
9357
9358 /* See if we can figure out if the class lives in a namespace.  We do
9359    this by looking for a member function; its demangled name will
9360    contain namespace info, if there is any.  */
9361
9362 static void
9363 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9364                                   struct dwarf2_cu *cu)
9365 {
9366   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9367      what template types look like, because the demangler
9368      frequently doesn't give the same name as the debug info.  We
9369      could fix this by only using the demangled name to get the
9370      prefix (but see comment in read_structure_type).  */
9371
9372   struct partial_die_info *real_pdi;
9373   struct partial_die_info *child_pdi;
9374
9375   /* If this DIE (this DIE's specification, if any) has a parent, then
9376      we should not do this.  We'll prepend the parent's fully qualified
9377      name when we create the partial symbol.  */
9378
9379   real_pdi = struct_pdi;
9380   while (real_pdi->has_specification)
9381     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9382
9383   if (real_pdi->die_parent != NULL)
9384     return;
9385
9386   for (child_pdi = struct_pdi->die_child;
9387        child_pdi != NULL;
9388        child_pdi = child_pdi->die_sibling)
9389     {
9390       if (child_pdi->tag == DW_TAG_subprogram
9391           && child_pdi->linkage_name != NULL)
9392         {
9393           char *actual_class_name
9394             = language_class_name_from_physname (cu->language_defn,
9395                                                  child_pdi->linkage_name);
9396           if (actual_class_name != NULL)
9397             {
9398               struct_pdi->name
9399                 = obsavestring (actual_class_name,
9400                                 strlen (actual_class_name),
9401                                 &cu->objfile->objfile_obstack);
9402               xfree (actual_class_name);
9403             }
9404           break;
9405         }
9406     }
9407 }
9408
9409 /* Adjust PART_DIE before generating a symbol for it.  This function
9410    may set the is_external flag or change the DIE's name.  */
9411
9412 static void
9413 fixup_partial_die (struct partial_die_info *part_die,
9414                    struct dwarf2_cu *cu)
9415 {
9416   /* Once we've fixed up a die, there's no point in doing so again.
9417      This also avoids a memory leak if we were to call
9418      guess_partial_die_structure_name multiple times.  */
9419   if (part_die->fixup_called)
9420     return;
9421
9422   /* If we found a reference attribute and the DIE has no name, try
9423      to find a name in the referred to DIE.  */
9424
9425   if (part_die->name == NULL && part_die->has_specification)
9426     {
9427       struct partial_die_info *spec_die;
9428
9429       spec_die = find_partial_die (part_die->spec_offset, cu);
9430
9431       fixup_partial_die (spec_die, cu);
9432
9433       if (spec_die->name)
9434         {
9435           part_die->name = spec_die->name;
9436
9437           /* Copy DW_AT_external attribute if it is set.  */
9438           if (spec_die->is_external)
9439             part_die->is_external = spec_die->is_external;
9440         }
9441     }
9442
9443   /* Set default names for some unnamed DIEs.  */
9444
9445   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9446     part_die->name = "(anonymous namespace)";
9447
9448   /* If there is no parent die to provide a namespace, and there are
9449      children, see if we can determine the namespace from their linkage
9450      name.
9451      NOTE: We need to do this even if cu->has_namespace_info != 0.
9452      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9453   if (cu->language == language_cplus
9454       && dwarf2_per_objfile->types.asection != NULL
9455       && part_die->die_parent == NULL
9456       && part_die->has_children
9457       && (part_die->tag == DW_TAG_class_type
9458           || part_die->tag == DW_TAG_structure_type
9459           || part_die->tag == DW_TAG_union_type))
9460     guess_partial_die_structure_name (part_die, cu);
9461
9462   /* GCC might emit a nameless struct or union that has a linkage
9463      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
9464   if (part_die->name == NULL
9465       && (part_die->tag == DW_TAG_structure_type
9466           || part_die->tag == DW_TAG_union_type
9467           || part_die->tag == DW_TAG_class_type)
9468       && part_die->linkage_name != NULL)
9469     {
9470       char *demangled;
9471
9472       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9473       if (demangled)
9474         {
9475           part_die->name = obsavestring (demangled, strlen (demangled),
9476                                          &cu->objfile->objfile_obstack);
9477           xfree (demangled);
9478         }
9479     }
9480
9481   part_die->fixup_called = 1;
9482 }
9483
9484 /* Read an attribute value described by an attribute form.  */
9485
9486 static gdb_byte *
9487 read_attribute_value (struct attribute *attr, unsigned form,
9488                       bfd *abfd, gdb_byte *info_ptr,
9489                       struct dwarf2_cu *cu)
9490 {
9491   struct comp_unit_head *cu_header = &cu->header;
9492   unsigned int bytes_read;
9493   struct dwarf_block *blk;
9494
9495   attr->form = form;
9496   switch (form)
9497     {
9498     case DW_FORM_ref_addr:
9499       if (cu->header.version == 2)
9500         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9501       else
9502         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9503                                       &cu->header, &bytes_read);
9504       info_ptr += bytes_read;
9505       break;
9506     case DW_FORM_addr:
9507       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9508       info_ptr += bytes_read;
9509       break;
9510     case DW_FORM_block2:
9511       blk = dwarf_alloc_block (cu);
9512       blk->size = read_2_bytes (abfd, info_ptr);
9513       info_ptr += 2;
9514       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9515       info_ptr += blk->size;
9516       DW_BLOCK (attr) = blk;
9517       break;
9518     case DW_FORM_block4:
9519       blk = dwarf_alloc_block (cu);
9520       blk->size = read_4_bytes (abfd, info_ptr);
9521       info_ptr += 4;
9522       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9523       info_ptr += blk->size;
9524       DW_BLOCK (attr) = blk;
9525       break;
9526     case DW_FORM_data2:
9527       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9528       info_ptr += 2;
9529       break;
9530     case DW_FORM_data4:
9531       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9532       info_ptr += 4;
9533       break;
9534     case DW_FORM_data8:
9535       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9536       info_ptr += 8;
9537       break;
9538     case DW_FORM_sec_offset:
9539       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9540       info_ptr += bytes_read;
9541       break;
9542     case DW_FORM_string:
9543       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9544       DW_STRING_IS_CANONICAL (attr) = 0;
9545       info_ptr += bytes_read;
9546       break;
9547     case DW_FORM_strp:
9548       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9549                                                &bytes_read);
9550       DW_STRING_IS_CANONICAL (attr) = 0;
9551       info_ptr += bytes_read;
9552       break;
9553     case DW_FORM_exprloc:
9554     case DW_FORM_block:
9555       blk = dwarf_alloc_block (cu);
9556       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9557       info_ptr += bytes_read;
9558       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9559       info_ptr += blk->size;
9560       DW_BLOCK (attr) = blk;
9561       break;
9562     case DW_FORM_block1:
9563       blk = dwarf_alloc_block (cu);
9564       blk->size = read_1_byte (abfd, info_ptr);
9565       info_ptr += 1;
9566       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9567       info_ptr += blk->size;
9568       DW_BLOCK (attr) = blk;
9569       break;
9570     case DW_FORM_data1:
9571       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9572       info_ptr += 1;
9573       break;
9574     case DW_FORM_flag:
9575       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9576       info_ptr += 1;
9577       break;
9578     case DW_FORM_flag_present:
9579       DW_UNSND (attr) = 1;
9580       break;
9581     case DW_FORM_sdata:
9582       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9583       info_ptr += bytes_read;
9584       break;
9585     case DW_FORM_udata:
9586       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9587       info_ptr += bytes_read;
9588       break;
9589     case DW_FORM_ref1:
9590       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9591       info_ptr += 1;
9592       break;
9593     case DW_FORM_ref2:
9594       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9595       info_ptr += 2;
9596       break;
9597     case DW_FORM_ref4:
9598       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9599       info_ptr += 4;
9600       break;
9601     case DW_FORM_ref8:
9602       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9603       info_ptr += 8;
9604       break;
9605     case DW_FORM_ref_sig8:
9606       /* Convert the signature to something we can record in DW_UNSND
9607          for later lookup.
9608          NOTE: This is NULL if the type wasn't found.  */
9609       DW_SIGNATURED_TYPE (attr) =
9610         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9611       info_ptr += 8;
9612       break;
9613     case DW_FORM_ref_udata:
9614       DW_ADDR (attr) = (cu->header.offset
9615                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9616       info_ptr += bytes_read;
9617       break;
9618     case DW_FORM_indirect:
9619       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9620       info_ptr += bytes_read;
9621       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9622       break;
9623     default:
9624       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9625              dwarf_form_name (form),
9626              bfd_get_filename (abfd));
9627     }
9628
9629   /* We have seen instances where the compiler tried to emit a byte
9630      size attribute of -1 which ended up being encoded as an unsigned
9631      0xffffffff.  Although 0xffffffff is technically a valid size value,
9632      an object of this size seems pretty unlikely so we can relatively
9633      safely treat these cases as if the size attribute was invalid and
9634      treat them as zero by default.  */
9635   if (attr->name == DW_AT_byte_size
9636       && form == DW_FORM_data4
9637       && DW_UNSND (attr) >= 0xffffffff)
9638     {
9639       complaint
9640         (&symfile_complaints,
9641          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9642          hex_string (DW_UNSND (attr)));
9643       DW_UNSND (attr) = 0;
9644     }
9645
9646   return info_ptr;
9647 }
9648
9649 /* Read an attribute described by an abbreviated attribute.  */
9650
9651 static gdb_byte *
9652 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9653                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9654 {
9655   attr->name = abbrev->name;
9656   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9657 }
9658
9659 /* Read dwarf information from a buffer.  */
9660
9661 static unsigned int
9662 read_1_byte (bfd *abfd, gdb_byte *buf)
9663 {
9664   return bfd_get_8 (abfd, buf);
9665 }
9666
9667 static int
9668 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9669 {
9670   return bfd_get_signed_8 (abfd, buf);
9671 }
9672
9673 static unsigned int
9674 read_2_bytes (bfd *abfd, gdb_byte *buf)
9675 {
9676   return bfd_get_16 (abfd, buf);
9677 }
9678
9679 static int
9680 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9681 {
9682   return bfd_get_signed_16 (abfd, buf);
9683 }
9684
9685 static unsigned int
9686 read_4_bytes (bfd *abfd, gdb_byte *buf)
9687 {
9688   return bfd_get_32 (abfd, buf);
9689 }
9690
9691 static int
9692 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9693 {
9694   return bfd_get_signed_32 (abfd, buf);
9695 }
9696
9697 static ULONGEST
9698 read_8_bytes (bfd *abfd, gdb_byte *buf)
9699 {
9700   return bfd_get_64 (abfd, buf);
9701 }
9702
9703 static CORE_ADDR
9704 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9705               unsigned int *bytes_read)
9706 {
9707   struct comp_unit_head *cu_header = &cu->header;
9708   CORE_ADDR retval = 0;
9709
9710   if (cu_header->signed_addr_p)
9711     {
9712       switch (cu_header->addr_size)
9713         {
9714         case 2:
9715           retval = bfd_get_signed_16 (abfd, buf);
9716           break;
9717         case 4:
9718           retval = bfd_get_signed_32 (abfd, buf);
9719           break;
9720         case 8:
9721           retval = bfd_get_signed_64 (abfd, buf);
9722           break;
9723         default:
9724           internal_error (__FILE__, __LINE__,
9725                           _("read_address: bad switch, signed [in module %s]"),
9726                           bfd_get_filename (abfd));
9727         }
9728     }
9729   else
9730     {
9731       switch (cu_header->addr_size)
9732         {
9733         case 2:
9734           retval = bfd_get_16 (abfd, buf);
9735           break;
9736         case 4:
9737           retval = bfd_get_32 (abfd, buf);
9738           break;
9739         case 8:
9740           retval = bfd_get_64 (abfd, buf);
9741           break;
9742         default:
9743           internal_error (__FILE__, __LINE__,
9744                           _("read_address: bad switch, "
9745                             "unsigned [in module %s]"),
9746                           bfd_get_filename (abfd));
9747         }
9748     }
9749
9750   *bytes_read = cu_header->addr_size;
9751   return retval;
9752 }
9753
9754 /* Read the initial length from a section.  The (draft) DWARF 3
9755    specification allows the initial length to take up either 4 bytes
9756    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9757    bytes describe the length and all offsets will be 8 bytes in length
9758    instead of 4.
9759
9760    An older, non-standard 64-bit format is also handled by this
9761    function.  The older format in question stores the initial length
9762    as an 8-byte quantity without an escape value.  Lengths greater
9763    than 2^32 aren't very common which means that the initial 4 bytes
9764    is almost always zero.  Since a length value of zero doesn't make
9765    sense for the 32-bit format, this initial zero can be considered to
9766    be an escape value which indicates the presence of the older 64-bit
9767    format.  As written, the code can't detect (old format) lengths
9768    greater than 4GB.  If it becomes necessary to handle lengths
9769    somewhat larger than 4GB, we could allow other small values (such
9770    as the non-sensical values of 1, 2, and 3) to also be used as
9771    escape values indicating the presence of the old format.
9772
9773    The value returned via bytes_read should be used to increment the
9774    relevant pointer after calling read_initial_length().
9775
9776    [ Note:  read_initial_length() and read_offset() are based on the
9777      document entitled "DWARF Debugging Information Format", revision
9778      3, draft 8, dated November 19, 2001.  This document was obtained
9779      from:
9780
9781         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9782
9783      This document is only a draft and is subject to change.  (So beware.)
9784
9785      Details regarding the older, non-standard 64-bit format were
9786      determined empirically by examining 64-bit ELF files produced by
9787      the SGI toolchain on an IRIX 6.5 machine.
9788
9789      - Kevin, July 16, 2002
9790    ] */
9791
9792 static LONGEST
9793 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9794 {
9795   LONGEST length = bfd_get_32 (abfd, buf);
9796
9797   if (length == 0xffffffff)
9798     {
9799       length = bfd_get_64 (abfd, buf + 4);
9800       *bytes_read = 12;
9801     }
9802   else if (length == 0)
9803     {
9804       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9805       length = bfd_get_64 (abfd, buf);
9806       *bytes_read = 8;
9807     }
9808   else
9809     {
9810       *bytes_read = 4;
9811     }
9812
9813   return length;
9814 }
9815
9816 /* Cover function for read_initial_length.
9817    Returns the length of the object at BUF, and stores the size of the
9818    initial length in *BYTES_READ and stores the size that offsets will be in
9819    *OFFSET_SIZE.
9820    If the initial length size is not equivalent to that specified in
9821    CU_HEADER then issue a complaint.
9822    This is useful when reading non-comp-unit headers.  */
9823
9824 static LONGEST
9825 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9826                                         const struct comp_unit_head *cu_header,
9827                                         unsigned int *bytes_read,
9828                                         unsigned int *offset_size)
9829 {
9830   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9831
9832   gdb_assert (cu_header->initial_length_size == 4
9833               || cu_header->initial_length_size == 8
9834               || cu_header->initial_length_size == 12);
9835
9836   if (cu_header->initial_length_size != *bytes_read)
9837     complaint (&symfile_complaints,
9838                _("intermixed 32-bit and 64-bit DWARF sections"));
9839
9840   *offset_size = (*bytes_read == 4) ? 4 : 8;
9841   return length;
9842 }
9843
9844 /* Read an offset from the data stream.  The size of the offset is
9845    given by cu_header->offset_size.  */
9846
9847 static LONGEST
9848 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9849              unsigned int *bytes_read)
9850 {
9851   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9852
9853   *bytes_read = cu_header->offset_size;
9854   return offset;
9855 }
9856
9857 /* Read an offset from the data stream.  */
9858
9859 static LONGEST
9860 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9861 {
9862   LONGEST retval = 0;
9863
9864   switch (offset_size)
9865     {
9866     case 4:
9867       retval = bfd_get_32 (abfd, buf);
9868       break;
9869     case 8:
9870       retval = bfd_get_64 (abfd, buf);
9871       break;
9872     default:
9873       internal_error (__FILE__, __LINE__,
9874                       _("read_offset_1: bad switch [in module %s]"),
9875                       bfd_get_filename (abfd));
9876     }
9877
9878   return retval;
9879 }
9880
9881 static gdb_byte *
9882 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9883 {
9884   /* If the size of a host char is 8 bits, we can return a pointer
9885      to the buffer, otherwise we have to copy the data to a buffer
9886      allocated on the temporary obstack.  */
9887   gdb_assert (HOST_CHAR_BIT == 8);
9888   return buf;
9889 }
9890
9891 static char *
9892 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9893 {
9894   /* If the size of a host char is 8 bits, we can return a pointer
9895      to the string, otherwise we have to copy the string to a buffer
9896      allocated on the temporary obstack.  */
9897   gdb_assert (HOST_CHAR_BIT == 8);
9898   if (*buf == '\0')
9899     {
9900       *bytes_read_ptr = 1;
9901       return NULL;
9902     }
9903   *bytes_read_ptr = strlen ((char *) buf) + 1;
9904   return (char *) buf;
9905 }
9906
9907 static char *
9908 read_indirect_string (bfd *abfd, gdb_byte *buf,
9909                       const struct comp_unit_head *cu_header,
9910                       unsigned int *bytes_read_ptr)
9911 {
9912   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9913
9914   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9915   if (dwarf2_per_objfile->str.buffer == NULL)
9916     {
9917       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9918                       bfd_get_filename (abfd));
9919       return NULL;
9920     }
9921   if (str_offset >= dwarf2_per_objfile->str.size)
9922     {
9923       error (_("DW_FORM_strp pointing outside of "
9924                ".debug_str section [in module %s]"),
9925              bfd_get_filename (abfd));
9926       return NULL;
9927     }
9928   gdb_assert (HOST_CHAR_BIT == 8);
9929   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9930     return NULL;
9931   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9932 }
9933
9934 static unsigned long
9935 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9936 {
9937   unsigned long result;
9938   unsigned int num_read;
9939   int i, shift;
9940   unsigned char byte;
9941
9942   result = 0;
9943   shift = 0;
9944   num_read = 0;
9945   i = 0;
9946   while (1)
9947     {
9948       byte = bfd_get_8 (abfd, buf);
9949       buf++;
9950       num_read++;
9951       result |= ((unsigned long)(byte & 127) << shift);
9952       if ((byte & 128) == 0)
9953         {
9954           break;
9955         }
9956       shift += 7;
9957     }
9958   *bytes_read_ptr = num_read;
9959   return result;
9960 }
9961
9962 static long
9963 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9964 {
9965   long result;
9966   int i, shift, num_read;
9967   unsigned char byte;
9968
9969   result = 0;
9970   shift = 0;
9971   num_read = 0;
9972   i = 0;
9973   while (1)
9974     {
9975       byte = bfd_get_8 (abfd, buf);
9976       buf++;
9977       num_read++;
9978       result |= ((long)(byte & 127) << shift);
9979       shift += 7;
9980       if ((byte & 128) == 0)
9981         {
9982           break;
9983         }
9984     }
9985   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9986     result |= -(((long)1) << shift);
9987   *bytes_read_ptr = num_read;
9988   return result;
9989 }
9990
9991 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9992
9993 static gdb_byte *
9994 skip_leb128 (bfd *abfd, gdb_byte *buf)
9995 {
9996   int byte;
9997
9998   while (1)
9999     {
10000       byte = bfd_get_8 (abfd, buf);
10001       buf++;
10002       if ((byte & 128) == 0)
10003         return buf;
10004     }
10005 }
10006
10007 static void
10008 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10009 {
10010   switch (lang)
10011     {
10012     case DW_LANG_C89:
10013     case DW_LANG_C99:
10014     case DW_LANG_C:
10015       cu->language = language_c;
10016       break;
10017     case DW_LANG_C_plus_plus:
10018       cu->language = language_cplus;
10019       break;
10020     case DW_LANG_D:
10021       cu->language = language_d;
10022       break;
10023     case DW_LANG_Fortran77:
10024     case DW_LANG_Fortran90:
10025     case DW_LANG_Fortran95:
10026       cu->language = language_fortran;
10027       break;
10028     case DW_LANG_Mips_Assembler:
10029       cu->language = language_asm;
10030       break;
10031     case DW_LANG_Java:
10032       cu->language = language_java;
10033       break;
10034     case DW_LANG_Ada83:
10035     case DW_LANG_Ada95:
10036       cu->language = language_ada;
10037       break;
10038     case DW_LANG_Modula2:
10039       cu->language = language_m2;
10040       break;
10041     case DW_LANG_Pascal83:
10042       cu->language = language_pascal;
10043       break;
10044     case DW_LANG_ObjC:
10045       cu->language = language_objc;
10046       break;
10047     case DW_LANG_Cobol74:
10048     case DW_LANG_Cobol85:
10049     default:
10050       cu->language = language_minimal;
10051       break;
10052     }
10053   cu->language_defn = language_def (cu->language);
10054 }
10055
10056 /* Return the named attribute or NULL if not there.  */
10057
10058 static struct attribute *
10059 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10060 {
10061   unsigned int i;
10062   struct attribute *spec = NULL;
10063
10064   for (i = 0; i < die->num_attrs; ++i)
10065     {
10066       if (die->attrs[i].name == name)
10067         return &die->attrs[i];
10068       if (die->attrs[i].name == DW_AT_specification
10069           || die->attrs[i].name == DW_AT_abstract_origin)
10070         spec = &die->attrs[i];
10071     }
10072
10073   if (spec)
10074     {
10075       die = follow_die_ref (die, spec, &cu);
10076       return dwarf2_attr (die, name, cu);
10077     }
10078
10079   return NULL;
10080 }
10081
10082 /* Return the named attribute or NULL if not there,
10083    but do not follow DW_AT_specification, etc.
10084    This is for use in contexts where we're reading .debug_types dies.
10085    Following DW_AT_specification, DW_AT_abstract_origin will take us
10086    back up the chain, and we want to go down.  */
10087
10088 static struct attribute *
10089 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10090                        struct dwarf2_cu *cu)
10091 {
10092   unsigned int i;
10093
10094   for (i = 0; i < die->num_attrs; ++i)
10095     if (die->attrs[i].name == name)
10096       return &die->attrs[i];
10097
10098   return NULL;
10099 }
10100
10101 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10102    and holds a non-zero value.  This function should only be used for
10103    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10104
10105 static int
10106 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10107 {
10108   struct attribute *attr = dwarf2_attr (die, name, cu);
10109
10110   return (attr && DW_UNSND (attr));
10111 }
10112
10113 static int
10114 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10115 {
10116   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10117      which value is non-zero.  However, we have to be careful with
10118      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10119      (via dwarf2_flag_true_p) follows this attribute.  So we may
10120      end up accidently finding a declaration attribute that belongs
10121      to a different DIE referenced by the specification attribute,
10122      even though the given DIE does not have a declaration attribute.  */
10123   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10124           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10125 }
10126
10127 /* Return the die giving the specification for DIE, if there is
10128    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10129    containing the return value on output.  If there is no
10130    specification, but there is an abstract origin, that is
10131    returned.  */
10132
10133 static struct die_info *
10134 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10135 {
10136   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10137                                              *spec_cu);
10138
10139   if (spec_attr == NULL)
10140     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10141
10142   if (spec_attr == NULL)
10143     return NULL;
10144   else
10145     return follow_die_ref (die, spec_attr, spec_cu);
10146 }
10147
10148 /* Free the line_header structure *LH, and any arrays and strings it
10149    refers to.
10150    NOTE: This is also used as a "cleanup" function.  */
10151
10152 static void
10153 free_line_header (struct line_header *lh)
10154 {
10155   if (lh->standard_opcode_lengths)
10156     xfree (lh->standard_opcode_lengths);
10157
10158   /* Remember that all the lh->file_names[i].name pointers are
10159      pointers into debug_line_buffer, and don't need to be freed.  */
10160   if (lh->file_names)
10161     xfree (lh->file_names);
10162
10163   /* Similarly for the include directory names.  */
10164   if (lh->include_dirs)
10165     xfree (lh->include_dirs);
10166
10167   xfree (lh);
10168 }
10169
10170 /* Add an entry to LH's include directory table.  */
10171
10172 static void
10173 add_include_dir (struct line_header *lh, char *include_dir)
10174 {
10175   /* Grow the array if necessary.  */
10176   if (lh->include_dirs_size == 0)
10177     {
10178       lh->include_dirs_size = 1; /* for testing */
10179       lh->include_dirs = xmalloc (lh->include_dirs_size
10180                                   * sizeof (*lh->include_dirs));
10181     }
10182   else if (lh->num_include_dirs >= lh->include_dirs_size)
10183     {
10184       lh->include_dirs_size *= 2;
10185       lh->include_dirs = xrealloc (lh->include_dirs,
10186                                    (lh->include_dirs_size
10187                                     * sizeof (*lh->include_dirs)));
10188     }
10189
10190   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10191 }
10192
10193 /* Add an entry to LH's file name table.  */
10194
10195 static void
10196 add_file_name (struct line_header *lh,
10197                char *name,
10198                unsigned int dir_index,
10199                unsigned int mod_time,
10200                unsigned int length)
10201 {
10202   struct file_entry *fe;
10203
10204   /* Grow the array if necessary.  */
10205   if (lh->file_names_size == 0)
10206     {
10207       lh->file_names_size = 1; /* for testing */
10208       lh->file_names = xmalloc (lh->file_names_size
10209                                 * sizeof (*lh->file_names));
10210     }
10211   else if (lh->num_file_names >= lh->file_names_size)
10212     {
10213       lh->file_names_size *= 2;
10214       lh->file_names = xrealloc (lh->file_names,
10215                                  (lh->file_names_size
10216                                   * sizeof (*lh->file_names)));
10217     }
10218
10219   fe = &lh->file_names[lh->num_file_names++];
10220   fe->name = name;
10221   fe->dir_index = dir_index;
10222   fe->mod_time = mod_time;
10223   fe->length = length;
10224   fe->included_p = 0;
10225   fe->symtab = NULL;
10226 }
10227
10228 /* Read the statement program header starting at OFFSET in
10229    .debug_line, according to the endianness of ABFD.  Return a pointer
10230    to a struct line_header, allocated using xmalloc.
10231
10232    NOTE: the strings in the include directory and file name tables of
10233    the returned object point into debug_line_buffer, and must not be
10234    freed.  */
10235
10236 static struct line_header *
10237 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10238                           struct dwarf2_cu *cu)
10239 {
10240   struct cleanup *back_to;
10241   struct line_header *lh;
10242   gdb_byte *line_ptr;
10243   unsigned int bytes_read, offset_size;
10244   int i;
10245   char *cur_dir, *cur_file;
10246
10247   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10248   if (dwarf2_per_objfile->line.buffer == NULL)
10249     {
10250       complaint (&symfile_complaints, _("missing .debug_line section"));
10251       return 0;
10252     }
10253
10254   /* Make sure that at least there's room for the total_length field.
10255      That could be 12 bytes long, but we're just going to fudge that.  */
10256   if (offset + 4 >= dwarf2_per_objfile->line.size)
10257     {
10258       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10259       return 0;
10260     }
10261
10262   lh = xmalloc (sizeof (*lh));
10263   memset (lh, 0, sizeof (*lh));
10264   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10265                           (void *) lh);
10266
10267   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10268
10269   /* Read in the header.  */
10270   lh->total_length =
10271     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10272                                             &bytes_read, &offset_size);
10273   line_ptr += bytes_read;
10274   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10275                                      + dwarf2_per_objfile->line.size))
10276     {
10277       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10278       return 0;
10279     }
10280   lh->statement_program_end = line_ptr + lh->total_length;
10281   lh->version = read_2_bytes (abfd, line_ptr);
10282   line_ptr += 2;
10283   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10284   line_ptr += offset_size;
10285   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10286   line_ptr += 1;
10287   if (lh->version >= 4)
10288     {
10289       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10290       line_ptr += 1;
10291     }
10292   else
10293     lh->maximum_ops_per_instruction = 1;
10294
10295   if (lh->maximum_ops_per_instruction == 0)
10296     {
10297       lh->maximum_ops_per_instruction = 1;
10298       complaint (&symfile_complaints,
10299                  _("invalid maximum_ops_per_instruction "
10300                    "in `.debug_line' section"));
10301     }
10302
10303   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10304   line_ptr += 1;
10305   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10306   line_ptr += 1;
10307   lh->line_range = read_1_byte (abfd, line_ptr);
10308   line_ptr += 1;
10309   lh->opcode_base = read_1_byte (abfd, line_ptr);
10310   line_ptr += 1;
10311   lh->standard_opcode_lengths
10312     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10313
10314   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10315   for (i = 1; i < lh->opcode_base; ++i)
10316     {
10317       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10318       line_ptr += 1;
10319     }
10320
10321   /* Read directory table.  */
10322   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10323     {
10324       line_ptr += bytes_read;
10325       add_include_dir (lh, cur_dir);
10326     }
10327   line_ptr += bytes_read;
10328
10329   /* Read file name table.  */
10330   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10331     {
10332       unsigned int dir_index, mod_time, length;
10333
10334       line_ptr += bytes_read;
10335       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10336       line_ptr += bytes_read;
10337       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10338       line_ptr += bytes_read;
10339       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10340       line_ptr += bytes_read;
10341
10342       add_file_name (lh, cur_file, dir_index, mod_time, length);
10343     }
10344   line_ptr += bytes_read;
10345   lh->statement_program_start = line_ptr;
10346
10347   if (line_ptr > (dwarf2_per_objfile->line.buffer
10348                   + dwarf2_per_objfile->line.size))
10349     complaint (&symfile_complaints,
10350                _("line number info header doesn't "
10351                  "fit in `.debug_line' section"));
10352
10353   discard_cleanups (back_to);
10354   return lh;
10355 }
10356
10357 /* This function exists to work around a bug in certain compilers
10358    (particularly GCC 2.95), in which the first line number marker of a
10359    function does not show up until after the prologue, right before
10360    the second line number marker.  This function shifts ADDRESS down
10361    to the beginning of the function if necessary, and is called on
10362    addresses passed to record_line.  */
10363
10364 static CORE_ADDR
10365 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10366 {
10367   struct function_range *fn;
10368
10369   /* Find the function_range containing address.  */
10370   if (!cu->first_fn)
10371     return address;
10372
10373   if (!cu->cached_fn)
10374     cu->cached_fn = cu->first_fn;
10375
10376   fn = cu->cached_fn;
10377   while (fn)
10378     if (fn->lowpc <= address && fn->highpc > address)
10379       goto found;
10380     else
10381       fn = fn->next;
10382
10383   fn = cu->first_fn;
10384   while (fn && fn != cu->cached_fn)
10385     if (fn->lowpc <= address && fn->highpc > address)
10386       goto found;
10387     else
10388       fn = fn->next;
10389
10390   return address;
10391
10392  found:
10393   if (fn->seen_line)
10394     return address;
10395   if (address != fn->lowpc)
10396     complaint (&symfile_complaints,
10397                _("misplaced first line number at 0x%lx for '%s'"),
10398                (unsigned long) address, fn->name);
10399   fn->seen_line = 1;
10400   return fn->lowpc;
10401 }
10402
10403 /* Subroutine of dwarf_decode_lines to simplify it.
10404    Return the file name of the psymtab for included file FILE_INDEX
10405    in line header LH of PST.
10406    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10407    If space for the result is malloc'd, it will be freed by a cleanup.
10408    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10409
10410 static char *
10411 psymtab_include_file_name (const struct line_header *lh, int file_index,
10412                            const struct partial_symtab *pst,
10413                            const char *comp_dir)
10414 {
10415   const struct file_entry fe = lh->file_names [file_index];
10416   char *include_name = fe.name;
10417   char *include_name_to_compare = include_name;
10418   char *dir_name = NULL;
10419   const char *pst_filename;
10420   char *copied_name = NULL;
10421   int file_is_pst;
10422
10423   if (fe.dir_index)
10424     dir_name = lh->include_dirs[fe.dir_index - 1];
10425
10426   if (!IS_ABSOLUTE_PATH (include_name)
10427       && (dir_name != NULL || comp_dir != NULL))
10428     {
10429       /* Avoid creating a duplicate psymtab for PST.
10430          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10431          Before we do the comparison, however, we need to account
10432          for DIR_NAME and COMP_DIR.
10433          First prepend dir_name (if non-NULL).  If we still don't
10434          have an absolute path prepend comp_dir (if non-NULL).
10435          However, the directory we record in the include-file's
10436          psymtab does not contain COMP_DIR (to match the
10437          corresponding symtab(s)).
10438
10439          Example:
10440
10441          bash$ cd /tmp
10442          bash$ gcc -g ./hello.c
10443          include_name = "hello.c"
10444          dir_name = "."
10445          DW_AT_comp_dir = comp_dir = "/tmp"
10446          DW_AT_name = "./hello.c"  */
10447
10448       if (dir_name != NULL)
10449         {
10450           include_name = concat (dir_name, SLASH_STRING,
10451                                  include_name, (char *)NULL);
10452           include_name_to_compare = include_name;
10453           make_cleanup (xfree, include_name);
10454         }
10455       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10456         {
10457           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10458                                             include_name, (char *)NULL);
10459         }
10460     }
10461
10462   pst_filename = pst->filename;
10463   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10464     {
10465       copied_name = concat (pst->dirname, SLASH_STRING,
10466                             pst_filename, (char *)NULL);
10467       pst_filename = copied_name;
10468     }
10469
10470   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10471
10472   if (include_name_to_compare != include_name)
10473     xfree (include_name_to_compare);
10474   if (copied_name != NULL)
10475     xfree (copied_name);
10476
10477   if (file_is_pst)
10478     return NULL;
10479   return include_name;
10480 }
10481
10482 /* Ignore this record_line request.  */
10483
10484 static void
10485 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10486 {
10487   return;
10488 }
10489
10490 /* Decode the Line Number Program (LNP) for the given line_header
10491    structure and CU.  The actual information extracted and the type
10492    of structures created from the LNP depends on the value of PST.
10493
10494    1. If PST is NULL, then this procedure uses the data from the program
10495       to create all necessary symbol tables, and their linetables.
10496
10497    2. If PST is not NULL, this procedure reads the program to determine
10498       the list of files included by the unit represented by PST, and
10499       builds all the associated partial symbol tables.
10500
10501    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10502    It is used for relative paths in the line table.
10503    NOTE: When processing partial symtabs (pst != NULL),
10504    comp_dir == pst->dirname.
10505
10506    NOTE: It is important that psymtabs have the same file name (via strcmp)
10507    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10508    symtab we don't use it in the name of the psymtabs we create.
10509    E.g. expand_line_sal requires this when finding psymtabs to expand.
10510    A good testcase for this is mb-inline.exp.  */
10511
10512 static void
10513 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10514                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10515 {
10516   gdb_byte *line_ptr, *extended_end;
10517   gdb_byte *line_end;
10518   unsigned int bytes_read, extended_len;
10519   unsigned char op_code, extended_op, adj_opcode;
10520   CORE_ADDR baseaddr;
10521   struct objfile *objfile = cu->objfile;
10522   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10523   const int decode_for_pst_p = (pst != NULL);
10524   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10525   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10526     = record_line;
10527
10528   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10529
10530   line_ptr = lh->statement_program_start;
10531   line_end = lh->statement_program_end;
10532
10533   /* Read the statement sequences until there's nothing left.  */
10534   while (line_ptr < line_end)
10535     {
10536       /* state machine registers  */
10537       CORE_ADDR address = 0;
10538       unsigned int file = 1;
10539       unsigned int line = 1;
10540       unsigned int column = 0;
10541       int is_stmt = lh->default_is_stmt;
10542       int basic_block = 0;
10543       int end_sequence = 0;
10544       CORE_ADDR addr;
10545       unsigned char op_index = 0;
10546
10547       if (!decode_for_pst_p && lh->num_file_names >= file)
10548         {
10549           /* Start a subfile for the current file of the state machine.  */
10550           /* lh->include_dirs and lh->file_names are 0-based, but the
10551              directory and file name numbers in the statement program
10552              are 1-based.  */
10553           struct file_entry *fe = &lh->file_names[file - 1];
10554           char *dir = NULL;
10555
10556           if (fe->dir_index)
10557             dir = lh->include_dirs[fe->dir_index - 1];
10558
10559           dwarf2_start_subfile (fe->name, dir, comp_dir);
10560         }
10561
10562       /* Decode the table.  */
10563       while (!end_sequence)
10564         {
10565           op_code = read_1_byte (abfd, line_ptr);
10566           line_ptr += 1;
10567           if (line_ptr > line_end)
10568             {
10569               dwarf2_debug_line_missing_end_sequence_complaint ();
10570               break;
10571             }
10572
10573           if (op_code >= lh->opcode_base)
10574             {
10575               /* Special operand.  */
10576               adj_opcode = op_code - lh->opcode_base;
10577               address += (((op_index + (adj_opcode / lh->line_range))
10578                            / lh->maximum_ops_per_instruction)
10579                           * lh->minimum_instruction_length);
10580               op_index = ((op_index + (adj_opcode / lh->line_range))
10581                           % lh->maximum_ops_per_instruction);
10582               line += lh->line_base + (adj_opcode % lh->line_range);
10583               if (lh->num_file_names < file || file == 0)
10584                 dwarf2_debug_line_missing_file_complaint ();
10585               /* For now we ignore lines not starting on an
10586                  instruction boundary.  */
10587               else if (op_index == 0)
10588                 {
10589                   lh->file_names[file - 1].included_p = 1;
10590                   if (!decode_for_pst_p && is_stmt)
10591                     {
10592                       if (last_subfile != current_subfile)
10593                         {
10594                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10595                           if (last_subfile)
10596                             (*p_record_line) (last_subfile, 0, addr);
10597                           last_subfile = current_subfile;
10598                         }
10599                       /* Append row to matrix using current values.  */
10600                       addr = check_cu_functions (address, cu);
10601                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10602                       (*p_record_line) (current_subfile, line, addr);
10603                     }
10604                 }
10605               basic_block = 0;
10606             }
10607           else switch (op_code)
10608             {
10609             case DW_LNS_extended_op:
10610               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10611                                                    &bytes_read);
10612               line_ptr += bytes_read;
10613               extended_end = line_ptr + extended_len;
10614               extended_op = read_1_byte (abfd, line_ptr);
10615               line_ptr += 1;
10616               switch (extended_op)
10617                 {
10618                 case DW_LNE_end_sequence:
10619                   p_record_line = record_line;
10620                   end_sequence = 1;
10621                   break;
10622                 case DW_LNE_set_address:
10623                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10624
10625                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10626                     {
10627                       /* This line table is for a function which has been
10628                          GCd by the linker.  Ignore it.  PR gdb/12528 */
10629
10630                       long line_offset
10631                         = line_ptr - dwarf2_per_objfile->line.buffer;
10632
10633                       complaint (&symfile_complaints,
10634                                  _(".debug_line address at offset 0x%lx is 0 "
10635                                    "[in module %s]"),
10636                                  line_offset, cu->objfile->name);
10637                       p_record_line = noop_record_line;
10638                     }
10639
10640                   op_index = 0;
10641                   line_ptr += bytes_read;
10642                   address += baseaddr;
10643                   break;
10644                 case DW_LNE_define_file:
10645                   {
10646                     char *cur_file;
10647                     unsigned int dir_index, mod_time, length;
10648
10649                     cur_file = read_direct_string (abfd, line_ptr,
10650                                                    &bytes_read);
10651                     line_ptr += bytes_read;
10652                     dir_index =
10653                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10654                     line_ptr += bytes_read;
10655                     mod_time =
10656                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10657                     line_ptr += bytes_read;
10658                     length =
10659                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10660                     line_ptr += bytes_read;
10661                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10662                   }
10663                   break;
10664                 case DW_LNE_set_discriminator:
10665                   /* The discriminator is not interesting to the debugger;
10666                      just ignore it.  */
10667                   line_ptr = extended_end;
10668                   break;
10669                 default:
10670                   complaint (&symfile_complaints,
10671                              _("mangled .debug_line section"));
10672                   return;
10673                 }
10674               /* Make sure that we parsed the extended op correctly.  If e.g.
10675                  we expected a different address size than the producer used,
10676                  we may have read the wrong number of bytes.  */
10677               if (line_ptr != extended_end)
10678                 {
10679                   complaint (&symfile_complaints,
10680                              _("mangled .debug_line section"));
10681                   return;
10682                 }
10683               break;
10684             case DW_LNS_copy:
10685               if (lh->num_file_names < file || file == 0)
10686                 dwarf2_debug_line_missing_file_complaint ();
10687               else
10688                 {
10689                   lh->file_names[file - 1].included_p = 1;
10690                   if (!decode_for_pst_p && is_stmt)
10691                     {
10692                       if (last_subfile != current_subfile)
10693                         {
10694                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10695                           if (last_subfile)
10696                             (*p_record_line) (last_subfile, 0, addr);
10697                           last_subfile = current_subfile;
10698                         }
10699                       addr = check_cu_functions (address, cu);
10700                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10701                       (*p_record_line) (current_subfile, line, addr);
10702                     }
10703                 }
10704               basic_block = 0;
10705               break;
10706             case DW_LNS_advance_pc:
10707               {
10708                 CORE_ADDR adjust
10709                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10710
10711                 address += (((op_index + adjust)
10712                              / lh->maximum_ops_per_instruction)
10713                             * lh->minimum_instruction_length);
10714                 op_index = ((op_index + adjust)
10715                             % lh->maximum_ops_per_instruction);
10716                 line_ptr += bytes_read;
10717               }
10718               break;
10719             case DW_LNS_advance_line:
10720               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10721               line_ptr += bytes_read;
10722               break;
10723             case DW_LNS_set_file:
10724               {
10725                 /* The arrays lh->include_dirs and lh->file_names are
10726                    0-based, but the directory and file name numbers in
10727                    the statement program are 1-based.  */
10728                 struct file_entry *fe;
10729                 char *dir = NULL;
10730
10731                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10732                 line_ptr += bytes_read;
10733                 if (lh->num_file_names < file || file == 0)
10734                   dwarf2_debug_line_missing_file_complaint ();
10735                 else
10736                   {
10737                     fe = &lh->file_names[file - 1];
10738                     if (fe->dir_index)
10739                       dir = lh->include_dirs[fe->dir_index - 1];
10740                     if (!decode_for_pst_p)
10741                       {
10742                         last_subfile = current_subfile;
10743                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10744                       }
10745                   }
10746               }
10747               break;
10748             case DW_LNS_set_column:
10749               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10750               line_ptr += bytes_read;
10751               break;
10752             case DW_LNS_negate_stmt:
10753               is_stmt = (!is_stmt);
10754               break;
10755             case DW_LNS_set_basic_block:
10756               basic_block = 1;
10757               break;
10758             /* Add to the address register of the state machine the
10759                address increment value corresponding to special opcode
10760                255.  I.e., this value is scaled by the minimum
10761                instruction length since special opcode 255 would have
10762                scaled the increment.  */
10763             case DW_LNS_const_add_pc:
10764               {
10765                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10766
10767                 address += (((op_index + adjust)
10768                              / lh->maximum_ops_per_instruction)
10769                             * lh->minimum_instruction_length);
10770                 op_index = ((op_index + adjust)
10771                             % lh->maximum_ops_per_instruction);
10772               }
10773               break;
10774             case DW_LNS_fixed_advance_pc:
10775               address += read_2_bytes (abfd, line_ptr);
10776               op_index = 0;
10777               line_ptr += 2;
10778               break;
10779             default:
10780               {
10781                 /* Unknown standard opcode, ignore it.  */
10782                 int i;
10783
10784                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10785                   {
10786                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10787                     line_ptr += bytes_read;
10788                   }
10789               }
10790             }
10791         }
10792       if (lh->num_file_names < file || file == 0)
10793         dwarf2_debug_line_missing_file_complaint ();
10794       else
10795         {
10796           lh->file_names[file - 1].included_p = 1;
10797           if (!decode_for_pst_p)
10798             {
10799               addr = gdbarch_addr_bits_remove (gdbarch, address);
10800               (*p_record_line) (current_subfile, 0, addr);
10801             }
10802         }
10803     }
10804
10805   if (decode_for_pst_p)
10806     {
10807       int file_index;
10808
10809       /* Now that we're done scanning the Line Header Program, we can
10810          create the psymtab of each included file.  */
10811       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10812         if (lh->file_names[file_index].included_p == 1)
10813           {
10814             char *include_name =
10815               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10816             if (include_name != NULL)
10817               dwarf2_create_include_psymtab (include_name, pst, objfile);
10818           }
10819     }
10820   else
10821     {
10822       /* Make sure a symtab is created for every file, even files
10823          which contain only variables (i.e. no code with associated
10824          line numbers).  */
10825
10826       int i;
10827       struct file_entry *fe;
10828
10829       for (i = 0; i < lh->num_file_names; i++)
10830         {
10831           char *dir = NULL;
10832
10833           fe = &lh->file_names[i];
10834           if (fe->dir_index)
10835             dir = lh->include_dirs[fe->dir_index - 1];
10836           dwarf2_start_subfile (fe->name, dir, comp_dir);
10837
10838           /* Skip the main file; we don't need it, and it must be
10839              allocated last, so that it will show up before the
10840              non-primary symtabs in the objfile's symtab list.  */
10841           if (current_subfile == first_subfile)
10842             continue;
10843
10844           if (current_subfile->symtab == NULL)
10845             current_subfile->symtab = allocate_symtab (current_subfile->name,
10846                                                        cu->objfile);
10847           fe->symtab = current_subfile->symtab;
10848         }
10849     }
10850 }
10851
10852 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10853    DIRNAME the name of the source directory which contains FILENAME
10854    or NULL if not known.  COMP_DIR is the compilation directory for the
10855    linetable's compilation unit or NULL if not known.
10856    This routine tries to keep line numbers from identical absolute and
10857    relative file names in a common subfile.
10858
10859    Using the `list' example from the GDB testsuite, which resides in
10860    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10861    of /srcdir/list0.c yields the following debugging information for list0.c:
10862
10863    DW_AT_name:          /srcdir/list0.c
10864    DW_AT_comp_dir:              /compdir
10865    files.files[0].name: list0.h
10866    files.files[0].dir:  /srcdir
10867    files.files[1].name: list0.c
10868    files.files[1].dir:  /srcdir
10869
10870    The line number information for list0.c has to end up in a single
10871    subfile, so that `break /srcdir/list0.c:1' works as expected.
10872    start_subfile will ensure that this happens provided that we pass the
10873    concatenation of files.files[1].dir and files.files[1].name as the
10874    subfile's name.  */
10875
10876 static void
10877 dwarf2_start_subfile (char *filename, const char *dirname,
10878                       const char *comp_dir)
10879 {
10880   char *fullname;
10881
10882   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10883      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10884      second argument to start_subfile.  To be consistent, we do the
10885      same here.  In order not to lose the line information directory,
10886      we concatenate it to the filename when it makes sense.
10887      Note that the Dwarf3 standard says (speaking of filenames in line
10888      information): ``The directory index is ignored for file names
10889      that represent full path names''.  Thus ignoring dirname in the
10890      `else' branch below isn't an issue.  */
10891
10892   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10893     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10894   else
10895     fullname = filename;
10896
10897   start_subfile (fullname, comp_dir);
10898
10899   if (fullname != filename)
10900     xfree (fullname);
10901 }
10902
10903 static void
10904 var_decode_location (struct attribute *attr, struct symbol *sym,
10905                      struct dwarf2_cu *cu)
10906 {
10907   struct objfile *objfile = cu->objfile;
10908   struct comp_unit_head *cu_header = &cu->header;
10909
10910   /* NOTE drow/2003-01-30: There used to be a comment and some special
10911      code here to turn a symbol with DW_AT_external and a
10912      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10913      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10914      with some versions of binutils) where shared libraries could have
10915      relocations against symbols in their debug information - the
10916      minimal symbol would have the right address, but the debug info
10917      would not.  It's no longer necessary, because we will explicitly
10918      apply relocations when we read in the debug information now.  */
10919
10920   /* A DW_AT_location attribute with no contents indicates that a
10921      variable has been optimized away.  */
10922   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10923     {
10924       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10925       return;
10926     }
10927
10928   /* Handle one degenerate form of location expression specially, to
10929      preserve GDB's previous behavior when section offsets are
10930      specified.  If this is just a DW_OP_addr then mark this symbol
10931      as LOC_STATIC.  */
10932
10933   if (attr_form_is_block (attr)
10934       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10935       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10936     {
10937       unsigned int dummy;
10938
10939       SYMBOL_VALUE_ADDRESS (sym) =
10940         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10941       SYMBOL_CLASS (sym) = LOC_STATIC;
10942       fixup_symbol_section (sym, objfile);
10943       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10944                                               SYMBOL_SECTION (sym));
10945       return;
10946     }
10947
10948   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10949      expression evaluator, and use LOC_COMPUTED only when necessary
10950      (i.e. when the value of a register or memory location is
10951      referenced, or a thread-local block, etc.).  Then again, it might
10952      not be worthwhile.  I'm assuming that it isn't unless performance
10953      or memory numbers show me otherwise.  */
10954
10955   dwarf2_symbol_mark_computed (attr, sym, cu);
10956   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10957 }
10958
10959 /* Given a pointer to a DWARF information entry, figure out if we need
10960    to make a symbol table entry for it, and if so, create a new entry
10961    and return a pointer to it.
10962    If TYPE is NULL, determine symbol type from the die, otherwise
10963    used the passed type.
10964    If SPACE is not NULL, use it to hold the new symbol.  If it is
10965    NULL, allocate a new symbol on the objfile's obstack.  */
10966
10967 static struct symbol *
10968 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10969                  struct symbol *space)
10970 {
10971   struct objfile *objfile = cu->objfile;
10972   struct symbol *sym = NULL;
10973   char *name;
10974   struct attribute *attr = NULL;
10975   struct attribute *attr2 = NULL;
10976   CORE_ADDR baseaddr;
10977   struct pending **list_to_add = NULL;
10978
10979   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10980
10981   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10982
10983   name = dwarf2_name (die, cu);
10984   if (name)
10985     {
10986       const char *linkagename;
10987       int suppress_add = 0;
10988
10989       if (space)
10990         sym = space;
10991       else
10992         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10993       OBJSTAT (objfile, n_syms++);
10994
10995       /* Cache this symbol's name and the name's demangled form (if any).  */
10996       SYMBOL_SET_LANGUAGE (sym, cu->language);
10997       linkagename = dwarf2_physname (name, die, cu);
10998       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10999
11000       /* Fortran does not have mangling standard and the mangling does differ
11001          between gfortran, iFort etc.  */
11002       if (cu->language == language_fortran
11003           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11004         symbol_set_demangled_name (&(sym->ginfo),
11005                                    (char *) dwarf2_full_name (name, die, cu),
11006                                    NULL);
11007
11008       /* Default assumptions.
11009          Use the passed type or decode it from the die.  */
11010       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11011       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11012       if (type != NULL)
11013         SYMBOL_TYPE (sym) = type;
11014       else
11015         SYMBOL_TYPE (sym) = die_type (die, cu);
11016       attr = dwarf2_attr (die,
11017                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11018                           cu);
11019       if (attr)
11020         {
11021           SYMBOL_LINE (sym) = DW_UNSND (attr);
11022         }
11023
11024       attr = dwarf2_attr (die,
11025                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11026                           cu);
11027       if (attr)
11028         {
11029           int file_index = DW_UNSND (attr);
11030
11031           if (cu->line_header == NULL
11032               || file_index > cu->line_header->num_file_names)
11033             complaint (&symfile_complaints,
11034                        _("file index out of range"));
11035           else if (file_index > 0)
11036             {
11037               struct file_entry *fe;
11038
11039               fe = &cu->line_header->file_names[file_index - 1];
11040               SYMBOL_SYMTAB (sym) = fe->symtab;
11041             }
11042         }
11043
11044       switch (die->tag)
11045         {
11046         case DW_TAG_label:
11047           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11048           if (attr)
11049             {
11050               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11051             }
11052           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11053           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11054           SYMBOL_CLASS (sym) = LOC_LABEL;
11055           add_symbol_to_list (sym, cu->list_in_scope);
11056           break;
11057         case DW_TAG_subprogram:
11058           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11059              finish_block.  */
11060           SYMBOL_CLASS (sym) = LOC_BLOCK;
11061           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11062           if ((attr2 && (DW_UNSND (attr2) != 0))
11063               || cu->language == language_ada)
11064             {
11065               /* Subprograms marked external are stored as a global symbol.
11066                  Ada subprograms, whether marked external or not, are always
11067                  stored as a global symbol, because we want to be able to
11068                  access them globally.  For instance, we want to be able
11069                  to break on a nested subprogram without having to
11070                  specify the context.  */
11071               list_to_add = &global_symbols;
11072             }
11073           else
11074             {
11075               list_to_add = cu->list_in_scope;
11076             }
11077           break;
11078         case DW_TAG_inlined_subroutine:
11079           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11080              finish_block.  */
11081           SYMBOL_CLASS (sym) = LOC_BLOCK;
11082           SYMBOL_INLINED (sym) = 1;
11083           /* Do not add the symbol to any lists.  It will be found via
11084              BLOCK_FUNCTION from the blockvector.  */
11085           break;
11086         case DW_TAG_template_value_param:
11087           suppress_add = 1;
11088           /* Fall through.  */
11089         case DW_TAG_constant:
11090         case DW_TAG_variable:
11091         case DW_TAG_member:
11092           /* Compilation with minimal debug info may result in
11093              variables with missing type entries.  Change the
11094              misleading `void' type to something sensible.  */
11095           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11096             SYMBOL_TYPE (sym)
11097               = objfile_type (objfile)->nodebug_data_symbol;
11098
11099           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11100           /* In the case of DW_TAG_member, we should only be called for
11101              static const members.  */
11102           if (die->tag == DW_TAG_member)
11103             {
11104               /* dwarf2_add_field uses die_is_declaration,
11105                  so we do the same.  */
11106               gdb_assert (die_is_declaration (die, cu));
11107               gdb_assert (attr);
11108             }
11109           if (attr)
11110             {
11111               dwarf2_const_value (attr, sym, cu);
11112               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11113               if (!suppress_add)
11114                 {
11115                   if (attr2 && (DW_UNSND (attr2) != 0))
11116                     list_to_add = &global_symbols;
11117                   else
11118                     list_to_add = cu->list_in_scope;
11119                 }
11120               break;
11121             }
11122           attr = dwarf2_attr (die, DW_AT_location, cu);
11123           if (attr)
11124             {
11125               var_decode_location (attr, sym, cu);
11126               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11127               if (SYMBOL_CLASS (sym) == LOC_STATIC
11128                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11129                   && !dwarf2_per_objfile->has_section_at_zero)
11130                 {
11131                   /* When a static variable is eliminated by the linker,
11132                      the corresponding debug information is not stripped
11133                      out, but the variable address is set to null;
11134                      do not add such variables into symbol table.  */
11135                 }
11136               else if (attr2 && (DW_UNSND (attr2) != 0))
11137                 {
11138                   /* Workaround gfortran PR debug/40040 - it uses
11139                      DW_AT_location for variables in -fPIC libraries which may
11140                      get overriden by other libraries/executable and get
11141                      a different address.  Resolve it by the minimal symbol
11142                      which may come from inferior's executable using copy
11143                      relocation.  Make this workaround only for gfortran as for
11144                      other compilers GDB cannot guess the minimal symbol
11145                      Fortran mangling kind.  */
11146                   if (cu->language == language_fortran && die->parent
11147                       && die->parent->tag == DW_TAG_module
11148                       && cu->producer
11149                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11150                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11151
11152                   /* A variable with DW_AT_external is never static,
11153                      but it may be block-scoped.  */
11154                   list_to_add = (cu->list_in_scope == &file_symbols
11155                                  ? &global_symbols : cu->list_in_scope);
11156                 }
11157               else
11158                 list_to_add = cu->list_in_scope;
11159             }
11160           else
11161             {
11162               /* We do not know the address of this symbol.
11163                  If it is an external symbol and we have type information
11164                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11165                  The address of the variable will then be determined from
11166                  the minimal symbol table whenever the variable is
11167                  referenced.  */
11168               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11169               if (attr2 && (DW_UNSND (attr2) != 0)
11170                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11171                 {
11172                   /* A variable with DW_AT_external is never static, but it
11173                      may be block-scoped.  */
11174                   list_to_add = (cu->list_in_scope == &file_symbols
11175                                  ? &global_symbols : cu->list_in_scope);
11176
11177                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11178                 }
11179               else if (!die_is_declaration (die, cu))
11180                 {
11181                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11182                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11183                   if (!suppress_add)
11184                     list_to_add = cu->list_in_scope;
11185                 }
11186             }
11187           break;
11188         case DW_TAG_formal_parameter:
11189           /* If we are inside a function, mark this as an argument.  If
11190              not, we might be looking at an argument to an inlined function
11191              when we do not have enough information to show inlined frames;
11192              pretend it's a local variable in that case so that the user can
11193              still see it.  */
11194           if (context_stack_depth > 0
11195               && context_stack[context_stack_depth - 1].name != NULL)
11196             SYMBOL_IS_ARGUMENT (sym) = 1;
11197           attr = dwarf2_attr (die, DW_AT_location, cu);
11198           if (attr)
11199             {
11200               var_decode_location (attr, sym, cu);
11201             }
11202           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11203           if (attr)
11204             {
11205               dwarf2_const_value (attr, sym, cu);
11206             }
11207           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11208           if (attr && DW_UNSND (attr))
11209             {
11210               struct type *ref_type;
11211
11212               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11213               SYMBOL_TYPE (sym) = ref_type;
11214             }
11215
11216           list_to_add = cu->list_in_scope;
11217           break;
11218         case DW_TAG_unspecified_parameters:
11219           /* From varargs functions; gdb doesn't seem to have any
11220              interest in this information, so just ignore it for now.
11221              (FIXME?) */
11222           break;
11223         case DW_TAG_template_type_param:
11224           suppress_add = 1;
11225           /* Fall through.  */
11226         case DW_TAG_class_type:
11227         case DW_TAG_interface_type:
11228         case DW_TAG_structure_type:
11229         case DW_TAG_union_type:
11230         case DW_TAG_set_type:
11231         case DW_TAG_enumeration_type:
11232           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11233           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11234
11235           {
11236             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11237                really ever be static objects: otherwise, if you try
11238                to, say, break of a class's method and you're in a file
11239                which doesn't mention that class, it won't work unless
11240                the check for all static symbols in lookup_symbol_aux
11241                saves you.  See the OtherFileClass tests in
11242                gdb.c++/namespace.exp.  */
11243
11244             if (!suppress_add)
11245               {
11246                 list_to_add = (cu->list_in_scope == &file_symbols
11247                                && (cu->language == language_cplus
11248                                    || cu->language == language_java)
11249                                ? &global_symbols : cu->list_in_scope);
11250
11251                 /* The semantics of C++ state that "struct foo {
11252                    ... }" also defines a typedef for "foo".  A Java
11253                    class declaration also defines a typedef for the
11254                    class.  */
11255                 if (cu->language == language_cplus
11256                     || cu->language == language_java
11257                     || cu->language == language_ada)
11258                   {
11259                     /* The symbol's name is already allocated along
11260                        with this objfile, so we don't need to
11261                        duplicate it for the type.  */
11262                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11263                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11264                   }
11265               }
11266           }
11267           break;
11268         case DW_TAG_typedef:
11269           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11270           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11271           list_to_add = cu->list_in_scope;
11272           break;
11273         case DW_TAG_base_type:
11274         case DW_TAG_subrange_type:
11275           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11276           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11277           list_to_add = cu->list_in_scope;
11278           break;
11279         case DW_TAG_enumerator:
11280           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11281           if (attr)
11282             {
11283               dwarf2_const_value (attr, sym, cu);
11284             }
11285           {
11286             /* NOTE: carlton/2003-11-10: See comment above in the
11287                DW_TAG_class_type, etc. block.  */
11288
11289             list_to_add = (cu->list_in_scope == &file_symbols
11290                            && (cu->language == language_cplus
11291                                || cu->language == language_java)
11292                            ? &global_symbols : cu->list_in_scope);
11293           }
11294           break;
11295         case DW_TAG_namespace:
11296           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11297           list_to_add = &global_symbols;
11298           break;
11299         default:
11300           /* Not a tag we recognize.  Hopefully we aren't processing
11301              trash data, but since we must specifically ignore things
11302              we don't recognize, there is nothing else we should do at
11303              this point.  */
11304           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11305                      dwarf_tag_name (die->tag));
11306           break;
11307         }
11308
11309       if (suppress_add)
11310         {
11311           sym->hash_next = objfile->template_symbols;
11312           objfile->template_symbols = sym;
11313           list_to_add = NULL;
11314         }
11315
11316       if (list_to_add != NULL)
11317         add_symbol_to_list (sym, list_to_add);
11318
11319       /* For the benefit of old versions of GCC, check for anonymous
11320          namespaces based on the demangled name.  */
11321       if (!processing_has_namespace_info
11322           && cu->language == language_cplus)
11323         cp_scan_for_anonymous_namespaces (sym);
11324     }
11325   return (sym);
11326 }
11327
11328 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11329
11330 static struct symbol *
11331 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11332 {
11333   return new_symbol_full (die, type, cu, NULL);
11334 }
11335
11336 /* Given an attr with a DW_FORM_dataN value in host byte order,
11337    zero-extend it as appropriate for the symbol's type.  The DWARF
11338    standard (v4) is not entirely clear about the meaning of using
11339    DW_FORM_dataN for a constant with a signed type, where the type is
11340    wider than the data.  The conclusion of a discussion on the DWARF
11341    list was that this is unspecified.  We choose to always zero-extend
11342    because that is the interpretation long in use by GCC.  */
11343
11344 static gdb_byte *
11345 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11346                          const char *name, struct obstack *obstack,
11347                          struct dwarf2_cu *cu, long *value, int bits)
11348 {
11349   struct objfile *objfile = cu->objfile;
11350   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11351                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11352   LONGEST l = DW_UNSND (attr);
11353
11354   if (bits < sizeof (*value) * 8)
11355     {
11356       l &= ((LONGEST) 1 << bits) - 1;
11357       *value = l;
11358     }
11359   else if (bits == sizeof (*value) * 8)
11360     *value = l;
11361   else
11362     {
11363       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11364       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11365       return bytes;
11366     }
11367
11368   return NULL;
11369 }
11370
11371 /* Read a constant value from an attribute.  Either set *VALUE, or if
11372    the value does not fit in *VALUE, set *BYTES - either already
11373    allocated on the objfile obstack, or newly allocated on OBSTACK,
11374    or, set *BATON, if we translated the constant to a location
11375    expression.  */
11376
11377 static void
11378 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11379                          const char *name, struct obstack *obstack,
11380                          struct dwarf2_cu *cu,
11381                          long *value, gdb_byte **bytes,
11382                          struct dwarf2_locexpr_baton **baton)
11383 {
11384   struct objfile *objfile = cu->objfile;
11385   struct comp_unit_head *cu_header = &cu->header;
11386   struct dwarf_block *blk;
11387   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11388                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11389
11390   *value = 0;
11391   *bytes = NULL;
11392   *baton = NULL;
11393
11394   switch (attr->form)
11395     {
11396     case DW_FORM_addr:
11397       {
11398         gdb_byte *data;
11399
11400         if (TYPE_LENGTH (type) != cu_header->addr_size)
11401           dwarf2_const_value_length_mismatch_complaint (name,
11402                                                         cu_header->addr_size,
11403                                                         TYPE_LENGTH (type));
11404         /* Symbols of this form are reasonably rare, so we just
11405            piggyback on the existing location code rather than writing
11406            a new implementation of symbol_computed_ops.  */
11407         *baton = obstack_alloc (&objfile->objfile_obstack,
11408                                 sizeof (struct dwarf2_locexpr_baton));
11409         (*baton)->per_cu = cu->per_cu;
11410         gdb_assert ((*baton)->per_cu);
11411
11412         (*baton)->size = 2 + cu_header->addr_size;
11413         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11414         (*baton)->data = data;
11415
11416         data[0] = DW_OP_addr;
11417         store_unsigned_integer (&data[1], cu_header->addr_size,
11418                                 byte_order, DW_ADDR (attr));
11419         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11420       }
11421       break;
11422     case DW_FORM_string:
11423     case DW_FORM_strp:
11424       /* DW_STRING is already allocated on the objfile obstack, point
11425          directly to it.  */
11426       *bytes = (gdb_byte *) DW_STRING (attr);
11427       break;
11428     case DW_FORM_block1:
11429     case DW_FORM_block2:
11430     case DW_FORM_block4:
11431     case DW_FORM_block:
11432     case DW_FORM_exprloc:
11433       blk = DW_BLOCK (attr);
11434       if (TYPE_LENGTH (type) != blk->size)
11435         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11436                                                       TYPE_LENGTH (type));
11437       *bytes = blk->data;
11438       break;
11439
11440       /* The DW_AT_const_value attributes are supposed to carry the
11441          symbol's value "represented as it would be on the target
11442          architecture."  By the time we get here, it's already been
11443          converted to host endianness, so we just need to sign- or
11444          zero-extend it as appropriate.  */
11445     case DW_FORM_data1:
11446       *bytes = dwarf2_const_value_data (attr, type, name,
11447                                         obstack, cu, value, 8);
11448       break;
11449     case DW_FORM_data2:
11450       *bytes = dwarf2_const_value_data (attr, type, name,
11451                                         obstack, cu, value, 16);
11452       break;
11453     case DW_FORM_data4:
11454       *bytes = dwarf2_const_value_data (attr, type, name,
11455                                         obstack, cu, value, 32);
11456       break;
11457     case DW_FORM_data8:
11458       *bytes = dwarf2_const_value_data (attr, type, name,
11459                                         obstack, cu, value, 64);
11460       break;
11461
11462     case DW_FORM_sdata:
11463       *value = DW_SND (attr);
11464       break;
11465
11466     case DW_FORM_udata:
11467       *value = DW_UNSND (attr);
11468       break;
11469
11470     default:
11471       complaint (&symfile_complaints,
11472                  _("unsupported const value attribute form: '%s'"),
11473                  dwarf_form_name (attr->form));
11474       *value = 0;
11475       break;
11476     }
11477 }
11478
11479
11480 /* Copy constant value from an attribute to a symbol.  */
11481
11482 static void
11483 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11484                     struct dwarf2_cu *cu)
11485 {
11486   struct objfile *objfile = cu->objfile;
11487   struct comp_unit_head *cu_header = &cu->header;
11488   long value;
11489   gdb_byte *bytes;
11490   struct dwarf2_locexpr_baton *baton;
11491
11492   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11493                            SYMBOL_PRINT_NAME (sym),
11494                            &objfile->objfile_obstack, cu,
11495                            &value, &bytes, &baton);
11496
11497   if (baton != NULL)
11498     {
11499       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11500       SYMBOL_LOCATION_BATON (sym) = baton;
11501       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11502     }
11503   else if (bytes != NULL)
11504      {
11505       SYMBOL_VALUE_BYTES (sym) = bytes;
11506       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11507     }
11508   else
11509     {
11510       SYMBOL_VALUE (sym) = value;
11511       SYMBOL_CLASS (sym) = LOC_CONST;
11512     }
11513 }
11514
11515 /* Return the type of the die in question using its DW_AT_type attribute.  */
11516
11517 static struct type *
11518 die_type (struct die_info *die, struct dwarf2_cu *cu)
11519 {
11520   struct attribute *type_attr;
11521
11522   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11523   if (!type_attr)
11524     {
11525       /* A missing DW_AT_type represents a void type.  */
11526       return objfile_type (cu->objfile)->builtin_void;
11527     }
11528
11529   return lookup_die_type (die, type_attr, cu);
11530 }
11531
11532 /* True iff CU's producer generates GNAT Ada auxiliary information
11533    that allows to find parallel types through that information instead
11534    of having to do expensive parallel lookups by type name.  */
11535
11536 static int
11537 need_gnat_info (struct dwarf2_cu *cu)
11538 {
11539   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11540      of GNAT produces this auxiliary information, without any indication
11541      that it is produced.  Part of enhancing the FSF version of GNAT
11542      to produce that information will be to put in place an indicator
11543      that we can use in order to determine whether the descriptive type
11544      info is available or not.  One suggestion that has been made is
11545      to use a new attribute, attached to the CU die.  For now, assume
11546      that the descriptive type info is not available.  */
11547   return 0;
11548 }
11549
11550 /* Return the auxiliary type of the die in question using its
11551    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11552    attribute is not present.  */
11553
11554 static struct type *
11555 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11556 {
11557   struct attribute *type_attr;
11558
11559   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11560   if (!type_attr)
11561     return NULL;
11562
11563   return lookup_die_type (die, type_attr, cu);
11564 }
11565
11566 /* If DIE has a descriptive_type attribute, then set the TYPE's
11567    descriptive type accordingly.  */
11568
11569 static void
11570 set_descriptive_type (struct type *type, struct die_info *die,
11571                       struct dwarf2_cu *cu)
11572 {
11573   struct type *descriptive_type = die_descriptive_type (die, cu);
11574
11575   if (descriptive_type)
11576     {
11577       ALLOCATE_GNAT_AUX_TYPE (type);
11578       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11579     }
11580 }
11581
11582 /* Return the containing type of the die in question using its
11583    DW_AT_containing_type attribute.  */
11584
11585 static struct type *
11586 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11587 {
11588   struct attribute *type_attr;
11589
11590   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11591   if (!type_attr)
11592     error (_("Dwarf Error: Problem turning containing type into gdb type "
11593              "[in module %s]"), cu->objfile->name);
11594
11595   return lookup_die_type (die, type_attr, cu);
11596 }
11597
11598 /* Look up the type of DIE in CU using its type attribute ATTR.
11599    If there is no type substitute an error marker.  */
11600
11601 static struct type *
11602 lookup_die_type (struct die_info *die, struct attribute *attr,
11603                  struct dwarf2_cu *cu)
11604 {
11605   struct type *this_type;
11606
11607   /* First see if we have it cached.  */
11608
11609   if (is_ref_attr (attr))
11610     {
11611       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11612
11613       this_type = get_die_type_at_offset (offset, cu->per_cu);
11614     }
11615   else if (attr->form == DW_FORM_ref_sig8)
11616     {
11617       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11618       struct dwarf2_cu *sig_cu;
11619       unsigned int offset;
11620
11621       /* sig_type will be NULL if the signatured type is missing from
11622          the debug info.  */
11623       if (sig_type == NULL)
11624         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11625                  "at 0x%x [in module %s]"),
11626                die->offset, cu->objfile->name);
11627
11628       gdb_assert (sig_type->per_cu.from_debug_types);
11629       offset = sig_type->offset + sig_type->type_offset;
11630       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11631     }
11632   else
11633     {
11634       dump_die_for_error (die);
11635       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11636              dwarf_attr_name (attr->name), cu->objfile->name);
11637     }
11638
11639   /* If not cached we need to read it in.  */
11640
11641   if (this_type == NULL)
11642     {
11643       struct die_info *type_die;
11644       struct dwarf2_cu *type_cu = cu;
11645
11646       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11647       /* If the type is cached, we should have found it above.  */
11648       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11649       this_type = read_type_die_1 (type_die, type_cu);
11650     }
11651
11652   /* If we still don't have a type use an error marker.  */
11653
11654   if (this_type == NULL)
11655     {
11656       char *message, *saved;
11657
11658       /* read_type_die already issued a complaint.  */
11659       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11660                             cu->objfile->name,
11661                             cu->header.offset,
11662                             die->offset);
11663       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11664                              message, strlen (message));
11665       xfree (message);
11666
11667       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11668     }
11669
11670   return this_type;
11671 }
11672
11673 /* Return the type in DIE, CU.
11674    Returns NULL for invalid types.
11675
11676    This first does a lookup in the appropriate type_hash table,
11677    and only reads the die in if necessary.
11678
11679    NOTE: This can be called when reading in partial or full symbols.  */
11680
11681 static struct type *
11682 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11683 {
11684   struct type *this_type;
11685
11686   this_type = get_die_type (die, cu);
11687   if (this_type)
11688     return this_type;
11689
11690   return read_type_die_1 (die, cu);
11691 }
11692
11693 /* Read the type in DIE, CU.
11694    Returns NULL for invalid types.  */
11695
11696 static struct type *
11697 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11698 {
11699   struct type *this_type = NULL;
11700
11701   switch (die->tag)
11702     {
11703     case DW_TAG_class_type:
11704     case DW_TAG_interface_type:
11705     case DW_TAG_structure_type:
11706     case DW_TAG_union_type:
11707       this_type = read_structure_type (die, cu);
11708       break;
11709     case DW_TAG_enumeration_type:
11710       this_type = read_enumeration_type (die, cu);
11711       break;
11712     case DW_TAG_subprogram:
11713     case DW_TAG_subroutine_type:
11714     case DW_TAG_inlined_subroutine:
11715       this_type = read_subroutine_type (die, cu);
11716       break;
11717     case DW_TAG_array_type:
11718       this_type = read_array_type (die, cu);
11719       break;
11720     case DW_TAG_set_type:
11721       this_type = read_set_type (die, cu);
11722       break;
11723     case DW_TAG_pointer_type:
11724       this_type = read_tag_pointer_type (die, cu);
11725       break;
11726     case DW_TAG_ptr_to_member_type:
11727       this_type = read_tag_ptr_to_member_type (die, cu);
11728       break;
11729     case DW_TAG_reference_type:
11730       this_type = read_tag_reference_type (die, cu);
11731       break;
11732     case DW_TAG_const_type:
11733       this_type = read_tag_const_type (die, cu);
11734       break;
11735     case DW_TAG_volatile_type:
11736       this_type = read_tag_volatile_type (die, cu);
11737       break;
11738     case DW_TAG_string_type:
11739       this_type = read_tag_string_type (die, cu);
11740       break;
11741     case DW_TAG_typedef:
11742       this_type = read_typedef (die, cu);
11743       break;
11744     case DW_TAG_subrange_type:
11745       this_type = read_subrange_type (die, cu);
11746       break;
11747     case DW_TAG_base_type:
11748       this_type = read_base_type (die, cu);
11749       break;
11750     case DW_TAG_unspecified_type:
11751       this_type = read_unspecified_type (die, cu);
11752       break;
11753     case DW_TAG_namespace:
11754       this_type = read_namespace_type (die, cu);
11755       break;
11756     case DW_TAG_module:
11757       this_type = read_module_type (die, cu);
11758       break;
11759     default:
11760       complaint (&symfile_complaints,
11761                  _("unexpected tag in read_type_die: '%s'"),
11762                  dwarf_tag_name (die->tag));
11763       break;
11764     }
11765
11766   return this_type;
11767 }
11768
11769 /* See if we can figure out if the class lives in a namespace.  We do
11770    this by looking for a member function; its demangled name will
11771    contain namespace info, if there is any.
11772    Return the computed name or NULL.
11773    Space for the result is allocated on the objfile's obstack.
11774    This is the full-die version of guess_partial_die_structure_name.
11775    In this case we know DIE has no useful parent.  */
11776
11777 static char *
11778 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11779 {
11780   struct die_info *spec_die;
11781   struct dwarf2_cu *spec_cu;
11782   struct die_info *child;
11783
11784   spec_cu = cu;
11785   spec_die = die_specification (die, &spec_cu);
11786   if (spec_die != NULL)
11787     {
11788       die = spec_die;
11789       cu = spec_cu;
11790     }
11791
11792   for (child = die->child;
11793        child != NULL;
11794        child = child->sibling)
11795     {
11796       if (child->tag == DW_TAG_subprogram)
11797         {
11798           struct attribute *attr;
11799
11800           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11801           if (attr == NULL)
11802             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11803           if (attr != NULL)
11804             {
11805               char *actual_name
11806                 = language_class_name_from_physname (cu->language_defn,
11807                                                      DW_STRING (attr));
11808               char *name = NULL;
11809
11810               if (actual_name != NULL)
11811                 {
11812                   char *die_name = dwarf2_name (die, cu);
11813
11814                   if (die_name != NULL
11815                       && strcmp (die_name, actual_name) != 0)
11816                     {
11817                       /* Strip off the class name from the full name.
11818                          We want the prefix.  */
11819                       int die_name_len = strlen (die_name);
11820                       int actual_name_len = strlen (actual_name);
11821
11822                       /* Test for '::' as a sanity check.  */
11823                       if (actual_name_len > die_name_len + 2
11824                           && actual_name[actual_name_len
11825                                          - die_name_len - 1] == ':')
11826                         name =
11827                           obsavestring (actual_name,
11828                                         actual_name_len - die_name_len - 2,
11829                                         &cu->objfile->objfile_obstack);
11830                     }
11831                 }
11832               xfree (actual_name);
11833               return name;
11834             }
11835         }
11836     }
11837
11838   return NULL;
11839 }
11840
11841 /* Return the name of the namespace/class that DIE is defined within,
11842    or "" if we can't tell.  The caller should not xfree the result.
11843
11844    For example, if we're within the method foo() in the following
11845    code:
11846
11847    namespace N {
11848      class C {
11849        void foo () {
11850        }
11851      };
11852    }
11853
11854    then determine_prefix on foo's die will return "N::C".  */
11855
11856 static char *
11857 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11858 {
11859   struct die_info *parent, *spec_die;
11860   struct dwarf2_cu *spec_cu;
11861   struct type *parent_type;
11862
11863   if (cu->language != language_cplus && cu->language != language_java
11864       && cu->language != language_fortran)
11865     return "";
11866
11867   /* We have to be careful in the presence of DW_AT_specification.
11868      For example, with GCC 3.4, given the code
11869
11870      namespace N {
11871        void foo() {
11872          // Definition of N::foo.
11873        }
11874      }
11875
11876      then we'll have a tree of DIEs like this:
11877
11878      1: DW_TAG_compile_unit
11879        2: DW_TAG_namespace        // N
11880          3: DW_TAG_subprogram     // declaration of N::foo
11881        4: DW_TAG_subprogram       // definition of N::foo
11882             DW_AT_specification   // refers to die #3
11883
11884      Thus, when processing die #4, we have to pretend that we're in
11885      the context of its DW_AT_specification, namely the contex of die
11886      #3.  */
11887   spec_cu = cu;
11888   spec_die = die_specification (die, &spec_cu);
11889   if (spec_die == NULL)
11890     parent = die->parent;
11891   else
11892     {
11893       parent = spec_die->parent;
11894       cu = spec_cu;
11895     }
11896
11897   if (parent == NULL)
11898     return "";
11899   else if (parent->building_fullname)
11900     {
11901       const char *name;
11902       const char *parent_name;
11903
11904       /* It has been seen on RealView 2.2 built binaries,
11905          DW_TAG_template_type_param types actually _defined_ as
11906          children of the parent class:
11907
11908          enum E {};
11909          template class <class Enum> Class{};
11910          Class<enum E> class_e;
11911
11912          1: DW_TAG_class_type (Class)
11913            2: DW_TAG_enumeration_type (E)
11914              3: DW_TAG_enumerator (enum1:0)
11915              3: DW_TAG_enumerator (enum2:1)
11916              ...
11917            2: DW_TAG_template_type_param
11918               DW_AT_type  DW_FORM_ref_udata (E)
11919
11920          Besides being broken debug info, it can put GDB into an
11921          infinite loop.  Consider:
11922
11923          When we're building the full name for Class<E>, we'll start
11924          at Class, and go look over its template type parameters,
11925          finding E.  We'll then try to build the full name of E, and
11926          reach here.  We're now trying to build the full name of E,
11927          and look over the parent DIE for containing scope.  In the
11928          broken case, if we followed the parent DIE of E, we'd again
11929          find Class, and once again go look at its template type
11930          arguments, etc., etc.  Simply don't consider such parent die
11931          as source-level parent of this die (it can't be, the language
11932          doesn't allow it), and break the loop here.  */
11933       name = dwarf2_name (die, cu);
11934       parent_name = dwarf2_name (parent, cu);
11935       complaint (&symfile_complaints,
11936                  _("template param type '%s' defined within parent '%s'"),
11937                  name ? name : "<unknown>",
11938                  parent_name ? parent_name : "<unknown>");
11939       return "";
11940     }
11941   else
11942     switch (parent->tag)
11943       {
11944       case DW_TAG_namespace:
11945         parent_type = read_type_die (parent, cu);
11946         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11947            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11948            Work around this problem here.  */
11949         if (cu->language == language_cplus
11950             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11951           return "";
11952         /* We give a name to even anonymous namespaces.  */
11953         return TYPE_TAG_NAME (parent_type);
11954       case DW_TAG_class_type:
11955       case DW_TAG_interface_type:
11956       case DW_TAG_structure_type:
11957       case DW_TAG_union_type:
11958       case DW_TAG_module:
11959         parent_type = read_type_die (parent, cu);
11960         if (TYPE_TAG_NAME (parent_type) != NULL)
11961           return TYPE_TAG_NAME (parent_type);
11962         else
11963           /* An anonymous structure is only allowed non-static data
11964              members; no typedefs, no member functions, et cetera.
11965              So it does not need a prefix.  */
11966           return "";
11967       case DW_TAG_compile_unit:
11968         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
11969         if (cu->language == language_cplus
11970             && dwarf2_per_objfile->types.asection != NULL
11971             && die->child != NULL
11972             && (die->tag == DW_TAG_class_type
11973                 || die->tag == DW_TAG_structure_type
11974                 || die->tag == DW_TAG_union_type))
11975           {
11976             char *name = guess_full_die_structure_name (die, cu);
11977             if (name != NULL)
11978               return name;
11979           }
11980         return "";
11981       default:
11982         return determine_prefix (parent, cu);
11983       }
11984 }
11985
11986 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11987    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11988    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
11989    an obconcat, otherwise allocate storage for the result.  The CU argument is
11990    used to determine the language and hence, the appropriate separator.  */
11991
11992 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11993
11994 static char *
11995 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11996                  int physname, struct dwarf2_cu *cu)
11997 {
11998   const char *lead = "";
11999   const char *sep;
12000
12001   if (suffix == NULL || suffix[0] == '\0'
12002       || prefix == NULL || prefix[0] == '\0')
12003     sep = "";
12004   else if (cu->language == language_java)
12005     sep = ".";
12006   else if (cu->language == language_fortran && physname)
12007     {
12008       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12009          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12010
12011       lead = "__";
12012       sep = "_MOD_";
12013     }
12014   else
12015     sep = "::";
12016
12017   if (prefix == NULL)
12018     prefix = "";
12019   if (suffix == NULL)
12020     suffix = "";
12021
12022   if (obs == NULL)
12023     {
12024       char *retval
12025         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12026
12027       strcpy (retval, lead);
12028       strcat (retval, prefix);
12029       strcat (retval, sep);
12030       strcat (retval, suffix);
12031       return retval;
12032     }
12033   else
12034     {
12035       /* We have an obstack.  */
12036       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12037     }
12038 }
12039
12040 /* Return sibling of die, NULL if no sibling.  */
12041
12042 static struct die_info *
12043 sibling_die (struct die_info *die)
12044 {
12045   return die->sibling;
12046 }
12047
12048 /* Get name of a die, return NULL if not found.  */
12049
12050 static char *
12051 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12052                           struct obstack *obstack)
12053 {
12054   if (name && cu->language == language_cplus)
12055     {
12056       char *canon_name = cp_canonicalize_string (name);
12057
12058       if (canon_name != NULL)
12059         {
12060           if (strcmp (canon_name, name) != 0)
12061             name = obsavestring (canon_name, strlen (canon_name),
12062                                  obstack);
12063           xfree (canon_name);
12064         }
12065     }
12066
12067   return name;
12068 }
12069
12070 /* Get name of a die, return NULL if not found.  */
12071
12072 static char *
12073 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12074 {
12075   struct attribute *attr;
12076
12077   attr = dwarf2_attr (die, DW_AT_name, cu);
12078   if ((!attr || !DW_STRING (attr))
12079       && die->tag != DW_TAG_class_type
12080       && die->tag != DW_TAG_interface_type
12081       && die->tag != DW_TAG_structure_type
12082       && die->tag != DW_TAG_union_type)
12083     return NULL;
12084
12085   switch (die->tag)
12086     {
12087     case DW_TAG_compile_unit:
12088       /* Compilation units have a DW_AT_name that is a filename, not
12089          a source language identifier.  */
12090     case DW_TAG_enumeration_type:
12091     case DW_TAG_enumerator:
12092       /* These tags always have simple identifiers already; no need
12093          to canonicalize them.  */
12094       return DW_STRING (attr);
12095
12096     case DW_TAG_subprogram:
12097       /* Java constructors will all be named "<init>", so return
12098          the class name when we see this special case.  */
12099       if (cu->language == language_java
12100           && DW_STRING (attr) != NULL
12101           && strcmp (DW_STRING (attr), "<init>") == 0)
12102         {
12103           struct dwarf2_cu *spec_cu = cu;
12104           struct die_info *spec_die;
12105
12106           /* GCJ will output '<init>' for Java constructor names.
12107              For this special case, return the name of the parent class.  */
12108
12109           /* GCJ may output suprogram DIEs with AT_specification set.
12110              If so, use the name of the specified DIE.  */
12111           spec_die = die_specification (die, &spec_cu);
12112           if (spec_die != NULL)
12113             return dwarf2_name (spec_die, spec_cu);
12114
12115           do
12116             {
12117               die = die->parent;
12118               if (die->tag == DW_TAG_class_type)
12119                 return dwarf2_name (die, cu);
12120             }
12121           while (die->tag != DW_TAG_compile_unit);
12122         }
12123       break;
12124
12125     case DW_TAG_class_type:
12126     case DW_TAG_interface_type:
12127     case DW_TAG_structure_type:
12128     case DW_TAG_union_type:
12129       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12130          structures or unions.  These were of the form "._%d" in GCC 4.1,
12131          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12132          and GCC 4.4.  We work around this problem by ignoring these.  */
12133       if (attr && DW_STRING (attr)
12134           && (strncmp (DW_STRING (attr), "._", 2) == 0
12135               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12136         return NULL;
12137
12138       /* GCC might emit a nameless typedef that has a linkage name.  See
12139          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12140       if (!attr || DW_STRING (attr) == NULL)
12141         {
12142           char *demangled = NULL;
12143
12144           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12145           if (attr == NULL)
12146             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12147
12148           if (attr == NULL || DW_STRING (attr) == NULL)
12149             return NULL;
12150
12151           /* Avoid demangling DW_STRING (attr) the second time on a second
12152              call for the same DIE.  */
12153           if (!DW_STRING_IS_CANONICAL (attr))
12154             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12155
12156           if (demangled)
12157             {
12158               /* FIXME: we already did this for the partial symbol... */
12159               DW_STRING (attr)
12160                 = obsavestring (demangled, strlen (demangled),
12161                                 &cu->objfile->objfile_obstack);
12162               DW_STRING_IS_CANONICAL (attr) = 1;
12163               xfree (demangled);
12164             }
12165         }
12166       break;
12167
12168     default:
12169       break;
12170     }
12171
12172   if (!DW_STRING_IS_CANONICAL (attr))
12173     {
12174       DW_STRING (attr)
12175         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12176                                     &cu->objfile->objfile_obstack);
12177       DW_STRING_IS_CANONICAL (attr) = 1;
12178     }
12179   return DW_STRING (attr);
12180 }
12181
12182 /* Return the die that this die in an extension of, or NULL if there
12183    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12184    containing the return value on output.  */
12185
12186 static struct die_info *
12187 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12188 {
12189   struct attribute *attr;
12190
12191   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12192   if (attr == NULL)
12193     return NULL;
12194
12195   return follow_die_ref (die, attr, ext_cu);
12196 }
12197
12198 /* Convert a DIE tag into its string name.  */
12199
12200 static char *
12201 dwarf_tag_name (unsigned tag)
12202 {
12203   switch (tag)
12204     {
12205     case DW_TAG_padding:
12206       return "DW_TAG_padding";
12207     case DW_TAG_array_type:
12208       return "DW_TAG_array_type";
12209     case DW_TAG_class_type:
12210       return "DW_TAG_class_type";
12211     case DW_TAG_entry_point:
12212       return "DW_TAG_entry_point";
12213     case DW_TAG_enumeration_type:
12214       return "DW_TAG_enumeration_type";
12215     case DW_TAG_formal_parameter:
12216       return "DW_TAG_formal_parameter";
12217     case DW_TAG_imported_declaration:
12218       return "DW_TAG_imported_declaration";
12219     case DW_TAG_label:
12220       return "DW_TAG_label";
12221     case DW_TAG_lexical_block:
12222       return "DW_TAG_lexical_block";
12223     case DW_TAG_member:
12224       return "DW_TAG_member";
12225     case DW_TAG_pointer_type:
12226       return "DW_TAG_pointer_type";
12227     case DW_TAG_reference_type:
12228       return "DW_TAG_reference_type";
12229     case DW_TAG_compile_unit:
12230       return "DW_TAG_compile_unit";
12231     case DW_TAG_string_type:
12232       return "DW_TAG_string_type";
12233     case DW_TAG_structure_type:
12234       return "DW_TAG_structure_type";
12235     case DW_TAG_subroutine_type:
12236       return "DW_TAG_subroutine_type";
12237     case DW_TAG_typedef:
12238       return "DW_TAG_typedef";
12239     case DW_TAG_union_type:
12240       return "DW_TAG_union_type";
12241     case DW_TAG_unspecified_parameters:
12242       return "DW_TAG_unspecified_parameters";
12243     case DW_TAG_variant:
12244       return "DW_TAG_variant";
12245     case DW_TAG_common_block:
12246       return "DW_TAG_common_block";
12247     case DW_TAG_common_inclusion:
12248       return "DW_TAG_common_inclusion";
12249     case DW_TAG_inheritance:
12250       return "DW_TAG_inheritance";
12251     case DW_TAG_inlined_subroutine:
12252       return "DW_TAG_inlined_subroutine";
12253     case DW_TAG_module:
12254       return "DW_TAG_module";
12255     case DW_TAG_ptr_to_member_type:
12256       return "DW_TAG_ptr_to_member_type";
12257     case DW_TAG_set_type:
12258       return "DW_TAG_set_type";
12259     case DW_TAG_subrange_type:
12260       return "DW_TAG_subrange_type";
12261     case DW_TAG_with_stmt:
12262       return "DW_TAG_with_stmt";
12263     case DW_TAG_access_declaration:
12264       return "DW_TAG_access_declaration";
12265     case DW_TAG_base_type:
12266       return "DW_TAG_base_type";
12267     case DW_TAG_catch_block:
12268       return "DW_TAG_catch_block";
12269     case DW_TAG_const_type:
12270       return "DW_TAG_const_type";
12271     case DW_TAG_constant:
12272       return "DW_TAG_constant";
12273     case DW_TAG_enumerator:
12274       return "DW_TAG_enumerator";
12275     case DW_TAG_file_type:
12276       return "DW_TAG_file_type";
12277     case DW_TAG_friend:
12278       return "DW_TAG_friend";
12279     case DW_TAG_namelist:
12280       return "DW_TAG_namelist";
12281     case DW_TAG_namelist_item:
12282       return "DW_TAG_namelist_item";
12283     case DW_TAG_packed_type:
12284       return "DW_TAG_packed_type";
12285     case DW_TAG_subprogram:
12286       return "DW_TAG_subprogram";
12287     case DW_TAG_template_type_param:
12288       return "DW_TAG_template_type_param";
12289     case DW_TAG_template_value_param:
12290       return "DW_TAG_template_value_param";
12291     case DW_TAG_thrown_type:
12292       return "DW_TAG_thrown_type";
12293     case DW_TAG_try_block:
12294       return "DW_TAG_try_block";
12295     case DW_TAG_variant_part:
12296       return "DW_TAG_variant_part";
12297     case DW_TAG_variable:
12298       return "DW_TAG_variable";
12299     case DW_TAG_volatile_type:
12300       return "DW_TAG_volatile_type";
12301     case DW_TAG_dwarf_procedure:
12302       return "DW_TAG_dwarf_procedure";
12303     case DW_TAG_restrict_type:
12304       return "DW_TAG_restrict_type";
12305     case DW_TAG_interface_type:
12306       return "DW_TAG_interface_type";
12307     case DW_TAG_namespace:
12308       return "DW_TAG_namespace";
12309     case DW_TAG_imported_module:
12310       return "DW_TAG_imported_module";
12311     case DW_TAG_unspecified_type:
12312       return "DW_TAG_unspecified_type";
12313     case DW_TAG_partial_unit:
12314       return "DW_TAG_partial_unit";
12315     case DW_TAG_imported_unit:
12316       return "DW_TAG_imported_unit";
12317     case DW_TAG_condition:
12318       return "DW_TAG_condition";
12319     case DW_TAG_shared_type:
12320       return "DW_TAG_shared_type";
12321     case DW_TAG_type_unit:
12322       return "DW_TAG_type_unit";
12323     case DW_TAG_MIPS_loop:
12324       return "DW_TAG_MIPS_loop";
12325     case DW_TAG_HP_array_descriptor:
12326       return "DW_TAG_HP_array_descriptor";
12327     case DW_TAG_format_label:
12328       return "DW_TAG_format_label";
12329     case DW_TAG_function_template:
12330       return "DW_TAG_function_template";
12331     case DW_TAG_class_template:
12332       return "DW_TAG_class_template";
12333     case DW_TAG_GNU_BINCL:
12334       return "DW_TAG_GNU_BINCL";
12335     case DW_TAG_GNU_EINCL:
12336       return "DW_TAG_GNU_EINCL";
12337     case DW_TAG_upc_shared_type:
12338       return "DW_TAG_upc_shared_type";
12339     case DW_TAG_upc_strict_type:
12340       return "DW_TAG_upc_strict_type";
12341     case DW_TAG_upc_relaxed_type:
12342       return "DW_TAG_upc_relaxed_type";
12343     case DW_TAG_PGI_kanji_type:
12344       return "DW_TAG_PGI_kanji_type";
12345     case DW_TAG_PGI_interface_block:
12346       return "DW_TAG_PGI_interface_block";
12347     default:
12348       return "DW_TAG_<unknown>";
12349     }
12350 }
12351
12352 /* Convert a DWARF attribute code into its string name.  */
12353
12354 static char *
12355 dwarf_attr_name (unsigned attr)
12356 {
12357   switch (attr)
12358     {
12359     case DW_AT_sibling:
12360       return "DW_AT_sibling";
12361     case DW_AT_location:
12362       return "DW_AT_location";
12363     case DW_AT_name:
12364       return "DW_AT_name";
12365     case DW_AT_ordering:
12366       return "DW_AT_ordering";
12367     case DW_AT_subscr_data:
12368       return "DW_AT_subscr_data";
12369     case DW_AT_byte_size:
12370       return "DW_AT_byte_size";
12371     case DW_AT_bit_offset:
12372       return "DW_AT_bit_offset";
12373     case DW_AT_bit_size:
12374       return "DW_AT_bit_size";
12375     case DW_AT_element_list:
12376       return "DW_AT_element_list";
12377     case DW_AT_stmt_list:
12378       return "DW_AT_stmt_list";
12379     case DW_AT_low_pc:
12380       return "DW_AT_low_pc";
12381     case DW_AT_high_pc:
12382       return "DW_AT_high_pc";
12383     case DW_AT_language:
12384       return "DW_AT_language";
12385     case DW_AT_member:
12386       return "DW_AT_member";
12387     case DW_AT_discr:
12388       return "DW_AT_discr";
12389     case DW_AT_discr_value:
12390       return "DW_AT_discr_value";
12391     case DW_AT_visibility:
12392       return "DW_AT_visibility";
12393     case DW_AT_import:
12394       return "DW_AT_import";
12395     case DW_AT_string_length:
12396       return "DW_AT_string_length";
12397     case DW_AT_common_reference:
12398       return "DW_AT_common_reference";
12399     case DW_AT_comp_dir:
12400       return "DW_AT_comp_dir";
12401     case DW_AT_const_value:
12402       return "DW_AT_const_value";
12403     case DW_AT_containing_type:
12404       return "DW_AT_containing_type";
12405     case DW_AT_default_value:
12406       return "DW_AT_default_value";
12407     case DW_AT_inline:
12408       return "DW_AT_inline";
12409     case DW_AT_is_optional:
12410       return "DW_AT_is_optional";
12411     case DW_AT_lower_bound:
12412       return "DW_AT_lower_bound";
12413     case DW_AT_producer:
12414       return "DW_AT_producer";
12415     case DW_AT_prototyped:
12416       return "DW_AT_prototyped";
12417     case DW_AT_return_addr:
12418       return "DW_AT_return_addr";
12419     case DW_AT_start_scope:
12420       return "DW_AT_start_scope";
12421     case DW_AT_bit_stride:
12422       return "DW_AT_bit_stride";
12423     case DW_AT_upper_bound:
12424       return "DW_AT_upper_bound";
12425     case DW_AT_abstract_origin:
12426       return "DW_AT_abstract_origin";
12427     case DW_AT_accessibility:
12428       return "DW_AT_accessibility";
12429     case DW_AT_address_class:
12430       return "DW_AT_address_class";
12431     case DW_AT_artificial:
12432       return "DW_AT_artificial";
12433     case DW_AT_base_types:
12434       return "DW_AT_base_types";
12435     case DW_AT_calling_convention:
12436       return "DW_AT_calling_convention";
12437     case DW_AT_count:
12438       return "DW_AT_count";
12439     case DW_AT_data_member_location:
12440       return "DW_AT_data_member_location";
12441     case DW_AT_decl_column:
12442       return "DW_AT_decl_column";
12443     case DW_AT_decl_file:
12444       return "DW_AT_decl_file";
12445     case DW_AT_decl_line:
12446       return "DW_AT_decl_line";
12447     case DW_AT_declaration:
12448       return "DW_AT_declaration";
12449     case DW_AT_discr_list:
12450       return "DW_AT_discr_list";
12451     case DW_AT_encoding:
12452       return "DW_AT_encoding";
12453     case DW_AT_external:
12454       return "DW_AT_external";
12455     case DW_AT_frame_base:
12456       return "DW_AT_frame_base";
12457     case DW_AT_friend:
12458       return "DW_AT_friend";
12459     case DW_AT_identifier_case:
12460       return "DW_AT_identifier_case";
12461     case DW_AT_macro_info:
12462       return "DW_AT_macro_info";
12463     case DW_AT_namelist_items:
12464       return "DW_AT_namelist_items";
12465     case DW_AT_priority:
12466       return "DW_AT_priority";
12467     case DW_AT_segment:
12468       return "DW_AT_segment";
12469     case DW_AT_specification:
12470       return "DW_AT_specification";
12471     case DW_AT_static_link:
12472       return "DW_AT_static_link";
12473     case DW_AT_type:
12474       return "DW_AT_type";
12475     case DW_AT_use_location:
12476       return "DW_AT_use_location";
12477     case DW_AT_variable_parameter:
12478       return "DW_AT_variable_parameter";
12479     case DW_AT_virtuality:
12480       return "DW_AT_virtuality";
12481     case DW_AT_vtable_elem_location:
12482       return "DW_AT_vtable_elem_location";
12483     /* DWARF 3 values.  */
12484     case DW_AT_allocated:
12485       return "DW_AT_allocated";
12486     case DW_AT_associated:
12487       return "DW_AT_associated";
12488     case DW_AT_data_location:
12489       return "DW_AT_data_location";
12490     case DW_AT_byte_stride:
12491       return "DW_AT_byte_stride";
12492     case DW_AT_entry_pc:
12493       return "DW_AT_entry_pc";
12494     case DW_AT_use_UTF8:
12495       return "DW_AT_use_UTF8";
12496     case DW_AT_extension:
12497       return "DW_AT_extension";
12498     case DW_AT_ranges:
12499       return "DW_AT_ranges";
12500     case DW_AT_trampoline:
12501       return "DW_AT_trampoline";
12502     case DW_AT_call_column:
12503       return "DW_AT_call_column";
12504     case DW_AT_call_file:
12505       return "DW_AT_call_file";
12506     case DW_AT_call_line:
12507       return "DW_AT_call_line";
12508     case DW_AT_description:
12509       return "DW_AT_description";
12510     case DW_AT_binary_scale:
12511       return "DW_AT_binary_scale";
12512     case DW_AT_decimal_scale:
12513       return "DW_AT_decimal_scale";
12514     case DW_AT_small:
12515       return "DW_AT_small";
12516     case DW_AT_decimal_sign:
12517       return "DW_AT_decimal_sign";
12518     case DW_AT_digit_count:
12519       return "DW_AT_digit_count";
12520     case DW_AT_picture_string:
12521       return "DW_AT_picture_string";
12522     case DW_AT_mutable:
12523       return "DW_AT_mutable";
12524     case DW_AT_threads_scaled:
12525       return "DW_AT_threads_scaled";
12526     case DW_AT_explicit:
12527       return "DW_AT_explicit";
12528     case DW_AT_object_pointer:
12529       return "DW_AT_object_pointer";
12530     case DW_AT_endianity:
12531       return "DW_AT_endianity";
12532     case DW_AT_elemental:
12533       return "DW_AT_elemental";
12534     case DW_AT_pure:
12535       return "DW_AT_pure";
12536     case DW_AT_recursive:
12537       return "DW_AT_recursive";
12538     /* DWARF 4 values.  */
12539     case DW_AT_signature:
12540       return "DW_AT_signature";
12541     case DW_AT_linkage_name:
12542       return "DW_AT_linkage_name";
12543     /* SGI/MIPS extensions.  */
12544 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12545     case DW_AT_MIPS_fde:
12546       return "DW_AT_MIPS_fde";
12547 #endif
12548     case DW_AT_MIPS_loop_begin:
12549       return "DW_AT_MIPS_loop_begin";
12550     case DW_AT_MIPS_tail_loop_begin:
12551       return "DW_AT_MIPS_tail_loop_begin";
12552     case DW_AT_MIPS_epilog_begin:
12553       return "DW_AT_MIPS_epilog_begin";
12554     case DW_AT_MIPS_loop_unroll_factor:
12555       return "DW_AT_MIPS_loop_unroll_factor";
12556     case DW_AT_MIPS_software_pipeline_depth:
12557       return "DW_AT_MIPS_software_pipeline_depth";
12558     case DW_AT_MIPS_linkage_name:
12559       return "DW_AT_MIPS_linkage_name";
12560     case DW_AT_MIPS_stride:
12561       return "DW_AT_MIPS_stride";
12562     case DW_AT_MIPS_abstract_name:
12563       return "DW_AT_MIPS_abstract_name";
12564     case DW_AT_MIPS_clone_origin:
12565       return "DW_AT_MIPS_clone_origin";
12566     case DW_AT_MIPS_has_inlines:
12567       return "DW_AT_MIPS_has_inlines";
12568     /* HP extensions.  */
12569 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12570     case DW_AT_HP_block_index:
12571       return "DW_AT_HP_block_index";
12572 #endif
12573     case DW_AT_HP_unmodifiable:
12574       return "DW_AT_HP_unmodifiable";
12575     case DW_AT_HP_actuals_stmt_list:
12576       return "DW_AT_HP_actuals_stmt_list";
12577     case DW_AT_HP_proc_per_section:
12578       return "DW_AT_HP_proc_per_section";
12579     case DW_AT_HP_raw_data_ptr:
12580       return "DW_AT_HP_raw_data_ptr";
12581     case DW_AT_HP_pass_by_reference:
12582       return "DW_AT_HP_pass_by_reference";
12583     case DW_AT_HP_opt_level:
12584       return "DW_AT_HP_opt_level";
12585     case DW_AT_HP_prof_version_id:
12586       return "DW_AT_HP_prof_version_id";
12587     case DW_AT_HP_opt_flags:
12588       return "DW_AT_HP_opt_flags";
12589     case DW_AT_HP_cold_region_low_pc:
12590       return "DW_AT_HP_cold_region_low_pc";
12591     case DW_AT_HP_cold_region_high_pc:
12592       return "DW_AT_HP_cold_region_high_pc";
12593     case DW_AT_HP_all_variables_modifiable:
12594       return "DW_AT_HP_all_variables_modifiable";
12595     case DW_AT_HP_linkage_name:
12596       return "DW_AT_HP_linkage_name";
12597     case DW_AT_HP_prof_flags:
12598       return "DW_AT_HP_prof_flags";
12599     /* GNU extensions.  */
12600     case DW_AT_sf_names:
12601       return "DW_AT_sf_names";
12602     case DW_AT_src_info:
12603       return "DW_AT_src_info";
12604     case DW_AT_mac_info:
12605       return "DW_AT_mac_info";
12606     case DW_AT_src_coords:
12607       return "DW_AT_src_coords";
12608     case DW_AT_body_begin:
12609       return "DW_AT_body_begin";
12610     case DW_AT_body_end:
12611       return "DW_AT_body_end";
12612     case DW_AT_GNU_vector:
12613       return "DW_AT_GNU_vector";
12614     case DW_AT_GNU_odr_signature:
12615       return "DW_AT_GNU_odr_signature";
12616     /* VMS extensions.  */
12617     case DW_AT_VMS_rtnbeg_pd_address:
12618       return "DW_AT_VMS_rtnbeg_pd_address";
12619     /* UPC extension.  */
12620     case DW_AT_upc_threads_scaled:
12621       return "DW_AT_upc_threads_scaled";
12622     /* PGI (STMicroelectronics) extensions.  */
12623     case DW_AT_PGI_lbase:
12624       return "DW_AT_PGI_lbase";
12625     case DW_AT_PGI_soffset:
12626       return "DW_AT_PGI_soffset";
12627     case DW_AT_PGI_lstride:
12628       return "DW_AT_PGI_lstride";
12629     default:
12630       return "DW_AT_<unknown>";
12631     }
12632 }
12633
12634 /* Convert a DWARF value form code into its string name.  */
12635
12636 static char *
12637 dwarf_form_name (unsigned form)
12638 {
12639   switch (form)
12640     {
12641     case DW_FORM_addr:
12642       return "DW_FORM_addr";
12643     case DW_FORM_block2:
12644       return "DW_FORM_block2";
12645     case DW_FORM_block4:
12646       return "DW_FORM_block4";
12647     case DW_FORM_data2:
12648       return "DW_FORM_data2";
12649     case DW_FORM_data4:
12650       return "DW_FORM_data4";
12651     case DW_FORM_data8:
12652       return "DW_FORM_data8";
12653     case DW_FORM_string:
12654       return "DW_FORM_string";
12655     case DW_FORM_block:
12656       return "DW_FORM_block";
12657     case DW_FORM_block1:
12658       return "DW_FORM_block1";
12659     case DW_FORM_data1:
12660       return "DW_FORM_data1";
12661     case DW_FORM_flag:
12662       return "DW_FORM_flag";
12663     case DW_FORM_sdata:
12664       return "DW_FORM_sdata";
12665     case DW_FORM_strp:
12666       return "DW_FORM_strp";
12667     case DW_FORM_udata:
12668       return "DW_FORM_udata";
12669     case DW_FORM_ref_addr:
12670       return "DW_FORM_ref_addr";
12671     case DW_FORM_ref1:
12672       return "DW_FORM_ref1";
12673     case DW_FORM_ref2:
12674       return "DW_FORM_ref2";
12675     case DW_FORM_ref4:
12676       return "DW_FORM_ref4";
12677     case DW_FORM_ref8:
12678       return "DW_FORM_ref8";
12679     case DW_FORM_ref_udata:
12680       return "DW_FORM_ref_udata";
12681     case DW_FORM_indirect:
12682       return "DW_FORM_indirect";
12683     case DW_FORM_sec_offset:
12684       return "DW_FORM_sec_offset";
12685     case DW_FORM_exprloc:
12686       return "DW_FORM_exprloc";
12687     case DW_FORM_flag_present:
12688       return "DW_FORM_flag_present";
12689     case DW_FORM_ref_sig8:
12690       return "DW_FORM_ref_sig8";
12691     default:
12692       return "DW_FORM_<unknown>";
12693     }
12694 }
12695
12696 /* Convert a DWARF stack opcode into its string name.  */
12697
12698 const char *
12699 dwarf_stack_op_name (unsigned op)
12700 {
12701   switch (op)
12702     {
12703     case DW_OP_addr:
12704       return "DW_OP_addr";
12705     case DW_OP_deref:
12706       return "DW_OP_deref";
12707     case DW_OP_const1u:
12708       return "DW_OP_const1u";
12709     case DW_OP_const1s:
12710       return "DW_OP_const1s";
12711     case DW_OP_const2u:
12712       return "DW_OP_const2u";
12713     case DW_OP_const2s:
12714       return "DW_OP_const2s";
12715     case DW_OP_const4u:
12716       return "DW_OP_const4u";
12717     case DW_OP_const4s:
12718       return "DW_OP_const4s";
12719     case DW_OP_const8u:
12720       return "DW_OP_const8u";
12721     case DW_OP_const8s:
12722       return "DW_OP_const8s";
12723     case DW_OP_constu:
12724       return "DW_OP_constu";
12725     case DW_OP_consts:
12726       return "DW_OP_consts";
12727     case DW_OP_dup:
12728       return "DW_OP_dup";
12729     case DW_OP_drop:
12730       return "DW_OP_drop";
12731     case DW_OP_over:
12732       return "DW_OP_over";
12733     case DW_OP_pick:
12734       return "DW_OP_pick";
12735     case DW_OP_swap:
12736       return "DW_OP_swap";
12737     case DW_OP_rot:
12738       return "DW_OP_rot";
12739     case DW_OP_xderef:
12740       return "DW_OP_xderef";
12741     case DW_OP_abs:
12742       return "DW_OP_abs";
12743     case DW_OP_and:
12744       return "DW_OP_and";
12745     case DW_OP_div:
12746       return "DW_OP_div";
12747     case DW_OP_minus:
12748       return "DW_OP_minus";
12749     case DW_OP_mod:
12750       return "DW_OP_mod";
12751     case DW_OP_mul:
12752       return "DW_OP_mul";
12753     case DW_OP_neg:
12754       return "DW_OP_neg";
12755     case DW_OP_not:
12756       return "DW_OP_not";
12757     case DW_OP_or:
12758       return "DW_OP_or";
12759     case DW_OP_plus:
12760       return "DW_OP_plus";
12761     case DW_OP_plus_uconst:
12762       return "DW_OP_plus_uconst";
12763     case DW_OP_shl:
12764       return "DW_OP_shl";
12765     case DW_OP_shr:
12766       return "DW_OP_shr";
12767     case DW_OP_shra:
12768       return "DW_OP_shra";
12769     case DW_OP_xor:
12770       return "DW_OP_xor";
12771     case DW_OP_bra:
12772       return "DW_OP_bra";
12773     case DW_OP_eq:
12774       return "DW_OP_eq";
12775     case DW_OP_ge:
12776       return "DW_OP_ge";
12777     case DW_OP_gt:
12778       return "DW_OP_gt";
12779     case DW_OP_le:
12780       return "DW_OP_le";
12781     case DW_OP_lt:
12782       return "DW_OP_lt";
12783     case DW_OP_ne:
12784       return "DW_OP_ne";
12785     case DW_OP_skip:
12786       return "DW_OP_skip";
12787     case DW_OP_lit0:
12788       return "DW_OP_lit0";
12789     case DW_OP_lit1:
12790       return "DW_OP_lit1";
12791     case DW_OP_lit2:
12792       return "DW_OP_lit2";
12793     case DW_OP_lit3:
12794       return "DW_OP_lit3";
12795     case DW_OP_lit4:
12796       return "DW_OP_lit4";
12797     case DW_OP_lit5:
12798       return "DW_OP_lit5";
12799     case DW_OP_lit6:
12800       return "DW_OP_lit6";
12801     case DW_OP_lit7:
12802       return "DW_OP_lit7";
12803     case DW_OP_lit8:
12804       return "DW_OP_lit8";
12805     case DW_OP_lit9:
12806       return "DW_OP_lit9";
12807     case DW_OP_lit10:
12808       return "DW_OP_lit10";
12809     case DW_OP_lit11:
12810       return "DW_OP_lit11";
12811     case DW_OP_lit12:
12812       return "DW_OP_lit12";
12813     case DW_OP_lit13:
12814       return "DW_OP_lit13";
12815     case DW_OP_lit14:
12816       return "DW_OP_lit14";
12817     case DW_OP_lit15:
12818       return "DW_OP_lit15";
12819     case DW_OP_lit16:
12820       return "DW_OP_lit16";
12821     case DW_OP_lit17:
12822       return "DW_OP_lit17";
12823     case DW_OP_lit18:
12824       return "DW_OP_lit18";
12825     case DW_OP_lit19:
12826       return "DW_OP_lit19";
12827     case DW_OP_lit20:
12828       return "DW_OP_lit20";
12829     case DW_OP_lit21:
12830       return "DW_OP_lit21";
12831     case DW_OP_lit22:
12832       return "DW_OP_lit22";
12833     case DW_OP_lit23:
12834       return "DW_OP_lit23";
12835     case DW_OP_lit24:
12836       return "DW_OP_lit24";
12837     case DW_OP_lit25:
12838       return "DW_OP_lit25";
12839     case DW_OP_lit26:
12840       return "DW_OP_lit26";
12841     case DW_OP_lit27:
12842       return "DW_OP_lit27";
12843     case DW_OP_lit28:
12844       return "DW_OP_lit28";
12845     case DW_OP_lit29:
12846       return "DW_OP_lit29";
12847     case DW_OP_lit30:
12848       return "DW_OP_lit30";
12849     case DW_OP_lit31:
12850       return "DW_OP_lit31";
12851     case DW_OP_reg0:
12852       return "DW_OP_reg0";
12853     case DW_OP_reg1:
12854       return "DW_OP_reg1";
12855     case DW_OP_reg2:
12856       return "DW_OP_reg2";
12857     case DW_OP_reg3:
12858       return "DW_OP_reg3";
12859     case DW_OP_reg4:
12860       return "DW_OP_reg4";
12861     case DW_OP_reg5:
12862       return "DW_OP_reg5";
12863     case DW_OP_reg6:
12864       return "DW_OP_reg6";
12865     case DW_OP_reg7:
12866       return "DW_OP_reg7";
12867     case DW_OP_reg8:
12868       return "DW_OP_reg8";
12869     case DW_OP_reg9:
12870       return "DW_OP_reg9";
12871     case DW_OP_reg10:
12872       return "DW_OP_reg10";
12873     case DW_OP_reg11:
12874       return "DW_OP_reg11";
12875     case DW_OP_reg12:
12876       return "DW_OP_reg12";
12877     case DW_OP_reg13:
12878       return "DW_OP_reg13";
12879     case DW_OP_reg14:
12880       return "DW_OP_reg14";
12881     case DW_OP_reg15:
12882       return "DW_OP_reg15";
12883     case DW_OP_reg16:
12884       return "DW_OP_reg16";
12885     case DW_OP_reg17:
12886       return "DW_OP_reg17";
12887     case DW_OP_reg18:
12888       return "DW_OP_reg18";
12889     case DW_OP_reg19:
12890       return "DW_OP_reg19";
12891     case DW_OP_reg20:
12892       return "DW_OP_reg20";
12893     case DW_OP_reg21:
12894       return "DW_OP_reg21";
12895     case DW_OP_reg22:
12896       return "DW_OP_reg22";
12897     case DW_OP_reg23:
12898       return "DW_OP_reg23";
12899     case DW_OP_reg24:
12900       return "DW_OP_reg24";
12901     case DW_OP_reg25:
12902       return "DW_OP_reg25";
12903     case DW_OP_reg26:
12904       return "DW_OP_reg26";
12905     case DW_OP_reg27:
12906       return "DW_OP_reg27";
12907     case DW_OP_reg28:
12908       return "DW_OP_reg28";
12909     case DW_OP_reg29:
12910       return "DW_OP_reg29";
12911     case DW_OP_reg30:
12912       return "DW_OP_reg30";
12913     case DW_OP_reg31:
12914       return "DW_OP_reg31";
12915     case DW_OP_breg0:
12916       return "DW_OP_breg0";
12917     case DW_OP_breg1:
12918       return "DW_OP_breg1";
12919     case DW_OP_breg2:
12920       return "DW_OP_breg2";
12921     case DW_OP_breg3:
12922       return "DW_OP_breg3";
12923     case DW_OP_breg4:
12924       return "DW_OP_breg4";
12925     case DW_OP_breg5:
12926       return "DW_OP_breg5";
12927     case DW_OP_breg6:
12928       return "DW_OP_breg6";
12929     case DW_OP_breg7:
12930       return "DW_OP_breg7";
12931     case DW_OP_breg8:
12932       return "DW_OP_breg8";
12933     case DW_OP_breg9:
12934       return "DW_OP_breg9";
12935     case DW_OP_breg10:
12936       return "DW_OP_breg10";
12937     case DW_OP_breg11:
12938       return "DW_OP_breg11";
12939     case DW_OP_breg12:
12940       return "DW_OP_breg12";
12941     case DW_OP_breg13:
12942       return "DW_OP_breg13";
12943     case DW_OP_breg14:
12944       return "DW_OP_breg14";
12945     case DW_OP_breg15:
12946       return "DW_OP_breg15";
12947     case DW_OP_breg16:
12948       return "DW_OP_breg16";
12949     case DW_OP_breg17:
12950       return "DW_OP_breg17";
12951     case DW_OP_breg18:
12952       return "DW_OP_breg18";
12953     case DW_OP_breg19:
12954       return "DW_OP_breg19";
12955     case DW_OP_breg20:
12956       return "DW_OP_breg20";
12957     case DW_OP_breg21:
12958       return "DW_OP_breg21";
12959     case DW_OP_breg22:
12960       return "DW_OP_breg22";
12961     case DW_OP_breg23:
12962       return "DW_OP_breg23";
12963     case DW_OP_breg24:
12964       return "DW_OP_breg24";
12965     case DW_OP_breg25:
12966       return "DW_OP_breg25";
12967     case DW_OP_breg26:
12968       return "DW_OP_breg26";
12969     case DW_OP_breg27:
12970       return "DW_OP_breg27";
12971     case DW_OP_breg28:
12972       return "DW_OP_breg28";
12973     case DW_OP_breg29:
12974       return "DW_OP_breg29";
12975     case DW_OP_breg30:
12976       return "DW_OP_breg30";
12977     case DW_OP_breg31:
12978       return "DW_OP_breg31";
12979     case DW_OP_regx:
12980       return "DW_OP_regx";
12981     case DW_OP_fbreg:
12982       return "DW_OP_fbreg";
12983     case DW_OP_bregx:
12984       return "DW_OP_bregx";
12985     case DW_OP_piece:
12986       return "DW_OP_piece";
12987     case DW_OP_deref_size:
12988       return "DW_OP_deref_size";
12989     case DW_OP_xderef_size:
12990       return "DW_OP_xderef_size";
12991     case DW_OP_nop:
12992       return "DW_OP_nop";
12993     /* DWARF 3 extensions.  */
12994     case DW_OP_push_object_address:
12995       return "DW_OP_push_object_address";
12996     case DW_OP_call2:
12997       return "DW_OP_call2";
12998     case DW_OP_call4:
12999       return "DW_OP_call4";
13000     case DW_OP_call_ref:
13001       return "DW_OP_call_ref";
13002     case DW_OP_form_tls_address:
13003       return "DW_OP_form_tls_address";
13004     case DW_OP_call_frame_cfa:
13005       return "DW_OP_call_frame_cfa";
13006     case DW_OP_bit_piece:
13007       return "DW_OP_bit_piece";
13008     /* DWARF 4 extensions.  */
13009     case DW_OP_implicit_value:
13010       return "DW_OP_implicit_value";
13011     case DW_OP_stack_value:
13012       return "DW_OP_stack_value";
13013     /* GNU extensions.  */
13014     case DW_OP_GNU_push_tls_address:
13015       return "DW_OP_GNU_push_tls_address";
13016     case DW_OP_GNU_uninit:
13017       return "DW_OP_GNU_uninit";
13018     case DW_OP_GNU_implicit_pointer:
13019       return "DW_OP_GNU_implicit_pointer";
13020     default:
13021       return NULL;
13022     }
13023 }
13024
13025 static char *
13026 dwarf_bool_name (unsigned mybool)
13027 {
13028   if (mybool)
13029     return "TRUE";
13030   else
13031     return "FALSE";
13032 }
13033
13034 /* Convert a DWARF type code into its string name.  */
13035
13036 static char *
13037 dwarf_type_encoding_name (unsigned enc)
13038 {
13039   switch (enc)
13040     {
13041     case DW_ATE_void:
13042       return "DW_ATE_void";
13043     case DW_ATE_address:
13044       return "DW_ATE_address";
13045     case DW_ATE_boolean:
13046       return "DW_ATE_boolean";
13047     case DW_ATE_complex_float:
13048       return "DW_ATE_complex_float";
13049     case DW_ATE_float:
13050       return "DW_ATE_float";
13051     case DW_ATE_signed:
13052       return "DW_ATE_signed";
13053     case DW_ATE_signed_char:
13054       return "DW_ATE_signed_char";
13055     case DW_ATE_unsigned:
13056       return "DW_ATE_unsigned";
13057     case DW_ATE_unsigned_char:
13058       return "DW_ATE_unsigned_char";
13059     /* DWARF 3.  */
13060     case DW_ATE_imaginary_float:
13061       return "DW_ATE_imaginary_float";
13062     case DW_ATE_packed_decimal:
13063       return "DW_ATE_packed_decimal";
13064     case DW_ATE_numeric_string:
13065       return "DW_ATE_numeric_string";
13066     case DW_ATE_edited:
13067       return "DW_ATE_edited";
13068     case DW_ATE_signed_fixed:
13069       return "DW_ATE_signed_fixed";
13070     case DW_ATE_unsigned_fixed:
13071       return "DW_ATE_unsigned_fixed";
13072     case DW_ATE_decimal_float:
13073       return "DW_ATE_decimal_float";
13074     /* DWARF 4.  */
13075     case DW_ATE_UTF:
13076       return "DW_ATE_UTF";
13077     /* HP extensions.  */
13078     case DW_ATE_HP_float80:
13079       return "DW_ATE_HP_float80";
13080     case DW_ATE_HP_complex_float80:
13081       return "DW_ATE_HP_complex_float80";
13082     case DW_ATE_HP_float128:
13083       return "DW_ATE_HP_float128";
13084     case DW_ATE_HP_complex_float128:
13085       return "DW_ATE_HP_complex_float128";
13086     case DW_ATE_HP_floathpintel:
13087       return "DW_ATE_HP_floathpintel";
13088     case DW_ATE_HP_imaginary_float80:
13089       return "DW_ATE_HP_imaginary_float80";
13090     case DW_ATE_HP_imaginary_float128:
13091       return "DW_ATE_HP_imaginary_float128";
13092     default:
13093       return "DW_ATE_<unknown>";
13094     }
13095 }
13096
13097 /* Convert a DWARF call frame info operation to its string name.  */
13098
13099 #if 0
13100 static char *
13101 dwarf_cfi_name (unsigned cfi_opc)
13102 {
13103   switch (cfi_opc)
13104     {
13105     case DW_CFA_advance_loc:
13106       return "DW_CFA_advance_loc";
13107     case DW_CFA_offset:
13108       return "DW_CFA_offset";
13109     case DW_CFA_restore:
13110       return "DW_CFA_restore";
13111     case DW_CFA_nop:
13112       return "DW_CFA_nop";
13113     case DW_CFA_set_loc:
13114       return "DW_CFA_set_loc";
13115     case DW_CFA_advance_loc1:
13116       return "DW_CFA_advance_loc1";
13117     case DW_CFA_advance_loc2:
13118       return "DW_CFA_advance_loc2";
13119     case DW_CFA_advance_loc4:
13120       return "DW_CFA_advance_loc4";
13121     case DW_CFA_offset_extended:
13122       return "DW_CFA_offset_extended";
13123     case DW_CFA_restore_extended:
13124       return "DW_CFA_restore_extended";
13125     case DW_CFA_undefined:
13126       return "DW_CFA_undefined";
13127     case DW_CFA_same_value:
13128       return "DW_CFA_same_value";
13129     case DW_CFA_register:
13130       return "DW_CFA_register";
13131     case DW_CFA_remember_state:
13132       return "DW_CFA_remember_state";
13133     case DW_CFA_restore_state:
13134       return "DW_CFA_restore_state";
13135     case DW_CFA_def_cfa:
13136       return "DW_CFA_def_cfa";
13137     case DW_CFA_def_cfa_register:
13138       return "DW_CFA_def_cfa_register";
13139     case DW_CFA_def_cfa_offset:
13140       return "DW_CFA_def_cfa_offset";
13141     /* DWARF 3.  */
13142     case DW_CFA_def_cfa_expression:
13143       return "DW_CFA_def_cfa_expression";
13144     case DW_CFA_expression:
13145       return "DW_CFA_expression";
13146     case DW_CFA_offset_extended_sf:
13147       return "DW_CFA_offset_extended_sf";
13148     case DW_CFA_def_cfa_sf:
13149       return "DW_CFA_def_cfa_sf";
13150     case DW_CFA_def_cfa_offset_sf:
13151       return "DW_CFA_def_cfa_offset_sf";
13152     case DW_CFA_val_offset:
13153       return "DW_CFA_val_offset";
13154     case DW_CFA_val_offset_sf:
13155       return "DW_CFA_val_offset_sf";
13156     case DW_CFA_val_expression:
13157       return "DW_CFA_val_expression";
13158     /* SGI/MIPS specific.  */
13159     case DW_CFA_MIPS_advance_loc8:
13160       return "DW_CFA_MIPS_advance_loc8";
13161     /* GNU extensions.  */
13162     case DW_CFA_GNU_window_save:
13163       return "DW_CFA_GNU_window_save";
13164     case DW_CFA_GNU_args_size:
13165       return "DW_CFA_GNU_args_size";
13166     case DW_CFA_GNU_negative_offset_extended:
13167       return "DW_CFA_GNU_negative_offset_extended";
13168     default:
13169       return "DW_CFA_<unknown>";
13170     }
13171 }
13172 #endif
13173
13174 static void
13175 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13176 {
13177   unsigned int i;
13178
13179   print_spaces (indent, f);
13180   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13181            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13182
13183   if (die->parent != NULL)
13184     {
13185       print_spaces (indent, f);
13186       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13187                           die->parent->offset);
13188     }
13189
13190   print_spaces (indent, f);
13191   fprintf_unfiltered (f, "  has children: %s\n",
13192            dwarf_bool_name (die->child != NULL));
13193
13194   print_spaces (indent, f);
13195   fprintf_unfiltered (f, "  attributes:\n");
13196
13197   for (i = 0; i < die->num_attrs; ++i)
13198     {
13199       print_spaces (indent, f);
13200       fprintf_unfiltered (f, "    %s (%s) ",
13201                dwarf_attr_name (die->attrs[i].name),
13202                dwarf_form_name (die->attrs[i].form));
13203
13204       switch (die->attrs[i].form)
13205         {
13206         case DW_FORM_ref_addr:
13207         case DW_FORM_addr:
13208           fprintf_unfiltered (f, "address: ");
13209           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13210           break;
13211         case DW_FORM_block2:
13212         case DW_FORM_block4:
13213         case DW_FORM_block:
13214         case DW_FORM_block1:
13215           fprintf_unfiltered (f, "block: size %d",
13216                               DW_BLOCK (&die->attrs[i])->size);
13217           break;
13218         case DW_FORM_exprloc:
13219           fprintf_unfiltered (f, "expression: size %u",
13220                               DW_BLOCK (&die->attrs[i])->size);
13221           break;
13222         case DW_FORM_ref1:
13223         case DW_FORM_ref2:
13224         case DW_FORM_ref4:
13225           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13226                               (long) (DW_ADDR (&die->attrs[i])));
13227           break;
13228         case DW_FORM_data1:
13229         case DW_FORM_data2:
13230         case DW_FORM_data4:
13231         case DW_FORM_data8:
13232         case DW_FORM_udata:
13233         case DW_FORM_sdata:
13234           fprintf_unfiltered (f, "constant: %s",
13235                               pulongest (DW_UNSND (&die->attrs[i])));
13236           break;
13237         case DW_FORM_sec_offset:
13238           fprintf_unfiltered (f, "section offset: %s",
13239                               pulongest (DW_UNSND (&die->attrs[i])));
13240           break;
13241         case DW_FORM_ref_sig8:
13242           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13243             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13244                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13245           else
13246             fprintf_unfiltered (f, "signatured type, offset: unknown");
13247           break;
13248         case DW_FORM_string:
13249         case DW_FORM_strp:
13250           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13251                    DW_STRING (&die->attrs[i])
13252                    ? DW_STRING (&die->attrs[i]) : "",
13253                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13254           break;
13255         case DW_FORM_flag:
13256           if (DW_UNSND (&die->attrs[i]))
13257             fprintf_unfiltered (f, "flag: TRUE");
13258           else
13259             fprintf_unfiltered (f, "flag: FALSE");
13260           break;
13261         case DW_FORM_flag_present:
13262           fprintf_unfiltered (f, "flag: TRUE");
13263           break;
13264         case DW_FORM_indirect:
13265           /* The reader will have reduced the indirect form to
13266              the "base form" so this form should not occur.  */
13267           fprintf_unfiltered (f, 
13268                               "unexpected attribute form: DW_FORM_indirect");
13269           break;
13270         default:
13271           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13272                    die->attrs[i].form);
13273           break;
13274         }
13275       fprintf_unfiltered (f, "\n");
13276     }
13277 }
13278
13279 static void
13280 dump_die_for_error (struct die_info *die)
13281 {
13282   dump_die_shallow (gdb_stderr, 0, die);
13283 }
13284
13285 static void
13286 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13287 {
13288   int indent = level * 4;
13289
13290   gdb_assert (die != NULL);
13291
13292   if (level >= max_level)
13293     return;
13294
13295   dump_die_shallow (f, indent, die);
13296
13297   if (die->child != NULL)
13298     {
13299       print_spaces (indent, f);
13300       fprintf_unfiltered (f, "  Children:");
13301       if (level + 1 < max_level)
13302         {
13303           fprintf_unfiltered (f, "\n");
13304           dump_die_1 (f, level + 1, max_level, die->child);
13305         }
13306       else
13307         {
13308           fprintf_unfiltered (f,
13309                               " [not printed, max nesting level reached]\n");
13310         }
13311     }
13312
13313   if (die->sibling != NULL && level > 0)
13314     {
13315       dump_die_1 (f, level, max_level, die->sibling);
13316     }
13317 }
13318
13319 /* This is called from the pdie macro in gdbinit.in.
13320    It's not static so gcc will keep a copy callable from gdb.  */
13321
13322 void
13323 dump_die (struct die_info *die, int max_level)
13324 {
13325   dump_die_1 (gdb_stdlog, 0, max_level, die);
13326 }
13327
13328 static void
13329 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13330 {
13331   void **slot;
13332
13333   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13334
13335   *slot = die;
13336 }
13337
13338 static int
13339 is_ref_attr (struct attribute *attr)
13340 {
13341   switch (attr->form)
13342     {
13343     case DW_FORM_ref_addr:
13344     case DW_FORM_ref1:
13345     case DW_FORM_ref2:
13346     case DW_FORM_ref4:
13347     case DW_FORM_ref8:
13348     case DW_FORM_ref_udata:
13349       return 1;
13350     default:
13351       return 0;
13352     }
13353 }
13354
13355 static unsigned int
13356 dwarf2_get_ref_die_offset (struct attribute *attr)
13357 {
13358   if (is_ref_attr (attr))
13359     return DW_ADDR (attr);
13360
13361   complaint (&symfile_complaints,
13362              _("unsupported die ref attribute form: '%s'"),
13363              dwarf_form_name (attr->form));
13364   return 0;
13365 }
13366
13367 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13368  * the value held by the attribute is not constant.  */
13369
13370 static LONGEST
13371 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13372 {
13373   if (attr->form == DW_FORM_sdata)
13374     return DW_SND (attr);
13375   else if (attr->form == DW_FORM_udata
13376            || attr->form == DW_FORM_data1
13377            || attr->form == DW_FORM_data2
13378            || attr->form == DW_FORM_data4
13379            || attr->form == DW_FORM_data8)
13380     return DW_UNSND (attr);
13381   else
13382     {
13383       complaint (&symfile_complaints,
13384                  _("Attribute value is not a constant (%s)"),
13385                  dwarf_form_name (attr->form));
13386       return default_value;
13387     }
13388 }
13389
13390 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13391    unit and add it to our queue.
13392    The result is non-zero if PER_CU was queued, otherwise the result is zero
13393    meaning either PER_CU is already queued or it is already loaded.  */
13394
13395 static int
13396 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13397                        struct dwarf2_per_cu_data *per_cu)
13398 {
13399   /* We may arrive here during partial symbol reading, if we need full
13400      DIEs to process an unusual case (e.g. template arguments).  Do
13401      not queue PER_CU, just tell our caller to load its DIEs.  */
13402   if (dwarf2_per_objfile->reading_partial_symbols)
13403     {
13404       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13405         return 1;
13406       return 0;
13407     }
13408
13409   /* Mark the dependence relation so that we don't flush PER_CU
13410      too early.  */
13411   dwarf2_add_dependence (this_cu, per_cu);
13412
13413   /* If it's already on the queue, we have nothing to do.  */
13414   if (per_cu->queued)
13415     return 0;
13416
13417   /* If the compilation unit is already loaded, just mark it as
13418      used.  */
13419   if (per_cu->cu != NULL)
13420     {
13421       per_cu->cu->last_used = 0;
13422       return 0;
13423     }
13424
13425   /* Add it to the queue.  */
13426   queue_comp_unit (per_cu, this_cu->objfile);
13427
13428   return 1;
13429 }
13430
13431 /* Follow reference or signature attribute ATTR of SRC_DIE.
13432    On entry *REF_CU is the CU of SRC_DIE.
13433    On exit *REF_CU is the CU of the result.  */
13434
13435 static struct die_info *
13436 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13437                        struct dwarf2_cu **ref_cu)
13438 {
13439   struct die_info *die;
13440
13441   if (is_ref_attr (attr))
13442     die = follow_die_ref (src_die, attr, ref_cu);
13443   else if (attr->form == DW_FORM_ref_sig8)
13444     die = follow_die_sig (src_die, attr, ref_cu);
13445   else
13446     {
13447       dump_die_for_error (src_die);
13448       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13449              (*ref_cu)->objfile->name);
13450     }
13451
13452   return die;
13453 }
13454
13455 /* Follow reference OFFSET.
13456    On entry *REF_CU is the CU of the source die referencing OFFSET.
13457    On exit *REF_CU is the CU of the result.
13458    Returns NULL if OFFSET is invalid.  */
13459
13460 static struct die_info *
13461 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13462 {
13463   struct die_info temp_die;
13464   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13465
13466   gdb_assert (cu->per_cu != NULL);
13467
13468   target_cu = cu;
13469
13470   if (cu->per_cu->from_debug_types)
13471     {
13472       /* .debug_types CUs cannot reference anything outside their CU.
13473          If they need to, they have to reference a signatured type via
13474          DW_FORM_ref_sig8.  */
13475       if (! offset_in_cu_p (&cu->header, offset))
13476         return NULL;
13477     }
13478   else if (! offset_in_cu_p (&cu->header, offset))
13479     {
13480       struct dwarf2_per_cu_data *per_cu;
13481
13482       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13483
13484       /* If necessary, add it to the queue and load its DIEs.  */
13485       if (maybe_queue_comp_unit (cu, per_cu))
13486         load_full_comp_unit (per_cu, cu->objfile);
13487
13488       target_cu = per_cu->cu;
13489     }
13490   else if (cu->dies == NULL)
13491     {
13492       /* We're loading full DIEs during partial symbol reading.  */
13493       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13494       load_full_comp_unit (cu->per_cu, cu->objfile);
13495     }
13496
13497   *ref_cu = target_cu;
13498   temp_die.offset = offset;
13499   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13500 }
13501
13502 /* Follow reference attribute ATTR of SRC_DIE.
13503    On entry *REF_CU is the CU of SRC_DIE.
13504    On exit *REF_CU is the CU of the result.  */
13505
13506 static struct die_info *
13507 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13508                 struct dwarf2_cu **ref_cu)
13509 {
13510   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13511   struct dwarf2_cu *cu = *ref_cu;
13512   struct die_info *die;
13513
13514   die = follow_die_offset (offset, ref_cu);
13515   if (!die)
13516     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13517            "at 0x%x [in module %s]"),
13518            offset, src_die->offset, cu->objfile->name);
13519
13520   return die;
13521 }
13522
13523 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13524    value is intended for DW_OP_call*.  */
13525
13526 struct dwarf2_locexpr_baton
13527 dwarf2_fetch_die_location_block (unsigned int offset,
13528                                  struct dwarf2_per_cu_data *per_cu,
13529                                  CORE_ADDR (*get_frame_pc) (void *baton),
13530                                  void *baton)
13531 {
13532   struct dwarf2_cu *cu = per_cu->cu;
13533   struct die_info *die;
13534   struct attribute *attr;
13535   struct dwarf2_locexpr_baton retval;
13536
13537   dw2_setup (per_cu->objfile);
13538
13539   die = follow_die_offset (offset, &cu);
13540   if (!die)
13541     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13542            offset, per_cu->cu->objfile->name);
13543
13544   attr = dwarf2_attr (die, DW_AT_location, cu);
13545   if (!attr)
13546     {
13547       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13548
13549       retval.data = NULL;
13550       retval.size = 0;
13551     }
13552   else if (attr_form_is_section_offset (attr))
13553     {
13554       struct dwarf2_loclist_baton loclist_baton;
13555       CORE_ADDR pc = (*get_frame_pc) (baton);
13556       size_t size;
13557
13558       fill_in_loclist_baton (cu, &loclist_baton, attr);
13559
13560       retval.data = dwarf2_find_location_expression (&loclist_baton,
13561                                                      &size, pc);
13562       retval.size = size;
13563     }
13564   else
13565     {
13566       if (!attr_form_is_block (attr))
13567         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13568                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13569                offset, per_cu->cu->objfile->name);
13570
13571       retval.data = DW_BLOCK (attr)->data;
13572       retval.size = DW_BLOCK (attr)->size;
13573     }
13574   retval.per_cu = cu->per_cu;
13575   return retval;
13576 }
13577
13578 /* Follow the signature attribute ATTR in SRC_DIE.
13579    On entry *REF_CU is the CU of SRC_DIE.
13580    On exit *REF_CU is the CU of the result.  */
13581
13582 static struct die_info *
13583 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13584                 struct dwarf2_cu **ref_cu)
13585 {
13586   struct objfile *objfile = (*ref_cu)->objfile;
13587   struct die_info temp_die;
13588   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13589   struct dwarf2_cu *sig_cu;
13590   struct die_info *die;
13591
13592   /* sig_type will be NULL if the signatured type is missing from
13593      the debug info.  */
13594   if (sig_type == NULL)
13595     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13596              "at 0x%x [in module %s]"),
13597            src_die->offset, objfile->name);
13598
13599   /* If necessary, add it to the queue and load its DIEs.  */
13600
13601   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13602     read_signatured_type (objfile, sig_type);
13603
13604   gdb_assert (sig_type->per_cu.cu != NULL);
13605
13606   sig_cu = sig_type->per_cu.cu;
13607   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13608   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13609   if (die)
13610     {
13611       *ref_cu = sig_cu;
13612       return die;
13613     }
13614
13615   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13616          "from DIE at 0x%x [in module %s]"),
13617          sig_type->type_offset, src_die->offset, objfile->name);
13618 }
13619
13620 /* Given an offset of a signatured type, return its signatured_type.  */
13621
13622 static struct signatured_type *
13623 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13624 {
13625   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13626   unsigned int length, initial_length_size;
13627   unsigned int sig_offset;
13628   struct signatured_type find_entry, *type_sig;
13629
13630   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13631   sig_offset = (initial_length_size
13632                 + 2 /*version*/
13633                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13634                 + 1 /*address_size*/);
13635   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13636   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13637
13638   /* This is only used to lookup previously recorded types.
13639      If we didn't find it, it's our bug.  */
13640   gdb_assert (type_sig != NULL);
13641   gdb_assert (offset == type_sig->offset);
13642
13643   return type_sig;
13644 }
13645
13646 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13647
13648 static void
13649 read_signatured_type_at_offset (struct objfile *objfile,
13650                                 unsigned int offset)
13651 {
13652   struct signatured_type *type_sig;
13653
13654   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13655
13656   /* We have the section offset, but we need the signature to do the
13657      hash table lookup.  */
13658   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13659
13660   gdb_assert (type_sig->per_cu.cu == NULL);
13661
13662   read_signatured_type (objfile, type_sig);
13663
13664   gdb_assert (type_sig->per_cu.cu != NULL);
13665 }
13666
13667 /* Read in a signatured type and build its CU and DIEs.  */
13668
13669 static void
13670 read_signatured_type (struct objfile *objfile,
13671                       struct signatured_type *type_sig)
13672 {
13673   gdb_byte *types_ptr;
13674   struct die_reader_specs reader_specs;
13675   struct dwarf2_cu *cu;
13676   ULONGEST signature;
13677   struct cleanup *back_to, *free_cu_cleanup;
13678
13679   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13680   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13681
13682   gdb_assert (type_sig->per_cu.cu == NULL);
13683
13684   cu = xmalloc (sizeof (*cu));
13685   init_one_comp_unit (cu, objfile);
13686
13687   type_sig->per_cu.cu = cu;
13688   cu->per_cu = &type_sig->per_cu;
13689
13690   /* If an error occurs while loading, release our storage.  */
13691   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13692
13693   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13694                                         types_ptr, objfile->obfd);
13695   gdb_assert (signature == type_sig->signature);
13696
13697   cu->die_hash
13698     = htab_create_alloc_ex (cu->header.length / 12,
13699                             die_hash,
13700                             die_eq,
13701                             NULL,
13702                             &cu->comp_unit_obstack,
13703                             hashtab_obstack_allocate,
13704                             dummy_obstack_deallocate);
13705
13706   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13707   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13708
13709   init_cu_die_reader (&reader_specs, cu);
13710
13711   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13712                                     NULL /*parent*/);
13713
13714   /* We try not to read any attributes in this function, because not
13715      all objfiles needed for references have been loaded yet, and symbol
13716      table processing isn't initialized.  But we have to set the CU language,
13717      or we won't be able to build types correctly.  */
13718   prepare_one_comp_unit (cu, cu->dies);
13719
13720   do_cleanups (back_to);
13721
13722   /* We've successfully allocated this compilation unit.  Let our caller
13723      clean it up when finished with it.  */
13724   discard_cleanups (free_cu_cleanup);
13725
13726   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13727   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13728 }
13729
13730 /* Decode simple location descriptions.
13731    Given a pointer to a dwarf block that defines a location, compute
13732    the location and return the value.
13733
13734    NOTE drow/2003-11-18: This function is called in two situations
13735    now: for the address of static or global variables (partial symbols
13736    only) and for offsets into structures which are expected to be
13737    (more or less) constant.  The partial symbol case should go away,
13738    and only the constant case should remain.  That will let this
13739    function complain more accurately.  A few special modes are allowed
13740    without complaint for global variables (for instance, global
13741    register values and thread-local values).
13742
13743    A location description containing no operations indicates that the
13744    object is optimized out.  The return value is 0 for that case.
13745    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13746    callers will only want a very basic result and this can become a
13747    complaint.
13748
13749    Note that stack[0] is unused except as a default error return.  */
13750
13751 static CORE_ADDR
13752 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13753 {
13754   struct objfile *objfile = cu->objfile;
13755   int i;
13756   int size = blk->size;
13757   gdb_byte *data = blk->data;
13758   CORE_ADDR stack[64];
13759   int stacki;
13760   unsigned int bytes_read, unsnd;
13761   gdb_byte op;
13762
13763   i = 0;
13764   stacki = 0;
13765   stack[stacki] = 0;
13766   stack[++stacki] = 0;
13767
13768   while (i < size)
13769     {
13770       op = data[i++];
13771       switch (op)
13772         {
13773         case DW_OP_lit0:
13774         case DW_OP_lit1:
13775         case DW_OP_lit2:
13776         case DW_OP_lit3:
13777         case DW_OP_lit4:
13778         case DW_OP_lit5:
13779         case DW_OP_lit6:
13780         case DW_OP_lit7:
13781         case DW_OP_lit8:
13782         case DW_OP_lit9:
13783         case DW_OP_lit10:
13784         case DW_OP_lit11:
13785         case DW_OP_lit12:
13786         case DW_OP_lit13:
13787         case DW_OP_lit14:
13788         case DW_OP_lit15:
13789         case DW_OP_lit16:
13790         case DW_OP_lit17:
13791         case DW_OP_lit18:
13792         case DW_OP_lit19:
13793         case DW_OP_lit20:
13794         case DW_OP_lit21:
13795         case DW_OP_lit22:
13796         case DW_OP_lit23:
13797         case DW_OP_lit24:
13798         case DW_OP_lit25:
13799         case DW_OP_lit26:
13800         case DW_OP_lit27:
13801         case DW_OP_lit28:
13802         case DW_OP_lit29:
13803         case DW_OP_lit30:
13804         case DW_OP_lit31:
13805           stack[++stacki] = op - DW_OP_lit0;
13806           break;
13807
13808         case DW_OP_reg0:
13809         case DW_OP_reg1:
13810         case DW_OP_reg2:
13811         case DW_OP_reg3:
13812         case DW_OP_reg4:
13813         case DW_OP_reg5:
13814         case DW_OP_reg6:
13815         case DW_OP_reg7:
13816         case DW_OP_reg8:
13817         case DW_OP_reg9:
13818         case DW_OP_reg10:
13819         case DW_OP_reg11:
13820         case DW_OP_reg12:
13821         case DW_OP_reg13:
13822         case DW_OP_reg14:
13823         case DW_OP_reg15:
13824         case DW_OP_reg16:
13825         case DW_OP_reg17:
13826         case DW_OP_reg18:
13827         case DW_OP_reg19:
13828         case DW_OP_reg20:
13829         case DW_OP_reg21:
13830         case DW_OP_reg22:
13831         case DW_OP_reg23:
13832         case DW_OP_reg24:
13833         case DW_OP_reg25:
13834         case DW_OP_reg26:
13835         case DW_OP_reg27:
13836         case DW_OP_reg28:
13837         case DW_OP_reg29:
13838         case DW_OP_reg30:
13839         case DW_OP_reg31:
13840           stack[++stacki] = op - DW_OP_reg0;
13841           if (i < size)
13842             dwarf2_complex_location_expr_complaint ();
13843           break;
13844
13845         case DW_OP_regx:
13846           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13847           i += bytes_read;
13848           stack[++stacki] = unsnd;
13849           if (i < size)
13850             dwarf2_complex_location_expr_complaint ();
13851           break;
13852
13853         case DW_OP_addr:
13854           stack[++stacki] = read_address (objfile->obfd, &data[i],
13855                                           cu, &bytes_read);
13856           i += bytes_read;
13857           break;
13858
13859         case DW_OP_const1u:
13860           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13861           i += 1;
13862           break;
13863
13864         case DW_OP_const1s:
13865           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13866           i += 1;
13867           break;
13868
13869         case DW_OP_const2u:
13870           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13871           i += 2;
13872           break;
13873
13874         case DW_OP_const2s:
13875           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13876           i += 2;
13877           break;
13878
13879         case DW_OP_const4u:
13880           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13881           i += 4;
13882           break;
13883
13884         case DW_OP_const4s:
13885           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13886           i += 4;
13887           break;
13888
13889         case DW_OP_constu:
13890           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13891                                                   &bytes_read);
13892           i += bytes_read;
13893           break;
13894
13895         case DW_OP_consts:
13896           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13897           i += bytes_read;
13898           break;
13899
13900         case DW_OP_dup:
13901           stack[stacki + 1] = stack[stacki];
13902           stacki++;
13903           break;
13904
13905         case DW_OP_plus:
13906           stack[stacki - 1] += stack[stacki];
13907           stacki--;
13908           break;
13909
13910         case DW_OP_plus_uconst:
13911           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13912                                                  &bytes_read);
13913           i += bytes_read;
13914           break;
13915
13916         case DW_OP_minus:
13917           stack[stacki - 1] -= stack[stacki];
13918           stacki--;
13919           break;
13920
13921         case DW_OP_deref:
13922           /* If we're not the last op, then we definitely can't encode
13923              this using GDB's address_class enum.  This is valid for partial
13924              global symbols, although the variable's address will be bogus
13925              in the psymtab.  */
13926           if (i < size)
13927             dwarf2_complex_location_expr_complaint ();
13928           break;
13929
13930         case DW_OP_GNU_push_tls_address:
13931           /* The top of the stack has the offset from the beginning
13932              of the thread control block at which the variable is located.  */
13933           /* Nothing should follow this operator, so the top of stack would
13934              be returned.  */
13935           /* This is valid for partial global symbols, but the variable's
13936              address will be bogus in the psymtab.  */
13937           if (i < size)
13938             dwarf2_complex_location_expr_complaint ();
13939           break;
13940
13941         case DW_OP_GNU_uninit:
13942           break;
13943
13944         default:
13945           {
13946             const char *name = dwarf_stack_op_name (op);
13947
13948             if (name)
13949               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13950                          name);
13951             else
13952               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13953                          op);
13954           }
13955
13956           return (stack[stacki]);
13957         }
13958
13959       /* Enforce maximum stack depth of SIZE-1 to avoid writing
13960          outside of the allocated space.  Also enforce minimum>0.  */
13961       if (stacki >= ARRAY_SIZE (stack) - 1)
13962         {
13963           complaint (&symfile_complaints,
13964                      _("location description stack overflow"));
13965           return 0;
13966         }
13967
13968       if (stacki <= 0)
13969         {
13970           complaint (&symfile_complaints,
13971                      _("location description stack underflow"));
13972           return 0;
13973         }
13974     }
13975   return (stack[stacki]);
13976 }
13977
13978 /* memory allocation interface */
13979
13980 static struct dwarf_block *
13981 dwarf_alloc_block (struct dwarf2_cu *cu)
13982 {
13983   struct dwarf_block *blk;
13984
13985   blk = (struct dwarf_block *)
13986     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13987   return (blk);
13988 }
13989
13990 static struct abbrev_info *
13991 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13992 {
13993   struct abbrev_info *abbrev;
13994
13995   abbrev = (struct abbrev_info *)
13996     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13997   memset (abbrev, 0, sizeof (struct abbrev_info));
13998   return (abbrev);
13999 }
14000
14001 static struct die_info *
14002 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14003 {
14004   struct die_info *die;
14005   size_t size = sizeof (struct die_info);
14006
14007   if (num_attrs > 1)
14008     size += (num_attrs - 1) * sizeof (struct attribute);
14009
14010   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14011   memset (die, 0, sizeof (struct die_info));
14012   return (die);
14013 }
14014
14015 \f
14016 /* Macro support.  */
14017
14018 /* Return the full name of file number I in *LH's file name table.
14019    Use COMP_DIR as the name of the current directory of the
14020    compilation.  The result is allocated using xmalloc; the caller is
14021    responsible for freeing it.  */
14022 static char *
14023 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14024 {
14025   /* Is the file number a valid index into the line header's file name
14026      table?  Remember that file numbers start with one, not zero.  */
14027   if (1 <= file && file <= lh->num_file_names)
14028     {
14029       struct file_entry *fe = &lh->file_names[file - 1];
14030
14031       if (IS_ABSOLUTE_PATH (fe->name))
14032         return xstrdup (fe->name);
14033       else
14034         {
14035           const char *dir;
14036           int dir_len;
14037           char *full_name;
14038
14039           if (fe->dir_index)
14040             dir = lh->include_dirs[fe->dir_index - 1];
14041           else
14042             dir = comp_dir;
14043
14044           if (dir)
14045             {
14046               dir_len = strlen (dir);
14047               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14048               strcpy (full_name, dir);
14049               full_name[dir_len] = '/';
14050               strcpy (full_name + dir_len + 1, fe->name);
14051               return full_name;
14052             }
14053           else
14054             return xstrdup (fe->name);
14055         }
14056     }
14057   else
14058     {
14059       /* The compiler produced a bogus file number.  We can at least
14060          record the macro definitions made in the file, even if we
14061          won't be able to find the file by name.  */
14062       char fake_name[80];
14063
14064       sprintf (fake_name, "<bad macro file number %d>", file);
14065
14066       complaint (&symfile_complaints,
14067                  _("bad file number in macro information (%d)"),
14068                  file);
14069
14070       return xstrdup (fake_name);
14071     }
14072 }
14073
14074
14075 static struct macro_source_file *
14076 macro_start_file (int file, int line,
14077                   struct macro_source_file *current_file,
14078                   const char *comp_dir,
14079                   struct line_header *lh, struct objfile *objfile)
14080 {
14081   /* The full name of this source file.  */
14082   char *full_name = file_full_name (file, lh, comp_dir);
14083
14084   /* We don't create a macro table for this compilation unit
14085      at all until we actually get a filename.  */
14086   if (! pending_macros)
14087     pending_macros = new_macro_table (&objfile->objfile_obstack,
14088                                       objfile->macro_cache);
14089
14090   if (! current_file)
14091     /* If we have no current file, then this must be the start_file
14092        directive for the compilation unit's main source file.  */
14093     current_file = macro_set_main (pending_macros, full_name);
14094   else
14095     current_file = macro_include (current_file, line, full_name);
14096
14097   xfree (full_name);
14098
14099   return current_file;
14100 }
14101
14102
14103 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14104    followed by a null byte.  */
14105 static char *
14106 copy_string (const char *buf, int len)
14107 {
14108   char *s = xmalloc (len + 1);
14109
14110   memcpy (s, buf, len);
14111   s[len] = '\0';
14112   return s;
14113 }
14114
14115
14116 static const char *
14117 consume_improper_spaces (const char *p, const char *body)
14118 {
14119   if (*p == ' ')
14120     {
14121       complaint (&symfile_complaints,
14122                  _("macro definition contains spaces "
14123                    "in formal argument list:\n`%s'"),
14124                  body);
14125
14126       while (*p == ' ')
14127         p++;
14128     }
14129
14130   return p;
14131 }
14132
14133
14134 static void
14135 parse_macro_definition (struct macro_source_file *file, int line,
14136                         const char *body)
14137 {
14138   const char *p;
14139
14140   /* The body string takes one of two forms.  For object-like macro
14141      definitions, it should be:
14142
14143         <macro name> " " <definition>
14144
14145      For function-like macro definitions, it should be:
14146
14147         <macro name> "() " <definition>
14148      or
14149         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14150
14151      Spaces may appear only where explicitly indicated, and in the
14152      <definition>.
14153
14154      The Dwarf 2 spec says that an object-like macro's name is always
14155      followed by a space, but versions of GCC around March 2002 omit
14156      the space when the macro's definition is the empty string.
14157
14158      The Dwarf 2 spec says that there should be no spaces between the
14159      formal arguments in a function-like macro's formal argument list,
14160      but versions of GCC around March 2002 include spaces after the
14161      commas.  */
14162
14163
14164   /* Find the extent of the macro name.  The macro name is terminated
14165      by either a space or null character (for an object-like macro) or
14166      an opening paren (for a function-like macro).  */
14167   for (p = body; *p; p++)
14168     if (*p == ' ' || *p == '(')
14169       break;
14170
14171   if (*p == ' ' || *p == '\0')
14172     {
14173       /* It's an object-like macro.  */
14174       int name_len = p - body;
14175       char *name = copy_string (body, name_len);
14176       const char *replacement;
14177
14178       if (*p == ' ')
14179         replacement = body + name_len + 1;
14180       else
14181         {
14182           dwarf2_macro_malformed_definition_complaint (body);
14183           replacement = body + name_len;
14184         }
14185
14186       macro_define_object (file, line, name, replacement);
14187
14188       xfree (name);
14189     }
14190   else if (*p == '(')
14191     {
14192       /* It's a function-like macro.  */
14193       char *name = copy_string (body, p - body);
14194       int argc = 0;
14195       int argv_size = 1;
14196       char **argv = xmalloc (argv_size * sizeof (*argv));
14197
14198       p++;
14199
14200       p = consume_improper_spaces (p, body);
14201
14202       /* Parse the formal argument list.  */
14203       while (*p && *p != ')')
14204         {
14205           /* Find the extent of the current argument name.  */
14206           const char *arg_start = p;
14207
14208           while (*p && *p != ',' && *p != ')' && *p != ' ')
14209             p++;
14210
14211           if (! *p || p == arg_start)
14212             dwarf2_macro_malformed_definition_complaint (body);
14213           else
14214             {
14215               /* Make sure argv has room for the new argument.  */
14216               if (argc >= argv_size)
14217                 {
14218                   argv_size *= 2;
14219                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14220                 }
14221
14222               argv[argc++] = copy_string (arg_start, p - arg_start);
14223             }
14224
14225           p = consume_improper_spaces (p, body);
14226
14227           /* Consume the comma, if present.  */
14228           if (*p == ',')
14229             {
14230               p++;
14231
14232               p = consume_improper_spaces (p, body);
14233             }
14234         }
14235
14236       if (*p == ')')
14237         {
14238           p++;
14239
14240           if (*p == ' ')
14241             /* Perfectly formed definition, no complaints.  */
14242             macro_define_function (file, line, name,
14243                                    argc, (const char **) argv,
14244                                    p + 1);
14245           else if (*p == '\0')
14246             {
14247               /* Complain, but do define it.  */
14248               dwarf2_macro_malformed_definition_complaint (body);
14249               macro_define_function (file, line, name,
14250                                      argc, (const char **) argv,
14251                                      p);
14252             }
14253           else
14254             /* Just complain.  */
14255             dwarf2_macro_malformed_definition_complaint (body);
14256         }
14257       else
14258         /* Just complain.  */
14259         dwarf2_macro_malformed_definition_complaint (body);
14260
14261       xfree (name);
14262       {
14263         int i;
14264
14265         for (i = 0; i < argc; i++)
14266           xfree (argv[i]);
14267       }
14268       xfree (argv);
14269     }
14270   else
14271     dwarf2_macro_malformed_definition_complaint (body);
14272 }
14273
14274
14275 static void
14276 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14277                      char *comp_dir, bfd *abfd,
14278                      struct dwarf2_cu *cu)
14279 {
14280   gdb_byte *mac_ptr, *mac_end;
14281   struct macro_source_file *current_file = 0;
14282   enum dwarf_macinfo_record_type macinfo_type;
14283   int at_commandline;
14284
14285   dwarf2_read_section (dwarf2_per_objfile->objfile,
14286                        &dwarf2_per_objfile->macinfo);
14287   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14288     {
14289       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14290       return;
14291     }
14292
14293   /* First pass: Find the name of the base filename.
14294      This filename is needed in order to process all macros whose definition
14295      (or undefinition) comes from the command line.  These macros are defined
14296      before the first DW_MACINFO_start_file entry, and yet still need to be
14297      associated to the base file.
14298
14299      To determine the base file name, we scan the macro definitions until we
14300      reach the first DW_MACINFO_start_file entry.  We then initialize
14301      CURRENT_FILE accordingly so that any macro definition found before the
14302      first DW_MACINFO_start_file can still be associated to the base file.  */
14303
14304   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14305   mac_end = dwarf2_per_objfile->macinfo.buffer
14306     + dwarf2_per_objfile->macinfo.size;
14307
14308   do
14309     {
14310       /* Do we at least have room for a macinfo type byte?  */
14311       if (mac_ptr >= mac_end)
14312         {
14313           /* Complaint is printed during the second pass as GDB will probably
14314              stop the first pass earlier upon finding
14315              DW_MACINFO_start_file.  */
14316           break;
14317         }
14318
14319       macinfo_type = read_1_byte (abfd, mac_ptr);
14320       mac_ptr++;
14321
14322       switch (macinfo_type)
14323         {
14324           /* A zero macinfo type indicates the end of the macro
14325              information.  */
14326         case 0:
14327           break;
14328
14329         case DW_MACINFO_define:
14330         case DW_MACINFO_undef:
14331           /* Only skip the data by MAC_PTR.  */
14332           {
14333             unsigned int bytes_read;
14334
14335             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14336             mac_ptr += bytes_read;
14337             read_direct_string (abfd, mac_ptr, &bytes_read);
14338             mac_ptr += bytes_read;
14339           }
14340           break;
14341
14342         case DW_MACINFO_start_file:
14343           {
14344             unsigned int bytes_read;
14345             int line, file;
14346
14347             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14348             mac_ptr += bytes_read;
14349             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14350             mac_ptr += bytes_read;
14351
14352             current_file = macro_start_file (file, line, current_file,
14353                                              comp_dir, lh, cu->objfile);
14354           }
14355           break;
14356
14357         case DW_MACINFO_end_file:
14358           /* No data to skip by MAC_PTR.  */
14359           break;
14360
14361         case DW_MACINFO_vendor_ext:
14362           /* Only skip the data by MAC_PTR.  */
14363           {
14364             unsigned int bytes_read;
14365
14366             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14367             mac_ptr += bytes_read;
14368             read_direct_string (abfd, mac_ptr, &bytes_read);
14369             mac_ptr += bytes_read;
14370           }
14371           break;
14372
14373         default:
14374           break;
14375         }
14376     } while (macinfo_type != 0 && current_file == NULL);
14377
14378   /* Second pass: Process all entries.
14379
14380      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14381      command-line macro definitions/undefinitions.  This flag is unset when we
14382      reach the first DW_MACINFO_start_file entry.  */
14383
14384   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14385
14386   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14387      GDB is still reading the definitions from command line.  First
14388      DW_MACINFO_start_file will need to be ignored as it was already executed
14389      to create CURRENT_FILE for the main source holding also the command line
14390      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14391      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14392
14393   at_commandline = 1;
14394
14395   do
14396     {
14397       /* Do we at least have room for a macinfo type byte?  */
14398       if (mac_ptr >= mac_end)
14399         {
14400           dwarf2_macros_too_long_complaint ();
14401           break;
14402         }
14403
14404       macinfo_type = read_1_byte (abfd, mac_ptr);
14405       mac_ptr++;
14406
14407       switch (macinfo_type)
14408         {
14409           /* A zero macinfo type indicates the end of the macro
14410              information.  */
14411         case 0:
14412           break;
14413
14414         case DW_MACINFO_define:
14415         case DW_MACINFO_undef:
14416           {
14417             unsigned int bytes_read;
14418             int line;
14419             char *body;
14420
14421             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14422             mac_ptr += bytes_read;
14423             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14424             mac_ptr += bytes_read;
14425
14426             if (! current_file)
14427               {
14428                 /* DWARF violation as no main source is present.  */
14429                 complaint (&symfile_complaints,
14430                            _("debug info with no main source gives macro %s "
14431                              "on line %d: %s"),
14432                            macinfo_type == DW_MACINFO_define ?
14433                              _("definition") :
14434                                macinfo_type == DW_MACINFO_undef ?
14435                                  _("undefinition") :
14436                                  _("something-or-other"), line, body);
14437                 break;
14438               }
14439             if ((line == 0 && !at_commandline)
14440                 || (line != 0 && at_commandline))
14441               complaint (&symfile_complaints,
14442                          _("debug info gives %s macro %s with %s line %d: %s"),
14443                          at_commandline ? _("command-line") : _("in-file"),
14444                          macinfo_type == DW_MACINFO_define ?
14445                            _("definition") :
14446                              macinfo_type == DW_MACINFO_undef ?
14447                                _("undefinition") :
14448                                _("something-or-other"),
14449                          line == 0 ? _("zero") : _("non-zero"), line, body);
14450
14451             if (macinfo_type == DW_MACINFO_define)
14452               parse_macro_definition (current_file, line, body);
14453             else if (macinfo_type == DW_MACINFO_undef)
14454               macro_undef (current_file, line, body);
14455           }
14456           break;
14457
14458         case DW_MACINFO_start_file:
14459           {
14460             unsigned int bytes_read;
14461             int line, file;
14462
14463             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14464             mac_ptr += bytes_read;
14465             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14466             mac_ptr += bytes_read;
14467
14468             if ((line == 0 && !at_commandline)
14469                 || (line != 0 && at_commandline))
14470               complaint (&symfile_complaints,
14471                          _("debug info gives source %d included "
14472                            "from %s at %s line %d"),
14473                          file, at_commandline ? _("command-line") : _("file"),
14474                          line == 0 ? _("zero") : _("non-zero"), line);
14475
14476             if (at_commandline)
14477               {
14478                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14479                 at_commandline = 0;
14480               }
14481             else
14482               current_file = macro_start_file (file, line,
14483                                                current_file, comp_dir,
14484                                                lh, cu->objfile);
14485           }
14486           break;
14487
14488         case DW_MACINFO_end_file:
14489           if (! current_file)
14490             complaint (&symfile_complaints,
14491                        _("macro debug info has an unmatched "
14492                          "`close_file' directive"));
14493           else
14494             {
14495               current_file = current_file->included_by;
14496               if (! current_file)
14497                 {
14498                   enum dwarf_macinfo_record_type next_type;
14499
14500                   /* GCC circa March 2002 doesn't produce the zero
14501                      type byte marking the end of the compilation
14502                      unit.  Complain if it's not there, but exit no
14503                      matter what.  */
14504
14505                   /* Do we at least have room for a macinfo type byte?  */
14506                   if (mac_ptr >= mac_end)
14507                     {
14508                       dwarf2_macros_too_long_complaint ();
14509                       return;
14510                     }
14511
14512                   /* We don't increment mac_ptr here, so this is just
14513                      a look-ahead.  */
14514                   next_type = read_1_byte (abfd, mac_ptr);
14515                   if (next_type != 0)
14516                     complaint (&symfile_complaints,
14517                                _("no terminating 0-type entry for "
14518                                  "macros in `.debug_macinfo' section"));
14519
14520                   return;
14521                 }
14522             }
14523           break;
14524
14525         case DW_MACINFO_vendor_ext:
14526           {
14527             unsigned int bytes_read;
14528             int constant;
14529
14530             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14531             mac_ptr += bytes_read;
14532             read_direct_string (abfd, mac_ptr, &bytes_read);
14533             mac_ptr += bytes_read;
14534
14535             /* We don't recognize any vendor extensions.  */
14536           }
14537           break;
14538         }
14539     } while (macinfo_type != 0);
14540 }
14541
14542 /* Check if the attribute's form is a DW_FORM_block*
14543    if so return true else false.  */
14544 static int
14545 attr_form_is_block (struct attribute *attr)
14546 {
14547   return (attr == NULL ? 0 :
14548       attr->form == DW_FORM_block1
14549       || attr->form == DW_FORM_block2
14550       || attr->form == DW_FORM_block4
14551       || attr->form == DW_FORM_block
14552       || attr->form == DW_FORM_exprloc);
14553 }
14554
14555 /* Return non-zero if ATTR's value is a section offset --- classes
14556    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14557    You may use DW_UNSND (attr) to retrieve such offsets.
14558
14559    Section 7.5.4, "Attribute Encodings", explains that no attribute
14560    may have a value that belongs to more than one of these classes; it
14561    would be ambiguous if we did, because we use the same forms for all
14562    of them.  */
14563 static int
14564 attr_form_is_section_offset (struct attribute *attr)
14565 {
14566   return (attr->form == DW_FORM_data4
14567           || attr->form == DW_FORM_data8
14568           || attr->form == DW_FORM_sec_offset);
14569 }
14570
14571
14572 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14573    zero otherwise.  When this function returns true, you can apply
14574    dwarf2_get_attr_constant_value to it.
14575
14576    However, note that for some attributes you must check
14577    attr_form_is_section_offset before using this test.  DW_FORM_data4
14578    and DW_FORM_data8 are members of both the constant class, and of
14579    the classes that contain offsets into other debug sections
14580    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14581    that, if an attribute's can be either a constant or one of the
14582    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14583    taken as section offsets, not constants.  */
14584 static int
14585 attr_form_is_constant (struct attribute *attr)
14586 {
14587   switch (attr->form)
14588     {
14589     case DW_FORM_sdata:
14590     case DW_FORM_udata:
14591     case DW_FORM_data1:
14592     case DW_FORM_data2:
14593     case DW_FORM_data4:
14594     case DW_FORM_data8:
14595       return 1;
14596     default:
14597       return 0;
14598     }
14599 }
14600
14601 /* A helper function that fills in a dwarf2_loclist_baton.  */
14602
14603 static void
14604 fill_in_loclist_baton (struct dwarf2_cu *cu,
14605                        struct dwarf2_loclist_baton *baton,
14606                        struct attribute *attr)
14607 {
14608   dwarf2_read_section (dwarf2_per_objfile->objfile,
14609                        &dwarf2_per_objfile->loc);
14610
14611   baton->per_cu = cu->per_cu;
14612   gdb_assert (baton->per_cu);
14613   /* We don't know how long the location list is, but make sure we
14614      don't run off the edge of the section.  */
14615   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14616   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14617   baton->base_address = cu->base_address;
14618 }
14619
14620 static void
14621 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14622                              struct dwarf2_cu *cu)
14623 {
14624   if (attr_form_is_section_offset (attr)
14625       /* ".debug_loc" may not exist at all, or the offset may be outside
14626          the section.  If so, fall through to the complaint in the
14627          other branch.  */
14628       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14629                                                 &dwarf2_per_objfile->loc))
14630     {
14631       struct dwarf2_loclist_baton *baton;
14632
14633       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14634                              sizeof (struct dwarf2_loclist_baton));
14635
14636       fill_in_loclist_baton (cu, baton, attr);
14637
14638       if (cu->base_known == 0)
14639         complaint (&symfile_complaints,
14640                    _("Location list used without "
14641                      "specifying the CU base address."));
14642
14643       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14644       SYMBOL_LOCATION_BATON (sym) = baton;
14645     }
14646   else
14647     {
14648       struct dwarf2_locexpr_baton *baton;
14649
14650       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14651                              sizeof (struct dwarf2_locexpr_baton));
14652       baton->per_cu = cu->per_cu;
14653       gdb_assert (baton->per_cu);
14654
14655       if (attr_form_is_block (attr))
14656         {
14657           /* Note that we're just copying the block's data pointer
14658              here, not the actual data.  We're still pointing into the
14659              info_buffer for SYM's objfile; right now we never release
14660              that buffer, but when we do clean up properly this may
14661              need to change.  */
14662           baton->size = DW_BLOCK (attr)->size;
14663           baton->data = DW_BLOCK (attr)->data;
14664         }
14665       else
14666         {
14667           dwarf2_invalid_attrib_class_complaint ("location description",
14668                                                  SYMBOL_NATURAL_NAME (sym));
14669           baton->size = 0;
14670           baton->data = NULL;
14671         }
14672
14673       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14674       SYMBOL_LOCATION_BATON (sym) = baton;
14675     }
14676 }
14677
14678 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14679    came from a separate debuginfo file, then the master objfile is
14680    returned.  */
14681
14682 struct objfile *
14683 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14684 {
14685   struct objfile *objfile = per_cu->objfile;
14686
14687   /* Return the master objfile, so that we can report and look up the
14688      correct file containing this variable.  */
14689   if (objfile->separate_debug_objfile_backlink)
14690     objfile = objfile->separate_debug_objfile_backlink;
14691
14692   return objfile;
14693 }
14694
14695 /* Return the address size given in the compilation unit header for CU.  */
14696
14697 CORE_ADDR
14698 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14699 {
14700   if (per_cu->cu)
14701     return per_cu->cu->header.addr_size;
14702   else
14703     {
14704       /* If the CU is not currently read in, we re-read its header.  */
14705       struct objfile *objfile = per_cu->objfile;
14706       struct dwarf2_per_objfile *per_objfile
14707         = objfile_data (objfile, dwarf2_objfile_data_key);
14708       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14709       struct comp_unit_head cu_header;
14710
14711       memset (&cu_header, 0, sizeof cu_header);
14712       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14713       return cu_header.addr_size;
14714     }
14715 }
14716
14717 /* Return the offset size given in the compilation unit header for CU.  */
14718
14719 int
14720 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14721 {
14722   if (per_cu->cu)
14723     return per_cu->cu->header.offset_size;
14724   else
14725     {
14726       /* If the CU is not currently read in, we re-read its header.  */
14727       struct objfile *objfile = per_cu->objfile;
14728       struct dwarf2_per_objfile *per_objfile
14729         = objfile_data (objfile, dwarf2_objfile_data_key);
14730       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14731       struct comp_unit_head cu_header;
14732
14733       memset (&cu_header, 0, sizeof cu_header);
14734       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14735       return cu_header.offset_size;
14736     }
14737 }
14738
14739 /* Return the text offset of the CU.  The returned offset comes from
14740    this CU's objfile.  If this objfile came from a separate debuginfo
14741    file, then the offset may be different from the corresponding
14742    offset in the parent objfile.  */
14743
14744 CORE_ADDR
14745 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14746 {
14747   struct objfile *objfile = per_cu->objfile;
14748
14749   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14750 }
14751
14752 /* Locate the .debug_info compilation unit from CU's objfile which contains
14753    the DIE at OFFSET.  Raises an error on failure.  */
14754
14755 static struct dwarf2_per_cu_data *
14756 dwarf2_find_containing_comp_unit (unsigned int offset,
14757                                   struct objfile *objfile)
14758 {
14759   struct dwarf2_per_cu_data *this_cu;
14760   int low, high;
14761
14762   low = 0;
14763   high = dwarf2_per_objfile->n_comp_units - 1;
14764   while (high > low)
14765     {
14766       int mid = low + (high - low) / 2;
14767
14768       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14769         high = mid;
14770       else
14771         low = mid + 1;
14772     }
14773   gdb_assert (low == high);
14774   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14775     {
14776       if (low == 0)
14777         error (_("Dwarf Error: could not find partial DIE containing "
14778                "offset 0x%lx [in module %s]"),
14779                (long) offset, bfd_get_filename (objfile->obfd));
14780
14781       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14782       return dwarf2_per_objfile->all_comp_units[low-1];
14783     }
14784   else
14785     {
14786       this_cu = dwarf2_per_objfile->all_comp_units[low];
14787       if (low == dwarf2_per_objfile->n_comp_units - 1
14788           && offset >= this_cu->offset + this_cu->length)
14789         error (_("invalid dwarf2 offset %u"), offset);
14790       gdb_assert (offset < this_cu->offset + this_cu->length);
14791       return this_cu;
14792     }
14793 }
14794
14795 /* Locate the compilation unit from OBJFILE which is located at exactly
14796    OFFSET.  Raises an error on failure.  */
14797
14798 static struct dwarf2_per_cu_data *
14799 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14800 {
14801   struct dwarf2_per_cu_data *this_cu;
14802
14803   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14804   if (this_cu->offset != offset)
14805     error (_("no compilation unit with offset %u."), offset);
14806   return this_cu;
14807 }
14808
14809 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14810
14811 static void
14812 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14813 {
14814   memset (cu, 0, sizeof (*cu));
14815   cu->objfile = objfile;
14816   obstack_init (&cu->comp_unit_obstack);
14817 }
14818
14819 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14820
14821 static void
14822 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14823 {
14824   struct attribute *attr;
14825
14826   /* Set the language we're debugging.  */
14827   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14828   if (attr)
14829     set_cu_language (DW_UNSND (attr), cu);
14830   else
14831     {
14832       cu->language = language_minimal;
14833       cu->language_defn = language_def (cu->language);
14834     }
14835 }
14836
14837 /* Release one cached compilation unit, CU.  We unlink it from the tree
14838    of compilation units, but we don't remove it from the read_in_chain;
14839    the caller is responsible for that.
14840    NOTE: DATA is a void * because this function is also used as a
14841    cleanup routine.  */
14842
14843 static void
14844 free_one_comp_unit (void *data)
14845 {
14846   struct dwarf2_cu *cu = data;
14847
14848   if (cu->per_cu != NULL)
14849     cu->per_cu->cu = NULL;
14850   cu->per_cu = NULL;
14851
14852   obstack_free (&cu->comp_unit_obstack, NULL);
14853
14854   xfree (cu);
14855 }
14856
14857 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14858    when we're finished with it.  We can't free the pointer itself, but be
14859    sure to unlink it from the cache.  Also release any associated storage
14860    and perform cache maintenance.
14861
14862    Only used during partial symbol parsing.  */
14863
14864 static void
14865 free_stack_comp_unit (void *data)
14866 {
14867   struct dwarf2_cu *cu = data;
14868
14869   obstack_free (&cu->comp_unit_obstack, NULL);
14870   cu->partial_dies = NULL;
14871
14872   if (cu->per_cu != NULL)
14873     {
14874       /* This compilation unit is on the stack in our caller, so we
14875          should not xfree it.  Just unlink it.  */
14876       cu->per_cu->cu = NULL;
14877       cu->per_cu = NULL;
14878
14879       /* If we had a per-cu pointer, then we may have other compilation
14880          units loaded, so age them now.  */
14881       age_cached_comp_units ();
14882     }
14883 }
14884
14885 /* Free all cached compilation units.  */
14886
14887 static void
14888 free_cached_comp_units (void *data)
14889 {
14890   struct dwarf2_per_cu_data *per_cu, **last_chain;
14891
14892   per_cu = dwarf2_per_objfile->read_in_chain;
14893   last_chain = &dwarf2_per_objfile->read_in_chain;
14894   while (per_cu != NULL)
14895     {
14896       struct dwarf2_per_cu_data *next_cu;
14897
14898       next_cu = per_cu->cu->read_in_chain;
14899
14900       free_one_comp_unit (per_cu->cu);
14901       *last_chain = next_cu;
14902
14903       per_cu = next_cu;
14904     }
14905 }
14906
14907 /* Increase the age counter on each cached compilation unit, and free
14908    any that are too old.  */
14909
14910 static void
14911 age_cached_comp_units (void)
14912 {
14913   struct dwarf2_per_cu_data *per_cu, **last_chain;
14914
14915   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14916   per_cu = dwarf2_per_objfile->read_in_chain;
14917   while (per_cu != NULL)
14918     {
14919       per_cu->cu->last_used ++;
14920       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14921         dwarf2_mark (per_cu->cu);
14922       per_cu = per_cu->cu->read_in_chain;
14923     }
14924
14925   per_cu = dwarf2_per_objfile->read_in_chain;
14926   last_chain = &dwarf2_per_objfile->read_in_chain;
14927   while (per_cu != NULL)
14928     {
14929       struct dwarf2_per_cu_data *next_cu;
14930
14931       next_cu = per_cu->cu->read_in_chain;
14932
14933       if (!per_cu->cu->mark)
14934         {
14935           free_one_comp_unit (per_cu->cu);
14936           *last_chain = next_cu;
14937         }
14938       else
14939         last_chain = &per_cu->cu->read_in_chain;
14940
14941       per_cu = next_cu;
14942     }
14943 }
14944
14945 /* Remove a single compilation unit from the cache.  */
14946
14947 static void
14948 free_one_cached_comp_unit (void *target_cu)
14949 {
14950   struct dwarf2_per_cu_data *per_cu, **last_chain;
14951
14952   per_cu = dwarf2_per_objfile->read_in_chain;
14953   last_chain = &dwarf2_per_objfile->read_in_chain;
14954   while (per_cu != NULL)
14955     {
14956       struct dwarf2_per_cu_data *next_cu;
14957
14958       next_cu = per_cu->cu->read_in_chain;
14959
14960       if (per_cu->cu == target_cu)
14961         {
14962           free_one_comp_unit (per_cu->cu);
14963           *last_chain = next_cu;
14964           break;
14965         }
14966       else
14967         last_chain = &per_cu->cu->read_in_chain;
14968
14969       per_cu = next_cu;
14970     }
14971 }
14972
14973 /* Release all extra memory associated with OBJFILE.  */
14974
14975 void
14976 dwarf2_free_objfile (struct objfile *objfile)
14977 {
14978   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14979
14980   if (dwarf2_per_objfile == NULL)
14981     return;
14982
14983   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14984   free_cached_comp_units (NULL);
14985
14986   if (dwarf2_per_objfile->quick_file_names_table)
14987     htab_delete (dwarf2_per_objfile->quick_file_names_table);
14988
14989   /* Everything else should be on the objfile obstack.  */
14990 }
14991
14992 /* A pair of DIE offset and GDB type pointer.  We store these
14993    in a hash table separate from the DIEs, and preserve them
14994    when the DIEs are flushed out of cache.  */
14995
14996 struct dwarf2_offset_and_type
14997 {
14998   unsigned int offset;
14999   struct type *type;
15000 };
15001
15002 /* Hash function for a dwarf2_offset_and_type.  */
15003
15004 static hashval_t
15005 offset_and_type_hash (const void *item)
15006 {
15007   const struct dwarf2_offset_and_type *ofs = item;
15008
15009   return ofs->offset;
15010 }
15011
15012 /* Equality function for a dwarf2_offset_and_type.  */
15013
15014 static int
15015 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15016 {
15017   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15018   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15019
15020   return ofs_lhs->offset == ofs_rhs->offset;
15021 }
15022
15023 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15024    table if necessary.  For convenience, return TYPE.
15025
15026    The DIEs reading must have careful ordering to:
15027     * Not cause infite loops trying to read in DIEs as a prerequisite for
15028       reading current DIE.
15029     * Not trying to dereference contents of still incompletely read in types
15030       while reading in other DIEs.
15031     * Enable referencing still incompletely read in types just by a pointer to
15032       the type without accessing its fields.
15033
15034    Therefore caller should follow these rules:
15035      * Try to fetch any prerequisite types we may need to build this DIE type
15036        before building the type and calling set_die_type.
15037      * After building type call set_die_type for current DIE as soon as
15038        possible before fetching more types to complete the current type.
15039      * Make the type as complete as possible before fetching more types.  */
15040
15041 static struct type *
15042 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15043 {
15044   struct dwarf2_offset_and_type **slot, ofs;
15045   struct objfile *objfile = cu->objfile;
15046   htab_t *type_hash_ptr;
15047
15048   /* For Ada types, make sure that the gnat-specific data is always
15049      initialized (if not already set).  There are a few types where
15050      we should not be doing so, because the type-specific area is
15051      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15052      where the type-specific area is used to store the floatformat).
15053      But this is not a problem, because the gnat-specific information
15054      is actually not needed for these types.  */
15055   if (need_gnat_info (cu)
15056       && TYPE_CODE (type) != TYPE_CODE_FUNC
15057       && TYPE_CODE (type) != TYPE_CODE_FLT
15058       && !HAVE_GNAT_AUX_INFO (type))
15059     INIT_GNAT_SPECIFIC (type);
15060
15061   if (cu->per_cu->from_debug_types)
15062     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15063   else
15064     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15065
15066   if (*type_hash_ptr == NULL)
15067     {
15068       *type_hash_ptr
15069         = htab_create_alloc_ex (127,
15070                                 offset_and_type_hash,
15071                                 offset_and_type_eq,
15072                                 NULL,
15073                                 &objfile->objfile_obstack,
15074                                 hashtab_obstack_allocate,
15075                                 dummy_obstack_deallocate);
15076     }
15077
15078   ofs.offset = die->offset;
15079   ofs.type = type;
15080   slot = (struct dwarf2_offset_and_type **)
15081     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15082   if (*slot)
15083     complaint (&symfile_complaints,
15084                _("A problem internal to GDB: DIE 0x%x has type already set"),
15085                die->offset);
15086   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15087   **slot = ofs;
15088   return type;
15089 }
15090
15091 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15092    table, or return NULL if the die does not have a saved type.  */
15093
15094 static struct type *
15095 get_die_type_at_offset (unsigned int offset,
15096                         struct dwarf2_per_cu_data *per_cu)
15097 {
15098   struct dwarf2_offset_and_type *slot, ofs;
15099   htab_t type_hash;
15100
15101   if (per_cu->from_debug_types)
15102     type_hash = dwarf2_per_objfile->debug_types_type_hash;
15103   else
15104     type_hash = dwarf2_per_objfile->debug_info_type_hash;
15105   if (type_hash == NULL)
15106     return NULL;
15107
15108   ofs.offset = offset;
15109   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15110   if (slot)
15111     return slot->type;
15112   else
15113     return NULL;
15114 }
15115
15116 /* Look up the type for DIE in the appropriate type_hash table,
15117    or return NULL if DIE does not have a saved type.  */
15118
15119 static struct type *
15120 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15121 {
15122   return get_die_type_at_offset (die->offset, cu->per_cu);
15123 }
15124
15125 /* Add a dependence relationship from CU to REF_PER_CU.  */
15126
15127 static void
15128 dwarf2_add_dependence (struct dwarf2_cu *cu,
15129                        struct dwarf2_per_cu_data *ref_per_cu)
15130 {
15131   void **slot;
15132
15133   if (cu->dependencies == NULL)
15134     cu->dependencies
15135       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15136                               NULL, &cu->comp_unit_obstack,
15137                               hashtab_obstack_allocate,
15138                               dummy_obstack_deallocate);
15139
15140   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15141   if (*slot == NULL)
15142     *slot = ref_per_cu;
15143 }
15144
15145 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15146    Set the mark field in every compilation unit in the
15147    cache that we must keep because we are keeping CU.  */
15148
15149 static int
15150 dwarf2_mark_helper (void **slot, void *data)
15151 {
15152   struct dwarf2_per_cu_data *per_cu;
15153
15154   per_cu = (struct dwarf2_per_cu_data *) *slot;
15155   if (per_cu->cu->mark)
15156     return 1;
15157   per_cu->cu->mark = 1;
15158
15159   if (per_cu->cu->dependencies != NULL)
15160     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15161
15162   return 1;
15163 }
15164
15165 /* Set the mark field in CU and in every other compilation unit in the
15166    cache that we must keep because we are keeping CU.  */
15167
15168 static void
15169 dwarf2_mark (struct dwarf2_cu *cu)
15170 {
15171   if (cu->mark)
15172     return;
15173   cu->mark = 1;
15174   if (cu->dependencies != NULL)
15175     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15176 }
15177
15178 static void
15179 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15180 {
15181   while (per_cu)
15182     {
15183       per_cu->cu->mark = 0;
15184       per_cu = per_cu->cu->read_in_chain;
15185     }
15186 }
15187
15188 /* Trivial hash function for partial_die_info: the hash value of a DIE
15189    is its offset in .debug_info for this objfile.  */
15190
15191 static hashval_t
15192 partial_die_hash (const void *item)
15193 {
15194   const struct partial_die_info *part_die = item;
15195
15196   return part_die->offset;
15197 }
15198
15199 /* Trivial comparison function for partial_die_info structures: two DIEs
15200    are equal if they have the same offset.  */
15201
15202 static int
15203 partial_die_eq (const void *item_lhs, const void *item_rhs)
15204 {
15205   const struct partial_die_info *part_die_lhs = item_lhs;
15206   const struct partial_die_info *part_die_rhs = item_rhs;
15207
15208   return part_die_lhs->offset == part_die_rhs->offset;
15209 }
15210
15211 static struct cmd_list_element *set_dwarf2_cmdlist;
15212 static struct cmd_list_element *show_dwarf2_cmdlist;
15213
15214 static void
15215 set_dwarf2_cmd (char *args, int from_tty)
15216 {
15217   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15218 }
15219
15220 static void
15221 show_dwarf2_cmd (char *args, int from_tty)
15222 {
15223   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15224 }
15225
15226 /* If section described by INFO was mmapped, munmap it now.  */
15227
15228 static void
15229 munmap_section_buffer (struct dwarf2_section_info *info)
15230 {
15231   if (info->was_mmapped)
15232     {
15233 #ifdef HAVE_MMAP
15234       intptr_t begin = (intptr_t) info->buffer;
15235       intptr_t map_begin = begin & ~(pagesize - 1);
15236       size_t map_length = info->size + begin - map_begin;
15237
15238       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15239 #else
15240       /* Without HAVE_MMAP, we should never be here to begin with.  */
15241       gdb_assert_not_reached ("no mmap support");
15242 #endif
15243     }
15244 }
15245
15246 /* munmap debug sections for OBJFILE, if necessary.  */
15247
15248 static void
15249 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15250 {
15251   struct dwarf2_per_objfile *data = d;
15252
15253   /* This is sorted according to the order they're defined in to make it easier
15254      to keep in sync.  */
15255   munmap_section_buffer (&data->info);
15256   munmap_section_buffer (&data->abbrev);
15257   munmap_section_buffer (&data->line);
15258   munmap_section_buffer (&data->loc);
15259   munmap_section_buffer (&data->macinfo);
15260   munmap_section_buffer (&data->str);
15261   munmap_section_buffer (&data->ranges);
15262   munmap_section_buffer (&data->types);
15263   munmap_section_buffer (&data->frame);
15264   munmap_section_buffer (&data->eh_frame);
15265   munmap_section_buffer (&data->gdb_index);
15266 }
15267
15268 \f
15269 /* The "save gdb-index" command.  */
15270
15271 /* The contents of the hash table we create when building the string
15272    table.  */
15273 struct strtab_entry
15274 {
15275   offset_type offset;
15276   const char *str;
15277 };
15278
15279 /* Hash function for a strtab_entry.
15280
15281    Function is used only during write_hash_table so no index format backward
15282    compatibility is needed.  */
15283
15284 static hashval_t
15285 hash_strtab_entry (const void *e)
15286 {
15287   const struct strtab_entry *entry = e;
15288   return mapped_index_string_hash (INT_MAX, entry->str);
15289 }
15290
15291 /* Equality function for a strtab_entry.  */
15292
15293 static int
15294 eq_strtab_entry (const void *a, const void *b)
15295 {
15296   const struct strtab_entry *ea = a;
15297   const struct strtab_entry *eb = b;
15298   return !strcmp (ea->str, eb->str);
15299 }
15300
15301 /* Create a strtab_entry hash table.  */
15302
15303 static htab_t
15304 create_strtab (void)
15305 {
15306   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15307                             xfree, xcalloc, xfree);
15308 }
15309
15310 /* Add a string to the constant pool.  Return the string's offset in
15311    host order.  */
15312
15313 static offset_type
15314 add_string (htab_t table, struct obstack *cpool, const char *str)
15315 {
15316   void **slot;
15317   struct strtab_entry entry;
15318   struct strtab_entry *result;
15319
15320   entry.str = str;
15321   slot = htab_find_slot (table, &entry, INSERT);
15322   if (*slot)
15323     result = *slot;
15324   else
15325     {
15326       result = XNEW (struct strtab_entry);
15327       result->offset = obstack_object_size (cpool);
15328       result->str = str;
15329       obstack_grow_str0 (cpool, str);
15330       *slot = result;
15331     }
15332   return result->offset;
15333 }
15334
15335 /* An entry in the symbol table.  */
15336 struct symtab_index_entry
15337 {
15338   /* The name of the symbol.  */
15339   const char *name;
15340   /* The offset of the name in the constant pool.  */
15341   offset_type index_offset;
15342   /* A sorted vector of the indices of all the CUs that hold an object
15343      of this name.  */
15344   VEC (offset_type) *cu_indices;
15345 };
15346
15347 /* The symbol table.  This is a power-of-2-sized hash table.  */
15348 struct mapped_symtab
15349 {
15350   offset_type n_elements;
15351   offset_type size;
15352   struct symtab_index_entry **data;
15353 };
15354
15355 /* Hash function for a symtab_index_entry.  */
15356
15357 static hashval_t
15358 hash_symtab_entry (const void *e)
15359 {
15360   const struct symtab_index_entry *entry = e;
15361   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15362                          sizeof (offset_type) * VEC_length (offset_type,
15363                                                             entry->cu_indices),
15364                          0);
15365 }
15366
15367 /* Equality function for a symtab_index_entry.  */
15368
15369 static int
15370 eq_symtab_entry (const void *a, const void *b)
15371 {
15372   const struct symtab_index_entry *ea = a;
15373   const struct symtab_index_entry *eb = b;
15374   int len = VEC_length (offset_type, ea->cu_indices);
15375   if (len != VEC_length (offset_type, eb->cu_indices))
15376     return 0;
15377   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15378                   VEC_address (offset_type, eb->cu_indices),
15379                   sizeof (offset_type) * len);
15380 }
15381
15382 /* Destroy a symtab_index_entry.  */
15383
15384 static void
15385 delete_symtab_entry (void *p)
15386 {
15387   struct symtab_index_entry *entry = p;
15388   VEC_free (offset_type, entry->cu_indices);
15389   xfree (entry);
15390 }
15391
15392 /* Create a hash table holding symtab_index_entry objects.  */
15393
15394 static htab_t
15395 create_symbol_hash_table (void)
15396 {
15397   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15398                             delete_symtab_entry, xcalloc, xfree);
15399 }
15400
15401 /* Create a new mapped symtab object.  */
15402
15403 static struct mapped_symtab *
15404 create_mapped_symtab (void)
15405 {
15406   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15407   symtab->n_elements = 0;
15408   symtab->size = 1024;
15409   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15410   return symtab;
15411 }
15412
15413 /* Destroy a mapped_symtab.  */
15414
15415 static void
15416 cleanup_mapped_symtab (void *p)
15417 {
15418   struct mapped_symtab *symtab = p;
15419   /* The contents of the array are freed when the other hash table is
15420      destroyed.  */
15421   xfree (symtab->data);
15422   xfree (symtab);
15423 }
15424
15425 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15426    the slot.
15427    
15428    Function is used only during write_hash_table so no index format backward
15429    compatibility is needed.  */
15430
15431 static struct symtab_index_entry **
15432 find_slot (struct mapped_symtab *symtab, const char *name)
15433 {
15434   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15435
15436   index = hash & (symtab->size - 1);
15437   step = ((hash * 17) & (symtab->size - 1)) | 1;
15438
15439   for (;;)
15440     {
15441       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15442         return &symtab->data[index];
15443       index = (index + step) & (symtab->size - 1);
15444     }
15445 }
15446
15447 /* Expand SYMTAB's hash table.  */
15448
15449 static void
15450 hash_expand (struct mapped_symtab *symtab)
15451 {
15452   offset_type old_size = symtab->size;
15453   offset_type i;
15454   struct symtab_index_entry **old_entries = symtab->data;
15455
15456   symtab->size *= 2;
15457   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15458
15459   for (i = 0; i < old_size; ++i)
15460     {
15461       if (old_entries[i])
15462         {
15463           struct symtab_index_entry **slot = find_slot (symtab,
15464                                                         old_entries[i]->name);
15465           *slot = old_entries[i];
15466         }
15467     }
15468
15469   xfree (old_entries);
15470 }
15471
15472 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15473    is the index of the CU in which the symbol appears.  */
15474
15475 static void
15476 add_index_entry (struct mapped_symtab *symtab, const char *name,
15477                  offset_type cu_index)
15478 {
15479   struct symtab_index_entry **slot;
15480
15481   ++symtab->n_elements;
15482   if (4 * symtab->n_elements / 3 >= symtab->size)
15483     hash_expand (symtab);
15484
15485   slot = find_slot (symtab, name);
15486   if (!*slot)
15487     {
15488       *slot = XNEW (struct symtab_index_entry);
15489       (*slot)->name = name;
15490       (*slot)->cu_indices = NULL;
15491     }
15492   /* Don't push an index twice.  Due to how we add entries we only
15493      have to check the last one.  */ 
15494   if (VEC_empty (offset_type, (*slot)->cu_indices)
15495       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15496     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15497 }
15498
15499 /* Add a vector of indices to the constant pool.  */
15500
15501 static offset_type
15502 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15503                       struct symtab_index_entry *entry)
15504 {
15505   void **slot;
15506
15507   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15508   if (!*slot)
15509     {
15510       offset_type len = VEC_length (offset_type, entry->cu_indices);
15511       offset_type val = MAYBE_SWAP (len);
15512       offset_type iter;
15513       int i;
15514
15515       *slot = entry;
15516       entry->index_offset = obstack_object_size (cpool);
15517
15518       obstack_grow (cpool, &val, sizeof (val));
15519       for (i = 0;
15520            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15521            ++i)
15522         {
15523           val = MAYBE_SWAP (iter);
15524           obstack_grow (cpool, &val, sizeof (val));
15525         }
15526     }
15527   else
15528     {
15529       struct symtab_index_entry *old_entry = *slot;
15530       entry->index_offset = old_entry->index_offset;
15531       entry = old_entry;
15532     }
15533   return entry->index_offset;
15534 }
15535
15536 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15537    constant pool entries going into the obstack CPOOL.  */
15538
15539 static void
15540 write_hash_table (struct mapped_symtab *symtab,
15541                   struct obstack *output, struct obstack *cpool)
15542 {
15543   offset_type i;
15544   htab_t symbol_hash_table;
15545   htab_t str_table;
15546
15547   symbol_hash_table = create_symbol_hash_table ();
15548   str_table = create_strtab ();
15549
15550   /* We add all the index vectors to the constant pool first, to
15551      ensure alignment is ok.  */
15552   for (i = 0; i < symtab->size; ++i)
15553     {
15554       if (symtab->data[i])
15555         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15556     }
15557
15558   /* Now write out the hash table.  */
15559   for (i = 0; i < symtab->size; ++i)
15560     {
15561       offset_type str_off, vec_off;
15562
15563       if (symtab->data[i])
15564         {
15565           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15566           vec_off = symtab->data[i]->index_offset;
15567         }
15568       else
15569         {
15570           /* While 0 is a valid constant pool index, it is not valid
15571              to have 0 for both offsets.  */
15572           str_off = 0;
15573           vec_off = 0;
15574         }
15575
15576       str_off = MAYBE_SWAP (str_off);
15577       vec_off = MAYBE_SWAP (vec_off);
15578
15579       obstack_grow (output, &str_off, sizeof (str_off));
15580       obstack_grow (output, &vec_off, sizeof (vec_off));
15581     }
15582
15583   htab_delete (str_table);
15584   htab_delete (symbol_hash_table);
15585 }
15586
15587 /* Struct to map psymtab to CU index in the index file.  */
15588 struct psymtab_cu_index_map
15589 {
15590   struct partial_symtab *psymtab;
15591   unsigned int cu_index;
15592 };
15593
15594 static hashval_t
15595 hash_psymtab_cu_index (const void *item)
15596 {
15597   const struct psymtab_cu_index_map *map = item;
15598
15599   return htab_hash_pointer (map->psymtab);
15600 }
15601
15602 static int
15603 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15604 {
15605   const struct psymtab_cu_index_map *lhs = item_lhs;
15606   const struct psymtab_cu_index_map *rhs = item_rhs;
15607
15608   return lhs->psymtab == rhs->psymtab;
15609 }
15610
15611 /* Helper struct for building the address table.  */
15612 struct addrmap_index_data
15613 {
15614   struct objfile *objfile;
15615   struct obstack *addr_obstack;
15616   htab_t cu_index_htab;
15617
15618   /* Non-zero if the previous_* fields are valid.
15619      We can't write an entry until we see the next entry (since it is only then
15620      that we know the end of the entry).  */
15621   int previous_valid;
15622   /* Index of the CU in the table of all CUs in the index file.  */
15623   unsigned int previous_cu_index;
15624   /* Start address of the CU.  */
15625   CORE_ADDR previous_cu_start;
15626 };
15627
15628 /* Write an address entry to OBSTACK.  */
15629
15630 static void
15631 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15632                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15633 {
15634   offset_type cu_index_to_write;
15635   char addr[8];
15636   CORE_ADDR baseaddr;
15637
15638   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15639
15640   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15641   obstack_grow (obstack, addr, 8);
15642   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15643   obstack_grow (obstack, addr, 8);
15644   cu_index_to_write = MAYBE_SWAP (cu_index);
15645   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15646 }
15647
15648 /* Worker function for traversing an addrmap to build the address table.  */
15649
15650 static int
15651 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15652 {
15653   struct addrmap_index_data *data = datap;
15654   struct partial_symtab *pst = obj;
15655   offset_type cu_index;
15656   void **slot;
15657
15658   if (data->previous_valid)
15659     add_address_entry (data->objfile, data->addr_obstack,
15660                        data->previous_cu_start, start_addr,
15661                        data->previous_cu_index);
15662
15663   data->previous_cu_start = start_addr;
15664   if (pst != NULL)
15665     {
15666       struct psymtab_cu_index_map find_map, *map;
15667       find_map.psymtab = pst;
15668       map = htab_find (data->cu_index_htab, &find_map);
15669       gdb_assert (map != NULL);
15670       data->previous_cu_index = map->cu_index;
15671       data->previous_valid = 1;
15672     }
15673   else
15674       data->previous_valid = 0;
15675
15676   return 0;
15677 }
15678
15679 /* Write OBJFILE's address map to OBSTACK.
15680    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15681    in the index file.  */
15682
15683 static void
15684 write_address_map (struct objfile *objfile, struct obstack *obstack,
15685                    htab_t cu_index_htab)
15686 {
15687   struct addrmap_index_data addrmap_index_data;
15688
15689   /* When writing the address table, we have to cope with the fact that
15690      the addrmap iterator only provides the start of a region; we have to
15691      wait until the next invocation to get the start of the next region.  */
15692
15693   addrmap_index_data.objfile = objfile;
15694   addrmap_index_data.addr_obstack = obstack;
15695   addrmap_index_data.cu_index_htab = cu_index_htab;
15696   addrmap_index_data.previous_valid = 0;
15697
15698   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15699                    &addrmap_index_data);
15700
15701   /* It's highly unlikely the last entry (end address = 0xff...ff)
15702      is valid, but we should still handle it.
15703      The end address is recorded as the start of the next region, but that
15704      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15705      anyway.  */
15706   if (addrmap_index_data.previous_valid)
15707     add_address_entry (objfile, obstack,
15708                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15709                        addrmap_index_data.previous_cu_index);
15710 }
15711
15712 /* Add a list of partial symbols to SYMTAB.  */
15713
15714 static void
15715 write_psymbols (struct mapped_symtab *symtab,
15716                 htab_t psyms_seen,
15717                 struct partial_symbol **psymp,
15718                 int count,
15719                 offset_type cu_index,
15720                 int is_static)
15721 {
15722   for (; count-- > 0; ++psymp)
15723     {
15724       void **slot, *lookup;
15725
15726       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15727         error (_("Ada is not currently supported by the index"));
15728
15729       /* We only want to add a given psymbol once.  However, we also
15730          want to account for whether it is global or static.  So, we
15731          may add it twice, using slightly different values.  */
15732       if (is_static)
15733         {
15734           uintptr_t val = 1 | (uintptr_t) *psymp;
15735
15736           lookup = (void *) val;
15737         }
15738       else
15739         lookup = *psymp;
15740
15741       /* Only add a given psymbol once.  */
15742       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15743       if (!*slot)
15744         {
15745           *slot = lookup;
15746           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15747         }
15748     }
15749 }
15750
15751 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15752    exception if there is an error.  */
15753
15754 static void
15755 write_obstack (FILE *file, struct obstack *obstack)
15756 {
15757   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15758               file)
15759       != obstack_object_size (obstack))
15760     error (_("couldn't data write to file"));
15761 }
15762
15763 /* Unlink a file if the argument is not NULL.  */
15764
15765 static void
15766 unlink_if_set (void *p)
15767 {
15768   char **filename = p;
15769   if (*filename)
15770     unlink (*filename);
15771 }
15772
15773 /* A helper struct used when iterating over debug_types.  */
15774 struct signatured_type_index_data
15775 {
15776   struct objfile *objfile;
15777   struct mapped_symtab *symtab;
15778   struct obstack *types_list;
15779   htab_t psyms_seen;
15780   int cu_index;
15781 };
15782
15783 /* A helper function that writes a single signatured_type to an
15784    obstack.  */
15785
15786 static int
15787 write_one_signatured_type (void **slot, void *d)
15788 {
15789   struct signatured_type_index_data *info = d;
15790   struct signatured_type *entry = (struct signatured_type *) *slot;
15791   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15792   struct partial_symtab *psymtab = per_cu->v.psymtab;
15793   gdb_byte val[8];
15794
15795   write_psymbols (info->symtab,
15796                   info->psyms_seen,
15797                   info->objfile->global_psymbols.list
15798                   + psymtab->globals_offset,
15799                   psymtab->n_global_syms, info->cu_index,
15800                   0);
15801   write_psymbols (info->symtab,
15802                   info->psyms_seen,
15803                   info->objfile->static_psymbols.list
15804                   + psymtab->statics_offset,
15805                   psymtab->n_static_syms, info->cu_index,
15806                   1);
15807
15808   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15809   obstack_grow (info->types_list, val, 8);
15810   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15811   obstack_grow (info->types_list, val, 8);
15812   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15813   obstack_grow (info->types_list, val, 8);
15814
15815   ++info->cu_index;
15816
15817   return 1;
15818 }
15819
15820 /* A cleanup function for an htab_t.  */
15821
15822 static void
15823 cleanup_htab (void *arg)
15824 {
15825   htab_delete (arg);
15826 }
15827
15828 /* Create an index file for OBJFILE in the directory DIR.  */
15829
15830 static void
15831 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15832 {
15833   struct cleanup *cleanup;
15834   char *filename, *cleanup_filename;
15835   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15836   struct obstack cu_list, types_cu_list;
15837   int i;
15838   FILE *out_file;
15839   struct mapped_symtab *symtab;
15840   offset_type val, size_of_contents, total_len;
15841   struct stat st;
15842   char buf[8];
15843   htab_t psyms_seen;
15844   htab_t cu_index_htab;
15845   struct psymtab_cu_index_map *psymtab_cu_index_map;
15846
15847   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15848     return;
15849
15850   if (dwarf2_per_objfile->using_index)
15851     error (_("Cannot use an index to create the index"));
15852
15853   if (stat (objfile->name, &st) < 0)
15854     perror_with_name (objfile->name);
15855
15856   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15857                      INDEX_SUFFIX, (char *) NULL);
15858   cleanup = make_cleanup (xfree, filename);
15859
15860   out_file = fopen (filename, "wb");
15861   if (!out_file)
15862     error (_("Can't open `%s' for writing"), filename);
15863
15864   cleanup_filename = filename;
15865   make_cleanup (unlink_if_set, &cleanup_filename);
15866
15867   symtab = create_mapped_symtab ();
15868   make_cleanup (cleanup_mapped_symtab, symtab);
15869
15870   obstack_init (&addr_obstack);
15871   make_cleanup_obstack_free (&addr_obstack);
15872
15873   obstack_init (&cu_list);
15874   make_cleanup_obstack_free (&cu_list);
15875
15876   obstack_init (&types_cu_list);
15877   make_cleanup_obstack_free (&types_cu_list);
15878
15879   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15880                                   NULL, xcalloc, xfree);
15881   make_cleanup (cleanup_htab, psyms_seen);
15882
15883   /* While we're scanning CU's create a table that maps a psymtab pointer
15884      (which is what addrmap records) to its index (which is what is recorded
15885      in the index file).  This will later be needed to write the address
15886      table.  */
15887   cu_index_htab = htab_create_alloc (100,
15888                                      hash_psymtab_cu_index,
15889                                      eq_psymtab_cu_index,
15890                                      NULL, xcalloc, xfree);
15891   make_cleanup (cleanup_htab, cu_index_htab);
15892   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15893     xmalloc (sizeof (struct psymtab_cu_index_map)
15894              * dwarf2_per_objfile->n_comp_units);
15895   make_cleanup (xfree, psymtab_cu_index_map);
15896
15897   /* The CU list is already sorted, so we don't need to do additional
15898      work here.  Also, the debug_types entries do not appear in
15899      all_comp_units, but only in their own hash table.  */
15900   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15901     {
15902       struct dwarf2_per_cu_data *per_cu
15903         = dwarf2_per_objfile->all_comp_units[i];
15904       struct partial_symtab *psymtab = per_cu->v.psymtab;
15905       gdb_byte val[8];
15906       struct psymtab_cu_index_map *map;
15907       void **slot;
15908
15909       write_psymbols (symtab,
15910                       psyms_seen,
15911                       objfile->global_psymbols.list + psymtab->globals_offset,
15912                       psymtab->n_global_syms, i,
15913                       0);
15914       write_psymbols (symtab,
15915                       psyms_seen,
15916                       objfile->static_psymbols.list + psymtab->statics_offset,
15917                       psymtab->n_static_syms, i,
15918                       1);
15919
15920       map = &psymtab_cu_index_map[i];
15921       map->psymtab = psymtab;
15922       map->cu_index = i;
15923       slot = htab_find_slot (cu_index_htab, map, INSERT);
15924       gdb_assert (slot != NULL);
15925       gdb_assert (*slot == NULL);
15926       *slot = map;
15927
15928       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15929       obstack_grow (&cu_list, val, 8);
15930       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15931       obstack_grow (&cu_list, val, 8);
15932     }
15933
15934   /* Dump the address map.  */
15935   write_address_map (objfile, &addr_obstack, cu_index_htab);
15936
15937   /* Write out the .debug_type entries, if any.  */
15938   if (dwarf2_per_objfile->signatured_types)
15939     {
15940       struct signatured_type_index_data sig_data;
15941
15942       sig_data.objfile = objfile;
15943       sig_data.symtab = symtab;
15944       sig_data.types_list = &types_cu_list;
15945       sig_data.psyms_seen = psyms_seen;
15946       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15947       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15948                               write_one_signatured_type, &sig_data);
15949     }
15950
15951   obstack_init (&constant_pool);
15952   make_cleanup_obstack_free (&constant_pool);
15953   obstack_init (&symtab_obstack);
15954   make_cleanup_obstack_free (&symtab_obstack);
15955   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15956
15957   obstack_init (&contents);
15958   make_cleanup_obstack_free (&contents);
15959   size_of_contents = 6 * sizeof (offset_type);
15960   total_len = size_of_contents;
15961
15962   /* The version number.  */
15963   val = MAYBE_SWAP (5);
15964   obstack_grow (&contents, &val, sizeof (val));
15965
15966   /* The offset of the CU list from the start of the file.  */
15967   val = MAYBE_SWAP (total_len);
15968   obstack_grow (&contents, &val, sizeof (val));
15969   total_len += obstack_object_size (&cu_list);
15970
15971   /* The offset of the types CU list from the start of the file.  */
15972   val = MAYBE_SWAP (total_len);
15973   obstack_grow (&contents, &val, sizeof (val));
15974   total_len += obstack_object_size (&types_cu_list);
15975
15976   /* The offset of the address table from the start of the file.  */
15977   val = MAYBE_SWAP (total_len);
15978   obstack_grow (&contents, &val, sizeof (val));
15979   total_len += obstack_object_size (&addr_obstack);
15980
15981   /* The offset of the symbol table from the start of the file.  */
15982   val = MAYBE_SWAP (total_len);
15983   obstack_grow (&contents, &val, sizeof (val));
15984   total_len += obstack_object_size (&symtab_obstack);
15985
15986   /* The offset of the constant pool from the start of the file.  */
15987   val = MAYBE_SWAP (total_len);
15988   obstack_grow (&contents, &val, sizeof (val));
15989   total_len += obstack_object_size (&constant_pool);
15990
15991   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15992
15993   write_obstack (out_file, &contents);
15994   write_obstack (out_file, &cu_list);
15995   write_obstack (out_file, &types_cu_list);
15996   write_obstack (out_file, &addr_obstack);
15997   write_obstack (out_file, &symtab_obstack);
15998   write_obstack (out_file, &constant_pool);
15999
16000   fclose (out_file);
16001
16002   /* We want to keep the file, so we set cleanup_filename to NULL
16003      here.  See unlink_if_set.  */
16004   cleanup_filename = NULL;
16005
16006   do_cleanups (cleanup);
16007 }
16008
16009 /* Implementation of the `save gdb-index' command.
16010    
16011    Note that the file format used by this command is documented in the
16012    GDB manual.  Any changes here must be documented there.  */
16013
16014 static void
16015 save_gdb_index_command (char *arg, int from_tty)
16016 {
16017   struct objfile *objfile;
16018
16019   if (!arg || !*arg)
16020     error (_("usage: save gdb-index DIRECTORY"));
16021
16022   ALL_OBJFILES (objfile)
16023   {
16024     struct stat st;
16025
16026     /* If the objfile does not correspond to an actual file, skip it.  */
16027     if (stat (objfile->name, &st) < 0)
16028       continue;
16029
16030     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16031     if (dwarf2_per_objfile)
16032       {
16033         volatile struct gdb_exception except;
16034
16035         TRY_CATCH (except, RETURN_MASK_ERROR)
16036           {
16037             write_psymtabs_to_index (objfile, arg);
16038           }
16039         if (except.reason < 0)
16040           exception_fprintf (gdb_stderr, except,
16041                              _("Error while writing index for `%s': "),
16042                              objfile->name);
16043       }
16044   }
16045 }
16046
16047 \f
16048
16049 int dwarf2_always_disassemble;
16050
16051 static void
16052 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16053                                 struct cmd_list_element *c, const char *value)
16054 {
16055   fprintf_filtered (file,
16056                     _("Whether to always disassemble "
16057                       "DWARF expressions is %s.\n"),
16058                     value);
16059 }
16060
16061 void _initialize_dwarf2_read (void);
16062
16063 void
16064 _initialize_dwarf2_read (void)
16065 {
16066   struct cmd_list_element *c;
16067
16068   dwarf2_objfile_data_key
16069     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16070
16071   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16072 Set DWARF 2 specific variables.\n\
16073 Configure DWARF 2 variables such as the cache size"),
16074                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16075                   0/*allow-unknown*/, &maintenance_set_cmdlist);
16076
16077   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16078 Show DWARF 2 specific variables\n\
16079 Show DWARF 2 variables such as the cache size"),
16080                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16081                   0/*allow-unknown*/, &maintenance_show_cmdlist);
16082
16083   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16084                             &dwarf2_max_cache_age, _("\
16085 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16086 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16087 A higher limit means that cached compilation units will be stored\n\
16088 in memory longer, and more total memory will be used.  Zero disables\n\
16089 caching, which can slow down startup."),
16090                             NULL,
16091                             show_dwarf2_max_cache_age,
16092                             &set_dwarf2_cmdlist,
16093                             &show_dwarf2_cmdlist);
16094
16095   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16096                            &dwarf2_always_disassemble, _("\
16097 Set whether `info address' always disassembles DWARF expressions."), _("\
16098 Show whether `info address' always disassembles DWARF expressions."), _("\
16099 When enabled, DWARF expressions are always printed in an assembly-like\n\
16100 syntax.  When disabled, expressions will be printed in a more\n\
16101 conversational style, when possible."),
16102                            NULL,
16103                            show_dwarf2_always_disassemble,
16104                            &set_dwarf2_cmdlist,
16105                            &show_dwarf2_cmdlist);
16106
16107   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16108 Set debugging of the dwarf2 DIE reader."), _("\
16109 Show debugging of the dwarf2 DIE reader."), _("\
16110 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16111 The value is the maximum depth to print."),
16112                             NULL,
16113                             NULL,
16114                             &setdebuglist, &showdebuglist);
16115
16116   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16117                _("\
16118 Save a gdb-index file.\n\
16119 Usage: save gdb-index DIRECTORY"),
16120                &save_cmdlist);
16121   set_cmd_completer (c, filename_completer);
16122 }