d12028f6175fc12c79c71a1f637df7f9f8f59990
[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
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80    Because of alignment constraints, this structure has padding and cannot
81    be mapped directly onto the beginning of the .debug_info section.  */
82 typedef struct comp_unit_header
83   {
84     unsigned int length;        /* length of the .debug_info
85                                    contribution */
86     unsigned short version;     /* version number -- 2 for DWARF
87                                    version 2 */
88     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89     unsigned char addr_size;    /* byte size of an address -- 4 */
90   }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96    Because of alignment constraints, this structure has padding and cannot
97    be mapped directly onto the beginning of the .debug_info section.  */
98 typedef struct statement_prologue
99   {
100     unsigned int total_length;  /* byte length of the statement
101                                    information */
102     unsigned short version;     /* version number -- 2 for DWARF
103                                    version 2 */
104     unsigned int prologue_length;       /* # bytes between prologue &
105                                            stmt program */
106     unsigned char minimum_instruction_length;   /* byte size of
107                                                    smallest instr */
108     unsigned char default_is_stmt;      /* initial value of is_stmt
109                                            register */
110     char line_base;
111     unsigned char line_range;
112     unsigned char opcode_base;  /* number assigned to first special
113                                    opcode */
114     unsigned char *standard_opcode_lengths;
115   }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in.  */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124    info for C++ namespaces.  GCC 3.3.x did not produce this information,
125    but later versions do.  */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133   asection *asection;
134   gdb_byte *buffer;
135   bfd_size_type size;
136   int was_mmapped;
137   /* True if we have tried to read this section.  */
138   int readin;
139 };
140
141 /* All offsets in the index are of this type.  It must be
142    architecture-independent.  */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index.  The file format is described in
148    a comment by the code that writes the index.  */
149 struct mapped_index
150 {
151   /* The total length of the buffer.  */
152   off_t total_size;
153   /* A pointer to the address table data.  */
154   const gdb_byte *address_table;
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157   /* The symbol table, implemented as a hash table.  */
158   const offset_type *symbol_table;
159   /* Size in slots, each slot is 2 offset_types.  */
160   offset_type symbol_table_slots;
161   /* A pointer to the constant pool.  */
162   const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167   struct dwarf2_section_info info;
168   struct dwarf2_section_info abbrev;
169   struct dwarf2_section_info line;
170   struct dwarf2_section_info loc;
171   struct dwarf2_section_info macinfo;
172   struct dwarf2_section_info str;
173   struct dwarf2_section_info ranges;
174   struct dwarf2_section_info types;
175   struct dwarf2_section_info frame;
176   struct dwarf2_section_info eh_frame;
177   struct dwarf2_section_info gdb_index;
178
179   /* Back link.  */
180   struct objfile *objfile;
181
182   /* A list of all the compilation units.  This is used to locate
183      the target compilation unit of a particular reference.  */
184   struct dwarf2_per_cu_data **all_comp_units;
185
186   /* The number of compilation units in ALL_COMP_UNITS.  */
187   int n_comp_units;
188
189   /* The number of .debug_types-related CUs.  */
190   int n_type_comp_units;
191
192   /* The .debug_types-related CUs.  */
193   struct dwarf2_per_cu_data **type_comp_units;
194
195   /* A chain of compilation units that are currently read in, so that
196      they can be freed later.  */
197   struct dwarf2_per_cu_data *read_in_chain;
198
199   /* A table mapping .debug_types signatures to its signatured_type entry.
200      This is NULL if the .debug_types section hasn't been read in yet.  */
201   htab_t signatured_types;
202
203   /* A flag indicating wether this objfile has a section loaded at a
204      VMA of 0.  */
205   int has_section_at_zero;
206
207   /* True if we are using the mapped index,
208      or we are faking it for OBJF_READNOW's sake.  */
209   unsigned char using_index;
210
211   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
212   struct mapped_index *index_table;
213
214   /* When using index_table, this keeps track of all quick_file_names entries.
215      TUs can share line table entries with CUs or other TUs, and there can be
216      a lot more TUs than unique line tables, so we maintain a separate table
217      of all line table entries to support the sharing.  */
218   htab_t quick_file_names_table;
219
220   /* Set during partial symbol reading, to prevent queueing of full
221      symbols.  */
222   int reading_partial_symbols;
223
224   /* Table mapping type .debug_info DIE offsets to types.
225      This is NULL if not allocated yet.
226      It (currently) makes sense to allocate debug_types_type_hash lazily.
227      To keep things simple we allocate both lazily.  */
228   htab_t debug_info_type_hash;
229
230   /* Table mapping type .debug_types DIE offsets to types.
231      This is NULL if not allocated yet.  */
232   htab_t debug_types_type_hash;
233 };
234
235 static struct dwarf2_per_objfile *dwarf2_per_objfile;
236
237 /* names of the debugging sections */
238
239 /* Note that if the debugging section has been compressed, it might
240    have a name like .zdebug_info.  */
241
242 #define INFO_SECTION     "debug_info"
243 #define ABBREV_SECTION   "debug_abbrev"
244 #define LINE_SECTION     "debug_line"
245 #define LOC_SECTION      "debug_loc"
246 #define MACINFO_SECTION  "debug_macinfo"
247 #define STR_SECTION      "debug_str"
248 #define RANGES_SECTION   "debug_ranges"
249 #define TYPES_SECTION    "debug_types"
250 #define FRAME_SECTION    "debug_frame"
251 #define EH_FRAME_SECTION "eh_frame"
252 #define GDB_INDEX_SECTION "gdb_index"
253
254 /* local data types */
255
256 /* We hold several abbreviation tables in memory at the same time.  */
257 #ifndef ABBREV_HASH_SIZE
258 #define ABBREV_HASH_SIZE 121
259 #endif
260
261 /* The data in a compilation unit header, after target2host
262    translation, looks like this.  */
263 struct comp_unit_head
264 {
265   unsigned int length;
266   short version;
267   unsigned char addr_size;
268   unsigned char signed_addr_p;
269   unsigned int abbrev_offset;
270
271   /* Size of file offsets; either 4 or 8.  */
272   unsigned int offset_size;
273
274   /* Size of the length field; either 4 or 12.  */
275   unsigned int initial_length_size;
276
277   /* Offset to the first byte of this compilation unit header in the
278      .debug_info section, for resolving relative reference dies.  */
279   unsigned int offset;
280
281   /* Offset to first die in this cu from the start of the cu.
282      This will be the first byte following the compilation unit header.  */
283   unsigned int first_die_offset;
284 };
285
286 /* Type used for delaying computation of method physnames.
287    See comments for compute_delayed_physnames.  */
288 struct delayed_method_info
289 {
290   /* The type to which the method is attached, i.e., its parent class.  */
291   struct type *type;
292
293   /* The index of the method in the type's function fieldlists.  */
294   int fnfield_index;
295
296   /* The index of the method in the fieldlist.  */
297   int index;
298
299   /* The name of the DIE.  */
300   const char *name;
301
302   /*  The DIE associated with this method.  */
303   struct die_info *die;
304 };
305
306 typedef struct delayed_method_info delayed_method_info;
307 DEF_VEC_O (delayed_method_info);
308
309 /* Internal state when decoding a particular compilation unit.  */
310 struct dwarf2_cu
311 {
312   /* The objfile containing this compilation unit.  */
313   struct objfile *objfile;
314
315   /* The header of the compilation unit.  */
316   struct comp_unit_head header;
317
318   /* Base address of this compilation unit.  */
319   CORE_ADDR base_address;
320
321   /* Non-zero if base_address has been set.  */
322   int base_known;
323
324   struct function_range *first_fn, *last_fn, *cached_fn;
325
326   /* The language we are debugging.  */
327   enum language language;
328   const struct language_defn *language_defn;
329
330   const char *producer;
331
332   /* The generic symbol table building routines have separate lists for
333      file scope symbols and all all other scopes (local scopes).  So
334      we need to select the right one to pass to add_symbol_to_list().
335      We do it by keeping a pointer to the correct list in list_in_scope.
336
337      FIXME: The original dwarf code just treated the file scope as the
338      first local scope, and all other local scopes as nested local
339      scopes, and worked fine.  Check to see if we really need to
340      distinguish these in buildsym.c.  */
341   struct pending **list_in_scope;
342
343   /* DWARF abbreviation table associated with this compilation unit.  */
344   struct abbrev_info **dwarf2_abbrevs;
345
346   /* Storage for the abbrev table.  */
347   struct obstack abbrev_obstack;
348
349   /* Hash table holding all the loaded partial DIEs.  */
350   htab_t partial_dies;
351
352   /* Storage for things with the same lifetime as this read-in compilation
353      unit, including partial DIEs.  */
354   struct obstack comp_unit_obstack;
355
356   /* When multiple dwarf2_cu structures are living in memory, this field
357      chains them all together, so that they can be released efficiently.
358      We will probably also want a generation counter so that most-recently-used
359      compilation units are cached...  */
360   struct dwarf2_per_cu_data *read_in_chain;
361
362   /* Backchain to our per_cu entry if the tree has been built.  */
363   struct dwarf2_per_cu_data *per_cu;
364
365   /* How many compilation units ago was this CU last referenced?  */
366   int last_used;
367
368   /* A hash table of die offsets for following references.  */
369   htab_t die_hash;
370
371   /* Full DIEs if read in.  */
372   struct die_info *dies;
373
374   /* A set of pointers to dwarf2_per_cu_data objects for compilation
375      units referenced by this one.  Only set during full symbol processing;
376      partial symbol tables do not have dependencies.  */
377   htab_t dependencies;
378
379   /* Header data from the line table, during full symbol processing.  */
380   struct line_header *line_header;
381
382   /* A list of methods which need to have physnames computed
383      after all type information has been read.  */
384   VEC (delayed_method_info) *method_list;
385
386   /* Mark used when releasing cached dies.  */
387   unsigned int mark : 1;
388
389   /* This flag will be set if this compilation unit might include
390      inter-compilation-unit references.  */
391   unsigned int has_form_ref_addr : 1;
392
393   /* This flag will be set if this compilation unit includes any
394      DW_TAG_namespace DIEs.  If we know that there are explicit
395      DIEs for namespaces, we don't need to try to infer them
396      from mangled names.  */
397   unsigned int has_namespace_info : 1;
398 };
399
400 /* Persistent data held for a compilation unit, even when not
401    processing it.  We put a pointer to this structure in the
402    read_symtab_private field of the psymtab.  If we encounter
403    inter-compilation-unit references, we also maintain a sorted
404    list of all compilation units.  */
405
406 struct dwarf2_per_cu_data
407 {
408   /* The start offset and length of this compilation unit.  2**29-1
409      bytes should suffice to store the length of any compilation unit
410      - if it doesn't, GDB will fall over anyway.
411      NOTE: Unlike comp_unit_head.length, this length includes
412      initial_length_size.  */
413   unsigned int offset;
414   unsigned int length : 29;
415
416   /* Flag indicating this compilation unit will be read in before
417      any of the current compilation units are processed.  */
418   unsigned int queued : 1;
419
420   /* This flag will be set if we need to load absolutely all DIEs
421      for this compilation unit, instead of just the ones we think
422      are interesting.  It gets set if we look for a DIE in the
423      hash table and don't find it.  */
424   unsigned int load_all_dies : 1;
425
426   /* Non-zero if this CU is from .debug_types.
427      Otherwise it's from .debug_info.  */
428   unsigned int from_debug_types : 1;
429
430   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
431      of the CU cache it gets reset to NULL again.  */
432   struct dwarf2_cu *cu;
433
434   /* The corresponding objfile.  */
435   struct objfile *objfile;
436
437   /* When using partial symbol tables, the 'psymtab' field is active.
438      Otherwise the 'quick' field is active.  */
439   union
440   {
441     /* The partial symbol table associated with this compilation unit,
442        or NULL for partial units (which do not have an associated
443        symtab).  */
444     struct partial_symtab *psymtab;
445
446     /* Data needed by the "quick" functions.  */
447     struct dwarf2_per_cu_quick_data *quick;
448   } v;
449 };
450
451 /* Entry in the signatured_types hash table.  */
452
453 struct signatured_type
454 {
455   ULONGEST signature;
456
457   /* Offset in .debug_types of the TU (type_unit) for this type.  */
458   unsigned int offset;
459
460   /* Offset in .debug_types of the type defined by this TU.  */
461   unsigned int type_offset;
462
463   /* The CU(/TU) of this type.  */
464   struct dwarf2_per_cu_data per_cu;
465 };
466
467 /* Struct used to pass misc. parameters to read_die_and_children, et
468    al.  which are used for both .debug_info and .debug_types dies.
469    All parameters here are unchanging for the life of the call.  This
470    struct exists to abstract away the constant parameters of die
471    reading.  */
472
473 struct die_reader_specs
474 {
475   /* The bfd of this objfile.  */
476   bfd* abfd;
477
478   /* The CU of the DIE we are parsing.  */
479   struct dwarf2_cu *cu;
480
481   /* Pointer to start of section buffer.
482      This is either the start of .debug_info or .debug_types.  */
483   const gdb_byte *buffer;
484 };
485
486 /* The line number information for a compilation unit (found in the
487    .debug_line section) begins with a "statement program header",
488    which contains the following information.  */
489 struct line_header
490 {
491   unsigned int total_length;
492   unsigned short version;
493   unsigned int header_length;
494   unsigned char minimum_instruction_length;
495   unsigned char maximum_ops_per_instruction;
496   unsigned char default_is_stmt;
497   int line_base;
498   unsigned char line_range;
499   unsigned char opcode_base;
500
501   /* standard_opcode_lengths[i] is the number of operands for the
502      standard opcode whose value is i.  This means that
503      standard_opcode_lengths[0] is unused, and the last meaningful
504      element is standard_opcode_lengths[opcode_base - 1].  */
505   unsigned char *standard_opcode_lengths;
506
507   /* The include_directories table.  NOTE!  These strings are not
508      allocated with xmalloc; instead, they are pointers into
509      debug_line_buffer.  If you try to free them, `free' will get
510      indigestion.  */
511   unsigned int num_include_dirs, include_dirs_size;
512   char **include_dirs;
513
514   /* The file_names table.  NOTE!  These strings are not allocated
515      with xmalloc; instead, they are pointers into debug_line_buffer.
516      Don't try to free them directly.  */
517   unsigned int num_file_names, file_names_size;
518   struct file_entry
519   {
520     char *name;
521     unsigned int dir_index;
522     unsigned int mod_time;
523     unsigned int length;
524     int included_p; /* Non-zero if referenced by the Line Number Program.  */
525     struct symtab *symtab; /* The associated symbol table, if any.  */
526   } *file_names;
527
528   /* The start and end of the statement program following this
529      header.  These point into dwarf2_per_objfile->line_buffer.  */
530   gdb_byte *statement_program_start, *statement_program_end;
531 };
532
533 /* When we construct a partial symbol table entry we only
534    need this much information.  */
535 struct partial_die_info
536   {
537     /* Offset of this DIE.  */
538     unsigned int offset;
539
540     /* DWARF-2 tag for this DIE.  */
541     ENUM_BITFIELD(dwarf_tag) tag : 16;
542
543     /* Assorted flags describing the data found in this DIE.  */
544     unsigned int has_children : 1;
545     unsigned int is_external : 1;
546     unsigned int is_declaration : 1;
547     unsigned int has_type : 1;
548     unsigned int has_specification : 1;
549     unsigned int has_pc_info : 1;
550
551     /* Flag set if the SCOPE field of this structure has been
552        computed.  */
553     unsigned int scope_set : 1;
554
555     /* Flag set if the DIE has a byte_size attribute.  */
556     unsigned int has_byte_size : 1;
557
558     /* Flag set if any of the DIE's children are template arguments.  */
559     unsigned int has_template_arguments : 1;
560
561     /* Flag set if fixup_partial_die has been called on this die.  */
562     unsigned int fixup_called : 1;
563
564     /* The name of this DIE.  Normally the value of DW_AT_name, but
565        sometimes a default name for unnamed DIEs.  */
566     char *name;
567
568     /* The linkage name, if present.  */
569     const char *linkage_name;
570
571     /* The scope to prepend to our children.  This is generally
572        allocated on the comp_unit_obstack, so will disappear
573        when this compilation unit leaves the cache.  */
574     char *scope;
575
576     /* The location description associated with this DIE, if any.  */
577     struct dwarf_block *locdesc;
578
579     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
580     CORE_ADDR lowpc;
581     CORE_ADDR highpc;
582
583     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
584        DW_AT_sibling, if any.  */
585     /* NOTE: This member isn't strictly necessary, read_partial_die could
586        return DW_AT_sibling values to its caller load_partial_dies.  */
587     gdb_byte *sibling;
588
589     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
590        DW_AT_specification (or DW_AT_abstract_origin or
591        DW_AT_extension).  */
592     unsigned int spec_offset;
593
594     /* Pointers to this DIE's parent, first child, and next sibling,
595        if any.  */
596     struct partial_die_info *die_parent, *die_child, *die_sibling;
597   };
598
599 /* This data structure holds the information of an abbrev.  */
600 struct abbrev_info
601   {
602     unsigned int number;        /* number identifying abbrev */
603     enum dwarf_tag tag;         /* dwarf tag */
604     unsigned short has_children;                /* boolean */
605     unsigned short num_attrs;   /* number of attributes */
606     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
607     struct abbrev_info *next;   /* next in chain */
608   };
609
610 struct attr_abbrev
611   {
612     ENUM_BITFIELD(dwarf_attribute) name : 16;
613     ENUM_BITFIELD(dwarf_form) form : 16;
614   };
615
616 /* Attributes have a name and a value.  */
617 struct attribute
618   {
619     ENUM_BITFIELD(dwarf_attribute) name : 16;
620     ENUM_BITFIELD(dwarf_form) form : 15;
621
622     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
623        field should be in u.str (existing only for DW_STRING) but it is kept
624        here for better struct attribute alignment.  */
625     unsigned int string_is_canonical : 1;
626
627     union
628       {
629         char *str;
630         struct dwarf_block *blk;
631         ULONGEST unsnd;
632         LONGEST snd;
633         CORE_ADDR addr;
634         struct signatured_type *signatured_type;
635       }
636     u;
637   };
638
639 /* This data structure holds a complete die structure.  */
640 struct die_info
641   {
642     /* DWARF-2 tag for this DIE.  */
643     ENUM_BITFIELD(dwarf_tag) tag : 16;
644
645     /* Number of attributes */
646     unsigned char num_attrs;
647
648     /* True if we're presently building the full type name for the
649        type derived from this DIE.  */
650     unsigned char building_fullname : 1;
651
652     /* Abbrev number */
653     unsigned int abbrev;
654
655     /* Offset in .debug_info or .debug_types section.  */
656     unsigned int offset;
657
658     /* The dies in a compilation unit form an n-ary tree.  PARENT
659        points to this die's parent; CHILD points to the first child of
660        this node; and all the children of a given node are chained
661        together via their SIBLING fields.  */
662     struct die_info *child;     /* Its first child, if any.  */
663     struct die_info *sibling;   /* Its next sibling, if any.  */
664     struct die_info *parent;    /* Its parent, if any.  */
665
666     /* An array of attributes, with NUM_ATTRS elements.  There may be
667        zero, but it's not common and zero-sized arrays are not
668        sufficiently portable C.  */
669     struct attribute attrs[1];
670   };
671
672 struct function_range
673 {
674   const char *name;
675   CORE_ADDR lowpc, highpc;
676   int seen_line;
677   struct function_range *next;
678 };
679
680 /* Get at parts of an attribute structure.  */
681
682 #define DW_STRING(attr)    ((attr)->u.str)
683 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
684 #define DW_UNSND(attr)     ((attr)->u.unsnd)
685 #define DW_BLOCK(attr)     ((attr)->u.blk)
686 #define DW_SND(attr)       ((attr)->u.snd)
687 #define DW_ADDR(attr)      ((attr)->u.addr)
688 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
689
690 /* Blocks are a bunch of untyped bytes.  */
691 struct dwarf_block
692   {
693     unsigned int size;
694     gdb_byte *data;
695   };
696
697 #ifndef ATTR_ALLOC_CHUNK
698 #define ATTR_ALLOC_CHUNK 4
699 #endif
700
701 /* Allocate fields for structs, unions and enums in this size.  */
702 #ifndef DW_FIELD_ALLOC_CHUNK
703 #define DW_FIELD_ALLOC_CHUNK 4
704 #endif
705
706 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
707    but this would require a corresponding change in unpack_field_as_long
708    and friends.  */
709 static int bits_per_byte = 8;
710
711 /* The routines that read and process dies for a C struct or C++ class
712    pass lists of data member fields and lists of member function fields
713    in an instance of a field_info structure, as defined below.  */
714 struct field_info
715   {
716     /* List of data member and baseclasses fields.  */
717     struct nextfield
718       {
719         struct nextfield *next;
720         int accessibility;
721         int virtuality;
722         struct field field;
723       }
724      *fields, *baseclasses;
725
726     /* Number of fields (including baseclasses).  */
727     int nfields;
728
729     /* Number of baseclasses.  */
730     int nbaseclasses;
731
732     /* Set if the accesibility of one of the fields is not public.  */
733     int non_public_fields;
734
735     /* Member function fields array, entries are allocated in the order they
736        are encountered in the object file.  */
737     struct nextfnfield
738       {
739         struct nextfnfield *next;
740         struct fn_field fnfield;
741       }
742      *fnfields;
743
744     /* Member function fieldlist array, contains name of possibly overloaded
745        member function, number of overloaded member functions and a pointer
746        to the head of the member function field chain.  */
747     struct fnfieldlist
748       {
749         char *name;
750         int length;
751         struct nextfnfield *head;
752       }
753      *fnfieldlists;
754
755     /* Number of entries in the fnfieldlists array.  */
756     int nfnfields;
757
758     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
759        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
760     struct typedef_field_list
761       {
762         struct typedef_field field;
763         struct typedef_field_list *next;
764       }
765     *typedef_field_list;
766     unsigned typedef_field_list_count;
767   };
768
769 /* One item on the queue of compilation units to read in full symbols
770    for.  */
771 struct dwarf2_queue_item
772 {
773   struct dwarf2_per_cu_data *per_cu;
774   struct dwarf2_queue_item *next;
775 };
776
777 /* The current queue.  */
778 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
779
780 /* Loaded secondary compilation units are kept in memory until they
781    have not been referenced for the processing of this many
782    compilation units.  Set this to zero to disable caching.  Cache
783    sizes of up to at least twenty will improve startup time for
784    typical inter-CU-reference binaries, at an obvious memory cost.  */
785 static int dwarf2_max_cache_age = 5;
786 static void
787 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
788                            struct cmd_list_element *c, const char *value)
789 {
790   fprintf_filtered (file, _("The upper bound on the age of cached "
791                             "dwarf2 compilation units is %s.\n"),
792                     value);
793 }
794
795
796 /* Various complaints about symbol reading that don't abort the process.  */
797
798 static void
799 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
800 {
801   complaint (&symfile_complaints,
802              _("statement list doesn't fit in .debug_line section"));
803 }
804
805 static void
806 dwarf2_debug_line_missing_file_complaint (void)
807 {
808   complaint (&symfile_complaints,
809              _(".debug_line section has line data without a file"));
810 }
811
812 static void
813 dwarf2_debug_line_missing_end_sequence_complaint (void)
814 {
815   complaint (&symfile_complaints,
816              _(".debug_line section has line "
817                "program sequence without an end"));
818 }
819
820 static void
821 dwarf2_complex_location_expr_complaint (void)
822 {
823   complaint (&symfile_complaints, _("location expression too complex"));
824 }
825
826 static void
827 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
828                                               int arg3)
829 {
830   complaint (&symfile_complaints,
831              _("const value length mismatch for '%s', got %d, expected %d"),
832              arg1, arg2, arg3);
833 }
834
835 static void
836 dwarf2_macros_too_long_complaint (void)
837 {
838   complaint (&symfile_complaints,
839              _("macro info runs off end of `.debug_macinfo' section"));
840 }
841
842 static void
843 dwarf2_macro_malformed_definition_complaint (const char *arg1)
844 {
845   complaint (&symfile_complaints,
846              _("macro debug info contains a "
847                "malformed macro definition:\n`%s'"),
848              arg1);
849 }
850
851 static void
852 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
853 {
854   complaint (&symfile_complaints,
855              _("invalid attribute class or form for '%s' in '%s'"),
856              arg1, arg2);
857 }
858
859 /* local function prototypes */
860
861 static void dwarf2_locate_sections (bfd *, asection *, void *);
862
863 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
864                                            struct objfile *);
865
866 static void dwarf2_build_psymtabs_hard (struct objfile *);
867
868 static void scan_partial_symbols (struct partial_die_info *,
869                                   CORE_ADDR *, CORE_ADDR *,
870                                   int, struct dwarf2_cu *);
871
872 static void add_partial_symbol (struct partial_die_info *,
873                                 struct dwarf2_cu *);
874
875 static void add_partial_namespace (struct partial_die_info *pdi,
876                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
877                                    int need_pc, struct dwarf2_cu *cu);
878
879 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
880                                 CORE_ADDR *highpc, int need_pc,
881                                 struct dwarf2_cu *cu);
882
883 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
884                                      struct dwarf2_cu *cu);
885
886 static void add_partial_subprogram (struct partial_die_info *pdi,
887                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
888                                     int need_pc, struct dwarf2_cu *cu);
889
890 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
891                                      gdb_byte *buffer, gdb_byte *info_ptr,
892                                      bfd *abfd, struct dwarf2_cu *cu);
893
894 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
895
896 static void psymtab_to_symtab_1 (struct partial_symtab *);
897
898 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
899
900 static void dwarf2_free_abbrev_table (void *);
901
902 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
903                                             struct dwarf2_cu *);
904
905 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
906                                                  struct dwarf2_cu *);
907
908 static struct partial_die_info *load_partial_dies (bfd *,
909                                                    gdb_byte *, gdb_byte *,
910                                                    int, struct dwarf2_cu *);
911
912 static gdb_byte *read_partial_die (struct partial_die_info *,
913                                    struct abbrev_info *abbrev,
914                                    unsigned int, bfd *,
915                                    gdb_byte *, gdb_byte *,
916                                    struct dwarf2_cu *);
917
918 static struct partial_die_info *find_partial_die (unsigned int,
919                                                   struct dwarf2_cu *);
920
921 static void fixup_partial_die (struct partial_die_info *,
922                                struct dwarf2_cu *);
923
924 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
925                                  bfd *, gdb_byte *, struct dwarf2_cu *);
926
927 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
928                                        bfd *, gdb_byte *, struct dwarf2_cu *);
929
930 static unsigned int read_1_byte (bfd *, gdb_byte *);
931
932 static int read_1_signed_byte (bfd *, gdb_byte *);
933
934 static unsigned int read_2_bytes (bfd *, gdb_byte *);
935
936 static unsigned int read_4_bytes (bfd *, gdb_byte *);
937
938 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
939
940 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
941                                unsigned int *);
942
943 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
944
945 static LONGEST read_checked_initial_length_and_offset
946   (bfd *, gdb_byte *, const struct comp_unit_head *,
947    unsigned int *, unsigned int *);
948
949 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
950                             unsigned int *);
951
952 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
953
954 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
955
956 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
957
958 static char *read_indirect_string (bfd *, gdb_byte *,
959                                    const struct comp_unit_head *,
960                                    unsigned int *);
961
962 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
963
964 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
965
966 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
967
968 static void set_cu_language (unsigned int, struct dwarf2_cu *);
969
970 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
971                                       struct dwarf2_cu *);
972
973 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
974                                                 unsigned int,
975                                                 struct dwarf2_cu *);
976
977 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
978                                struct dwarf2_cu *cu);
979
980 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
981
982 static struct die_info *die_specification (struct die_info *die,
983                                            struct dwarf2_cu **);
984
985 static void free_line_header (struct line_header *lh);
986
987 static void add_file_name (struct line_header *, char *, unsigned int,
988                            unsigned int, unsigned int);
989
990 static struct line_header *(dwarf_decode_line_header
991                             (unsigned int offset,
992                              bfd *abfd, struct dwarf2_cu *cu));
993
994 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
995                                 struct dwarf2_cu *, struct partial_symtab *);
996
997 static void dwarf2_start_subfile (char *, const char *, const char *);
998
999 static struct symbol *new_symbol (struct die_info *, struct type *,
1000                                   struct dwarf2_cu *);
1001
1002 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003                                        struct dwarf2_cu *, struct symbol *);
1004
1005 static void dwarf2_const_value (struct attribute *, struct symbol *,
1006                                 struct dwarf2_cu *);
1007
1008 static void dwarf2_const_value_attr (struct attribute *attr,
1009                                      struct type *type,
1010                                      const char *name,
1011                                      struct obstack *obstack,
1012                                      struct dwarf2_cu *cu, long *value,
1013                                      gdb_byte **bytes,
1014                                      struct dwarf2_locexpr_baton **baton);
1015
1016 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1017
1018 static int need_gnat_info (struct dwarf2_cu *);
1019
1020 static struct type *die_descriptive_type (struct die_info *,
1021                                           struct dwarf2_cu *);
1022
1023 static void set_descriptive_type (struct type *, struct die_info *,
1024                                   struct dwarf2_cu *);
1025
1026 static struct type *die_containing_type (struct die_info *,
1027                                          struct dwarf2_cu *);
1028
1029 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030                                      struct dwarf2_cu *);
1031
1032 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1033
1034 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
1036 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1037
1038 static char *typename_concat (struct obstack *obs, const char *prefix,
1039                               const char *suffix, int physname,
1040                               struct dwarf2_cu *cu);
1041
1042 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1043
1044 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
1046 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1047
1048 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1049
1050 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1051                                struct dwarf2_cu *, struct partial_symtab *);
1052
1053 static int dwarf2_get_pc_bounds (struct die_info *,
1054                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1055                                  struct partial_symtab *);
1056
1057 static void get_scope_pc_bounds (struct die_info *,
1058                                  CORE_ADDR *, CORE_ADDR *,
1059                                  struct dwarf2_cu *);
1060
1061 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1062                                         CORE_ADDR, struct dwarf2_cu *);
1063
1064 static void dwarf2_add_field (struct field_info *, struct die_info *,
1065                               struct dwarf2_cu *);
1066
1067 static void dwarf2_attach_fields_to_type (struct field_info *,
1068                                           struct type *, struct dwarf2_cu *);
1069
1070 static void dwarf2_add_member_fn (struct field_info *,
1071                                   struct die_info *, struct type *,
1072                                   struct dwarf2_cu *);
1073
1074 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1075                                              struct type *,
1076                                              struct dwarf2_cu *);
1077
1078 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1079
1080 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1081
1082 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1083
1084 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1085
1086 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1087
1088 static struct type *read_module_type (struct die_info *die,
1089                                       struct dwarf2_cu *cu);
1090
1091 static const char *namespace_name (struct die_info *die,
1092                                    int *is_anonymous, struct dwarf2_cu *);
1093
1094 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1095
1096 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1097
1098 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1099                                                        struct dwarf2_cu *);
1100
1101 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1102
1103 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1104                                                  gdb_byte *info_ptr,
1105                                                  gdb_byte **new_info_ptr,
1106                                                  struct die_info *parent);
1107
1108 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1109                                                gdb_byte *info_ptr,
1110                                                gdb_byte **new_info_ptr,
1111                                                struct die_info *parent);
1112
1113 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1114                                                gdb_byte *info_ptr,
1115                                                gdb_byte **new_info_ptr,
1116                                                struct die_info *parent);
1117
1118 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1119                                 struct die_info **, gdb_byte *,
1120                                 int *);
1121
1122 static void process_die (struct die_info *, struct dwarf2_cu *);
1123
1124 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1125                                        struct obstack *);
1126
1127 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1128
1129 static const char *dwarf2_full_name (char *name,
1130                                      struct die_info *die,
1131                                      struct dwarf2_cu *cu);
1132
1133 static struct die_info *dwarf2_extension (struct die_info *die,
1134                                           struct dwarf2_cu **);
1135
1136 static char *dwarf_tag_name (unsigned int);
1137
1138 static char *dwarf_attr_name (unsigned int);
1139
1140 static char *dwarf_form_name (unsigned int);
1141
1142 static char *dwarf_bool_name (unsigned int);
1143
1144 static char *dwarf_type_encoding_name (unsigned int);
1145
1146 #if 0
1147 static char *dwarf_cfi_name (unsigned int);
1148 #endif
1149
1150 static struct die_info *sibling_die (struct die_info *);
1151
1152 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1153
1154 static void dump_die_for_error (struct die_info *);
1155
1156 static void dump_die_1 (struct ui_file *, int level, int max_level,
1157                         struct die_info *);
1158
1159 /*static*/ void dump_die (struct die_info *, int max_level);
1160
1161 static void store_in_ref_table (struct die_info *,
1162                                 struct dwarf2_cu *);
1163
1164 static int is_ref_attr (struct attribute *);
1165
1166 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1167
1168 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1169
1170 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1171                                                struct attribute *,
1172                                                struct dwarf2_cu **);
1173
1174 static struct die_info *follow_die_ref (struct die_info *,
1175                                         struct attribute *,
1176                                         struct dwarf2_cu **);
1177
1178 static struct die_info *follow_die_sig (struct die_info *,
1179                                         struct attribute *,
1180                                         struct dwarf2_cu **);
1181
1182 static void read_signatured_type_at_offset (struct objfile *objfile,
1183                                             unsigned int offset);
1184
1185 static void read_signatured_type (struct objfile *,
1186                                   struct signatured_type *type_sig);
1187
1188 /* memory allocation interface */
1189
1190 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1191
1192 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1193
1194 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1195
1196 static void initialize_cu_func_list (struct dwarf2_cu *);
1197
1198 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1199                                  struct dwarf2_cu *);
1200
1201 static void dwarf_decode_macros (struct line_header *, unsigned int,
1202                                  char *, bfd *, struct dwarf2_cu *);
1203
1204 static int attr_form_is_block (struct attribute *);
1205
1206 static int attr_form_is_section_offset (struct attribute *);
1207
1208 static int attr_form_is_constant (struct attribute *);
1209
1210 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211                                    struct dwarf2_loclist_baton *baton,
1212                                    struct attribute *attr);
1213
1214 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215                                          struct symbol *sym,
1216                                          struct dwarf2_cu *cu);
1217
1218 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219                                struct abbrev_info *abbrev,
1220                                struct dwarf2_cu *cu);
1221
1222 static void free_stack_comp_unit (void *);
1223
1224 static hashval_t partial_die_hash (const void *item);
1225
1226 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
1228 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1229   (unsigned int offset, struct objfile *objfile);
1230
1231 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1232   (unsigned int offset, struct objfile *objfile);
1233
1234 static void init_one_comp_unit (struct dwarf2_cu *cu,
1235                                 struct objfile *objfile);
1236
1237 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1238                                    struct die_info *comp_unit_die);
1239
1240 static void free_one_comp_unit (void *);
1241
1242 static void free_cached_comp_units (void *);
1243
1244 static void age_cached_comp_units (void);
1245
1246 static void free_one_cached_comp_unit (void *);
1247
1248 static struct type *set_die_type (struct die_info *, struct type *,
1249                                   struct dwarf2_cu *);
1250
1251 static void create_all_comp_units (struct objfile *);
1252
1253 static int create_debug_types_hash_table (struct objfile *objfile);
1254
1255 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1256                                  struct objfile *);
1257
1258 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1259
1260 static void dwarf2_add_dependence (struct dwarf2_cu *,
1261                                    struct dwarf2_per_cu_data *);
1262
1263 static void dwarf2_mark (struct dwarf2_cu *);
1264
1265 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1266
1267 static struct type *get_die_type_at_offset (unsigned int,
1268                                             struct dwarf2_per_cu_data *per_cu);
1269
1270 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1271
1272 static void dwarf2_release_queue (void *dummy);
1273
1274 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1275                              struct objfile *objfile);
1276
1277 static void process_queue (struct objfile *objfile);
1278
1279 static void find_file_and_directory (struct die_info *die,
1280                                      struct dwarf2_cu *cu,
1281                                      char **name, char **comp_dir);
1282
1283 static char *file_full_name (int file, struct line_header *lh,
1284                              const char *comp_dir);
1285
1286 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1287                                               gdb_byte *info_ptr,
1288                                               gdb_byte *buffer,
1289                                               unsigned int buffer_size,
1290                                               bfd *abfd);
1291
1292 static void init_cu_die_reader (struct die_reader_specs *reader,
1293                                 struct dwarf2_cu *cu);
1294
1295 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1296
1297 #if WORDS_BIGENDIAN
1298
1299 /* Convert VALUE between big- and little-endian.  */
1300 static offset_type
1301 byte_swap (offset_type value)
1302 {
1303   offset_type result;
1304
1305   result = (value & 0xff) << 24;
1306   result |= (value & 0xff00) << 8;
1307   result |= (value & 0xff0000) >> 8;
1308   result |= (value & 0xff000000) >> 24;
1309   return result;
1310 }
1311
1312 #define MAYBE_SWAP(V)  byte_swap (V)
1313
1314 #else
1315 #define MAYBE_SWAP(V) (V)
1316 #endif /* WORDS_BIGENDIAN */
1317
1318 /* The suffix for an index file.  */
1319 #define INDEX_SUFFIX ".gdb-index"
1320
1321 static const char *dwarf2_physname (char *name, struct die_info *die,
1322                                     struct dwarf2_cu *cu);
1323
1324 /* Try to locate the sections we need for DWARF 2 debugging
1325    information and return true if we have enough to do something.  */
1326
1327 int
1328 dwarf2_has_info (struct objfile *objfile)
1329 {
1330   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1331   if (!dwarf2_per_objfile)
1332     {
1333       /* Initialize per-objfile state.  */
1334       struct dwarf2_per_objfile *data
1335         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1336
1337       memset (data, 0, sizeof (*data));
1338       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1339       dwarf2_per_objfile = data;
1340
1341       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1342       dwarf2_per_objfile->objfile = objfile;
1343     }
1344   return (dwarf2_per_objfile->info.asection != NULL
1345           && dwarf2_per_objfile->abbrev.asection != NULL);
1346 }
1347
1348 /* When loading sections, we can either look for ".<name>", or for
1349  * ".z<name>", which indicates a compressed section.  */
1350
1351 static int
1352 section_is_p (const char *section_name, const char *name)
1353 {
1354   return (section_name[0] == '.'
1355           && (strcmp (section_name + 1, name) == 0
1356               || (section_name[1] == 'z'
1357                   && strcmp (section_name + 2, name) == 0)));
1358 }
1359
1360 /* This function is mapped across the sections and remembers the
1361    offset and size of each of the debugging sections we are interested
1362    in.  */
1363
1364 static void
1365 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1366 {
1367   if (section_is_p (sectp->name, INFO_SECTION))
1368     {
1369       dwarf2_per_objfile->info.asection = sectp;
1370       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1371     }
1372   else if (section_is_p (sectp->name, ABBREV_SECTION))
1373     {
1374       dwarf2_per_objfile->abbrev.asection = sectp;
1375       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1376     }
1377   else if (section_is_p (sectp->name, LINE_SECTION))
1378     {
1379       dwarf2_per_objfile->line.asection = sectp;
1380       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1381     }
1382   else if (section_is_p (sectp->name, LOC_SECTION))
1383     {
1384       dwarf2_per_objfile->loc.asection = sectp;
1385       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1386     }
1387   else if (section_is_p (sectp->name, MACINFO_SECTION))
1388     {
1389       dwarf2_per_objfile->macinfo.asection = sectp;
1390       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1391     }
1392   else if (section_is_p (sectp->name, STR_SECTION))
1393     {
1394       dwarf2_per_objfile->str.asection = sectp;
1395       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1396     }
1397   else if (section_is_p (sectp->name, FRAME_SECTION))
1398     {
1399       dwarf2_per_objfile->frame.asection = sectp;
1400       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1401     }
1402   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1403     {
1404       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1405
1406       if (aflag & SEC_HAS_CONTENTS)
1407         {
1408           dwarf2_per_objfile->eh_frame.asection = sectp;
1409           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1410         }
1411     }
1412   else if (section_is_p (sectp->name, RANGES_SECTION))
1413     {
1414       dwarf2_per_objfile->ranges.asection = sectp;
1415       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1416     }
1417   else if (section_is_p (sectp->name, TYPES_SECTION))
1418     {
1419       dwarf2_per_objfile->types.asection = sectp;
1420       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1421     }
1422   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1423     {
1424       dwarf2_per_objfile->gdb_index.asection = sectp;
1425       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1426     }
1427
1428   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1429       && bfd_section_vma (abfd, sectp) == 0)
1430     dwarf2_per_objfile->has_section_at_zero = 1;
1431 }
1432
1433 /* Decompress a section that was compressed using zlib.  Store the
1434    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1435
1436 static void
1437 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1438                          gdb_byte **outbuf, bfd_size_type *outsize)
1439 {
1440   bfd *abfd = objfile->obfd;
1441 #ifndef HAVE_ZLIB_H
1442   error (_("Support for zlib-compressed DWARF data (from '%s') "
1443            "is disabled in this copy of GDB"),
1444          bfd_get_filename (abfd));
1445 #else
1446   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1447   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1448   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1449   bfd_size_type uncompressed_size;
1450   gdb_byte *uncompressed_buffer;
1451   z_stream strm;
1452   int rc;
1453   int header_size = 12;
1454
1455   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1456       || bfd_bread (compressed_buffer,
1457                     compressed_size, abfd) != compressed_size)
1458     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1459            bfd_get_filename (abfd));
1460
1461   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1462      by the uncompressed section size, 8 bytes in big-endian order.  */
1463   if (compressed_size < header_size
1464       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1465     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1466            bfd_get_filename (abfd));
1467   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1468   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1469   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1470   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1471   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1472   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1473   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1474   uncompressed_size += compressed_buffer[11];
1475
1476   /* It is possible the section consists of several compressed
1477      buffers concatenated together, so we uncompress in a loop.  */
1478   strm.zalloc = NULL;
1479   strm.zfree = NULL;
1480   strm.opaque = NULL;
1481   strm.avail_in = compressed_size - header_size;
1482   strm.next_in = (Bytef*) compressed_buffer + header_size;
1483   strm.avail_out = uncompressed_size;
1484   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1485                                        uncompressed_size);
1486   rc = inflateInit (&strm);
1487   while (strm.avail_in > 0)
1488     {
1489       if (rc != Z_OK)
1490         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1491                bfd_get_filename (abfd), rc);
1492       strm.next_out = ((Bytef*) uncompressed_buffer
1493                        + (uncompressed_size - strm.avail_out));
1494       rc = inflate (&strm, Z_FINISH);
1495       if (rc != Z_STREAM_END)
1496         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1497                bfd_get_filename (abfd), rc);
1498       rc = inflateReset (&strm);
1499     }
1500   rc = inflateEnd (&strm);
1501   if (rc != Z_OK
1502       || strm.avail_out != 0)
1503     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1504            bfd_get_filename (abfd), rc);
1505
1506   do_cleanups (cleanup);
1507   *outbuf = uncompressed_buffer;
1508   *outsize = uncompressed_size;
1509 #endif
1510 }
1511
1512 /* A helper function that decides whether a section is empty.  */
1513
1514 static int
1515 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1516 {
1517   return info->asection == NULL || info->size == 0;
1518 }
1519
1520 /* Read the contents of the section SECTP from object file specified by
1521    OBJFILE, store info about the section into INFO.
1522    If the section is compressed, uncompress it before returning.  */
1523
1524 static void
1525 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1526 {
1527   bfd *abfd = objfile->obfd;
1528   asection *sectp = info->asection;
1529   gdb_byte *buf, *retbuf;
1530   unsigned char header[4];
1531
1532   if (info->readin)
1533     return;
1534   info->buffer = NULL;
1535   info->was_mmapped = 0;
1536   info->readin = 1;
1537
1538   if (dwarf2_section_empty_p (info))
1539     return;
1540
1541   /* Check if the file has a 4-byte header indicating compression.  */
1542   if (info->size > sizeof (header)
1543       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1544       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1545     {
1546       /* Upon decompression, update the buffer and its size.  */
1547       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1548         {
1549           zlib_decompress_section (objfile, sectp, &info->buffer,
1550                                    &info->size);
1551           return;
1552         }
1553     }
1554
1555 #ifdef HAVE_MMAP
1556   if (pagesize == 0)
1557     pagesize = getpagesize ();
1558
1559   /* Only try to mmap sections which are large enough: we don't want to
1560      waste space due to fragmentation.  Also, only try mmap for sections
1561      without relocations.  */
1562
1563   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1564     {
1565       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1566       size_t map_length = info->size + sectp->filepos - pg_offset;
1567       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1568                                  MAP_PRIVATE, pg_offset);
1569
1570       if (retbuf != MAP_FAILED)
1571         {
1572           info->was_mmapped = 1;
1573           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1574 #if HAVE_POSIX_MADVISE
1575           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1576 #endif
1577           return;
1578         }
1579     }
1580 #endif
1581
1582   /* If we get here, we are a normal, not-compressed section.  */
1583   info->buffer = buf
1584     = obstack_alloc (&objfile->objfile_obstack, info->size);
1585
1586   /* When debugging .o files, we may need to apply relocations; see
1587      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1588      We never compress sections in .o files, so we only need to
1589      try this when the section is not compressed.  */
1590   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1591   if (retbuf != NULL)
1592     {
1593       info->buffer = retbuf;
1594       return;
1595     }
1596
1597   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1598       || bfd_bread (buf, info->size, abfd) != info->size)
1599     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1600            bfd_get_filename (abfd));
1601 }
1602
1603 /* A helper function that returns the size of a section in a safe way.
1604    If you are positive that the section has been read before using the
1605    size, then it is safe to refer to the dwarf2_section_info object's
1606    "size" field directly.  In other cases, you must call this
1607    function, because for compressed sections the size field is not set
1608    correctly until the section has been read.  */
1609
1610 static bfd_size_type
1611 dwarf2_section_size (struct objfile *objfile,
1612                      struct dwarf2_section_info *info)
1613 {
1614   if (!info->readin)
1615     dwarf2_read_section (objfile, info);
1616   return info->size;
1617 }
1618
1619 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1620    SECTION_NAME.  */
1621
1622 void
1623 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1624                          asection **sectp, gdb_byte **bufp,
1625                          bfd_size_type *sizep)
1626 {
1627   struct dwarf2_per_objfile *data
1628     = objfile_data (objfile, dwarf2_objfile_data_key);
1629   struct dwarf2_section_info *info;
1630
1631   /* We may see an objfile without any DWARF, in which case we just
1632      return nothing.  */
1633   if (data == NULL)
1634     {
1635       *sectp = NULL;
1636       *bufp = NULL;
1637       *sizep = 0;
1638       return;
1639     }
1640   if (section_is_p (section_name, EH_FRAME_SECTION))
1641     info = &data->eh_frame;
1642   else if (section_is_p (section_name, FRAME_SECTION))
1643     info = &data->frame;
1644   else
1645     gdb_assert_not_reached ("unexpected section");
1646
1647   dwarf2_read_section (objfile, info);
1648
1649   *sectp = info->asection;
1650   *bufp = info->buffer;
1651   *sizep = info->size;
1652 }
1653
1654 \f
1655 /* DWARF quick_symbols_functions support.  */
1656
1657 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1658    unique line tables, so we maintain a separate table of all .debug_line
1659    derived entries to support the sharing.
1660    All the quick functions need is the list of file names.  We discard the
1661    line_header when we're done and don't need to record it here.  */
1662 struct quick_file_names
1663 {
1664   /* The offset in .debug_line of the line table.  We hash on this.  */
1665   unsigned int offset;
1666
1667   /* The number of entries in file_names, real_names.  */
1668   unsigned int num_file_names;
1669
1670   /* The file names from the line table, after being run through
1671      file_full_name.  */
1672   const char **file_names;
1673
1674   /* The file names from the line table after being run through
1675      gdb_realpath.  These are computed lazily.  */
1676   const char **real_names;
1677 };
1678
1679 /* When using the index (and thus not using psymtabs), each CU has an
1680    object of this type.  This is used to hold information needed by
1681    the various "quick" methods.  */
1682 struct dwarf2_per_cu_quick_data
1683 {
1684   /* The file table.  This can be NULL if there was no file table
1685      or it's currently not read in.
1686      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1687   struct quick_file_names *file_names;
1688
1689   /* The corresponding symbol table.  This is NULL if symbols for this
1690      CU have not yet been read.  */
1691   struct symtab *symtab;
1692
1693   /* A temporary mark bit used when iterating over all CUs in
1694      expand_symtabs_matching.  */
1695   unsigned int mark : 1;
1696
1697   /* True if we've tried to read the file table and found there isn't one.
1698      There will be no point in trying to read it again next time.  */
1699   unsigned int no_file_data : 1;
1700 };
1701
1702 /* Hash function for a quick_file_names.  */
1703
1704 static hashval_t
1705 hash_file_name_entry (const void *e)
1706 {
1707   const struct quick_file_names *file_data = e;
1708
1709   return file_data->offset;
1710 }
1711
1712 /* Equality function for a quick_file_names.  */
1713
1714 static int
1715 eq_file_name_entry (const void *a, const void *b)
1716 {
1717   const struct quick_file_names *ea = a;
1718   const struct quick_file_names *eb = b;
1719
1720   return ea->offset == eb->offset;
1721 }
1722
1723 /* Delete function for a quick_file_names.  */
1724
1725 static void
1726 delete_file_name_entry (void *e)
1727 {
1728   struct quick_file_names *file_data = e;
1729   int i;
1730
1731   for (i = 0; i < file_data->num_file_names; ++i)
1732     {
1733       xfree ((void*) file_data->file_names[i]);
1734       if (file_data->real_names)
1735         xfree ((void*) file_data->real_names[i]);
1736     }
1737
1738   /* The space for the struct itself lives on objfile_obstack,
1739      so we don't free it here.  */
1740 }
1741
1742 /* Create a quick_file_names hash table.  */
1743
1744 static htab_t
1745 create_quick_file_names_table (unsigned int nr_initial_entries)
1746 {
1747   return htab_create_alloc (nr_initial_entries,
1748                             hash_file_name_entry, eq_file_name_entry,
1749                             delete_file_name_entry, xcalloc, xfree);
1750 }
1751
1752 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1753    this CU came.  */
1754
1755 static void
1756 dw2_do_instantiate_symtab (struct objfile *objfile,
1757                            struct dwarf2_per_cu_data *per_cu)
1758 {
1759   struct cleanup *back_to;
1760
1761   back_to = make_cleanup (dwarf2_release_queue, NULL);
1762
1763   queue_comp_unit (per_cu, objfile);
1764
1765   if (per_cu->from_debug_types)
1766     read_signatured_type_at_offset (objfile, per_cu->offset);
1767   else
1768     load_full_comp_unit (per_cu, objfile);
1769
1770   process_queue (objfile);
1771
1772   /* Age the cache, releasing compilation units that have not
1773      been used recently.  */
1774   age_cached_comp_units ();
1775
1776   do_cleanups (back_to);
1777 }
1778
1779 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1780    the objfile from which this CU came.  Returns the resulting symbol
1781    table.  */
1782
1783 static struct symtab *
1784 dw2_instantiate_symtab (struct objfile *objfile,
1785                         struct dwarf2_per_cu_data *per_cu)
1786 {
1787   if (!per_cu->v.quick->symtab)
1788     {
1789       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1790       increment_reading_symtab ();
1791       dw2_do_instantiate_symtab (objfile, per_cu);
1792       do_cleanups (back_to);
1793     }
1794   return per_cu->v.quick->symtab;
1795 }
1796
1797 /* Return the CU given its index.  */
1798
1799 static struct dwarf2_per_cu_data *
1800 dw2_get_cu (int index)
1801 {
1802   if (index >= dwarf2_per_objfile->n_comp_units)
1803     {
1804       index -= dwarf2_per_objfile->n_comp_units;
1805       return dwarf2_per_objfile->type_comp_units[index];
1806     }
1807   return dwarf2_per_objfile->all_comp_units[index];
1808 }
1809
1810 /* A helper function that knows how to read a 64-bit value in a way
1811    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1812    otherwise.  */
1813
1814 static int
1815 extract_cu_value (const char *bytes, ULONGEST *result)
1816 {
1817   if (sizeof (ULONGEST) < 8)
1818     {
1819       int i;
1820
1821       /* Ignore the upper 4 bytes if they are all zero.  */
1822       for (i = 0; i < 4; ++i)
1823         if (bytes[i + 4] != 0)
1824           return 0;
1825
1826       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1827     }
1828   else
1829     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1830   return 1;
1831 }
1832
1833 /* Read the CU list from the mapped index, and use it to create all
1834    the CU objects for this objfile.  Return 0 if something went wrong,
1835    1 if everything went ok.  */
1836
1837 static int
1838 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1839                        offset_type cu_list_elements)
1840 {
1841   offset_type i;
1842
1843   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1844   dwarf2_per_objfile->all_comp_units
1845     = obstack_alloc (&objfile->objfile_obstack,
1846                      dwarf2_per_objfile->n_comp_units
1847                      * sizeof (struct dwarf2_per_cu_data *));
1848
1849   for (i = 0; i < cu_list_elements; i += 2)
1850     {
1851       struct dwarf2_per_cu_data *the_cu;
1852       ULONGEST offset, length;
1853
1854       if (!extract_cu_value (cu_list, &offset)
1855           || !extract_cu_value (cu_list + 8, &length))
1856         return 0;
1857       cu_list += 2 * 8;
1858
1859       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1860                                struct dwarf2_per_cu_data);
1861       the_cu->offset = offset;
1862       the_cu->length = length;
1863       the_cu->objfile = objfile;
1864       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1865                                         struct dwarf2_per_cu_quick_data);
1866       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1867     }
1868
1869   return 1;
1870 }
1871
1872 /* Create the signatured type hash table from the index.  */
1873
1874 static int
1875 create_signatured_type_table_from_index (struct objfile *objfile,
1876                                          const gdb_byte *bytes,
1877                                          offset_type elements)
1878 {
1879   offset_type i;
1880   htab_t sig_types_hash;
1881
1882   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1883   dwarf2_per_objfile->type_comp_units
1884     = obstack_alloc (&objfile->objfile_obstack,
1885                      dwarf2_per_objfile->n_type_comp_units
1886                      * sizeof (struct dwarf2_per_cu_data *));
1887
1888   sig_types_hash = allocate_signatured_type_table (objfile);
1889
1890   for (i = 0; i < elements; i += 3)
1891     {
1892       struct signatured_type *type_sig;
1893       ULONGEST offset, type_offset, signature;
1894       void **slot;
1895
1896       if (!extract_cu_value (bytes, &offset)
1897           || !extract_cu_value (bytes + 8, &type_offset))
1898         return 0;
1899       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1900       bytes += 3 * 8;
1901
1902       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1903                                  struct signatured_type);
1904       type_sig->signature = signature;
1905       type_sig->offset = offset;
1906       type_sig->type_offset = type_offset;
1907       type_sig->per_cu.from_debug_types = 1;
1908       type_sig->per_cu.offset = offset;
1909       type_sig->per_cu.objfile = objfile;
1910       type_sig->per_cu.v.quick
1911         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1912                           struct dwarf2_per_cu_quick_data);
1913
1914       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1915       *slot = type_sig;
1916
1917       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1918     }
1919
1920   dwarf2_per_objfile->signatured_types = sig_types_hash;
1921
1922   return 1;
1923 }
1924
1925 /* Read the address map data from the mapped index, and use it to
1926    populate the objfile's psymtabs_addrmap.  */
1927
1928 static void
1929 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1930 {
1931   const gdb_byte *iter, *end;
1932   struct obstack temp_obstack;
1933   struct addrmap *mutable_map;
1934   struct cleanup *cleanup;
1935   CORE_ADDR baseaddr;
1936
1937   obstack_init (&temp_obstack);
1938   cleanup = make_cleanup_obstack_free (&temp_obstack);
1939   mutable_map = addrmap_create_mutable (&temp_obstack);
1940
1941   iter = index->address_table;
1942   end = iter + index->address_table_size;
1943
1944   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1945
1946   while (iter < end)
1947     {
1948       ULONGEST hi, lo, cu_index;
1949       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1950       iter += 8;
1951       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1952       iter += 8;
1953       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1954       iter += 4;
1955       
1956       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1957                          dw2_get_cu (cu_index));
1958     }
1959
1960   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1961                                                     &objfile->objfile_obstack);
1962   do_cleanups (cleanup);
1963 }
1964
1965 /* The hash function for strings in the mapped index.  This is the
1966    same as the hashtab.c hash function, but we keep a separate copy to
1967    maintain control over the implementation.  This is necessary
1968    because the hash function is tied to the format of the mapped index
1969    file.  */
1970
1971 static hashval_t
1972 mapped_index_string_hash (const void *p)
1973 {
1974   const unsigned char *str = (const unsigned char *) p;
1975   hashval_t r = 0;
1976   unsigned char c;
1977
1978   while ((c = *str++) != 0)
1979     r = r * 67 + c - 113;
1980
1981   return r;
1982 }
1983
1984 /* Find a slot in the mapped index INDEX for the object named NAME.
1985    If NAME is found, set *VEC_OUT to point to the CU vector in the
1986    constant pool and return 1.  If NAME cannot be found, return 0.  */
1987
1988 static int
1989 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1990                           offset_type **vec_out)
1991 {
1992   offset_type hash = mapped_index_string_hash (name);
1993   offset_type slot, step;
1994
1995   slot = hash & (index->symbol_table_slots - 1);
1996   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
1997
1998   for (;;)
1999     {
2000       /* Convert a slot number to an offset into the table.  */
2001       offset_type i = 2 * slot;
2002       const char *str;
2003       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2004         return 0;
2005
2006       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2007       if (!strcmp (name, str))
2008         {
2009           *vec_out = (offset_type *) (index->constant_pool
2010                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2011           return 1;
2012         }
2013
2014       slot = (slot + step) & (index->symbol_table_slots - 1);
2015     }
2016 }
2017
2018 /* Read the index file.  If everything went ok, initialize the "quick"
2019    elements of all the CUs and return 1.  Otherwise, return 0.  */
2020
2021 static int
2022 dwarf2_read_index (struct objfile *objfile)
2023 {
2024   char *addr;
2025   struct mapped_index *map;
2026   offset_type *metadata;
2027   const gdb_byte *cu_list;
2028   const gdb_byte *types_list = NULL;
2029   offset_type version, cu_list_elements;
2030   offset_type types_list_elements = 0;
2031   int i;
2032
2033   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2034     return 0;
2035
2036   /* Older elfutils strip versions could keep the section in the main
2037      executable while splitting it for the separate debug info file.  */
2038   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2039        & SEC_HAS_CONTENTS) == 0)
2040     return 0;
2041
2042   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2043
2044   addr = dwarf2_per_objfile->gdb_index.buffer;
2045   /* Version check.  */
2046   version = MAYBE_SWAP (*(offset_type *) addr);
2047   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2048      causes the index to behave very poorly for certain requests.  Version 4
2049      contained incomplete addrmap.  So, it seems better to just ignore such
2050      indices.  */
2051   if (version < 4)
2052     return 0;
2053   /* Indexes with higher version than the one supported by GDB may be no
2054      longer backward compatible.  */
2055   if (version > 4)
2056     return 0;
2057
2058   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2059   map->total_size = dwarf2_per_objfile->gdb_index.size;
2060
2061   metadata = (offset_type *) (addr + sizeof (offset_type));
2062
2063   i = 0;
2064   cu_list = addr + MAYBE_SWAP (metadata[i]);
2065   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2066                       / 8);
2067   ++i;
2068
2069   types_list = addr + MAYBE_SWAP (metadata[i]);
2070   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2071                           - MAYBE_SWAP (metadata[i]))
2072                          / 8);
2073   ++i;
2074
2075   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2076   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2077                              - MAYBE_SWAP (metadata[i]));
2078   ++i;
2079
2080   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2081   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2082                               - MAYBE_SWAP (metadata[i]))
2083                              / (2 * sizeof (offset_type)));
2084   ++i;
2085
2086   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2087
2088   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2089     return 0;
2090
2091   if (types_list_elements
2092       && !create_signatured_type_table_from_index (objfile, types_list,
2093                                                    types_list_elements))
2094     return 0;
2095
2096   create_addrmap_from_index (objfile, map);
2097
2098   dwarf2_per_objfile->index_table = map;
2099   dwarf2_per_objfile->using_index = 1;
2100   dwarf2_per_objfile->quick_file_names_table =
2101     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2102
2103   return 1;
2104 }
2105
2106 /* A helper for the "quick" functions which sets the global
2107    dwarf2_per_objfile according to OBJFILE.  */
2108
2109 static void
2110 dw2_setup (struct objfile *objfile)
2111 {
2112   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2113   gdb_assert (dwarf2_per_objfile);
2114 }
2115
2116 /* A helper for the "quick" functions which attempts to read the line
2117    table for THIS_CU.  */
2118
2119 static struct quick_file_names *
2120 dw2_get_file_names (struct objfile *objfile,
2121                     struct dwarf2_per_cu_data *this_cu)
2122 {
2123   bfd *abfd = objfile->obfd;
2124   struct line_header *lh;
2125   struct attribute *attr;
2126   struct cleanup *cleanups;
2127   struct die_info *comp_unit_die;
2128   struct dwarf2_section_info* sec;
2129   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2130   int has_children, i;
2131   struct dwarf2_cu cu;
2132   unsigned int bytes_read, buffer_size;
2133   struct die_reader_specs reader_specs;
2134   char *name, *comp_dir;
2135   void **slot;
2136   struct quick_file_names *qfn;
2137   unsigned int line_offset;
2138
2139   if (this_cu->v.quick->file_names != NULL)
2140     return this_cu->v.quick->file_names;
2141   /* If we know there is no line data, no point in looking again.  */
2142   if (this_cu->v.quick->no_file_data)
2143     return NULL;
2144
2145   init_one_comp_unit (&cu, objfile);
2146   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2147
2148   if (this_cu->from_debug_types)
2149     sec = &dwarf2_per_objfile->types;
2150   else
2151     sec = &dwarf2_per_objfile->info;
2152   dwarf2_read_section (objfile, sec);
2153   buffer_size = sec->size;
2154   buffer = sec->buffer;
2155   info_ptr = buffer + this_cu->offset;
2156   beg_of_comp_unit = info_ptr;
2157
2158   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2159                                           buffer, buffer_size,
2160                                           abfd);
2161
2162   /* Complete the cu_header.  */
2163   cu.header.offset = beg_of_comp_unit - buffer;
2164   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2165
2166   this_cu->cu = &cu;
2167   cu.per_cu = this_cu;
2168
2169   dwarf2_read_abbrevs (abfd, &cu);
2170   make_cleanup (dwarf2_free_abbrev_table, &cu);
2171
2172   if (this_cu->from_debug_types)
2173     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2174   init_cu_die_reader (&reader_specs, &cu);
2175   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2176                  &has_children);
2177
2178   lh = NULL;
2179   slot = NULL;
2180   line_offset = 0;
2181   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2182   if (attr)
2183     {
2184       struct quick_file_names find_entry;
2185
2186       line_offset = DW_UNSND (attr);
2187
2188       /* We may have already read in this line header (TU line header sharing).
2189          If we have we're done.  */
2190       find_entry.offset = line_offset;
2191       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2192                              &find_entry, INSERT);
2193       if (*slot != NULL)
2194         {
2195           do_cleanups (cleanups);
2196           this_cu->v.quick->file_names = *slot;
2197           return *slot;
2198         }
2199
2200       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2201     }
2202   if (lh == NULL)
2203     {
2204       do_cleanups (cleanups);
2205       this_cu->v.quick->no_file_data = 1;
2206       return NULL;
2207     }
2208
2209   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2210   qfn->offset = line_offset;
2211   gdb_assert (slot != NULL);
2212   *slot = qfn;
2213
2214   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2215
2216   qfn->num_file_names = lh->num_file_names;
2217   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2218                                    lh->num_file_names * sizeof (char *));
2219   for (i = 0; i < lh->num_file_names; ++i)
2220     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2221   qfn->real_names = NULL;
2222
2223   free_line_header (lh);
2224   do_cleanups (cleanups);
2225
2226   this_cu->v.quick->file_names = qfn;
2227   return qfn;
2228 }
2229
2230 /* A helper for the "quick" functions which computes and caches the
2231    real path for a given file name from the line table.  */
2232
2233 static const char *
2234 dw2_get_real_path (struct objfile *objfile,
2235                    struct quick_file_names *qfn, int index)
2236 {
2237   if (qfn->real_names == NULL)
2238     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2239                                       qfn->num_file_names, sizeof (char *));
2240
2241   if (qfn->real_names[index] == NULL)
2242     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2243
2244   return qfn->real_names[index];
2245 }
2246
2247 static struct symtab *
2248 dw2_find_last_source_symtab (struct objfile *objfile)
2249 {
2250   int index;
2251
2252   dw2_setup (objfile);
2253   index = dwarf2_per_objfile->n_comp_units - 1;
2254   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2255 }
2256
2257 /* Traversal function for dw2_forget_cached_source_info.  */
2258
2259 static int
2260 dw2_free_cached_file_names (void **slot, void *info)
2261 {
2262   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2263
2264   if (file_data->real_names)
2265     {
2266       int i;
2267
2268       for (i = 0; i < file_data->num_file_names; ++i)
2269         {
2270           xfree ((void*) file_data->real_names[i]);
2271           file_data->real_names[i] = NULL;
2272         }
2273     }
2274
2275   return 1;
2276 }
2277
2278 static void
2279 dw2_forget_cached_source_info (struct objfile *objfile)
2280 {
2281   dw2_setup (objfile);
2282
2283   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2284                           dw2_free_cached_file_names, NULL);
2285 }
2286
2287 static int
2288 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2289                    const char *full_path, const char *real_path,
2290                    struct symtab **result)
2291 {
2292   int i;
2293   int check_basename = lbasename (name) == name;
2294   struct dwarf2_per_cu_data *base_cu = NULL;
2295
2296   dw2_setup (objfile);
2297
2298   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2299                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2300     {
2301       int j;
2302       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2303       struct quick_file_names *file_data;
2304
2305       if (per_cu->v.quick->symtab)
2306         continue;
2307
2308       file_data = dw2_get_file_names (objfile, per_cu);
2309       if (file_data == NULL)
2310         continue;
2311
2312       for (j = 0; j < file_data->num_file_names; ++j)
2313         {
2314           const char *this_name = file_data->file_names[j];
2315
2316           if (FILENAME_CMP (name, this_name) == 0)
2317             {
2318               *result = dw2_instantiate_symtab (objfile, per_cu);
2319               return 1;
2320             }
2321
2322           if (check_basename && ! base_cu
2323               && FILENAME_CMP (lbasename (this_name), name) == 0)
2324             base_cu = per_cu;
2325
2326           if (full_path != NULL)
2327             {
2328               const char *this_real_name = dw2_get_real_path (objfile,
2329                                                               file_data, j);
2330
2331               if (this_real_name != NULL
2332                   && FILENAME_CMP (full_path, this_real_name) == 0)
2333                 {
2334                   *result = dw2_instantiate_symtab (objfile, per_cu);
2335                   return 1;
2336                 }
2337             }
2338
2339           if (real_path != NULL)
2340             {
2341               const char *this_real_name = dw2_get_real_path (objfile,
2342                                                               file_data, j);
2343
2344               if (this_real_name != NULL
2345                   && FILENAME_CMP (real_path, this_real_name) == 0)
2346                 {
2347                   *result = dw2_instantiate_symtab (objfile, per_cu);
2348                   return 1;
2349                 }
2350             }
2351         }
2352     }
2353
2354   if (base_cu)
2355     {
2356       *result = dw2_instantiate_symtab (objfile, base_cu);
2357       return 1;
2358     }
2359
2360   return 0;
2361 }
2362
2363 static struct symtab *
2364 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2365                    const char *name, domain_enum domain)
2366 {
2367   /* We do all the work in the pre_expand_symtabs_matching hook
2368      instead.  */
2369   return NULL;
2370 }
2371
2372 /* A helper function that expands all symtabs that hold an object
2373    named NAME.  */
2374
2375 static void
2376 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2377 {
2378   dw2_setup (objfile);
2379
2380   /* index_table is NULL if OBJF_READNOW.  */
2381   if (dwarf2_per_objfile->index_table)
2382     {
2383       offset_type *vec;
2384
2385       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2386                                     name, &vec))
2387         {
2388           offset_type i, len = MAYBE_SWAP (*vec);
2389           for (i = 0; i < len; ++i)
2390             {
2391               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2392               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2393
2394               dw2_instantiate_symtab (objfile, per_cu);
2395             }
2396         }
2397     }
2398 }
2399
2400 static void
2401 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2402                                  int kind, const char *name,
2403                                  domain_enum domain)
2404 {
2405   dw2_do_expand_symtabs_matching (objfile, name);
2406 }
2407
2408 static void
2409 dw2_print_stats (struct objfile *objfile)
2410 {
2411   int i, count;
2412
2413   dw2_setup (objfile);
2414   count = 0;
2415   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2417     {
2418       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2419
2420       if (!per_cu->v.quick->symtab)
2421         ++count;
2422     }
2423   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2424 }
2425
2426 static void
2427 dw2_dump (struct objfile *objfile)
2428 {
2429   /* Nothing worth printing.  */
2430 }
2431
2432 static void
2433 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2434               struct section_offsets *delta)
2435 {
2436   /* There's nothing to relocate here.  */
2437 }
2438
2439 static void
2440 dw2_expand_symtabs_for_function (struct objfile *objfile,
2441                                  const char *func_name)
2442 {
2443   dw2_do_expand_symtabs_matching (objfile, func_name);
2444 }
2445
2446 static void
2447 dw2_expand_all_symtabs (struct objfile *objfile)
2448 {
2449   int i;
2450
2451   dw2_setup (objfile);
2452
2453   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2454                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2455     {
2456       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2457
2458       dw2_instantiate_symtab (objfile, per_cu);
2459     }
2460 }
2461
2462 static void
2463 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2464                                   const char *filename)
2465 {
2466   int i;
2467
2468   dw2_setup (objfile);
2469
2470   /* We don't need to consider type units here.
2471      This is only called for examining code, e.g. expand_line_sal.
2472      There can be an order of magnitude (or more) more type units
2473      than comp units, and we avoid them if we can.  */
2474
2475   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2476     {
2477       int j;
2478       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2479       struct quick_file_names *file_data;
2480
2481       if (per_cu->v.quick->symtab)
2482         continue;
2483
2484       file_data = dw2_get_file_names (objfile, per_cu);
2485       if (file_data == NULL)
2486         continue;
2487
2488       for (j = 0; j < file_data->num_file_names; ++j)
2489         {
2490           const char *this_name = file_data->file_names[j];
2491           if (FILENAME_CMP (this_name, filename) == 0)
2492             {
2493               dw2_instantiate_symtab (objfile, per_cu);
2494               break;
2495             }
2496         }
2497     }
2498 }
2499
2500 static const char *
2501 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2502 {
2503   struct dwarf2_per_cu_data *per_cu;
2504   offset_type *vec;
2505   struct quick_file_names *file_data;
2506
2507   dw2_setup (objfile);
2508
2509   /* index_table is NULL if OBJF_READNOW.  */
2510   if (!dwarf2_per_objfile->index_table)
2511     return NULL;
2512
2513   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2514                                  name, &vec))
2515     return NULL;
2516
2517   /* Note that this just looks at the very first one named NAME -- but
2518      actually we are looking for a function.  find_main_filename
2519      should be rewritten so that it doesn't require a custom hook.  It
2520      could just use the ordinary symbol tables.  */
2521   /* vec[0] is the length, which must always be >0.  */
2522   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2523
2524   file_data = dw2_get_file_names (objfile, per_cu);
2525   if (file_data == NULL)
2526     return NULL;
2527
2528   return file_data->file_names[file_data->num_file_names - 1];
2529 }
2530
2531 static void
2532 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2533                           struct objfile *objfile, int global,
2534                           int (*callback) (struct block *,
2535                                            struct symbol *, void *),
2536                           void *data, symbol_compare_ftype *match,
2537                           symbol_compare_ftype *ordered_compare)
2538 {
2539   /* Currently unimplemented; used for Ada.  The function can be called if the
2540      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2541      does not look for non-Ada symbols this function should just return.  */
2542 }
2543
2544 static void
2545 dw2_expand_symtabs_matching (struct objfile *objfile,
2546                              int (*file_matcher) (const char *, void *),
2547                              int (*name_matcher) (const char *, void *),
2548                              domain_enum kind,
2549                              void *data)
2550 {
2551   int i;
2552   offset_type iter;
2553   struct mapped_index *index;
2554
2555   dw2_setup (objfile);
2556
2557   /* index_table is NULL if OBJF_READNOW.  */
2558   if (!dwarf2_per_objfile->index_table)
2559     return;
2560   index = dwarf2_per_objfile->index_table;
2561
2562   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2563                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2564     {
2565       int j;
2566       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2567       struct quick_file_names *file_data;
2568
2569       per_cu->v.quick->mark = 0;
2570       if (per_cu->v.quick->symtab)
2571         continue;
2572
2573       file_data = dw2_get_file_names (objfile, per_cu);
2574       if (file_data == NULL)
2575         continue;
2576
2577       for (j = 0; j < file_data->num_file_names; ++j)
2578         {
2579           if (file_matcher (file_data->file_names[j], data))
2580             {
2581               per_cu->v.quick->mark = 1;
2582               break;
2583             }
2584         }
2585     }
2586
2587   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2588     {
2589       offset_type idx = 2 * iter;
2590       const char *name;
2591       offset_type *vec, vec_len, vec_idx;
2592
2593       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2594         continue;
2595
2596       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2597
2598       if (! (*name_matcher) (name, data))
2599         continue;
2600
2601       /* The name was matched, now expand corresponding CUs that were
2602          marked.  */
2603       vec = (offset_type *) (index->constant_pool
2604                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2605       vec_len = MAYBE_SWAP (vec[0]);
2606       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2607         {
2608           struct dwarf2_per_cu_data *per_cu;
2609
2610           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2611           if (per_cu->v.quick->mark)
2612             dw2_instantiate_symtab (objfile, per_cu);
2613         }
2614     }
2615 }
2616
2617 static struct symtab *
2618 dw2_find_pc_sect_symtab (struct objfile *objfile,
2619                          struct minimal_symbol *msymbol,
2620                          CORE_ADDR pc,
2621                          struct obj_section *section,
2622                          int warn_if_readin)
2623 {
2624   struct dwarf2_per_cu_data *data;
2625
2626   dw2_setup (objfile);
2627
2628   if (!objfile->psymtabs_addrmap)
2629     return NULL;
2630
2631   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2632   if (!data)
2633     return NULL;
2634
2635   if (warn_if_readin && data->v.quick->symtab)
2636     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2637              paddress (get_objfile_arch (objfile), pc));
2638
2639   return dw2_instantiate_symtab (objfile, data);
2640 }
2641
2642 static void
2643 dw2_map_symbol_names (struct objfile *objfile,
2644                       void (*fun) (const char *, void *),
2645                       void *data)
2646 {
2647   offset_type iter;
2648   struct mapped_index *index;
2649
2650   dw2_setup (objfile);
2651
2652   /* index_table is NULL if OBJF_READNOW.  */
2653   if (!dwarf2_per_objfile->index_table)
2654     return;
2655   index = dwarf2_per_objfile->index_table;
2656
2657   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2658     {
2659       offset_type idx = 2 * iter;
2660       const char *name;
2661       offset_type *vec, vec_len, vec_idx;
2662
2663       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2664         continue;
2665
2666       name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
2667
2668       (*fun) (name, data);
2669     }
2670 }
2671
2672 static void
2673 dw2_map_symbol_filenames (struct objfile *objfile,
2674                           void (*fun) (const char *, const char *, void *),
2675                           void *data)
2676 {
2677   int i;
2678
2679   dw2_setup (objfile);
2680
2681   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2682                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2683     {
2684       int j;
2685       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2686       struct quick_file_names *file_data;
2687
2688       if (per_cu->v.quick->symtab)
2689         continue;
2690
2691       file_data = dw2_get_file_names (objfile, per_cu);
2692       if (file_data == NULL)
2693         continue;
2694
2695       for (j = 0; j < file_data->num_file_names; ++j)
2696         {
2697           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2698                                                           j);
2699           (*fun) (file_data->file_names[j], this_real_name, data);
2700         }
2701     }
2702 }
2703
2704 static int
2705 dw2_has_symbols (struct objfile *objfile)
2706 {
2707   return 1;
2708 }
2709
2710 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2711 {
2712   dw2_has_symbols,
2713   dw2_find_last_source_symtab,
2714   dw2_forget_cached_source_info,
2715   dw2_lookup_symtab,
2716   dw2_lookup_symbol,
2717   dw2_pre_expand_symtabs_matching,
2718   dw2_print_stats,
2719   dw2_dump,
2720   dw2_relocate,
2721   dw2_expand_symtabs_for_function,
2722   dw2_expand_all_symtabs,
2723   dw2_expand_symtabs_with_filename,
2724   dw2_find_symbol_file,
2725   dw2_map_matching_symbols,
2726   dw2_expand_symtabs_matching,
2727   dw2_find_pc_sect_symtab,
2728   dw2_map_symbol_names,
2729   dw2_map_symbol_filenames
2730 };
2731
2732 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2733    file will use psymtabs, or 1 if using the GNU index.  */
2734
2735 int
2736 dwarf2_initialize_objfile (struct objfile *objfile)
2737 {
2738   /* If we're about to read full symbols, don't bother with the
2739      indices.  In this case we also don't care if some other debug
2740      format is making psymtabs, because they are all about to be
2741      expanded anyway.  */
2742   if ((objfile->flags & OBJF_READNOW))
2743     {
2744       int i;
2745
2746       dwarf2_per_objfile->using_index = 1;
2747       create_all_comp_units (objfile);
2748       create_debug_types_hash_table (objfile);
2749       dwarf2_per_objfile->quick_file_names_table =
2750         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2751
2752       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2753                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2754         {
2755           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2756
2757           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2758                                             struct dwarf2_per_cu_quick_data);
2759         }
2760
2761       /* Return 1 so that gdb sees the "quick" functions.  However,
2762          these functions will be no-ops because we will have expanded
2763          all symtabs.  */
2764       return 1;
2765     }
2766
2767   if (dwarf2_read_index (objfile))
2768     return 1;
2769
2770   dwarf2_build_psymtabs (objfile);
2771   return 0;
2772 }
2773
2774 \f
2775
2776 /* Build a partial symbol table.  */
2777
2778 void
2779 dwarf2_build_psymtabs (struct objfile *objfile)
2780 {
2781   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2782     {
2783       init_psymbol_list (objfile, 1024);
2784     }
2785
2786   dwarf2_build_psymtabs_hard (objfile);
2787 }
2788
2789 /* Return TRUE if OFFSET is within CU_HEADER.  */
2790
2791 static inline int
2792 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2793 {
2794   unsigned int bottom = cu_header->offset;
2795   unsigned int top = (cu_header->offset
2796                       + cu_header->length
2797                       + cu_header->initial_length_size);
2798
2799   return (offset >= bottom && offset < top);
2800 }
2801
2802 /* Read in the comp unit header information from the debug_info at info_ptr.
2803    NOTE: This leaves members offset, first_die_offset to be filled in
2804    by the caller.  */
2805
2806 static gdb_byte *
2807 read_comp_unit_head (struct comp_unit_head *cu_header,
2808                      gdb_byte *info_ptr, bfd *abfd)
2809 {
2810   int signed_addr;
2811   unsigned int bytes_read;
2812
2813   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2814   cu_header->initial_length_size = bytes_read;
2815   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2816   info_ptr += bytes_read;
2817   cu_header->version = read_2_bytes (abfd, info_ptr);
2818   info_ptr += 2;
2819   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2820                                           &bytes_read);
2821   info_ptr += bytes_read;
2822   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2823   info_ptr += 1;
2824   signed_addr = bfd_get_sign_extend_vma (abfd);
2825   if (signed_addr < 0)
2826     internal_error (__FILE__, __LINE__,
2827                     _("read_comp_unit_head: dwarf from non elf file"));
2828   cu_header->signed_addr_p = signed_addr;
2829
2830   return info_ptr;
2831 }
2832
2833 static gdb_byte *
2834 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2835                              gdb_byte *buffer, unsigned int buffer_size,
2836                              bfd *abfd)
2837 {
2838   gdb_byte *beg_of_comp_unit = info_ptr;
2839
2840   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2841
2842   if (header->version != 2 && header->version != 3 && header->version != 4)
2843     error (_("Dwarf Error: wrong version in compilation unit header "
2844            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2845            bfd_get_filename (abfd));
2846
2847   if (header->abbrev_offset
2848       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2849                               &dwarf2_per_objfile->abbrev))
2850     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2851            "(offset 0x%lx + 6) [in module %s]"),
2852            (long) header->abbrev_offset,
2853            (long) (beg_of_comp_unit - buffer),
2854            bfd_get_filename (abfd));
2855
2856   if (beg_of_comp_unit + header->length + header->initial_length_size
2857       > buffer + buffer_size)
2858     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2859            "(offset 0x%lx + 0) [in module %s]"),
2860            (long) header->length,
2861            (long) (beg_of_comp_unit - buffer),
2862            bfd_get_filename (abfd));
2863
2864   return info_ptr;
2865 }
2866
2867 /* Read in the types comp unit header information from .debug_types entry at
2868    types_ptr.  The result is a pointer to one past the end of the header.  */
2869
2870 static gdb_byte *
2871 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2872                           ULONGEST *signature,
2873                           gdb_byte *types_ptr, bfd *abfd)
2874 {
2875   gdb_byte *initial_types_ptr = types_ptr;
2876
2877   dwarf2_read_section (dwarf2_per_objfile->objfile,
2878                        &dwarf2_per_objfile->types);
2879   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2880
2881   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2882
2883   *signature = read_8_bytes (abfd, types_ptr);
2884   types_ptr += 8;
2885   types_ptr += cu_header->offset_size;
2886   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2887
2888   return types_ptr;
2889 }
2890
2891 /* Allocate a new partial symtab for file named NAME and mark this new
2892    partial symtab as being an include of PST.  */
2893
2894 static void
2895 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2896                                struct objfile *objfile)
2897 {
2898   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2899
2900   subpst->section_offsets = pst->section_offsets;
2901   subpst->textlow = 0;
2902   subpst->texthigh = 0;
2903
2904   subpst->dependencies = (struct partial_symtab **)
2905     obstack_alloc (&objfile->objfile_obstack,
2906                    sizeof (struct partial_symtab *));
2907   subpst->dependencies[0] = pst;
2908   subpst->number_of_dependencies = 1;
2909
2910   subpst->globals_offset = 0;
2911   subpst->n_global_syms = 0;
2912   subpst->statics_offset = 0;
2913   subpst->n_static_syms = 0;
2914   subpst->symtab = NULL;
2915   subpst->read_symtab = pst->read_symtab;
2916   subpst->readin = 0;
2917
2918   /* No private part is necessary for include psymtabs.  This property
2919      can be used to differentiate between such include psymtabs and
2920      the regular ones.  */
2921   subpst->read_symtab_private = NULL;
2922 }
2923
2924 /* Read the Line Number Program data and extract the list of files
2925    included by the source file represented by PST.  Build an include
2926    partial symtab for each of these included files.  */
2927
2928 static void
2929 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2930                                struct die_info *die,
2931                                struct partial_symtab *pst)
2932 {
2933   struct objfile *objfile = cu->objfile;
2934   bfd *abfd = objfile->obfd;
2935   struct line_header *lh = NULL;
2936   struct attribute *attr;
2937
2938   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2939   if (attr)
2940     {
2941       unsigned int line_offset = DW_UNSND (attr);
2942
2943       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2944     }
2945   if (lh == NULL)
2946     return;  /* No linetable, so no includes.  */
2947
2948   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2949   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2950
2951   free_line_header (lh);
2952 }
2953
2954 static hashval_t
2955 hash_type_signature (const void *item)
2956 {
2957   const struct signatured_type *type_sig = item;
2958
2959   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2960   return type_sig->signature;
2961 }
2962
2963 static int
2964 eq_type_signature (const void *item_lhs, const void *item_rhs)
2965 {
2966   const struct signatured_type *lhs = item_lhs;
2967   const struct signatured_type *rhs = item_rhs;
2968
2969   return lhs->signature == rhs->signature;
2970 }
2971
2972 /* Allocate a hash table for signatured types.  */
2973
2974 static htab_t
2975 allocate_signatured_type_table (struct objfile *objfile)
2976 {
2977   return htab_create_alloc_ex (41,
2978                                hash_type_signature,
2979                                eq_type_signature,
2980                                NULL,
2981                                &objfile->objfile_obstack,
2982                                hashtab_obstack_allocate,
2983                                dummy_obstack_deallocate);
2984 }
2985
2986 /* A helper function to add a signatured type CU to a list.  */
2987
2988 static int
2989 add_signatured_type_cu_to_list (void **slot, void *datum)
2990 {
2991   struct signatured_type *sigt = *slot;
2992   struct dwarf2_per_cu_data ***datap = datum;
2993
2994   **datap = &sigt->per_cu;
2995   ++*datap;
2996
2997   return 1;
2998 }
2999
3000 /* Create the hash table of all entries in the .debug_types section.
3001    The result is zero if there is an error (e.g. missing .debug_types section),
3002    otherwise non-zero.  */
3003
3004 static int
3005 create_debug_types_hash_table (struct objfile *objfile)
3006 {
3007   gdb_byte *info_ptr;
3008   htab_t types_htab;
3009   struct dwarf2_per_cu_data **iter;
3010
3011   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3012   info_ptr = dwarf2_per_objfile->types.buffer;
3013
3014   if (info_ptr == NULL)
3015     {
3016       dwarf2_per_objfile->signatured_types = NULL;
3017       return 0;
3018     }
3019
3020   types_htab = allocate_signatured_type_table (objfile);
3021
3022   if (dwarf2_die_debug)
3023     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3024
3025   while (info_ptr < dwarf2_per_objfile->types.buffer
3026          + dwarf2_per_objfile->types.size)
3027     {
3028       unsigned int offset;
3029       unsigned int offset_size;
3030       unsigned int type_offset;
3031       unsigned int length, initial_length_size;
3032       unsigned short version;
3033       ULONGEST signature;
3034       struct signatured_type *type_sig;
3035       void **slot;
3036       gdb_byte *ptr = info_ptr;
3037
3038       offset = ptr - dwarf2_per_objfile->types.buffer;
3039
3040       /* We need to read the type's signature in order to build the hash
3041          table, but we don't need to read anything else just yet.  */
3042
3043       /* Sanity check to ensure entire cu is present.  */
3044       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3045       if (ptr + length + initial_length_size
3046           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3047         {
3048           complaint (&symfile_complaints,
3049                      _("debug type entry runs off end "
3050                        "of `.debug_types' section, ignored"));
3051           break;
3052         }
3053
3054       offset_size = initial_length_size == 4 ? 4 : 8;
3055       ptr += initial_length_size;
3056       version = bfd_get_16 (objfile->obfd, ptr);
3057       ptr += 2;
3058       ptr += offset_size; /* abbrev offset */
3059       ptr += 1; /* address size */
3060       signature = bfd_get_64 (objfile->obfd, ptr);
3061       ptr += 8;
3062       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3063
3064       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3065       memset (type_sig, 0, sizeof (*type_sig));
3066       type_sig->signature = signature;
3067       type_sig->offset = offset;
3068       type_sig->type_offset = type_offset;
3069       type_sig->per_cu.objfile = objfile;
3070       type_sig->per_cu.from_debug_types = 1;
3071
3072       slot = htab_find_slot (types_htab, type_sig, INSERT);
3073       gdb_assert (slot != NULL);
3074       *slot = type_sig;
3075
3076       if (dwarf2_die_debug)
3077         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3078                             offset, phex (signature, sizeof (signature)));
3079
3080       info_ptr = info_ptr + initial_length_size + length;
3081     }
3082
3083   dwarf2_per_objfile->signatured_types = types_htab;
3084
3085   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3086   dwarf2_per_objfile->type_comp_units
3087     = obstack_alloc (&objfile->objfile_obstack,
3088                      dwarf2_per_objfile->n_type_comp_units
3089                      * sizeof (struct dwarf2_per_cu_data *));
3090   iter = &dwarf2_per_objfile->type_comp_units[0];
3091   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3092   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3093               == dwarf2_per_objfile->n_type_comp_units);
3094
3095   return 1;
3096 }
3097
3098 /* Lookup a signature based type.
3099    Returns NULL if SIG is not present in the table.  */
3100
3101 static struct signatured_type *
3102 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3103 {
3104   struct signatured_type find_entry, *entry;
3105
3106   if (dwarf2_per_objfile->signatured_types == NULL)
3107     {
3108       complaint (&symfile_complaints,
3109                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
3110       return 0;
3111     }
3112
3113   find_entry.signature = sig;
3114   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3115   return entry;
3116 }
3117
3118 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3119
3120 static void
3121 init_cu_die_reader (struct die_reader_specs *reader,
3122                     struct dwarf2_cu *cu)
3123 {
3124   reader->abfd = cu->objfile->obfd;
3125   reader->cu = cu;
3126   if (cu->per_cu->from_debug_types)
3127     {
3128       gdb_assert (dwarf2_per_objfile->types.readin);
3129       reader->buffer = dwarf2_per_objfile->types.buffer;
3130     }
3131   else
3132     {
3133       gdb_assert (dwarf2_per_objfile->info.readin);
3134       reader->buffer = dwarf2_per_objfile->info.buffer;
3135     }
3136 }
3137
3138 /* Find the base address of the compilation unit for range lists and
3139    location lists.  It will normally be specified by DW_AT_low_pc.
3140    In DWARF-3 draft 4, the base address could be overridden by
3141    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3142    compilation units with discontinuous ranges.  */
3143
3144 static void
3145 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3146 {
3147   struct attribute *attr;
3148
3149   cu->base_known = 0;
3150   cu->base_address = 0;
3151
3152   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3153   if (attr)
3154     {
3155       cu->base_address = DW_ADDR (attr);
3156       cu->base_known = 1;
3157     }
3158   else
3159     {
3160       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3161       if (attr)
3162         {
3163           cu->base_address = DW_ADDR (attr);
3164           cu->base_known = 1;
3165         }
3166     }
3167 }
3168
3169 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3170    to combine the common parts.
3171    Process a compilation unit for a psymtab.
3172    BUFFER is a pointer to the beginning of the dwarf section buffer,
3173    either .debug_info or debug_types.
3174    INFO_PTR is a pointer to the start of the CU.
3175    Returns a pointer to the next CU.  */
3176
3177 static gdb_byte *
3178 process_psymtab_comp_unit (struct objfile *objfile,
3179                            struct dwarf2_per_cu_data *this_cu,
3180                            gdb_byte *buffer, gdb_byte *info_ptr,
3181                            unsigned int buffer_size)
3182 {
3183   bfd *abfd = objfile->obfd;
3184   gdb_byte *beg_of_comp_unit = info_ptr;
3185   struct die_info *comp_unit_die;
3186   struct partial_symtab *pst;
3187   CORE_ADDR baseaddr;
3188   struct cleanup *back_to_inner;
3189   struct dwarf2_cu cu;
3190   int has_children, has_pc_info;
3191   struct attribute *attr;
3192   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3193   struct die_reader_specs reader_specs;
3194   const char *filename;
3195
3196   init_one_comp_unit (&cu, objfile);
3197   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3198
3199   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3200                                           buffer, buffer_size,
3201                                           abfd);
3202
3203   /* Complete the cu_header.  */
3204   cu.header.offset = beg_of_comp_unit - buffer;
3205   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3206
3207   cu.list_in_scope = &file_symbols;
3208
3209   /* If this compilation unit was already read in, free the
3210      cached copy in order to read it in again.  This is
3211      necessary because we skipped some symbols when we first
3212      read in the compilation unit (see load_partial_dies).
3213      This problem could be avoided, but the benefit is
3214      unclear.  */
3215   if (this_cu->cu != NULL)
3216     free_one_cached_comp_unit (this_cu->cu);
3217
3218   /* Note that this is a pointer to our stack frame, being
3219      added to a global data structure.  It will be cleaned up
3220      in free_stack_comp_unit when we finish with this
3221      compilation unit.  */
3222   this_cu->cu = &cu;
3223   cu.per_cu = this_cu;
3224
3225   /* Read the abbrevs for this compilation unit into a table.  */
3226   dwarf2_read_abbrevs (abfd, &cu);
3227   make_cleanup (dwarf2_free_abbrev_table, &cu);
3228
3229   /* Read the compilation unit die.  */
3230   if (this_cu->from_debug_types)
3231     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3232   init_cu_die_reader (&reader_specs, &cu);
3233   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3234                             &has_children);
3235
3236   if (this_cu->from_debug_types)
3237     {
3238       /* offset,length haven't been set yet for type units.  */
3239       this_cu->offset = cu.header.offset;
3240       this_cu->length = cu.header.length + cu.header.initial_length_size;
3241     }
3242   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3243     {
3244       info_ptr = (beg_of_comp_unit + cu.header.length
3245                   + cu.header.initial_length_size);
3246       do_cleanups (back_to_inner);
3247       return info_ptr;
3248     }
3249
3250   prepare_one_comp_unit (&cu, comp_unit_die);
3251
3252   /* Allocate a new partial symbol table structure.  */
3253   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3254   if (attr == NULL || !DW_STRING (attr))
3255     filename = "";
3256   else
3257     filename = DW_STRING (attr);
3258   pst = start_psymtab_common (objfile, objfile->section_offsets,
3259                               filename,
3260                               /* TEXTLOW and TEXTHIGH are set below.  */
3261                               0,
3262                               objfile->global_psymbols.next,
3263                               objfile->static_psymbols.next);
3264
3265   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3266   if (attr != NULL)
3267     pst->dirname = DW_STRING (attr);
3268
3269   pst->read_symtab_private = this_cu;
3270
3271   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3272
3273   /* Store the function that reads in the rest of the symbol table.  */
3274   pst->read_symtab = dwarf2_psymtab_to_symtab;
3275
3276   this_cu->v.psymtab = pst;
3277
3278   dwarf2_find_base_address (comp_unit_die, &cu);
3279
3280   /* Possibly set the default values of LOWPC and HIGHPC from
3281      `DW_AT_ranges'.  */
3282   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3283                                       &best_highpc, &cu, pst);
3284   if (has_pc_info == 1 && best_lowpc < best_highpc)
3285     /* Store the contiguous range if it is not empty; it can be empty for
3286        CUs with no code.  */
3287     addrmap_set_empty (objfile->psymtabs_addrmap,
3288                        best_lowpc + baseaddr,
3289                        best_highpc + baseaddr - 1, pst);
3290
3291   /* Check if comp unit has_children.
3292      If so, read the rest of the partial symbols from this comp unit.
3293      If not, there's no more debug_info for this comp unit.  */
3294   if (has_children)
3295     {
3296       struct partial_die_info *first_die;
3297       CORE_ADDR lowpc, highpc;
3298
3299       lowpc = ((CORE_ADDR) -1);
3300       highpc = ((CORE_ADDR) 0);
3301
3302       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3303
3304       scan_partial_symbols (first_die, &lowpc, &highpc,
3305                             ! has_pc_info, &cu);
3306
3307       /* If we didn't find a lowpc, set it to highpc to avoid
3308          complaints from `maint check'.  */
3309       if (lowpc == ((CORE_ADDR) -1))
3310         lowpc = highpc;
3311
3312       /* If the compilation unit didn't have an explicit address range,
3313          then use the information extracted from its child dies.  */
3314       if (! has_pc_info)
3315         {
3316           best_lowpc = lowpc;
3317           best_highpc = highpc;
3318         }
3319     }
3320   pst->textlow = best_lowpc + baseaddr;
3321   pst->texthigh = best_highpc + baseaddr;
3322
3323   pst->n_global_syms = objfile->global_psymbols.next -
3324     (objfile->global_psymbols.list + pst->globals_offset);
3325   pst->n_static_syms = objfile->static_psymbols.next -
3326     (objfile->static_psymbols.list + pst->statics_offset);
3327   sort_pst_symbols (pst);
3328
3329   info_ptr = (beg_of_comp_unit + cu.header.length
3330               + cu.header.initial_length_size);
3331
3332   if (this_cu->from_debug_types)
3333     {
3334       /* It's not clear we want to do anything with stmt lists here.
3335          Waiting to see what gcc ultimately does.  */
3336     }
3337   else
3338     {
3339       /* Get the list of files included in the current compilation unit,
3340          and build a psymtab for each of them.  */
3341       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3342     }
3343
3344   do_cleanups (back_to_inner);
3345
3346   return info_ptr;
3347 }
3348
3349 /* Traversal function for htab_traverse_noresize.
3350    Process one .debug_types comp-unit.  */
3351
3352 static int
3353 process_type_comp_unit (void **slot, void *info)
3354 {
3355   struct signatured_type *entry = (struct signatured_type *) *slot;
3356   struct objfile *objfile = (struct objfile *) info;
3357   struct dwarf2_per_cu_data *this_cu;
3358
3359   this_cu = &entry->per_cu;
3360
3361   gdb_assert (dwarf2_per_objfile->types.readin);
3362   process_psymtab_comp_unit (objfile, this_cu,
3363                              dwarf2_per_objfile->types.buffer,
3364                              dwarf2_per_objfile->types.buffer + entry->offset,
3365                              dwarf2_per_objfile->types.size);
3366
3367   return 1;
3368 }
3369
3370 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3371    Build partial symbol tables for the .debug_types comp-units.  */
3372
3373 static void
3374 build_type_psymtabs (struct objfile *objfile)
3375 {
3376   if (! create_debug_types_hash_table (objfile))
3377     return;
3378
3379   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3380                           process_type_comp_unit, objfile);
3381 }
3382
3383 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3384
3385 static void
3386 psymtabs_addrmap_cleanup (void *o)
3387 {
3388   struct objfile *objfile = o;
3389
3390   objfile->psymtabs_addrmap = NULL;
3391 }
3392
3393 /* Build the partial symbol table by doing a quick pass through the
3394    .debug_info and .debug_abbrev sections.  */
3395
3396 static void
3397 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3398 {
3399   gdb_byte *info_ptr;
3400   struct cleanup *back_to, *addrmap_cleanup;
3401   struct obstack temp_obstack;
3402
3403   dwarf2_per_objfile->reading_partial_symbols = 1;
3404
3405   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3406   info_ptr = dwarf2_per_objfile->info.buffer;
3407
3408   /* Any cached compilation units will be linked by the per-objfile
3409      read_in_chain.  Make sure to free them when we're done.  */
3410   back_to = make_cleanup (free_cached_comp_units, NULL);
3411
3412   build_type_psymtabs (objfile);
3413
3414   create_all_comp_units (objfile);
3415
3416   /* Create a temporary address map on a temporary obstack.  We later
3417      copy this to the final obstack.  */
3418   obstack_init (&temp_obstack);
3419   make_cleanup_obstack_free (&temp_obstack);
3420   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3421   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3422
3423   /* Since the objects we're extracting from .debug_info vary in
3424      length, only the individual functions to extract them (like
3425      read_comp_unit_head and load_partial_die) can really know whether
3426      the buffer is large enough to hold another complete object.
3427
3428      At the moment, they don't actually check that.  If .debug_info
3429      holds just one extra byte after the last compilation unit's dies,
3430      then read_comp_unit_head will happily read off the end of the
3431      buffer.  read_partial_die is similarly casual.  Those functions
3432      should be fixed.
3433
3434      For this loop condition, simply checking whether there's any data
3435      left at all should be sufficient.  */
3436
3437   while (info_ptr < (dwarf2_per_objfile->info.buffer
3438                      + dwarf2_per_objfile->info.size))
3439     {
3440       struct dwarf2_per_cu_data *this_cu;
3441
3442       this_cu = dwarf2_find_comp_unit (info_ptr
3443                                        - dwarf2_per_objfile->info.buffer,
3444                                        objfile);
3445
3446       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3447                                             dwarf2_per_objfile->info.buffer,
3448                                             info_ptr,
3449                                             dwarf2_per_objfile->info.size);
3450     }
3451
3452   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3453                                                     &objfile->objfile_obstack);
3454   discard_cleanups (addrmap_cleanup);
3455
3456   do_cleanups (back_to);
3457 }
3458
3459 /* Load the partial DIEs for a secondary CU into memory.  */
3460
3461 static void
3462 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3463                         struct objfile *objfile)
3464 {
3465   bfd *abfd = objfile->obfd;
3466   gdb_byte *info_ptr, *beg_of_comp_unit;
3467   struct die_info *comp_unit_die;
3468   struct dwarf2_cu *cu;
3469   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3470   int has_children;
3471   struct die_reader_specs reader_specs;
3472   int read_cu = 0;
3473
3474   gdb_assert (! this_cu->from_debug_types);
3475
3476   gdb_assert (dwarf2_per_objfile->info.readin);
3477   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3478   beg_of_comp_unit = info_ptr;
3479
3480   if (this_cu->cu == NULL)
3481     {
3482       cu = xmalloc (sizeof (*cu));
3483       init_one_comp_unit (cu, objfile);
3484
3485       read_cu = 1;
3486
3487       /* If an error occurs while loading, release our storage.  */
3488       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3489
3490       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3491                                               dwarf2_per_objfile->info.buffer,
3492                                               dwarf2_per_objfile->info.size,
3493                                               abfd);
3494
3495       /* Complete the cu_header.  */
3496       cu->header.offset = this_cu->offset;
3497       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3498
3499       /* Link this compilation unit into the compilation unit tree.  */
3500       this_cu->cu = cu;
3501       cu->per_cu = this_cu;
3502
3503       /* Link this CU into read_in_chain.  */
3504       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3505       dwarf2_per_objfile->read_in_chain = this_cu;
3506     }
3507   else
3508     {
3509       cu = this_cu->cu;
3510       info_ptr += cu->header.first_die_offset;
3511     }
3512
3513   /* Read the abbrevs for this compilation unit into a table.  */
3514   gdb_assert (cu->dwarf2_abbrevs == NULL);
3515   dwarf2_read_abbrevs (abfd, cu);
3516   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3517
3518   /* Read the compilation unit die.  */
3519   init_cu_die_reader (&reader_specs, cu);
3520   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3521                             &has_children);
3522
3523   prepare_one_comp_unit (cu, comp_unit_die);
3524
3525   /* Check if comp unit has_children.
3526      If so, read the rest of the partial symbols from this comp unit.
3527      If not, there's no more debug_info for this comp unit.  */
3528   if (has_children)
3529     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3530
3531   do_cleanups (free_abbrevs_cleanup);
3532
3533   if (read_cu)
3534     {
3535       /* We've successfully allocated this compilation unit.  Let our
3536          caller clean it up when finished with it.  */
3537       discard_cleanups (free_cu_cleanup);
3538     }
3539 }
3540
3541 /* Create a list of all compilation units in OBJFILE.  We do this only
3542    if an inter-comp-unit reference is found; presumably if there is one,
3543    there will be many, and one will occur early in the .debug_info section.
3544    So there's no point in building this list incrementally.  */
3545
3546 static void
3547 create_all_comp_units (struct objfile *objfile)
3548 {
3549   int n_allocated;
3550   int n_comp_units;
3551   struct dwarf2_per_cu_data **all_comp_units;
3552   gdb_byte *info_ptr;
3553
3554   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3555   info_ptr = dwarf2_per_objfile->info.buffer;
3556
3557   n_comp_units = 0;
3558   n_allocated = 10;
3559   all_comp_units = xmalloc (n_allocated
3560                             * sizeof (struct dwarf2_per_cu_data *));
3561
3562   while (info_ptr < dwarf2_per_objfile->info.buffer
3563          + dwarf2_per_objfile->info.size)
3564     {
3565       unsigned int length, initial_length_size;
3566       struct dwarf2_per_cu_data *this_cu;
3567       unsigned int offset;
3568
3569       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3570
3571       /* Read just enough information to find out where the next
3572          compilation unit is.  */
3573       length = read_initial_length (objfile->obfd, info_ptr,
3574                                     &initial_length_size);
3575
3576       /* Save the compilation unit for later lookup.  */
3577       this_cu = obstack_alloc (&objfile->objfile_obstack,
3578                                sizeof (struct dwarf2_per_cu_data));
3579       memset (this_cu, 0, sizeof (*this_cu));
3580       this_cu->offset = offset;
3581       this_cu->length = length + initial_length_size;
3582       this_cu->objfile = objfile;
3583
3584       if (n_comp_units == n_allocated)
3585         {
3586           n_allocated *= 2;
3587           all_comp_units = xrealloc (all_comp_units,
3588                                      n_allocated
3589                                      * sizeof (struct dwarf2_per_cu_data *));
3590         }
3591       all_comp_units[n_comp_units++] = this_cu;
3592
3593       info_ptr = info_ptr + this_cu->length;
3594     }
3595
3596   dwarf2_per_objfile->all_comp_units
3597     = obstack_alloc (&objfile->objfile_obstack,
3598                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3599   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3600           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3601   xfree (all_comp_units);
3602   dwarf2_per_objfile->n_comp_units = n_comp_units;
3603 }
3604
3605 /* Process all loaded DIEs for compilation unit CU, starting at
3606    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3607    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3608    DW_AT_ranges).  If NEED_PC is set, then this function will set
3609    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3610    and record the covered ranges in the addrmap.  */
3611
3612 static void
3613 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3614                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3615 {
3616   struct partial_die_info *pdi;
3617
3618   /* Now, march along the PDI's, descending into ones which have
3619      interesting children but skipping the children of the other ones,
3620      until we reach the end of the compilation unit.  */
3621
3622   pdi = first_die;
3623
3624   while (pdi != NULL)
3625     {
3626       fixup_partial_die (pdi, cu);
3627
3628       /* Anonymous namespaces or modules have no name but have interesting
3629          children, so we need to look at them.  Ditto for anonymous
3630          enums.  */
3631
3632       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3633           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3634         {
3635           switch (pdi->tag)
3636             {
3637             case DW_TAG_subprogram:
3638               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3639               break;
3640             case DW_TAG_constant:
3641             case DW_TAG_variable:
3642             case DW_TAG_typedef:
3643             case DW_TAG_union_type:
3644               if (!pdi->is_declaration)
3645                 {
3646                   add_partial_symbol (pdi, cu);
3647                 }
3648               break;
3649             case DW_TAG_class_type:
3650             case DW_TAG_interface_type:
3651             case DW_TAG_structure_type:
3652               if (!pdi->is_declaration)
3653                 {
3654                   add_partial_symbol (pdi, cu);
3655                 }
3656               break;
3657             case DW_TAG_enumeration_type:
3658               if (!pdi->is_declaration)
3659                 add_partial_enumeration (pdi, cu);
3660               break;
3661             case DW_TAG_base_type:
3662             case DW_TAG_subrange_type:
3663               /* File scope base type definitions are added to the partial
3664                  symbol table.  */
3665               add_partial_symbol (pdi, cu);
3666               break;
3667             case DW_TAG_namespace:
3668               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3669               break;
3670             case DW_TAG_module:
3671               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3672               break;
3673             default:
3674               break;
3675             }
3676         }
3677
3678       /* If the die has a sibling, skip to the sibling.  */
3679
3680       pdi = pdi->die_sibling;
3681     }
3682 }
3683
3684 /* Functions used to compute the fully scoped name of a partial DIE.
3685
3686    Normally, this is simple.  For C++, the parent DIE's fully scoped
3687    name is concatenated with "::" and the partial DIE's name.  For
3688    Java, the same thing occurs except that "." is used instead of "::".
3689    Enumerators are an exception; they use the scope of their parent
3690    enumeration type, i.e. the name of the enumeration type is not
3691    prepended to the enumerator.
3692
3693    There are two complexities.  One is DW_AT_specification; in this
3694    case "parent" means the parent of the target of the specification,
3695    instead of the direct parent of the DIE.  The other is compilers
3696    which do not emit DW_TAG_namespace; in this case we try to guess
3697    the fully qualified name of structure types from their members'
3698    linkage names.  This must be done using the DIE's children rather
3699    than the children of any DW_AT_specification target.  We only need
3700    to do this for structures at the top level, i.e. if the target of
3701    any DW_AT_specification (if any; otherwise the DIE itself) does not
3702    have a parent.  */
3703
3704 /* Compute the scope prefix associated with PDI's parent, in
3705    compilation unit CU.  The result will be allocated on CU's
3706    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3707    field.  NULL is returned if no prefix is necessary.  */
3708 static char *
3709 partial_die_parent_scope (struct partial_die_info *pdi,
3710                           struct dwarf2_cu *cu)
3711 {
3712   char *grandparent_scope;
3713   struct partial_die_info *parent, *real_pdi;
3714
3715   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3716      then this means the parent of the specification DIE.  */
3717
3718   real_pdi = pdi;
3719   while (real_pdi->has_specification)
3720     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3721
3722   parent = real_pdi->die_parent;
3723   if (parent == NULL)
3724     return NULL;
3725
3726   if (parent->scope_set)
3727     return parent->scope;
3728
3729   fixup_partial_die (parent, cu);
3730
3731   grandparent_scope = partial_die_parent_scope (parent, cu);
3732
3733   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3734      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3735      Work around this problem here.  */
3736   if (cu->language == language_cplus
3737       && parent->tag == DW_TAG_namespace
3738       && strcmp (parent->name, "::") == 0
3739       && grandparent_scope == NULL)
3740     {
3741       parent->scope = NULL;
3742       parent->scope_set = 1;
3743       return NULL;
3744     }
3745
3746   if (parent->tag == DW_TAG_namespace
3747       || parent->tag == DW_TAG_module
3748       || parent->tag == DW_TAG_structure_type
3749       || parent->tag == DW_TAG_class_type
3750       || parent->tag == DW_TAG_interface_type
3751       || parent->tag == DW_TAG_union_type
3752       || parent->tag == DW_TAG_enumeration_type)
3753     {
3754       if (grandparent_scope == NULL)
3755         parent->scope = parent->name;
3756       else
3757         parent->scope = typename_concat (&cu->comp_unit_obstack,
3758                                          grandparent_scope,
3759                                          parent->name, 0, cu);
3760     }
3761   else if (parent->tag == DW_TAG_enumerator)
3762     /* Enumerators should not get the name of the enumeration as a prefix.  */
3763     parent->scope = grandparent_scope;
3764   else
3765     {
3766       /* FIXME drow/2004-04-01: What should we be doing with
3767          function-local names?  For partial symbols, we should probably be
3768          ignoring them.  */
3769       complaint (&symfile_complaints,
3770                  _("unhandled containing DIE tag %d for DIE at %d"),
3771                  parent->tag, pdi->offset);
3772       parent->scope = grandparent_scope;
3773     }
3774
3775   parent->scope_set = 1;
3776   return parent->scope;
3777 }
3778
3779 /* Return the fully scoped name associated with PDI, from compilation unit
3780    CU.  The result will be allocated with malloc.  */
3781 static char *
3782 partial_die_full_name (struct partial_die_info *pdi,
3783                        struct dwarf2_cu *cu)
3784 {
3785   char *parent_scope;
3786
3787   /* If this is a template instantiation, we can not work out the
3788      template arguments from partial DIEs.  So, unfortunately, we have
3789      to go through the full DIEs.  At least any work we do building
3790      types here will be reused if full symbols are loaded later.  */
3791   if (pdi->has_template_arguments)
3792     {
3793       fixup_partial_die (pdi, cu);
3794
3795       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3796         {
3797           struct die_info *die;
3798           struct attribute attr;
3799           struct dwarf2_cu *ref_cu = cu;
3800
3801           attr.name = 0;
3802           attr.form = DW_FORM_ref_addr;
3803           attr.u.addr = pdi->offset;
3804           die = follow_die_ref (NULL, &attr, &ref_cu);
3805
3806           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3807         }
3808     }
3809
3810   parent_scope = partial_die_parent_scope (pdi, cu);
3811   if (parent_scope == NULL)
3812     return NULL;
3813   else
3814     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3815 }
3816
3817 static void
3818 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3819 {
3820   struct objfile *objfile = cu->objfile;
3821   CORE_ADDR addr = 0;
3822   char *actual_name = NULL;
3823   const struct partial_symbol *psym = NULL;
3824   CORE_ADDR baseaddr;
3825   int built_actual_name = 0;
3826
3827   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3828
3829   actual_name = partial_die_full_name (pdi, cu);
3830   if (actual_name)
3831     built_actual_name = 1;
3832
3833   if (actual_name == NULL)
3834     actual_name = pdi->name;
3835
3836   switch (pdi->tag)
3837     {
3838     case DW_TAG_subprogram:
3839       if (pdi->is_external || cu->language == language_ada)
3840         {
3841           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3842              of the global scope.  But in Ada, we want to be able to access
3843              nested procedures globally.  So all Ada subprograms are stored
3844              in the global scope.  */
3845           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3846              mst_text, objfile); */
3847           add_psymbol_to_list (actual_name, strlen (actual_name),
3848                                built_actual_name,
3849                                VAR_DOMAIN, LOC_BLOCK,
3850                                &objfile->global_psymbols,
3851                                0, pdi->lowpc + baseaddr,
3852                                cu->language, objfile);
3853         }
3854       else
3855         {
3856           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3857              mst_file_text, objfile); */
3858           add_psymbol_to_list (actual_name, strlen (actual_name),
3859                                built_actual_name,
3860                                VAR_DOMAIN, LOC_BLOCK,
3861                                &objfile->static_psymbols,
3862                                0, pdi->lowpc + baseaddr,
3863                                cu->language, objfile);
3864         }
3865       break;
3866     case DW_TAG_constant:
3867       {
3868         struct psymbol_allocation_list *list;
3869
3870         if (pdi->is_external)
3871           list = &objfile->global_psymbols;
3872         else
3873           list = &objfile->static_psymbols;
3874         add_psymbol_to_list (actual_name, strlen (actual_name),
3875                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3876                              list, 0, 0, cu->language, objfile);
3877       }
3878       break;
3879     case DW_TAG_variable:
3880       if (pdi->locdesc)
3881         addr = decode_locdesc (pdi->locdesc, cu);
3882
3883       if (pdi->locdesc
3884           && addr == 0
3885           && !dwarf2_per_objfile->has_section_at_zero)
3886         {
3887           /* A global or static variable may also have been stripped
3888              out by the linker if unused, in which case its address
3889              will be nullified; do not add such variables into partial
3890              symbol table then.  */
3891         }
3892       else if (pdi->is_external)
3893         {
3894           /* Global Variable.
3895              Don't enter into the minimal symbol tables as there is
3896              a minimal symbol table entry from the ELF symbols already.
3897              Enter into partial symbol table if it has a location
3898              descriptor or a type.
3899              If the location descriptor is missing, new_symbol will create
3900              a LOC_UNRESOLVED symbol, the address of the variable will then
3901              be determined from the minimal symbol table whenever the variable
3902              is referenced.
3903              The address for the partial symbol table entry is not
3904              used by GDB, but it comes in handy for debugging partial symbol
3905              table building.  */
3906
3907           if (pdi->locdesc || pdi->has_type)
3908             add_psymbol_to_list (actual_name, strlen (actual_name),
3909                                  built_actual_name,
3910                                  VAR_DOMAIN, LOC_STATIC,
3911                                  &objfile->global_psymbols,
3912                                  0, addr + baseaddr,
3913                                  cu->language, objfile);
3914         }
3915       else
3916         {
3917           /* Static Variable.  Skip symbols without location descriptors.  */
3918           if (pdi->locdesc == NULL)
3919             {
3920               if (built_actual_name)
3921                 xfree (actual_name);
3922               return;
3923             }
3924           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3925              mst_file_data, objfile); */
3926           add_psymbol_to_list (actual_name, strlen (actual_name),
3927                                built_actual_name,
3928                                VAR_DOMAIN, LOC_STATIC,
3929                                &objfile->static_psymbols,
3930                                0, addr + baseaddr,
3931                                cu->language, objfile);
3932         }
3933       break;
3934     case DW_TAG_typedef:
3935     case DW_TAG_base_type:
3936     case DW_TAG_subrange_type:
3937       add_psymbol_to_list (actual_name, strlen (actual_name),
3938                            built_actual_name,
3939                            VAR_DOMAIN, LOC_TYPEDEF,
3940                            &objfile->static_psymbols,
3941                            0, (CORE_ADDR) 0, cu->language, objfile);
3942       break;
3943     case DW_TAG_namespace:
3944       add_psymbol_to_list (actual_name, strlen (actual_name),
3945                            built_actual_name,
3946                            VAR_DOMAIN, LOC_TYPEDEF,
3947                            &objfile->global_psymbols,
3948                            0, (CORE_ADDR) 0, cu->language, objfile);
3949       break;
3950     case DW_TAG_class_type:
3951     case DW_TAG_interface_type:
3952     case DW_TAG_structure_type:
3953     case DW_TAG_union_type:
3954     case DW_TAG_enumeration_type:
3955       /* Skip external references.  The DWARF standard says in the section
3956          about "Structure, Union, and Class Type Entries": "An incomplete
3957          structure, union or class type is represented by a structure,
3958          union or class entry that does not have a byte size attribute
3959          and that has a DW_AT_declaration attribute."  */
3960       if (!pdi->has_byte_size && pdi->is_declaration)
3961         {
3962           if (built_actual_name)
3963             xfree (actual_name);
3964           return;
3965         }
3966
3967       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3968          static vs. global.  */
3969       add_psymbol_to_list (actual_name, strlen (actual_name),
3970                            built_actual_name,
3971                            STRUCT_DOMAIN, LOC_TYPEDEF,
3972                            (cu->language == language_cplus
3973                             || cu->language == language_java)
3974                            ? &objfile->global_psymbols
3975                            : &objfile->static_psymbols,
3976                            0, (CORE_ADDR) 0, cu->language, objfile);
3977
3978       break;
3979     case DW_TAG_enumerator:
3980       add_psymbol_to_list (actual_name, strlen (actual_name),
3981                            built_actual_name,
3982                            VAR_DOMAIN, LOC_CONST,
3983                            (cu->language == language_cplus
3984                             || cu->language == language_java)
3985                            ? &objfile->global_psymbols
3986                            : &objfile->static_psymbols,
3987                            0, (CORE_ADDR) 0, cu->language, objfile);
3988       break;
3989     default:
3990       break;
3991     }
3992
3993   if (built_actual_name)
3994     xfree (actual_name);
3995 }
3996
3997 /* Read a partial die corresponding to a namespace; also, add a symbol
3998    corresponding to that namespace to the symbol table.  NAMESPACE is
3999    the name of the enclosing namespace.  */
4000
4001 static void
4002 add_partial_namespace (struct partial_die_info *pdi,
4003                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4004                        int need_pc, struct dwarf2_cu *cu)
4005 {
4006   /* Add a symbol for the namespace.  */
4007
4008   add_partial_symbol (pdi, cu);
4009
4010   /* Now scan partial symbols in that namespace.  */
4011
4012   if (pdi->has_children)
4013     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4014 }
4015
4016 /* Read a partial die corresponding to a Fortran module.  */
4017
4018 static void
4019 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4020                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4021 {
4022   /* Now scan partial symbols in that module.  */
4023
4024   if (pdi->has_children)
4025     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4026 }
4027
4028 /* Read a partial die corresponding to a subprogram and create a partial
4029    symbol for that subprogram.  When the CU language allows it, this
4030    routine also defines a partial symbol for each nested subprogram
4031    that this subprogram contains.
4032
4033    DIE my also be a lexical block, in which case we simply search
4034    recursively for suprograms defined inside that lexical block.
4035    Again, this is only performed when the CU language allows this
4036    type of definitions.  */
4037
4038 static void
4039 add_partial_subprogram (struct partial_die_info *pdi,
4040                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4041                         int need_pc, struct dwarf2_cu *cu)
4042 {
4043   if (pdi->tag == DW_TAG_subprogram)
4044     {
4045       if (pdi->has_pc_info)
4046         {
4047           if (pdi->lowpc < *lowpc)
4048             *lowpc = pdi->lowpc;
4049           if (pdi->highpc > *highpc)
4050             *highpc = pdi->highpc;
4051           if (need_pc)
4052             {
4053               CORE_ADDR baseaddr;
4054               struct objfile *objfile = cu->objfile;
4055
4056               baseaddr = ANOFFSET (objfile->section_offsets,
4057                                    SECT_OFF_TEXT (objfile));
4058               addrmap_set_empty (objfile->psymtabs_addrmap,
4059                                  pdi->lowpc + baseaddr,
4060                                  pdi->highpc - 1 + baseaddr,
4061                                  cu->per_cu->v.psymtab);
4062             }
4063           if (!pdi->is_declaration)
4064             /* Ignore subprogram DIEs that do not have a name, they are
4065                illegal.  Do not emit a complaint at this point, we will
4066                do so when we convert this psymtab into a symtab.  */
4067             if (pdi->name)
4068               add_partial_symbol (pdi, cu);
4069         }
4070     }
4071
4072   if (! pdi->has_children)
4073     return;
4074
4075   if (cu->language == language_ada)
4076     {
4077       pdi = pdi->die_child;
4078       while (pdi != NULL)
4079         {
4080           fixup_partial_die (pdi, cu);
4081           if (pdi->tag == DW_TAG_subprogram
4082               || pdi->tag == DW_TAG_lexical_block)
4083             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4084           pdi = pdi->die_sibling;
4085         }
4086     }
4087 }
4088
4089 /* Read a partial die corresponding to an enumeration type.  */
4090
4091 static void
4092 add_partial_enumeration (struct partial_die_info *enum_pdi,
4093                          struct dwarf2_cu *cu)
4094 {
4095   struct partial_die_info *pdi;
4096
4097   if (enum_pdi->name != NULL)
4098     add_partial_symbol (enum_pdi, cu);
4099
4100   pdi = enum_pdi->die_child;
4101   while (pdi)
4102     {
4103       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4104         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4105       else
4106         add_partial_symbol (pdi, cu);
4107       pdi = pdi->die_sibling;
4108     }
4109 }
4110
4111 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4112    Return the corresponding abbrev, or NULL if the number is zero (indicating
4113    an empty DIE).  In either case *BYTES_READ will be set to the length of
4114    the initial number.  */
4115
4116 static struct abbrev_info *
4117 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4118                  struct dwarf2_cu *cu)
4119 {
4120   bfd *abfd = cu->objfile->obfd;
4121   unsigned int abbrev_number;
4122   struct abbrev_info *abbrev;
4123
4124   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4125
4126   if (abbrev_number == 0)
4127     return NULL;
4128
4129   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4130   if (!abbrev)
4131     {
4132       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4133              abbrev_number, bfd_get_filename (abfd));
4134     }
4135
4136   return abbrev;
4137 }
4138
4139 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4140    Returns a pointer to the end of a series of DIEs, terminated by an empty
4141    DIE.  Any children of the skipped DIEs will also be skipped.  */
4142
4143 static gdb_byte *
4144 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4145 {
4146   struct abbrev_info *abbrev;
4147   unsigned int bytes_read;
4148
4149   while (1)
4150     {
4151       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4152       if (abbrev == NULL)
4153         return info_ptr + bytes_read;
4154       else
4155         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4156     }
4157 }
4158
4159 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4160    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4161    abbrev corresponding to that skipped uleb128 should be passed in
4162    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4163    children.  */
4164
4165 static gdb_byte *
4166 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4167               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4168 {
4169   unsigned int bytes_read;
4170   struct attribute attr;
4171   bfd *abfd = cu->objfile->obfd;
4172   unsigned int form, i;
4173
4174   for (i = 0; i < abbrev->num_attrs; i++)
4175     {
4176       /* The only abbrev we care about is DW_AT_sibling.  */
4177       if (abbrev->attrs[i].name == DW_AT_sibling)
4178         {
4179           read_attribute (&attr, &abbrev->attrs[i],
4180                           abfd, info_ptr, cu);
4181           if (attr.form == DW_FORM_ref_addr)
4182             complaint (&symfile_complaints,
4183                        _("ignoring absolute DW_AT_sibling"));
4184           else
4185             return buffer + dwarf2_get_ref_die_offset (&attr);
4186         }
4187
4188       /* If it isn't DW_AT_sibling, skip this attribute.  */
4189       form = abbrev->attrs[i].form;
4190     skip_attribute:
4191       switch (form)
4192         {
4193         case DW_FORM_ref_addr:
4194           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4195              and later it is offset sized.  */
4196           if (cu->header.version == 2)
4197             info_ptr += cu->header.addr_size;
4198           else
4199             info_ptr += cu->header.offset_size;
4200           break;
4201         case DW_FORM_addr:
4202           info_ptr += cu->header.addr_size;
4203           break;
4204         case DW_FORM_data1:
4205         case DW_FORM_ref1:
4206         case DW_FORM_flag:
4207           info_ptr += 1;
4208           break;
4209         case DW_FORM_flag_present:
4210           break;
4211         case DW_FORM_data2:
4212         case DW_FORM_ref2:
4213           info_ptr += 2;
4214           break;
4215         case DW_FORM_data4:
4216         case DW_FORM_ref4:
4217           info_ptr += 4;
4218           break;
4219         case DW_FORM_data8:
4220         case DW_FORM_ref8:
4221         case DW_FORM_sig8:
4222           info_ptr += 8;
4223           break;
4224         case DW_FORM_string:
4225           read_direct_string (abfd, info_ptr, &bytes_read);
4226           info_ptr += bytes_read;
4227           break;
4228         case DW_FORM_sec_offset:
4229         case DW_FORM_strp:
4230           info_ptr += cu->header.offset_size;
4231           break;
4232         case DW_FORM_exprloc:
4233         case DW_FORM_block:
4234           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4235           info_ptr += bytes_read;
4236           break;
4237         case DW_FORM_block1:
4238           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4239           break;
4240         case DW_FORM_block2:
4241           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4242           break;
4243         case DW_FORM_block4:
4244           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4245           break;
4246         case DW_FORM_sdata:
4247         case DW_FORM_udata:
4248         case DW_FORM_ref_udata:
4249           info_ptr = skip_leb128 (abfd, info_ptr);
4250           break;
4251         case DW_FORM_indirect:
4252           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4253           info_ptr += bytes_read;
4254           /* We need to continue parsing from here, so just go back to
4255              the top.  */
4256           goto skip_attribute;
4257
4258         default:
4259           error (_("Dwarf Error: Cannot handle %s "
4260                    "in DWARF reader [in module %s]"),
4261                  dwarf_form_name (form),
4262                  bfd_get_filename (abfd));
4263         }
4264     }
4265
4266   if (abbrev->has_children)
4267     return skip_children (buffer, info_ptr, cu);
4268   else
4269     return info_ptr;
4270 }
4271
4272 /* Locate ORIG_PDI's sibling.
4273    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4274    in BUFFER.  */
4275
4276 static gdb_byte *
4277 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4278                     gdb_byte *buffer, gdb_byte *info_ptr,
4279                     bfd *abfd, struct dwarf2_cu *cu)
4280 {
4281   /* Do we know the sibling already?  */
4282
4283   if (orig_pdi->sibling)
4284     return orig_pdi->sibling;
4285
4286   /* Are there any children to deal with?  */
4287
4288   if (!orig_pdi->has_children)
4289     return info_ptr;
4290
4291   /* Skip the children the long way.  */
4292
4293   return skip_children (buffer, info_ptr, cu);
4294 }
4295
4296 /* Expand this partial symbol table into a full symbol table.  */
4297
4298 static void
4299 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4300 {
4301   if (pst != NULL)
4302     {
4303       if (pst->readin)
4304         {
4305           warning (_("bug: psymtab for %s is already read in."),
4306                    pst->filename);
4307         }
4308       else
4309         {
4310           if (info_verbose)
4311             {
4312               printf_filtered (_("Reading in symbols for %s..."),
4313                                pst->filename);
4314               gdb_flush (gdb_stdout);
4315             }
4316
4317           /* Restore our global data.  */
4318           dwarf2_per_objfile = objfile_data (pst->objfile,
4319                                              dwarf2_objfile_data_key);
4320
4321           /* If this psymtab is constructed from a debug-only objfile, the
4322              has_section_at_zero flag will not necessarily be correct.  We
4323              can get the correct value for this flag by looking at the data
4324              associated with the (presumably stripped) associated objfile.  */
4325           if (pst->objfile->separate_debug_objfile_backlink)
4326             {
4327               struct dwarf2_per_objfile *dpo_backlink
4328                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4329                                 dwarf2_objfile_data_key);
4330
4331               dwarf2_per_objfile->has_section_at_zero
4332                 = dpo_backlink->has_section_at_zero;
4333             }
4334
4335           dwarf2_per_objfile->reading_partial_symbols = 0;
4336
4337           psymtab_to_symtab_1 (pst);
4338
4339           /* Finish up the debug error message.  */
4340           if (info_verbose)
4341             printf_filtered (_("done.\n"));
4342         }
4343     }
4344 }
4345
4346 /* Add PER_CU to the queue.  */
4347
4348 static void
4349 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4350 {
4351   struct dwarf2_queue_item *item;
4352
4353   per_cu->queued = 1;
4354   item = xmalloc (sizeof (*item));
4355   item->per_cu = per_cu;
4356   item->next = NULL;
4357
4358   if (dwarf2_queue == NULL)
4359     dwarf2_queue = item;
4360   else
4361     dwarf2_queue_tail->next = item;
4362
4363   dwarf2_queue_tail = item;
4364 }
4365
4366 /* Process the queue.  */
4367
4368 static void
4369 process_queue (struct objfile *objfile)
4370 {
4371   struct dwarf2_queue_item *item, *next_item;
4372
4373   /* The queue starts out with one item, but following a DIE reference
4374      may load a new CU, adding it to the end of the queue.  */
4375   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4376     {
4377       if (dwarf2_per_objfile->using_index
4378           ? !item->per_cu->v.quick->symtab
4379           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4380         process_full_comp_unit (item->per_cu);
4381
4382       item->per_cu->queued = 0;
4383       next_item = item->next;
4384       xfree (item);
4385     }
4386
4387   dwarf2_queue_tail = NULL;
4388 }
4389
4390 /* Free all allocated queue entries.  This function only releases anything if
4391    an error was thrown; if the queue was processed then it would have been
4392    freed as we went along.  */
4393
4394 static void
4395 dwarf2_release_queue (void *dummy)
4396 {
4397   struct dwarf2_queue_item *item, *last;
4398
4399   item = dwarf2_queue;
4400   while (item)
4401     {
4402       /* Anything still marked queued is likely to be in an
4403          inconsistent state, so discard it.  */
4404       if (item->per_cu->queued)
4405         {
4406           if (item->per_cu->cu != NULL)
4407             free_one_cached_comp_unit (item->per_cu->cu);
4408           item->per_cu->queued = 0;
4409         }
4410
4411       last = item;
4412       item = item->next;
4413       xfree (last);
4414     }
4415
4416   dwarf2_queue = dwarf2_queue_tail = NULL;
4417 }
4418
4419 /* Read in full symbols for PST, and anything it depends on.  */
4420
4421 static void
4422 psymtab_to_symtab_1 (struct partial_symtab *pst)
4423 {
4424   struct dwarf2_per_cu_data *per_cu;
4425   struct cleanup *back_to;
4426   int i;
4427
4428   for (i = 0; i < pst->number_of_dependencies; i++)
4429     if (!pst->dependencies[i]->readin)
4430       {
4431         /* Inform about additional files that need to be read in.  */
4432         if (info_verbose)
4433           {
4434             /* FIXME: i18n: Need to make this a single string.  */
4435             fputs_filtered (" ", gdb_stdout);
4436             wrap_here ("");
4437             fputs_filtered ("and ", gdb_stdout);
4438             wrap_here ("");
4439             printf_filtered ("%s...", pst->dependencies[i]->filename);
4440             wrap_here ("");     /* Flush output.  */
4441             gdb_flush (gdb_stdout);
4442           }
4443         psymtab_to_symtab_1 (pst->dependencies[i]);
4444       }
4445
4446   per_cu = pst->read_symtab_private;
4447
4448   if (per_cu == NULL)
4449     {
4450       /* It's an include file, no symbols to read for it.
4451          Everything is in the parent symtab.  */
4452       pst->readin = 1;
4453       return;
4454     }
4455
4456   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4457 }
4458
4459 /* Load the DIEs associated with PER_CU into memory.  */
4460
4461 static void
4462 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4463                      struct objfile *objfile)
4464 {
4465   bfd *abfd = objfile->obfd;
4466   struct dwarf2_cu *cu;
4467   unsigned int offset;
4468   gdb_byte *info_ptr, *beg_of_comp_unit;
4469   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4470   struct attribute *attr;
4471   int read_cu = 0;
4472
4473   gdb_assert (! per_cu->from_debug_types);
4474
4475   /* Set local variables from the partial symbol table info.  */
4476   offset = per_cu->offset;
4477
4478   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4479   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4480   beg_of_comp_unit = info_ptr;
4481
4482   if (per_cu->cu == NULL)
4483     {
4484       cu = xmalloc (sizeof (*cu));
4485       init_one_comp_unit (cu, objfile);
4486
4487       read_cu = 1;
4488
4489       /* If an error occurs while loading, release our storage.  */
4490       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4491
4492       /* Read in the comp_unit header.  */
4493       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4494
4495       /* Complete the cu_header.  */
4496       cu->header.offset = offset;
4497       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4498
4499       /* Read the abbrevs for this compilation unit.  */
4500       dwarf2_read_abbrevs (abfd, cu);
4501       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4502
4503       /* Link this compilation unit into the compilation unit tree.  */
4504       per_cu->cu = cu;
4505       cu->per_cu = per_cu;
4506
4507       /* Link this CU into read_in_chain.  */
4508       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4509       dwarf2_per_objfile->read_in_chain = per_cu;
4510     }
4511   else
4512     {
4513       cu = per_cu->cu;
4514       info_ptr += cu->header.first_die_offset;
4515     }
4516
4517   cu->dies = read_comp_unit (info_ptr, cu);
4518
4519   /* We try not to read any attributes in this function, because not
4520      all objfiles needed for references have been loaded yet, and symbol
4521      table processing isn't initialized.  But we have to set the CU language,
4522      or we won't be able to build types correctly.  */
4523   prepare_one_comp_unit (cu, cu->dies);
4524
4525   /* Similarly, if we do not read the producer, we can not apply
4526      producer-specific interpretation.  */
4527   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4528   if (attr)
4529     cu->producer = DW_STRING (attr);
4530
4531   if (read_cu)
4532     {
4533       do_cleanups (free_abbrevs_cleanup);
4534
4535       /* We've successfully allocated this compilation unit.  Let our
4536          caller clean it up when finished with it.  */
4537       discard_cleanups (free_cu_cleanup);
4538     }
4539 }
4540
4541 /* Add a DIE to the delayed physname list.  */
4542
4543 static void
4544 add_to_method_list (struct type *type, int fnfield_index, int index,
4545                     const char *name, struct die_info *die,
4546                     struct dwarf2_cu *cu)
4547 {
4548   struct delayed_method_info mi;
4549   mi.type = type;
4550   mi.fnfield_index = fnfield_index;
4551   mi.index = index;
4552   mi.name = name;
4553   mi.die = die;
4554   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4555 }
4556
4557 /* A cleanup for freeing the delayed method list.  */
4558
4559 static void
4560 free_delayed_list (void *ptr)
4561 {
4562   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4563   if (cu->method_list != NULL)
4564     {
4565       VEC_free (delayed_method_info, cu->method_list);
4566       cu->method_list = NULL;
4567     }
4568 }
4569
4570 /* Compute the physnames of any methods on the CU's method list.
4571
4572    The computation of method physnames is delayed in order to avoid the
4573    (bad) condition that one of the method's formal parameters is of an as yet
4574    incomplete type.  */
4575
4576 static void
4577 compute_delayed_physnames (struct dwarf2_cu *cu)
4578 {
4579   int i;
4580   struct delayed_method_info *mi;
4581   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4582     {
4583       char *physname;
4584       struct fn_fieldlist *fn_flp
4585         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4586       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4587       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4588     }
4589 }
4590
4591 /* Generate full symbol information for PST and CU, whose DIEs have
4592    already been loaded into memory.  */
4593
4594 static void
4595 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4596 {
4597   struct dwarf2_cu *cu = per_cu->cu;
4598   struct objfile *objfile = per_cu->objfile;
4599   CORE_ADDR lowpc, highpc;
4600   struct symtab *symtab;
4601   struct cleanup *back_to, *delayed_list_cleanup;
4602   CORE_ADDR baseaddr;
4603
4604   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4605
4606   buildsym_init ();
4607   back_to = make_cleanup (really_free_pendings, NULL);
4608   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4609
4610   cu->list_in_scope = &file_symbols;
4611
4612   dwarf2_find_base_address (cu->dies, cu);
4613
4614   /* Do line number decoding in read_file_scope () */
4615   process_die (cu->dies, cu);
4616
4617   /* Now that we have processed all the DIEs in the CU, all the types 
4618      should be complete, and it should now be safe to compute all of the
4619      physnames.  */
4620   compute_delayed_physnames (cu);
4621   do_cleanups (delayed_list_cleanup);
4622
4623   /* Some compilers don't define a DW_AT_high_pc attribute for the
4624      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4625      it, by scanning the DIE's below the compilation unit.  */
4626   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4627
4628   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4629
4630   /* Set symtab language to language from DW_AT_language.
4631      If the compilation is from a C file generated by language preprocessors,
4632      do not set the language if it was already deduced by start_subfile.  */
4633   if (symtab != NULL
4634       && !(cu->language == language_c && symtab->language != language_c))
4635     {
4636       symtab->language = cu->language;
4637     }
4638
4639   if (dwarf2_per_objfile->using_index)
4640     per_cu->v.quick->symtab = symtab;
4641   else
4642     {
4643       struct partial_symtab *pst = per_cu->v.psymtab;
4644       pst->symtab = symtab;
4645       pst->readin = 1;
4646     }
4647
4648   do_cleanups (back_to);
4649 }
4650
4651 /* Process a die and its children.  */
4652
4653 static void
4654 process_die (struct die_info *die, struct dwarf2_cu *cu)
4655 {
4656   switch (die->tag)
4657     {
4658     case DW_TAG_padding:
4659       break;
4660     case DW_TAG_compile_unit:
4661       read_file_scope (die, cu);
4662       break;
4663     case DW_TAG_type_unit:
4664       read_type_unit_scope (die, cu);
4665       break;
4666     case DW_TAG_subprogram:
4667     case DW_TAG_inlined_subroutine:
4668       read_func_scope (die, cu);
4669       break;
4670     case DW_TAG_lexical_block:
4671     case DW_TAG_try_block:
4672     case DW_TAG_catch_block:
4673       read_lexical_block_scope (die, cu);
4674       break;
4675     case DW_TAG_class_type:
4676     case DW_TAG_interface_type:
4677     case DW_TAG_structure_type:
4678     case DW_TAG_union_type:
4679       process_structure_scope (die, cu);
4680       break;
4681     case DW_TAG_enumeration_type:
4682       process_enumeration_scope (die, cu);
4683       break;
4684
4685     /* These dies have a type, but processing them does not create
4686        a symbol or recurse to process the children.  Therefore we can
4687        read them on-demand through read_type_die.  */
4688     case DW_TAG_subroutine_type:
4689     case DW_TAG_set_type:
4690     case DW_TAG_array_type:
4691     case DW_TAG_pointer_type:
4692     case DW_TAG_ptr_to_member_type:
4693     case DW_TAG_reference_type:
4694     case DW_TAG_string_type:
4695       break;
4696
4697     case DW_TAG_base_type:
4698     case DW_TAG_subrange_type:
4699     case DW_TAG_typedef:
4700       /* Add a typedef symbol for the type definition, if it has a
4701          DW_AT_name.  */
4702       new_symbol (die, read_type_die (die, cu), cu);
4703       break;
4704     case DW_TAG_common_block:
4705       read_common_block (die, cu);
4706       break;
4707     case DW_TAG_common_inclusion:
4708       break;
4709     case DW_TAG_namespace:
4710       processing_has_namespace_info = 1;
4711       read_namespace (die, cu);
4712       break;
4713     case DW_TAG_module:
4714       processing_has_namespace_info = 1;
4715       read_module (die, cu);
4716       break;
4717     case DW_TAG_imported_declaration:
4718     case DW_TAG_imported_module:
4719       processing_has_namespace_info = 1;
4720       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4721                                  || cu->language != language_fortran))
4722         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4723                    dwarf_tag_name (die->tag));
4724       read_import_statement (die, cu);
4725       break;
4726     default:
4727       new_symbol (die, NULL, cu);
4728       break;
4729     }
4730 }
4731
4732 /* A helper function for dwarf2_compute_name which determines whether DIE
4733    needs to have the name of the scope prepended to the name listed in the
4734    die.  */
4735
4736 static int
4737 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4738 {
4739   struct attribute *attr;
4740
4741   switch (die->tag)
4742     {
4743     case DW_TAG_namespace:
4744     case DW_TAG_typedef:
4745     case DW_TAG_class_type:
4746     case DW_TAG_interface_type:
4747     case DW_TAG_structure_type:
4748     case DW_TAG_union_type:
4749     case DW_TAG_enumeration_type:
4750     case DW_TAG_enumerator:
4751     case DW_TAG_subprogram:
4752     case DW_TAG_member:
4753       return 1;
4754
4755     case DW_TAG_variable:
4756     case DW_TAG_constant:
4757       /* We only need to prefix "globally" visible variables.  These include
4758          any variable marked with DW_AT_external or any variable that
4759          lives in a namespace.  [Variables in anonymous namespaces
4760          require prefixing, but they are not DW_AT_external.]  */
4761
4762       if (dwarf2_attr (die, DW_AT_specification, cu))
4763         {
4764           struct dwarf2_cu *spec_cu = cu;
4765
4766           return die_needs_namespace (die_specification (die, &spec_cu),
4767                                       spec_cu);
4768         }
4769
4770       attr = dwarf2_attr (die, DW_AT_external, cu);
4771       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4772           && die->parent->tag != DW_TAG_module)
4773         return 0;
4774       /* A variable in a lexical block of some kind does not need a
4775          namespace, even though in C++ such variables may be external
4776          and have a mangled name.  */
4777       if (die->parent->tag ==  DW_TAG_lexical_block
4778           || die->parent->tag ==  DW_TAG_try_block
4779           || die->parent->tag ==  DW_TAG_catch_block
4780           || die->parent->tag == DW_TAG_subprogram)
4781         return 0;
4782       return 1;
4783
4784     default:
4785       return 0;
4786     }
4787 }
4788
4789 /* Retrieve the last character from a mem_file.  */
4790
4791 static void
4792 do_ui_file_peek_last (void *object, const char *buffer, long length)
4793 {
4794   char *last_char_p = (char *) object;
4795
4796   if (length > 0)
4797     *last_char_p = buffer[length - 1];
4798 }
4799
4800 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4801    compute the physname for the object, which include a method's
4802    formal parameters (C++/Java) and return type (Java).
4803
4804    For Ada, return the DIE's linkage name rather than the fully qualified
4805    name.  PHYSNAME is ignored..
4806
4807    The result is allocated on the objfile_obstack and canonicalized.  */
4808
4809 static const char *
4810 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4811                      int physname)
4812 {
4813   if (name == NULL)
4814     name = dwarf2_name (die, cu);
4815
4816   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4817      compute it by typename_concat inside GDB.  */
4818   if (cu->language == language_ada
4819       || (cu->language == language_fortran && physname))
4820     {
4821       /* For Ada unit, we prefer the linkage name over the name, as
4822          the former contains the exported name, which the user expects
4823          to be able to reference.  Ideally, we want the user to be able
4824          to reference this entity using either natural or linkage name,
4825          but we haven't started looking at this enhancement yet.  */
4826       struct attribute *attr;
4827
4828       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4829       if (attr == NULL)
4830         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4831       if (attr && DW_STRING (attr))
4832         return DW_STRING (attr);
4833     }
4834
4835   /* These are the only languages we know how to qualify names in.  */
4836   if (name != NULL
4837       && (cu->language == language_cplus || cu->language == language_java
4838           || cu->language == language_fortran))
4839     {
4840       if (die_needs_namespace (die, cu))
4841         {
4842           long length;
4843           char *prefix;
4844           struct ui_file *buf;
4845
4846           prefix = determine_prefix (die, cu);
4847           buf = mem_fileopen ();
4848           if (*prefix != '\0')
4849             {
4850               char *prefixed_name = typename_concat (NULL, prefix, name,
4851                                                      physname, cu);
4852
4853               fputs_unfiltered (prefixed_name, buf);
4854               xfree (prefixed_name);
4855             }
4856           else
4857             fputs_unfiltered (name, buf);
4858
4859           /* Template parameters may be specified in the DIE's DW_AT_name, or
4860              as children with DW_TAG_template_type_param or
4861              DW_TAG_value_type_param.  If the latter, add them to the name
4862              here.  If the name already has template parameters, then
4863              skip this step; some versions of GCC emit both, and
4864              it is more efficient to use the pre-computed name.
4865
4866              Something to keep in mind about this process: it is very
4867              unlikely, or in some cases downright impossible, to produce
4868              something that will match the mangled name of a function.
4869              If the definition of the function has the same debug info,
4870              we should be able to match up with it anyway.  But fallbacks
4871              using the minimal symbol, for instance to find a method
4872              implemented in a stripped copy of libstdc++, will not work.
4873              If we do not have debug info for the definition, we will have to
4874              match them up some other way.
4875
4876              When we do name matching there is a related problem with function
4877              templates; two instantiated function templates are allowed to
4878              differ only by their return types, which we do not add here.  */
4879
4880           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4881             {
4882               struct attribute *attr;
4883               struct die_info *child;
4884               int first = 1;
4885
4886               die->building_fullname = 1;
4887
4888               for (child = die->child; child != NULL; child = child->sibling)
4889                 {
4890                   struct type *type;
4891                   long value;
4892                   gdb_byte *bytes;
4893                   struct dwarf2_locexpr_baton *baton;
4894                   struct value *v;
4895
4896                   if (child->tag != DW_TAG_template_type_param
4897                       && child->tag != DW_TAG_template_value_param)
4898                     continue;
4899
4900                   if (first)
4901                     {
4902                       fputs_unfiltered ("<", buf);
4903                       first = 0;
4904                     }
4905                   else
4906                     fputs_unfiltered (", ", buf);
4907
4908                   attr = dwarf2_attr (child, DW_AT_type, cu);
4909                   if (attr == NULL)
4910                     {
4911                       complaint (&symfile_complaints,
4912                                  _("template parameter missing DW_AT_type"));
4913                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4914                       continue;
4915                     }
4916                   type = die_type (child, cu);
4917
4918                   if (child->tag == DW_TAG_template_type_param)
4919                     {
4920                       c_print_type (type, "", buf, -1, 0);
4921                       continue;
4922                     }
4923
4924                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4925                   if (attr == NULL)
4926                     {
4927                       complaint (&symfile_complaints,
4928                                  _("template parameter missing "
4929                                    "DW_AT_const_value"));
4930                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4931                       continue;
4932                     }
4933
4934                   dwarf2_const_value_attr (attr, type, name,
4935                                            &cu->comp_unit_obstack, cu,
4936                                            &value, &bytes, &baton);
4937
4938                   if (TYPE_NOSIGN (type))
4939                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4940                        changed, this can use value_print instead.  */
4941                     c_printchar (value, type, buf);
4942                   else
4943                     {
4944                       struct value_print_options opts;
4945
4946                       if (baton != NULL)
4947                         v = dwarf2_evaluate_loc_desc (type, NULL,
4948                                                       baton->data,
4949                                                       baton->size,
4950                                                       baton->per_cu);
4951                       else if (bytes != NULL)
4952                         {
4953                           v = allocate_value (type);
4954                           memcpy (value_contents_writeable (v), bytes,
4955                                   TYPE_LENGTH (type));
4956                         }
4957                       else
4958                         v = value_from_longest (type, value);
4959
4960                       /* Specify decimal so that we do not depend on
4961                          the radix.  */
4962                       get_formatted_print_options (&opts, 'd');
4963                       opts.raw = 1;
4964                       value_print (v, buf, &opts);
4965                       release_value (v);
4966                       value_free (v);
4967                     }
4968                 }
4969
4970               die->building_fullname = 0;
4971
4972               if (!first)
4973                 {
4974                   /* Close the argument list, with a space if necessary
4975                      (nested templates).  */
4976                   char last_char = '\0';
4977                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4978                   if (last_char == '>')
4979                     fputs_unfiltered (" >", buf);
4980                   else
4981                     fputs_unfiltered (">", buf);
4982                 }
4983             }
4984
4985           /* For Java and C++ methods, append formal parameter type
4986              information, if PHYSNAME.  */
4987
4988           if (physname && die->tag == DW_TAG_subprogram
4989               && (cu->language == language_cplus
4990                   || cu->language == language_java))
4991             {
4992               struct type *type = read_type_die (die, cu);
4993
4994               c_type_print_args (type, buf, 0, cu->language);
4995
4996               if (cu->language == language_java)
4997                 {
4998                   /* For java, we must append the return type to method
4999                      names.  */
5000                   if (die->tag == DW_TAG_subprogram)
5001                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5002                                      0, 0);
5003                 }
5004               else if (cu->language == language_cplus)
5005                 {
5006                   /* Assume that an artificial first parameter is
5007                      "this", but do not crash if it is not.  RealView
5008                      marks unnamed (and thus unused) parameters as
5009                      artificial; there is no way to differentiate
5010                      the two cases.  */
5011                   if (TYPE_NFIELDS (type) > 0
5012                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5013                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5014                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5015                                                                         0))))
5016                     fputs_unfiltered (" const", buf);
5017                 }
5018             }
5019
5020           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5021                                        &length);
5022           ui_file_delete (buf);
5023
5024           if (cu->language == language_cplus)
5025             {
5026               char *cname
5027                 = dwarf2_canonicalize_name (name, cu,
5028                                             &cu->objfile->objfile_obstack);
5029
5030               if (cname != NULL)
5031                 name = cname;
5032             }
5033         }
5034     }
5035
5036   return name;
5037 }
5038
5039 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5040    If scope qualifiers are appropriate they will be added.  The result
5041    will be allocated on the objfile_obstack, or NULL if the DIE does
5042    not have a name.  NAME may either be from a previous call to
5043    dwarf2_name or NULL.
5044
5045    The output string will be canonicalized (if C++/Java).  */
5046
5047 static const char *
5048 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5049 {
5050   return dwarf2_compute_name (name, die, cu, 0);
5051 }
5052
5053 /* Construct a physname for the given DIE in CU.  NAME may either be
5054    from a previous call to dwarf2_name or NULL.  The result will be
5055    allocated on the objfile_objstack or NULL if the DIE does not have a
5056    name.
5057
5058    The output string will be canonicalized (if C++/Java).  */
5059
5060 static const char *
5061 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5062 {
5063   return dwarf2_compute_name (name, die, cu, 1);
5064 }
5065
5066 /* Read the import statement specified by the given die and record it.  */
5067
5068 static void
5069 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5070 {
5071   struct attribute *import_attr;
5072   struct die_info *imported_die;
5073   struct dwarf2_cu *imported_cu;
5074   const char *imported_name;
5075   const char *imported_name_prefix;
5076   const char *canonical_name;
5077   const char *import_alias;
5078   const char *imported_declaration = NULL;
5079   const char *import_prefix;
5080
5081   char *temp;
5082
5083   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5084   if (import_attr == NULL)
5085     {
5086       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5087                  dwarf_tag_name (die->tag));
5088       return;
5089     }
5090
5091   imported_cu = cu;
5092   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5093   imported_name = dwarf2_name (imported_die, imported_cu);
5094   if (imported_name == NULL)
5095     {
5096       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5097
5098         The import in the following code:
5099         namespace A
5100           {
5101             typedef int B;
5102           }
5103
5104         int main ()
5105           {
5106             using A::B;
5107             B b;
5108             return b;
5109           }
5110
5111         ...
5112          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5113             <52>   DW_AT_decl_file   : 1
5114             <53>   DW_AT_decl_line   : 6
5115             <54>   DW_AT_import      : <0x75>
5116          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5117             <59>   DW_AT_name        : B
5118             <5b>   DW_AT_decl_file   : 1
5119             <5c>   DW_AT_decl_line   : 2
5120             <5d>   DW_AT_type        : <0x6e>
5121         ...
5122          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5123             <76>   DW_AT_byte_size   : 4
5124             <77>   DW_AT_encoding    : 5        (signed)
5125
5126         imports the wrong die ( 0x75 instead of 0x58 ).
5127         This case will be ignored until the gcc bug is fixed.  */
5128       return;
5129     }
5130
5131   /* Figure out the local name after import.  */
5132   import_alias = dwarf2_name (die, cu);
5133
5134   /* Figure out where the statement is being imported to.  */
5135   import_prefix = determine_prefix (die, cu);
5136
5137   /* Figure out what the scope of the imported die is and prepend it
5138      to the name of the imported die.  */
5139   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5140
5141   if (imported_die->tag != DW_TAG_namespace
5142       && imported_die->tag != DW_TAG_module)
5143     {
5144       imported_declaration = imported_name;
5145       canonical_name = imported_name_prefix;
5146     }
5147   else if (strlen (imported_name_prefix) > 0)
5148     {
5149       temp = alloca (strlen (imported_name_prefix)
5150                      + 2 + strlen (imported_name) + 1);
5151       strcpy (temp, imported_name_prefix);
5152       strcat (temp, "::");
5153       strcat (temp, imported_name);
5154       canonical_name = temp;
5155     }
5156   else
5157     canonical_name = imported_name;
5158
5159   cp_add_using_directive (import_prefix,
5160                           canonical_name,
5161                           import_alias,
5162                           imported_declaration,
5163                           &cu->objfile->objfile_obstack);
5164 }
5165
5166 static void
5167 initialize_cu_func_list (struct dwarf2_cu *cu)
5168 {
5169   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5170 }
5171
5172 /* Cleanup function for read_file_scope.  */
5173
5174 static void
5175 free_cu_line_header (void *arg)
5176 {
5177   struct dwarf2_cu *cu = arg;
5178
5179   free_line_header (cu->line_header);
5180   cu->line_header = NULL;
5181 }
5182
5183 static void
5184 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5185                          char **name, char **comp_dir)
5186 {
5187   struct attribute *attr;
5188
5189   *name = NULL;
5190   *comp_dir = NULL;
5191
5192   /* Find the filename.  Do not use dwarf2_name here, since the filename
5193      is not a source language identifier.  */
5194   attr = dwarf2_attr (die, DW_AT_name, cu);
5195   if (attr)
5196     {
5197       *name = DW_STRING (attr);
5198     }
5199
5200   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5201   if (attr)
5202     *comp_dir = DW_STRING (attr);
5203   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5204     {
5205       *comp_dir = ldirname (*name);
5206       if (*comp_dir != NULL)
5207         make_cleanup (xfree, *comp_dir);
5208     }
5209   if (*comp_dir != NULL)
5210     {
5211       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5212          directory, get rid of it.  */
5213       char *cp = strchr (*comp_dir, ':');
5214
5215       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5216         *comp_dir = cp + 1;
5217     }
5218
5219   if (*name == NULL)
5220     *name = "<unknown>";
5221 }
5222
5223 /* Process DW_TAG_compile_unit.  */
5224
5225 static void
5226 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5227 {
5228   struct objfile *objfile = cu->objfile;
5229   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5230   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5231   CORE_ADDR highpc = ((CORE_ADDR) 0);
5232   struct attribute *attr;
5233   char *name = NULL;
5234   char *comp_dir = NULL;
5235   struct die_info *child_die;
5236   bfd *abfd = objfile->obfd;
5237   struct line_header *line_header = 0;
5238   CORE_ADDR baseaddr;
5239
5240   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5241
5242   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5243
5244   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5245      from finish_block.  */
5246   if (lowpc == ((CORE_ADDR) -1))
5247     lowpc = highpc;
5248   lowpc += baseaddr;
5249   highpc += baseaddr;
5250
5251   find_file_and_directory (die, cu, &name, &comp_dir);
5252
5253   attr = dwarf2_attr (die, DW_AT_language, cu);
5254   if (attr)
5255     {
5256       set_cu_language (DW_UNSND (attr), cu);
5257     }
5258
5259   attr = dwarf2_attr (die, DW_AT_producer, cu);
5260   if (attr)
5261     cu->producer = DW_STRING (attr);
5262
5263   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5264      standardised yet.  As a workaround for the language detection we fall
5265      back to the DW_AT_producer string.  */
5266   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5267     cu->language = language_opencl;
5268
5269   /* We assume that we're processing GCC output.  */
5270   processing_gcc_compilation = 2;
5271
5272   processing_has_namespace_info = 0;
5273
5274   start_symtab (name, comp_dir, lowpc);
5275   record_debugformat ("DWARF 2");
5276   record_producer (cu->producer);
5277
5278   initialize_cu_func_list (cu);
5279
5280   /* Decode line number information if present.  We do this before
5281      processing child DIEs, so that the line header table is available
5282      for DW_AT_decl_file.  */
5283   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5284   if (attr)
5285     {
5286       unsigned int line_offset = DW_UNSND (attr);
5287       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5288       if (line_header)
5289         {
5290           cu->line_header = line_header;
5291           make_cleanup (free_cu_line_header, cu);
5292           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5293         }
5294     }
5295
5296   /* Process all dies in compilation unit.  */
5297   if (die->child != NULL)
5298     {
5299       child_die = die->child;
5300       while (child_die && child_die->tag)
5301         {
5302           process_die (child_die, cu);
5303           child_die = sibling_die (child_die);
5304         }
5305     }
5306
5307   /* Decode macro information, if present.  Dwarf 2 macro information
5308      refers to information in the line number info statement program
5309      header, so we can only read it if we've read the header
5310      successfully.  */
5311   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5312   if (attr && line_header)
5313     {
5314       unsigned int macro_offset = DW_UNSND (attr);
5315
5316       dwarf_decode_macros (line_header, macro_offset,
5317                            comp_dir, abfd, cu);
5318     }
5319   do_cleanups (back_to);
5320 }
5321
5322 /* Process DW_TAG_type_unit.
5323    For TUs we want to skip the first top level sibling if it's not the
5324    actual type being defined by this TU.  In this case the first top
5325    level sibling is there to provide context only.  */
5326
5327 static void
5328 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5329 {
5330   struct objfile *objfile = cu->objfile;
5331   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5332   CORE_ADDR lowpc;
5333   struct attribute *attr;
5334   char *name = NULL;
5335   char *comp_dir = NULL;
5336   struct die_info *child_die;
5337   bfd *abfd = objfile->obfd;
5338
5339   /* start_symtab needs a low pc, but we don't really have one.
5340      Do what read_file_scope would do in the absence of such info.  */
5341   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5342
5343   /* Find the filename.  Do not use dwarf2_name here, since the filename
5344      is not a source language identifier.  */
5345   attr = dwarf2_attr (die, DW_AT_name, cu);
5346   if (attr)
5347     name = DW_STRING (attr);
5348
5349   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5350   if (attr)
5351     comp_dir = DW_STRING (attr);
5352   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5353     {
5354       comp_dir = ldirname (name);
5355       if (comp_dir != NULL)
5356         make_cleanup (xfree, comp_dir);
5357     }
5358
5359   if (name == NULL)
5360     name = "<unknown>";
5361
5362   attr = dwarf2_attr (die, DW_AT_language, cu);
5363   if (attr)
5364     set_cu_language (DW_UNSND (attr), cu);
5365
5366   /* This isn't technically needed today.  It is done for symmetry
5367      with read_file_scope.  */
5368   attr = dwarf2_attr (die, DW_AT_producer, cu);
5369   if (attr)
5370     cu->producer = DW_STRING (attr);
5371
5372   /* We assume that we're processing GCC output.  */
5373   processing_gcc_compilation = 2;
5374
5375   processing_has_namespace_info = 0;
5376
5377   start_symtab (name, comp_dir, lowpc);
5378   record_debugformat ("DWARF 2");
5379   record_producer (cu->producer);
5380
5381   /* Process the dies in the type unit.  */
5382   if (die->child == NULL)
5383     {
5384       dump_die_for_error (die);
5385       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5386              bfd_get_filename (abfd));
5387     }
5388
5389   child_die = die->child;
5390
5391   while (child_die && child_die->tag)
5392     {
5393       process_die (child_die, cu);
5394
5395       child_die = sibling_die (child_die);
5396     }
5397
5398   do_cleanups (back_to);
5399 }
5400
5401 static void
5402 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5403                      struct dwarf2_cu *cu)
5404 {
5405   struct function_range *thisfn;
5406
5407   thisfn = (struct function_range *)
5408     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5409   thisfn->name = name;
5410   thisfn->lowpc = lowpc;
5411   thisfn->highpc = highpc;
5412   thisfn->seen_line = 0;
5413   thisfn->next = NULL;
5414
5415   if (cu->last_fn == NULL)
5416       cu->first_fn = thisfn;
5417   else
5418       cu->last_fn->next = thisfn;
5419
5420   cu->last_fn = thisfn;
5421 }
5422
5423 /* qsort helper for inherit_abstract_dies.  */
5424
5425 static int
5426 unsigned_int_compar (const void *ap, const void *bp)
5427 {
5428   unsigned int a = *(unsigned int *) ap;
5429   unsigned int b = *(unsigned int *) bp;
5430
5431   return (a > b) - (b > a);
5432 }
5433
5434 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5435    Inherit only the children of the DW_AT_abstract_origin DIE not being
5436    already referenced by DW_AT_abstract_origin from the children of the
5437    current DIE.  */
5438
5439 static void
5440 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5441 {
5442   struct die_info *child_die;
5443   unsigned die_children_count;
5444   /* CU offsets which were referenced by children of the current DIE.  */
5445   unsigned *offsets;
5446   unsigned *offsets_end, *offsetp;
5447   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5448   struct die_info *origin_die;
5449   /* Iterator of the ORIGIN_DIE children.  */
5450   struct die_info *origin_child_die;
5451   struct cleanup *cleanups;
5452   struct attribute *attr;
5453   struct dwarf2_cu *origin_cu;
5454   struct pending **origin_previous_list_in_scope;
5455
5456   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5457   if (!attr)
5458     return;
5459
5460   /* Note that following die references may follow to a die in a
5461      different cu.  */
5462
5463   origin_cu = cu;
5464   origin_die = follow_die_ref (die, attr, &origin_cu);
5465
5466   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5467      symbols in.  */
5468   origin_previous_list_in_scope = origin_cu->list_in_scope;
5469   origin_cu->list_in_scope = cu->list_in_scope;
5470
5471   if (die->tag != origin_die->tag
5472       && !(die->tag == DW_TAG_inlined_subroutine
5473            && origin_die->tag == DW_TAG_subprogram))
5474     complaint (&symfile_complaints,
5475                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5476                die->offset, origin_die->offset);
5477
5478   child_die = die->child;
5479   die_children_count = 0;
5480   while (child_die && child_die->tag)
5481     {
5482       child_die = sibling_die (child_die);
5483       die_children_count++;
5484     }
5485   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5486   cleanups = make_cleanup (xfree, offsets);
5487
5488   offsets_end = offsets;
5489   child_die = die->child;
5490   while (child_die && child_die->tag)
5491     {
5492       /* For each CHILD_DIE, find the corresponding child of
5493          ORIGIN_DIE.  If there is more than one layer of
5494          DW_AT_abstract_origin, follow them all; there shouldn't be,
5495          but GCC versions at least through 4.4 generate this (GCC PR
5496          40573).  */
5497       struct die_info *child_origin_die = child_die;
5498       struct dwarf2_cu *child_origin_cu = cu;
5499
5500       while (1)
5501         {
5502           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5503                               child_origin_cu);
5504           if (attr == NULL)
5505             break;
5506           child_origin_die = follow_die_ref (child_origin_die, attr,
5507                                              &child_origin_cu);
5508         }
5509
5510       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5511          counterpart may exist.  */
5512       if (child_origin_die != child_die)
5513         {
5514           if (child_die->tag != child_origin_die->tag
5515               && !(child_die->tag == DW_TAG_inlined_subroutine
5516                    && child_origin_die->tag == DW_TAG_subprogram))
5517             complaint (&symfile_complaints,
5518                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5519                          "different tags"), child_die->offset,
5520                        child_origin_die->offset);
5521           if (child_origin_die->parent != origin_die)
5522             complaint (&symfile_complaints,
5523                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5524                          "different parents"), child_die->offset,
5525                        child_origin_die->offset);
5526           else
5527             *offsets_end++ = child_origin_die->offset;
5528         }
5529       child_die = sibling_die (child_die);
5530     }
5531   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5532          unsigned_int_compar);
5533   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5534     if (offsetp[-1] == *offsetp)
5535       complaint (&symfile_complaints,
5536                  _("Multiple children of DIE 0x%x refer "
5537                    "to DIE 0x%x as their abstract origin"),
5538                  die->offset, *offsetp);
5539
5540   offsetp = offsets;
5541   origin_child_die = origin_die->child;
5542   while (origin_child_die && origin_child_die->tag)
5543     {
5544       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5545       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5546         offsetp++;
5547       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5548         {
5549           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5550           process_die (origin_child_die, origin_cu);
5551         }
5552       origin_child_die = sibling_die (origin_child_die);
5553     }
5554   origin_cu->list_in_scope = origin_previous_list_in_scope;
5555
5556   do_cleanups (cleanups);
5557 }
5558
5559 static void
5560 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5561 {
5562   struct objfile *objfile = cu->objfile;
5563   struct context_stack *new;
5564   CORE_ADDR lowpc;
5565   CORE_ADDR highpc;
5566   struct die_info *child_die;
5567   struct attribute *attr, *call_line, *call_file;
5568   char *name;
5569   CORE_ADDR baseaddr;
5570   struct block *block;
5571   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5572   VEC (symbolp) *template_args = NULL;
5573   struct template_symbol *templ_func = NULL;
5574
5575   if (inlined_func)
5576     {
5577       /* If we do not have call site information, we can't show the
5578          caller of this inlined function.  That's too confusing, so
5579          only use the scope for local variables.  */
5580       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5581       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5582       if (call_line == NULL || call_file == NULL)
5583         {
5584           read_lexical_block_scope (die, cu);
5585           return;
5586         }
5587     }
5588
5589   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5590
5591   name = dwarf2_name (die, cu);
5592
5593   /* Ignore functions with missing or empty names.  These are actually
5594      illegal according to the DWARF standard.  */
5595   if (name == NULL)
5596     {
5597       complaint (&symfile_complaints,
5598                  _("missing name for subprogram DIE at %d"), die->offset);
5599       return;
5600     }
5601
5602   /* Ignore functions with missing or invalid low and high pc attributes.  */
5603   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5604     {
5605       attr = dwarf2_attr (die, DW_AT_external, cu);
5606       if (!attr || !DW_UNSND (attr))
5607         complaint (&symfile_complaints,
5608                    _("cannot get low and high bounds "
5609                      "for subprogram DIE at %d"),
5610                    die->offset);
5611       return;
5612     }
5613
5614   lowpc += baseaddr;
5615   highpc += baseaddr;
5616
5617   /* Record the function range for dwarf_decode_lines.  */
5618   add_to_cu_func_list (name, lowpc, highpc, cu);
5619
5620   /* If we have any template arguments, then we must allocate a
5621      different sort of symbol.  */
5622   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5623     {
5624       if (child_die->tag == DW_TAG_template_type_param
5625           || child_die->tag == DW_TAG_template_value_param)
5626         {
5627           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5628                                        struct template_symbol);
5629           templ_func->base.is_cplus_template_function = 1;
5630           break;
5631         }
5632     }
5633
5634   new = push_context (0, lowpc);
5635   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5636                                (struct symbol *) templ_func);
5637
5638   /* If there is a location expression for DW_AT_frame_base, record
5639      it.  */
5640   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5641   if (attr)
5642     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5643        expression is being recorded directly in the function's symbol
5644        and not in a separate frame-base object.  I guess this hack is
5645        to avoid adding some sort of frame-base adjunct/annex to the
5646        function's symbol :-(.  The problem with doing this is that it
5647        results in a function symbol with a location expression that
5648        has nothing to do with the location of the function, ouch!  The
5649        relationship should be: a function's symbol has-a frame base; a
5650        frame-base has-a location expression.  */
5651     dwarf2_symbol_mark_computed (attr, new->name, cu);
5652
5653   cu->list_in_scope = &local_symbols;
5654
5655   if (die->child != NULL)
5656     {
5657       child_die = die->child;
5658       while (child_die && child_die->tag)
5659         {
5660           if (child_die->tag == DW_TAG_template_type_param
5661               || child_die->tag == DW_TAG_template_value_param)
5662             {
5663               struct symbol *arg = new_symbol (child_die, NULL, cu);
5664
5665               if (arg != NULL)
5666                 VEC_safe_push (symbolp, template_args, arg);
5667             }
5668           else
5669             process_die (child_die, cu);
5670           child_die = sibling_die (child_die);
5671         }
5672     }
5673
5674   inherit_abstract_dies (die, cu);
5675
5676   /* If we have a DW_AT_specification, we might need to import using
5677      directives from the context of the specification DIE.  See the
5678      comment in determine_prefix.  */
5679   if (cu->language == language_cplus
5680       && dwarf2_attr (die, DW_AT_specification, cu))
5681     {
5682       struct dwarf2_cu *spec_cu = cu;
5683       struct die_info *spec_die = die_specification (die, &spec_cu);
5684
5685       while (spec_die)
5686         {
5687           child_die = spec_die->child;
5688           while (child_die && child_die->tag)
5689             {
5690               if (child_die->tag == DW_TAG_imported_module)
5691                 process_die (child_die, spec_cu);
5692               child_die = sibling_die (child_die);
5693             }
5694
5695           /* In some cases, GCC generates specification DIEs that
5696              themselves contain DW_AT_specification attributes.  */
5697           spec_die = die_specification (spec_die, &spec_cu);
5698         }
5699     }
5700
5701   new = pop_context ();
5702   /* Make a block for the local symbols within.  */
5703   block = finish_block (new->name, &local_symbols, new->old_blocks,
5704                         lowpc, highpc, objfile);
5705
5706   /* For C++, set the block's scope.  */
5707   if (cu->language == language_cplus || cu->language == language_fortran)
5708     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5709                         determine_prefix (die, cu),
5710                         processing_has_namespace_info);
5711
5712   /* If we have address ranges, record them.  */
5713   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5714
5715   /* Attach template arguments to function.  */
5716   if (! VEC_empty (symbolp, template_args))
5717     {
5718       gdb_assert (templ_func != NULL);
5719
5720       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5721       templ_func->template_arguments
5722         = obstack_alloc (&objfile->objfile_obstack,
5723                          (templ_func->n_template_arguments
5724                           * sizeof (struct symbol *)));
5725       memcpy (templ_func->template_arguments,
5726               VEC_address (symbolp, template_args),
5727               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5728       VEC_free (symbolp, template_args);
5729     }
5730
5731   /* In C++, we can have functions nested inside functions (e.g., when
5732      a function declares a class that has methods).  This means that
5733      when we finish processing a function scope, we may need to go
5734      back to building a containing block's symbol lists.  */
5735   local_symbols = new->locals;
5736   param_symbols = new->params;
5737   using_directives = new->using_directives;
5738
5739   /* If we've finished processing a top-level function, subsequent
5740      symbols go in the file symbol list.  */
5741   if (outermost_context_p ())
5742     cu->list_in_scope = &file_symbols;
5743 }
5744
5745 /* Process all the DIES contained within a lexical block scope.  Start
5746    a new scope, process the dies, and then close the scope.  */
5747
5748 static void
5749 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5750 {
5751   struct objfile *objfile = cu->objfile;
5752   struct context_stack *new;
5753   CORE_ADDR lowpc, highpc;
5754   struct die_info *child_die;
5755   CORE_ADDR baseaddr;
5756
5757   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5758
5759   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5760   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5761      as multiple lexical blocks?  Handling children in a sane way would
5762      be nasty.  Might be easier to properly extend generic blocks to
5763      describe ranges.  */
5764   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5765     return;
5766   lowpc += baseaddr;
5767   highpc += baseaddr;
5768
5769   push_context (0, lowpc);
5770   if (die->child != NULL)
5771     {
5772       child_die = die->child;
5773       while (child_die && child_die->tag)
5774         {
5775           process_die (child_die, cu);
5776           child_die = sibling_die (child_die);
5777         }
5778     }
5779   new = pop_context ();
5780
5781   if (local_symbols != NULL || using_directives != NULL)
5782     {
5783       struct block *block
5784         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5785                         highpc, objfile);
5786
5787       /* Note that recording ranges after traversing children, as we
5788          do here, means that recording a parent's ranges entails
5789          walking across all its children's ranges as they appear in
5790          the address map, which is quadratic behavior.
5791
5792          It would be nicer to record the parent's ranges before
5793          traversing its children, simply overriding whatever you find
5794          there.  But since we don't even decide whether to create a
5795          block until after we've traversed its children, that's hard
5796          to do.  */
5797       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5798     }
5799   local_symbols = new->locals;
5800   using_directives = new->using_directives;
5801 }
5802
5803 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5804    Return 1 if the attributes are present and valid, otherwise, return 0.
5805    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5806
5807 static int
5808 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5809                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5810                     struct partial_symtab *ranges_pst)
5811 {
5812   struct objfile *objfile = cu->objfile;
5813   struct comp_unit_head *cu_header = &cu->header;
5814   bfd *obfd = objfile->obfd;
5815   unsigned int addr_size = cu_header->addr_size;
5816   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5817   /* Base address selection entry.  */
5818   CORE_ADDR base;
5819   int found_base;
5820   unsigned int dummy;
5821   gdb_byte *buffer;
5822   CORE_ADDR marker;
5823   int low_set;
5824   CORE_ADDR low = 0;
5825   CORE_ADDR high = 0;
5826   CORE_ADDR baseaddr;
5827
5828   found_base = cu->base_known;
5829   base = cu->base_address;
5830
5831   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5832   if (offset >= dwarf2_per_objfile->ranges.size)
5833     {
5834       complaint (&symfile_complaints,
5835                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5836                  offset);
5837       return 0;
5838     }
5839   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5840
5841   /* Read in the largest possible address.  */
5842   marker = read_address (obfd, buffer, cu, &dummy);
5843   if ((marker & mask) == mask)
5844     {
5845       /* If we found the largest possible address, then
5846          read the base address.  */
5847       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5848       buffer += 2 * addr_size;
5849       offset += 2 * addr_size;
5850       found_base = 1;
5851     }
5852
5853   low_set = 0;
5854
5855   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5856
5857   while (1)
5858     {
5859       CORE_ADDR range_beginning, range_end;
5860
5861       range_beginning = read_address (obfd, buffer, cu, &dummy);
5862       buffer += addr_size;
5863       range_end = read_address (obfd, buffer, cu, &dummy);
5864       buffer += addr_size;
5865       offset += 2 * addr_size;
5866
5867       /* An end of list marker is a pair of zero addresses.  */
5868       if (range_beginning == 0 && range_end == 0)
5869         /* Found the end of list entry.  */
5870         break;
5871
5872       /* Each base address selection entry is a pair of 2 values.
5873          The first is the largest possible address, the second is
5874          the base address.  Check for a base address here.  */
5875       if ((range_beginning & mask) == mask)
5876         {
5877           /* If we found the largest possible address, then
5878              read the base address.  */
5879           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5880           found_base = 1;
5881           continue;
5882         }
5883
5884       if (!found_base)
5885         {
5886           /* We have no valid base address for the ranges
5887              data.  */
5888           complaint (&symfile_complaints,
5889                      _("Invalid .debug_ranges data (no base address)"));
5890           return 0;
5891         }
5892
5893       if (range_beginning > range_end)
5894         {
5895           /* Inverted range entries are invalid.  */
5896           complaint (&symfile_complaints,
5897                      _("Invalid .debug_ranges data (inverted range)"));
5898           return 0;
5899         }
5900
5901       /* Empty range entries have no effect.  */
5902       if (range_beginning == range_end)
5903         continue;
5904
5905       range_beginning += base;
5906       range_end += base;
5907
5908       if (ranges_pst != NULL)
5909         addrmap_set_empty (objfile->psymtabs_addrmap,
5910                            range_beginning + baseaddr,
5911                            range_end - 1 + baseaddr,
5912                            ranges_pst);
5913
5914       /* FIXME: This is recording everything as a low-high
5915          segment of consecutive addresses.  We should have a
5916          data structure for discontiguous block ranges
5917          instead.  */
5918       if (! low_set)
5919         {
5920           low = range_beginning;
5921           high = range_end;
5922           low_set = 1;
5923         }
5924       else
5925         {
5926           if (range_beginning < low)
5927             low = range_beginning;
5928           if (range_end > high)
5929             high = range_end;
5930         }
5931     }
5932
5933   if (! low_set)
5934     /* If the first entry is an end-of-list marker, the range
5935        describes an empty scope, i.e. no instructions.  */
5936     return 0;
5937
5938   if (low_return)
5939     *low_return = low;
5940   if (high_return)
5941     *high_return = high;
5942   return 1;
5943 }
5944
5945 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5946    are present and valid, otherwise, return 0.  Return -1 if the range is
5947    discontinuous, i.e. derived from DW_AT_ranges information.  */
5948 static int
5949 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5950                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5951                       struct partial_symtab *pst)
5952 {
5953   struct attribute *attr;
5954   CORE_ADDR low = 0;
5955   CORE_ADDR high = 0;
5956   int ret = 0;
5957
5958   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5959   if (attr)
5960     {
5961       high = DW_ADDR (attr);
5962       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5963       if (attr)
5964         low = DW_ADDR (attr);
5965       else
5966         /* Found high w/o low attribute.  */
5967         return 0;
5968
5969       /* Found consecutive range of addresses.  */
5970       ret = 1;
5971     }
5972   else
5973     {
5974       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5975       if (attr != NULL)
5976         {
5977           /* Value of the DW_AT_ranges attribute is the offset in the
5978              .debug_ranges section.  */
5979           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5980             return 0;
5981           /* Found discontinuous range of addresses.  */
5982           ret = -1;
5983         }
5984     }
5985
5986   if (high < low)
5987     return 0;
5988
5989   /* When using the GNU linker, .gnu.linkonce. sections are used to
5990      eliminate duplicate copies of functions and vtables and such.
5991      The linker will arbitrarily choose one and discard the others.
5992      The AT_*_pc values for such functions refer to local labels in
5993      these sections.  If the section from that file was discarded, the
5994      labels are not in the output, so the relocs get a value of 0.
5995      If this is a discarded function, mark the pc bounds as invalid,
5996      so that GDB will ignore it.  */
5997   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5998     return 0;
5999
6000   *lowpc = low;
6001   *highpc = high;
6002   return ret;
6003 }
6004
6005 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6006    its low and high PC addresses.  Do nothing if these addresses could not
6007    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6008    and HIGHPC to the high address if greater than HIGHPC.  */
6009
6010 static void
6011 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6012                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6013                                  struct dwarf2_cu *cu)
6014 {
6015   CORE_ADDR low, high;
6016   struct die_info *child = die->child;
6017
6018   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6019     {
6020       *lowpc = min (*lowpc, low);
6021       *highpc = max (*highpc, high);
6022     }
6023
6024   /* If the language does not allow nested subprograms (either inside
6025      subprograms or lexical blocks), we're done.  */
6026   if (cu->language != language_ada)
6027     return;
6028
6029   /* Check all the children of the given DIE.  If it contains nested
6030      subprograms, then check their pc bounds.  Likewise, we need to
6031      check lexical blocks as well, as they may also contain subprogram
6032      definitions.  */
6033   while (child && child->tag)
6034     {
6035       if (child->tag == DW_TAG_subprogram
6036           || child->tag == DW_TAG_lexical_block)
6037         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6038       child = sibling_die (child);
6039     }
6040 }
6041
6042 /* Get the low and high pc's represented by the scope DIE, and store
6043    them in *LOWPC and *HIGHPC.  If the correct values can't be
6044    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6045
6046 static void
6047 get_scope_pc_bounds (struct die_info *die,
6048                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6049                      struct dwarf2_cu *cu)
6050 {
6051   CORE_ADDR best_low = (CORE_ADDR) -1;
6052   CORE_ADDR best_high = (CORE_ADDR) 0;
6053   CORE_ADDR current_low, current_high;
6054
6055   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6056     {
6057       best_low = current_low;
6058       best_high = current_high;
6059     }
6060   else
6061     {
6062       struct die_info *child = die->child;
6063
6064       while (child && child->tag)
6065         {
6066           switch (child->tag) {
6067           case DW_TAG_subprogram:
6068             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6069             break;
6070           case DW_TAG_namespace:
6071           case DW_TAG_module:
6072             /* FIXME: carlton/2004-01-16: Should we do this for
6073                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6074                that current GCC's always emit the DIEs corresponding
6075                to definitions of methods of classes as children of a
6076                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6077                the DIEs giving the declarations, which could be
6078                anywhere).  But I don't see any reason why the
6079                standards says that they have to be there.  */
6080             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6081
6082             if (current_low != ((CORE_ADDR) -1))
6083               {
6084                 best_low = min (best_low, current_low);
6085                 best_high = max (best_high, current_high);
6086               }
6087             break;
6088           default:
6089             /* Ignore.  */
6090             break;
6091           }
6092
6093           child = sibling_die (child);
6094         }
6095     }
6096
6097   *lowpc = best_low;
6098   *highpc = best_high;
6099 }
6100
6101 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6102    in DIE.  */
6103 static void
6104 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6105                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6106 {
6107   struct attribute *attr;
6108
6109   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6110   if (attr)
6111     {
6112       CORE_ADDR high = DW_ADDR (attr);
6113
6114       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6115       if (attr)
6116         {
6117           CORE_ADDR low = DW_ADDR (attr);
6118
6119           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6120         }
6121     }
6122
6123   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6124   if (attr)
6125     {
6126       bfd *obfd = cu->objfile->obfd;
6127
6128       /* The value of the DW_AT_ranges attribute is the offset of the
6129          address range list in the .debug_ranges section.  */
6130       unsigned long offset = DW_UNSND (attr);
6131       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6132
6133       /* For some target architectures, but not others, the
6134          read_address function sign-extends the addresses it returns.
6135          To recognize base address selection entries, we need a
6136          mask.  */
6137       unsigned int addr_size = cu->header.addr_size;
6138       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6139
6140       /* The base address, to which the next pair is relative.  Note
6141          that this 'base' is a DWARF concept: most entries in a range
6142          list are relative, to reduce the number of relocs against the
6143          debugging information.  This is separate from this function's
6144          'baseaddr' argument, which GDB uses to relocate debugging
6145          information from a shared library based on the address at
6146          which the library was loaded.  */
6147       CORE_ADDR base = cu->base_address;
6148       int base_known = cu->base_known;
6149
6150       gdb_assert (dwarf2_per_objfile->ranges.readin);
6151       if (offset >= dwarf2_per_objfile->ranges.size)
6152         {
6153           complaint (&symfile_complaints,
6154                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6155                      offset);
6156           return;
6157         }
6158
6159       for (;;)
6160         {
6161           unsigned int bytes_read;
6162           CORE_ADDR start, end;
6163
6164           start = read_address (obfd, buffer, cu, &bytes_read);
6165           buffer += bytes_read;
6166           end = read_address (obfd, buffer, cu, &bytes_read);
6167           buffer += bytes_read;
6168
6169           /* Did we find the end of the range list?  */
6170           if (start == 0 && end == 0)
6171             break;
6172
6173           /* Did we find a base address selection entry?  */
6174           else if ((start & base_select_mask) == base_select_mask)
6175             {
6176               base = end;
6177               base_known = 1;
6178             }
6179
6180           /* We found an ordinary address range.  */
6181           else
6182             {
6183               if (!base_known)
6184                 {
6185                   complaint (&symfile_complaints,
6186                              _("Invalid .debug_ranges data "
6187                                "(no base address)"));
6188                   return;
6189                 }
6190
6191               if (start > end)
6192                 {
6193                   /* Inverted range entries are invalid.  */
6194                   complaint (&symfile_complaints,
6195                              _("Invalid .debug_ranges data "
6196                                "(inverted range)"));
6197                   return;
6198                 }
6199
6200               /* Empty range entries have no effect.  */
6201               if (start == end)
6202                 continue;
6203
6204               record_block_range (block,
6205                                   baseaddr + base + start,
6206                                   baseaddr + base + end - 1);
6207             }
6208         }
6209     }
6210 }
6211
6212 /* Add an aggregate field to the field list.  */
6213
6214 static void
6215 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6216                   struct dwarf2_cu *cu)
6217 {
6218   struct objfile *objfile = cu->objfile;
6219   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6220   struct nextfield *new_field;
6221   struct attribute *attr;
6222   struct field *fp;
6223   char *fieldname = "";
6224
6225   /* Allocate a new field list entry and link it in.  */
6226   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6227   make_cleanup (xfree, new_field);
6228   memset (new_field, 0, sizeof (struct nextfield));
6229
6230   if (die->tag == DW_TAG_inheritance)
6231     {
6232       new_field->next = fip->baseclasses;
6233       fip->baseclasses = new_field;
6234     }
6235   else
6236     {
6237       new_field->next = fip->fields;
6238       fip->fields = new_field;
6239     }
6240   fip->nfields++;
6241
6242   /* Handle accessibility and virtuality of field.
6243      The default accessibility for members is public, the default
6244      accessibility for inheritance is private.  */
6245   if (die->tag != DW_TAG_inheritance)
6246     new_field->accessibility = DW_ACCESS_public;
6247   else
6248     new_field->accessibility = DW_ACCESS_private;
6249   new_field->virtuality = DW_VIRTUALITY_none;
6250
6251   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6252   if (attr)
6253     new_field->accessibility = DW_UNSND (attr);
6254   if (new_field->accessibility != DW_ACCESS_public)
6255     fip->non_public_fields = 1;
6256   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6257   if (attr)
6258     new_field->virtuality = DW_UNSND (attr);
6259
6260   fp = &new_field->field;
6261
6262   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6263     {
6264       /* Data member other than a C++ static data member.  */
6265
6266       /* Get type of field.  */
6267       fp->type = die_type (die, cu);
6268
6269       SET_FIELD_BITPOS (*fp, 0);
6270
6271       /* Get bit size of field (zero if none).  */
6272       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6273       if (attr)
6274         {
6275           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6276         }
6277       else
6278         {
6279           FIELD_BITSIZE (*fp) = 0;
6280         }
6281
6282       /* Get bit offset of field.  */
6283       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6284       if (attr)
6285         {
6286           int byte_offset = 0;
6287
6288           if (attr_form_is_section_offset (attr))
6289             dwarf2_complex_location_expr_complaint ();
6290           else if (attr_form_is_constant (attr))
6291             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6292           else if (attr_form_is_block (attr))
6293             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6294           else
6295             dwarf2_complex_location_expr_complaint ();
6296
6297           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6298         }
6299       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6300       if (attr)
6301         {
6302           if (gdbarch_bits_big_endian (gdbarch))
6303             {
6304               /* For big endian bits, the DW_AT_bit_offset gives the
6305                  additional bit offset from the MSB of the containing
6306                  anonymous object to the MSB of the field.  We don't
6307                  have to do anything special since we don't need to
6308                  know the size of the anonymous object.  */
6309               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6310             }
6311           else
6312             {
6313               /* For little endian bits, compute the bit offset to the
6314                  MSB of the anonymous object, subtract off the number of
6315                  bits from the MSB of the field to the MSB of the
6316                  object, and then subtract off the number of bits of
6317                  the field itself.  The result is the bit offset of
6318                  the LSB of the field.  */
6319               int anonymous_size;
6320               int bit_offset = DW_UNSND (attr);
6321
6322               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6323               if (attr)
6324                 {
6325                   /* The size of the anonymous object containing
6326                      the bit field is explicit, so use the
6327                      indicated size (in bytes).  */
6328                   anonymous_size = DW_UNSND (attr);
6329                 }
6330               else
6331                 {
6332                   /* The size of the anonymous object containing
6333                      the bit field must be inferred from the type
6334                      attribute of the data member containing the
6335                      bit field.  */
6336                   anonymous_size = TYPE_LENGTH (fp->type);
6337                 }
6338               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6339                 - bit_offset - FIELD_BITSIZE (*fp);
6340             }
6341         }
6342
6343       /* Get name of field.  */
6344       fieldname = dwarf2_name (die, cu);
6345       if (fieldname == NULL)
6346         fieldname = "";
6347
6348       /* The name is already allocated along with this objfile, so we don't
6349          need to duplicate it for the type.  */
6350       fp->name = fieldname;
6351
6352       /* Change accessibility for artificial fields (e.g. virtual table
6353          pointer or virtual base class pointer) to private.  */
6354       if (dwarf2_attr (die, DW_AT_artificial, cu))
6355         {
6356           FIELD_ARTIFICIAL (*fp) = 1;
6357           new_field->accessibility = DW_ACCESS_private;
6358           fip->non_public_fields = 1;
6359         }
6360     }
6361   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6362     {
6363       /* C++ static member.  */
6364
6365       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6366          is a declaration, but all versions of G++ as of this writing
6367          (so through at least 3.2.1) incorrectly generate
6368          DW_TAG_variable tags.  */
6369
6370       char *physname;
6371
6372       /* Get name of field.  */
6373       fieldname = dwarf2_name (die, cu);
6374       if (fieldname == NULL)
6375         return;
6376
6377       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6378       if (attr
6379           /* Only create a symbol if this is an external value.
6380              new_symbol checks this and puts the value in the global symbol
6381              table, which we want.  If it is not external, new_symbol
6382              will try to put the value in cu->list_in_scope which is wrong.  */
6383           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6384         {
6385           /* A static const member, not much different than an enum as far as
6386              we're concerned, except that we can support more types.  */
6387           new_symbol (die, NULL, cu);
6388         }
6389
6390       /* Get physical name.  */
6391       physname = (char *) dwarf2_physname (fieldname, die, cu);
6392
6393       /* The name is already allocated along with this objfile, so we don't
6394          need to duplicate it for the type.  */
6395       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6396       FIELD_TYPE (*fp) = die_type (die, cu);
6397       FIELD_NAME (*fp) = fieldname;
6398     }
6399   else if (die->tag == DW_TAG_inheritance)
6400     {
6401       /* C++ base class field.  */
6402       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6403       if (attr)
6404         {
6405           int byte_offset = 0;
6406
6407           if (attr_form_is_section_offset (attr))
6408             dwarf2_complex_location_expr_complaint ();
6409           else if (attr_form_is_constant (attr))
6410             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6411           else if (attr_form_is_block (attr))
6412             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6413           else
6414             dwarf2_complex_location_expr_complaint ();
6415
6416           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6417         }
6418       FIELD_BITSIZE (*fp) = 0;
6419       FIELD_TYPE (*fp) = die_type (die, cu);
6420       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6421       fip->nbaseclasses++;
6422     }
6423 }
6424
6425 /* Add a typedef defined in the scope of the FIP's class.  */
6426
6427 static void
6428 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6429                     struct dwarf2_cu *cu)
6430 {
6431   struct objfile *objfile = cu->objfile;
6432   struct typedef_field_list *new_field;
6433   struct attribute *attr;
6434   struct typedef_field *fp;
6435   char *fieldname = "";
6436
6437   /* Allocate a new field list entry and link it in.  */
6438   new_field = xzalloc (sizeof (*new_field));
6439   make_cleanup (xfree, new_field);
6440
6441   gdb_assert (die->tag == DW_TAG_typedef);
6442
6443   fp = &new_field->field;
6444
6445   /* Get name of field.  */
6446   fp->name = dwarf2_name (die, cu);
6447   if (fp->name == NULL)
6448     return;
6449
6450   fp->type = read_type_die (die, cu);
6451
6452   new_field->next = fip->typedef_field_list;
6453   fip->typedef_field_list = new_field;
6454   fip->typedef_field_list_count++;
6455 }
6456
6457 /* Create the vector of fields, and attach it to the type.  */
6458
6459 static void
6460 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6461                               struct dwarf2_cu *cu)
6462 {
6463   int nfields = fip->nfields;
6464
6465   /* Record the field count, allocate space for the array of fields,
6466      and create blank accessibility bitfields if necessary.  */
6467   TYPE_NFIELDS (type) = nfields;
6468   TYPE_FIELDS (type) = (struct field *)
6469     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6470   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6471
6472   if (fip->non_public_fields && cu->language != language_ada)
6473     {
6474       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6475
6476       TYPE_FIELD_PRIVATE_BITS (type) =
6477         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6478       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6479
6480       TYPE_FIELD_PROTECTED_BITS (type) =
6481         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6482       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6483
6484       TYPE_FIELD_IGNORE_BITS (type) =
6485         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6486       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6487     }
6488
6489   /* If the type has baseclasses, allocate and clear a bit vector for
6490      TYPE_FIELD_VIRTUAL_BITS.  */
6491   if (fip->nbaseclasses && cu->language != language_ada)
6492     {
6493       int num_bytes = B_BYTES (fip->nbaseclasses);
6494       unsigned char *pointer;
6495
6496       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6497       pointer = TYPE_ALLOC (type, num_bytes);
6498       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6499       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6500       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6501     }
6502
6503   /* Copy the saved-up fields into the field vector.  Start from the head of
6504      the list, adding to the tail of the field array, so that they end up in
6505      the same order in the array in which they were added to the list.  */
6506   while (nfields-- > 0)
6507     {
6508       struct nextfield *fieldp;
6509
6510       if (fip->fields)
6511         {
6512           fieldp = fip->fields;
6513           fip->fields = fieldp->next;
6514         }
6515       else
6516         {
6517           fieldp = fip->baseclasses;
6518           fip->baseclasses = fieldp->next;
6519         }
6520
6521       TYPE_FIELD (type, nfields) = fieldp->field;
6522       switch (fieldp->accessibility)
6523         {
6524         case DW_ACCESS_private:
6525           if (cu->language != language_ada)
6526             SET_TYPE_FIELD_PRIVATE (type, nfields);
6527           break;
6528
6529         case DW_ACCESS_protected:
6530           if (cu->language != language_ada)
6531             SET_TYPE_FIELD_PROTECTED (type, nfields);
6532           break;
6533
6534         case DW_ACCESS_public:
6535           break;
6536
6537         default:
6538           /* Unknown accessibility.  Complain and treat it as public.  */
6539           {
6540             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6541                        fieldp->accessibility);
6542           }
6543           break;
6544         }
6545       if (nfields < fip->nbaseclasses)
6546         {
6547           switch (fieldp->virtuality)
6548             {
6549             case DW_VIRTUALITY_virtual:
6550             case DW_VIRTUALITY_pure_virtual:
6551               if (cu->language == language_ada)
6552                 error (_("unexpected virtuality in component of Ada type"));
6553               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6554               break;
6555             }
6556         }
6557     }
6558 }
6559
6560 /* Add a member function to the proper fieldlist.  */
6561
6562 static void
6563 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6564                       struct type *type, struct dwarf2_cu *cu)
6565 {
6566   struct objfile *objfile = cu->objfile;
6567   struct attribute *attr;
6568   struct fnfieldlist *flp;
6569   int i;
6570   struct fn_field *fnp;
6571   char *fieldname;
6572   struct nextfnfield *new_fnfield;
6573   struct type *this_type;
6574
6575   if (cu->language == language_ada)
6576     error (_("unexpected member function in Ada type"));
6577
6578   /* Get name of member function.  */
6579   fieldname = dwarf2_name (die, cu);
6580   if (fieldname == NULL)
6581     return;
6582
6583   /* Look up member function name in fieldlist.  */
6584   for (i = 0; i < fip->nfnfields; i++)
6585     {
6586       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6587         break;
6588     }
6589
6590   /* Create new list element if necessary.  */
6591   if (i < fip->nfnfields)
6592     flp = &fip->fnfieldlists[i];
6593   else
6594     {
6595       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6596         {
6597           fip->fnfieldlists = (struct fnfieldlist *)
6598             xrealloc (fip->fnfieldlists,
6599                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6600                       * sizeof (struct fnfieldlist));
6601           if (fip->nfnfields == 0)
6602             make_cleanup (free_current_contents, &fip->fnfieldlists);
6603         }
6604       flp = &fip->fnfieldlists[fip->nfnfields];
6605       flp->name = fieldname;
6606       flp->length = 0;
6607       flp->head = NULL;
6608       i = fip->nfnfields++;
6609     }
6610
6611   /* Create a new member function field and chain it to the field list
6612      entry.  */
6613   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6614   make_cleanup (xfree, new_fnfield);
6615   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6616   new_fnfield->next = flp->head;
6617   flp->head = new_fnfield;
6618   flp->length++;
6619
6620   /* Fill in the member function field info.  */
6621   fnp = &new_fnfield->fnfield;
6622
6623   /* Delay processing of the physname until later.  */
6624   if (cu->language == language_cplus || cu->language == language_java)
6625     {
6626       add_to_method_list (type, i, flp->length - 1, fieldname,
6627                           die, cu);
6628     }
6629   else
6630     {
6631       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6632       fnp->physname = physname ? physname : "";
6633     }
6634
6635   fnp->type = alloc_type (objfile);
6636   this_type = read_type_die (die, cu);
6637   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6638     {
6639       int nparams = TYPE_NFIELDS (this_type);
6640
6641       /* TYPE is the domain of this method, and THIS_TYPE is the type
6642            of the method itself (TYPE_CODE_METHOD).  */
6643       smash_to_method_type (fnp->type, type,
6644                             TYPE_TARGET_TYPE (this_type),
6645                             TYPE_FIELDS (this_type),
6646                             TYPE_NFIELDS (this_type),
6647                             TYPE_VARARGS (this_type));
6648
6649       /* Handle static member functions.
6650          Dwarf2 has no clean way to discern C++ static and non-static
6651          member functions.  G++ helps GDB by marking the first
6652          parameter for non-static member functions (which is the this
6653          pointer) as artificial.  We obtain this information from
6654          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6655       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6656         fnp->voffset = VOFFSET_STATIC;
6657     }
6658   else
6659     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6660                dwarf2_full_name (fieldname, die, cu));
6661
6662   /* Get fcontext from DW_AT_containing_type if present.  */
6663   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6664     fnp->fcontext = die_containing_type (die, cu);
6665
6666   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6667      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6668
6669   /* Get accessibility.  */
6670   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6671   if (attr)
6672     {
6673       switch (DW_UNSND (attr))
6674         {
6675         case DW_ACCESS_private:
6676           fnp->is_private = 1;
6677           break;
6678         case DW_ACCESS_protected:
6679           fnp->is_protected = 1;
6680           break;
6681         }
6682     }
6683
6684   /* Check for artificial methods.  */
6685   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6686   if (attr && DW_UNSND (attr) != 0)
6687     fnp->is_artificial = 1;
6688
6689   /* Get index in virtual function table if it is a virtual member
6690      function.  For older versions of GCC, this is an offset in the
6691      appropriate virtual table, as specified by DW_AT_containing_type.
6692      For everyone else, it is an expression to be evaluated relative
6693      to the object address.  */
6694
6695   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6696   if (attr)
6697     {
6698       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6699         {
6700           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6701             {
6702               /* Old-style GCC.  */
6703               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6704             }
6705           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6706                    || (DW_BLOCK (attr)->size > 1
6707                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6708                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6709             {
6710               struct dwarf_block blk;
6711               int offset;
6712
6713               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6714                         ? 1 : 2);
6715               blk.size = DW_BLOCK (attr)->size - offset;
6716               blk.data = DW_BLOCK (attr)->data + offset;
6717               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6718               if ((fnp->voffset % cu->header.addr_size) != 0)
6719                 dwarf2_complex_location_expr_complaint ();
6720               else
6721                 fnp->voffset /= cu->header.addr_size;
6722               fnp->voffset += 2;
6723             }
6724           else
6725             dwarf2_complex_location_expr_complaint ();
6726
6727           if (!fnp->fcontext)
6728             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6729         }
6730       else if (attr_form_is_section_offset (attr))
6731         {
6732           dwarf2_complex_location_expr_complaint ();
6733         }
6734       else
6735         {
6736           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6737                                                  fieldname);
6738         }
6739     }
6740   else
6741     {
6742       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6743       if (attr && DW_UNSND (attr))
6744         {
6745           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6746           complaint (&symfile_complaints,
6747                      _("Member function \"%s\" (offset %d) is virtual "
6748                        "but the vtable offset is not specified"),
6749                      fieldname, die->offset);
6750           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6751           TYPE_CPLUS_DYNAMIC (type) = 1;
6752         }
6753     }
6754 }
6755
6756 /* Create the vector of member function fields, and attach it to the type.  */
6757
6758 static void
6759 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6760                                  struct dwarf2_cu *cu)
6761 {
6762   struct fnfieldlist *flp;
6763   int total_length = 0;
6764   int i;
6765
6766   if (cu->language == language_ada)
6767     error (_("unexpected member functions in Ada type"));
6768
6769   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6770   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6771     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6772
6773   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6774     {
6775       struct nextfnfield *nfp = flp->head;
6776       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6777       int k;
6778
6779       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6780       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6781       fn_flp->fn_fields = (struct fn_field *)
6782         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6783       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6784         fn_flp->fn_fields[k] = nfp->fnfield;
6785
6786       total_length += flp->length;
6787     }
6788
6789   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6790   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6791 }
6792
6793 /* Returns non-zero if NAME is the name of a vtable member in CU's
6794    language, zero otherwise.  */
6795 static int
6796 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6797 {
6798   static const char vptr[] = "_vptr";
6799   static const char vtable[] = "vtable";
6800
6801   /* Look for the C++ and Java forms of the vtable.  */
6802   if ((cu->language == language_java
6803        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6804        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6805        && is_cplus_marker (name[sizeof (vptr) - 1])))
6806     return 1;
6807
6808   return 0;
6809 }
6810
6811 /* GCC outputs unnamed structures that are really pointers to member
6812    functions, with the ABI-specified layout.  If TYPE describes
6813    such a structure, smash it into a member function type.
6814
6815    GCC shouldn't do this; it should just output pointer to member DIEs.
6816    This is GCC PR debug/28767.  */
6817
6818 static void
6819 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6820 {
6821   struct type *pfn_type, *domain_type, *new_type;
6822
6823   /* Check for a structure with no name and two children.  */
6824   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6825     return;
6826
6827   /* Check for __pfn and __delta members.  */
6828   if (TYPE_FIELD_NAME (type, 0) == NULL
6829       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6830       || TYPE_FIELD_NAME (type, 1) == NULL
6831       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6832     return;
6833
6834   /* Find the type of the method.  */
6835   pfn_type = TYPE_FIELD_TYPE (type, 0);
6836   if (pfn_type == NULL
6837       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6838       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6839     return;
6840
6841   /* Look for the "this" argument.  */
6842   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6843   if (TYPE_NFIELDS (pfn_type) == 0
6844       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6845       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6846     return;
6847
6848   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6849   new_type = alloc_type (objfile);
6850   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6851                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6852                         TYPE_VARARGS (pfn_type));
6853   smash_to_methodptr_type (type, new_type);
6854 }
6855
6856 /* Called when we find the DIE that starts a structure or union scope
6857    (definition) to create a type for the structure or union.  Fill in
6858    the type's name and general properties; the members will not be
6859    processed until process_structure_type.
6860
6861    NOTE: we need to call these functions regardless of whether or not the
6862    DIE has a DW_AT_name attribute, since it might be an anonymous
6863    structure or union.  This gets the type entered into our set of
6864    user defined types.
6865
6866    However, if the structure is incomplete (an opaque struct/union)
6867    then suppress creating a symbol table entry for it since gdb only
6868    wants to find the one with the complete definition.  Note that if
6869    it is complete, we just call new_symbol, which does it's own
6870    checking about whether the struct/union is anonymous or not (and
6871    suppresses creating a symbol table entry itself).  */
6872
6873 static struct type *
6874 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6875 {
6876   struct objfile *objfile = cu->objfile;
6877   struct type *type;
6878   struct attribute *attr;
6879   char *name;
6880
6881   /* If the definition of this type lives in .debug_types, read that type.
6882      Don't follow DW_AT_specification though, that will take us back up
6883      the chain and we want to go down.  */
6884   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6885   if (attr)
6886     {
6887       struct dwarf2_cu *type_cu = cu;
6888       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6889
6890       /* We could just recurse on read_structure_type, but we need to call
6891          get_die_type to ensure only one type for this DIE is created.
6892          This is important, for example, because for c++ classes we need
6893          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6894       type = read_type_die (type_die, type_cu);
6895
6896       /* TYPE_CU may not be the same as CU.
6897          Ensure TYPE is recorded in CU's type_hash table.  */
6898       return set_die_type (die, type, cu);
6899     }
6900
6901   type = alloc_type (objfile);
6902   INIT_CPLUS_SPECIFIC (type);
6903
6904   name = dwarf2_name (die, cu);
6905   if (name != NULL)
6906     {
6907       if (cu->language == language_cplus
6908           || cu->language == language_java)
6909         {
6910           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6911
6912           /* dwarf2_full_name might have already finished building the DIE's
6913              type.  If so, there is no need to continue.  */
6914           if (get_die_type (die, cu) != NULL)
6915             return get_die_type (die, cu);
6916
6917           TYPE_TAG_NAME (type) = full_name;
6918           if (die->tag == DW_TAG_structure_type
6919               || die->tag == DW_TAG_class_type)
6920             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6921         }
6922       else
6923         {
6924           /* The name is already allocated along with this objfile, so
6925              we don't need to duplicate it for the type.  */
6926           TYPE_TAG_NAME (type) = (char *) name;
6927           if (die->tag == DW_TAG_class_type)
6928             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6929         }
6930     }
6931
6932   if (die->tag == DW_TAG_structure_type)
6933     {
6934       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6935     }
6936   else if (die->tag == DW_TAG_union_type)
6937     {
6938       TYPE_CODE (type) = TYPE_CODE_UNION;
6939     }
6940   else
6941     {
6942       TYPE_CODE (type) = TYPE_CODE_CLASS;
6943     }
6944
6945   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6946     TYPE_DECLARED_CLASS (type) = 1;
6947
6948   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6949   if (attr)
6950     {
6951       TYPE_LENGTH (type) = DW_UNSND (attr);
6952     }
6953   else
6954     {
6955       TYPE_LENGTH (type) = 0;
6956     }
6957
6958   TYPE_STUB_SUPPORTED (type) = 1;
6959   if (die_is_declaration (die, cu))
6960     TYPE_STUB (type) = 1;
6961   else if (attr == NULL && die->child == NULL
6962            && producer_is_realview (cu->producer))
6963     /* RealView does not output the required DW_AT_declaration
6964        on incomplete types.  */
6965     TYPE_STUB (type) = 1;
6966
6967   /* We need to add the type field to the die immediately so we don't
6968      infinitely recurse when dealing with pointers to the structure
6969      type within the structure itself.  */
6970   set_die_type (die, type, cu);
6971
6972   /* set_die_type should be already done.  */
6973   set_descriptive_type (type, die, cu);
6974
6975   return type;
6976 }
6977
6978 /* Finish creating a structure or union type, including filling in
6979    its members and creating a symbol for it.  */
6980
6981 static void
6982 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6983 {
6984   struct objfile *objfile = cu->objfile;
6985   struct die_info *child_die = die->child;
6986   struct type *type;
6987
6988   type = get_die_type (die, cu);
6989   if (type == NULL)
6990     type = read_structure_type (die, cu);
6991
6992   if (die->child != NULL && ! die_is_declaration (die, cu))
6993     {
6994       struct field_info fi;
6995       struct die_info *child_die;
6996       VEC (symbolp) *template_args = NULL;
6997       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6998
6999       memset (&fi, 0, sizeof (struct field_info));
7000
7001       child_die = die->child;
7002
7003       while (child_die && child_die->tag)
7004         {
7005           if (child_die->tag == DW_TAG_member
7006               || child_die->tag == DW_TAG_variable)
7007             {
7008               /* NOTE: carlton/2002-11-05: A C++ static data member
7009                  should be a DW_TAG_member that is a declaration, but
7010                  all versions of G++ as of this writing (so through at
7011                  least 3.2.1) incorrectly generate DW_TAG_variable
7012                  tags for them instead.  */
7013               dwarf2_add_field (&fi, child_die, cu);
7014             }
7015           else if (child_die->tag == DW_TAG_subprogram)
7016             {
7017               /* C++ member function.  */
7018               dwarf2_add_member_fn (&fi, child_die, type, cu);
7019             }
7020           else if (child_die->tag == DW_TAG_inheritance)
7021             {
7022               /* C++ base class field.  */
7023               dwarf2_add_field (&fi, child_die, cu);
7024             }
7025           else if (child_die->tag == DW_TAG_typedef)
7026             dwarf2_add_typedef (&fi, child_die, cu);
7027           else if (child_die->tag == DW_TAG_template_type_param
7028                    || child_die->tag == DW_TAG_template_value_param)
7029             {
7030               struct symbol *arg = new_symbol (child_die, NULL, cu);
7031
7032               if (arg != NULL)
7033                 VEC_safe_push (symbolp, template_args, arg);
7034             }
7035
7036           child_die = sibling_die (child_die);
7037         }
7038
7039       /* Attach template arguments to type.  */
7040       if (! VEC_empty (symbolp, template_args))
7041         {
7042           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7043           TYPE_N_TEMPLATE_ARGUMENTS (type)
7044             = VEC_length (symbolp, template_args);
7045           TYPE_TEMPLATE_ARGUMENTS (type)
7046             = obstack_alloc (&objfile->objfile_obstack,
7047                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7048                               * sizeof (struct symbol *)));
7049           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7050                   VEC_address (symbolp, template_args),
7051                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7052                    * sizeof (struct symbol *)));
7053           VEC_free (symbolp, template_args);
7054         }
7055
7056       /* Attach fields and member functions to the type.  */
7057       if (fi.nfields)
7058         dwarf2_attach_fields_to_type (&fi, type, cu);
7059       if (fi.nfnfields)
7060         {
7061           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7062
7063           /* Get the type which refers to the base class (possibly this
7064              class itself) which contains the vtable pointer for the current
7065              class from the DW_AT_containing_type attribute.  This use of
7066              DW_AT_containing_type is a GNU extension.  */
7067
7068           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7069             {
7070               struct type *t = die_containing_type (die, cu);
7071
7072               TYPE_VPTR_BASETYPE (type) = t;
7073               if (type == t)
7074                 {
7075                   int i;
7076
7077                   /* Our own class provides vtbl ptr.  */
7078                   for (i = TYPE_NFIELDS (t) - 1;
7079                        i >= TYPE_N_BASECLASSES (t);
7080                        --i)
7081                     {
7082                       char *fieldname = TYPE_FIELD_NAME (t, i);
7083
7084                       if (is_vtable_name (fieldname, cu))
7085                         {
7086                           TYPE_VPTR_FIELDNO (type) = i;
7087                           break;
7088                         }
7089                     }
7090
7091                   /* Complain if virtual function table field not found.  */
7092                   if (i < TYPE_N_BASECLASSES (t))
7093                     complaint (&symfile_complaints,
7094                                _("virtual function table pointer "
7095                                  "not found when defining class '%s'"),
7096                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7097                                "");
7098                 }
7099               else
7100                 {
7101                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7102                 }
7103             }
7104           else if (cu->producer
7105                    && strncmp (cu->producer,
7106                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7107             {
7108               /* The IBM XLC compiler does not provide direct indication
7109                  of the containing type, but the vtable pointer is
7110                  always named __vfp.  */
7111
7112               int i;
7113
7114               for (i = TYPE_NFIELDS (type) - 1;
7115                    i >= TYPE_N_BASECLASSES (type);
7116                    --i)
7117                 {
7118                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7119                     {
7120                       TYPE_VPTR_FIELDNO (type) = i;
7121                       TYPE_VPTR_BASETYPE (type) = type;
7122                       break;
7123                     }
7124                 }
7125             }
7126         }
7127
7128       /* Copy fi.typedef_field_list linked list elements content into the
7129          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7130       if (fi.typedef_field_list)
7131         {
7132           int i = fi.typedef_field_list_count;
7133
7134           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7135           TYPE_TYPEDEF_FIELD_ARRAY (type)
7136             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7137           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7138
7139           /* Reverse the list order to keep the debug info elements order.  */
7140           while (--i >= 0)
7141             {
7142               struct typedef_field *dest, *src;
7143
7144               dest = &TYPE_TYPEDEF_FIELD (type, i);
7145               src = &fi.typedef_field_list->field;
7146               fi.typedef_field_list = fi.typedef_field_list->next;
7147               *dest = *src;
7148             }
7149         }
7150
7151       do_cleanups (back_to);
7152     }
7153
7154   quirk_gcc_member_function_pointer (type, cu->objfile);
7155
7156   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7157      snapshots) has been known to create a die giving a declaration
7158      for a class that has, as a child, a die giving a definition for a
7159      nested class.  So we have to process our children even if the
7160      current die is a declaration.  Normally, of course, a declaration
7161      won't have any children at all.  */
7162
7163   while (child_die != NULL && child_die->tag)
7164     {
7165       if (child_die->tag == DW_TAG_member
7166           || child_die->tag == DW_TAG_variable
7167           || child_die->tag == DW_TAG_inheritance
7168           || child_die->tag == DW_TAG_template_value_param
7169           || child_die->tag == DW_TAG_template_type_param)
7170         {
7171           /* Do nothing.  */
7172         }
7173       else
7174         process_die (child_die, cu);
7175
7176       child_die = sibling_die (child_die);
7177     }
7178
7179   /* Do not consider external references.  According to the DWARF standard,
7180      these DIEs are identified by the fact that they have no byte_size
7181      attribute, and a declaration attribute.  */
7182   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7183       || !die_is_declaration (die, cu))
7184     new_symbol (die, type, cu);
7185 }
7186
7187 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7188    complete the type's fields yet, or create any symbols.  */
7189
7190 static struct type *
7191 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7192 {
7193   struct objfile *objfile = cu->objfile;
7194   struct type *type;
7195   struct attribute *attr;
7196   const char *name;
7197
7198   /* If the definition of this type lives in .debug_types, read that type.
7199      Don't follow DW_AT_specification though, that will take us back up
7200      the chain and we want to go down.  */
7201   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7202   if (attr)
7203     {
7204       struct dwarf2_cu *type_cu = cu;
7205       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7206
7207       type = read_type_die (type_die, type_cu);
7208
7209       /* TYPE_CU may not be the same as CU.
7210          Ensure TYPE is recorded in CU's type_hash table.  */
7211       return set_die_type (die, type, cu);
7212     }
7213
7214   type = alloc_type (objfile);
7215
7216   TYPE_CODE (type) = TYPE_CODE_ENUM;
7217   name = dwarf2_full_name (NULL, die, cu);
7218   if (name != NULL)
7219     TYPE_TAG_NAME (type) = (char *) name;
7220
7221   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7222   if (attr)
7223     {
7224       TYPE_LENGTH (type) = DW_UNSND (attr);
7225     }
7226   else
7227     {
7228       TYPE_LENGTH (type) = 0;
7229     }
7230
7231   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7232      declared as private in the package spec, and then defined only
7233      inside the package body.  Such types are known as Taft Amendment
7234      Types.  When another package uses such a type, an incomplete DIE
7235      may be generated by the compiler.  */
7236   if (die_is_declaration (die, cu))
7237     TYPE_STUB (type) = 1;
7238
7239   return set_die_type (die, type, cu);
7240 }
7241
7242 /* Given a pointer to a die which begins an enumeration, process all
7243    the dies that define the members of the enumeration, and create the
7244    symbol for the enumeration type.
7245
7246    NOTE: We reverse the order of the element list.  */
7247
7248 static void
7249 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7250 {
7251   struct type *this_type;
7252
7253   this_type = get_die_type (die, cu);
7254   if (this_type == NULL)
7255     this_type = read_enumeration_type (die, cu);
7256
7257   if (die->child != NULL)
7258     {
7259       struct die_info *child_die;
7260       struct symbol *sym;
7261       struct field *fields = NULL;
7262       int num_fields = 0;
7263       int unsigned_enum = 1;
7264       char *name;
7265
7266       child_die = die->child;
7267       while (child_die && child_die->tag)
7268         {
7269           if (child_die->tag != DW_TAG_enumerator)
7270             {
7271               process_die (child_die, cu);
7272             }
7273           else
7274             {
7275               name = dwarf2_name (child_die, cu);
7276               if (name)
7277                 {
7278                   sym = new_symbol (child_die, this_type, cu);
7279                   if (SYMBOL_VALUE (sym) < 0)
7280                     unsigned_enum = 0;
7281
7282                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7283                     {
7284                       fields = (struct field *)
7285                         xrealloc (fields,
7286                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7287                                   * sizeof (struct field));
7288                     }
7289
7290                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7291                   FIELD_TYPE (fields[num_fields]) = NULL;
7292                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7293                   FIELD_BITSIZE (fields[num_fields]) = 0;
7294
7295                   num_fields++;
7296                 }
7297             }
7298
7299           child_die = sibling_die (child_die);
7300         }
7301
7302       if (num_fields)
7303         {
7304           TYPE_NFIELDS (this_type) = num_fields;
7305           TYPE_FIELDS (this_type) = (struct field *)
7306             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7307           memcpy (TYPE_FIELDS (this_type), fields,
7308                   sizeof (struct field) * num_fields);
7309           xfree (fields);
7310         }
7311       if (unsigned_enum)
7312         TYPE_UNSIGNED (this_type) = 1;
7313     }
7314
7315   new_symbol (die, this_type, cu);
7316 }
7317
7318 /* Extract all information from a DW_TAG_array_type DIE and put it in
7319    the DIE's type field.  For now, this only handles one dimensional
7320    arrays.  */
7321
7322 static struct type *
7323 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7324 {
7325   struct objfile *objfile = cu->objfile;
7326   struct die_info *child_die;
7327   struct type *type;
7328   struct type *element_type, *range_type, *index_type;
7329   struct type **range_types = NULL;
7330   struct attribute *attr;
7331   int ndim = 0;
7332   struct cleanup *back_to;
7333   char *name;
7334
7335   element_type = die_type (die, cu);
7336
7337   /* The die_type call above may have already set the type for this DIE.  */
7338   type = get_die_type (die, cu);
7339   if (type)
7340     return type;
7341
7342   /* Irix 6.2 native cc creates array types without children for
7343      arrays with unspecified length.  */
7344   if (die->child == NULL)
7345     {
7346       index_type = objfile_type (objfile)->builtin_int;
7347       range_type = create_range_type (NULL, index_type, 0, -1);
7348       type = create_array_type (NULL, element_type, range_type);
7349       return set_die_type (die, type, cu);
7350     }
7351
7352   back_to = make_cleanup (null_cleanup, NULL);
7353   child_die = die->child;
7354   while (child_die && child_die->tag)
7355     {
7356       if (child_die->tag == DW_TAG_subrange_type)
7357         {
7358           struct type *child_type = read_type_die (child_die, cu);
7359
7360           if (child_type != NULL)
7361             {
7362               /* The range type was succesfully read.  Save it for the
7363                  array type creation.  */
7364               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7365                 {
7366                   range_types = (struct type **)
7367                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7368                               * sizeof (struct type *));
7369                   if (ndim == 0)
7370                     make_cleanup (free_current_contents, &range_types);
7371                 }
7372               range_types[ndim++] = child_type;
7373             }
7374         }
7375       child_die = sibling_die (child_die);
7376     }
7377
7378   /* Dwarf2 dimensions are output from left to right, create the
7379      necessary array types in backwards order.  */
7380
7381   type = element_type;
7382
7383   if (read_array_order (die, cu) == DW_ORD_col_major)
7384     {
7385       int i = 0;
7386
7387       while (i < ndim)
7388         type = create_array_type (NULL, type, range_types[i++]);
7389     }
7390   else
7391     {
7392       while (ndim-- > 0)
7393         type = create_array_type (NULL, type, range_types[ndim]);
7394     }
7395
7396   /* Understand Dwarf2 support for vector types (like they occur on
7397      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7398      array type.  This is not part of the Dwarf2/3 standard yet, but a
7399      custom vendor extension.  The main difference between a regular
7400      array and the vector variant is that vectors are passed by value
7401      to functions.  */
7402   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7403   if (attr)
7404     make_vector_type (type);
7405
7406   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7407      implementation may choose to implement triple vectors using this
7408      attribute.  */
7409   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7410   if (attr)
7411     {
7412       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7413         TYPE_LENGTH (type) = DW_UNSND (attr);
7414       else
7415         complaint (&symfile_complaints,
7416                    _("DW_AT_byte_size for array type smaller "
7417                      "than the total size of elements"));
7418     }
7419
7420   name = dwarf2_name (die, cu);
7421   if (name)
7422     TYPE_NAME (type) = name;
7423
7424   /* Install the type in the die.  */
7425   set_die_type (die, type, cu);
7426
7427   /* set_die_type should be already done.  */
7428   set_descriptive_type (type, die, cu);
7429
7430   do_cleanups (back_to);
7431
7432   return type;
7433 }
7434
7435 static enum dwarf_array_dim_ordering
7436 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7437 {
7438   struct attribute *attr;
7439
7440   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7441
7442   if (attr) return DW_SND (attr);
7443
7444   /* GNU F77 is a special case, as at 08/2004 array type info is the
7445      opposite order to the dwarf2 specification, but data is still
7446      laid out as per normal fortran.
7447
7448      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7449      version checking.  */
7450
7451   if (cu->language == language_fortran
7452       && cu->producer && strstr (cu->producer, "GNU F77"))
7453     {
7454       return DW_ORD_row_major;
7455     }
7456
7457   switch (cu->language_defn->la_array_ordering)
7458     {
7459     case array_column_major:
7460       return DW_ORD_col_major;
7461     case array_row_major:
7462     default:
7463       return DW_ORD_row_major;
7464     };
7465 }
7466
7467 /* Extract all information from a DW_TAG_set_type DIE and put it in
7468    the DIE's type field.  */
7469
7470 static struct type *
7471 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7472 {
7473   struct type *domain_type, *set_type;
7474   struct attribute *attr;
7475
7476   domain_type = die_type (die, cu);
7477
7478   /* The die_type call above may have already set the type for this DIE.  */
7479   set_type = get_die_type (die, cu);
7480   if (set_type)
7481     return set_type;
7482
7483   set_type = create_set_type (NULL, domain_type);
7484
7485   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7486   if (attr)
7487     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7488
7489   return set_die_type (die, set_type, cu);
7490 }
7491
7492 /* First cut: install each common block member as a global variable.  */
7493
7494 static void
7495 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7496 {
7497   struct die_info *child_die;
7498   struct attribute *attr;
7499   struct symbol *sym;
7500   CORE_ADDR base = (CORE_ADDR) 0;
7501
7502   attr = dwarf2_attr (die, DW_AT_location, cu);
7503   if (attr)
7504     {
7505       /* Support the .debug_loc offsets.  */
7506       if (attr_form_is_block (attr))
7507         {
7508           base = decode_locdesc (DW_BLOCK (attr), cu);
7509         }
7510       else if (attr_form_is_section_offset (attr))
7511         {
7512           dwarf2_complex_location_expr_complaint ();
7513         }
7514       else
7515         {
7516           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7517                                                  "common block member");
7518         }
7519     }
7520   if (die->child != NULL)
7521     {
7522       child_die = die->child;
7523       while (child_die && child_die->tag)
7524         {
7525           sym = new_symbol (child_die, NULL, cu);
7526           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7527           if (sym != NULL && attr != NULL)
7528             {
7529               CORE_ADDR byte_offset = 0;
7530
7531               if (attr_form_is_section_offset (attr))
7532                 dwarf2_complex_location_expr_complaint ();
7533               else if (attr_form_is_constant (attr))
7534                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7535               else if (attr_form_is_block (attr))
7536                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7537               else
7538                 dwarf2_complex_location_expr_complaint ();
7539
7540               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7541               add_symbol_to_list (sym, &global_symbols);
7542             }
7543           child_die = sibling_die (child_die);
7544         }
7545     }
7546 }
7547
7548 /* Create a type for a C++ namespace.  */
7549
7550 static struct type *
7551 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7552 {
7553   struct objfile *objfile = cu->objfile;
7554   const char *previous_prefix, *name;
7555   int is_anonymous;
7556   struct type *type;
7557
7558   /* For extensions, reuse the type of the original namespace.  */
7559   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7560     {
7561       struct die_info *ext_die;
7562       struct dwarf2_cu *ext_cu = cu;
7563
7564       ext_die = dwarf2_extension (die, &ext_cu);
7565       type = read_type_die (ext_die, ext_cu);
7566
7567       /* EXT_CU may not be the same as CU.
7568          Ensure TYPE is recorded in CU's type_hash table.  */
7569       return set_die_type (die, type, cu);
7570     }
7571
7572   name = namespace_name (die, &is_anonymous, cu);
7573
7574   /* Now build the name of the current namespace.  */
7575
7576   previous_prefix = determine_prefix (die, cu);
7577   if (previous_prefix[0] != '\0')
7578     name = typename_concat (&objfile->objfile_obstack,
7579                             previous_prefix, name, 0, cu);
7580
7581   /* Create the type.  */
7582   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7583                     objfile);
7584   TYPE_NAME (type) = (char *) name;
7585   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7586
7587   return set_die_type (die, type, cu);
7588 }
7589
7590 /* Read a C++ namespace.  */
7591
7592 static void
7593 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7594 {
7595   struct objfile *objfile = cu->objfile;
7596   int is_anonymous;
7597
7598   /* Add a symbol associated to this if we haven't seen the namespace
7599      before.  Also, add a using directive if it's an anonymous
7600      namespace.  */
7601
7602   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7603     {
7604       struct type *type;
7605
7606       type = read_type_die (die, cu);
7607       new_symbol (die, type, cu);
7608
7609       namespace_name (die, &is_anonymous, cu);
7610       if (is_anonymous)
7611         {
7612           const char *previous_prefix = determine_prefix (die, cu);
7613
7614           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7615                                   NULL, &objfile->objfile_obstack);
7616         }
7617     }
7618
7619   if (die->child != NULL)
7620     {
7621       struct die_info *child_die = die->child;
7622
7623       while (child_die && child_die->tag)
7624         {
7625           process_die (child_die, cu);
7626           child_die = sibling_die (child_die);
7627         }
7628     }
7629 }
7630
7631 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7632    imported module.  Still we need that type as local Fortran "use ... only"
7633    declaration imports depend on the created type in determine_prefix.  */
7634
7635 static struct type *
7636 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7637 {
7638   struct objfile *objfile = cu->objfile;
7639   char *module_name;
7640   struct type *type;
7641
7642   module_name = dwarf2_name (die, cu);
7643   if (!module_name)
7644     complaint (&symfile_complaints,
7645                _("DW_TAG_module has no name, offset 0x%x"),
7646                die->offset);
7647   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7648
7649   /* determine_prefix uses TYPE_TAG_NAME.  */
7650   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7651
7652   return set_die_type (die, type, cu);
7653 }
7654
7655 /* Read a Fortran module.  */
7656
7657 static void
7658 read_module (struct die_info *die, struct dwarf2_cu *cu)
7659 {
7660   struct die_info *child_die = die->child;
7661
7662   while (child_die && child_die->tag)
7663     {
7664       process_die (child_die, cu);
7665       child_die = sibling_die (child_die);
7666     }
7667 }
7668
7669 /* Return the name of the namespace represented by DIE.  Set
7670    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7671    namespace.  */
7672
7673 static const char *
7674 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7675 {
7676   struct die_info *current_die;
7677   const char *name = NULL;
7678
7679   /* Loop through the extensions until we find a name.  */
7680
7681   for (current_die = die;
7682        current_die != NULL;
7683        current_die = dwarf2_extension (die, &cu))
7684     {
7685       name = dwarf2_name (current_die, cu);
7686       if (name != NULL)
7687         break;
7688     }
7689
7690   /* Is it an anonymous namespace?  */
7691
7692   *is_anonymous = (name == NULL);
7693   if (*is_anonymous)
7694     name = "(anonymous namespace)";
7695
7696   return name;
7697 }
7698
7699 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7700    the user defined type vector.  */
7701
7702 static struct type *
7703 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7704 {
7705   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7706   struct comp_unit_head *cu_header = &cu->header;
7707   struct type *type;
7708   struct attribute *attr_byte_size;
7709   struct attribute *attr_address_class;
7710   int byte_size, addr_class;
7711   struct type *target_type;
7712
7713   target_type = die_type (die, cu);
7714
7715   /* The die_type call above may have already set the type for this DIE.  */
7716   type = get_die_type (die, cu);
7717   if (type)
7718     return type;
7719
7720   type = lookup_pointer_type (target_type);
7721
7722   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7723   if (attr_byte_size)
7724     byte_size = DW_UNSND (attr_byte_size);
7725   else
7726     byte_size = cu_header->addr_size;
7727
7728   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7729   if (attr_address_class)
7730     addr_class = DW_UNSND (attr_address_class);
7731   else
7732     addr_class = DW_ADDR_none;
7733
7734   /* If the pointer size or address class is different than the
7735      default, create a type variant marked as such and set the
7736      length accordingly.  */
7737   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7738     {
7739       if (gdbarch_address_class_type_flags_p (gdbarch))
7740         {
7741           int type_flags;
7742
7743           type_flags = gdbarch_address_class_type_flags
7744                          (gdbarch, byte_size, addr_class);
7745           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7746                       == 0);
7747           type = make_type_with_address_space (type, type_flags);
7748         }
7749       else if (TYPE_LENGTH (type) != byte_size)
7750         {
7751           complaint (&symfile_complaints,
7752                      _("invalid pointer size %d"), byte_size);
7753         }
7754       else
7755         {
7756           /* Should we also complain about unhandled address classes?  */
7757         }
7758     }
7759
7760   TYPE_LENGTH (type) = byte_size;
7761   return set_die_type (die, type, cu);
7762 }
7763
7764 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7765    the user defined type vector.  */
7766
7767 static struct type *
7768 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7769 {
7770   struct type *type;
7771   struct type *to_type;
7772   struct type *domain;
7773
7774   to_type = die_type (die, cu);
7775   domain = die_containing_type (die, cu);
7776
7777   /* The calls above may have already set the type for this DIE.  */
7778   type = get_die_type (die, cu);
7779   if (type)
7780     return type;
7781
7782   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7783     type = lookup_methodptr_type (to_type);
7784   else
7785     type = lookup_memberptr_type (to_type, domain);
7786
7787   return set_die_type (die, type, cu);
7788 }
7789
7790 /* Extract all information from a DW_TAG_reference_type DIE and add to
7791    the user defined type vector.  */
7792
7793 static struct type *
7794 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7795 {
7796   struct comp_unit_head *cu_header = &cu->header;
7797   struct type *type, *target_type;
7798   struct attribute *attr;
7799
7800   target_type = die_type (die, cu);
7801
7802   /* The die_type call above may have already set the type for this DIE.  */
7803   type = get_die_type (die, cu);
7804   if (type)
7805     return type;
7806
7807   type = lookup_reference_type (target_type);
7808   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7809   if (attr)
7810     {
7811       TYPE_LENGTH (type) = DW_UNSND (attr);
7812     }
7813   else
7814     {
7815       TYPE_LENGTH (type) = cu_header->addr_size;
7816     }
7817   return set_die_type (die, type, cu);
7818 }
7819
7820 static struct type *
7821 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7822 {
7823   struct type *base_type, *cv_type;
7824
7825   base_type = die_type (die, cu);
7826
7827   /* The die_type call above may have already set the type for this DIE.  */
7828   cv_type = get_die_type (die, cu);
7829   if (cv_type)
7830     return cv_type;
7831
7832   /* In case the const qualifier is applied to an array type, the element type
7833      is so qualified, not the array type (section 6.7.3 of C99).  */
7834   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7835     {
7836       struct type *el_type, *inner_array;
7837
7838       base_type = copy_type (base_type);
7839       inner_array = base_type;
7840
7841       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7842         {
7843           TYPE_TARGET_TYPE (inner_array) =
7844             copy_type (TYPE_TARGET_TYPE (inner_array));
7845           inner_array = TYPE_TARGET_TYPE (inner_array);
7846         }
7847
7848       el_type = TYPE_TARGET_TYPE (inner_array);
7849       TYPE_TARGET_TYPE (inner_array) =
7850         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7851
7852       return set_die_type (die, base_type, cu);
7853     }
7854
7855   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7856   return set_die_type (die, cv_type, cu);
7857 }
7858
7859 static struct type *
7860 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7861 {
7862   struct type *base_type, *cv_type;
7863
7864   base_type = die_type (die, cu);
7865
7866   /* The die_type call above may have already set the type for this DIE.  */
7867   cv_type = get_die_type (die, cu);
7868   if (cv_type)
7869     return cv_type;
7870
7871   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7872   return set_die_type (die, cv_type, cu);
7873 }
7874
7875 /* Extract all information from a DW_TAG_string_type DIE and add to
7876    the user defined type vector.  It isn't really a user defined type,
7877    but it behaves like one, with other DIE's using an AT_user_def_type
7878    attribute to reference it.  */
7879
7880 static struct type *
7881 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7882 {
7883   struct objfile *objfile = cu->objfile;
7884   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7885   struct type *type, *range_type, *index_type, *char_type;
7886   struct attribute *attr;
7887   unsigned int length;
7888
7889   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7890   if (attr)
7891     {
7892       length = DW_UNSND (attr);
7893     }
7894   else
7895     {
7896       /* Check for the DW_AT_byte_size attribute.  */
7897       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7898       if (attr)
7899         {
7900           length = DW_UNSND (attr);
7901         }
7902       else
7903         {
7904           length = 1;
7905         }
7906     }
7907
7908   index_type = objfile_type (objfile)->builtin_int;
7909   range_type = create_range_type (NULL, index_type, 1, length);
7910   char_type = language_string_char_type (cu->language_defn, gdbarch);
7911   type = create_string_type (NULL, char_type, range_type);
7912
7913   return set_die_type (die, type, cu);
7914 }
7915
7916 /* Handle DIES due to C code like:
7917
7918    struct foo
7919    {
7920    int (*funcp)(int a, long l);
7921    int b;
7922    };
7923
7924    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
7925
7926 static struct type *
7927 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7928 {
7929   struct type *type;            /* Type that this function returns.  */
7930   struct type *ftype;           /* Function that returns above type.  */
7931   struct attribute *attr;
7932
7933   type = die_type (die, cu);
7934
7935   /* The die_type call above may have already set the type for this DIE.  */
7936   ftype = get_die_type (die, cu);
7937   if (ftype)
7938     return ftype;
7939
7940   ftype = lookup_function_type (type);
7941
7942   /* All functions in C++, Pascal and Java have prototypes.  */
7943   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7944   if ((attr && (DW_UNSND (attr) != 0))
7945       || cu->language == language_cplus
7946       || cu->language == language_java
7947       || cu->language == language_pascal)
7948     TYPE_PROTOTYPED (ftype) = 1;
7949   else if (producer_is_realview (cu->producer))
7950     /* RealView does not emit DW_AT_prototyped.  We can not
7951        distinguish prototyped and unprototyped functions; default to
7952        prototyped, since that is more common in modern code (and
7953        RealView warns about unprototyped functions).  */
7954     TYPE_PROTOTYPED (ftype) = 1;
7955
7956   /* Store the calling convention in the type if it's available in
7957      the subroutine die.  Otherwise set the calling convention to
7958      the default value DW_CC_normal.  */
7959   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7960   if (attr)
7961     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
7962   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
7963     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
7964   else
7965     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
7966
7967   /* We need to add the subroutine type to the die immediately so
7968      we don't infinitely recurse when dealing with parameters
7969      declared as the same subroutine type.  */
7970   set_die_type (die, ftype, cu);
7971
7972   if (die->child != NULL)
7973     {
7974       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7975       struct die_info *child_die;
7976       int nparams, iparams;
7977
7978       /* Count the number of parameters.
7979          FIXME: GDB currently ignores vararg functions, but knows about
7980          vararg member functions.  */
7981       nparams = 0;
7982       child_die = die->child;
7983       while (child_die && child_die->tag)
7984         {
7985           if (child_die->tag == DW_TAG_formal_parameter)
7986             nparams++;
7987           else if (child_die->tag == DW_TAG_unspecified_parameters)
7988             TYPE_VARARGS (ftype) = 1;
7989           child_die = sibling_die (child_die);
7990         }
7991
7992       /* Allocate storage for parameters and fill them in.  */
7993       TYPE_NFIELDS (ftype) = nparams;
7994       TYPE_FIELDS (ftype) = (struct field *)
7995         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7996
7997       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7998          even if we error out during the parameters reading below.  */
7999       for (iparams = 0; iparams < nparams; iparams++)
8000         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8001
8002       iparams = 0;
8003       child_die = die->child;
8004       while (child_die && child_die->tag)
8005         {
8006           if (child_die->tag == DW_TAG_formal_parameter)
8007             {
8008               struct type *arg_type;
8009
8010               /* DWARF version 2 has no clean way to discern C++
8011                  static and non-static member functions.  G++ helps
8012                  GDB by marking the first parameter for non-static
8013                  member functions (which is the this pointer) as
8014                  artificial.  We pass this information to
8015                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8016
8017                  DWARF version 3 added DW_AT_object_pointer, which GCC
8018                  4.5 does not yet generate.  */
8019               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8020               if (attr)
8021                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8022               else
8023                 {
8024                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8025
8026                   /* GCC/43521: In java, the formal parameter
8027                      "this" is sometimes not marked with DW_AT_artificial.  */
8028                   if (cu->language == language_java)
8029                     {
8030                       const char *name = dwarf2_name (child_die, cu);
8031
8032                       if (name && !strcmp (name, "this"))
8033                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8034                     }
8035                 }
8036               arg_type = die_type (child_die, cu);
8037
8038               /* RealView does not mark THIS as const, which the testsuite
8039                  expects.  GCC marks THIS as const in method definitions,
8040                  but not in the class specifications (GCC PR 43053).  */
8041               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8042                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8043                 {
8044                   int is_this = 0;
8045                   struct dwarf2_cu *arg_cu = cu;
8046                   const char *name = dwarf2_name (child_die, cu);
8047
8048                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8049                   if (attr)
8050                     {
8051                       /* If the compiler emits this, use it.  */
8052                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8053                         is_this = 1;
8054                     }
8055                   else if (name && strcmp (name, "this") == 0)
8056                     /* Function definitions will have the argument names.  */
8057                     is_this = 1;
8058                   else if (name == NULL && iparams == 0)
8059                     /* Declarations may not have the names, so like
8060                        elsewhere in GDB, assume an artificial first
8061                        argument is "this".  */
8062                     is_this = 1;
8063
8064                   if (is_this)
8065                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8066                                              arg_type, 0);
8067                 }
8068
8069               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8070               iparams++;
8071             }
8072           child_die = sibling_die (child_die);
8073         }
8074     }
8075
8076   return ftype;
8077 }
8078
8079 static struct type *
8080 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8081 {
8082   struct objfile *objfile = cu->objfile;
8083   const char *name = NULL;
8084   struct type *this_type;
8085
8086   name = dwarf2_full_name (NULL, die, cu);
8087   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8088                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8089   TYPE_NAME (this_type) = (char *) name;
8090   set_die_type (die, this_type, cu);
8091   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8092   return this_type;
8093 }
8094
8095 /* Find a representation of a given base type and install
8096    it in the TYPE field of the die.  */
8097
8098 static struct type *
8099 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8100 {
8101   struct objfile *objfile = cu->objfile;
8102   struct type *type;
8103   struct attribute *attr;
8104   int encoding = 0, size = 0;
8105   char *name;
8106   enum type_code code = TYPE_CODE_INT;
8107   int type_flags = 0;
8108   struct type *target_type = NULL;
8109
8110   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8111   if (attr)
8112     {
8113       encoding = DW_UNSND (attr);
8114     }
8115   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8116   if (attr)
8117     {
8118       size = DW_UNSND (attr);
8119     }
8120   name = dwarf2_name (die, cu);
8121   if (!name)
8122     {
8123       complaint (&symfile_complaints,
8124                  _("DW_AT_name missing from DW_TAG_base_type"));
8125     }
8126
8127   switch (encoding)
8128     {
8129       case DW_ATE_address:
8130         /* Turn DW_ATE_address into a void * pointer.  */
8131         code = TYPE_CODE_PTR;
8132         type_flags |= TYPE_FLAG_UNSIGNED;
8133         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8134         break;
8135       case DW_ATE_boolean:
8136         code = TYPE_CODE_BOOL;
8137         type_flags |= TYPE_FLAG_UNSIGNED;
8138         break;
8139       case DW_ATE_complex_float:
8140         code = TYPE_CODE_COMPLEX;
8141         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8142         break;
8143       case DW_ATE_decimal_float:
8144         code = TYPE_CODE_DECFLOAT;
8145         break;
8146       case DW_ATE_float:
8147         code = TYPE_CODE_FLT;
8148         break;
8149       case DW_ATE_signed:
8150         break;
8151       case DW_ATE_unsigned:
8152         type_flags |= TYPE_FLAG_UNSIGNED;
8153         break;
8154       case DW_ATE_signed_char:
8155         if (cu->language == language_ada || cu->language == language_m2
8156             || cu->language == language_pascal)
8157           code = TYPE_CODE_CHAR;
8158         break;
8159       case DW_ATE_unsigned_char:
8160         if (cu->language == language_ada || cu->language == language_m2
8161             || cu->language == language_pascal)
8162           code = TYPE_CODE_CHAR;
8163         type_flags |= TYPE_FLAG_UNSIGNED;
8164         break;
8165       case DW_ATE_UTF:
8166         /* We just treat this as an integer and then recognize the
8167            type by name elsewhere.  */
8168         break;
8169
8170       default:
8171         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8172                    dwarf_type_encoding_name (encoding));
8173         break;
8174     }
8175
8176   type = init_type (code, size, type_flags, NULL, objfile);
8177   TYPE_NAME (type) = name;
8178   TYPE_TARGET_TYPE (type) = target_type;
8179
8180   if (name && strcmp (name, "char") == 0)
8181     TYPE_NOSIGN (type) = 1;
8182
8183   return set_die_type (die, type, cu);
8184 }
8185
8186 /* Read the given DW_AT_subrange DIE.  */
8187
8188 static struct type *
8189 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8190 {
8191   struct type *base_type;
8192   struct type *range_type;
8193   struct attribute *attr;
8194   LONGEST low = 0;
8195   LONGEST high = -1;
8196   char *name;
8197   LONGEST negative_mask;
8198
8199   base_type = die_type (die, cu);
8200   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8201   check_typedef (base_type);
8202
8203   /* The die_type call above may have already set the type for this DIE.  */
8204   range_type = get_die_type (die, cu);
8205   if (range_type)
8206     return range_type;
8207
8208   if (cu->language == language_fortran)
8209     {
8210       /* FORTRAN implies a lower bound of 1, if not given.  */
8211       low = 1;
8212     }
8213
8214   /* FIXME: For variable sized arrays either of these could be
8215      a variable rather than a constant value.  We'll allow it,
8216      but we don't know how to handle it.  */
8217   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8218   if (attr)
8219     low = dwarf2_get_attr_constant_value (attr, 0);
8220
8221   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8222   if (attr)
8223     {
8224       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8225         {
8226           /* GCC encodes arrays with unspecified or dynamic length
8227              with a DW_FORM_block1 attribute or a reference attribute.
8228              FIXME: GDB does not yet know how to handle dynamic
8229              arrays properly, treat them as arrays with unspecified
8230              length for now.
8231
8232              FIXME: jimb/2003-09-22: GDB does not really know
8233              how to handle arrays of unspecified length
8234              either; we just represent them as zero-length
8235              arrays.  Choose an appropriate upper bound given
8236              the lower bound we've computed above.  */
8237           high = low - 1;
8238         }
8239       else
8240         high = dwarf2_get_attr_constant_value (attr, 1);
8241     }
8242   else
8243     {
8244       attr = dwarf2_attr (die, DW_AT_count, cu);
8245       if (attr)
8246         {
8247           int count = dwarf2_get_attr_constant_value (attr, 1);
8248           high = low + count - 1;
8249         }
8250       else
8251         {
8252           /* Unspecified array length.  */
8253           high = low - 1;
8254         }
8255     }
8256
8257   /* Dwarf-2 specifications explicitly allows to create subrange types
8258      without specifying a base type.
8259      In that case, the base type must be set to the type of
8260      the lower bound, upper bound or count, in that order, if any of these
8261      three attributes references an object that has a type.
8262      If no base type is found, the Dwarf-2 specifications say that
8263      a signed integer type of size equal to the size of an address should
8264      be used.
8265      For the following C code: `extern char gdb_int [];'
8266      GCC produces an empty range DIE.
8267      FIXME: muller/2010-05-28: Possible references to object for low bound,
8268      high bound or count are not yet handled by this code.  */
8269   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8270     {
8271       struct objfile *objfile = cu->objfile;
8272       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8273       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8274       struct type *int_type = objfile_type (objfile)->builtin_int;
8275
8276       /* Test "int", "long int", and "long long int" objfile types,
8277          and select the first one having a size above or equal to the
8278          architecture address size.  */
8279       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8280         base_type = int_type;
8281       else
8282         {
8283           int_type = objfile_type (objfile)->builtin_long;
8284           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8285             base_type = int_type;
8286           else
8287             {
8288               int_type = objfile_type (objfile)->builtin_long_long;
8289               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8290                 base_type = int_type;
8291             }
8292         }
8293     }
8294
8295   negative_mask =
8296     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8297   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8298     low |= negative_mask;
8299   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8300     high |= negative_mask;
8301
8302   range_type = create_range_type (NULL, base_type, low, high);
8303
8304   /* Mark arrays with dynamic length at least as an array of unspecified
8305      length.  GDB could check the boundary but before it gets implemented at
8306      least allow accessing the array elements.  */
8307   if (attr && attr->form == DW_FORM_block1)
8308     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8309
8310   /* Ada expects an empty array on no boundary attributes.  */
8311   if (attr == NULL && cu->language != language_ada)
8312     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8313
8314   name = dwarf2_name (die, cu);
8315   if (name)
8316     TYPE_NAME (range_type) = name;
8317
8318   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8319   if (attr)
8320     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8321
8322   set_die_type (die, range_type, cu);
8323
8324   /* set_die_type should be already done.  */
8325   set_descriptive_type (range_type, die, cu);
8326
8327   return range_type;
8328 }
8329
8330 static struct type *
8331 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8332 {
8333   struct type *type;
8334
8335   /* For now, we only support the C meaning of an unspecified type: void.  */
8336
8337   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8338   TYPE_NAME (type) = dwarf2_name (die, cu);
8339
8340   return set_die_type (die, type, cu);
8341 }
8342
8343 /* Trivial hash function for die_info: the hash value of a DIE
8344    is its offset in .debug_info for this objfile.  */
8345
8346 static hashval_t
8347 die_hash (const void *item)
8348 {
8349   const struct die_info *die = item;
8350
8351   return die->offset;
8352 }
8353
8354 /* Trivial comparison function for die_info structures: two DIEs
8355    are equal if they have the same offset.  */
8356
8357 static int
8358 die_eq (const void *item_lhs, const void *item_rhs)
8359 {
8360   const struct die_info *die_lhs = item_lhs;
8361   const struct die_info *die_rhs = item_rhs;
8362
8363   return die_lhs->offset == die_rhs->offset;
8364 }
8365
8366 /* Read a whole compilation unit into a linked list of dies.  */
8367
8368 static struct die_info *
8369 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8370 {
8371   struct die_reader_specs reader_specs;
8372   int read_abbrevs = 0;
8373   struct cleanup *back_to = NULL;
8374   struct die_info *die;
8375
8376   if (cu->dwarf2_abbrevs == NULL)
8377     {
8378       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8379       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8380       read_abbrevs = 1;
8381     }
8382
8383   gdb_assert (cu->die_hash == NULL);
8384   cu->die_hash
8385     = htab_create_alloc_ex (cu->header.length / 12,
8386                             die_hash,
8387                             die_eq,
8388                             NULL,
8389                             &cu->comp_unit_obstack,
8390                             hashtab_obstack_allocate,
8391                             dummy_obstack_deallocate);
8392
8393   init_cu_die_reader (&reader_specs, cu);
8394
8395   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8396
8397   if (read_abbrevs)
8398     do_cleanups (back_to);
8399
8400   return die;
8401 }
8402
8403 /* Main entry point for reading a DIE and all children.
8404    Read the DIE and dump it if requested.  */
8405
8406 static struct die_info *
8407 read_die_and_children (const struct die_reader_specs *reader,
8408                        gdb_byte *info_ptr,
8409                        gdb_byte **new_info_ptr,
8410                        struct die_info *parent)
8411 {
8412   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8413                                                      new_info_ptr, parent);
8414
8415   if (dwarf2_die_debug)
8416     {
8417       fprintf_unfiltered (gdb_stdlog,
8418                           "\nRead die from %s of %s:\n",
8419                           reader->buffer == dwarf2_per_objfile->info.buffer
8420                           ? ".debug_info"
8421                           : reader->buffer == dwarf2_per_objfile->types.buffer
8422                           ? ".debug_types"
8423                           : "unknown section",
8424                           reader->abfd->filename);
8425       dump_die (result, dwarf2_die_debug);
8426     }
8427
8428   return result;
8429 }
8430
8431 /* Read a single die and all its descendents.  Set the die's sibling
8432    field to NULL; set other fields in the die correctly, and set all
8433    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8434    location of the info_ptr after reading all of those dies.  PARENT
8435    is the parent of the die in question.  */
8436
8437 static struct die_info *
8438 read_die_and_children_1 (const struct die_reader_specs *reader,
8439                          gdb_byte *info_ptr,
8440                          gdb_byte **new_info_ptr,
8441                          struct die_info *parent)
8442 {
8443   struct die_info *die;
8444   gdb_byte *cur_ptr;
8445   int has_children;
8446
8447   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8448   if (die == NULL)
8449     {
8450       *new_info_ptr = cur_ptr;
8451       return NULL;
8452     }
8453   store_in_ref_table (die, reader->cu);
8454
8455   if (has_children)
8456     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8457   else
8458     {
8459       die->child = NULL;
8460       *new_info_ptr = cur_ptr;
8461     }
8462
8463   die->sibling = NULL;
8464   die->parent = parent;
8465   return die;
8466 }
8467
8468 /* Read a die, all of its descendents, and all of its siblings; set
8469    all of the fields of all of the dies correctly.  Arguments are as
8470    in read_die_and_children.  */
8471
8472 static struct die_info *
8473 read_die_and_siblings (const struct die_reader_specs *reader,
8474                        gdb_byte *info_ptr,
8475                        gdb_byte **new_info_ptr,
8476                        struct die_info *parent)
8477 {
8478   struct die_info *first_die, *last_sibling;
8479   gdb_byte *cur_ptr;
8480
8481   cur_ptr = info_ptr;
8482   first_die = last_sibling = NULL;
8483
8484   while (1)
8485     {
8486       struct die_info *die
8487         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8488
8489       if (die == NULL)
8490         {
8491           *new_info_ptr = cur_ptr;
8492           return first_die;
8493         }
8494
8495       if (!first_die)
8496         first_die = die;
8497       else
8498         last_sibling->sibling = die;
8499
8500       last_sibling = die;
8501     }
8502 }
8503
8504 /* Read the die from the .debug_info section buffer.  Set DIEP to
8505    point to a newly allocated die with its information, except for its
8506    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8507    whether the die has children or not.  */
8508
8509 static gdb_byte *
8510 read_full_die (const struct die_reader_specs *reader,
8511                struct die_info **diep, gdb_byte *info_ptr,
8512                int *has_children)
8513 {
8514   unsigned int abbrev_number, bytes_read, i, offset;
8515   struct abbrev_info *abbrev;
8516   struct die_info *die;
8517   struct dwarf2_cu *cu = reader->cu;
8518   bfd *abfd = reader->abfd;
8519
8520   offset = info_ptr - reader->buffer;
8521   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8522   info_ptr += bytes_read;
8523   if (!abbrev_number)
8524     {
8525       *diep = NULL;
8526       *has_children = 0;
8527       return info_ptr;
8528     }
8529
8530   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8531   if (!abbrev)
8532     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8533            abbrev_number,
8534            bfd_get_filename (abfd));
8535
8536   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8537   die->offset = offset;
8538   die->tag = abbrev->tag;
8539   die->abbrev = abbrev_number;
8540
8541   die->num_attrs = abbrev->num_attrs;
8542
8543   for (i = 0; i < abbrev->num_attrs; ++i)
8544     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8545                                abfd, info_ptr, cu);
8546
8547   *diep = die;
8548   *has_children = abbrev->has_children;
8549   return info_ptr;
8550 }
8551
8552 /* In DWARF version 2, the description of the debugging information is
8553    stored in a separate .debug_abbrev section.  Before we read any
8554    dies from a section we read in all abbreviations and install them
8555    in a hash table.  This function also sets flags in CU describing
8556    the data found in the abbrev table.  */
8557
8558 static void
8559 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8560 {
8561   struct comp_unit_head *cu_header = &cu->header;
8562   gdb_byte *abbrev_ptr;
8563   struct abbrev_info *cur_abbrev;
8564   unsigned int abbrev_number, bytes_read, abbrev_name;
8565   unsigned int abbrev_form, hash_number;
8566   struct attr_abbrev *cur_attrs;
8567   unsigned int allocated_attrs;
8568
8569   /* Initialize dwarf2 abbrevs.  */
8570   obstack_init (&cu->abbrev_obstack);
8571   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8572                                       (ABBREV_HASH_SIZE
8573                                        * sizeof (struct abbrev_info *)));
8574   memset (cu->dwarf2_abbrevs, 0,
8575           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8576
8577   dwarf2_read_section (dwarf2_per_objfile->objfile,
8578                        &dwarf2_per_objfile->abbrev);
8579   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8580   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8581   abbrev_ptr += bytes_read;
8582
8583   allocated_attrs = ATTR_ALLOC_CHUNK;
8584   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8585
8586   /* Loop until we reach an abbrev number of 0.  */
8587   while (abbrev_number)
8588     {
8589       cur_abbrev = dwarf_alloc_abbrev (cu);
8590
8591       /* read in abbrev header */
8592       cur_abbrev->number = abbrev_number;
8593       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8594       abbrev_ptr += bytes_read;
8595       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8596       abbrev_ptr += 1;
8597
8598       if (cur_abbrev->tag == DW_TAG_namespace)
8599         cu->has_namespace_info = 1;
8600
8601       /* now read in declarations */
8602       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8603       abbrev_ptr += bytes_read;
8604       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8605       abbrev_ptr += bytes_read;
8606       while (abbrev_name)
8607         {
8608           if (cur_abbrev->num_attrs == allocated_attrs)
8609             {
8610               allocated_attrs += ATTR_ALLOC_CHUNK;
8611               cur_attrs
8612                 = xrealloc (cur_attrs, (allocated_attrs
8613                                         * sizeof (struct attr_abbrev)));
8614             }
8615
8616           /* Record whether this compilation unit might have
8617              inter-compilation-unit references.  If we don't know what form
8618              this attribute will have, then it might potentially be a
8619              DW_FORM_ref_addr, so we conservatively expect inter-CU
8620              references.  */
8621
8622           if (abbrev_form == DW_FORM_ref_addr
8623               || abbrev_form == DW_FORM_indirect)
8624             cu->has_form_ref_addr = 1;
8625
8626           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8627           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8628           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8629           abbrev_ptr += bytes_read;
8630           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8631           abbrev_ptr += bytes_read;
8632         }
8633
8634       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8635                                          (cur_abbrev->num_attrs
8636                                           * sizeof (struct attr_abbrev)));
8637       memcpy (cur_abbrev->attrs, cur_attrs,
8638               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8639
8640       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8641       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8642       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8643
8644       /* Get next abbreviation.
8645          Under Irix6 the abbreviations for a compilation unit are not
8646          always properly terminated with an abbrev number of 0.
8647          Exit loop if we encounter an abbreviation which we have
8648          already read (which means we are about to read the abbreviations
8649          for the next compile unit) or if the end of the abbreviation
8650          table is reached.  */
8651       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8652           >= dwarf2_per_objfile->abbrev.size)
8653         break;
8654       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8655       abbrev_ptr += bytes_read;
8656       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8657         break;
8658     }
8659
8660   xfree (cur_attrs);
8661 }
8662
8663 /* Release the memory used by the abbrev table for a compilation unit.  */
8664
8665 static void
8666 dwarf2_free_abbrev_table (void *ptr_to_cu)
8667 {
8668   struct dwarf2_cu *cu = ptr_to_cu;
8669
8670   obstack_free (&cu->abbrev_obstack, NULL);
8671   cu->dwarf2_abbrevs = NULL;
8672 }
8673
8674 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8675
8676 static struct abbrev_info *
8677 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8678 {
8679   unsigned int hash_number;
8680   struct abbrev_info *abbrev;
8681
8682   hash_number = number % ABBREV_HASH_SIZE;
8683   abbrev = cu->dwarf2_abbrevs[hash_number];
8684
8685   while (abbrev)
8686     {
8687       if (abbrev->number == number)
8688         return abbrev;
8689       else
8690         abbrev = abbrev->next;
8691     }
8692   return NULL;
8693 }
8694
8695 /* Returns nonzero if TAG represents a type that we might generate a partial
8696    symbol for.  */
8697
8698 static int
8699 is_type_tag_for_partial (int tag)
8700 {
8701   switch (tag)
8702     {
8703 #if 0
8704     /* Some types that would be reasonable to generate partial symbols for,
8705        that we don't at present.  */
8706     case DW_TAG_array_type:
8707     case DW_TAG_file_type:
8708     case DW_TAG_ptr_to_member_type:
8709     case DW_TAG_set_type:
8710     case DW_TAG_string_type:
8711     case DW_TAG_subroutine_type:
8712 #endif
8713     case DW_TAG_base_type:
8714     case DW_TAG_class_type:
8715     case DW_TAG_interface_type:
8716     case DW_TAG_enumeration_type:
8717     case DW_TAG_structure_type:
8718     case DW_TAG_subrange_type:
8719     case DW_TAG_typedef:
8720     case DW_TAG_union_type:
8721       return 1;
8722     default:
8723       return 0;
8724     }
8725 }
8726
8727 /* Load all DIEs that are interesting for partial symbols into memory.  */
8728
8729 static struct partial_die_info *
8730 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8731                    int building_psymtab, struct dwarf2_cu *cu)
8732 {
8733   struct partial_die_info *part_die;
8734   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8735   struct abbrev_info *abbrev;
8736   unsigned int bytes_read;
8737   unsigned int load_all = 0;
8738
8739   int nesting_level = 1;
8740
8741   parent_die = NULL;
8742   last_die = NULL;
8743
8744   if (cu->per_cu && cu->per_cu->load_all_dies)
8745     load_all = 1;
8746
8747   cu->partial_dies
8748     = htab_create_alloc_ex (cu->header.length / 12,
8749                             partial_die_hash,
8750                             partial_die_eq,
8751                             NULL,
8752                             &cu->comp_unit_obstack,
8753                             hashtab_obstack_allocate,
8754                             dummy_obstack_deallocate);
8755
8756   part_die = obstack_alloc (&cu->comp_unit_obstack,
8757                             sizeof (struct partial_die_info));
8758
8759   while (1)
8760     {
8761       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8762
8763       /* A NULL abbrev means the end of a series of children.  */
8764       if (abbrev == NULL)
8765         {
8766           if (--nesting_level == 0)
8767             {
8768               /* PART_DIE was probably the last thing allocated on the
8769                  comp_unit_obstack, so we could call obstack_free
8770                  here.  We don't do that because the waste is small,
8771                  and will be cleaned up when we're done with this
8772                  compilation unit.  This way, we're also more robust
8773                  against other users of the comp_unit_obstack.  */
8774               return first_die;
8775             }
8776           info_ptr += bytes_read;
8777           last_die = parent_die;
8778           parent_die = parent_die->die_parent;
8779           continue;
8780         }
8781
8782       /* Check for template arguments.  We never save these; if
8783          they're seen, we just mark the parent, and go on our way.  */
8784       if (parent_die != NULL
8785           && cu->language == language_cplus
8786           && (abbrev->tag == DW_TAG_template_type_param
8787               || abbrev->tag == DW_TAG_template_value_param))
8788         {
8789           parent_die->has_template_arguments = 1;
8790
8791           if (!load_all)
8792             {
8793               /* We don't need a partial DIE for the template argument.  */
8794               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8795                                        cu);
8796               continue;
8797             }
8798         }
8799
8800       /* We only recurse into subprograms looking for template arguments.
8801          Skip their other children.  */
8802       if (!load_all
8803           && cu->language == language_cplus
8804           && parent_die != NULL
8805           && parent_die->tag == DW_TAG_subprogram)
8806         {
8807           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8808           continue;
8809         }
8810
8811       /* Check whether this DIE is interesting enough to save.  Normally
8812          we would not be interested in members here, but there may be
8813          later variables referencing them via DW_AT_specification (for
8814          static members).  */
8815       if (!load_all
8816           && !is_type_tag_for_partial (abbrev->tag)
8817           && abbrev->tag != DW_TAG_constant
8818           && abbrev->tag != DW_TAG_enumerator
8819           && abbrev->tag != DW_TAG_subprogram
8820           && abbrev->tag != DW_TAG_lexical_block
8821           && abbrev->tag != DW_TAG_variable
8822           && abbrev->tag != DW_TAG_namespace
8823           && abbrev->tag != DW_TAG_module
8824           && abbrev->tag != DW_TAG_member)
8825         {
8826           /* Otherwise we skip to the next sibling, if any.  */
8827           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8828           continue;
8829         }
8830
8831       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8832                                    buffer, info_ptr, cu);
8833
8834       /* This two-pass algorithm for processing partial symbols has a
8835          high cost in cache pressure.  Thus, handle some simple cases
8836          here which cover the majority of C partial symbols.  DIEs
8837          which neither have specification tags in them, nor could have
8838          specification tags elsewhere pointing at them, can simply be
8839          processed and discarded.
8840
8841          This segment is also optional; scan_partial_symbols and
8842          add_partial_symbol will handle these DIEs if we chain
8843          them in normally.  When compilers which do not emit large
8844          quantities of duplicate debug information are more common,
8845          this code can probably be removed.  */
8846
8847       /* Any complete simple types at the top level (pretty much all
8848          of them, for a language without namespaces), can be processed
8849          directly.  */
8850       if (parent_die == NULL
8851           && part_die->has_specification == 0
8852           && part_die->is_declaration == 0
8853           && (part_die->tag == DW_TAG_typedef
8854               || part_die->tag == DW_TAG_base_type
8855               || part_die->tag == DW_TAG_subrange_type))
8856         {
8857           if (building_psymtab && part_die->name != NULL)
8858             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8859                                  VAR_DOMAIN, LOC_TYPEDEF,
8860                                  &cu->objfile->static_psymbols,
8861                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8862           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8863           continue;
8864         }
8865
8866       /* If we're at the second level, and we're an enumerator, and
8867          our parent has no specification (meaning possibly lives in a
8868          namespace elsewhere), then we can add the partial symbol now
8869          instead of queueing it.  */
8870       if (part_die->tag == DW_TAG_enumerator
8871           && parent_die != NULL
8872           && parent_die->die_parent == NULL
8873           && parent_die->tag == DW_TAG_enumeration_type
8874           && parent_die->has_specification == 0)
8875         {
8876           if (part_die->name == NULL)
8877             complaint (&symfile_complaints,
8878                        _("malformed enumerator DIE ignored"));
8879           else if (building_psymtab)
8880             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8881                                  VAR_DOMAIN, LOC_CONST,
8882                                  (cu->language == language_cplus
8883                                   || cu->language == language_java)
8884                                  ? &cu->objfile->global_psymbols
8885                                  : &cu->objfile->static_psymbols,
8886                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8887
8888           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8889           continue;
8890         }
8891
8892       /* We'll save this DIE so link it in.  */
8893       part_die->die_parent = parent_die;
8894       part_die->die_sibling = NULL;
8895       part_die->die_child = NULL;
8896
8897       if (last_die && last_die == parent_die)
8898         last_die->die_child = part_die;
8899       else if (last_die)
8900         last_die->die_sibling = part_die;
8901
8902       last_die = part_die;
8903
8904       if (first_die == NULL)
8905         first_die = part_die;
8906
8907       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8908          find interesting need to be in the hash table, because we
8909          also have the parent/sibling/child chains; only those that we
8910          might refer to by offset later during partial symbol reading.
8911
8912          For now this means things that might have be the target of a
8913          DW_AT_specification, DW_AT_abstract_origin, or
8914          DW_AT_extension.  DW_AT_extension will refer only to
8915          namespaces; DW_AT_abstract_origin refers to functions (and
8916          many things under the function DIE, but we do not recurse
8917          into function DIEs during partial symbol reading) and
8918          possibly variables as well; DW_AT_specification refers to
8919          declarations.  Declarations ought to have the DW_AT_declaration
8920          flag.  It happens that GCC forgets to put it in sometimes, but
8921          only for functions, not for types.
8922
8923          Adding more things than necessary to the hash table is harmless
8924          except for the performance cost.  Adding too few will result in
8925          wasted time in find_partial_die, when we reread the compilation
8926          unit with load_all_dies set.  */
8927
8928       if (load_all
8929           || abbrev->tag == DW_TAG_constant
8930           || abbrev->tag == DW_TAG_subprogram
8931           || abbrev->tag == DW_TAG_variable
8932           || abbrev->tag == DW_TAG_namespace
8933           || part_die->is_declaration)
8934         {
8935           void **slot;
8936
8937           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8938                                            part_die->offset, INSERT);
8939           *slot = part_die;
8940         }
8941
8942       part_die = obstack_alloc (&cu->comp_unit_obstack,
8943                                 sizeof (struct partial_die_info));
8944
8945       /* For some DIEs we want to follow their children (if any).  For C
8946          we have no reason to follow the children of structures; for other
8947          languages we have to, so that we can get at method physnames
8948          to infer fully qualified class names, for DW_AT_specification,
8949          and for C++ template arguments.  For C++, we also look one level
8950          inside functions to find template arguments (if the name of the
8951          function does not already contain the template arguments).
8952
8953          For Ada, we need to scan the children of subprograms and lexical
8954          blocks as well because Ada allows the definition of nested
8955          entities that could be interesting for the debugger, such as
8956          nested subprograms for instance.  */
8957       if (last_die->has_children
8958           && (load_all
8959               || last_die->tag == DW_TAG_namespace
8960               || last_die->tag == DW_TAG_module
8961               || last_die->tag == DW_TAG_enumeration_type
8962               || (cu->language == language_cplus
8963                   && last_die->tag == DW_TAG_subprogram
8964                   && (last_die->name == NULL
8965                       || strchr (last_die->name, '<') == NULL))
8966               || (cu->language != language_c
8967                   && (last_die->tag == DW_TAG_class_type
8968                       || last_die->tag == DW_TAG_interface_type
8969                       || last_die->tag == DW_TAG_structure_type
8970                       || last_die->tag == DW_TAG_union_type))
8971               || (cu->language == language_ada
8972                   && (last_die->tag == DW_TAG_subprogram
8973                       || last_die->tag == DW_TAG_lexical_block))))
8974         {
8975           nesting_level++;
8976           parent_die = last_die;
8977           continue;
8978         }
8979
8980       /* Otherwise we skip to the next sibling, if any.  */
8981       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8982
8983       /* Back to the top, do it again.  */
8984     }
8985 }
8986
8987 /* Read a minimal amount of information into the minimal die structure.  */
8988
8989 static gdb_byte *
8990 read_partial_die (struct partial_die_info *part_die,
8991                   struct abbrev_info *abbrev,
8992                   unsigned int abbrev_len, bfd *abfd,
8993                   gdb_byte *buffer, gdb_byte *info_ptr,
8994                   struct dwarf2_cu *cu)
8995 {
8996   unsigned int i;
8997   struct attribute attr;
8998   int has_low_pc_attr = 0;
8999   int has_high_pc_attr = 0;
9000
9001   memset (part_die, 0, sizeof (struct partial_die_info));
9002
9003   part_die->offset = info_ptr - buffer;
9004
9005   info_ptr += abbrev_len;
9006
9007   if (abbrev == NULL)
9008     return info_ptr;
9009
9010   part_die->tag = abbrev->tag;
9011   part_die->has_children = abbrev->has_children;
9012
9013   for (i = 0; i < abbrev->num_attrs; ++i)
9014     {
9015       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9016
9017       /* Store the data if it is of an attribute we want to keep in a
9018          partial symbol table.  */
9019       switch (attr.name)
9020         {
9021         case DW_AT_name:
9022           switch (part_die->tag)
9023             {
9024             case DW_TAG_compile_unit:
9025             case DW_TAG_type_unit:
9026               /* Compilation units have a DW_AT_name that is a filename, not
9027                  a source language identifier.  */
9028             case DW_TAG_enumeration_type:
9029             case DW_TAG_enumerator:
9030               /* These tags always have simple identifiers already; no need
9031                  to canonicalize them.  */
9032               part_die->name = DW_STRING (&attr);
9033               break;
9034             default:
9035               part_die->name
9036                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9037                                             &cu->objfile->objfile_obstack);
9038               break;
9039             }
9040           break;
9041         case DW_AT_linkage_name:
9042         case DW_AT_MIPS_linkage_name:
9043           /* Note that both forms of linkage name might appear.  We
9044              assume they will be the same, and we only store the last
9045              one we see.  */
9046           if (cu->language == language_ada)
9047             part_die->name = DW_STRING (&attr);
9048           part_die->linkage_name = DW_STRING (&attr);
9049           break;
9050         case DW_AT_low_pc:
9051           has_low_pc_attr = 1;
9052           part_die->lowpc = DW_ADDR (&attr);
9053           break;
9054         case DW_AT_high_pc:
9055           has_high_pc_attr = 1;
9056           part_die->highpc = DW_ADDR (&attr);
9057           break;
9058         case DW_AT_location:
9059           /* Support the .debug_loc offsets.  */
9060           if (attr_form_is_block (&attr))
9061             {
9062                part_die->locdesc = DW_BLOCK (&attr);
9063             }
9064           else if (attr_form_is_section_offset (&attr))
9065             {
9066               dwarf2_complex_location_expr_complaint ();
9067             }
9068           else
9069             {
9070               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9071                                                      "partial symbol information");
9072             }
9073           break;
9074         case DW_AT_external:
9075           part_die->is_external = DW_UNSND (&attr);
9076           break;
9077         case DW_AT_declaration:
9078           part_die->is_declaration = DW_UNSND (&attr);
9079           break;
9080         case DW_AT_type:
9081           part_die->has_type = 1;
9082           break;
9083         case DW_AT_abstract_origin:
9084         case DW_AT_specification:
9085         case DW_AT_extension:
9086           part_die->has_specification = 1;
9087           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9088           break;
9089         case DW_AT_sibling:
9090           /* Ignore absolute siblings, they might point outside of
9091              the current compile unit.  */
9092           if (attr.form == DW_FORM_ref_addr)
9093             complaint (&symfile_complaints,
9094                        _("ignoring absolute DW_AT_sibling"));
9095           else
9096             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9097           break;
9098         case DW_AT_byte_size:
9099           part_die->has_byte_size = 1;
9100           break;
9101         case DW_AT_calling_convention:
9102           /* DWARF doesn't provide a way to identify a program's source-level
9103              entry point.  DW_AT_calling_convention attributes are only meant
9104              to describe functions' calling conventions.
9105
9106              However, because it's a necessary piece of information in
9107              Fortran, and because DW_CC_program is the only piece of debugging
9108              information whose definition refers to a 'main program' at all,
9109              several compilers have begun marking Fortran main programs with
9110              DW_CC_program --- even when those functions use the standard
9111              calling conventions.
9112
9113              So until DWARF specifies a way to provide this information and
9114              compilers pick up the new representation, we'll support this
9115              practice.  */
9116           if (DW_UNSND (&attr) == DW_CC_program
9117               && cu->language == language_fortran)
9118             {
9119               set_main_name (part_die->name);
9120
9121               /* As this DIE has a static linkage the name would be difficult
9122                  to look up later.  */
9123               language_of_main = language_fortran;
9124             }
9125           break;
9126         default:
9127           break;
9128         }
9129     }
9130
9131   /* When using the GNU linker, .gnu.linkonce. sections are used to
9132      eliminate duplicate copies of functions and vtables and such.
9133      The linker will arbitrarily choose one and discard the others.
9134      The AT_*_pc values for such functions refer to local labels in
9135      these sections.  If the section from that file was discarded, the
9136      labels are not in the output, so the relocs get a value of 0.
9137      If this is a discarded function, mark the pc bounds as invalid,
9138      so that GDB will ignore it.  */
9139   if (has_low_pc_attr && has_high_pc_attr
9140       && part_die->lowpc < part_die->highpc
9141       && (part_die->lowpc != 0
9142           || dwarf2_per_objfile->has_section_at_zero))
9143     part_die->has_pc_info = 1;
9144
9145   return info_ptr;
9146 }
9147
9148 /* Find a cached partial DIE at OFFSET in CU.  */
9149
9150 static struct partial_die_info *
9151 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9152 {
9153   struct partial_die_info *lookup_die = NULL;
9154   struct partial_die_info part_die;
9155
9156   part_die.offset = offset;
9157   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9158
9159   return lookup_die;
9160 }
9161
9162 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9163    except in the case of .debug_types DIEs which do not reference
9164    outside their CU (they do however referencing other types via
9165    DW_FORM_sig8).  */
9166
9167 static struct partial_die_info *
9168 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9169 {
9170   struct dwarf2_per_cu_data *per_cu = NULL;
9171   struct partial_die_info *pd = NULL;
9172
9173   if (cu->per_cu->from_debug_types)
9174     {
9175       pd = find_partial_die_in_comp_unit (offset, cu);
9176       if (pd != NULL)
9177         return pd;
9178       goto not_found;
9179     }
9180
9181   if (offset_in_cu_p (&cu->header, offset))
9182     {
9183       pd = find_partial_die_in_comp_unit (offset, cu);
9184       if (pd != NULL)
9185         return pd;
9186     }
9187
9188   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9189
9190   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9191     load_partial_comp_unit (per_cu, cu->objfile);
9192
9193   per_cu->cu->last_used = 0;
9194   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9195
9196   if (pd == NULL && per_cu->load_all_dies == 0)
9197     {
9198       struct cleanup *back_to;
9199       struct partial_die_info comp_unit_die;
9200       struct abbrev_info *abbrev;
9201       unsigned int bytes_read;
9202       char *info_ptr;
9203
9204       per_cu->load_all_dies = 1;
9205
9206       /* Re-read the DIEs.  */
9207       back_to = make_cleanup (null_cleanup, 0);
9208       if (per_cu->cu->dwarf2_abbrevs == NULL)
9209         {
9210           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9211           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9212         }
9213       info_ptr = (dwarf2_per_objfile->info.buffer
9214                   + per_cu->cu->header.offset
9215                   + per_cu->cu->header.first_die_offset);
9216       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9217       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9218                                    per_cu->cu->objfile->obfd,
9219                                    dwarf2_per_objfile->info.buffer, info_ptr,
9220                                    per_cu->cu);
9221       if (comp_unit_die.has_children)
9222         load_partial_dies (per_cu->cu->objfile->obfd,
9223                            dwarf2_per_objfile->info.buffer, info_ptr,
9224                            0, per_cu->cu);
9225       do_cleanups (back_to);
9226
9227       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9228     }
9229
9230  not_found:
9231
9232   if (pd == NULL)
9233     internal_error (__FILE__, __LINE__,
9234                     _("could not find partial DIE 0x%x "
9235                       "in cache [from module %s]\n"),
9236                     offset, bfd_get_filename (cu->objfile->obfd));
9237   return pd;
9238 }
9239
9240 /* See if we can figure out if the class lives in a namespace.  We do
9241    this by looking for a member function; its demangled name will
9242    contain namespace info, if there is any.  */
9243
9244 static void
9245 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9246                                   struct dwarf2_cu *cu)
9247 {
9248   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9249      what template types look like, because the demangler
9250      frequently doesn't give the same name as the debug info.  We
9251      could fix this by only using the demangled name to get the
9252      prefix (but see comment in read_structure_type).  */
9253
9254   struct partial_die_info *real_pdi;
9255   struct partial_die_info *child_pdi;
9256
9257   /* If this DIE (this DIE's specification, if any) has a parent, then
9258      we should not do this.  We'll prepend the parent's fully qualified
9259      name when we create the partial symbol.  */
9260
9261   real_pdi = struct_pdi;
9262   while (real_pdi->has_specification)
9263     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9264
9265   if (real_pdi->die_parent != NULL)
9266     return;
9267
9268   for (child_pdi = struct_pdi->die_child;
9269        child_pdi != NULL;
9270        child_pdi = child_pdi->die_sibling)
9271     {
9272       if (child_pdi->tag == DW_TAG_subprogram
9273           && child_pdi->linkage_name != NULL)
9274         {
9275           char *actual_class_name
9276             = language_class_name_from_physname (cu->language_defn,
9277                                                  child_pdi->linkage_name);
9278           if (actual_class_name != NULL)
9279             {
9280               struct_pdi->name
9281                 = obsavestring (actual_class_name,
9282                                 strlen (actual_class_name),
9283                                 &cu->objfile->objfile_obstack);
9284               xfree (actual_class_name);
9285             }
9286           break;
9287         }
9288     }
9289 }
9290
9291 /* Adjust PART_DIE before generating a symbol for it.  This function
9292    may set the is_external flag or change the DIE's name.  */
9293
9294 static void
9295 fixup_partial_die (struct partial_die_info *part_die,
9296                    struct dwarf2_cu *cu)
9297 {
9298   /* Once we've fixed up a die, there's no point in doing so again.
9299      This also avoids a memory leak if we were to call
9300      guess_partial_die_structure_name multiple times.  */
9301   if (part_die->fixup_called)
9302     return;
9303
9304   /* If we found a reference attribute and the DIE has no name, try
9305      to find a name in the referred to DIE.  */
9306
9307   if (part_die->name == NULL && part_die->has_specification)
9308     {
9309       struct partial_die_info *spec_die;
9310
9311       spec_die = find_partial_die (part_die->spec_offset, cu);
9312
9313       fixup_partial_die (spec_die, cu);
9314
9315       if (spec_die->name)
9316         {
9317           part_die->name = spec_die->name;
9318
9319           /* Copy DW_AT_external attribute if it is set.  */
9320           if (spec_die->is_external)
9321             part_die->is_external = spec_die->is_external;
9322         }
9323     }
9324
9325   /* Set default names for some unnamed DIEs.  */
9326
9327   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9328     part_die->name = "(anonymous namespace)";
9329
9330   /* If there is no parent die to provide a namespace, and there are
9331      children, see if we can determine the namespace from their linkage
9332      name.
9333      NOTE: We need to do this even if cu->has_namespace_info != 0.
9334      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9335   if (cu->language == language_cplus
9336       && dwarf2_per_objfile->types.asection != NULL
9337       && part_die->die_parent == NULL
9338       && part_die->has_children
9339       && (part_die->tag == DW_TAG_class_type
9340           || part_die->tag == DW_TAG_structure_type
9341           || part_die->tag == DW_TAG_union_type))
9342     guess_partial_die_structure_name (part_die, cu);
9343
9344   part_die->fixup_called = 1;
9345 }
9346
9347 /* Read an attribute value described by an attribute form.  */
9348
9349 static gdb_byte *
9350 read_attribute_value (struct attribute *attr, unsigned form,
9351                       bfd *abfd, gdb_byte *info_ptr,
9352                       struct dwarf2_cu *cu)
9353 {
9354   struct comp_unit_head *cu_header = &cu->header;
9355   unsigned int bytes_read;
9356   struct dwarf_block *blk;
9357
9358   attr->form = form;
9359   switch (form)
9360     {
9361     case DW_FORM_ref_addr:
9362       if (cu->header.version == 2)
9363         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9364       else
9365         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9366                                       &cu->header, &bytes_read);
9367       info_ptr += bytes_read;
9368       break;
9369     case DW_FORM_addr:
9370       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9371       info_ptr += bytes_read;
9372       break;
9373     case DW_FORM_block2:
9374       blk = dwarf_alloc_block (cu);
9375       blk->size = read_2_bytes (abfd, info_ptr);
9376       info_ptr += 2;
9377       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9378       info_ptr += blk->size;
9379       DW_BLOCK (attr) = blk;
9380       break;
9381     case DW_FORM_block4:
9382       blk = dwarf_alloc_block (cu);
9383       blk->size = read_4_bytes (abfd, info_ptr);
9384       info_ptr += 4;
9385       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9386       info_ptr += blk->size;
9387       DW_BLOCK (attr) = blk;
9388       break;
9389     case DW_FORM_data2:
9390       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9391       info_ptr += 2;
9392       break;
9393     case DW_FORM_data4:
9394       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9395       info_ptr += 4;
9396       break;
9397     case DW_FORM_data8:
9398       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9399       info_ptr += 8;
9400       break;
9401     case DW_FORM_sec_offset:
9402       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9403       info_ptr += bytes_read;
9404       break;
9405     case DW_FORM_string:
9406       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9407       DW_STRING_IS_CANONICAL (attr) = 0;
9408       info_ptr += bytes_read;
9409       break;
9410     case DW_FORM_strp:
9411       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9412                                                &bytes_read);
9413       DW_STRING_IS_CANONICAL (attr) = 0;
9414       info_ptr += bytes_read;
9415       break;
9416     case DW_FORM_exprloc:
9417     case DW_FORM_block:
9418       blk = dwarf_alloc_block (cu);
9419       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9420       info_ptr += bytes_read;
9421       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9422       info_ptr += blk->size;
9423       DW_BLOCK (attr) = blk;
9424       break;
9425     case DW_FORM_block1:
9426       blk = dwarf_alloc_block (cu);
9427       blk->size = read_1_byte (abfd, info_ptr);
9428       info_ptr += 1;
9429       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9430       info_ptr += blk->size;
9431       DW_BLOCK (attr) = blk;
9432       break;
9433     case DW_FORM_data1:
9434       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9435       info_ptr += 1;
9436       break;
9437     case DW_FORM_flag:
9438       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9439       info_ptr += 1;
9440       break;
9441     case DW_FORM_flag_present:
9442       DW_UNSND (attr) = 1;
9443       break;
9444     case DW_FORM_sdata:
9445       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9446       info_ptr += bytes_read;
9447       break;
9448     case DW_FORM_udata:
9449       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9450       info_ptr += bytes_read;
9451       break;
9452     case DW_FORM_ref1:
9453       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9454       info_ptr += 1;
9455       break;
9456     case DW_FORM_ref2:
9457       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9458       info_ptr += 2;
9459       break;
9460     case DW_FORM_ref4:
9461       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9462       info_ptr += 4;
9463       break;
9464     case DW_FORM_ref8:
9465       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9466       info_ptr += 8;
9467       break;
9468     case DW_FORM_sig8:
9469       /* Convert the signature to something we can record in DW_UNSND
9470          for later lookup.
9471          NOTE: This is NULL if the type wasn't found.  */
9472       DW_SIGNATURED_TYPE (attr) =
9473         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9474       info_ptr += 8;
9475       break;
9476     case DW_FORM_ref_udata:
9477       DW_ADDR (attr) = (cu->header.offset
9478                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9479       info_ptr += bytes_read;
9480       break;
9481     case DW_FORM_indirect:
9482       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9483       info_ptr += bytes_read;
9484       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9485       break;
9486     default:
9487       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9488              dwarf_form_name (form),
9489              bfd_get_filename (abfd));
9490     }
9491
9492   /* We have seen instances where the compiler tried to emit a byte
9493      size attribute of -1 which ended up being encoded as an unsigned
9494      0xffffffff.  Although 0xffffffff is technically a valid size value,
9495      an object of this size seems pretty unlikely so we can relatively
9496      safely treat these cases as if the size attribute was invalid and
9497      treat them as zero by default.  */
9498   if (attr->name == DW_AT_byte_size
9499       && form == DW_FORM_data4
9500       && DW_UNSND (attr) >= 0xffffffff)
9501     {
9502       complaint
9503         (&symfile_complaints,
9504          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9505          hex_string (DW_UNSND (attr)));
9506       DW_UNSND (attr) = 0;
9507     }
9508
9509   return info_ptr;
9510 }
9511
9512 /* Read an attribute described by an abbreviated attribute.  */
9513
9514 static gdb_byte *
9515 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9516                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9517 {
9518   attr->name = abbrev->name;
9519   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9520 }
9521
9522 /* Read dwarf information from a buffer.  */
9523
9524 static unsigned int
9525 read_1_byte (bfd *abfd, gdb_byte *buf)
9526 {
9527   return bfd_get_8 (abfd, buf);
9528 }
9529
9530 static int
9531 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9532 {
9533   return bfd_get_signed_8 (abfd, buf);
9534 }
9535
9536 static unsigned int
9537 read_2_bytes (bfd *abfd, gdb_byte *buf)
9538 {
9539   return bfd_get_16 (abfd, buf);
9540 }
9541
9542 static int
9543 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9544 {
9545   return bfd_get_signed_16 (abfd, buf);
9546 }
9547
9548 static unsigned int
9549 read_4_bytes (bfd *abfd, gdb_byte *buf)
9550 {
9551   return bfd_get_32 (abfd, buf);
9552 }
9553
9554 static int
9555 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9556 {
9557   return bfd_get_signed_32 (abfd, buf);
9558 }
9559
9560 static ULONGEST
9561 read_8_bytes (bfd *abfd, gdb_byte *buf)
9562 {
9563   return bfd_get_64 (abfd, buf);
9564 }
9565
9566 static CORE_ADDR
9567 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9568               unsigned int *bytes_read)
9569 {
9570   struct comp_unit_head *cu_header = &cu->header;
9571   CORE_ADDR retval = 0;
9572
9573   if (cu_header->signed_addr_p)
9574     {
9575       switch (cu_header->addr_size)
9576         {
9577         case 2:
9578           retval = bfd_get_signed_16 (abfd, buf);
9579           break;
9580         case 4:
9581           retval = bfd_get_signed_32 (abfd, buf);
9582           break;
9583         case 8:
9584           retval = bfd_get_signed_64 (abfd, buf);
9585           break;
9586         default:
9587           internal_error (__FILE__, __LINE__,
9588                           _("read_address: bad switch, signed [in module %s]"),
9589                           bfd_get_filename (abfd));
9590         }
9591     }
9592   else
9593     {
9594       switch (cu_header->addr_size)
9595         {
9596         case 2:
9597           retval = bfd_get_16 (abfd, buf);
9598           break;
9599         case 4:
9600           retval = bfd_get_32 (abfd, buf);
9601           break;
9602         case 8:
9603           retval = bfd_get_64 (abfd, buf);
9604           break;
9605         default:
9606           internal_error (__FILE__, __LINE__,
9607                           _("read_address: bad switch, "
9608                             "unsigned [in module %s]"),
9609                           bfd_get_filename (abfd));
9610         }
9611     }
9612
9613   *bytes_read = cu_header->addr_size;
9614   return retval;
9615 }
9616
9617 /* Read the initial length from a section.  The (draft) DWARF 3
9618    specification allows the initial length to take up either 4 bytes
9619    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9620    bytes describe the length and all offsets will be 8 bytes in length
9621    instead of 4.
9622
9623    An older, non-standard 64-bit format is also handled by this
9624    function.  The older format in question stores the initial length
9625    as an 8-byte quantity without an escape value.  Lengths greater
9626    than 2^32 aren't very common which means that the initial 4 bytes
9627    is almost always zero.  Since a length value of zero doesn't make
9628    sense for the 32-bit format, this initial zero can be considered to
9629    be an escape value which indicates the presence of the older 64-bit
9630    format.  As written, the code can't detect (old format) lengths
9631    greater than 4GB.  If it becomes necessary to handle lengths
9632    somewhat larger than 4GB, we could allow other small values (such
9633    as the non-sensical values of 1, 2, and 3) to also be used as
9634    escape values indicating the presence of the old format.
9635
9636    The value returned via bytes_read should be used to increment the
9637    relevant pointer after calling read_initial_length().
9638
9639    [ Note:  read_initial_length() and read_offset() are based on the
9640      document entitled "DWARF Debugging Information Format", revision
9641      3, draft 8, dated November 19, 2001.  This document was obtained
9642      from:
9643
9644         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9645
9646      This document is only a draft and is subject to change.  (So beware.)
9647
9648      Details regarding the older, non-standard 64-bit format were
9649      determined empirically by examining 64-bit ELF files produced by
9650      the SGI toolchain on an IRIX 6.5 machine.
9651
9652      - Kevin, July 16, 2002
9653    ] */
9654
9655 static LONGEST
9656 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9657 {
9658   LONGEST length = bfd_get_32 (abfd, buf);
9659
9660   if (length == 0xffffffff)
9661     {
9662       length = bfd_get_64 (abfd, buf + 4);
9663       *bytes_read = 12;
9664     }
9665   else if (length == 0)
9666     {
9667       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9668       length = bfd_get_64 (abfd, buf);
9669       *bytes_read = 8;
9670     }
9671   else
9672     {
9673       *bytes_read = 4;
9674     }
9675
9676   return length;
9677 }
9678
9679 /* Cover function for read_initial_length.
9680    Returns the length of the object at BUF, and stores the size of the
9681    initial length in *BYTES_READ and stores the size that offsets will be in
9682    *OFFSET_SIZE.
9683    If the initial length size is not equivalent to that specified in
9684    CU_HEADER then issue a complaint.
9685    This is useful when reading non-comp-unit headers.  */
9686
9687 static LONGEST
9688 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9689                                         const struct comp_unit_head *cu_header,
9690                                         unsigned int *bytes_read,
9691                                         unsigned int *offset_size)
9692 {
9693   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9694
9695   gdb_assert (cu_header->initial_length_size == 4
9696               || cu_header->initial_length_size == 8
9697               || cu_header->initial_length_size == 12);
9698
9699   if (cu_header->initial_length_size != *bytes_read)
9700     complaint (&symfile_complaints,
9701                _("intermixed 32-bit and 64-bit DWARF sections"));
9702
9703   *offset_size = (*bytes_read == 4) ? 4 : 8;
9704   return length;
9705 }
9706
9707 /* Read an offset from the data stream.  The size of the offset is
9708    given by cu_header->offset_size.  */
9709
9710 static LONGEST
9711 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9712              unsigned int *bytes_read)
9713 {
9714   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9715
9716   *bytes_read = cu_header->offset_size;
9717   return offset;
9718 }
9719
9720 /* Read an offset from the data stream.  */
9721
9722 static LONGEST
9723 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9724 {
9725   LONGEST retval = 0;
9726
9727   switch (offset_size)
9728     {
9729     case 4:
9730       retval = bfd_get_32 (abfd, buf);
9731       break;
9732     case 8:
9733       retval = bfd_get_64 (abfd, buf);
9734       break;
9735     default:
9736       internal_error (__FILE__, __LINE__,
9737                       _("read_offset_1: bad switch [in module %s]"),
9738                       bfd_get_filename (abfd));
9739     }
9740
9741   return retval;
9742 }
9743
9744 static gdb_byte *
9745 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9746 {
9747   /* If the size of a host char is 8 bits, we can return a pointer
9748      to the buffer, otherwise we have to copy the data to a buffer
9749      allocated on the temporary obstack.  */
9750   gdb_assert (HOST_CHAR_BIT == 8);
9751   return buf;
9752 }
9753
9754 static char *
9755 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9756 {
9757   /* If the size of a host char is 8 bits, we can return a pointer
9758      to the string, otherwise we have to copy the string to a buffer
9759      allocated on the temporary obstack.  */
9760   gdb_assert (HOST_CHAR_BIT == 8);
9761   if (*buf == '\0')
9762     {
9763       *bytes_read_ptr = 1;
9764       return NULL;
9765     }
9766   *bytes_read_ptr = strlen ((char *) buf) + 1;
9767   return (char *) buf;
9768 }
9769
9770 static char *
9771 read_indirect_string (bfd *abfd, gdb_byte *buf,
9772                       const struct comp_unit_head *cu_header,
9773                       unsigned int *bytes_read_ptr)
9774 {
9775   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9776
9777   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9778   if (dwarf2_per_objfile->str.buffer == NULL)
9779     {
9780       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9781                       bfd_get_filename (abfd));
9782       return NULL;
9783     }
9784   if (str_offset >= dwarf2_per_objfile->str.size)
9785     {
9786       error (_("DW_FORM_strp pointing outside of "
9787                ".debug_str section [in module %s]"),
9788              bfd_get_filename (abfd));
9789       return NULL;
9790     }
9791   gdb_assert (HOST_CHAR_BIT == 8);
9792   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9793     return NULL;
9794   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9795 }
9796
9797 static unsigned long
9798 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9799 {
9800   unsigned long result;
9801   unsigned int num_read;
9802   int i, shift;
9803   unsigned char byte;
9804
9805   result = 0;
9806   shift = 0;
9807   num_read = 0;
9808   i = 0;
9809   while (1)
9810     {
9811       byte = bfd_get_8 (abfd, buf);
9812       buf++;
9813       num_read++;
9814       result |= ((unsigned long)(byte & 127) << shift);
9815       if ((byte & 128) == 0)
9816         {
9817           break;
9818         }
9819       shift += 7;
9820     }
9821   *bytes_read_ptr = num_read;
9822   return result;
9823 }
9824
9825 static long
9826 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9827 {
9828   long result;
9829   int i, shift, num_read;
9830   unsigned char byte;
9831
9832   result = 0;
9833   shift = 0;
9834   num_read = 0;
9835   i = 0;
9836   while (1)
9837     {
9838       byte = bfd_get_8 (abfd, buf);
9839       buf++;
9840       num_read++;
9841       result |= ((long)(byte & 127) << shift);
9842       shift += 7;
9843       if ((byte & 128) == 0)
9844         {
9845           break;
9846         }
9847     }
9848   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9849     result |= -(((long)1) << shift);
9850   *bytes_read_ptr = num_read;
9851   return result;
9852 }
9853
9854 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9855
9856 static gdb_byte *
9857 skip_leb128 (bfd *abfd, gdb_byte *buf)
9858 {
9859   int byte;
9860
9861   while (1)
9862     {
9863       byte = bfd_get_8 (abfd, buf);
9864       buf++;
9865       if ((byte & 128) == 0)
9866         return buf;
9867     }
9868 }
9869
9870 static void
9871 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9872 {
9873   switch (lang)
9874     {
9875     case DW_LANG_C89:
9876     case DW_LANG_C99:
9877     case DW_LANG_C:
9878       cu->language = language_c;
9879       break;
9880     case DW_LANG_C_plus_plus:
9881       cu->language = language_cplus;
9882       break;
9883     case DW_LANG_D:
9884       cu->language = language_d;
9885       break;
9886     case DW_LANG_Fortran77:
9887     case DW_LANG_Fortran90:
9888     case DW_LANG_Fortran95:
9889       cu->language = language_fortran;
9890       break;
9891     case DW_LANG_Mips_Assembler:
9892       cu->language = language_asm;
9893       break;
9894     case DW_LANG_Java:
9895       cu->language = language_java;
9896       break;
9897     case DW_LANG_Ada83:
9898     case DW_LANG_Ada95:
9899       cu->language = language_ada;
9900       break;
9901     case DW_LANG_Modula2:
9902       cu->language = language_m2;
9903       break;
9904     case DW_LANG_Pascal83:
9905       cu->language = language_pascal;
9906       break;
9907     case DW_LANG_ObjC:
9908       cu->language = language_objc;
9909       break;
9910     case DW_LANG_Cobol74:
9911     case DW_LANG_Cobol85:
9912     default:
9913       cu->language = language_minimal;
9914       break;
9915     }
9916   cu->language_defn = language_def (cu->language);
9917 }
9918
9919 /* Return the named attribute or NULL if not there.  */
9920
9921 static struct attribute *
9922 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9923 {
9924   unsigned int i;
9925   struct attribute *spec = NULL;
9926
9927   for (i = 0; i < die->num_attrs; ++i)
9928     {
9929       if (die->attrs[i].name == name)
9930         return &die->attrs[i];
9931       if (die->attrs[i].name == DW_AT_specification
9932           || die->attrs[i].name == DW_AT_abstract_origin)
9933         spec = &die->attrs[i];
9934     }
9935
9936   if (spec)
9937     {
9938       die = follow_die_ref (die, spec, &cu);
9939       return dwarf2_attr (die, name, cu);
9940     }
9941
9942   return NULL;
9943 }
9944
9945 /* Return the named attribute or NULL if not there,
9946    but do not follow DW_AT_specification, etc.
9947    This is for use in contexts where we're reading .debug_types dies.
9948    Following DW_AT_specification, DW_AT_abstract_origin will take us
9949    back up the chain, and we want to go down.  */
9950
9951 static struct attribute *
9952 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9953                        struct dwarf2_cu *cu)
9954 {
9955   unsigned int i;
9956
9957   for (i = 0; i < die->num_attrs; ++i)
9958     if (die->attrs[i].name == name)
9959       return &die->attrs[i];
9960
9961   return NULL;
9962 }
9963
9964 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9965    and holds a non-zero value.  This function should only be used for
9966    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9967
9968 static int
9969 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9970 {
9971   struct attribute *attr = dwarf2_attr (die, name, cu);
9972
9973   return (attr && DW_UNSND (attr));
9974 }
9975
9976 static int
9977 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9978 {
9979   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9980      which value is non-zero.  However, we have to be careful with
9981      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9982      (via dwarf2_flag_true_p) follows this attribute.  So we may
9983      end up accidently finding a declaration attribute that belongs
9984      to a different DIE referenced by the specification attribute,
9985      even though the given DIE does not have a declaration attribute.  */
9986   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9987           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9988 }
9989
9990 /* Return the die giving the specification for DIE, if there is
9991    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9992    containing the return value on output.  If there is no
9993    specification, but there is an abstract origin, that is
9994    returned.  */
9995
9996 static struct die_info *
9997 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9998 {
9999   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10000                                              *spec_cu);
10001
10002   if (spec_attr == NULL)
10003     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10004
10005   if (spec_attr == NULL)
10006     return NULL;
10007   else
10008     return follow_die_ref (die, spec_attr, spec_cu);
10009 }
10010
10011 /* Free the line_header structure *LH, and any arrays and strings it
10012    refers to.
10013    NOTE: This is also used as a "cleanup" function.  */
10014
10015 static void
10016 free_line_header (struct line_header *lh)
10017 {
10018   if (lh->standard_opcode_lengths)
10019     xfree (lh->standard_opcode_lengths);
10020
10021   /* Remember that all the lh->file_names[i].name pointers are
10022      pointers into debug_line_buffer, and don't need to be freed.  */
10023   if (lh->file_names)
10024     xfree (lh->file_names);
10025
10026   /* Similarly for the include directory names.  */
10027   if (lh->include_dirs)
10028     xfree (lh->include_dirs);
10029
10030   xfree (lh);
10031 }
10032
10033 /* Add an entry to LH's include directory table.  */
10034
10035 static void
10036 add_include_dir (struct line_header *lh, char *include_dir)
10037 {
10038   /* Grow the array if necessary.  */
10039   if (lh->include_dirs_size == 0)
10040     {
10041       lh->include_dirs_size = 1; /* for testing */
10042       lh->include_dirs = xmalloc (lh->include_dirs_size
10043                                   * sizeof (*lh->include_dirs));
10044     }
10045   else if (lh->num_include_dirs >= lh->include_dirs_size)
10046     {
10047       lh->include_dirs_size *= 2;
10048       lh->include_dirs = xrealloc (lh->include_dirs,
10049                                    (lh->include_dirs_size
10050                                     * sizeof (*lh->include_dirs)));
10051     }
10052
10053   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10054 }
10055
10056 /* Add an entry to LH's file name table.  */
10057
10058 static void
10059 add_file_name (struct line_header *lh,
10060                char *name,
10061                unsigned int dir_index,
10062                unsigned int mod_time,
10063                unsigned int length)
10064 {
10065   struct file_entry *fe;
10066
10067   /* Grow the array if necessary.  */
10068   if (lh->file_names_size == 0)
10069     {
10070       lh->file_names_size = 1; /* for testing */
10071       lh->file_names = xmalloc (lh->file_names_size
10072                                 * sizeof (*lh->file_names));
10073     }
10074   else if (lh->num_file_names >= lh->file_names_size)
10075     {
10076       lh->file_names_size *= 2;
10077       lh->file_names = xrealloc (lh->file_names,
10078                                  (lh->file_names_size
10079                                   * sizeof (*lh->file_names)));
10080     }
10081
10082   fe = &lh->file_names[lh->num_file_names++];
10083   fe->name = name;
10084   fe->dir_index = dir_index;
10085   fe->mod_time = mod_time;
10086   fe->length = length;
10087   fe->included_p = 0;
10088   fe->symtab = NULL;
10089 }
10090
10091 /* Read the statement program header starting at OFFSET in
10092    .debug_line, according to the endianness of ABFD.  Return a pointer
10093    to a struct line_header, allocated using xmalloc.
10094
10095    NOTE: the strings in the include directory and file name tables of
10096    the returned object point into debug_line_buffer, and must not be
10097    freed.  */
10098
10099 static struct line_header *
10100 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10101                           struct dwarf2_cu *cu)
10102 {
10103   struct cleanup *back_to;
10104   struct line_header *lh;
10105   gdb_byte *line_ptr;
10106   unsigned int bytes_read, offset_size;
10107   int i;
10108   char *cur_dir, *cur_file;
10109
10110   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10111   if (dwarf2_per_objfile->line.buffer == NULL)
10112     {
10113       complaint (&symfile_complaints, _("missing .debug_line section"));
10114       return 0;
10115     }
10116
10117   /* Make sure that at least there's room for the total_length field.
10118      That could be 12 bytes long, but we're just going to fudge that.  */
10119   if (offset + 4 >= dwarf2_per_objfile->line.size)
10120     {
10121       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10122       return 0;
10123     }
10124
10125   lh = xmalloc (sizeof (*lh));
10126   memset (lh, 0, sizeof (*lh));
10127   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10128                           (void *) lh);
10129
10130   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10131
10132   /* Read in the header.  */
10133   lh->total_length =
10134     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10135                                             &bytes_read, &offset_size);
10136   line_ptr += bytes_read;
10137   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10138                                      + dwarf2_per_objfile->line.size))
10139     {
10140       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10141       return 0;
10142     }
10143   lh->statement_program_end = line_ptr + lh->total_length;
10144   lh->version = read_2_bytes (abfd, line_ptr);
10145   line_ptr += 2;
10146   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10147   line_ptr += offset_size;
10148   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10149   line_ptr += 1;
10150   if (lh->version >= 4)
10151     {
10152       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10153       line_ptr += 1;
10154     }
10155   else
10156     lh->maximum_ops_per_instruction = 1;
10157
10158   if (lh->maximum_ops_per_instruction == 0)
10159     {
10160       lh->maximum_ops_per_instruction = 1;
10161       complaint (&symfile_complaints,
10162                  _("invalid maximum_ops_per_instruction "
10163                    "in `.debug_line' section"));
10164     }
10165
10166   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10167   line_ptr += 1;
10168   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10169   line_ptr += 1;
10170   lh->line_range = read_1_byte (abfd, line_ptr);
10171   line_ptr += 1;
10172   lh->opcode_base = read_1_byte (abfd, line_ptr);
10173   line_ptr += 1;
10174   lh->standard_opcode_lengths
10175     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10176
10177   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10178   for (i = 1; i < lh->opcode_base; ++i)
10179     {
10180       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10181       line_ptr += 1;
10182     }
10183
10184   /* Read directory table.  */
10185   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10186     {
10187       line_ptr += bytes_read;
10188       add_include_dir (lh, cur_dir);
10189     }
10190   line_ptr += bytes_read;
10191
10192   /* Read file name table.  */
10193   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10194     {
10195       unsigned int dir_index, mod_time, length;
10196
10197       line_ptr += bytes_read;
10198       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10199       line_ptr += bytes_read;
10200       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10201       line_ptr += bytes_read;
10202       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10203       line_ptr += bytes_read;
10204
10205       add_file_name (lh, cur_file, dir_index, mod_time, length);
10206     }
10207   line_ptr += bytes_read;
10208   lh->statement_program_start = line_ptr;
10209
10210   if (line_ptr > (dwarf2_per_objfile->line.buffer
10211                   + dwarf2_per_objfile->line.size))
10212     complaint (&symfile_complaints,
10213                _("line number info header doesn't "
10214                  "fit in `.debug_line' section"));
10215
10216   discard_cleanups (back_to);
10217   return lh;
10218 }
10219
10220 /* This function exists to work around a bug in certain compilers
10221    (particularly GCC 2.95), in which the first line number marker of a
10222    function does not show up until after the prologue, right before
10223    the second line number marker.  This function shifts ADDRESS down
10224    to the beginning of the function if necessary, and is called on
10225    addresses passed to record_line.  */
10226
10227 static CORE_ADDR
10228 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10229 {
10230   struct function_range *fn;
10231
10232   /* Find the function_range containing address.  */
10233   if (!cu->first_fn)
10234     return address;
10235
10236   if (!cu->cached_fn)
10237     cu->cached_fn = cu->first_fn;
10238
10239   fn = cu->cached_fn;
10240   while (fn)
10241     if (fn->lowpc <= address && fn->highpc > address)
10242       goto found;
10243     else
10244       fn = fn->next;
10245
10246   fn = cu->first_fn;
10247   while (fn && fn != cu->cached_fn)
10248     if (fn->lowpc <= address && fn->highpc > address)
10249       goto found;
10250     else
10251       fn = fn->next;
10252
10253   return address;
10254
10255  found:
10256   if (fn->seen_line)
10257     return address;
10258   if (address != fn->lowpc)
10259     complaint (&symfile_complaints,
10260                _("misplaced first line number at 0x%lx for '%s'"),
10261                (unsigned long) address, fn->name);
10262   fn->seen_line = 1;
10263   return fn->lowpc;
10264 }
10265
10266 /* Subroutine of dwarf_decode_lines to simplify it.
10267    Return the file name of the psymtab for included file FILE_INDEX
10268    in line header LH of PST.
10269    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10270    If space for the result is malloc'd, it will be freed by a cleanup.
10271    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10272
10273 static char *
10274 psymtab_include_file_name (const struct line_header *lh, int file_index,
10275                            const struct partial_symtab *pst,
10276                            const char *comp_dir)
10277 {
10278   const struct file_entry fe = lh->file_names [file_index];
10279   char *include_name = fe.name;
10280   char *include_name_to_compare = include_name;
10281   char *dir_name = NULL;
10282   const char *pst_filename;
10283   char *copied_name = NULL;
10284   int file_is_pst;
10285
10286   if (fe.dir_index)
10287     dir_name = lh->include_dirs[fe.dir_index - 1];
10288
10289   if (!IS_ABSOLUTE_PATH (include_name)
10290       && (dir_name != NULL || comp_dir != NULL))
10291     {
10292       /* Avoid creating a duplicate psymtab for PST.
10293          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10294          Before we do the comparison, however, we need to account
10295          for DIR_NAME and COMP_DIR.
10296          First prepend dir_name (if non-NULL).  If we still don't
10297          have an absolute path prepend comp_dir (if non-NULL).
10298          However, the directory we record in the include-file's
10299          psymtab does not contain COMP_DIR (to match the
10300          corresponding symtab(s)).
10301
10302          Example:
10303
10304          bash$ cd /tmp
10305          bash$ gcc -g ./hello.c
10306          include_name = "hello.c"
10307          dir_name = "."
10308          DW_AT_comp_dir = comp_dir = "/tmp"
10309          DW_AT_name = "./hello.c"  */
10310
10311       if (dir_name != NULL)
10312         {
10313           include_name = concat (dir_name, SLASH_STRING,
10314                                  include_name, (char *)NULL);
10315           include_name_to_compare = include_name;
10316           make_cleanup (xfree, include_name);
10317         }
10318       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10319         {
10320           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10321                                             include_name, (char *)NULL);
10322         }
10323     }
10324
10325   pst_filename = pst->filename;
10326   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10327     {
10328       copied_name = concat (pst->dirname, SLASH_STRING,
10329                             pst_filename, (char *)NULL);
10330       pst_filename = copied_name;
10331     }
10332
10333   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10334
10335   if (include_name_to_compare != include_name)
10336     xfree (include_name_to_compare);
10337   if (copied_name != NULL)
10338     xfree (copied_name);
10339
10340   if (file_is_pst)
10341     return NULL;
10342   return include_name;
10343 }
10344
10345 /* Decode the Line Number Program (LNP) for the given line_header
10346    structure and CU.  The actual information extracted and the type
10347    of structures created from the LNP depends on the value of PST.
10348
10349    1. If PST is NULL, then this procedure uses the data from the program
10350       to create all necessary symbol tables, and their linetables.
10351
10352    2. If PST is not NULL, this procedure reads the program to determine
10353       the list of files included by the unit represented by PST, and
10354       builds all the associated partial symbol tables.
10355
10356    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10357    It is used for relative paths in the line table.
10358    NOTE: When processing partial symtabs (pst != NULL),
10359    comp_dir == pst->dirname.
10360
10361    NOTE: It is important that psymtabs have the same file name (via strcmp)
10362    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10363    symtab we don't use it in the name of the psymtabs we create.
10364    E.g. expand_line_sal requires this when finding psymtabs to expand.
10365    A good testcase for this is mb-inline.exp.  */
10366
10367 static void
10368 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10369                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10370 {
10371   gdb_byte *line_ptr, *extended_end;
10372   gdb_byte *line_end;
10373   unsigned int bytes_read, extended_len;
10374   unsigned char op_code, extended_op, adj_opcode;
10375   CORE_ADDR baseaddr;
10376   struct objfile *objfile = cu->objfile;
10377   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10378   const int decode_for_pst_p = (pst != NULL);
10379   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10380
10381   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10382
10383   line_ptr = lh->statement_program_start;
10384   line_end = lh->statement_program_end;
10385
10386   /* Read the statement sequences until there's nothing left.  */
10387   while (line_ptr < line_end)
10388     {
10389       /* state machine registers  */
10390       CORE_ADDR address = 0;
10391       unsigned int file = 1;
10392       unsigned int line = 1;
10393       unsigned int column = 0;
10394       int is_stmt = lh->default_is_stmt;
10395       int basic_block = 0;
10396       int end_sequence = 0;
10397       CORE_ADDR addr;
10398       unsigned char op_index = 0;
10399
10400       if (!decode_for_pst_p && lh->num_file_names >= file)
10401         {
10402           /* Start a subfile for the current file of the state machine.  */
10403           /* lh->include_dirs and lh->file_names are 0-based, but the
10404              directory and file name numbers in the statement program
10405              are 1-based.  */
10406           struct file_entry *fe = &lh->file_names[file - 1];
10407           char *dir = NULL;
10408
10409           if (fe->dir_index)
10410             dir = lh->include_dirs[fe->dir_index - 1];
10411
10412           dwarf2_start_subfile (fe->name, dir, comp_dir);
10413         }
10414
10415       /* Decode the table.  */
10416       while (!end_sequence)
10417         {
10418           op_code = read_1_byte (abfd, line_ptr);
10419           line_ptr += 1;
10420           if (line_ptr > line_end)
10421             {
10422               dwarf2_debug_line_missing_end_sequence_complaint ();
10423               break;
10424             }
10425
10426           if (op_code >= lh->opcode_base)
10427             {
10428               /* Special operand.  */
10429               adj_opcode = op_code - lh->opcode_base;
10430               address += (((op_index + (adj_opcode / lh->line_range))
10431                            / lh->maximum_ops_per_instruction)
10432                           * lh->minimum_instruction_length);
10433               op_index = ((op_index + (adj_opcode / lh->line_range))
10434                           % lh->maximum_ops_per_instruction);
10435               line += lh->line_base + (adj_opcode % lh->line_range);
10436               if (lh->num_file_names < file || file == 0)
10437                 dwarf2_debug_line_missing_file_complaint ();
10438               /* For now we ignore lines not starting on an
10439                  instruction boundary.  */
10440               else if (op_index == 0)
10441                 {
10442                   lh->file_names[file - 1].included_p = 1;
10443                   if (!decode_for_pst_p && is_stmt)
10444                     {
10445                       if (last_subfile != current_subfile)
10446                         {
10447                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10448                           if (last_subfile)
10449                             record_line (last_subfile, 0, addr);
10450                           last_subfile = current_subfile;
10451                         }
10452                       /* Append row to matrix using current values.  */
10453                       addr = check_cu_functions (address, cu);
10454                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10455                       record_line (current_subfile, line, addr);
10456                     }
10457                 }
10458               basic_block = 0;
10459             }
10460           else switch (op_code)
10461             {
10462             case DW_LNS_extended_op:
10463               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10464                                                    &bytes_read);
10465               line_ptr += bytes_read;
10466               extended_end = line_ptr + extended_len;
10467               extended_op = read_1_byte (abfd, line_ptr);
10468               line_ptr += 1;
10469               switch (extended_op)
10470                 {
10471                 case DW_LNE_end_sequence:
10472                   end_sequence = 1;
10473                   break;
10474                 case DW_LNE_set_address:
10475                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10476                   op_index = 0;
10477                   line_ptr += bytes_read;
10478                   address += baseaddr;
10479                   break;
10480                 case DW_LNE_define_file:
10481                   {
10482                     char *cur_file;
10483                     unsigned int dir_index, mod_time, length;
10484
10485                     cur_file = read_direct_string (abfd, line_ptr,
10486                                                    &bytes_read);
10487                     line_ptr += bytes_read;
10488                     dir_index =
10489                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10490                     line_ptr += bytes_read;
10491                     mod_time =
10492                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10493                     line_ptr += bytes_read;
10494                     length =
10495                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10496                     line_ptr += bytes_read;
10497                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10498                   }
10499                   break;
10500                 case DW_LNE_set_discriminator:
10501                   /* The discriminator is not interesting to the debugger;
10502                      just ignore it.  */
10503                   line_ptr = extended_end;
10504                   break;
10505                 default:
10506                   complaint (&symfile_complaints,
10507                              _("mangled .debug_line section"));
10508                   return;
10509                 }
10510               /* Make sure that we parsed the extended op correctly.  If e.g.
10511                  we expected a different address size than the producer used,
10512                  we may have read the wrong number of bytes.  */
10513               if (line_ptr != extended_end)
10514                 {
10515                   complaint (&symfile_complaints,
10516                              _("mangled .debug_line section"));
10517                   return;
10518                 }
10519               break;
10520             case DW_LNS_copy:
10521               if (lh->num_file_names < file || file == 0)
10522                 dwarf2_debug_line_missing_file_complaint ();
10523               else
10524                 {
10525                   lh->file_names[file - 1].included_p = 1;
10526                   if (!decode_for_pst_p && is_stmt)
10527                     {
10528                       if (last_subfile != current_subfile)
10529                         {
10530                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10531                           if (last_subfile)
10532                             record_line (last_subfile, 0, addr);
10533                           last_subfile = current_subfile;
10534                         }
10535                       addr = check_cu_functions (address, cu);
10536                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10537                       record_line (current_subfile, line, addr);
10538                     }
10539                 }
10540               basic_block = 0;
10541               break;
10542             case DW_LNS_advance_pc:
10543               {
10544                 CORE_ADDR adjust
10545                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10546
10547                 address += (((op_index + adjust)
10548                              / lh->maximum_ops_per_instruction)
10549                             * lh->minimum_instruction_length);
10550                 op_index = ((op_index + adjust)
10551                             % lh->maximum_ops_per_instruction);
10552                 line_ptr += bytes_read;
10553               }
10554               break;
10555             case DW_LNS_advance_line:
10556               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10557               line_ptr += bytes_read;
10558               break;
10559             case DW_LNS_set_file:
10560               {
10561                 /* The arrays lh->include_dirs and lh->file_names are
10562                    0-based, but the directory and file name numbers in
10563                    the statement program are 1-based.  */
10564                 struct file_entry *fe;
10565                 char *dir = NULL;
10566
10567                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10568                 line_ptr += bytes_read;
10569                 if (lh->num_file_names < file || file == 0)
10570                   dwarf2_debug_line_missing_file_complaint ();
10571                 else
10572                   {
10573                     fe = &lh->file_names[file - 1];
10574                     if (fe->dir_index)
10575                       dir = lh->include_dirs[fe->dir_index - 1];
10576                     if (!decode_for_pst_p)
10577                       {
10578                         last_subfile = current_subfile;
10579                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10580                       }
10581                   }
10582               }
10583               break;
10584             case DW_LNS_set_column:
10585               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10586               line_ptr += bytes_read;
10587               break;
10588             case DW_LNS_negate_stmt:
10589               is_stmt = (!is_stmt);
10590               break;
10591             case DW_LNS_set_basic_block:
10592               basic_block = 1;
10593               break;
10594             /* Add to the address register of the state machine the
10595                address increment value corresponding to special opcode
10596                255.  I.e., this value is scaled by the minimum
10597                instruction length since special opcode 255 would have
10598                scaled the increment.  */
10599             case DW_LNS_const_add_pc:
10600               {
10601                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10602
10603                 address += (((op_index + adjust)
10604                              / lh->maximum_ops_per_instruction)
10605                             * lh->minimum_instruction_length);
10606                 op_index = ((op_index + adjust)
10607                             % lh->maximum_ops_per_instruction);
10608               }
10609               break;
10610             case DW_LNS_fixed_advance_pc:
10611               address += read_2_bytes (abfd, line_ptr);
10612               op_index = 0;
10613               line_ptr += 2;
10614               break;
10615             default:
10616               {
10617                 /* Unknown standard opcode, ignore it.  */
10618                 int i;
10619
10620                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10621                   {
10622                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10623                     line_ptr += bytes_read;
10624                   }
10625               }
10626             }
10627         }
10628       if (lh->num_file_names < file || file == 0)
10629         dwarf2_debug_line_missing_file_complaint ();
10630       else
10631         {
10632           lh->file_names[file - 1].included_p = 1;
10633           if (!decode_for_pst_p)
10634             {
10635               addr = gdbarch_addr_bits_remove (gdbarch, address);
10636               record_line (current_subfile, 0, addr);
10637             }
10638         }
10639     }
10640
10641   if (decode_for_pst_p)
10642     {
10643       int file_index;
10644
10645       /* Now that we're done scanning the Line Header Program, we can
10646          create the psymtab of each included file.  */
10647       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10648         if (lh->file_names[file_index].included_p == 1)
10649           {
10650             char *include_name =
10651               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10652             if (include_name != NULL)
10653               dwarf2_create_include_psymtab (include_name, pst, objfile);
10654           }
10655     }
10656   else
10657     {
10658       /* Make sure a symtab is created for every file, even files
10659          which contain only variables (i.e. no code with associated
10660          line numbers).  */
10661
10662       int i;
10663       struct file_entry *fe;
10664
10665       for (i = 0; i < lh->num_file_names; i++)
10666         {
10667           char *dir = NULL;
10668
10669           fe = &lh->file_names[i];
10670           if (fe->dir_index)
10671             dir = lh->include_dirs[fe->dir_index - 1];
10672           dwarf2_start_subfile (fe->name, dir, comp_dir);
10673
10674           /* Skip the main file; we don't need it, and it must be
10675              allocated last, so that it will show up before the
10676              non-primary symtabs in the objfile's symtab list.  */
10677           if (current_subfile == first_subfile)
10678             continue;
10679
10680           if (current_subfile->symtab == NULL)
10681             current_subfile->symtab = allocate_symtab (current_subfile->name,
10682                                                        cu->objfile);
10683           fe->symtab = current_subfile->symtab;
10684         }
10685     }
10686 }
10687
10688 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10689    DIRNAME the name of the source directory which contains FILENAME
10690    or NULL if not known.  COMP_DIR is the compilation directory for the
10691    linetable's compilation unit or NULL if not known.
10692    This routine tries to keep line numbers from identical absolute and
10693    relative file names in a common subfile.
10694
10695    Using the `list' example from the GDB testsuite, which resides in
10696    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10697    of /srcdir/list0.c yields the following debugging information for list0.c:
10698
10699    DW_AT_name:          /srcdir/list0.c
10700    DW_AT_comp_dir:              /compdir
10701    files.files[0].name: list0.h
10702    files.files[0].dir:  /srcdir
10703    files.files[1].name: list0.c
10704    files.files[1].dir:  /srcdir
10705
10706    The line number information for list0.c has to end up in a single
10707    subfile, so that `break /srcdir/list0.c:1' works as expected.
10708    start_subfile will ensure that this happens provided that we pass the
10709    concatenation of files.files[1].dir and files.files[1].name as the
10710    subfile's name.  */
10711
10712 static void
10713 dwarf2_start_subfile (char *filename, const char *dirname,
10714                       const char *comp_dir)
10715 {
10716   char *fullname;
10717
10718   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10719      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10720      second argument to start_subfile.  To be consistent, we do the
10721      same here.  In order not to lose the line information directory,
10722      we concatenate it to the filename when it makes sense.
10723      Note that the Dwarf3 standard says (speaking of filenames in line
10724      information): ``The directory index is ignored for file names
10725      that represent full path names''.  Thus ignoring dirname in the
10726      `else' branch below isn't an issue.  */
10727
10728   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10729     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10730   else
10731     fullname = filename;
10732
10733   start_subfile (fullname, comp_dir);
10734
10735   if (fullname != filename)
10736     xfree (fullname);
10737 }
10738
10739 static void
10740 var_decode_location (struct attribute *attr, struct symbol *sym,
10741                      struct dwarf2_cu *cu)
10742 {
10743   struct objfile *objfile = cu->objfile;
10744   struct comp_unit_head *cu_header = &cu->header;
10745
10746   /* NOTE drow/2003-01-30: There used to be a comment and some special
10747      code here to turn a symbol with DW_AT_external and a
10748      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10749      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10750      with some versions of binutils) where shared libraries could have
10751      relocations against symbols in their debug information - the
10752      minimal symbol would have the right address, but the debug info
10753      would not.  It's no longer necessary, because we will explicitly
10754      apply relocations when we read in the debug information now.  */
10755
10756   /* A DW_AT_location attribute with no contents indicates that a
10757      variable has been optimized away.  */
10758   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10759     {
10760       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10761       return;
10762     }
10763
10764   /* Handle one degenerate form of location expression specially, to
10765      preserve GDB's previous behavior when section offsets are
10766      specified.  If this is just a DW_OP_addr then mark this symbol
10767      as LOC_STATIC.  */
10768
10769   if (attr_form_is_block (attr)
10770       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10771       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10772     {
10773       unsigned int dummy;
10774
10775       SYMBOL_VALUE_ADDRESS (sym) =
10776         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10777       SYMBOL_CLASS (sym) = LOC_STATIC;
10778       fixup_symbol_section (sym, objfile);
10779       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10780                                               SYMBOL_SECTION (sym));
10781       return;
10782     }
10783
10784   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10785      expression evaluator, and use LOC_COMPUTED only when necessary
10786      (i.e. when the value of a register or memory location is
10787      referenced, or a thread-local block, etc.).  Then again, it might
10788      not be worthwhile.  I'm assuming that it isn't unless performance
10789      or memory numbers show me otherwise.  */
10790
10791   dwarf2_symbol_mark_computed (attr, sym, cu);
10792   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10793 }
10794
10795 /* Given a pointer to a DWARF information entry, figure out if we need
10796    to make a symbol table entry for it, and if so, create a new entry
10797    and return a pointer to it.
10798    If TYPE is NULL, determine symbol type from the die, otherwise
10799    used the passed type.
10800    If SPACE is not NULL, use it to hold the new symbol.  If it is
10801    NULL, allocate a new symbol on the objfile's obstack.  */
10802
10803 static struct symbol *
10804 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10805                  struct symbol *space)
10806 {
10807   struct objfile *objfile = cu->objfile;
10808   struct symbol *sym = NULL;
10809   char *name;
10810   struct attribute *attr = NULL;
10811   struct attribute *attr2 = NULL;
10812   CORE_ADDR baseaddr;
10813   struct pending **list_to_add = NULL;
10814
10815   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10816
10817   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10818
10819   name = dwarf2_name (die, cu);
10820   if (name)
10821     {
10822       const char *linkagename;
10823       int suppress_add = 0;
10824
10825       if (space)
10826         sym = space;
10827       else
10828         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10829       OBJSTAT (objfile, n_syms++);
10830
10831       /* Cache this symbol's name and the name's demangled form (if any).  */
10832       SYMBOL_SET_LANGUAGE (sym, cu->language);
10833       linkagename = dwarf2_physname (name, die, cu);
10834       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10835
10836       /* Fortran does not have mangling standard and the mangling does differ
10837          between gfortran, iFort etc.  */
10838       if (cu->language == language_fortran
10839           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10840         symbol_set_demangled_name (&(sym->ginfo),
10841                                    (char *) dwarf2_full_name (name, die, cu),
10842                                    NULL);
10843
10844       /* Default assumptions.
10845          Use the passed type or decode it from the die.  */
10846       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10847       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10848       if (type != NULL)
10849         SYMBOL_TYPE (sym) = type;
10850       else
10851         SYMBOL_TYPE (sym) = die_type (die, cu);
10852       attr = dwarf2_attr (die,
10853                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10854                           cu);
10855       if (attr)
10856         {
10857           SYMBOL_LINE (sym) = DW_UNSND (attr);
10858         }
10859
10860       attr = dwarf2_attr (die,
10861                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10862                           cu);
10863       if (attr)
10864         {
10865           int file_index = DW_UNSND (attr);
10866
10867           if (cu->line_header == NULL
10868               || file_index > cu->line_header->num_file_names)
10869             complaint (&symfile_complaints,
10870                        _("file index out of range"));
10871           else if (file_index > 0)
10872             {
10873               struct file_entry *fe;
10874
10875               fe = &cu->line_header->file_names[file_index - 1];
10876               SYMBOL_SYMTAB (sym) = fe->symtab;
10877             }
10878         }
10879
10880       switch (die->tag)
10881         {
10882         case DW_TAG_label:
10883           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10884           if (attr)
10885             {
10886               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10887             }
10888           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10889           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10890           SYMBOL_CLASS (sym) = LOC_LABEL;
10891           add_symbol_to_list (sym, cu->list_in_scope);
10892           break;
10893         case DW_TAG_subprogram:
10894           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10895              finish_block.  */
10896           SYMBOL_CLASS (sym) = LOC_BLOCK;
10897           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10898           if ((attr2 && (DW_UNSND (attr2) != 0))
10899               || cu->language == language_ada)
10900             {
10901               /* Subprograms marked external are stored as a global symbol.
10902                  Ada subprograms, whether marked external or not, are always
10903                  stored as a global symbol, because we want to be able to
10904                  access them globally.  For instance, we want to be able
10905                  to break on a nested subprogram without having to
10906                  specify the context.  */
10907               list_to_add = &global_symbols;
10908             }
10909           else
10910             {
10911               list_to_add = cu->list_in_scope;
10912             }
10913           break;
10914         case DW_TAG_inlined_subroutine:
10915           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10916              finish_block.  */
10917           SYMBOL_CLASS (sym) = LOC_BLOCK;
10918           SYMBOL_INLINED (sym) = 1;
10919           /* Do not add the symbol to any lists.  It will be found via
10920              BLOCK_FUNCTION from the blockvector.  */
10921           break;
10922         case DW_TAG_template_value_param:
10923           suppress_add = 1;
10924           /* Fall through.  */
10925         case DW_TAG_constant:
10926         case DW_TAG_variable:
10927         case DW_TAG_member:
10928           /* Compilation with minimal debug info may result in
10929              variables with missing type entries.  Change the
10930              misleading `void' type to something sensible.  */
10931           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10932             SYMBOL_TYPE (sym)
10933               = objfile_type (objfile)->nodebug_data_symbol;
10934
10935           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10936           /* In the case of DW_TAG_member, we should only be called for
10937              static const members.  */
10938           if (die->tag == DW_TAG_member)
10939             {
10940               /* dwarf2_add_field uses die_is_declaration,
10941                  so we do the same.  */
10942               gdb_assert (die_is_declaration (die, cu));
10943               gdb_assert (attr);
10944             }
10945           if (attr)
10946             {
10947               dwarf2_const_value (attr, sym, cu);
10948               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10949               if (!suppress_add)
10950                 {
10951                   if (attr2 && (DW_UNSND (attr2) != 0))
10952                     list_to_add = &global_symbols;
10953                   else
10954                     list_to_add = cu->list_in_scope;
10955                 }
10956               break;
10957             }
10958           attr = dwarf2_attr (die, DW_AT_location, cu);
10959           if (attr)
10960             {
10961               var_decode_location (attr, sym, cu);
10962               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10963               if (SYMBOL_CLASS (sym) == LOC_STATIC
10964                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10965                   && !dwarf2_per_objfile->has_section_at_zero)
10966                 {
10967                   /* When a static variable is eliminated by the linker,
10968                      the corresponding debug information is not stripped
10969                      out, but the variable address is set to null;
10970                      do not add such variables into symbol table.  */
10971                 }
10972               else if (attr2 && (DW_UNSND (attr2) != 0))
10973                 {
10974                   /* Workaround gfortran PR debug/40040 - it uses
10975                      DW_AT_location for variables in -fPIC libraries which may
10976                      get overriden by other libraries/executable and get
10977                      a different address.  Resolve it by the minimal symbol
10978                      which may come from inferior's executable using copy
10979                      relocation.  Make this workaround only for gfortran as for
10980                      other compilers GDB cannot guess the minimal symbol
10981                      Fortran mangling kind.  */
10982                   if (cu->language == language_fortran && die->parent
10983                       && die->parent->tag == DW_TAG_module
10984                       && cu->producer
10985                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10986                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10987
10988                   /* A variable with DW_AT_external is never static,
10989                      but it may be block-scoped.  */
10990                   list_to_add = (cu->list_in_scope == &file_symbols
10991                                  ? &global_symbols : cu->list_in_scope);
10992                 }
10993               else
10994                 list_to_add = cu->list_in_scope;
10995             }
10996           else
10997             {
10998               /* We do not know the address of this symbol.
10999                  If it is an external symbol and we have type information
11000                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11001                  The address of the variable will then be determined from
11002                  the minimal symbol table whenever the variable is
11003                  referenced.  */
11004               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11005               if (attr2 && (DW_UNSND (attr2) != 0)
11006                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11007                 {
11008                   /* A variable with DW_AT_external is never static, but it
11009                      may be block-scoped.  */
11010                   list_to_add = (cu->list_in_scope == &file_symbols
11011                                  ? &global_symbols : cu->list_in_scope);
11012
11013                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11014                 }
11015               else if (!die_is_declaration (die, cu))
11016                 {
11017                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11018                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11019                   if (!suppress_add)
11020                     list_to_add = cu->list_in_scope;
11021                 }
11022             }
11023           break;
11024         case DW_TAG_formal_parameter:
11025           /* If we are inside a function, mark this as an argument.  If
11026              not, we might be looking at an argument to an inlined function
11027              when we do not have enough information to show inlined frames;
11028              pretend it's a local variable in that case so that the user can
11029              still see it.  */
11030           if (context_stack_depth > 0
11031               && context_stack[context_stack_depth - 1].name != NULL)
11032             SYMBOL_IS_ARGUMENT (sym) = 1;
11033           attr = dwarf2_attr (die, DW_AT_location, cu);
11034           if (attr)
11035             {
11036               var_decode_location (attr, sym, cu);
11037             }
11038           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11039           if (attr)
11040             {
11041               dwarf2_const_value (attr, sym, cu);
11042             }
11043           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11044           if (attr && DW_UNSND (attr))
11045             {
11046               struct type *ref_type;
11047
11048               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11049               SYMBOL_TYPE (sym) = ref_type;
11050             }
11051
11052           list_to_add = cu->list_in_scope;
11053           break;
11054         case DW_TAG_unspecified_parameters:
11055           /* From varargs functions; gdb doesn't seem to have any
11056              interest in this information, so just ignore it for now.
11057              (FIXME?) */
11058           break;
11059         case DW_TAG_template_type_param:
11060           suppress_add = 1;
11061           /* Fall through.  */
11062         case DW_TAG_class_type:
11063         case DW_TAG_interface_type:
11064         case DW_TAG_structure_type:
11065         case DW_TAG_union_type:
11066         case DW_TAG_set_type:
11067         case DW_TAG_enumeration_type:
11068           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11069           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11070
11071           {
11072             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11073                really ever be static objects: otherwise, if you try
11074                to, say, break of a class's method and you're in a file
11075                which doesn't mention that class, it won't work unless
11076                the check for all static symbols in lookup_symbol_aux
11077                saves you.  See the OtherFileClass tests in
11078                gdb.c++/namespace.exp.  */
11079
11080             if (!suppress_add)
11081               {
11082                 list_to_add = (cu->list_in_scope == &file_symbols
11083                                && (cu->language == language_cplus
11084                                    || cu->language == language_java)
11085                                ? &global_symbols : cu->list_in_scope);
11086
11087                 /* The semantics of C++ state that "struct foo {
11088                    ... }" also defines a typedef for "foo".  A Java
11089                    class declaration also defines a typedef for the
11090                    class.  */
11091                 if (cu->language == language_cplus
11092                     || cu->language == language_java
11093                     || cu->language == language_ada)
11094                   {
11095                     /* The symbol's name is already allocated along
11096                        with this objfile, so we don't need to
11097                        duplicate it for the type.  */
11098                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11099                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11100                   }
11101               }
11102           }
11103           break;
11104         case DW_TAG_typedef:
11105           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11106           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11107           list_to_add = cu->list_in_scope;
11108           break;
11109         case DW_TAG_base_type:
11110         case DW_TAG_subrange_type:
11111           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11112           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11113           list_to_add = cu->list_in_scope;
11114           break;
11115         case DW_TAG_enumerator:
11116           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11117           if (attr)
11118             {
11119               dwarf2_const_value (attr, sym, cu);
11120             }
11121           {
11122             /* NOTE: carlton/2003-11-10: See comment above in the
11123                DW_TAG_class_type, etc. block.  */
11124
11125             list_to_add = (cu->list_in_scope == &file_symbols
11126                            && (cu->language == language_cplus
11127                                || cu->language == language_java)
11128                            ? &global_symbols : cu->list_in_scope);
11129           }
11130           break;
11131         case DW_TAG_namespace:
11132           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11133           list_to_add = &global_symbols;
11134           break;
11135         default:
11136           /* Not a tag we recognize.  Hopefully we aren't processing
11137              trash data, but since we must specifically ignore things
11138              we don't recognize, there is nothing else we should do at
11139              this point.  */
11140           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11141                      dwarf_tag_name (die->tag));
11142           break;
11143         }
11144
11145       if (suppress_add)
11146         {
11147           sym->hash_next = objfile->template_symbols;
11148           objfile->template_symbols = sym;
11149           list_to_add = NULL;
11150         }
11151
11152       if (list_to_add != NULL)
11153         add_symbol_to_list (sym, list_to_add);
11154
11155       /* For the benefit of old versions of GCC, check for anonymous
11156          namespaces based on the demangled name.  */
11157       if (!processing_has_namespace_info
11158           && cu->language == language_cplus)
11159         cp_scan_for_anonymous_namespaces (sym);
11160     }
11161   return (sym);
11162 }
11163
11164 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11165
11166 static struct symbol *
11167 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11168 {
11169   return new_symbol_full (die, type, cu, NULL);
11170 }
11171
11172 /* Given an attr with a DW_FORM_dataN value in host byte order,
11173    zero-extend it as appropriate for the symbol's type.  The DWARF
11174    standard (v4) is not entirely clear about the meaning of using
11175    DW_FORM_dataN for a constant with a signed type, where the type is
11176    wider than the data.  The conclusion of a discussion on the DWARF
11177    list was that this is unspecified.  We choose to always zero-extend
11178    because that is the interpretation long in use by GCC.  */
11179
11180 static gdb_byte *
11181 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11182                          const char *name, struct obstack *obstack,
11183                          struct dwarf2_cu *cu, long *value, int bits)
11184 {
11185   struct objfile *objfile = cu->objfile;
11186   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11187                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11188   LONGEST l = DW_UNSND (attr);
11189
11190   if (bits < sizeof (*value) * 8)
11191     {
11192       l &= ((LONGEST) 1 << bits) - 1;
11193       *value = l;
11194     }
11195   else if (bits == sizeof (*value) * 8)
11196     *value = l;
11197   else
11198     {
11199       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11200       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11201       return bytes;
11202     }
11203
11204   return NULL;
11205 }
11206
11207 /* Read a constant value from an attribute.  Either set *VALUE, or if
11208    the value does not fit in *VALUE, set *BYTES - either already
11209    allocated on the objfile obstack, or newly allocated on OBSTACK,
11210    or, set *BATON, if we translated the constant to a location
11211    expression.  */
11212
11213 static void
11214 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11215                          const char *name, struct obstack *obstack,
11216                          struct dwarf2_cu *cu,
11217                          long *value, gdb_byte **bytes,
11218                          struct dwarf2_locexpr_baton **baton)
11219 {
11220   struct objfile *objfile = cu->objfile;
11221   struct comp_unit_head *cu_header = &cu->header;
11222   struct dwarf_block *blk;
11223   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11224                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11225
11226   *value = 0;
11227   *bytes = NULL;
11228   *baton = NULL;
11229
11230   switch (attr->form)
11231     {
11232     case DW_FORM_addr:
11233       {
11234         gdb_byte *data;
11235
11236         if (TYPE_LENGTH (type) != cu_header->addr_size)
11237           dwarf2_const_value_length_mismatch_complaint (name,
11238                                                         cu_header->addr_size,
11239                                                         TYPE_LENGTH (type));
11240         /* Symbols of this form are reasonably rare, so we just
11241            piggyback on the existing location code rather than writing
11242            a new implementation of symbol_computed_ops.  */
11243         *baton = obstack_alloc (&objfile->objfile_obstack,
11244                                 sizeof (struct dwarf2_locexpr_baton));
11245         (*baton)->per_cu = cu->per_cu;
11246         gdb_assert ((*baton)->per_cu);
11247
11248         (*baton)->size = 2 + cu_header->addr_size;
11249         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11250         (*baton)->data = data;
11251
11252         data[0] = DW_OP_addr;
11253         store_unsigned_integer (&data[1], cu_header->addr_size,
11254                                 byte_order, DW_ADDR (attr));
11255         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11256       }
11257       break;
11258     case DW_FORM_string:
11259     case DW_FORM_strp:
11260       /* DW_STRING is already allocated on the objfile obstack, point
11261          directly to it.  */
11262       *bytes = (gdb_byte *) DW_STRING (attr);
11263       break;
11264     case DW_FORM_block1:
11265     case DW_FORM_block2:
11266     case DW_FORM_block4:
11267     case DW_FORM_block:
11268     case DW_FORM_exprloc:
11269       blk = DW_BLOCK (attr);
11270       if (TYPE_LENGTH (type) != blk->size)
11271         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11272                                                       TYPE_LENGTH (type));
11273       *bytes = blk->data;
11274       break;
11275
11276       /* The DW_AT_const_value attributes are supposed to carry the
11277          symbol's value "represented as it would be on the target
11278          architecture."  By the time we get here, it's already been
11279          converted to host endianness, so we just need to sign- or
11280          zero-extend it as appropriate.  */
11281     case DW_FORM_data1:
11282       *bytes = dwarf2_const_value_data (attr, type, name,
11283                                         obstack, cu, value, 8);
11284       break;
11285     case DW_FORM_data2:
11286       *bytes = dwarf2_const_value_data (attr, type, name,
11287                                         obstack, cu, value, 16);
11288       break;
11289     case DW_FORM_data4:
11290       *bytes = dwarf2_const_value_data (attr, type, name,
11291                                         obstack, cu, value, 32);
11292       break;
11293     case DW_FORM_data8:
11294       *bytes = dwarf2_const_value_data (attr, type, name,
11295                                         obstack, cu, value, 64);
11296       break;
11297
11298     case DW_FORM_sdata:
11299       *value = DW_SND (attr);
11300       break;
11301
11302     case DW_FORM_udata:
11303       *value = DW_UNSND (attr);
11304       break;
11305
11306     default:
11307       complaint (&symfile_complaints,
11308                  _("unsupported const value attribute form: '%s'"),
11309                  dwarf_form_name (attr->form));
11310       *value = 0;
11311       break;
11312     }
11313 }
11314
11315
11316 /* Copy constant value from an attribute to a symbol.  */
11317
11318 static void
11319 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11320                     struct dwarf2_cu *cu)
11321 {
11322   struct objfile *objfile = cu->objfile;
11323   struct comp_unit_head *cu_header = &cu->header;
11324   long value;
11325   gdb_byte *bytes;
11326   struct dwarf2_locexpr_baton *baton;
11327
11328   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11329                            SYMBOL_PRINT_NAME (sym),
11330                            &objfile->objfile_obstack, cu,
11331                            &value, &bytes, &baton);
11332
11333   if (baton != NULL)
11334     {
11335       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11336       SYMBOL_LOCATION_BATON (sym) = baton;
11337       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11338     }
11339   else if (bytes != NULL)
11340      {
11341       SYMBOL_VALUE_BYTES (sym) = bytes;
11342       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11343     }
11344   else
11345     {
11346       SYMBOL_VALUE (sym) = value;
11347       SYMBOL_CLASS (sym) = LOC_CONST;
11348     }
11349 }
11350
11351 /* Return the type of the die in question using its DW_AT_type attribute.  */
11352
11353 static struct type *
11354 die_type (struct die_info *die, struct dwarf2_cu *cu)
11355 {
11356   struct attribute *type_attr;
11357
11358   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11359   if (!type_attr)
11360     {
11361       /* A missing DW_AT_type represents a void type.  */
11362       return objfile_type (cu->objfile)->builtin_void;
11363     }
11364
11365   return lookup_die_type (die, type_attr, cu);
11366 }
11367
11368 /* True iff CU's producer generates GNAT Ada auxiliary information
11369    that allows to find parallel types through that information instead
11370    of having to do expensive parallel lookups by type name.  */
11371
11372 static int
11373 need_gnat_info (struct dwarf2_cu *cu)
11374 {
11375   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11376      of GNAT produces this auxiliary information, without any indication
11377      that it is produced.  Part of enhancing the FSF version of GNAT
11378      to produce that information will be to put in place an indicator
11379      that we can use in order to determine whether the descriptive type
11380      info is available or not.  One suggestion that has been made is
11381      to use a new attribute, attached to the CU die.  For now, assume
11382      that the descriptive type info is not available.  */
11383   return 0;
11384 }
11385
11386 /* Return the auxiliary type of the die in question using its
11387    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11388    attribute is not present.  */
11389
11390 static struct type *
11391 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11392 {
11393   struct attribute *type_attr;
11394
11395   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11396   if (!type_attr)
11397     return NULL;
11398
11399   return lookup_die_type (die, type_attr, cu);
11400 }
11401
11402 /* If DIE has a descriptive_type attribute, then set the TYPE's
11403    descriptive type accordingly.  */
11404
11405 static void
11406 set_descriptive_type (struct type *type, struct die_info *die,
11407                       struct dwarf2_cu *cu)
11408 {
11409   struct type *descriptive_type = die_descriptive_type (die, cu);
11410
11411   if (descriptive_type)
11412     {
11413       ALLOCATE_GNAT_AUX_TYPE (type);
11414       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11415     }
11416 }
11417
11418 /* Return the containing type of the die in question using its
11419    DW_AT_containing_type attribute.  */
11420
11421 static struct type *
11422 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11423 {
11424   struct attribute *type_attr;
11425
11426   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11427   if (!type_attr)
11428     error (_("Dwarf Error: Problem turning containing type into gdb type "
11429              "[in module %s]"), cu->objfile->name);
11430
11431   return lookup_die_type (die, type_attr, cu);
11432 }
11433
11434 /* Look up the type of DIE in CU using its type attribute ATTR.
11435    If there is no type substitute an error marker.  */
11436
11437 static struct type *
11438 lookup_die_type (struct die_info *die, struct attribute *attr,
11439                  struct dwarf2_cu *cu)
11440 {
11441   struct type *this_type;
11442
11443   /* First see if we have it cached.  */
11444
11445   if (is_ref_attr (attr))
11446     {
11447       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11448
11449       this_type = get_die_type_at_offset (offset, cu->per_cu);
11450     }
11451   else if (attr->form == DW_FORM_sig8)
11452     {
11453       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11454       struct dwarf2_cu *sig_cu;
11455       unsigned int offset;
11456
11457       /* sig_type will be NULL if the signatured type is missing from
11458          the debug info.  */
11459       if (sig_type == NULL)
11460         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11461                  "at 0x%x [in module %s]"),
11462                die->offset, cu->objfile->name);
11463
11464       gdb_assert (sig_type->per_cu.from_debug_types);
11465       offset = sig_type->offset + sig_type->type_offset;
11466       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11467     }
11468   else
11469     {
11470       dump_die_for_error (die);
11471       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11472              dwarf_attr_name (attr->name), cu->objfile->name);
11473     }
11474
11475   /* If not cached we need to read it in.  */
11476
11477   if (this_type == NULL)
11478     {
11479       struct die_info *type_die;
11480       struct dwarf2_cu *type_cu = cu;
11481
11482       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11483       /* If the type is cached, we should have found it above.  */
11484       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11485       this_type = read_type_die_1 (type_die, type_cu);
11486     }
11487
11488   /* If we still don't have a type use an error marker.  */
11489
11490   if (this_type == NULL)
11491     {
11492       char *message, *saved;
11493
11494       /* read_type_die already issued a complaint.  */
11495       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11496                             cu->objfile->name,
11497                             cu->header.offset,
11498                             die->offset);
11499       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11500                              message, strlen (message));
11501       xfree (message);
11502
11503       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11504     }
11505
11506   return this_type;
11507 }
11508
11509 /* Return the type in DIE, CU.
11510    Returns NULL for invalid types.
11511
11512    This first does a lookup in the appropriate type_hash table,
11513    and only reads the die in if necessary.
11514
11515    NOTE: This can be called when reading in partial or full symbols.  */
11516
11517 static struct type *
11518 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11519 {
11520   struct type *this_type;
11521
11522   this_type = get_die_type (die, cu);
11523   if (this_type)
11524     return this_type;
11525
11526   return read_type_die_1 (die, cu);
11527 }
11528
11529 /* Read the type in DIE, CU.
11530    Returns NULL for invalid types.  */
11531
11532 static struct type *
11533 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11534 {
11535   struct type *this_type = NULL;
11536
11537   switch (die->tag)
11538     {
11539     case DW_TAG_class_type:
11540     case DW_TAG_interface_type:
11541     case DW_TAG_structure_type:
11542     case DW_TAG_union_type:
11543       this_type = read_structure_type (die, cu);
11544       break;
11545     case DW_TAG_enumeration_type:
11546       this_type = read_enumeration_type (die, cu);
11547       break;
11548     case DW_TAG_subprogram:
11549     case DW_TAG_subroutine_type:
11550     case DW_TAG_inlined_subroutine:
11551       this_type = read_subroutine_type (die, cu);
11552       break;
11553     case DW_TAG_array_type:
11554       this_type = read_array_type (die, cu);
11555       break;
11556     case DW_TAG_set_type:
11557       this_type = read_set_type (die, cu);
11558       break;
11559     case DW_TAG_pointer_type:
11560       this_type = read_tag_pointer_type (die, cu);
11561       break;
11562     case DW_TAG_ptr_to_member_type:
11563       this_type = read_tag_ptr_to_member_type (die, cu);
11564       break;
11565     case DW_TAG_reference_type:
11566       this_type = read_tag_reference_type (die, cu);
11567       break;
11568     case DW_TAG_const_type:
11569       this_type = read_tag_const_type (die, cu);
11570       break;
11571     case DW_TAG_volatile_type:
11572       this_type = read_tag_volatile_type (die, cu);
11573       break;
11574     case DW_TAG_string_type:
11575       this_type = read_tag_string_type (die, cu);
11576       break;
11577     case DW_TAG_typedef:
11578       this_type = read_typedef (die, cu);
11579       break;
11580     case DW_TAG_subrange_type:
11581       this_type = read_subrange_type (die, cu);
11582       break;
11583     case DW_TAG_base_type:
11584       this_type = read_base_type (die, cu);
11585       break;
11586     case DW_TAG_unspecified_type:
11587       this_type = read_unspecified_type (die, cu);
11588       break;
11589     case DW_TAG_namespace:
11590       this_type = read_namespace_type (die, cu);
11591       break;
11592     case DW_TAG_module:
11593       this_type = read_module_type (die, cu);
11594       break;
11595     default:
11596       complaint (&symfile_complaints,
11597                  _("unexpected tag in read_type_die: '%s'"),
11598                  dwarf_tag_name (die->tag));
11599       break;
11600     }
11601
11602   return this_type;
11603 }
11604
11605 /* See if we can figure out if the class lives in a namespace.  We do
11606    this by looking for a member function; its demangled name will
11607    contain namespace info, if there is any.
11608    Return the computed name or NULL.
11609    Space for the result is allocated on the objfile's obstack.
11610    This is the full-die version of guess_partial_die_structure_name.
11611    In this case we know DIE has no useful parent.  */
11612
11613 static char *
11614 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11615 {
11616   struct die_info *spec_die;
11617   struct dwarf2_cu *spec_cu;
11618   struct die_info *child;
11619
11620   spec_cu = cu;
11621   spec_die = die_specification (die, &spec_cu);
11622   if (spec_die != NULL)
11623     {
11624       die = spec_die;
11625       cu = spec_cu;
11626     }
11627
11628   for (child = die->child;
11629        child != NULL;
11630        child = child->sibling)
11631     {
11632       if (child->tag == DW_TAG_subprogram)
11633         {
11634           struct attribute *attr;
11635
11636           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11637           if (attr == NULL)
11638             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11639           if (attr != NULL)
11640             {
11641               char *actual_name
11642                 = language_class_name_from_physname (cu->language_defn,
11643                                                      DW_STRING (attr));
11644               char *name = NULL;
11645
11646               if (actual_name != NULL)
11647                 {
11648                   char *die_name = dwarf2_name (die, cu);
11649
11650                   if (die_name != NULL
11651                       && strcmp (die_name, actual_name) != 0)
11652                     {
11653                       /* Strip off the class name from the full name.
11654                          We want the prefix.  */
11655                       int die_name_len = strlen (die_name);
11656                       int actual_name_len = strlen (actual_name);
11657
11658                       /* Test for '::' as a sanity check.  */
11659                       if (actual_name_len > die_name_len + 2
11660                           && actual_name[actual_name_len
11661                                          - die_name_len - 1] == ':')
11662                         name =
11663                           obsavestring (actual_name,
11664                                         actual_name_len - die_name_len - 2,
11665                                         &cu->objfile->objfile_obstack);
11666                     }
11667                 }
11668               xfree (actual_name);
11669               return name;
11670             }
11671         }
11672     }
11673
11674   return NULL;
11675 }
11676
11677 /* Return the name of the namespace/class that DIE is defined within,
11678    or "" if we can't tell.  The caller should not xfree the result.
11679
11680    For example, if we're within the method foo() in the following
11681    code:
11682
11683    namespace N {
11684      class C {
11685        void foo () {
11686        }
11687      };
11688    }
11689
11690    then determine_prefix on foo's die will return "N::C".  */
11691
11692 static char *
11693 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11694 {
11695   struct die_info *parent, *spec_die;
11696   struct dwarf2_cu *spec_cu;
11697   struct type *parent_type;
11698
11699   if (cu->language != language_cplus && cu->language != language_java
11700       && cu->language != language_fortran)
11701     return "";
11702
11703   /* We have to be careful in the presence of DW_AT_specification.
11704      For example, with GCC 3.4, given the code
11705
11706      namespace N {
11707        void foo() {
11708          // Definition of N::foo.
11709        }
11710      }
11711
11712      then we'll have a tree of DIEs like this:
11713
11714      1: DW_TAG_compile_unit
11715        2: DW_TAG_namespace        // N
11716          3: DW_TAG_subprogram     // declaration of N::foo
11717        4: DW_TAG_subprogram       // definition of N::foo
11718             DW_AT_specification   // refers to die #3
11719
11720      Thus, when processing die #4, we have to pretend that we're in
11721      the context of its DW_AT_specification, namely the contex of die
11722      #3.  */
11723   spec_cu = cu;
11724   spec_die = die_specification (die, &spec_cu);
11725   if (spec_die == NULL)
11726     parent = die->parent;
11727   else
11728     {
11729       parent = spec_die->parent;
11730       cu = spec_cu;
11731     }
11732
11733   if (parent == NULL)
11734     return "";
11735   else if (parent->building_fullname)
11736     {
11737       const char *name;
11738       const char *parent_name;
11739
11740       /* It has been seen on RealView 2.2 built binaries,
11741          DW_TAG_template_type_param types actually _defined_ as
11742          children of the parent class:
11743
11744          enum E {};
11745          template class <class Enum> Class{};
11746          Class<enum E> class_e;
11747
11748          1: DW_TAG_class_type (Class)
11749            2: DW_TAG_enumeration_type (E)
11750              3: DW_TAG_enumerator (enum1:0)
11751              3: DW_TAG_enumerator (enum2:1)
11752              ...
11753            2: DW_TAG_template_type_param
11754               DW_AT_type  DW_FORM_ref_udata (E)
11755
11756          Besides being broken debug info, it can put GDB into an
11757          infinite loop.  Consider:
11758
11759          When we're building the full name for Class<E>, we'll start
11760          at Class, and go look over its template type parameters,
11761          finding E.  We'll then try to build the full name of E, and
11762          reach here.  We're now trying to build the full name of E,
11763          and look over the parent DIE for containing scope.  In the
11764          broken case, if we followed the parent DIE of E, we'd again
11765          find Class, and once again go look at its template type
11766          arguments, etc., etc.  Simply don't consider such parent die
11767          as source-level parent of this die (it can't be, the language
11768          doesn't allow it), and break the loop here.  */
11769       name = dwarf2_name (die, cu);
11770       parent_name = dwarf2_name (parent, cu);
11771       complaint (&symfile_complaints,
11772                  _("template param type '%s' defined within parent '%s'"),
11773                  name ? name : "<unknown>",
11774                  parent_name ? parent_name : "<unknown>");
11775       return "";
11776     }
11777   else
11778     switch (parent->tag)
11779       {
11780       case DW_TAG_namespace:
11781         parent_type = read_type_die (parent, cu);
11782         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11783            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11784            Work around this problem here.  */
11785         if (cu->language == language_cplus
11786             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11787           return "";
11788         /* We give a name to even anonymous namespaces.  */
11789         return TYPE_TAG_NAME (parent_type);
11790       case DW_TAG_class_type:
11791       case DW_TAG_interface_type:
11792       case DW_TAG_structure_type:
11793       case DW_TAG_union_type:
11794       case DW_TAG_module:
11795         parent_type = read_type_die (parent, cu);
11796         if (TYPE_TAG_NAME (parent_type) != NULL)
11797           return TYPE_TAG_NAME (parent_type);
11798         else
11799           /* An anonymous structure is only allowed non-static data
11800              members; no typedefs, no member functions, et cetera.
11801              So it does not need a prefix.  */
11802           return "";
11803       case DW_TAG_compile_unit:
11804         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
11805         if (cu->language == language_cplus
11806             && dwarf2_per_objfile->types.asection != NULL
11807             && die->child != NULL
11808             && (die->tag == DW_TAG_class_type
11809                 || die->tag == DW_TAG_structure_type
11810                 || die->tag == DW_TAG_union_type))
11811           {
11812             char *name = guess_full_die_structure_name (die, cu);
11813             if (name != NULL)
11814               return name;
11815           }
11816         return "";
11817       default:
11818         return determine_prefix (parent, cu);
11819       }
11820 }
11821
11822 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11823    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11824    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
11825    an obconcat, otherwise allocate storage for the result.  The CU argument is
11826    used to determine the language and hence, the appropriate separator.  */
11827
11828 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11829
11830 static char *
11831 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11832                  int physname, struct dwarf2_cu *cu)
11833 {
11834   const char *lead = "";
11835   const char *sep;
11836
11837   if (suffix == NULL || suffix[0] == '\0'
11838       || prefix == NULL || prefix[0] == '\0')
11839     sep = "";
11840   else if (cu->language == language_java)
11841     sep = ".";
11842   else if (cu->language == language_fortran && physname)
11843     {
11844       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11845          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11846
11847       lead = "__";
11848       sep = "_MOD_";
11849     }
11850   else
11851     sep = "::";
11852
11853   if (prefix == NULL)
11854     prefix = "";
11855   if (suffix == NULL)
11856     suffix = "";
11857
11858   if (obs == NULL)
11859     {
11860       char *retval
11861         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11862
11863       strcpy (retval, lead);
11864       strcat (retval, prefix);
11865       strcat (retval, sep);
11866       strcat (retval, suffix);
11867       return retval;
11868     }
11869   else
11870     {
11871       /* We have an obstack.  */
11872       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11873     }
11874 }
11875
11876 /* Return sibling of die, NULL if no sibling.  */
11877
11878 static struct die_info *
11879 sibling_die (struct die_info *die)
11880 {
11881   return die->sibling;
11882 }
11883
11884 /* Get name of a die, return NULL if not found.  */
11885
11886 static char *
11887 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11888                           struct obstack *obstack)
11889 {
11890   if (name && cu->language == language_cplus)
11891     {
11892       char *canon_name = cp_canonicalize_string (name);
11893
11894       if (canon_name != NULL)
11895         {
11896           if (strcmp (canon_name, name) != 0)
11897             name = obsavestring (canon_name, strlen (canon_name),
11898                                  obstack);
11899           xfree (canon_name);
11900         }
11901     }
11902
11903   return name;
11904 }
11905
11906 /* Get name of a die, return NULL if not found.  */
11907
11908 static char *
11909 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11910 {
11911   struct attribute *attr;
11912
11913   attr = dwarf2_attr (die, DW_AT_name, cu);
11914   if (!attr || !DW_STRING (attr))
11915     return NULL;
11916
11917   switch (die->tag)
11918     {
11919     case DW_TAG_compile_unit:
11920       /* Compilation units have a DW_AT_name that is a filename, not
11921          a source language identifier.  */
11922     case DW_TAG_enumeration_type:
11923     case DW_TAG_enumerator:
11924       /* These tags always have simple identifiers already; no need
11925          to canonicalize them.  */
11926       return DW_STRING (attr);
11927
11928     case DW_TAG_subprogram:
11929       /* Java constructors will all be named "<init>", so return
11930          the class name when we see this special case.  */
11931       if (cu->language == language_java
11932           && DW_STRING (attr) != NULL
11933           && strcmp (DW_STRING (attr), "<init>") == 0)
11934         {
11935           struct dwarf2_cu *spec_cu = cu;
11936           struct die_info *spec_die;
11937
11938           /* GCJ will output '<init>' for Java constructor names.
11939              For this special case, return the name of the parent class.  */
11940
11941           /* GCJ may output suprogram DIEs with AT_specification set.
11942              If so, use the name of the specified DIE.  */
11943           spec_die = die_specification (die, &spec_cu);
11944           if (spec_die != NULL)
11945             return dwarf2_name (spec_die, spec_cu);
11946
11947           do
11948             {
11949               die = die->parent;
11950               if (die->tag == DW_TAG_class_type)
11951                 return dwarf2_name (die, cu);
11952             }
11953           while (die->tag != DW_TAG_compile_unit);
11954         }
11955       break;
11956
11957     case DW_TAG_class_type:
11958     case DW_TAG_interface_type:
11959     case DW_TAG_structure_type:
11960     case DW_TAG_union_type:
11961       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11962          structures or unions.  These were of the form "._%d" in GCC 4.1,
11963          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11964          and GCC 4.4.  We work around this problem by ignoring these.  */
11965       if (strncmp (DW_STRING (attr), "._", 2) == 0
11966           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11967         return NULL;
11968       break;
11969
11970     default:
11971       break;
11972     }
11973
11974   if (!DW_STRING_IS_CANONICAL (attr))
11975     {
11976       DW_STRING (attr)
11977         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11978                                     &cu->objfile->objfile_obstack);
11979       DW_STRING_IS_CANONICAL (attr) = 1;
11980     }
11981   return DW_STRING (attr);
11982 }
11983
11984 /* Return the die that this die in an extension of, or NULL if there
11985    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11986    containing the return value on output.  */
11987
11988 static struct die_info *
11989 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11990 {
11991   struct attribute *attr;
11992
11993   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11994   if (attr == NULL)
11995     return NULL;
11996
11997   return follow_die_ref (die, attr, ext_cu);
11998 }
11999
12000 /* Convert a DIE tag into its string name.  */
12001
12002 static char *
12003 dwarf_tag_name (unsigned tag)
12004 {
12005   switch (tag)
12006     {
12007     case DW_TAG_padding:
12008       return "DW_TAG_padding";
12009     case DW_TAG_array_type:
12010       return "DW_TAG_array_type";
12011     case DW_TAG_class_type:
12012       return "DW_TAG_class_type";
12013     case DW_TAG_entry_point:
12014       return "DW_TAG_entry_point";
12015     case DW_TAG_enumeration_type:
12016       return "DW_TAG_enumeration_type";
12017     case DW_TAG_formal_parameter:
12018       return "DW_TAG_formal_parameter";
12019     case DW_TAG_imported_declaration:
12020       return "DW_TAG_imported_declaration";
12021     case DW_TAG_label:
12022       return "DW_TAG_label";
12023     case DW_TAG_lexical_block:
12024       return "DW_TAG_lexical_block";
12025     case DW_TAG_member:
12026       return "DW_TAG_member";
12027     case DW_TAG_pointer_type:
12028       return "DW_TAG_pointer_type";
12029     case DW_TAG_reference_type:
12030       return "DW_TAG_reference_type";
12031     case DW_TAG_compile_unit:
12032       return "DW_TAG_compile_unit";
12033     case DW_TAG_string_type:
12034       return "DW_TAG_string_type";
12035     case DW_TAG_structure_type:
12036       return "DW_TAG_structure_type";
12037     case DW_TAG_subroutine_type:
12038       return "DW_TAG_subroutine_type";
12039     case DW_TAG_typedef:
12040       return "DW_TAG_typedef";
12041     case DW_TAG_union_type:
12042       return "DW_TAG_union_type";
12043     case DW_TAG_unspecified_parameters:
12044       return "DW_TAG_unspecified_parameters";
12045     case DW_TAG_variant:
12046       return "DW_TAG_variant";
12047     case DW_TAG_common_block:
12048       return "DW_TAG_common_block";
12049     case DW_TAG_common_inclusion:
12050       return "DW_TAG_common_inclusion";
12051     case DW_TAG_inheritance:
12052       return "DW_TAG_inheritance";
12053     case DW_TAG_inlined_subroutine:
12054       return "DW_TAG_inlined_subroutine";
12055     case DW_TAG_module:
12056       return "DW_TAG_module";
12057     case DW_TAG_ptr_to_member_type:
12058       return "DW_TAG_ptr_to_member_type";
12059     case DW_TAG_set_type:
12060       return "DW_TAG_set_type";
12061     case DW_TAG_subrange_type:
12062       return "DW_TAG_subrange_type";
12063     case DW_TAG_with_stmt:
12064       return "DW_TAG_with_stmt";
12065     case DW_TAG_access_declaration:
12066       return "DW_TAG_access_declaration";
12067     case DW_TAG_base_type:
12068       return "DW_TAG_base_type";
12069     case DW_TAG_catch_block:
12070       return "DW_TAG_catch_block";
12071     case DW_TAG_const_type:
12072       return "DW_TAG_const_type";
12073     case DW_TAG_constant:
12074       return "DW_TAG_constant";
12075     case DW_TAG_enumerator:
12076       return "DW_TAG_enumerator";
12077     case DW_TAG_file_type:
12078       return "DW_TAG_file_type";
12079     case DW_TAG_friend:
12080       return "DW_TAG_friend";
12081     case DW_TAG_namelist:
12082       return "DW_TAG_namelist";
12083     case DW_TAG_namelist_item:
12084       return "DW_TAG_namelist_item";
12085     case DW_TAG_packed_type:
12086       return "DW_TAG_packed_type";
12087     case DW_TAG_subprogram:
12088       return "DW_TAG_subprogram";
12089     case DW_TAG_template_type_param:
12090       return "DW_TAG_template_type_param";
12091     case DW_TAG_template_value_param:
12092       return "DW_TAG_template_value_param";
12093     case DW_TAG_thrown_type:
12094       return "DW_TAG_thrown_type";
12095     case DW_TAG_try_block:
12096       return "DW_TAG_try_block";
12097     case DW_TAG_variant_part:
12098       return "DW_TAG_variant_part";
12099     case DW_TAG_variable:
12100       return "DW_TAG_variable";
12101     case DW_TAG_volatile_type:
12102       return "DW_TAG_volatile_type";
12103     case DW_TAG_dwarf_procedure:
12104       return "DW_TAG_dwarf_procedure";
12105     case DW_TAG_restrict_type:
12106       return "DW_TAG_restrict_type";
12107     case DW_TAG_interface_type:
12108       return "DW_TAG_interface_type";
12109     case DW_TAG_namespace:
12110       return "DW_TAG_namespace";
12111     case DW_TAG_imported_module:
12112       return "DW_TAG_imported_module";
12113     case DW_TAG_unspecified_type:
12114       return "DW_TAG_unspecified_type";
12115     case DW_TAG_partial_unit:
12116       return "DW_TAG_partial_unit";
12117     case DW_TAG_imported_unit:
12118       return "DW_TAG_imported_unit";
12119     case DW_TAG_condition:
12120       return "DW_TAG_condition";
12121     case DW_TAG_shared_type:
12122       return "DW_TAG_shared_type";
12123     case DW_TAG_type_unit:
12124       return "DW_TAG_type_unit";
12125     case DW_TAG_MIPS_loop:
12126       return "DW_TAG_MIPS_loop";
12127     case DW_TAG_HP_array_descriptor:
12128       return "DW_TAG_HP_array_descriptor";
12129     case DW_TAG_format_label:
12130       return "DW_TAG_format_label";
12131     case DW_TAG_function_template:
12132       return "DW_TAG_function_template";
12133     case DW_TAG_class_template:
12134       return "DW_TAG_class_template";
12135     case DW_TAG_GNU_BINCL:
12136       return "DW_TAG_GNU_BINCL";
12137     case DW_TAG_GNU_EINCL:
12138       return "DW_TAG_GNU_EINCL";
12139     case DW_TAG_upc_shared_type:
12140       return "DW_TAG_upc_shared_type";
12141     case DW_TAG_upc_strict_type:
12142       return "DW_TAG_upc_strict_type";
12143     case DW_TAG_upc_relaxed_type:
12144       return "DW_TAG_upc_relaxed_type";
12145     case DW_TAG_PGI_kanji_type:
12146       return "DW_TAG_PGI_kanji_type";
12147     case DW_TAG_PGI_interface_block:
12148       return "DW_TAG_PGI_interface_block";
12149     default:
12150       return "DW_TAG_<unknown>";
12151     }
12152 }
12153
12154 /* Convert a DWARF attribute code into its string name.  */
12155
12156 static char *
12157 dwarf_attr_name (unsigned attr)
12158 {
12159   switch (attr)
12160     {
12161     case DW_AT_sibling:
12162       return "DW_AT_sibling";
12163     case DW_AT_location:
12164       return "DW_AT_location";
12165     case DW_AT_name:
12166       return "DW_AT_name";
12167     case DW_AT_ordering:
12168       return "DW_AT_ordering";
12169     case DW_AT_subscr_data:
12170       return "DW_AT_subscr_data";
12171     case DW_AT_byte_size:
12172       return "DW_AT_byte_size";
12173     case DW_AT_bit_offset:
12174       return "DW_AT_bit_offset";
12175     case DW_AT_bit_size:
12176       return "DW_AT_bit_size";
12177     case DW_AT_element_list:
12178       return "DW_AT_element_list";
12179     case DW_AT_stmt_list:
12180       return "DW_AT_stmt_list";
12181     case DW_AT_low_pc:
12182       return "DW_AT_low_pc";
12183     case DW_AT_high_pc:
12184       return "DW_AT_high_pc";
12185     case DW_AT_language:
12186       return "DW_AT_language";
12187     case DW_AT_member:
12188       return "DW_AT_member";
12189     case DW_AT_discr:
12190       return "DW_AT_discr";
12191     case DW_AT_discr_value:
12192       return "DW_AT_discr_value";
12193     case DW_AT_visibility:
12194       return "DW_AT_visibility";
12195     case DW_AT_import:
12196       return "DW_AT_import";
12197     case DW_AT_string_length:
12198       return "DW_AT_string_length";
12199     case DW_AT_common_reference:
12200       return "DW_AT_common_reference";
12201     case DW_AT_comp_dir:
12202       return "DW_AT_comp_dir";
12203     case DW_AT_const_value:
12204       return "DW_AT_const_value";
12205     case DW_AT_containing_type:
12206       return "DW_AT_containing_type";
12207     case DW_AT_default_value:
12208       return "DW_AT_default_value";
12209     case DW_AT_inline:
12210       return "DW_AT_inline";
12211     case DW_AT_is_optional:
12212       return "DW_AT_is_optional";
12213     case DW_AT_lower_bound:
12214       return "DW_AT_lower_bound";
12215     case DW_AT_producer:
12216       return "DW_AT_producer";
12217     case DW_AT_prototyped:
12218       return "DW_AT_prototyped";
12219     case DW_AT_return_addr:
12220       return "DW_AT_return_addr";
12221     case DW_AT_start_scope:
12222       return "DW_AT_start_scope";
12223     case DW_AT_bit_stride:
12224       return "DW_AT_bit_stride";
12225     case DW_AT_upper_bound:
12226       return "DW_AT_upper_bound";
12227     case DW_AT_abstract_origin:
12228       return "DW_AT_abstract_origin";
12229     case DW_AT_accessibility:
12230       return "DW_AT_accessibility";
12231     case DW_AT_address_class:
12232       return "DW_AT_address_class";
12233     case DW_AT_artificial:
12234       return "DW_AT_artificial";
12235     case DW_AT_base_types:
12236       return "DW_AT_base_types";
12237     case DW_AT_calling_convention:
12238       return "DW_AT_calling_convention";
12239     case DW_AT_count:
12240       return "DW_AT_count";
12241     case DW_AT_data_member_location:
12242       return "DW_AT_data_member_location";
12243     case DW_AT_decl_column:
12244       return "DW_AT_decl_column";
12245     case DW_AT_decl_file:
12246       return "DW_AT_decl_file";
12247     case DW_AT_decl_line:
12248       return "DW_AT_decl_line";
12249     case DW_AT_declaration:
12250       return "DW_AT_declaration";
12251     case DW_AT_discr_list:
12252       return "DW_AT_discr_list";
12253     case DW_AT_encoding:
12254       return "DW_AT_encoding";
12255     case DW_AT_external:
12256       return "DW_AT_external";
12257     case DW_AT_frame_base:
12258       return "DW_AT_frame_base";
12259     case DW_AT_friend:
12260       return "DW_AT_friend";
12261     case DW_AT_identifier_case:
12262       return "DW_AT_identifier_case";
12263     case DW_AT_macro_info:
12264       return "DW_AT_macro_info";
12265     case DW_AT_namelist_items:
12266       return "DW_AT_namelist_items";
12267     case DW_AT_priority:
12268       return "DW_AT_priority";
12269     case DW_AT_segment:
12270       return "DW_AT_segment";
12271     case DW_AT_specification:
12272       return "DW_AT_specification";
12273     case DW_AT_static_link:
12274       return "DW_AT_static_link";
12275     case DW_AT_type:
12276       return "DW_AT_type";
12277     case DW_AT_use_location:
12278       return "DW_AT_use_location";
12279     case DW_AT_variable_parameter:
12280       return "DW_AT_variable_parameter";
12281     case DW_AT_virtuality:
12282       return "DW_AT_virtuality";
12283     case DW_AT_vtable_elem_location:
12284       return "DW_AT_vtable_elem_location";
12285     /* DWARF 3 values.  */
12286     case DW_AT_allocated:
12287       return "DW_AT_allocated";
12288     case DW_AT_associated:
12289       return "DW_AT_associated";
12290     case DW_AT_data_location:
12291       return "DW_AT_data_location";
12292     case DW_AT_byte_stride:
12293       return "DW_AT_byte_stride";
12294     case DW_AT_entry_pc:
12295       return "DW_AT_entry_pc";
12296     case DW_AT_use_UTF8:
12297       return "DW_AT_use_UTF8";
12298     case DW_AT_extension:
12299       return "DW_AT_extension";
12300     case DW_AT_ranges:
12301       return "DW_AT_ranges";
12302     case DW_AT_trampoline:
12303       return "DW_AT_trampoline";
12304     case DW_AT_call_column:
12305       return "DW_AT_call_column";
12306     case DW_AT_call_file:
12307       return "DW_AT_call_file";
12308     case DW_AT_call_line:
12309       return "DW_AT_call_line";
12310     case DW_AT_description:
12311       return "DW_AT_description";
12312     case DW_AT_binary_scale:
12313       return "DW_AT_binary_scale";
12314     case DW_AT_decimal_scale:
12315       return "DW_AT_decimal_scale";
12316     case DW_AT_small:
12317       return "DW_AT_small";
12318     case DW_AT_decimal_sign:
12319       return "DW_AT_decimal_sign";
12320     case DW_AT_digit_count:
12321       return "DW_AT_digit_count";
12322     case DW_AT_picture_string:
12323       return "DW_AT_picture_string";
12324     case DW_AT_mutable:
12325       return "DW_AT_mutable";
12326     case DW_AT_threads_scaled:
12327       return "DW_AT_threads_scaled";
12328     case DW_AT_explicit:
12329       return "DW_AT_explicit";
12330     case DW_AT_object_pointer:
12331       return "DW_AT_object_pointer";
12332     case DW_AT_endianity:
12333       return "DW_AT_endianity";
12334     case DW_AT_elemental:
12335       return "DW_AT_elemental";
12336     case DW_AT_pure:
12337       return "DW_AT_pure";
12338     case DW_AT_recursive:
12339       return "DW_AT_recursive";
12340     /* DWARF 4 values.  */
12341     case DW_AT_signature:
12342       return "DW_AT_signature";
12343     case DW_AT_linkage_name:
12344       return "DW_AT_linkage_name";
12345     /* SGI/MIPS extensions.  */
12346 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12347     case DW_AT_MIPS_fde:
12348       return "DW_AT_MIPS_fde";
12349 #endif
12350     case DW_AT_MIPS_loop_begin:
12351       return "DW_AT_MIPS_loop_begin";
12352     case DW_AT_MIPS_tail_loop_begin:
12353       return "DW_AT_MIPS_tail_loop_begin";
12354     case DW_AT_MIPS_epilog_begin:
12355       return "DW_AT_MIPS_epilog_begin";
12356     case DW_AT_MIPS_loop_unroll_factor:
12357       return "DW_AT_MIPS_loop_unroll_factor";
12358     case DW_AT_MIPS_software_pipeline_depth:
12359       return "DW_AT_MIPS_software_pipeline_depth";
12360     case DW_AT_MIPS_linkage_name:
12361       return "DW_AT_MIPS_linkage_name";
12362     case DW_AT_MIPS_stride:
12363       return "DW_AT_MIPS_stride";
12364     case DW_AT_MIPS_abstract_name:
12365       return "DW_AT_MIPS_abstract_name";
12366     case DW_AT_MIPS_clone_origin:
12367       return "DW_AT_MIPS_clone_origin";
12368     case DW_AT_MIPS_has_inlines:
12369       return "DW_AT_MIPS_has_inlines";
12370     /* HP extensions.  */
12371 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12372     case DW_AT_HP_block_index:
12373       return "DW_AT_HP_block_index";
12374 #endif
12375     case DW_AT_HP_unmodifiable:
12376       return "DW_AT_HP_unmodifiable";
12377     case DW_AT_HP_actuals_stmt_list:
12378       return "DW_AT_HP_actuals_stmt_list";
12379     case DW_AT_HP_proc_per_section:
12380       return "DW_AT_HP_proc_per_section";
12381     case DW_AT_HP_raw_data_ptr:
12382       return "DW_AT_HP_raw_data_ptr";
12383     case DW_AT_HP_pass_by_reference:
12384       return "DW_AT_HP_pass_by_reference";
12385     case DW_AT_HP_opt_level:
12386       return "DW_AT_HP_opt_level";
12387     case DW_AT_HP_prof_version_id:
12388       return "DW_AT_HP_prof_version_id";
12389     case DW_AT_HP_opt_flags:
12390       return "DW_AT_HP_opt_flags";
12391     case DW_AT_HP_cold_region_low_pc:
12392       return "DW_AT_HP_cold_region_low_pc";
12393     case DW_AT_HP_cold_region_high_pc:
12394       return "DW_AT_HP_cold_region_high_pc";
12395     case DW_AT_HP_all_variables_modifiable:
12396       return "DW_AT_HP_all_variables_modifiable";
12397     case DW_AT_HP_linkage_name:
12398       return "DW_AT_HP_linkage_name";
12399     case DW_AT_HP_prof_flags:
12400       return "DW_AT_HP_prof_flags";
12401     /* GNU extensions.  */
12402     case DW_AT_sf_names:
12403       return "DW_AT_sf_names";
12404     case DW_AT_src_info:
12405       return "DW_AT_src_info";
12406     case DW_AT_mac_info:
12407       return "DW_AT_mac_info";
12408     case DW_AT_src_coords:
12409       return "DW_AT_src_coords";
12410     case DW_AT_body_begin:
12411       return "DW_AT_body_begin";
12412     case DW_AT_body_end:
12413       return "DW_AT_body_end";
12414     case DW_AT_GNU_vector:
12415       return "DW_AT_GNU_vector";
12416     case DW_AT_GNU_odr_signature:
12417       return "DW_AT_GNU_odr_signature";
12418     /* VMS extensions.  */
12419     case DW_AT_VMS_rtnbeg_pd_address:
12420       return "DW_AT_VMS_rtnbeg_pd_address";
12421     /* UPC extension.  */
12422     case DW_AT_upc_threads_scaled:
12423       return "DW_AT_upc_threads_scaled";
12424     /* PGI (STMicroelectronics) extensions.  */
12425     case DW_AT_PGI_lbase:
12426       return "DW_AT_PGI_lbase";
12427     case DW_AT_PGI_soffset:
12428       return "DW_AT_PGI_soffset";
12429     case DW_AT_PGI_lstride:
12430       return "DW_AT_PGI_lstride";
12431     default:
12432       return "DW_AT_<unknown>";
12433     }
12434 }
12435
12436 /* Convert a DWARF value form code into its string name.  */
12437
12438 static char *
12439 dwarf_form_name (unsigned form)
12440 {
12441   switch (form)
12442     {
12443     case DW_FORM_addr:
12444       return "DW_FORM_addr";
12445     case DW_FORM_block2:
12446       return "DW_FORM_block2";
12447     case DW_FORM_block4:
12448       return "DW_FORM_block4";
12449     case DW_FORM_data2:
12450       return "DW_FORM_data2";
12451     case DW_FORM_data4:
12452       return "DW_FORM_data4";
12453     case DW_FORM_data8:
12454       return "DW_FORM_data8";
12455     case DW_FORM_string:
12456       return "DW_FORM_string";
12457     case DW_FORM_block:
12458       return "DW_FORM_block";
12459     case DW_FORM_block1:
12460       return "DW_FORM_block1";
12461     case DW_FORM_data1:
12462       return "DW_FORM_data1";
12463     case DW_FORM_flag:
12464       return "DW_FORM_flag";
12465     case DW_FORM_sdata:
12466       return "DW_FORM_sdata";
12467     case DW_FORM_strp:
12468       return "DW_FORM_strp";
12469     case DW_FORM_udata:
12470       return "DW_FORM_udata";
12471     case DW_FORM_ref_addr:
12472       return "DW_FORM_ref_addr";
12473     case DW_FORM_ref1:
12474       return "DW_FORM_ref1";
12475     case DW_FORM_ref2:
12476       return "DW_FORM_ref2";
12477     case DW_FORM_ref4:
12478       return "DW_FORM_ref4";
12479     case DW_FORM_ref8:
12480       return "DW_FORM_ref8";
12481     case DW_FORM_ref_udata:
12482       return "DW_FORM_ref_udata";
12483     case DW_FORM_indirect:
12484       return "DW_FORM_indirect";
12485     case DW_FORM_sec_offset:
12486       return "DW_FORM_sec_offset";
12487     case DW_FORM_exprloc:
12488       return "DW_FORM_exprloc";
12489     case DW_FORM_flag_present:
12490       return "DW_FORM_flag_present";
12491     case DW_FORM_sig8:
12492       return "DW_FORM_sig8";
12493     default:
12494       return "DW_FORM_<unknown>";
12495     }
12496 }
12497
12498 /* Convert a DWARF stack opcode into its string name.  */
12499
12500 const char *
12501 dwarf_stack_op_name (unsigned op)
12502 {
12503   switch (op)
12504     {
12505     case DW_OP_addr:
12506       return "DW_OP_addr";
12507     case DW_OP_deref:
12508       return "DW_OP_deref";
12509     case DW_OP_const1u:
12510       return "DW_OP_const1u";
12511     case DW_OP_const1s:
12512       return "DW_OP_const1s";
12513     case DW_OP_const2u:
12514       return "DW_OP_const2u";
12515     case DW_OP_const2s:
12516       return "DW_OP_const2s";
12517     case DW_OP_const4u:
12518       return "DW_OP_const4u";
12519     case DW_OP_const4s:
12520       return "DW_OP_const4s";
12521     case DW_OP_const8u:
12522       return "DW_OP_const8u";
12523     case DW_OP_const8s:
12524       return "DW_OP_const8s";
12525     case DW_OP_constu:
12526       return "DW_OP_constu";
12527     case DW_OP_consts:
12528       return "DW_OP_consts";
12529     case DW_OP_dup:
12530       return "DW_OP_dup";
12531     case DW_OP_drop:
12532       return "DW_OP_drop";
12533     case DW_OP_over:
12534       return "DW_OP_over";
12535     case DW_OP_pick:
12536       return "DW_OP_pick";
12537     case DW_OP_swap:
12538       return "DW_OP_swap";
12539     case DW_OP_rot:
12540       return "DW_OP_rot";
12541     case DW_OP_xderef:
12542       return "DW_OP_xderef";
12543     case DW_OP_abs:
12544       return "DW_OP_abs";
12545     case DW_OP_and:
12546       return "DW_OP_and";
12547     case DW_OP_div:
12548       return "DW_OP_div";
12549     case DW_OP_minus:
12550       return "DW_OP_minus";
12551     case DW_OP_mod:
12552       return "DW_OP_mod";
12553     case DW_OP_mul:
12554       return "DW_OP_mul";
12555     case DW_OP_neg:
12556       return "DW_OP_neg";
12557     case DW_OP_not:
12558       return "DW_OP_not";
12559     case DW_OP_or:
12560       return "DW_OP_or";
12561     case DW_OP_plus:
12562       return "DW_OP_plus";
12563     case DW_OP_plus_uconst:
12564       return "DW_OP_plus_uconst";
12565     case DW_OP_shl:
12566       return "DW_OP_shl";
12567     case DW_OP_shr:
12568       return "DW_OP_shr";
12569     case DW_OP_shra:
12570       return "DW_OP_shra";
12571     case DW_OP_xor:
12572       return "DW_OP_xor";
12573     case DW_OP_bra:
12574       return "DW_OP_bra";
12575     case DW_OP_eq:
12576       return "DW_OP_eq";
12577     case DW_OP_ge:
12578       return "DW_OP_ge";
12579     case DW_OP_gt:
12580       return "DW_OP_gt";
12581     case DW_OP_le:
12582       return "DW_OP_le";
12583     case DW_OP_lt:
12584       return "DW_OP_lt";
12585     case DW_OP_ne:
12586       return "DW_OP_ne";
12587     case DW_OP_skip:
12588       return "DW_OP_skip";
12589     case DW_OP_lit0:
12590       return "DW_OP_lit0";
12591     case DW_OP_lit1:
12592       return "DW_OP_lit1";
12593     case DW_OP_lit2:
12594       return "DW_OP_lit2";
12595     case DW_OP_lit3:
12596       return "DW_OP_lit3";
12597     case DW_OP_lit4:
12598       return "DW_OP_lit4";
12599     case DW_OP_lit5:
12600       return "DW_OP_lit5";
12601     case DW_OP_lit6:
12602       return "DW_OP_lit6";
12603     case DW_OP_lit7:
12604       return "DW_OP_lit7";
12605     case DW_OP_lit8:
12606       return "DW_OP_lit8";
12607     case DW_OP_lit9:
12608       return "DW_OP_lit9";
12609     case DW_OP_lit10:
12610       return "DW_OP_lit10";
12611     case DW_OP_lit11:
12612       return "DW_OP_lit11";
12613     case DW_OP_lit12:
12614       return "DW_OP_lit12";
12615     case DW_OP_lit13:
12616       return "DW_OP_lit13";
12617     case DW_OP_lit14:
12618       return "DW_OP_lit14";
12619     case DW_OP_lit15:
12620       return "DW_OP_lit15";
12621     case DW_OP_lit16:
12622       return "DW_OP_lit16";
12623     case DW_OP_lit17:
12624       return "DW_OP_lit17";
12625     case DW_OP_lit18:
12626       return "DW_OP_lit18";
12627     case DW_OP_lit19:
12628       return "DW_OP_lit19";
12629     case DW_OP_lit20:
12630       return "DW_OP_lit20";
12631     case DW_OP_lit21:
12632       return "DW_OP_lit21";
12633     case DW_OP_lit22:
12634       return "DW_OP_lit22";
12635     case DW_OP_lit23:
12636       return "DW_OP_lit23";
12637     case DW_OP_lit24:
12638       return "DW_OP_lit24";
12639     case DW_OP_lit25:
12640       return "DW_OP_lit25";
12641     case DW_OP_lit26:
12642       return "DW_OP_lit26";
12643     case DW_OP_lit27:
12644       return "DW_OP_lit27";
12645     case DW_OP_lit28:
12646       return "DW_OP_lit28";
12647     case DW_OP_lit29:
12648       return "DW_OP_lit29";
12649     case DW_OP_lit30:
12650       return "DW_OP_lit30";
12651     case DW_OP_lit31:
12652       return "DW_OP_lit31";
12653     case DW_OP_reg0:
12654       return "DW_OP_reg0";
12655     case DW_OP_reg1:
12656       return "DW_OP_reg1";
12657     case DW_OP_reg2:
12658       return "DW_OP_reg2";
12659     case DW_OP_reg3:
12660       return "DW_OP_reg3";
12661     case DW_OP_reg4:
12662       return "DW_OP_reg4";
12663     case DW_OP_reg5:
12664       return "DW_OP_reg5";
12665     case DW_OP_reg6:
12666       return "DW_OP_reg6";
12667     case DW_OP_reg7:
12668       return "DW_OP_reg7";
12669     case DW_OP_reg8:
12670       return "DW_OP_reg8";
12671     case DW_OP_reg9:
12672       return "DW_OP_reg9";
12673     case DW_OP_reg10:
12674       return "DW_OP_reg10";
12675     case DW_OP_reg11:
12676       return "DW_OP_reg11";
12677     case DW_OP_reg12:
12678       return "DW_OP_reg12";
12679     case DW_OP_reg13:
12680       return "DW_OP_reg13";
12681     case DW_OP_reg14:
12682       return "DW_OP_reg14";
12683     case DW_OP_reg15:
12684       return "DW_OP_reg15";
12685     case DW_OP_reg16:
12686       return "DW_OP_reg16";
12687     case DW_OP_reg17:
12688       return "DW_OP_reg17";
12689     case DW_OP_reg18:
12690       return "DW_OP_reg18";
12691     case DW_OP_reg19:
12692       return "DW_OP_reg19";
12693     case DW_OP_reg20:
12694       return "DW_OP_reg20";
12695     case DW_OP_reg21:
12696       return "DW_OP_reg21";
12697     case DW_OP_reg22:
12698       return "DW_OP_reg22";
12699     case DW_OP_reg23:
12700       return "DW_OP_reg23";
12701     case DW_OP_reg24:
12702       return "DW_OP_reg24";
12703     case DW_OP_reg25:
12704       return "DW_OP_reg25";
12705     case DW_OP_reg26:
12706       return "DW_OP_reg26";
12707     case DW_OP_reg27:
12708       return "DW_OP_reg27";
12709     case DW_OP_reg28:
12710       return "DW_OP_reg28";
12711     case DW_OP_reg29:
12712       return "DW_OP_reg29";
12713     case DW_OP_reg30:
12714       return "DW_OP_reg30";
12715     case DW_OP_reg31:
12716       return "DW_OP_reg31";
12717     case DW_OP_breg0:
12718       return "DW_OP_breg0";
12719     case DW_OP_breg1:
12720       return "DW_OP_breg1";
12721     case DW_OP_breg2:
12722       return "DW_OP_breg2";
12723     case DW_OP_breg3:
12724       return "DW_OP_breg3";
12725     case DW_OP_breg4:
12726       return "DW_OP_breg4";
12727     case DW_OP_breg5:
12728       return "DW_OP_breg5";
12729     case DW_OP_breg6:
12730       return "DW_OP_breg6";
12731     case DW_OP_breg7:
12732       return "DW_OP_breg7";
12733     case DW_OP_breg8:
12734       return "DW_OP_breg8";
12735     case DW_OP_breg9:
12736       return "DW_OP_breg9";
12737     case DW_OP_breg10:
12738       return "DW_OP_breg10";
12739     case DW_OP_breg11:
12740       return "DW_OP_breg11";
12741     case DW_OP_breg12:
12742       return "DW_OP_breg12";
12743     case DW_OP_breg13:
12744       return "DW_OP_breg13";
12745     case DW_OP_breg14:
12746       return "DW_OP_breg14";
12747     case DW_OP_breg15:
12748       return "DW_OP_breg15";
12749     case DW_OP_breg16:
12750       return "DW_OP_breg16";
12751     case DW_OP_breg17:
12752       return "DW_OP_breg17";
12753     case DW_OP_breg18:
12754       return "DW_OP_breg18";
12755     case DW_OP_breg19:
12756       return "DW_OP_breg19";
12757     case DW_OP_breg20:
12758       return "DW_OP_breg20";
12759     case DW_OP_breg21:
12760       return "DW_OP_breg21";
12761     case DW_OP_breg22:
12762       return "DW_OP_breg22";
12763     case DW_OP_breg23:
12764       return "DW_OP_breg23";
12765     case DW_OP_breg24:
12766       return "DW_OP_breg24";
12767     case DW_OP_breg25:
12768       return "DW_OP_breg25";
12769     case DW_OP_breg26:
12770       return "DW_OP_breg26";
12771     case DW_OP_breg27:
12772       return "DW_OP_breg27";
12773     case DW_OP_breg28:
12774       return "DW_OP_breg28";
12775     case DW_OP_breg29:
12776       return "DW_OP_breg29";
12777     case DW_OP_breg30:
12778       return "DW_OP_breg30";
12779     case DW_OP_breg31:
12780       return "DW_OP_breg31";
12781     case DW_OP_regx:
12782       return "DW_OP_regx";
12783     case DW_OP_fbreg:
12784       return "DW_OP_fbreg";
12785     case DW_OP_bregx:
12786       return "DW_OP_bregx";
12787     case DW_OP_piece:
12788       return "DW_OP_piece";
12789     case DW_OP_deref_size:
12790       return "DW_OP_deref_size";
12791     case DW_OP_xderef_size:
12792       return "DW_OP_xderef_size";
12793     case DW_OP_nop:
12794       return "DW_OP_nop";
12795     /* DWARF 3 extensions.  */
12796     case DW_OP_push_object_address:
12797       return "DW_OP_push_object_address";
12798     case DW_OP_call2:
12799       return "DW_OP_call2";
12800     case DW_OP_call4:
12801       return "DW_OP_call4";
12802     case DW_OP_call_ref:
12803       return "DW_OP_call_ref";
12804     case DW_OP_form_tls_address:
12805       return "DW_OP_form_tls_address";
12806     case DW_OP_call_frame_cfa:
12807       return "DW_OP_call_frame_cfa";
12808     case DW_OP_bit_piece:
12809       return "DW_OP_bit_piece";
12810     /* DWARF 4 extensions.  */
12811     case DW_OP_implicit_value:
12812       return "DW_OP_implicit_value";
12813     case DW_OP_stack_value:
12814       return "DW_OP_stack_value";
12815     /* GNU extensions.  */
12816     case DW_OP_GNU_push_tls_address:
12817       return "DW_OP_GNU_push_tls_address";
12818     case DW_OP_GNU_uninit:
12819       return "DW_OP_GNU_uninit";
12820     case DW_OP_GNU_implicit_pointer:
12821       return "DW_OP_GNU_implicit_pointer";
12822     default:
12823       return NULL;
12824     }
12825 }
12826
12827 static char *
12828 dwarf_bool_name (unsigned mybool)
12829 {
12830   if (mybool)
12831     return "TRUE";
12832   else
12833     return "FALSE";
12834 }
12835
12836 /* Convert a DWARF type code into its string name.  */
12837
12838 static char *
12839 dwarf_type_encoding_name (unsigned enc)
12840 {
12841   switch (enc)
12842     {
12843     case DW_ATE_void:
12844       return "DW_ATE_void";
12845     case DW_ATE_address:
12846       return "DW_ATE_address";
12847     case DW_ATE_boolean:
12848       return "DW_ATE_boolean";
12849     case DW_ATE_complex_float:
12850       return "DW_ATE_complex_float";
12851     case DW_ATE_float:
12852       return "DW_ATE_float";
12853     case DW_ATE_signed:
12854       return "DW_ATE_signed";
12855     case DW_ATE_signed_char:
12856       return "DW_ATE_signed_char";
12857     case DW_ATE_unsigned:
12858       return "DW_ATE_unsigned";
12859     case DW_ATE_unsigned_char:
12860       return "DW_ATE_unsigned_char";
12861     /* DWARF 3.  */
12862     case DW_ATE_imaginary_float:
12863       return "DW_ATE_imaginary_float";
12864     case DW_ATE_packed_decimal:
12865       return "DW_ATE_packed_decimal";
12866     case DW_ATE_numeric_string:
12867       return "DW_ATE_numeric_string";
12868     case DW_ATE_edited:
12869       return "DW_ATE_edited";
12870     case DW_ATE_signed_fixed:
12871       return "DW_ATE_signed_fixed";
12872     case DW_ATE_unsigned_fixed:
12873       return "DW_ATE_unsigned_fixed";
12874     case DW_ATE_decimal_float:
12875       return "DW_ATE_decimal_float";
12876     /* DWARF 4.  */
12877     case DW_ATE_UTF:
12878       return "DW_ATE_UTF";
12879     /* HP extensions.  */
12880     case DW_ATE_HP_float80:
12881       return "DW_ATE_HP_float80";
12882     case DW_ATE_HP_complex_float80:
12883       return "DW_ATE_HP_complex_float80";
12884     case DW_ATE_HP_float128:
12885       return "DW_ATE_HP_float128";
12886     case DW_ATE_HP_complex_float128:
12887       return "DW_ATE_HP_complex_float128";
12888     case DW_ATE_HP_floathpintel:
12889       return "DW_ATE_HP_floathpintel";
12890     case DW_ATE_HP_imaginary_float80:
12891       return "DW_ATE_HP_imaginary_float80";
12892     case DW_ATE_HP_imaginary_float128:
12893       return "DW_ATE_HP_imaginary_float128";
12894     default:
12895       return "DW_ATE_<unknown>";
12896     }
12897 }
12898
12899 /* Convert a DWARF call frame info operation to its string name.  */
12900
12901 #if 0
12902 static char *
12903 dwarf_cfi_name (unsigned cfi_opc)
12904 {
12905   switch (cfi_opc)
12906     {
12907     case DW_CFA_advance_loc:
12908       return "DW_CFA_advance_loc";
12909     case DW_CFA_offset:
12910       return "DW_CFA_offset";
12911     case DW_CFA_restore:
12912       return "DW_CFA_restore";
12913     case DW_CFA_nop:
12914       return "DW_CFA_nop";
12915     case DW_CFA_set_loc:
12916       return "DW_CFA_set_loc";
12917     case DW_CFA_advance_loc1:
12918       return "DW_CFA_advance_loc1";
12919     case DW_CFA_advance_loc2:
12920       return "DW_CFA_advance_loc2";
12921     case DW_CFA_advance_loc4:
12922       return "DW_CFA_advance_loc4";
12923     case DW_CFA_offset_extended:
12924       return "DW_CFA_offset_extended";
12925     case DW_CFA_restore_extended:
12926       return "DW_CFA_restore_extended";
12927     case DW_CFA_undefined:
12928       return "DW_CFA_undefined";
12929     case DW_CFA_same_value:
12930       return "DW_CFA_same_value";
12931     case DW_CFA_register:
12932       return "DW_CFA_register";
12933     case DW_CFA_remember_state:
12934       return "DW_CFA_remember_state";
12935     case DW_CFA_restore_state:
12936       return "DW_CFA_restore_state";
12937     case DW_CFA_def_cfa:
12938       return "DW_CFA_def_cfa";
12939     case DW_CFA_def_cfa_register:
12940       return "DW_CFA_def_cfa_register";
12941     case DW_CFA_def_cfa_offset:
12942       return "DW_CFA_def_cfa_offset";
12943     /* DWARF 3.  */
12944     case DW_CFA_def_cfa_expression:
12945       return "DW_CFA_def_cfa_expression";
12946     case DW_CFA_expression:
12947       return "DW_CFA_expression";
12948     case DW_CFA_offset_extended_sf:
12949       return "DW_CFA_offset_extended_sf";
12950     case DW_CFA_def_cfa_sf:
12951       return "DW_CFA_def_cfa_sf";
12952     case DW_CFA_def_cfa_offset_sf:
12953       return "DW_CFA_def_cfa_offset_sf";
12954     case DW_CFA_val_offset:
12955       return "DW_CFA_val_offset";
12956     case DW_CFA_val_offset_sf:
12957       return "DW_CFA_val_offset_sf";
12958     case DW_CFA_val_expression:
12959       return "DW_CFA_val_expression";
12960     /* SGI/MIPS specific.  */
12961     case DW_CFA_MIPS_advance_loc8:
12962       return "DW_CFA_MIPS_advance_loc8";
12963     /* GNU extensions.  */
12964     case DW_CFA_GNU_window_save:
12965       return "DW_CFA_GNU_window_save";
12966     case DW_CFA_GNU_args_size:
12967       return "DW_CFA_GNU_args_size";
12968     case DW_CFA_GNU_negative_offset_extended:
12969       return "DW_CFA_GNU_negative_offset_extended";
12970     default:
12971       return "DW_CFA_<unknown>";
12972     }
12973 }
12974 #endif
12975
12976 static void
12977 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12978 {
12979   unsigned int i;
12980
12981   print_spaces (indent, f);
12982   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12983            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12984
12985   if (die->parent != NULL)
12986     {
12987       print_spaces (indent, f);
12988       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12989                           die->parent->offset);
12990     }
12991
12992   print_spaces (indent, f);
12993   fprintf_unfiltered (f, "  has children: %s\n",
12994            dwarf_bool_name (die->child != NULL));
12995
12996   print_spaces (indent, f);
12997   fprintf_unfiltered (f, "  attributes:\n");
12998
12999   for (i = 0; i < die->num_attrs; ++i)
13000     {
13001       print_spaces (indent, f);
13002       fprintf_unfiltered (f, "    %s (%s) ",
13003                dwarf_attr_name (die->attrs[i].name),
13004                dwarf_form_name (die->attrs[i].form));
13005
13006       switch (die->attrs[i].form)
13007         {
13008         case DW_FORM_ref_addr:
13009         case DW_FORM_addr:
13010           fprintf_unfiltered (f, "address: ");
13011           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13012           break;
13013         case DW_FORM_block2:
13014         case DW_FORM_block4:
13015         case DW_FORM_block:
13016         case DW_FORM_block1:
13017           fprintf_unfiltered (f, "block: size %d",
13018                               DW_BLOCK (&die->attrs[i])->size);
13019           break;
13020         case DW_FORM_exprloc:
13021           fprintf_unfiltered (f, "expression: size %u",
13022                               DW_BLOCK (&die->attrs[i])->size);
13023           break;
13024         case DW_FORM_ref1:
13025         case DW_FORM_ref2:
13026         case DW_FORM_ref4:
13027           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13028                               (long) (DW_ADDR (&die->attrs[i])));
13029           break;
13030         case DW_FORM_data1:
13031         case DW_FORM_data2:
13032         case DW_FORM_data4:
13033         case DW_FORM_data8:
13034         case DW_FORM_udata:
13035         case DW_FORM_sdata:
13036           fprintf_unfiltered (f, "constant: %s",
13037                               pulongest (DW_UNSND (&die->attrs[i])));
13038           break;
13039         case DW_FORM_sec_offset:
13040           fprintf_unfiltered (f, "section offset: %s",
13041                               pulongest (DW_UNSND (&die->attrs[i])));
13042           break;
13043         case DW_FORM_sig8:
13044           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13045             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13046                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13047           else
13048             fprintf_unfiltered (f, "signatured type, offset: unknown");
13049           break;
13050         case DW_FORM_string:
13051         case DW_FORM_strp:
13052           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13053                    DW_STRING (&die->attrs[i])
13054                    ? DW_STRING (&die->attrs[i]) : "",
13055                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13056           break;
13057         case DW_FORM_flag:
13058           if (DW_UNSND (&die->attrs[i]))
13059             fprintf_unfiltered (f, "flag: TRUE");
13060           else
13061             fprintf_unfiltered (f, "flag: FALSE");
13062           break;
13063         case DW_FORM_flag_present:
13064           fprintf_unfiltered (f, "flag: TRUE");
13065           break;
13066         case DW_FORM_indirect:
13067           /* The reader will have reduced the indirect form to
13068              the "base form" so this form should not occur.  */
13069           fprintf_unfiltered (f, 
13070                               "unexpected attribute form: DW_FORM_indirect");
13071           break;
13072         default:
13073           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13074                    die->attrs[i].form);
13075           break;
13076         }
13077       fprintf_unfiltered (f, "\n");
13078     }
13079 }
13080
13081 static void
13082 dump_die_for_error (struct die_info *die)
13083 {
13084   dump_die_shallow (gdb_stderr, 0, die);
13085 }
13086
13087 static void
13088 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13089 {
13090   int indent = level * 4;
13091
13092   gdb_assert (die != NULL);
13093
13094   if (level >= max_level)
13095     return;
13096
13097   dump_die_shallow (f, indent, die);
13098
13099   if (die->child != NULL)
13100     {
13101       print_spaces (indent, f);
13102       fprintf_unfiltered (f, "  Children:");
13103       if (level + 1 < max_level)
13104         {
13105           fprintf_unfiltered (f, "\n");
13106           dump_die_1 (f, level + 1, max_level, die->child);
13107         }
13108       else
13109         {
13110           fprintf_unfiltered (f,
13111                               " [not printed, max nesting level reached]\n");
13112         }
13113     }
13114
13115   if (die->sibling != NULL && level > 0)
13116     {
13117       dump_die_1 (f, level, max_level, die->sibling);
13118     }
13119 }
13120
13121 /* This is called from the pdie macro in gdbinit.in.
13122    It's not static so gcc will keep a copy callable from gdb.  */
13123
13124 void
13125 dump_die (struct die_info *die, int max_level)
13126 {
13127   dump_die_1 (gdb_stdlog, 0, max_level, die);
13128 }
13129
13130 static void
13131 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13132 {
13133   void **slot;
13134
13135   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13136
13137   *slot = die;
13138 }
13139
13140 static int
13141 is_ref_attr (struct attribute *attr)
13142 {
13143   switch (attr->form)
13144     {
13145     case DW_FORM_ref_addr:
13146     case DW_FORM_ref1:
13147     case DW_FORM_ref2:
13148     case DW_FORM_ref4:
13149     case DW_FORM_ref8:
13150     case DW_FORM_ref_udata:
13151       return 1;
13152     default:
13153       return 0;
13154     }
13155 }
13156
13157 static unsigned int
13158 dwarf2_get_ref_die_offset (struct attribute *attr)
13159 {
13160   if (is_ref_attr (attr))
13161     return DW_ADDR (attr);
13162
13163   complaint (&symfile_complaints,
13164              _("unsupported die ref attribute form: '%s'"),
13165              dwarf_form_name (attr->form));
13166   return 0;
13167 }
13168
13169 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13170  * the value held by the attribute is not constant.  */
13171
13172 static LONGEST
13173 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13174 {
13175   if (attr->form == DW_FORM_sdata)
13176     return DW_SND (attr);
13177   else if (attr->form == DW_FORM_udata
13178            || attr->form == DW_FORM_data1
13179            || attr->form == DW_FORM_data2
13180            || attr->form == DW_FORM_data4
13181            || attr->form == DW_FORM_data8)
13182     return DW_UNSND (attr);
13183   else
13184     {
13185       complaint (&symfile_complaints,
13186                  _("Attribute value is not a constant (%s)"),
13187                  dwarf_form_name (attr->form));
13188       return default_value;
13189     }
13190 }
13191
13192 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13193    unit and add it to our queue.
13194    The result is non-zero if PER_CU was queued, otherwise the result is zero
13195    meaning either PER_CU is already queued or it is already loaded.  */
13196
13197 static int
13198 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13199                        struct dwarf2_per_cu_data *per_cu)
13200 {
13201   /* We may arrive here during partial symbol reading, if we need full
13202      DIEs to process an unusual case (e.g. template arguments).  Do
13203      not queue PER_CU, just tell our caller to load its DIEs.  */
13204   if (dwarf2_per_objfile->reading_partial_symbols)
13205     {
13206       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13207         return 1;
13208       return 0;
13209     }
13210
13211   /* Mark the dependence relation so that we don't flush PER_CU
13212      too early.  */
13213   dwarf2_add_dependence (this_cu, per_cu);
13214
13215   /* If it's already on the queue, we have nothing to do.  */
13216   if (per_cu->queued)
13217     return 0;
13218
13219   /* If the compilation unit is already loaded, just mark it as
13220      used.  */
13221   if (per_cu->cu != NULL)
13222     {
13223       per_cu->cu->last_used = 0;
13224       return 0;
13225     }
13226
13227   /* Add it to the queue.  */
13228   queue_comp_unit (per_cu, this_cu->objfile);
13229
13230   return 1;
13231 }
13232
13233 /* Follow reference or signature attribute ATTR of SRC_DIE.
13234    On entry *REF_CU is the CU of SRC_DIE.
13235    On exit *REF_CU is the CU of the result.  */
13236
13237 static struct die_info *
13238 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13239                        struct dwarf2_cu **ref_cu)
13240 {
13241   struct die_info *die;
13242
13243   if (is_ref_attr (attr))
13244     die = follow_die_ref (src_die, attr, ref_cu);
13245   else if (attr->form == DW_FORM_sig8)
13246     die = follow_die_sig (src_die, attr, ref_cu);
13247   else
13248     {
13249       dump_die_for_error (src_die);
13250       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13251              (*ref_cu)->objfile->name);
13252     }
13253
13254   return die;
13255 }
13256
13257 /* Follow reference OFFSET.
13258    On entry *REF_CU is the CU of the source die referencing OFFSET.
13259    On exit *REF_CU is the CU of the result.
13260    Returns NULL if OFFSET is invalid.  */
13261
13262 static struct die_info *
13263 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13264 {
13265   struct die_info temp_die;
13266   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13267
13268   gdb_assert (cu->per_cu != NULL);
13269
13270   target_cu = cu;
13271
13272   if (cu->per_cu->from_debug_types)
13273     {
13274       /* .debug_types CUs cannot reference anything outside their CU.
13275          If they need to, they have to reference a signatured type via
13276          DW_FORM_sig8.  */
13277       if (! offset_in_cu_p (&cu->header, offset))
13278         return NULL;
13279     }
13280   else if (! offset_in_cu_p (&cu->header, offset))
13281     {
13282       struct dwarf2_per_cu_data *per_cu;
13283
13284       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13285
13286       /* If necessary, add it to the queue and load its DIEs.  */
13287       if (maybe_queue_comp_unit (cu, per_cu))
13288         load_full_comp_unit (per_cu, cu->objfile);
13289
13290       target_cu = per_cu->cu;
13291     }
13292   else if (cu->dies == NULL)
13293     {
13294       /* We're loading full DIEs during partial symbol reading.  */
13295       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13296       load_full_comp_unit (cu->per_cu, cu->objfile);
13297     }
13298
13299   *ref_cu = target_cu;
13300   temp_die.offset = offset;
13301   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13302 }
13303
13304 /* Follow reference attribute ATTR of SRC_DIE.
13305    On entry *REF_CU is the CU of SRC_DIE.
13306    On exit *REF_CU is the CU of the result.  */
13307
13308 static struct die_info *
13309 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13310                 struct dwarf2_cu **ref_cu)
13311 {
13312   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13313   struct dwarf2_cu *cu = *ref_cu;
13314   struct die_info *die;
13315
13316   die = follow_die_offset (offset, ref_cu);
13317   if (!die)
13318     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13319            "at 0x%x [in module %s]"),
13320            offset, src_die->offset, cu->objfile->name);
13321
13322   return die;
13323 }
13324
13325 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13326    value is intended for DW_OP_call*.  */
13327
13328 struct dwarf2_locexpr_baton
13329 dwarf2_fetch_die_location_block (unsigned int offset,
13330                                  struct dwarf2_per_cu_data *per_cu,
13331                                  CORE_ADDR (*get_frame_pc) (void *baton),
13332                                  void *baton)
13333 {
13334   struct dwarf2_cu *cu = per_cu->cu;
13335   struct die_info *die;
13336   struct attribute *attr;
13337   struct dwarf2_locexpr_baton retval;
13338
13339   dw2_setup (per_cu->objfile);
13340
13341   die = follow_die_offset (offset, &cu);
13342   if (!die)
13343     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13344            offset, per_cu->cu->objfile->name);
13345
13346   attr = dwarf2_attr (die, DW_AT_location, cu);
13347   if (!attr)
13348     {
13349       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13350
13351       retval.data = NULL;
13352       retval.size = 0;
13353     }
13354   else if (attr_form_is_section_offset (attr))
13355     {
13356       struct dwarf2_loclist_baton loclist_baton;
13357       CORE_ADDR pc = (*get_frame_pc) (baton);
13358       size_t size;
13359
13360       fill_in_loclist_baton (cu, &loclist_baton, attr);
13361
13362       retval.data = dwarf2_find_location_expression (&loclist_baton,
13363                                                      &size, pc);
13364       retval.size = size;
13365     }
13366   else
13367     {
13368       if (!attr_form_is_block (attr))
13369         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13370                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13371                offset, per_cu->cu->objfile->name);
13372
13373       retval.data = DW_BLOCK (attr)->data;
13374       retval.size = DW_BLOCK (attr)->size;
13375     }
13376   retval.per_cu = cu->per_cu;
13377   return retval;
13378 }
13379
13380 /* Follow the signature attribute ATTR in SRC_DIE.
13381    On entry *REF_CU is the CU of SRC_DIE.
13382    On exit *REF_CU is the CU of the result.  */
13383
13384 static struct die_info *
13385 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13386                 struct dwarf2_cu **ref_cu)
13387 {
13388   struct objfile *objfile = (*ref_cu)->objfile;
13389   struct die_info temp_die;
13390   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13391   struct dwarf2_cu *sig_cu;
13392   struct die_info *die;
13393
13394   /* sig_type will be NULL if the signatured type is missing from
13395      the debug info.  */
13396   if (sig_type == NULL)
13397     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13398              "at 0x%x [in module %s]"),
13399            src_die->offset, objfile->name);
13400
13401   /* If necessary, add it to the queue and load its DIEs.  */
13402
13403   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13404     read_signatured_type (objfile, sig_type);
13405
13406   gdb_assert (sig_type->per_cu.cu != NULL);
13407
13408   sig_cu = sig_type->per_cu.cu;
13409   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13410   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13411   if (die)
13412     {
13413       *ref_cu = sig_cu;
13414       return die;
13415     }
13416
13417   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13418          "from DIE at 0x%x [in module %s]"),
13419          sig_type->type_offset, src_die->offset, objfile->name);
13420 }
13421
13422 /* Given an offset of a signatured type, return its signatured_type.  */
13423
13424 static struct signatured_type *
13425 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13426 {
13427   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13428   unsigned int length, initial_length_size;
13429   unsigned int sig_offset;
13430   struct signatured_type find_entry, *type_sig;
13431
13432   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13433   sig_offset = (initial_length_size
13434                 + 2 /*version*/
13435                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13436                 + 1 /*address_size*/);
13437   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13438   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13439
13440   /* This is only used to lookup previously recorded types.
13441      If we didn't find it, it's our bug.  */
13442   gdb_assert (type_sig != NULL);
13443   gdb_assert (offset == type_sig->offset);
13444
13445   return type_sig;
13446 }
13447
13448 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13449
13450 static void
13451 read_signatured_type_at_offset (struct objfile *objfile,
13452                                 unsigned int offset)
13453 {
13454   struct signatured_type *type_sig;
13455
13456   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13457
13458   /* We have the section offset, but we need the signature to do the
13459      hash table lookup.  */
13460   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13461
13462   gdb_assert (type_sig->per_cu.cu == NULL);
13463
13464   read_signatured_type (objfile, type_sig);
13465
13466   gdb_assert (type_sig->per_cu.cu != NULL);
13467 }
13468
13469 /* Read in a signatured type and build its CU and DIEs.  */
13470
13471 static void
13472 read_signatured_type (struct objfile *objfile,
13473                       struct signatured_type *type_sig)
13474 {
13475   gdb_byte *types_ptr;
13476   struct die_reader_specs reader_specs;
13477   struct dwarf2_cu *cu;
13478   ULONGEST signature;
13479   struct cleanup *back_to, *free_cu_cleanup;
13480
13481   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13482   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13483
13484   gdb_assert (type_sig->per_cu.cu == NULL);
13485
13486   cu = xmalloc (sizeof (*cu));
13487   init_one_comp_unit (cu, objfile);
13488
13489   type_sig->per_cu.cu = cu;
13490   cu->per_cu = &type_sig->per_cu;
13491
13492   /* If an error occurs while loading, release our storage.  */
13493   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13494
13495   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13496                                         types_ptr, objfile->obfd);
13497   gdb_assert (signature == type_sig->signature);
13498
13499   cu->die_hash
13500     = htab_create_alloc_ex (cu->header.length / 12,
13501                             die_hash,
13502                             die_eq,
13503                             NULL,
13504                             &cu->comp_unit_obstack,
13505                             hashtab_obstack_allocate,
13506                             dummy_obstack_deallocate);
13507
13508   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13509   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13510
13511   init_cu_die_reader (&reader_specs, cu);
13512
13513   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13514                                     NULL /*parent*/);
13515
13516   /* We try not to read any attributes in this function, because not
13517      all objfiles needed for references have been loaded yet, and symbol
13518      table processing isn't initialized.  But we have to set the CU language,
13519      or we won't be able to build types correctly.  */
13520   prepare_one_comp_unit (cu, cu->dies);
13521
13522   do_cleanups (back_to);
13523
13524   /* We've successfully allocated this compilation unit.  Let our caller
13525      clean it up when finished with it.  */
13526   discard_cleanups (free_cu_cleanup);
13527
13528   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13529   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13530 }
13531
13532 /* Decode simple location descriptions.
13533    Given a pointer to a dwarf block that defines a location, compute
13534    the location and return the value.
13535
13536    NOTE drow/2003-11-18: This function is called in two situations
13537    now: for the address of static or global variables (partial symbols
13538    only) and for offsets into structures which are expected to be
13539    (more or less) constant.  The partial symbol case should go away,
13540    and only the constant case should remain.  That will let this
13541    function complain more accurately.  A few special modes are allowed
13542    without complaint for global variables (for instance, global
13543    register values and thread-local values).
13544
13545    A location description containing no operations indicates that the
13546    object is optimized out.  The return value is 0 for that case.
13547    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13548    callers will only want a very basic result and this can become a
13549    complaint.
13550
13551    Note that stack[0] is unused except as a default error return.  */
13552
13553 static CORE_ADDR
13554 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13555 {
13556   struct objfile *objfile = cu->objfile;
13557   int i;
13558   int size = blk->size;
13559   gdb_byte *data = blk->data;
13560   CORE_ADDR stack[64];
13561   int stacki;
13562   unsigned int bytes_read, unsnd;
13563   gdb_byte op;
13564
13565   i = 0;
13566   stacki = 0;
13567   stack[stacki] = 0;
13568   stack[++stacki] = 0;
13569
13570   while (i < size)
13571     {
13572       op = data[i++];
13573       switch (op)
13574         {
13575         case DW_OP_lit0:
13576         case DW_OP_lit1:
13577         case DW_OP_lit2:
13578         case DW_OP_lit3:
13579         case DW_OP_lit4:
13580         case DW_OP_lit5:
13581         case DW_OP_lit6:
13582         case DW_OP_lit7:
13583         case DW_OP_lit8:
13584         case DW_OP_lit9:
13585         case DW_OP_lit10:
13586         case DW_OP_lit11:
13587         case DW_OP_lit12:
13588         case DW_OP_lit13:
13589         case DW_OP_lit14:
13590         case DW_OP_lit15:
13591         case DW_OP_lit16:
13592         case DW_OP_lit17:
13593         case DW_OP_lit18:
13594         case DW_OP_lit19:
13595         case DW_OP_lit20:
13596         case DW_OP_lit21:
13597         case DW_OP_lit22:
13598         case DW_OP_lit23:
13599         case DW_OP_lit24:
13600         case DW_OP_lit25:
13601         case DW_OP_lit26:
13602         case DW_OP_lit27:
13603         case DW_OP_lit28:
13604         case DW_OP_lit29:
13605         case DW_OP_lit30:
13606         case DW_OP_lit31:
13607           stack[++stacki] = op - DW_OP_lit0;
13608           break;
13609
13610         case DW_OP_reg0:
13611         case DW_OP_reg1:
13612         case DW_OP_reg2:
13613         case DW_OP_reg3:
13614         case DW_OP_reg4:
13615         case DW_OP_reg5:
13616         case DW_OP_reg6:
13617         case DW_OP_reg7:
13618         case DW_OP_reg8:
13619         case DW_OP_reg9:
13620         case DW_OP_reg10:
13621         case DW_OP_reg11:
13622         case DW_OP_reg12:
13623         case DW_OP_reg13:
13624         case DW_OP_reg14:
13625         case DW_OP_reg15:
13626         case DW_OP_reg16:
13627         case DW_OP_reg17:
13628         case DW_OP_reg18:
13629         case DW_OP_reg19:
13630         case DW_OP_reg20:
13631         case DW_OP_reg21:
13632         case DW_OP_reg22:
13633         case DW_OP_reg23:
13634         case DW_OP_reg24:
13635         case DW_OP_reg25:
13636         case DW_OP_reg26:
13637         case DW_OP_reg27:
13638         case DW_OP_reg28:
13639         case DW_OP_reg29:
13640         case DW_OP_reg30:
13641         case DW_OP_reg31:
13642           stack[++stacki] = op - DW_OP_reg0;
13643           if (i < size)
13644             dwarf2_complex_location_expr_complaint ();
13645           break;
13646
13647         case DW_OP_regx:
13648           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13649           i += bytes_read;
13650           stack[++stacki] = unsnd;
13651           if (i < size)
13652             dwarf2_complex_location_expr_complaint ();
13653           break;
13654
13655         case DW_OP_addr:
13656           stack[++stacki] = read_address (objfile->obfd, &data[i],
13657                                           cu, &bytes_read);
13658           i += bytes_read;
13659           break;
13660
13661         case DW_OP_const1u:
13662           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13663           i += 1;
13664           break;
13665
13666         case DW_OP_const1s:
13667           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13668           i += 1;
13669           break;
13670
13671         case DW_OP_const2u:
13672           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13673           i += 2;
13674           break;
13675
13676         case DW_OP_const2s:
13677           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13678           i += 2;
13679           break;
13680
13681         case DW_OP_const4u:
13682           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13683           i += 4;
13684           break;
13685
13686         case DW_OP_const4s:
13687           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13688           i += 4;
13689           break;
13690
13691         case DW_OP_constu:
13692           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13693                                                   &bytes_read);
13694           i += bytes_read;
13695           break;
13696
13697         case DW_OP_consts:
13698           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13699           i += bytes_read;
13700           break;
13701
13702         case DW_OP_dup:
13703           stack[stacki + 1] = stack[stacki];
13704           stacki++;
13705           break;
13706
13707         case DW_OP_plus:
13708           stack[stacki - 1] += stack[stacki];
13709           stacki--;
13710           break;
13711
13712         case DW_OP_plus_uconst:
13713           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13714                                                  &bytes_read);
13715           i += bytes_read;
13716           break;
13717
13718         case DW_OP_minus:
13719           stack[stacki - 1] -= stack[stacki];
13720           stacki--;
13721           break;
13722
13723         case DW_OP_deref:
13724           /* If we're not the last op, then we definitely can't encode
13725              this using GDB's address_class enum.  This is valid for partial
13726              global symbols, although the variable's address will be bogus
13727              in the psymtab.  */
13728           if (i < size)
13729             dwarf2_complex_location_expr_complaint ();
13730           break;
13731
13732         case DW_OP_GNU_push_tls_address:
13733           /* The top of the stack has the offset from the beginning
13734              of the thread control block at which the variable is located.  */
13735           /* Nothing should follow this operator, so the top of stack would
13736              be returned.  */
13737           /* This is valid for partial global symbols, but the variable's
13738              address will be bogus in the psymtab.  */
13739           if (i < size)
13740             dwarf2_complex_location_expr_complaint ();
13741           break;
13742
13743         case DW_OP_GNU_uninit:
13744           break;
13745
13746         default:
13747           {
13748             const char *name = dwarf_stack_op_name (op);
13749
13750             if (name)
13751               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13752                          name);
13753             else
13754               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13755                          op);
13756           }
13757
13758           return (stack[stacki]);
13759         }
13760
13761       /* Enforce maximum stack depth of SIZE-1 to avoid writing
13762          outside of the allocated space.  Also enforce minimum>0.  */
13763       if (stacki >= ARRAY_SIZE (stack) - 1)
13764         {
13765           complaint (&symfile_complaints,
13766                      _("location description stack overflow"));
13767           return 0;
13768         }
13769
13770       if (stacki <= 0)
13771         {
13772           complaint (&symfile_complaints,
13773                      _("location description stack underflow"));
13774           return 0;
13775         }
13776     }
13777   return (stack[stacki]);
13778 }
13779
13780 /* memory allocation interface */
13781
13782 static struct dwarf_block *
13783 dwarf_alloc_block (struct dwarf2_cu *cu)
13784 {
13785   struct dwarf_block *blk;
13786
13787   blk = (struct dwarf_block *)
13788     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13789   return (blk);
13790 }
13791
13792 static struct abbrev_info *
13793 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13794 {
13795   struct abbrev_info *abbrev;
13796
13797   abbrev = (struct abbrev_info *)
13798     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13799   memset (abbrev, 0, sizeof (struct abbrev_info));
13800   return (abbrev);
13801 }
13802
13803 static struct die_info *
13804 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13805 {
13806   struct die_info *die;
13807   size_t size = sizeof (struct die_info);
13808
13809   if (num_attrs > 1)
13810     size += (num_attrs - 1) * sizeof (struct attribute);
13811
13812   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13813   memset (die, 0, sizeof (struct die_info));
13814   return (die);
13815 }
13816
13817 \f
13818 /* Macro support.  */
13819
13820 /* Return the full name of file number I in *LH's file name table.
13821    Use COMP_DIR as the name of the current directory of the
13822    compilation.  The result is allocated using xmalloc; the caller is
13823    responsible for freeing it.  */
13824 static char *
13825 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13826 {
13827   /* Is the file number a valid index into the line header's file name
13828      table?  Remember that file numbers start with one, not zero.  */
13829   if (1 <= file && file <= lh->num_file_names)
13830     {
13831       struct file_entry *fe = &lh->file_names[file - 1];
13832
13833       if (IS_ABSOLUTE_PATH (fe->name))
13834         return xstrdup (fe->name);
13835       else
13836         {
13837           const char *dir;
13838           int dir_len;
13839           char *full_name;
13840
13841           if (fe->dir_index)
13842             dir = lh->include_dirs[fe->dir_index - 1];
13843           else
13844             dir = comp_dir;
13845
13846           if (dir)
13847             {
13848               dir_len = strlen (dir);
13849               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13850               strcpy (full_name, dir);
13851               full_name[dir_len] = '/';
13852               strcpy (full_name + dir_len + 1, fe->name);
13853               return full_name;
13854             }
13855           else
13856             return xstrdup (fe->name);
13857         }
13858     }
13859   else
13860     {
13861       /* The compiler produced a bogus file number.  We can at least
13862          record the macro definitions made in the file, even if we
13863          won't be able to find the file by name.  */
13864       char fake_name[80];
13865
13866       sprintf (fake_name, "<bad macro file number %d>", file);
13867
13868       complaint (&symfile_complaints,
13869                  _("bad file number in macro information (%d)"),
13870                  file);
13871
13872       return xstrdup (fake_name);
13873     }
13874 }
13875
13876
13877 static struct macro_source_file *
13878 macro_start_file (int file, int line,
13879                   struct macro_source_file *current_file,
13880                   const char *comp_dir,
13881                   struct line_header *lh, struct objfile *objfile)
13882 {
13883   /* The full name of this source file.  */
13884   char *full_name = file_full_name (file, lh, comp_dir);
13885
13886   /* We don't create a macro table for this compilation unit
13887      at all until we actually get a filename.  */
13888   if (! pending_macros)
13889     pending_macros = new_macro_table (&objfile->objfile_obstack,
13890                                       objfile->macro_cache);
13891
13892   if (! current_file)
13893     /* If we have no current file, then this must be the start_file
13894        directive for the compilation unit's main source file.  */
13895     current_file = macro_set_main (pending_macros, full_name);
13896   else
13897     current_file = macro_include (current_file, line, full_name);
13898
13899   xfree (full_name);
13900
13901   return current_file;
13902 }
13903
13904
13905 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13906    followed by a null byte.  */
13907 static char *
13908 copy_string (const char *buf, int len)
13909 {
13910   char *s = xmalloc (len + 1);
13911
13912   memcpy (s, buf, len);
13913   s[len] = '\0';
13914   return s;
13915 }
13916
13917
13918 static const char *
13919 consume_improper_spaces (const char *p, const char *body)
13920 {
13921   if (*p == ' ')
13922     {
13923       complaint (&symfile_complaints,
13924                  _("macro definition contains spaces "
13925                    "in formal argument list:\n`%s'"),
13926                  body);
13927
13928       while (*p == ' ')
13929         p++;
13930     }
13931
13932   return p;
13933 }
13934
13935
13936 static void
13937 parse_macro_definition (struct macro_source_file *file, int line,
13938                         const char *body)
13939 {
13940   const char *p;
13941
13942   /* The body string takes one of two forms.  For object-like macro
13943      definitions, it should be:
13944
13945         <macro name> " " <definition>
13946
13947      For function-like macro definitions, it should be:
13948
13949         <macro name> "() " <definition>
13950      or
13951         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13952
13953      Spaces may appear only where explicitly indicated, and in the
13954      <definition>.
13955
13956      The Dwarf 2 spec says that an object-like macro's name is always
13957      followed by a space, but versions of GCC around March 2002 omit
13958      the space when the macro's definition is the empty string.
13959
13960      The Dwarf 2 spec says that there should be no spaces between the
13961      formal arguments in a function-like macro's formal argument list,
13962      but versions of GCC around March 2002 include spaces after the
13963      commas.  */
13964
13965
13966   /* Find the extent of the macro name.  The macro name is terminated
13967      by either a space or null character (for an object-like macro) or
13968      an opening paren (for a function-like macro).  */
13969   for (p = body; *p; p++)
13970     if (*p == ' ' || *p == '(')
13971       break;
13972
13973   if (*p == ' ' || *p == '\0')
13974     {
13975       /* It's an object-like macro.  */
13976       int name_len = p - body;
13977       char *name = copy_string (body, name_len);
13978       const char *replacement;
13979
13980       if (*p == ' ')
13981         replacement = body + name_len + 1;
13982       else
13983         {
13984           dwarf2_macro_malformed_definition_complaint (body);
13985           replacement = body + name_len;
13986         }
13987
13988       macro_define_object (file, line, name, replacement);
13989
13990       xfree (name);
13991     }
13992   else if (*p == '(')
13993     {
13994       /* It's a function-like macro.  */
13995       char *name = copy_string (body, p - body);
13996       int argc = 0;
13997       int argv_size = 1;
13998       char **argv = xmalloc (argv_size * sizeof (*argv));
13999
14000       p++;
14001
14002       p = consume_improper_spaces (p, body);
14003
14004       /* Parse the formal argument list.  */
14005       while (*p && *p != ')')
14006         {
14007           /* Find the extent of the current argument name.  */
14008           const char *arg_start = p;
14009
14010           while (*p && *p != ',' && *p != ')' && *p != ' ')
14011             p++;
14012
14013           if (! *p || p == arg_start)
14014             dwarf2_macro_malformed_definition_complaint (body);
14015           else
14016             {
14017               /* Make sure argv has room for the new argument.  */
14018               if (argc >= argv_size)
14019                 {
14020                   argv_size *= 2;
14021                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14022                 }
14023
14024               argv[argc++] = copy_string (arg_start, p - arg_start);
14025             }
14026
14027           p = consume_improper_spaces (p, body);
14028
14029           /* Consume the comma, if present.  */
14030           if (*p == ',')
14031             {
14032               p++;
14033
14034               p = consume_improper_spaces (p, body);
14035             }
14036         }
14037
14038       if (*p == ')')
14039         {
14040           p++;
14041
14042           if (*p == ' ')
14043             /* Perfectly formed definition, no complaints.  */
14044             macro_define_function (file, line, name,
14045                                    argc, (const char **) argv,
14046                                    p + 1);
14047           else if (*p == '\0')
14048             {
14049               /* Complain, but do define it.  */
14050               dwarf2_macro_malformed_definition_complaint (body);
14051               macro_define_function (file, line, name,
14052                                      argc, (const char **) argv,
14053                                      p);
14054             }
14055           else
14056             /* Just complain.  */
14057             dwarf2_macro_malformed_definition_complaint (body);
14058         }
14059       else
14060         /* Just complain.  */
14061         dwarf2_macro_malformed_definition_complaint (body);
14062
14063       xfree (name);
14064       {
14065         int i;
14066
14067         for (i = 0; i < argc; i++)
14068           xfree (argv[i]);
14069       }
14070       xfree (argv);
14071     }
14072   else
14073     dwarf2_macro_malformed_definition_complaint (body);
14074 }
14075
14076
14077 static void
14078 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14079                      char *comp_dir, bfd *abfd,
14080                      struct dwarf2_cu *cu)
14081 {
14082   gdb_byte *mac_ptr, *mac_end;
14083   struct macro_source_file *current_file = 0;
14084   enum dwarf_macinfo_record_type macinfo_type;
14085   int at_commandline;
14086
14087   dwarf2_read_section (dwarf2_per_objfile->objfile,
14088                        &dwarf2_per_objfile->macinfo);
14089   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14090     {
14091       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14092       return;
14093     }
14094
14095   /* First pass: Find the name of the base filename.
14096      This filename is needed in order to process all macros whose definition
14097      (or undefinition) comes from the command line.  These macros are defined
14098      before the first DW_MACINFO_start_file entry, and yet still need to be
14099      associated to the base file.
14100
14101      To determine the base file name, we scan the macro definitions until we
14102      reach the first DW_MACINFO_start_file entry.  We then initialize
14103      CURRENT_FILE accordingly so that any macro definition found before the
14104      first DW_MACINFO_start_file can still be associated to the base file.  */
14105
14106   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14107   mac_end = dwarf2_per_objfile->macinfo.buffer
14108     + dwarf2_per_objfile->macinfo.size;
14109
14110   do
14111     {
14112       /* Do we at least have room for a macinfo type byte?  */
14113       if (mac_ptr >= mac_end)
14114         {
14115           /* Complaint is printed during the second pass as GDB will probably
14116              stop the first pass earlier upon finding
14117              DW_MACINFO_start_file.  */
14118           break;
14119         }
14120
14121       macinfo_type = read_1_byte (abfd, mac_ptr);
14122       mac_ptr++;
14123
14124       switch (macinfo_type)
14125         {
14126           /* A zero macinfo type indicates the end of the macro
14127              information.  */
14128         case 0:
14129           break;
14130
14131         case DW_MACINFO_define:
14132         case DW_MACINFO_undef:
14133           /* Only skip the data by MAC_PTR.  */
14134           {
14135             unsigned int bytes_read;
14136
14137             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14138             mac_ptr += bytes_read;
14139             read_direct_string (abfd, mac_ptr, &bytes_read);
14140             mac_ptr += bytes_read;
14141           }
14142           break;
14143
14144         case DW_MACINFO_start_file:
14145           {
14146             unsigned int bytes_read;
14147             int line, file;
14148
14149             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14150             mac_ptr += bytes_read;
14151             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14152             mac_ptr += bytes_read;
14153
14154             current_file = macro_start_file (file, line, current_file,
14155                                              comp_dir, lh, cu->objfile);
14156           }
14157           break;
14158
14159         case DW_MACINFO_end_file:
14160           /* No data to skip by MAC_PTR.  */
14161           break;
14162
14163         case DW_MACINFO_vendor_ext:
14164           /* Only skip the data by MAC_PTR.  */
14165           {
14166             unsigned int bytes_read;
14167
14168             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14169             mac_ptr += bytes_read;
14170             read_direct_string (abfd, mac_ptr, &bytes_read);
14171             mac_ptr += bytes_read;
14172           }
14173           break;
14174
14175         default:
14176           break;
14177         }
14178     } while (macinfo_type != 0 && current_file == NULL);
14179
14180   /* Second pass: Process all entries.
14181
14182      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14183      command-line macro definitions/undefinitions.  This flag is unset when we
14184      reach the first DW_MACINFO_start_file entry.  */
14185
14186   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14187
14188   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14189      GDB is still reading the definitions from command line.  First
14190      DW_MACINFO_start_file will need to be ignored as it was already executed
14191      to create CURRENT_FILE for the main source holding also the command line
14192      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14193      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14194
14195   at_commandline = 1;
14196
14197   do
14198     {
14199       /* Do we at least have room for a macinfo type byte?  */
14200       if (mac_ptr >= mac_end)
14201         {
14202           dwarf2_macros_too_long_complaint ();
14203           break;
14204         }
14205
14206       macinfo_type = read_1_byte (abfd, mac_ptr);
14207       mac_ptr++;
14208
14209       switch (macinfo_type)
14210         {
14211           /* A zero macinfo type indicates the end of the macro
14212              information.  */
14213         case 0:
14214           break;
14215
14216         case DW_MACINFO_define:
14217         case DW_MACINFO_undef:
14218           {
14219             unsigned int bytes_read;
14220             int line;
14221             char *body;
14222
14223             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14224             mac_ptr += bytes_read;
14225             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14226             mac_ptr += bytes_read;
14227
14228             if (! current_file)
14229               {
14230                 /* DWARF violation as no main source is present.  */
14231                 complaint (&symfile_complaints,
14232                            _("debug info with no main source gives macro %s "
14233                              "on line %d: %s"),
14234                            macinfo_type == DW_MACINFO_define ?
14235                              _("definition") :
14236                                macinfo_type == DW_MACINFO_undef ?
14237                                  _("undefinition") :
14238                                  _("something-or-other"), line, body);
14239                 break;
14240               }
14241             if ((line == 0 && !at_commandline)
14242                 || (line != 0 && at_commandline))
14243               complaint (&symfile_complaints,
14244                          _("debug info gives %s macro %s with %s line %d: %s"),
14245                          at_commandline ? _("command-line") : _("in-file"),
14246                          macinfo_type == DW_MACINFO_define ?
14247                            _("definition") :
14248                              macinfo_type == DW_MACINFO_undef ?
14249                                _("undefinition") :
14250                                _("something-or-other"),
14251                          line == 0 ? _("zero") : _("non-zero"), line, body);
14252
14253             if (macinfo_type == DW_MACINFO_define)
14254               parse_macro_definition (current_file, line, body);
14255             else if (macinfo_type == DW_MACINFO_undef)
14256               macro_undef (current_file, line, body);
14257           }
14258           break;
14259
14260         case DW_MACINFO_start_file:
14261           {
14262             unsigned int bytes_read;
14263             int line, file;
14264
14265             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14266             mac_ptr += bytes_read;
14267             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14268             mac_ptr += bytes_read;
14269
14270             if ((line == 0 && !at_commandline)
14271                 || (line != 0 && at_commandline))
14272               complaint (&symfile_complaints,
14273                          _("debug info gives source %d included "
14274                            "from %s at %s line %d"),
14275                          file, at_commandline ? _("command-line") : _("file"),
14276                          line == 0 ? _("zero") : _("non-zero"), line);
14277
14278             if (at_commandline)
14279               {
14280                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14281                 at_commandline = 0;
14282               }
14283             else
14284               current_file = macro_start_file (file, line,
14285                                                current_file, comp_dir,
14286                                                lh, cu->objfile);
14287           }
14288           break;
14289
14290         case DW_MACINFO_end_file:
14291           if (! current_file)
14292             complaint (&symfile_complaints,
14293                        _("macro debug info has an unmatched "
14294                          "`close_file' directive"));
14295           else
14296             {
14297               current_file = current_file->included_by;
14298               if (! current_file)
14299                 {
14300                   enum dwarf_macinfo_record_type next_type;
14301
14302                   /* GCC circa March 2002 doesn't produce the zero
14303                      type byte marking the end of the compilation
14304                      unit.  Complain if it's not there, but exit no
14305                      matter what.  */
14306
14307                   /* Do we at least have room for a macinfo type byte?  */
14308                   if (mac_ptr >= mac_end)
14309                     {
14310                       dwarf2_macros_too_long_complaint ();
14311                       return;
14312                     }
14313
14314                   /* We don't increment mac_ptr here, so this is just
14315                      a look-ahead.  */
14316                   next_type = read_1_byte (abfd, mac_ptr);
14317                   if (next_type != 0)
14318                     complaint (&symfile_complaints,
14319                                _("no terminating 0-type entry for "
14320                                  "macros in `.debug_macinfo' section"));
14321
14322                   return;
14323                 }
14324             }
14325           break;
14326
14327         case DW_MACINFO_vendor_ext:
14328           {
14329             unsigned int bytes_read;
14330             int constant;
14331
14332             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14333             mac_ptr += bytes_read;
14334             read_direct_string (abfd, mac_ptr, &bytes_read);
14335             mac_ptr += bytes_read;
14336
14337             /* We don't recognize any vendor extensions.  */
14338           }
14339           break;
14340         }
14341     } while (macinfo_type != 0);
14342 }
14343
14344 /* Check if the attribute's form is a DW_FORM_block*
14345    if so return true else false.  */
14346 static int
14347 attr_form_is_block (struct attribute *attr)
14348 {
14349   return (attr == NULL ? 0 :
14350       attr->form == DW_FORM_block1
14351       || attr->form == DW_FORM_block2
14352       || attr->form == DW_FORM_block4
14353       || attr->form == DW_FORM_block
14354       || attr->form == DW_FORM_exprloc);
14355 }
14356
14357 /* Return non-zero if ATTR's value is a section offset --- classes
14358    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14359    You may use DW_UNSND (attr) to retrieve such offsets.
14360
14361    Section 7.5.4, "Attribute Encodings", explains that no attribute
14362    may have a value that belongs to more than one of these classes; it
14363    would be ambiguous if we did, because we use the same forms for all
14364    of them.  */
14365 static int
14366 attr_form_is_section_offset (struct attribute *attr)
14367 {
14368   return (attr->form == DW_FORM_data4
14369           || attr->form == DW_FORM_data8
14370           || attr->form == DW_FORM_sec_offset);
14371 }
14372
14373
14374 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14375    zero otherwise.  When this function returns true, you can apply
14376    dwarf2_get_attr_constant_value to it.
14377
14378    However, note that for some attributes you must check
14379    attr_form_is_section_offset before using this test.  DW_FORM_data4
14380    and DW_FORM_data8 are members of both the constant class, and of
14381    the classes that contain offsets into other debug sections
14382    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14383    that, if an attribute's can be either a constant or one of the
14384    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14385    taken as section offsets, not constants.  */
14386 static int
14387 attr_form_is_constant (struct attribute *attr)
14388 {
14389   switch (attr->form)
14390     {
14391     case DW_FORM_sdata:
14392     case DW_FORM_udata:
14393     case DW_FORM_data1:
14394     case DW_FORM_data2:
14395     case DW_FORM_data4:
14396     case DW_FORM_data8:
14397       return 1;
14398     default:
14399       return 0;
14400     }
14401 }
14402
14403 /* A helper function that fills in a dwarf2_loclist_baton.  */
14404
14405 static void
14406 fill_in_loclist_baton (struct dwarf2_cu *cu,
14407                        struct dwarf2_loclist_baton *baton,
14408                        struct attribute *attr)
14409 {
14410   dwarf2_read_section (dwarf2_per_objfile->objfile,
14411                        &dwarf2_per_objfile->loc);
14412
14413   baton->per_cu = cu->per_cu;
14414   gdb_assert (baton->per_cu);
14415   /* We don't know how long the location list is, but make sure we
14416      don't run off the edge of the section.  */
14417   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14418   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14419   baton->base_address = cu->base_address;
14420 }
14421
14422 static void
14423 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14424                              struct dwarf2_cu *cu)
14425 {
14426   if (attr_form_is_section_offset (attr)
14427       /* ".debug_loc" may not exist at all, or the offset may be outside
14428          the section.  If so, fall through to the complaint in the
14429          other branch.  */
14430       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14431                                                 &dwarf2_per_objfile->loc))
14432     {
14433       struct dwarf2_loclist_baton *baton;
14434
14435       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14436                              sizeof (struct dwarf2_loclist_baton));
14437
14438       fill_in_loclist_baton (cu, baton, attr);
14439
14440       if (cu->base_known == 0)
14441         complaint (&symfile_complaints,
14442                    _("Location list used without "
14443                      "specifying the CU base address."));
14444
14445       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14446       SYMBOL_LOCATION_BATON (sym) = baton;
14447     }
14448   else
14449     {
14450       struct dwarf2_locexpr_baton *baton;
14451
14452       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14453                              sizeof (struct dwarf2_locexpr_baton));
14454       baton->per_cu = cu->per_cu;
14455       gdb_assert (baton->per_cu);
14456
14457       if (attr_form_is_block (attr))
14458         {
14459           /* Note that we're just copying the block's data pointer
14460              here, not the actual data.  We're still pointing into the
14461              info_buffer for SYM's objfile; right now we never release
14462              that buffer, but when we do clean up properly this may
14463              need to change.  */
14464           baton->size = DW_BLOCK (attr)->size;
14465           baton->data = DW_BLOCK (attr)->data;
14466         }
14467       else
14468         {
14469           dwarf2_invalid_attrib_class_complaint ("location description",
14470                                                  SYMBOL_NATURAL_NAME (sym));
14471           baton->size = 0;
14472           baton->data = NULL;
14473         }
14474
14475       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14476       SYMBOL_LOCATION_BATON (sym) = baton;
14477     }
14478 }
14479
14480 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14481    came from a separate debuginfo file, then the master objfile is
14482    returned.  */
14483
14484 struct objfile *
14485 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14486 {
14487   struct objfile *objfile = per_cu->objfile;
14488
14489   /* Return the master objfile, so that we can report and look up the
14490      correct file containing this variable.  */
14491   if (objfile->separate_debug_objfile_backlink)
14492     objfile = objfile->separate_debug_objfile_backlink;
14493
14494   return objfile;
14495 }
14496
14497 /* Return the address size given in the compilation unit header for CU.  */
14498
14499 CORE_ADDR
14500 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14501 {
14502   if (per_cu->cu)
14503     return per_cu->cu->header.addr_size;
14504   else
14505     {
14506       /* If the CU is not currently read in, we re-read its header.  */
14507       struct objfile *objfile = per_cu->objfile;
14508       struct dwarf2_per_objfile *per_objfile
14509         = objfile_data (objfile, dwarf2_objfile_data_key);
14510       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14511       struct comp_unit_head cu_header;
14512
14513       memset (&cu_header, 0, sizeof cu_header);
14514       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14515       return cu_header.addr_size;
14516     }
14517 }
14518
14519 /* Return the offset size given in the compilation unit header for CU.  */
14520
14521 int
14522 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14523 {
14524   if (per_cu->cu)
14525     return per_cu->cu->header.offset_size;
14526   else
14527     {
14528       /* If the CU is not currently read in, we re-read its header.  */
14529       struct objfile *objfile = per_cu->objfile;
14530       struct dwarf2_per_objfile *per_objfile
14531         = objfile_data (objfile, dwarf2_objfile_data_key);
14532       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14533       struct comp_unit_head cu_header;
14534
14535       memset (&cu_header, 0, sizeof cu_header);
14536       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14537       return cu_header.offset_size;
14538     }
14539 }
14540
14541 /* Return the text offset of the CU.  The returned offset comes from
14542    this CU's objfile.  If this objfile came from a separate debuginfo
14543    file, then the offset may be different from the corresponding
14544    offset in the parent objfile.  */
14545
14546 CORE_ADDR
14547 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14548 {
14549   struct objfile *objfile = per_cu->objfile;
14550
14551   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14552 }
14553
14554 /* Locate the .debug_info compilation unit from CU's objfile which contains
14555    the DIE at OFFSET.  Raises an error on failure.  */
14556
14557 static struct dwarf2_per_cu_data *
14558 dwarf2_find_containing_comp_unit (unsigned int offset,
14559                                   struct objfile *objfile)
14560 {
14561   struct dwarf2_per_cu_data *this_cu;
14562   int low, high;
14563
14564   low = 0;
14565   high = dwarf2_per_objfile->n_comp_units - 1;
14566   while (high > low)
14567     {
14568       int mid = low + (high - low) / 2;
14569
14570       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14571         high = mid;
14572       else
14573         low = mid + 1;
14574     }
14575   gdb_assert (low == high);
14576   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14577     {
14578       if (low == 0)
14579         error (_("Dwarf Error: could not find partial DIE containing "
14580                "offset 0x%lx [in module %s]"),
14581                (long) offset, bfd_get_filename (objfile->obfd));
14582
14583       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14584       return dwarf2_per_objfile->all_comp_units[low-1];
14585     }
14586   else
14587     {
14588       this_cu = dwarf2_per_objfile->all_comp_units[low];
14589       if (low == dwarf2_per_objfile->n_comp_units - 1
14590           && offset >= this_cu->offset + this_cu->length)
14591         error (_("invalid dwarf2 offset %u"), offset);
14592       gdb_assert (offset < this_cu->offset + this_cu->length);
14593       return this_cu;
14594     }
14595 }
14596
14597 /* Locate the compilation unit from OBJFILE which is located at exactly
14598    OFFSET.  Raises an error on failure.  */
14599
14600 static struct dwarf2_per_cu_data *
14601 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14602 {
14603   struct dwarf2_per_cu_data *this_cu;
14604
14605   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14606   if (this_cu->offset != offset)
14607     error (_("no compilation unit with offset %u."), offset);
14608   return this_cu;
14609 }
14610
14611 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14612
14613 static void
14614 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14615 {
14616   memset (cu, 0, sizeof (*cu));
14617   cu->objfile = objfile;
14618   obstack_init (&cu->comp_unit_obstack);
14619 }
14620
14621 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14622
14623 static void
14624 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14625 {
14626   struct attribute *attr;
14627
14628   /* Set the language we're debugging.  */
14629   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14630   if (attr)
14631     set_cu_language (DW_UNSND (attr), cu);
14632   else
14633     set_cu_language (language_minimal, cu);
14634 }
14635
14636 /* Release one cached compilation unit, CU.  We unlink it from the tree
14637    of compilation units, but we don't remove it from the read_in_chain;
14638    the caller is responsible for that.
14639    NOTE: DATA is a void * because this function is also used as a
14640    cleanup routine.  */
14641
14642 static void
14643 free_one_comp_unit (void *data)
14644 {
14645   struct dwarf2_cu *cu = data;
14646
14647   if (cu->per_cu != NULL)
14648     cu->per_cu->cu = NULL;
14649   cu->per_cu = NULL;
14650
14651   obstack_free (&cu->comp_unit_obstack, NULL);
14652
14653   xfree (cu);
14654 }
14655
14656 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14657    when we're finished with it.  We can't free the pointer itself, but be
14658    sure to unlink it from the cache.  Also release any associated storage
14659    and perform cache maintenance.
14660
14661    Only used during partial symbol parsing.  */
14662
14663 static void
14664 free_stack_comp_unit (void *data)
14665 {
14666   struct dwarf2_cu *cu = data;
14667
14668   obstack_free (&cu->comp_unit_obstack, NULL);
14669   cu->partial_dies = NULL;
14670
14671   if (cu->per_cu != NULL)
14672     {
14673       /* This compilation unit is on the stack in our caller, so we
14674          should not xfree it.  Just unlink it.  */
14675       cu->per_cu->cu = NULL;
14676       cu->per_cu = NULL;
14677
14678       /* If we had a per-cu pointer, then we may have other compilation
14679          units loaded, so age them now.  */
14680       age_cached_comp_units ();
14681     }
14682 }
14683
14684 /* Free all cached compilation units.  */
14685
14686 static void
14687 free_cached_comp_units (void *data)
14688 {
14689   struct dwarf2_per_cu_data *per_cu, **last_chain;
14690
14691   per_cu = dwarf2_per_objfile->read_in_chain;
14692   last_chain = &dwarf2_per_objfile->read_in_chain;
14693   while (per_cu != NULL)
14694     {
14695       struct dwarf2_per_cu_data *next_cu;
14696
14697       next_cu = per_cu->cu->read_in_chain;
14698
14699       free_one_comp_unit (per_cu->cu);
14700       *last_chain = next_cu;
14701
14702       per_cu = next_cu;
14703     }
14704 }
14705
14706 /* Increase the age counter on each cached compilation unit, and free
14707    any that are too old.  */
14708
14709 static void
14710 age_cached_comp_units (void)
14711 {
14712   struct dwarf2_per_cu_data *per_cu, **last_chain;
14713
14714   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14715   per_cu = dwarf2_per_objfile->read_in_chain;
14716   while (per_cu != NULL)
14717     {
14718       per_cu->cu->last_used ++;
14719       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14720         dwarf2_mark (per_cu->cu);
14721       per_cu = per_cu->cu->read_in_chain;
14722     }
14723
14724   per_cu = dwarf2_per_objfile->read_in_chain;
14725   last_chain = &dwarf2_per_objfile->read_in_chain;
14726   while (per_cu != NULL)
14727     {
14728       struct dwarf2_per_cu_data *next_cu;
14729
14730       next_cu = per_cu->cu->read_in_chain;
14731
14732       if (!per_cu->cu->mark)
14733         {
14734           free_one_comp_unit (per_cu->cu);
14735           *last_chain = next_cu;
14736         }
14737       else
14738         last_chain = &per_cu->cu->read_in_chain;
14739
14740       per_cu = next_cu;
14741     }
14742 }
14743
14744 /* Remove a single compilation unit from the cache.  */
14745
14746 static void
14747 free_one_cached_comp_unit (void *target_cu)
14748 {
14749   struct dwarf2_per_cu_data *per_cu, **last_chain;
14750
14751   per_cu = dwarf2_per_objfile->read_in_chain;
14752   last_chain = &dwarf2_per_objfile->read_in_chain;
14753   while (per_cu != NULL)
14754     {
14755       struct dwarf2_per_cu_data *next_cu;
14756
14757       next_cu = per_cu->cu->read_in_chain;
14758
14759       if (per_cu->cu == target_cu)
14760         {
14761           free_one_comp_unit (per_cu->cu);
14762           *last_chain = next_cu;
14763           break;
14764         }
14765       else
14766         last_chain = &per_cu->cu->read_in_chain;
14767
14768       per_cu = next_cu;
14769     }
14770 }
14771
14772 /* Release all extra memory associated with OBJFILE.  */
14773
14774 void
14775 dwarf2_free_objfile (struct objfile *objfile)
14776 {
14777   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14778
14779   if (dwarf2_per_objfile == NULL)
14780     return;
14781
14782   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14783   free_cached_comp_units (NULL);
14784
14785   if (dwarf2_per_objfile->quick_file_names_table)
14786     htab_delete (dwarf2_per_objfile->quick_file_names_table);
14787
14788   /* Everything else should be on the objfile obstack.  */
14789 }
14790
14791 /* A pair of DIE offset and GDB type pointer.  We store these
14792    in a hash table separate from the DIEs, and preserve them
14793    when the DIEs are flushed out of cache.  */
14794
14795 struct dwarf2_offset_and_type
14796 {
14797   unsigned int offset;
14798   struct type *type;
14799 };
14800
14801 /* Hash function for a dwarf2_offset_and_type.  */
14802
14803 static hashval_t
14804 offset_and_type_hash (const void *item)
14805 {
14806   const struct dwarf2_offset_and_type *ofs = item;
14807
14808   return ofs->offset;
14809 }
14810
14811 /* Equality function for a dwarf2_offset_and_type.  */
14812
14813 static int
14814 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14815 {
14816   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14817   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14818
14819   return ofs_lhs->offset == ofs_rhs->offset;
14820 }
14821
14822 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14823    table if necessary.  For convenience, return TYPE.
14824
14825    The DIEs reading must have careful ordering to:
14826     * Not cause infite loops trying to read in DIEs as a prerequisite for
14827       reading current DIE.
14828     * Not trying to dereference contents of still incompletely read in types
14829       while reading in other DIEs.
14830     * Enable referencing still incompletely read in types just by a pointer to
14831       the type without accessing its fields.
14832
14833    Therefore caller should follow these rules:
14834      * Try to fetch any prerequisite types we may need to build this DIE type
14835        before building the type and calling set_die_type.
14836      * After building type call set_die_type for current DIE as soon as
14837        possible before fetching more types to complete the current type.
14838      * Make the type as complete as possible before fetching more types.  */
14839
14840 static struct type *
14841 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14842 {
14843   struct dwarf2_offset_and_type **slot, ofs;
14844   struct objfile *objfile = cu->objfile;
14845   htab_t *type_hash_ptr;
14846
14847   /* For Ada types, make sure that the gnat-specific data is always
14848      initialized (if not already set).  There are a few types where
14849      we should not be doing so, because the type-specific area is
14850      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14851      where the type-specific area is used to store the floatformat).
14852      But this is not a problem, because the gnat-specific information
14853      is actually not needed for these types.  */
14854   if (need_gnat_info (cu)
14855       && TYPE_CODE (type) != TYPE_CODE_FUNC
14856       && TYPE_CODE (type) != TYPE_CODE_FLT
14857       && !HAVE_GNAT_AUX_INFO (type))
14858     INIT_GNAT_SPECIFIC (type);
14859
14860   if (cu->per_cu->from_debug_types)
14861     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14862   else
14863     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14864
14865   if (*type_hash_ptr == NULL)
14866     {
14867       *type_hash_ptr
14868         = htab_create_alloc_ex (127,
14869                                 offset_and_type_hash,
14870                                 offset_and_type_eq,
14871                                 NULL,
14872                                 &objfile->objfile_obstack,
14873                                 hashtab_obstack_allocate,
14874                                 dummy_obstack_deallocate);
14875     }
14876
14877   ofs.offset = die->offset;
14878   ofs.type = type;
14879   slot = (struct dwarf2_offset_and_type **)
14880     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14881   if (*slot)
14882     complaint (&symfile_complaints,
14883                _("A problem internal to GDB: DIE 0x%x has type already set"),
14884                die->offset);
14885   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14886   **slot = ofs;
14887   return type;
14888 }
14889
14890 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14891    table, or return NULL if the die does not have a saved type.  */
14892
14893 static struct type *
14894 get_die_type_at_offset (unsigned int offset,
14895                         struct dwarf2_per_cu_data *per_cu)
14896 {
14897   struct dwarf2_offset_and_type *slot, ofs;
14898   htab_t type_hash;
14899
14900   if (per_cu->from_debug_types)
14901     type_hash = dwarf2_per_objfile->debug_types_type_hash;
14902   else
14903     type_hash = dwarf2_per_objfile->debug_info_type_hash;
14904   if (type_hash == NULL)
14905     return NULL;
14906
14907   ofs.offset = offset;
14908   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14909   if (slot)
14910     return slot->type;
14911   else
14912     return NULL;
14913 }
14914
14915 /* Look up the type for DIE in the appropriate type_hash table,
14916    or return NULL if DIE does not have a saved type.  */
14917
14918 static struct type *
14919 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14920 {
14921   return get_die_type_at_offset (die->offset, cu->per_cu);
14922 }
14923
14924 /* Add a dependence relationship from CU to REF_PER_CU.  */
14925
14926 static void
14927 dwarf2_add_dependence (struct dwarf2_cu *cu,
14928                        struct dwarf2_per_cu_data *ref_per_cu)
14929 {
14930   void **slot;
14931
14932   if (cu->dependencies == NULL)
14933     cu->dependencies
14934       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14935                               NULL, &cu->comp_unit_obstack,
14936                               hashtab_obstack_allocate,
14937                               dummy_obstack_deallocate);
14938
14939   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14940   if (*slot == NULL)
14941     *slot = ref_per_cu;
14942 }
14943
14944 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14945    Set the mark field in every compilation unit in the
14946    cache that we must keep because we are keeping CU.  */
14947
14948 static int
14949 dwarf2_mark_helper (void **slot, void *data)
14950 {
14951   struct dwarf2_per_cu_data *per_cu;
14952
14953   per_cu = (struct dwarf2_per_cu_data *) *slot;
14954   if (per_cu->cu->mark)
14955     return 1;
14956   per_cu->cu->mark = 1;
14957
14958   if (per_cu->cu->dependencies != NULL)
14959     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14960
14961   return 1;
14962 }
14963
14964 /* Set the mark field in CU and in every other compilation unit in the
14965    cache that we must keep because we are keeping CU.  */
14966
14967 static void
14968 dwarf2_mark (struct dwarf2_cu *cu)
14969 {
14970   if (cu->mark)
14971     return;
14972   cu->mark = 1;
14973   if (cu->dependencies != NULL)
14974     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14975 }
14976
14977 static void
14978 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14979 {
14980   while (per_cu)
14981     {
14982       per_cu->cu->mark = 0;
14983       per_cu = per_cu->cu->read_in_chain;
14984     }
14985 }
14986
14987 /* Trivial hash function for partial_die_info: the hash value of a DIE
14988    is its offset in .debug_info for this objfile.  */
14989
14990 static hashval_t
14991 partial_die_hash (const void *item)
14992 {
14993   const struct partial_die_info *part_die = item;
14994
14995   return part_die->offset;
14996 }
14997
14998 /* Trivial comparison function for partial_die_info structures: two DIEs
14999    are equal if they have the same offset.  */
15000
15001 static int
15002 partial_die_eq (const void *item_lhs, const void *item_rhs)
15003 {
15004   const struct partial_die_info *part_die_lhs = item_lhs;
15005   const struct partial_die_info *part_die_rhs = item_rhs;
15006
15007   return part_die_lhs->offset == part_die_rhs->offset;
15008 }
15009
15010 static struct cmd_list_element *set_dwarf2_cmdlist;
15011 static struct cmd_list_element *show_dwarf2_cmdlist;
15012
15013 static void
15014 set_dwarf2_cmd (char *args, int from_tty)
15015 {
15016   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15017 }
15018
15019 static void
15020 show_dwarf2_cmd (char *args, int from_tty)
15021 {
15022   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15023 }
15024
15025 /* If section described by INFO was mmapped, munmap it now.  */
15026
15027 static void
15028 munmap_section_buffer (struct dwarf2_section_info *info)
15029 {
15030   if (info->was_mmapped)
15031     {
15032 #ifdef HAVE_MMAP
15033       intptr_t begin = (intptr_t) info->buffer;
15034       intptr_t map_begin = begin & ~(pagesize - 1);
15035       size_t map_length = info->size + begin - map_begin;
15036
15037       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15038 #else
15039       /* Without HAVE_MMAP, we should never be here to begin with.  */
15040       gdb_assert_not_reached ("no mmap support");
15041 #endif
15042     }
15043 }
15044
15045 /* munmap debug sections for OBJFILE, if necessary.  */
15046
15047 static void
15048 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15049 {
15050   struct dwarf2_per_objfile *data = d;
15051
15052   /* This is sorted according to the order they're defined in to make it easier
15053      to keep in sync.  */
15054   munmap_section_buffer (&data->info);
15055   munmap_section_buffer (&data->abbrev);
15056   munmap_section_buffer (&data->line);
15057   munmap_section_buffer (&data->loc);
15058   munmap_section_buffer (&data->macinfo);
15059   munmap_section_buffer (&data->str);
15060   munmap_section_buffer (&data->ranges);
15061   munmap_section_buffer (&data->types);
15062   munmap_section_buffer (&data->frame);
15063   munmap_section_buffer (&data->eh_frame);
15064   munmap_section_buffer (&data->gdb_index);
15065 }
15066
15067 \f
15068 /* The "save gdb-index" command.  */
15069
15070 /* The contents of the hash table we create when building the string
15071    table.  */
15072 struct strtab_entry
15073 {
15074   offset_type offset;
15075   const char *str;
15076 };
15077
15078 /* Hash function for a strtab_entry.  */
15079
15080 static hashval_t
15081 hash_strtab_entry (const void *e)
15082 {
15083   const struct strtab_entry *entry = e;
15084   return mapped_index_string_hash (entry->str);
15085 }
15086
15087 /* Equality function for a strtab_entry.  */
15088
15089 static int
15090 eq_strtab_entry (const void *a, const void *b)
15091 {
15092   const struct strtab_entry *ea = a;
15093   const struct strtab_entry *eb = b;
15094   return !strcmp (ea->str, eb->str);
15095 }
15096
15097 /* Create a strtab_entry hash table.  */
15098
15099 static htab_t
15100 create_strtab (void)
15101 {
15102   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15103                             xfree, xcalloc, xfree);
15104 }
15105
15106 /* Add a string to the constant pool.  Return the string's offset in
15107    host order.  */
15108
15109 static offset_type
15110 add_string (htab_t table, struct obstack *cpool, const char *str)
15111 {
15112   void **slot;
15113   struct strtab_entry entry;
15114   struct strtab_entry *result;
15115
15116   entry.str = str;
15117   slot = htab_find_slot (table, &entry, INSERT);
15118   if (*slot)
15119     result = *slot;
15120   else
15121     {
15122       result = XNEW (struct strtab_entry);
15123       result->offset = obstack_object_size (cpool);
15124       result->str = str;
15125       obstack_grow_str0 (cpool, str);
15126       *slot = result;
15127     }
15128   return result->offset;
15129 }
15130
15131 /* An entry in the symbol table.  */
15132 struct symtab_index_entry
15133 {
15134   /* The name of the symbol.  */
15135   const char *name;
15136   /* The offset of the name in the constant pool.  */
15137   offset_type index_offset;
15138   /* A sorted vector of the indices of all the CUs that hold an object
15139      of this name.  */
15140   VEC (offset_type) *cu_indices;
15141 };
15142
15143 /* The symbol table.  This is a power-of-2-sized hash table.  */
15144 struct mapped_symtab
15145 {
15146   offset_type n_elements;
15147   offset_type size;
15148   struct symtab_index_entry **data;
15149 };
15150
15151 /* Hash function for a symtab_index_entry.  */
15152
15153 static hashval_t
15154 hash_symtab_entry (const void *e)
15155 {
15156   const struct symtab_index_entry *entry = e;
15157   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15158                          sizeof (offset_type) * VEC_length (offset_type,
15159                                                             entry->cu_indices),
15160                          0);
15161 }
15162
15163 /* Equality function for a symtab_index_entry.  */
15164
15165 static int
15166 eq_symtab_entry (const void *a, const void *b)
15167 {
15168   const struct symtab_index_entry *ea = a;
15169   const struct symtab_index_entry *eb = b;
15170   int len = VEC_length (offset_type, ea->cu_indices);
15171   if (len != VEC_length (offset_type, eb->cu_indices))
15172     return 0;
15173   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15174                   VEC_address (offset_type, eb->cu_indices),
15175                   sizeof (offset_type) * len);
15176 }
15177
15178 /* Destroy a symtab_index_entry.  */
15179
15180 static void
15181 delete_symtab_entry (void *p)
15182 {
15183   struct symtab_index_entry *entry = p;
15184   VEC_free (offset_type, entry->cu_indices);
15185   xfree (entry);
15186 }
15187
15188 /* Create a hash table holding symtab_index_entry objects.  */
15189
15190 static htab_t
15191 create_symbol_hash_table (void)
15192 {
15193   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15194                             delete_symtab_entry, xcalloc, xfree);
15195 }
15196
15197 /* Create a new mapped symtab object.  */
15198
15199 static struct mapped_symtab *
15200 create_mapped_symtab (void)
15201 {
15202   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15203   symtab->n_elements = 0;
15204   symtab->size = 1024;
15205   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15206   return symtab;
15207 }
15208
15209 /* Destroy a mapped_symtab.  */
15210
15211 static void
15212 cleanup_mapped_symtab (void *p)
15213 {
15214   struct mapped_symtab *symtab = p;
15215   /* The contents of the array are freed when the other hash table is
15216      destroyed.  */
15217   xfree (symtab->data);
15218   xfree (symtab);
15219 }
15220
15221 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15222    the slot.  */
15223
15224 static struct symtab_index_entry **
15225 find_slot (struct mapped_symtab *symtab, const char *name)
15226 {
15227   offset_type index, step, hash = mapped_index_string_hash (name);
15228
15229   index = hash & (symtab->size - 1);
15230   step = ((hash * 17) & (symtab->size - 1)) | 1;
15231
15232   for (;;)
15233     {
15234       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15235         return &symtab->data[index];
15236       index = (index + step) & (symtab->size - 1);
15237     }
15238 }
15239
15240 /* Expand SYMTAB's hash table.  */
15241
15242 static void
15243 hash_expand (struct mapped_symtab *symtab)
15244 {
15245   offset_type old_size = symtab->size;
15246   offset_type i;
15247   struct symtab_index_entry **old_entries = symtab->data;
15248
15249   symtab->size *= 2;
15250   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15251
15252   for (i = 0; i < old_size; ++i)
15253     {
15254       if (old_entries[i])
15255         {
15256           struct symtab_index_entry **slot = find_slot (symtab,
15257                                                         old_entries[i]->name);
15258           *slot = old_entries[i];
15259         }
15260     }
15261
15262   xfree (old_entries);
15263 }
15264
15265 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15266    is the index of the CU in which the symbol appears.  */
15267
15268 static void
15269 add_index_entry (struct mapped_symtab *symtab, const char *name,
15270                  offset_type cu_index)
15271 {
15272   struct symtab_index_entry **slot;
15273
15274   ++symtab->n_elements;
15275   if (4 * symtab->n_elements / 3 >= symtab->size)
15276     hash_expand (symtab);
15277
15278   slot = find_slot (symtab, name);
15279   if (!*slot)
15280     {
15281       *slot = XNEW (struct symtab_index_entry);
15282       (*slot)->name = name;
15283       (*slot)->cu_indices = NULL;
15284     }
15285   /* Don't push an index twice.  Due to how we add entries we only
15286      have to check the last one.  */ 
15287   if (VEC_empty (offset_type, (*slot)->cu_indices)
15288       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15289     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15290 }
15291
15292 /* Add a vector of indices to the constant pool.  */
15293
15294 static offset_type
15295 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15296                       struct symtab_index_entry *entry)
15297 {
15298   void **slot;
15299
15300   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15301   if (!*slot)
15302     {
15303       offset_type len = VEC_length (offset_type, entry->cu_indices);
15304       offset_type val = MAYBE_SWAP (len);
15305       offset_type iter;
15306       int i;
15307
15308       *slot = entry;
15309       entry->index_offset = obstack_object_size (cpool);
15310
15311       obstack_grow (cpool, &val, sizeof (val));
15312       for (i = 0;
15313            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15314            ++i)
15315         {
15316           val = MAYBE_SWAP (iter);
15317           obstack_grow (cpool, &val, sizeof (val));
15318         }
15319     }
15320   else
15321     {
15322       struct symtab_index_entry *old_entry = *slot;
15323       entry->index_offset = old_entry->index_offset;
15324       entry = old_entry;
15325     }
15326   return entry->index_offset;
15327 }
15328
15329 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15330    constant pool entries going into the obstack CPOOL.  */
15331
15332 static void
15333 write_hash_table (struct mapped_symtab *symtab,
15334                   struct obstack *output, struct obstack *cpool)
15335 {
15336   offset_type i;
15337   htab_t symbol_hash_table;
15338   htab_t str_table;
15339
15340   symbol_hash_table = create_symbol_hash_table ();
15341   str_table = create_strtab ();
15342
15343   /* We add all the index vectors to the constant pool first, to
15344      ensure alignment is ok.  */
15345   for (i = 0; i < symtab->size; ++i)
15346     {
15347       if (symtab->data[i])
15348         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15349     }
15350
15351   /* Now write out the hash table.  */
15352   for (i = 0; i < symtab->size; ++i)
15353     {
15354       offset_type str_off, vec_off;
15355
15356       if (symtab->data[i])
15357         {
15358           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15359           vec_off = symtab->data[i]->index_offset;
15360         }
15361       else
15362         {
15363           /* While 0 is a valid constant pool index, it is not valid
15364              to have 0 for both offsets.  */
15365           str_off = 0;
15366           vec_off = 0;
15367         }
15368
15369       str_off = MAYBE_SWAP (str_off);
15370       vec_off = MAYBE_SWAP (vec_off);
15371
15372       obstack_grow (output, &str_off, sizeof (str_off));
15373       obstack_grow (output, &vec_off, sizeof (vec_off));
15374     }
15375
15376   htab_delete (str_table);
15377   htab_delete (symbol_hash_table);
15378 }
15379
15380 /* Struct to map psymtab to CU index in the index file.  */
15381 struct psymtab_cu_index_map
15382 {
15383   struct partial_symtab *psymtab;
15384   unsigned int cu_index;
15385 };
15386
15387 static hashval_t
15388 hash_psymtab_cu_index (const void *item)
15389 {
15390   const struct psymtab_cu_index_map *map = item;
15391
15392   return htab_hash_pointer (map->psymtab);
15393 }
15394
15395 static int
15396 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15397 {
15398   const struct psymtab_cu_index_map *lhs = item_lhs;
15399   const struct psymtab_cu_index_map *rhs = item_rhs;
15400
15401   return lhs->psymtab == rhs->psymtab;
15402 }
15403
15404 /* Helper struct for building the address table.  */
15405 struct addrmap_index_data
15406 {
15407   struct objfile *objfile;
15408   struct obstack *addr_obstack;
15409   htab_t cu_index_htab;
15410
15411   /* Non-zero if the previous_* fields are valid.
15412      We can't write an entry until we see the next entry (since it is only then
15413      that we know the end of the entry).  */
15414   int previous_valid;
15415   /* Index of the CU in the table of all CUs in the index file.  */
15416   unsigned int previous_cu_index;
15417   /* Start address of the CU.  */
15418   CORE_ADDR previous_cu_start;
15419 };
15420
15421 /* Write an address entry to OBSTACK.  */
15422
15423 static void
15424 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15425                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15426 {
15427   offset_type cu_index_to_write;
15428   char addr[8];
15429   CORE_ADDR baseaddr;
15430
15431   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15432
15433   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15434   obstack_grow (obstack, addr, 8);
15435   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15436   obstack_grow (obstack, addr, 8);
15437   cu_index_to_write = MAYBE_SWAP (cu_index);
15438   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15439 }
15440
15441 /* Worker function for traversing an addrmap to build the address table.  */
15442
15443 static int
15444 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15445 {
15446   struct addrmap_index_data *data = datap;
15447   struct partial_symtab *pst = obj;
15448   offset_type cu_index;
15449   void **slot;
15450
15451   if (data->previous_valid)
15452     add_address_entry (data->objfile, data->addr_obstack,
15453                        data->previous_cu_start, start_addr,
15454                        data->previous_cu_index);
15455
15456   data->previous_cu_start = start_addr;
15457   if (pst != NULL)
15458     {
15459       struct psymtab_cu_index_map find_map, *map;
15460       find_map.psymtab = pst;
15461       map = htab_find (data->cu_index_htab, &find_map);
15462       gdb_assert (map != NULL);
15463       data->previous_cu_index = map->cu_index;
15464       data->previous_valid = 1;
15465     }
15466   else
15467       data->previous_valid = 0;
15468
15469   return 0;
15470 }
15471
15472 /* Write OBJFILE's address map to OBSTACK.
15473    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15474    in the index file.  */
15475
15476 static void
15477 write_address_map (struct objfile *objfile, struct obstack *obstack,
15478                    htab_t cu_index_htab)
15479 {
15480   struct addrmap_index_data addrmap_index_data;
15481
15482   /* When writing the address table, we have to cope with the fact that
15483      the addrmap iterator only provides the start of a region; we have to
15484      wait until the next invocation to get the start of the next region.  */
15485
15486   addrmap_index_data.objfile = objfile;
15487   addrmap_index_data.addr_obstack = obstack;
15488   addrmap_index_data.cu_index_htab = cu_index_htab;
15489   addrmap_index_data.previous_valid = 0;
15490
15491   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15492                    &addrmap_index_data);
15493
15494   /* It's highly unlikely the last entry (end address = 0xff...ff)
15495      is valid, but we should still handle it.
15496      The end address is recorded as the start of the next region, but that
15497      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15498      anyway.  */
15499   if (addrmap_index_data.previous_valid)
15500     add_address_entry (objfile, obstack,
15501                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15502                        addrmap_index_data.previous_cu_index);
15503 }
15504
15505 /* Add a list of partial symbols to SYMTAB.  */
15506
15507 static void
15508 write_psymbols (struct mapped_symtab *symtab,
15509                 htab_t psyms_seen,
15510                 struct partial_symbol **psymp,
15511                 int count,
15512                 offset_type cu_index,
15513                 int is_static)
15514 {
15515   for (; count-- > 0; ++psymp)
15516     {
15517       void **slot, *lookup;
15518
15519       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15520         error (_("Ada is not currently supported by the index"));
15521
15522       /* We only want to add a given psymbol once.  However, we also
15523          want to account for whether it is global or static.  So, we
15524          may add it twice, using slightly different values.  */
15525       if (is_static)
15526         {
15527           uintptr_t val = 1 | (uintptr_t) *psymp;
15528
15529           lookup = (void *) val;
15530         }
15531       else
15532         lookup = *psymp;
15533
15534       /* Only add a given psymbol once.  */
15535       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15536       if (!*slot)
15537         {
15538           *slot = lookup;
15539           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15540         }
15541     }
15542 }
15543
15544 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15545    exception if there is an error.  */
15546
15547 static void
15548 write_obstack (FILE *file, struct obstack *obstack)
15549 {
15550   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15551               file)
15552       != obstack_object_size (obstack))
15553     error (_("couldn't data write to file"));
15554 }
15555
15556 /* Unlink a file if the argument is not NULL.  */
15557
15558 static void
15559 unlink_if_set (void *p)
15560 {
15561   char **filename = p;
15562   if (*filename)
15563     unlink (*filename);
15564 }
15565
15566 /* A helper struct used when iterating over debug_types.  */
15567 struct signatured_type_index_data
15568 {
15569   struct objfile *objfile;
15570   struct mapped_symtab *symtab;
15571   struct obstack *types_list;
15572   htab_t psyms_seen;
15573   int cu_index;
15574 };
15575
15576 /* A helper function that writes a single signatured_type to an
15577    obstack.  */
15578
15579 static int
15580 write_one_signatured_type (void **slot, void *d)
15581 {
15582   struct signatured_type_index_data *info = d;
15583   struct signatured_type *entry = (struct signatured_type *) *slot;
15584   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15585   struct partial_symtab *psymtab = per_cu->v.psymtab;
15586   gdb_byte val[8];
15587
15588   write_psymbols (info->symtab,
15589                   info->psyms_seen,
15590                   info->objfile->global_psymbols.list
15591                   + psymtab->globals_offset,
15592                   psymtab->n_global_syms, info->cu_index,
15593                   0);
15594   write_psymbols (info->symtab,
15595                   info->psyms_seen,
15596                   info->objfile->static_psymbols.list
15597                   + psymtab->statics_offset,
15598                   psymtab->n_static_syms, info->cu_index,
15599                   1);
15600
15601   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15602   obstack_grow (info->types_list, val, 8);
15603   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15604   obstack_grow (info->types_list, val, 8);
15605   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15606   obstack_grow (info->types_list, val, 8);
15607
15608   ++info->cu_index;
15609
15610   return 1;
15611 }
15612
15613 /* A cleanup function for an htab_t.  */
15614
15615 static void
15616 cleanup_htab (void *arg)
15617 {
15618   htab_delete (arg);
15619 }
15620
15621 /* Create an index file for OBJFILE in the directory DIR.  */
15622
15623 static void
15624 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15625 {
15626   struct cleanup *cleanup;
15627   char *filename, *cleanup_filename;
15628   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15629   struct obstack cu_list, types_cu_list;
15630   int i;
15631   FILE *out_file;
15632   struct mapped_symtab *symtab;
15633   offset_type val, size_of_contents, total_len;
15634   struct stat st;
15635   char buf[8];
15636   htab_t psyms_seen;
15637   htab_t cu_index_htab;
15638   struct psymtab_cu_index_map *psymtab_cu_index_map;
15639
15640   if (!objfile->psymtabs)
15641     return;
15642   if (dwarf2_per_objfile->using_index)
15643     error (_("Cannot use an index to create the index"));
15644
15645   if (stat (objfile->name, &st) < 0)
15646     perror_with_name (objfile->name);
15647
15648   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15649                      INDEX_SUFFIX, (char *) NULL);
15650   cleanup = make_cleanup (xfree, filename);
15651
15652   out_file = fopen (filename, "wb");
15653   if (!out_file)
15654     error (_("Can't open `%s' for writing"), filename);
15655
15656   cleanup_filename = filename;
15657   make_cleanup (unlink_if_set, &cleanup_filename);
15658
15659   symtab = create_mapped_symtab ();
15660   make_cleanup (cleanup_mapped_symtab, symtab);
15661
15662   obstack_init (&addr_obstack);
15663   make_cleanup_obstack_free (&addr_obstack);
15664
15665   obstack_init (&cu_list);
15666   make_cleanup_obstack_free (&cu_list);
15667
15668   obstack_init (&types_cu_list);
15669   make_cleanup_obstack_free (&types_cu_list);
15670
15671   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15672                                   NULL, xcalloc, xfree);
15673   make_cleanup (cleanup_htab, psyms_seen);
15674
15675   /* While we're scanning CU's create a table that maps a psymtab pointer
15676      (which is what addrmap records) to its index (which is what is recorded
15677      in the index file).  This will later be needed to write the address
15678      table.  */
15679   cu_index_htab = htab_create_alloc (100,
15680                                      hash_psymtab_cu_index,
15681                                      eq_psymtab_cu_index,
15682                                      NULL, xcalloc, xfree);
15683   make_cleanup (cleanup_htab, cu_index_htab);
15684   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15685     xmalloc (sizeof (struct psymtab_cu_index_map)
15686              * dwarf2_per_objfile->n_comp_units);
15687   make_cleanup (xfree, psymtab_cu_index_map);
15688
15689   /* The CU list is already sorted, so we don't need to do additional
15690      work here.  Also, the debug_types entries do not appear in
15691      all_comp_units, but only in their own hash table.  */
15692   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15693     {
15694       struct dwarf2_per_cu_data *per_cu
15695         = dwarf2_per_objfile->all_comp_units[i];
15696       struct partial_symtab *psymtab = per_cu->v.psymtab;
15697       gdb_byte val[8];
15698       struct psymtab_cu_index_map *map;
15699       void **slot;
15700
15701       write_psymbols (symtab,
15702                       psyms_seen,
15703                       objfile->global_psymbols.list + psymtab->globals_offset,
15704                       psymtab->n_global_syms, i,
15705                       0);
15706       write_psymbols (symtab,
15707                       psyms_seen,
15708                       objfile->static_psymbols.list + psymtab->statics_offset,
15709                       psymtab->n_static_syms, i,
15710                       1);
15711
15712       map = &psymtab_cu_index_map[i];
15713       map->psymtab = psymtab;
15714       map->cu_index = i;
15715       slot = htab_find_slot (cu_index_htab, map, INSERT);
15716       gdb_assert (slot != NULL);
15717       gdb_assert (*slot == NULL);
15718       *slot = map;
15719
15720       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15721       obstack_grow (&cu_list, val, 8);
15722       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15723       obstack_grow (&cu_list, val, 8);
15724     }
15725
15726   /* Dump the address map.  */
15727   write_address_map (objfile, &addr_obstack, cu_index_htab);
15728
15729   /* Write out the .debug_type entries, if any.  */
15730   if (dwarf2_per_objfile->signatured_types)
15731     {
15732       struct signatured_type_index_data sig_data;
15733
15734       sig_data.objfile = objfile;
15735       sig_data.symtab = symtab;
15736       sig_data.types_list = &types_cu_list;
15737       sig_data.psyms_seen = psyms_seen;
15738       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15739       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15740                               write_one_signatured_type, &sig_data);
15741     }
15742
15743   obstack_init (&constant_pool);
15744   make_cleanup_obstack_free (&constant_pool);
15745   obstack_init (&symtab_obstack);
15746   make_cleanup_obstack_free (&symtab_obstack);
15747   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15748
15749   obstack_init (&contents);
15750   make_cleanup_obstack_free (&contents);
15751   size_of_contents = 6 * sizeof (offset_type);
15752   total_len = size_of_contents;
15753
15754   /* The version number.  */
15755   val = MAYBE_SWAP (4);
15756   obstack_grow (&contents, &val, sizeof (val));
15757
15758   /* The offset of the CU list from the start of the file.  */
15759   val = MAYBE_SWAP (total_len);
15760   obstack_grow (&contents, &val, sizeof (val));
15761   total_len += obstack_object_size (&cu_list);
15762
15763   /* The offset of the types CU list from the start of the file.  */
15764   val = MAYBE_SWAP (total_len);
15765   obstack_grow (&contents, &val, sizeof (val));
15766   total_len += obstack_object_size (&types_cu_list);
15767
15768   /* The offset of the address table from the start of the file.  */
15769   val = MAYBE_SWAP (total_len);
15770   obstack_grow (&contents, &val, sizeof (val));
15771   total_len += obstack_object_size (&addr_obstack);
15772
15773   /* The offset of the symbol table from the start of the file.  */
15774   val = MAYBE_SWAP (total_len);
15775   obstack_grow (&contents, &val, sizeof (val));
15776   total_len += obstack_object_size (&symtab_obstack);
15777
15778   /* The offset of the constant pool from the start of the file.  */
15779   val = MAYBE_SWAP (total_len);
15780   obstack_grow (&contents, &val, sizeof (val));
15781   total_len += obstack_object_size (&constant_pool);
15782
15783   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15784
15785   write_obstack (out_file, &contents);
15786   write_obstack (out_file, &cu_list);
15787   write_obstack (out_file, &types_cu_list);
15788   write_obstack (out_file, &addr_obstack);
15789   write_obstack (out_file, &symtab_obstack);
15790   write_obstack (out_file, &constant_pool);
15791
15792   fclose (out_file);
15793
15794   /* We want to keep the file, so we set cleanup_filename to NULL
15795      here.  See unlink_if_set.  */
15796   cleanup_filename = NULL;
15797
15798   do_cleanups (cleanup);
15799 }
15800
15801 /* The mapped index file format is designed to be directly mmap()able
15802    on any architecture.  In most cases, a datum is represented using a
15803    little-endian 32-bit integer value, called an offset_type.  Big
15804    endian machines must byte-swap the values before using them.
15805    Exceptions to this rule are noted.  The data is laid out such that
15806    alignment is always respected.
15807
15808    A mapped index consists of several sections.
15809
15810    1. The file header.  This is a sequence of values, of offset_type
15811    unless otherwise noted:
15812
15813    [0] The version number, currently 4.  Versions 1, 2 and 3 are
15814    obsolete.
15815    [1] The offset, from the start of the file, of the CU list.
15816    [2] The offset, from the start of the file, of the types CU list.
15817    Note that this section can be empty, in which case this offset will
15818    be equal to the next offset.
15819    [3] The offset, from the start of the file, of the address section.
15820    [4] The offset, from the start of the file, of the symbol table.
15821    [5] The offset, from the start of the file, of the constant pool.
15822
15823    2. The CU list.  This is a sequence of pairs of 64-bit
15824    little-endian values, sorted by the CU offset.  The first element
15825    in each pair is the offset of a CU in the .debug_info section.  The
15826    second element in each pair is the length of that CU.  References
15827    to a CU elsewhere in the map are done using a CU index, which is
15828    just the 0-based index into this table.  Note that if there are
15829    type CUs, then conceptually CUs and type CUs form a single list for
15830    the purposes of CU indices.
15831
15832    3. The types CU list.  This is a sequence of triplets of 64-bit
15833    little-endian values.  In a triplet, the first value is the CU
15834    offset, the second value is the type offset in the CU, and the
15835    third value is the type signature.  The types CU list is not
15836    sorted.
15837
15838    4. The address section.  The address section consists of a sequence
15839    of address entries.  Each address entry has three elements.
15840    [0] The low address.  This is a 64-bit little-endian value.
15841    [1] The high address.  This is a 64-bit little-endian value.
15842        Like DW_AT_high_pc, the value is one byte beyond the end.
15843    [2] The CU index.  This is an offset_type value.
15844
15845    5. The symbol table.  This is a hash table.  The size of the hash
15846    table is always a power of 2.  The initial hash and the step are
15847    currently defined by the `find_slot' function.
15848
15849    Each slot in the hash table consists of a pair of offset_type
15850    values.  The first value is the offset of the symbol's name in the
15851    constant pool.  The second value is the offset of the CU vector in
15852    the constant pool.
15853
15854    If both values are 0, then this slot in the hash table is empty.
15855    This is ok because while 0 is a valid constant pool index, it
15856    cannot be a valid index for both a string and a CU vector.
15857
15858    A string in the constant pool is stored as a \0-terminated string,
15859    as you'd expect.
15860
15861    A CU vector in the constant pool is a sequence of offset_type
15862    values.  The first value is the number of CU indices in the vector.
15863    Each subsequent value is the index of a CU in the CU list.  This
15864    element in the hash table is used to indicate which CUs define the
15865    symbol.
15866
15867    6. The constant pool.  This is simply a bunch of bytes.  It is
15868    organized so that alignment is correct: CU vectors are stored
15869    first, followed by strings.  */
15870
15871 static void
15872 save_gdb_index_command (char *arg, int from_tty)
15873 {
15874   struct objfile *objfile;
15875
15876   if (!arg || !*arg)
15877     error (_("usage: save gdb-index DIRECTORY"));
15878
15879   ALL_OBJFILES (objfile)
15880   {
15881     struct stat st;
15882
15883     /* If the objfile does not correspond to an actual file, skip it.  */
15884     if (stat (objfile->name, &st) < 0)
15885       continue;
15886
15887     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15888     if (dwarf2_per_objfile)
15889       {
15890         volatile struct gdb_exception except;
15891
15892         TRY_CATCH (except, RETURN_MASK_ERROR)
15893           {
15894             write_psymtabs_to_index (objfile, arg);
15895           }
15896         if (except.reason < 0)
15897           exception_fprintf (gdb_stderr, except,
15898                              _("Error while writing index for `%s': "),
15899                              objfile->name);
15900       }
15901   }
15902 }
15903
15904 \f
15905
15906 int dwarf2_always_disassemble;
15907
15908 static void
15909 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15910                                 struct cmd_list_element *c, const char *value)
15911 {
15912   fprintf_filtered (file,
15913                     _("Whether to always disassemble "
15914                       "DWARF expressions is %s.\n"),
15915                     value);
15916 }
15917
15918 void _initialize_dwarf2_read (void);
15919
15920 void
15921 _initialize_dwarf2_read (void)
15922 {
15923   struct cmd_list_element *c;
15924
15925   dwarf2_objfile_data_key
15926     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15927
15928   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15929 Set DWARF 2 specific variables.\n\
15930 Configure DWARF 2 variables such as the cache size"),
15931                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15932                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15933
15934   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15935 Show DWARF 2 specific variables\n\
15936 Show DWARF 2 variables such as the cache size"),
15937                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15938                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15939
15940   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15941                             &dwarf2_max_cache_age, _("\
15942 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15943 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15944 A higher limit means that cached compilation units will be stored\n\
15945 in memory longer, and more total memory will be used.  Zero disables\n\
15946 caching, which can slow down startup."),
15947                             NULL,
15948                             show_dwarf2_max_cache_age,
15949                             &set_dwarf2_cmdlist,
15950                             &show_dwarf2_cmdlist);
15951
15952   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15953                            &dwarf2_always_disassemble, _("\
15954 Set whether `info address' always disassembles DWARF expressions."), _("\
15955 Show whether `info address' always disassembles DWARF expressions."), _("\
15956 When enabled, DWARF expressions are always printed in an assembly-like\n\
15957 syntax.  When disabled, expressions will be printed in a more\n\
15958 conversational style, when possible."),
15959                            NULL,
15960                            show_dwarf2_always_disassemble,
15961                            &set_dwarf2_cmdlist,
15962                            &show_dwarf2_cmdlist);
15963
15964   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15965 Set debugging of the dwarf2 DIE reader."), _("\
15966 Show debugging of the dwarf2 DIE reader."), _("\
15967 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15968 The value is the maximum depth to print."),
15969                             NULL,
15970                             NULL,
15971                             &setdebuglist, &showdebuglist);
15972
15973   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15974                _("\
15975 Save a gdb-index file.\n\
15976 Usage: save gdb-index DIRECTORY"),
15977                &save_cmdlist);
15978   set_cmd_completer (c, filename_completer);
15979 }