OSDN Git Service

add i18n markup in error message (utils.c:parse_escape)
[pf3gnuchains/sourceware.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
3195   init_one_comp_unit (&cu, objfile);
3196   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3197
3198   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3199                                           buffer, buffer_size,
3200                                           abfd);
3201
3202   /* Complete the cu_header.  */
3203   cu.header.offset = beg_of_comp_unit - buffer;
3204   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3205
3206   cu.list_in_scope = &file_symbols;
3207
3208   /* If this compilation unit was already read in, free the
3209      cached copy in order to read it in again.  This is
3210      necessary because we skipped some symbols when we first
3211      read in the compilation unit (see load_partial_dies).
3212      This problem could be avoided, but the benefit is
3213      unclear.  */
3214   if (this_cu->cu != NULL)
3215     free_one_cached_comp_unit (this_cu->cu);
3216
3217   /* Note that this is a pointer to our stack frame, being
3218      added to a global data structure.  It will be cleaned up
3219      in free_stack_comp_unit when we finish with this
3220      compilation unit.  */
3221   this_cu->cu = &cu;
3222   cu.per_cu = this_cu;
3223
3224   /* Read the abbrevs for this compilation unit into a table.  */
3225   dwarf2_read_abbrevs (abfd, &cu);
3226   make_cleanup (dwarf2_free_abbrev_table, &cu);
3227
3228   /* Read the compilation unit die.  */
3229   if (this_cu->from_debug_types)
3230     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3231   init_cu_die_reader (&reader_specs, &cu);
3232   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3233                             &has_children);
3234
3235   if (this_cu->from_debug_types)
3236     {
3237       /* offset,length haven't been set yet for type units.  */
3238       this_cu->offset = cu.header.offset;
3239       this_cu->length = cu.header.length + cu.header.initial_length_size;
3240     }
3241   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3242     {
3243       info_ptr = (beg_of_comp_unit + cu.header.length
3244                   + cu.header.initial_length_size);
3245       do_cleanups (back_to_inner);
3246       return info_ptr;
3247     }
3248
3249   prepare_one_comp_unit (&cu, comp_unit_die);
3250
3251   /* Allocate a new partial symbol table structure.  */
3252   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3253   pst = start_psymtab_common (objfile, objfile->section_offsets,
3254                               (attr != NULL) ? DW_STRING (attr) : "",
3255                               /* TEXTLOW and TEXTHIGH are set below.  */
3256                               0,
3257                               objfile->global_psymbols.next,
3258                               objfile->static_psymbols.next);
3259
3260   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3261   if (attr != NULL)
3262     pst->dirname = DW_STRING (attr);
3263
3264   pst->read_symtab_private = this_cu;
3265
3266   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3267
3268   /* Store the function that reads in the rest of the symbol table.  */
3269   pst->read_symtab = dwarf2_psymtab_to_symtab;
3270
3271   this_cu->v.psymtab = pst;
3272
3273   dwarf2_find_base_address (comp_unit_die, &cu);
3274
3275   /* Possibly set the default values of LOWPC and HIGHPC from
3276      `DW_AT_ranges'.  */
3277   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3278                                       &best_highpc, &cu, pst);
3279   if (has_pc_info == 1 && best_lowpc < best_highpc)
3280     /* Store the contiguous range if it is not empty; it can be empty for
3281        CUs with no code.  */
3282     addrmap_set_empty (objfile->psymtabs_addrmap,
3283                        best_lowpc + baseaddr,
3284                        best_highpc + baseaddr - 1, pst);
3285
3286   /* Check if comp unit has_children.
3287      If so, read the rest of the partial symbols from this comp unit.
3288      If not, there's no more debug_info for this comp unit.  */
3289   if (has_children)
3290     {
3291       struct partial_die_info *first_die;
3292       CORE_ADDR lowpc, highpc;
3293
3294       lowpc = ((CORE_ADDR) -1);
3295       highpc = ((CORE_ADDR) 0);
3296
3297       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3298
3299       scan_partial_symbols (first_die, &lowpc, &highpc,
3300                             ! has_pc_info, &cu);
3301
3302       /* If we didn't find a lowpc, set it to highpc to avoid
3303          complaints from `maint check'.  */
3304       if (lowpc == ((CORE_ADDR) -1))
3305         lowpc = highpc;
3306
3307       /* If the compilation unit didn't have an explicit address range,
3308          then use the information extracted from its child dies.  */
3309       if (! has_pc_info)
3310         {
3311           best_lowpc = lowpc;
3312           best_highpc = highpc;
3313         }
3314     }
3315   pst->textlow = best_lowpc + baseaddr;
3316   pst->texthigh = best_highpc + baseaddr;
3317
3318   pst->n_global_syms = objfile->global_psymbols.next -
3319     (objfile->global_psymbols.list + pst->globals_offset);
3320   pst->n_static_syms = objfile->static_psymbols.next -
3321     (objfile->static_psymbols.list + pst->statics_offset);
3322   sort_pst_symbols (pst);
3323
3324   info_ptr = (beg_of_comp_unit + cu.header.length
3325               + cu.header.initial_length_size);
3326
3327   if (this_cu->from_debug_types)
3328     {
3329       /* It's not clear we want to do anything with stmt lists here.
3330          Waiting to see what gcc ultimately does.  */
3331     }
3332   else
3333     {
3334       /* Get the list of files included in the current compilation unit,
3335          and build a psymtab for each of them.  */
3336       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3337     }
3338
3339   do_cleanups (back_to_inner);
3340
3341   return info_ptr;
3342 }
3343
3344 /* Traversal function for htab_traverse_noresize.
3345    Process one .debug_types comp-unit.  */
3346
3347 static int
3348 process_type_comp_unit (void **slot, void *info)
3349 {
3350   struct signatured_type *entry = (struct signatured_type *) *slot;
3351   struct objfile *objfile = (struct objfile *) info;
3352   struct dwarf2_per_cu_data *this_cu;
3353
3354   this_cu = &entry->per_cu;
3355
3356   gdb_assert (dwarf2_per_objfile->types.readin);
3357   process_psymtab_comp_unit (objfile, this_cu,
3358                              dwarf2_per_objfile->types.buffer,
3359                              dwarf2_per_objfile->types.buffer + entry->offset,
3360                              dwarf2_per_objfile->types.size);
3361
3362   return 1;
3363 }
3364
3365 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3366    Build partial symbol tables for the .debug_types comp-units.  */
3367
3368 static void
3369 build_type_psymtabs (struct objfile *objfile)
3370 {
3371   if (! create_debug_types_hash_table (objfile))
3372     return;
3373
3374   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3375                           process_type_comp_unit, objfile);
3376 }
3377
3378 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3379
3380 static void
3381 psymtabs_addrmap_cleanup (void *o)
3382 {
3383   struct objfile *objfile = o;
3384
3385   objfile->psymtabs_addrmap = NULL;
3386 }
3387
3388 /* Build the partial symbol table by doing a quick pass through the
3389    .debug_info and .debug_abbrev sections.  */
3390
3391 static void
3392 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3393 {
3394   gdb_byte *info_ptr;
3395   struct cleanup *back_to, *addrmap_cleanup;
3396   struct obstack temp_obstack;
3397
3398   dwarf2_per_objfile->reading_partial_symbols = 1;
3399
3400   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3401   info_ptr = dwarf2_per_objfile->info.buffer;
3402
3403   /* Any cached compilation units will be linked by the per-objfile
3404      read_in_chain.  Make sure to free them when we're done.  */
3405   back_to = make_cleanup (free_cached_comp_units, NULL);
3406
3407   build_type_psymtabs (objfile);
3408
3409   create_all_comp_units (objfile);
3410
3411   /* Create a temporary address map on a temporary obstack.  We later
3412      copy this to the final obstack.  */
3413   obstack_init (&temp_obstack);
3414   make_cleanup_obstack_free (&temp_obstack);
3415   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3416   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3417
3418   /* Since the objects we're extracting from .debug_info vary in
3419      length, only the individual functions to extract them (like
3420      read_comp_unit_head and load_partial_die) can really know whether
3421      the buffer is large enough to hold another complete object.
3422
3423      At the moment, they don't actually check that.  If .debug_info
3424      holds just one extra byte after the last compilation unit's dies,
3425      then read_comp_unit_head will happily read off the end of the
3426      buffer.  read_partial_die is similarly casual.  Those functions
3427      should be fixed.
3428
3429      For this loop condition, simply checking whether there's any data
3430      left at all should be sufficient.  */
3431
3432   while (info_ptr < (dwarf2_per_objfile->info.buffer
3433                      + dwarf2_per_objfile->info.size))
3434     {
3435       struct dwarf2_per_cu_data *this_cu;
3436
3437       this_cu = dwarf2_find_comp_unit (info_ptr
3438                                        - dwarf2_per_objfile->info.buffer,
3439                                        objfile);
3440
3441       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3442                                             dwarf2_per_objfile->info.buffer,
3443                                             info_ptr,
3444                                             dwarf2_per_objfile->info.size);
3445     }
3446
3447   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3448                                                     &objfile->objfile_obstack);
3449   discard_cleanups (addrmap_cleanup);
3450
3451   do_cleanups (back_to);
3452 }
3453
3454 /* Load the partial DIEs for a secondary CU into memory.  */
3455
3456 static void
3457 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3458                         struct objfile *objfile)
3459 {
3460   bfd *abfd = objfile->obfd;
3461   gdb_byte *info_ptr, *beg_of_comp_unit;
3462   struct die_info *comp_unit_die;
3463   struct dwarf2_cu *cu;
3464   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3465   int has_children;
3466   struct die_reader_specs reader_specs;
3467   int read_cu = 0;
3468
3469   gdb_assert (! this_cu->from_debug_types);
3470
3471   gdb_assert (dwarf2_per_objfile->info.readin);
3472   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3473   beg_of_comp_unit = info_ptr;
3474
3475   if (this_cu->cu == NULL)
3476     {
3477       cu = xmalloc (sizeof (*cu));
3478       init_one_comp_unit (cu, objfile);
3479
3480       read_cu = 1;
3481
3482       /* If an error occurs while loading, release our storage.  */
3483       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3484
3485       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3486                                               dwarf2_per_objfile->info.buffer,
3487                                               dwarf2_per_objfile->info.size,
3488                                               abfd);
3489
3490       /* Complete the cu_header.  */
3491       cu->header.offset = this_cu->offset;
3492       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3493
3494       /* Link this compilation unit into the compilation unit tree.  */
3495       this_cu->cu = cu;
3496       cu->per_cu = this_cu;
3497
3498       /* Link this CU into read_in_chain.  */
3499       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3500       dwarf2_per_objfile->read_in_chain = this_cu;
3501     }
3502   else
3503     {
3504       cu = this_cu->cu;
3505       info_ptr += cu->header.first_die_offset;
3506     }
3507
3508   /* Read the abbrevs for this compilation unit into a table.  */
3509   gdb_assert (cu->dwarf2_abbrevs == NULL);
3510   dwarf2_read_abbrevs (abfd, cu);
3511   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3512
3513   /* Read the compilation unit die.  */
3514   init_cu_die_reader (&reader_specs, cu);
3515   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3516                             &has_children);
3517
3518   prepare_one_comp_unit (cu, comp_unit_die);
3519
3520   /* Check if comp unit has_children.
3521      If so, read the rest of the partial symbols from this comp unit.
3522      If not, there's no more debug_info for this comp unit.  */
3523   if (has_children)
3524     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3525
3526   do_cleanups (free_abbrevs_cleanup);
3527
3528   if (read_cu)
3529     {
3530       /* We've successfully allocated this compilation unit.  Let our
3531          caller clean it up when finished with it.  */
3532       discard_cleanups (free_cu_cleanup);
3533     }
3534 }
3535
3536 /* Create a list of all compilation units in OBJFILE.  We do this only
3537    if an inter-comp-unit reference is found; presumably if there is one,
3538    there will be many, and one will occur early in the .debug_info section.
3539    So there's no point in building this list incrementally.  */
3540
3541 static void
3542 create_all_comp_units (struct objfile *objfile)
3543 {
3544   int n_allocated;
3545   int n_comp_units;
3546   struct dwarf2_per_cu_data **all_comp_units;
3547   gdb_byte *info_ptr;
3548
3549   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3550   info_ptr = dwarf2_per_objfile->info.buffer;
3551
3552   n_comp_units = 0;
3553   n_allocated = 10;
3554   all_comp_units = xmalloc (n_allocated
3555                             * sizeof (struct dwarf2_per_cu_data *));
3556
3557   while (info_ptr < dwarf2_per_objfile->info.buffer
3558          + dwarf2_per_objfile->info.size)
3559     {
3560       unsigned int length, initial_length_size;
3561       struct dwarf2_per_cu_data *this_cu;
3562       unsigned int offset;
3563
3564       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3565
3566       /* Read just enough information to find out where the next
3567          compilation unit is.  */
3568       length = read_initial_length (objfile->obfd, info_ptr,
3569                                     &initial_length_size);
3570
3571       /* Save the compilation unit for later lookup.  */
3572       this_cu = obstack_alloc (&objfile->objfile_obstack,
3573                                sizeof (struct dwarf2_per_cu_data));
3574       memset (this_cu, 0, sizeof (*this_cu));
3575       this_cu->offset = offset;
3576       this_cu->length = length + initial_length_size;
3577       this_cu->objfile = objfile;
3578
3579       if (n_comp_units == n_allocated)
3580         {
3581           n_allocated *= 2;
3582           all_comp_units = xrealloc (all_comp_units,
3583                                      n_allocated
3584                                      * sizeof (struct dwarf2_per_cu_data *));
3585         }
3586       all_comp_units[n_comp_units++] = this_cu;
3587
3588       info_ptr = info_ptr + this_cu->length;
3589     }
3590
3591   dwarf2_per_objfile->all_comp_units
3592     = obstack_alloc (&objfile->objfile_obstack,
3593                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3594   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3595           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3596   xfree (all_comp_units);
3597   dwarf2_per_objfile->n_comp_units = n_comp_units;
3598 }
3599
3600 /* Process all loaded DIEs for compilation unit CU, starting at
3601    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3602    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3603    DW_AT_ranges).  If NEED_PC is set, then this function will set
3604    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3605    and record the covered ranges in the addrmap.  */
3606
3607 static void
3608 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3609                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3610 {
3611   struct partial_die_info *pdi;
3612
3613   /* Now, march along the PDI's, descending into ones which have
3614      interesting children but skipping the children of the other ones,
3615      until we reach the end of the compilation unit.  */
3616
3617   pdi = first_die;
3618
3619   while (pdi != NULL)
3620     {
3621       fixup_partial_die (pdi, cu);
3622
3623       /* Anonymous namespaces or modules have no name but have interesting
3624          children, so we need to look at them.  Ditto for anonymous
3625          enums.  */
3626
3627       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3628           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3629         {
3630           switch (pdi->tag)
3631             {
3632             case DW_TAG_subprogram:
3633               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3634               break;
3635             case DW_TAG_constant:
3636             case DW_TAG_variable:
3637             case DW_TAG_typedef:
3638             case DW_TAG_union_type:
3639               if (!pdi->is_declaration)
3640                 {
3641                   add_partial_symbol (pdi, cu);
3642                 }
3643               break;
3644             case DW_TAG_class_type:
3645             case DW_TAG_interface_type:
3646             case DW_TAG_structure_type:
3647               if (!pdi->is_declaration)
3648                 {
3649                   add_partial_symbol (pdi, cu);
3650                 }
3651               break;
3652             case DW_TAG_enumeration_type:
3653               if (!pdi->is_declaration)
3654                 add_partial_enumeration (pdi, cu);
3655               break;
3656             case DW_TAG_base_type:
3657             case DW_TAG_subrange_type:
3658               /* File scope base type definitions are added to the partial
3659                  symbol table.  */
3660               add_partial_symbol (pdi, cu);
3661               break;
3662             case DW_TAG_namespace:
3663               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3664               break;
3665             case DW_TAG_module:
3666               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3667               break;
3668             default:
3669               break;
3670             }
3671         }
3672
3673       /* If the die has a sibling, skip to the sibling.  */
3674
3675       pdi = pdi->die_sibling;
3676     }
3677 }
3678
3679 /* Functions used to compute the fully scoped name of a partial DIE.
3680
3681    Normally, this is simple.  For C++, the parent DIE's fully scoped
3682    name is concatenated with "::" and the partial DIE's name.  For
3683    Java, the same thing occurs except that "." is used instead of "::".
3684    Enumerators are an exception; they use the scope of their parent
3685    enumeration type, i.e. the name of the enumeration type is not
3686    prepended to the enumerator.
3687
3688    There are two complexities.  One is DW_AT_specification; in this
3689    case "parent" means the parent of the target of the specification,
3690    instead of the direct parent of the DIE.  The other is compilers
3691    which do not emit DW_TAG_namespace; in this case we try to guess
3692    the fully qualified name of structure types from their members'
3693    linkage names.  This must be done using the DIE's children rather
3694    than the children of any DW_AT_specification target.  We only need
3695    to do this for structures at the top level, i.e. if the target of
3696    any DW_AT_specification (if any; otherwise the DIE itself) does not
3697    have a parent.  */
3698
3699 /* Compute the scope prefix associated with PDI's parent, in
3700    compilation unit CU.  The result will be allocated on CU's
3701    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3702    field.  NULL is returned if no prefix is necessary.  */
3703 static char *
3704 partial_die_parent_scope (struct partial_die_info *pdi,
3705                           struct dwarf2_cu *cu)
3706 {
3707   char *grandparent_scope;
3708   struct partial_die_info *parent, *real_pdi;
3709
3710   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3711      then this means the parent of the specification DIE.  */
3712
3713   real_pdi = pdi;
3714   while (real_pdi->has_specification)
3715     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3716
3717   parent = real_pdi->die_parent;
3718   if (parent == NULL)
3719     return NULL;
3720
3721   if (parent->scope_set)
3722     return parent->scope;
3723
3724   fixup_partial_die (parent, cu);
3725
3726   grandparent_scope = partial_die_parent_scope (parent, cu);
3727
3728   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3729      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3730      Work around this problem here.  */
3731   if (cu->language == language_cplus
3732       && parent->tag == DW_TAG_namespace
3733       && strcmp (parent->name, "::") == 0
3734       && grandparent_scope == NULL)
3735     {
3736       parent->scope = NULL;
3737       parent->scope_set = 1;
3738       return NULL;
3739     }
3740
3741   if (parent->tag == DW_TAG_namespace
3742       || parent->tag == DW_TAG_module
3743       || parent->tag == DW_TAG_structure_type
3744       || parent->tag == DW_TAG_class_type
3745       || parent->tag == DW_TAG_interface_type
3746       || parent->tag == DW_TAG_union_type
3747       || parent->tag == DW_TAG_enumeration_type)
3748     {
3749       if (grandparent_scope == NULL)
3750         parent->scope = parent->name;
3751       else
3752         parent->scope = typename_concat (&cu->comp_unit_obstack,
3753                                          grandparent_scope,
3754                                          parent->name, 0, cu);
3755     }
3756   else if (parent->tag == DW_TAG_enumerator)
3757     /* Enumerators should not get the name of the enumeration as a prefix.  */
3758     parent->scope = grandparent_scope;
3759   else
3760     {
3761       /* FIXME drow/2004-04-01: What should we be doing with
3762          function-local names?  For partial symbols, we should probably be
3763          ignoring them.  */
3764       complaint (&symfile_complaints,
3765                  _("unhandled containing DIE tag %d for DIE at %d"),
3766                  parent->tag, pdi->offset);
3767       parent->scope = grandparent_scope;
3768     }
3769
3770   parent->scope_set = 1;
3771   return parent->scope;
3772 }
3773
3774 /* Return the fully scoped name associated with PDI, from compilation unit
3775    CU.  The result will be allocated with malloc.  */
3776 static char *
3777 partial_die_full_name (struct partial_die_info *pdi,
3778                        struct dwarf2_cu *cu)
3779 {
3780   char *parent_scope;
3781
3782   /* If this is a template instantiation, we can not work out the
3783      template arguments from partial DIEs.  So, unfortunately, we have
3784      to go through the full DIEs.  At least any work we do building
3785      types here will be reused if full symbols are loaded later.  */
3786   if (pdi->has_template_arguments)
3787     {
3788       fixup_partial_die (pdi, cu);
3789
3790       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3791         {
3792           struct die_info *die;
3793           struct attribute attr;
3794           struct dwarf2_cu *ref_cu = cu;
3795
3796           attr.name = 0;
3797           attr.form = DW_FORM_ref_addr;
3798           attr.u.addr = pdi->offset;
3799           die = follow_die_ref (NULL, &attr, &ref_cu);
3800
3801           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3802         }
3803     }
3804
3805   parent_scope = partial_die_parent_scope (pdi, cu);
3806   if (parent_scope == NULL)
3807     return NULL;
3808   else
3809     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3810 }
3811
3812 static void
3813 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3814 {
3815   struct objfile *objfile = cu->objfile;
3816   CORE_ADDR addr = 0;
3817   char *actual_name = NULL;
3818   const struct partial_symbol *psym = NULL;
3819   CORE_ADDR baseaddr;
3820   int built_actual_name = 0;
3821
3822   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3823
3824   actual_name = partial_die_full_name (pdi, cu);
3825   if (actual_name)
3826     built_actual_name = 1;
3827
3828   if (actual_name == NULL)
3829     actual_name = pdi->name;
3830
3831   switch (pdi->tag)
3832     {
3833     case DW_TAG_subprogram:
3834       if (pdi->is_external || cu->language == language_ada)
3835         {
3836           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3837              of the global scope.  But in Ada, we want to be able to access
3838              nested procedures globally.  So all Ada subprograms are stored
3839              in the global scope.  */
3840           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3841              mst_text, objfile); */
3842           add_psymbol_to_list (actual_name, strlen (actual_name),
3843                                built_actual_name,
3844                                VAR_DOMAIN, LOC_BLOCK,
3845                                &objfile->global_psymbols,
3846                                0, pdi->lowpc + baseaddr,
3847                                cu->language, objfile);
3848         }
3849       else
3850         {
3851           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3852              mst_file_text, objfile); */
3853           add_psymbol_to_list (actual_name, strlen (actual_name),
3854                                built_actual_name,
3855                                VAR_DOMAIN, LOC_BLOCK,
3856                                &objfile->static_psymbols,
3857                                0, pdi->lowpc + baseaddr,
3858                                cu->language, objfile);
3859         }
3860       break;
3861     case DW_TAG_constant:
3862       {
3863         struct psymbol_allocation_list *list;
3864
3865         if (pdi->is_external)
3866           list = &objfile->global_psymbols;
3867         else
3868           list = &objfile->static_psymbols;
3869         add_psymbol_to_list (actual_name, strlen (actual_name),
3870                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3871                              list, 0, 0, cu->language, objfile);
3872       }
3873       break;
3874     case DW_TAG_variable:
3875       if (pdi->locdesc)
3876         addr = decode_locdesc (pdi->locdesc, cu);
3877
3878       if (pdi->locdesc
3879           && addr == 0
3880           && !dwarf2_per_objfile->has_section_at_zero)
3881         {
3882           /* A global or static variable may also have been stripped
3883              out by the linker if unused, in which case its address
3884              will be nullified; do not add such variables into partial
3885              symbol table then.  */
3886         }
3887       else if (pdi->is_external)
3888         {
3889           /* Global Variable.
3890              Don't enter into the minimal symbol tables as there is
3891              a minimal symbol table entry from the ELF symbols already.
3892              Enter into partial symbol table if it has a location
3893              descriptor or a type.
3894              If the location descriptor is missing, new_symbol will create
3895              a LOC_UNRESOLVED symbol, the address of the variable will then
3896              be determined from the minimal symbol table whenever the variable
3897              is referenced.
3898              The address for the partial symbol table entry is not
3899              used by GDB, but it comes in handy for debugging partial symbol
3900              table building.  */
3901
3902           if (pdi->locdesc || pdi->has_type)
3903             add_psymbol_to_list (actual_name, strlen (actual_name),
3904                                  built_actual_name,
3905                                  VAR_DOMAIN, LOC_STATIC,
3906                                  &objfile->global_psymbols,
3907                                  0, addr + baseaddr,
3908                                  cu->language, objfile);
3909         }
3910       else
3911         {
3912           /* Static Variable.  Skip symbols without location descriptors.  */
3913           if (pdi->locdesc == NULL)
3914             {
3915               if (built_actual_name)
3916                 xfree (actual_name);
3917               return;
3918             }
3919           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3920              mst_file_data, objfile); */
3921           add_psymbol_to_list (actual_name, strlen (actual_name),
3922                                built_actual_name,
3923                                VAR_DOMAIN, LOC_STATIC,
3924                                &objfile->static_psymbols,
3925                                0, addr + baseaddr,
3926                                cu->language, objfile);
3927         }
3928       break;
3929     case DW_TAG_typedef:
3930     case DW_TAG_base_type:
3931     case DW_TAG_subrange_type:
3932       add_psymbol_to_list (actual_name, strlen (actual_name),
3933                            built_actual_name,
3934                            VAR_DOMAIN, LOC_TYPEDEF,
3935                            &objfile->static_psymbols,
3936                            0, (CORE_ADDR) 0, cu->language, objfile);
3937       break;
3938     case DW_TAG_namespace:
3939       add_psymbol_to_list (actual_name, strlen (actual_name),
3940                            built_actual_name,
3941                            VAR_DOMAIN, LOC_TYPEDEF,
3942                            &objfile->global_psymbols,
3943                            0, (CORE_ADDR) 0, cu->language, objfile);
3944       break;
3945     case DW_TAG_class_type:
3946     case DW_TAG_interface_type:
3947     case DW_TAG_structure_type:
3948     case DW_TAG_union_type:
3949     case DW_TAG_enumeration_type:
3950       /* Skip external references.  The DWARF standard says in the section
3951          about "Structure, Union, and Class Type Entries": "An incomplete
3952          structure, union or class type is represented by a structure,
3953          union or class entry that does not have a byte size attribute
3954          and that has a DW_AT_declaration attribute."  */
3955       if (!pdi->has_byte_size && pdi->is_declaration)
3956         {
3957           if (built_actual_name)
3958             xfree (actual_name);
3959           return;
3960         }
3961
3962       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3963          static vs. global.  */
3964       add_psymbol_to_list (actual_name, strlen (actual_name),
3965                            built_actual_name,
3966                            STRUCT_DOMAIN, LOC_TYPEDEF,
3967                            (cu->language == language_cplus
3968                             || cu->language == language_java)
3969                            ? &objfile->global_psymbols
3970                            : &objfile->static_psymbols,
3971                            0, (CORE_ADDR) 0, cu->language, objfile);
3972
3973       break;
3974     case DW_TAG_enumerator:
3975       add_psymbol_to_list (actual_name, strlen (actual_name),
3976                            built_actual_name,
3977                            VAR_DOMAIN, LOC_CONST,
3978                            (cu->language == language_cplus
3979                             || cu->language == language_java)
3980                            ? &objfile->global_psymbols
3981                            : &objfile->static_psymbols,
3982                            0, (CORE_ADDR) 0, cu->language, objfile);
3983       break;
3984     default:
3985       break;
3986     }
3987
3988   if (built_actual_name)
3989     xfree (actual_name);
3990 }
3991
3992 /* Read a partial die corresponding to a namespace; also, add a symbol
3993    corresponding to that namespace to the symbol table.  NAMESPACE is
3994    the name of the enclosing namespace.  */
3995
3996 static void
3997 add_partial_namespace (struct partial_die_info *pdi,
3998                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3999                        int need_pc, struct dwarf2_cu *cu)
4000 {
4001   /* Add a symbol for the namespace.  */
4002
4003   add_partial_symbol (pdi, cu);
4004
4005   /* Now scan partial symbols in that namespace.  */
4006
4007   if (pdi->has_children)
4008     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4009 }
4010
4011 /* Read a partial die corresponding to a Fortran module.  */
4012
4013 static void
4014 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4015                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4016 {
4017   /* Now scan partial symbols in that module.  */
4018
4019   if (pdi->has_children)
4020     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4021 }
4022
4023 /* Read a partial die corresponding to a subprogram and create a partial
4024    symbol for that subprogram.  When the CU language allows it, this
4025    routine also defines a partial symbol for each nested subprogram
4026    that this subprogram contains.
4027
4028    DIE my also be a lexical block, in which case we simply search
4029    recursively for suprograms defined inside that lexical block.
4030    Again, this is only performed when the CU language allows this
4031    type of definitions.  */
4032
4033 static void
4034 add_partial_subprogram (struct partial_die_info *pdi,
4035                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4036                         int need_pc, struct dwarf2_cu *cu)
4037 {
4038   if (pdi->tag == DW_TAG_subprogram)
4039     {
4040       if (pdi->has_pc_info)
4041         {
4042           if (pdi->lowpc < *lowpc)
4043             *lowpc = pdi->lowpc;
4044           if (pdi->highpc > *highpc)
4045             *highpc = pdi->highpc;
4046           if (need_pc)
4047             {
4048               CORE_ADDR baseaddr;
4049               struct objfile *objfile = cu->objfile;
4050
4051               baseaddr = ANOFFSET (objfile->section_offsets,
4052                                    SECT_OFF_TEXT (objfile));
4053               addrmap_set_empty (objfile->psymtabs_addrmap,
4054                                  pdi->lowpc + baseaddr,
4055                                  pdi->highpc - 1 + baseaddr,
4056                                  cu->per_cu->v.psymtab);
4057             }
4058           if (!pdi->is_declaration)
4059             /* Ignore subprogram DIEs that do not have a name, they are
4060                illegal.  Do not emit a complaint at this point, we will
4061                do so when we convert this psymtab into a symtab.  */
4062             if (pdi->name)
4063               add_partial_symbol (pdi, cu);
4064         }
4065     }
4066
4067   if (! pdi->has_children)
4068     return;
4069
4070   if (cu->language == language_ada)
4071     {
4072       pdi = pdi->die_child;
4073       while (pdi != NULL)
4074         {
4075           fixup_partial_die (pdi, cu);
4076           if (pdi->tag == DW_TAG_subprogram
4077               || pdi->tag == DW_TAG_lexical_block)
4078             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4079           pdi = pdi->die_sibling;
4080         }
4081     }
4082 }
4083
4084 /* Read a partial die corresponding to an enumeration type.  */
4085
4086 static void
4087 add_partial_enumeration (struct partial_die_info *enum_pdi,
4088                          struct dwarf2_cu *cu)
4089 {
4090   struct partial_die_info *pdi;
4091
4092   if (enum_pdi->name != NULL)
4093     add_partial_symbol (enum_pdi, cu);
4094
4095   pdi = enum_pdi->die_child;
4096   while (pdi)
4097     {
4098       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4099         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4100       else
4101         add_partial_symbol (pdi, cu);
4102       pdi = pdi->die_sibling;
4103     }
4104 }
4105
4106 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4107    Return the corresponding abbrev, or NULL if the number is zero (indicating
4108    an empty DIE).  In either case *BYTES_READ will be set to the length of
4109    the initial number.  */
4110
4111 static struct abbrev_info *
4112 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4113                  struct dwarf2_cu *cu)
4114 {
4115   bfd *abfd = cu->objfile->obfd;
4116   unsigned int abbrev_number;
4117   struct abbrev_info *abbrev;
4118
4119   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4120
4121   if (abbrev_number == 0)
4122     return NULL;
4123
4124   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4125   if (!abbrev)
4126     {
4127       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4128              abbrev_number, bfd_get_filename (abfd));
4129     }
4130
4131   return abbrev;
4132 }
4133
4134 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4135    Returns a pointer to the end of a series of DIEs, terminated by an empty
4136    DIE.  Any children of the skipped DIEs will also be skipped.  */
4137
4138 static gdb_byte *
4139 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4140 {
4141   struct abbrev_info *abbrev;
4142   unsigned int bytes_read;
4143
4144   while (1)
4145     {
4146       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4147       if (abbrev == NULL)
4148         return info_ptr + bytes_read;
4149       else
4150         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4151     }
4152 }
4153
4154 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4155    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4156    abbrev corresponding to that skipped uleb128 should be passed in
4157    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4158    children.  */
4159
4160 static gdb_byte *
4161 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4162               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4163 {
4164   unsigned int bytes_read;
4165   struct attribute attr;
4166   bfd *abfd = cu->objfile->obfd;
4167   unsigned int form, i;
4168
4169   for (i = 0; i < abbrev->num_attrs; i++)
4170     {
4171       /* The only abbrev we care about is DW_AT_sibling.  */
4172       if (abbrev->attrs[i].name == DW_AT_sibling)
4173         {
4174           read_attribute (&attr, &abbrev->attrs[i],
4175                           abfd, info_ptr, cu);
4176           if (attr.form == DW_FORM_ref_addr)
4177             complaint (&symfile_complaints,
4178                        _("ignoring absolute DW_AT_sibling"));
4179           else
4180             return buffer + dwarf2_get_ref_die_offset (&attr);
4181         }
4182
4183       /* If it isn't DW_AT_sibling, skip this attribute.  */
4184       form = abbrev->attrs[i].form;
4185     skip_attribute:
4186       switch (form)
4187         {
4188         case DW_FORM_ref_addr:
4189           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4190              and later it is offset sized.  */
4191           if (cu->header.version == 2)
4192             info_ptr += cu->header.addr_size;
4193           else
4194             info_ptr += cu->header.offset_size;
4195           break;
4196         case DW_FORM_addr:
4197           info_ptr += cu->header.addr_size;
4198           break;
4199         case DW_FORM_data1:
4200         case DW_FORM_ref1:
4201         case DW_FORM_flag:
4202           info_ptr += 1;
4203           break;
4204         case DW_FORM_flag_present:
4205           break;
4206         case DW_FORM_data2:
4207         case DW_FORM_ref2:
4208           info_ptr += 2;
4209           break;
4210         case DW_FORM_data4:
4211         case DW_FORM_ref4:
4212           info_ptr += 4;
4213           break;
4214         case DW_FORM_data8:
4215         case DW_FORM_ref8:
4216         case DW_FORM_sig8:
4217           info_ptr += 8;
4218           break;
4219         case DW_FORM_string:
4220           read_direct_string (abfd, info_ptr, &bytes_read);
4221           info_ptr += bytes_read;
4222           break;
4223         case DW_FORM_sec_offset:
4224         case DW_FORM_strp:
4225           info_ptr += cu->header.offset_size;
4226           break;
4227         case DW_FORM_exprloc:
4228         case DW_FORM_block:
4229           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4230           info_ptr += bytes_read;
4231           break;
4232         case DW_FORM_block1:
4233           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4234           break;
4235         case DW_FORM_block2:
4236           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4237           break;
4238         case DW_FORM_block4:
4239           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4240           break;
4241         case DW_FORM_sdata:
4242         case DW_FORM_udata:
4243         case DW_FORM_ref_udata:
4244           info_ptr = skip_leb128 (abfd, info_ptr);
4245           break;
4246         case DW_FORM_indirect:
4247           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4248           info_ptr += bytes_read;
4249           /* We need to continue parsing from here, so just go back to
4250              the top.  */
4251           goto skip_attribute;
4252
4253         default:
4254           error (_("Dwarf Error: Cannot handle %s "
4255                    "in DWARF reader [in module %s]"),
4256                  dwarf_form_name (form),
4257                  bfd_get_filename (abfd));
4258         }
4259     }
4260
4261   if (abbrev->has_children)
4262     return skip_children (buffer, info_ptr, cu);
4263   else
4264     return info_ptr;
4265 }
4266
4267 /* Locate ORIG_PDI's sibling.
4268    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4269    in BUFFER.  */
4270
4271 static gdb_byte *
4272 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4273                     gdb_byte *buffer, gdb_byte *info_ptr,
4274                     bfd *abfd, struct dwarf2_cu *cu)
4275 {
4276   /* Do we know the sibling already?  */
4277
4278   if (orig_pdi->sibling)
4279     return orig_pdi->sibling;
4280
4281   /* Are there any children to deal with?  */
4282
4283   if (!orig_pdi->has_children)
4284     return info_ptr;
4285
4286   /* Skip the children the long way.  */
4287
4288   return skip_children (buffer, info_ptr, cu);
4289 }
4290
4291 /* Expand this partial symbol table into a full symbol table.  */
4292
4293 static void
4294 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4295 {
4296   if (pst != NULL)
4297     {
4298       if (pst->readin)
4299         {
4300           warning (_("bug: psymtab for %s is already read in."),
4301                    pst->filename);
4302         }
4303       else
4304         {
4305           if (info_verbose)
4306             {
4307               printf_filtered (_("Reading in symbols for %s..."),
4308                                pst->filename);
4309               gdb_flush (gdb_stdout);
4310             }
4311
4312           /* Restore our global data.  */
4313           dwarf2_per_objfile = objfile_data (pst->objfile,
4314                                              dwarf2_objfile_data_key);
4315
4316           /* If this psymtab is constructed from a debug-only objfile, the
4317              has_section_at_zero flag will not necessarily be correct.  We
4318              can get the correct value for this flag by looking at the data
4319              associated with the (presumably stripped) associated objfile.  */
4320           if (pst->objfile->separate_debug_objfile_backlink)
4321             {
4322               struct dwarf2_per_objfile *dpo_backlink
4323                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4324                                 dwarf2_objfile_data_key);
4325
4326               dwarf2_per_objfile->has_section_at_zero
4327                 = dpo_backlink->has_section_at_zero;
4328             }
4329
4330           dwarf2_per_objfile->reading_partial_symbols = 0;
4331
4332           psymtab_to_symtab_1 (pst);
4333
4334           /* Finish up the debug error message.  */
4335           if (info_verbose)
4336             printf_filtered (_("done.\n"));
4337         }
4338     }
4339 }
4340
4341 /* Add PER_CU to the queue.  */
4342
4343 static void
4344 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4345 {
4346   struct dwarf2_queue_item *item;
4347
4348   per_cu->queued = 1;
4349   item = xmalloc (sizeof (*item));
4350   item->per_cu = per_cu;
4351   item->next = NULL;
4352
4353   if (dwarf2_queue == NULL)
4354     dwarf2_queue = item;
4355   else
4356     dwarf2_queue_tail->next = item;
4357
4358   dwarf2_queue_tail = item;
4359 }
4360
4361 /* Process the queue.  */
4362
4363 static void
4364 process_queue (struct objfile *objfile)
4365 {
4366   struct dwarf2_queue_item *item, *next_item;
4367
4368   /* The queue starts out with one item, but following a DIE reference
4369      may load a new CU, adding it to the end of the queue.  */
4370   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4371     {
4372       if (dwarf2_per_objfile->using_index
4373           ? !item->per_cu->v.quick->symtab
4374           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4375         process_full_comp_unit (item->per_cu);
4376
4377       item->per_cu->queued = 0;
4378       next_item = item->next;
4379       xfree (item);
4380     }
4381
4382   dwarf2_queue_tail = NULL;
4383 }
4384
4385 /* Free all allocated queue entries.  This function only releases anything if
4386    an error was thrown; if the queue was processed then it would have been
4387    freed as we went along.  */
4388
4389 static void
4390 dwarf2_release_queue (void *dummy)
4391 {
4392   struct dwarf2_queue_item *item, *last;
4393
4394   item = dwarf2_queue;
4395   while (item)
4396     {
4397       /* Anything still marked queued is likely to be in an
4398          inconsistent state, so discard it.  */
4399       if (item->per_cu->queued)
4400         {
4401           if (item->per_cu->cu != NULL)
4402             free_one_cached_comp_unit (item->per_cu->cu);
4403           item->per_cu->queued = 0;
4404         }
4405
4406       last = item;
4407       item = item->next;
4408       xfree (last);
4409     }
4410
4411   dwarf2_queue = dwarf2_queue_tail = NULL;
4412 }
4413
4414 /* Read in full symbols for PST, and anything it depends on.  */
4415
4416 static void
4417 psymtab_to_symtab_1 (struct partial_symtab *pst)
4418 {
4419   struct dwarf2_per_cu_data *per_cu;
4420   struct cleanup *back_to;
4421   int i;
4422
4423   for (i = 0; i < pst->number_of_dependencies; i++)
4424     if (!pst->dependencies[i]->readin)
4425       {
4426         /* Inform about additional files that need to be read in.  */
4427         if (info_verbose)
4428           {
4429             /* FIXME: i18n: Need to make this a single string.  */
4430             fputs_filtered (" ", gdb_stdout);
4431             wrap_here ("");
4432             fputs_filtered ("and ", gdb_stdout);
4433             wrap_here ("");
4434             printf_filtered ("%s...", pst->dependencies[i]->filename);
4435             wrap_here ("");     /* Flush output.  */
4436             gdb_flush (gdb_stdout);
4437           }
4438         psymtab_to_symtab_1 (pst->dependencies[i]);
4439       }
4440
4441   per_cu = pst->read_symtab_private;
4442
4443   if (per_cu == NULL)
4444     {
4445       /* It's an include file, no symbols to read for it.
4446          Everything is in the parent symtab.  */
4447       pst->readin = 1;
4448       return;
4449     }
4450
4451   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4452 }
4453
4454 /* Load the DIEs associated with PER_CU into memory.  */
4455
4456 static void
4457 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4458                      struct objfile *objfile)
4459 {
4460   bfd *abfd = objfile->obfd;
4461   struct dwarf2_cu *cu;
4462   unsigned int offset;
4463   gdb_byte *info_ptr, *beg_of_comp_unit;
4464   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4465   struct attribute *attr;
4466   int read_cu = 0;
4467
4468   gdb_assert (! per_cu->from_debug_types);
4469
4470   /* Set local variables from the partial symbol table info.  */
4471   offset = per_cu->offset;
4472
4473   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4474   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4475   beg_of_comp_unit = info_ptr;
4476
4477   if (per_cu->cu == NULL)
4478     {
4479       cu = xmalloc (sizeof (*cu));
4480       init_one_comp_unit (cu, objfile);
4481
4482       read_cu = 1;
4483
4484       /* If an error occurs while loading, release our storage.  */
4485       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4486
4487       /* Read in the comp_unit header.  */
4488       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4489
4490       /* Complete the cu_header.  */
4491       cu->header.offset = offset;
4492       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4493
4494       /* Read the abbrevs for this compilation unit.  */
4495       dwarf2_read_abbrevs (abfd, cu);
4496       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4497
4498       /* Link this compilation unit into the compilation unit tree.  */
4499       per_cu->cu = cu;
4500       cu->per_cu = per_cu;
4501
4502       /* Link this CU into read_in_chain.  */
4503       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4504       dwarf2_per_objfile->read_in_chain = per_cu;
4505     }
4506   else
4507     {
4508       cu = per_cu->cu;
4509       info_ptr += cu->header.first_die_offset;
4510     }
4511
4512   cu->dies = read_comp_unit (info_ptr, cu);
4513
4514   /* We try not to read any attributes in this function, because not
4515      all objfiles needed for references have been loaded yet, and symbol
4516      table processing isn't initialized.  But we have to set the CU language,
4517      or we won't be able to build types correctly.  */
4518   prepare_one_comp_unit (cu, cu->dies);
4519
4520   /* Similarly, if we do not read the producer, we can not apply
4521      producer-specific interpretation.  */
4522   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4523   if (attr)
4524     cu->producer = DW_STRING (attr);
4525
4526   if (read_cu)
4527     {
4528       do_cleanups (free_abbrevs_cleanup);
4529
4530       /* We've successfully allocated this compilation unit.  Let our
4531          caller clean it up when finished with it.  */
4532       discard_cleanups (free_cu_cleanup);
4533     }
4534 }
4535
4536 /* Add a DIE to the delayed physname list.  */
4537
4538 static void
4539 add_to_method_list (struct type *type, int fnfield_index, int index,
4540                     const char *name, struct die_info *die,
4541                     struct dwarf2_cu *cu)
4542 {
4543   struct delayed_method_info mi;
4544   mi.type = type;
4545   mi.fnfield_index = fnfield_index;
4546   mi.index = index;
4547   mi.name = name;
4548   mi.die = die;
4549   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4550 }
4551
4552 /* A cleanup for freeing the delayed method list.  */
4553
4554 static void
4555 free_delayed_list (void *ptr)
4556 {
4557   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4558   if (cu->method_list != NULL)
4559     {
4560       VEC_free (delayed_method_info, cu->method_list);
4561       cu->method_list = NULL;
4562     }
4563 }
4564
4565 /* Compute the physnames of any methods on the CU's method list.
4566
4567    The computation of method physnames is delayed in order to avoid the
4568    (bad) condition that one of the method's formal parameters is of an as yet
4569    incomplete type.  */
4570
4571 static void
4572 compute_delayed_physnames (struct dwarf2_cu *cu)
4573 {
4574   int i;
4575   struct delayed_method_info *mi;
4576   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4577     {
4578       char *physname;
4579       struct fn_fieldlist *fn_flp
4580         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4581       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4582       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4583     }
4584 }
4585
4586 /* Generate full symbol information for PST and CU, whose DIEs have
4587    already been loaded into memory.  */
4588
4589 static void
4590 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4591 {
4592   struct dwarf2_cu *cu = per_cu->cu;
4593   struct objfile *objfile = per_cu->objfile;
4594   CORE_ADDR lowpc, highpc;
4595   struct symtab *symtab;
4596   struct cleanup *back_to, *delayed_list_cleanup;
4597   CORE_ADDR baseaddr;
4598
4599   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4600
4601   buildsym_init ();
4602   back_to = make_cleanup (really_free_pendings, NULL);
4603   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4604
4605   cu->list_in_scope = &file_symbols;
4606
4607   dwarf2_find_base_address (cu->dies, cu);
4608
4609   /* Do line number decoding in read_file_scope () */
4610   process_die (cu->dies, cu);
4611
4612   /* Now that we have processed all the DIEs in the CU, all the types 
4613      should be complete, and it should now be safe to compute all of the
4614      physnames.  */
4615   compute_delayed_physnames (cu);
4616   do_cleanups (delayed_list_cleanup);
4617
4618   /* Some compilers don't define a DW_AT_high_pc attribute for the
4619      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4620      it, by scanning the DIE's below the compilation unit.  */
4621   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4622
4623   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4624
4625   /* Set symtab language to language from DW_AT_language.
4626      If the compilation is from a C file generated by language preprocessors,
4627      do not set the language if it was already deduced by start_subfile.  */
4628   if (symtab != NULL
4629       && !(cu->language == language_c && symtab->language != language_c))
4630     {
4631       symtab->language = cu->language;
4632     }
4633
4634   if (dwarf2_per_objfile->using_index)
4635     per_cu->v.quick->symtab = symtab;
4636   else
4637     {
4638       struct partial_symtab *pst = per_cu->v.psymtab;
4639       pst->symtab = symtab;
4640       pst->readin = 1;
4641     }
4642
4643   do_cleanups (back_to);
4644 }
4645
4646 /* Process a die and its children.  */
4647
4648 static void
4649 process_die (struct die_info *die, struct dwarf2_cu *cu)
4650 {
4651   switch (die->tag)
4652     {
4653     case DW_TAG_padding:
4654       break;
4655     case DW_TAG_compile_unit:
4656       read_file_scope (die, cu);
4657       break;
4658     case DW_TAG_type_unit:
4659       read_type_unit_scope (die, cu);
4660       break;
4661     case DW_TAG_subprogram:
4662     case DW_TAG_inlined_subroutine:
4663       read_func_scope (die, cu);
4664       break;
4665     case DW_TAG_lexical_block:
4666     case DW_TAG_try_block:
4667     case DW_TAG_catch_block:
4668       read_lexical_block_scope (die, cu);
4669       break;
4670     case DW_TAG_class_type:
4671     case DW_TAG_interface_type:
4672     case DW_TAG_structure_type:
4673     case DW_TAG_union_type:
4674       process_structure_scope (die, cu);
4675       break;
4676     case DW_TAG_enumeration_type:
4677       process_enumeration_scope (die, cu);
4678       break;
4679
4680     /* These dies have a type, but processing them does not create
4681        a symbol or recurse to process the children.  Therefore we can
4682        read them on-demand through read_type_die.  */
4683     case DW_TAG_subroutine_type:
4684     case DW_TAG_set_type:
4685     case DW_TAG_array_type:
4686     case DW_TAG_pointer_type:
4687     case DW_TAG_ptr_to_member_type:
4688     case DW_TAG_reference_type:
4689     case DW_TAG_string_type:
4690       break;
4691
4692     case DW_TAG_base_type:
4693     case DW_TAG_subrange_type:
4694     case DW_TAG_typedef:
4695       /* Add a typedef symbol for the type definition, if it has a
4696          DW_AT_name.  */
4697       new_symbol (die, read_type_die (die, cu), cu);
4698       break;
4699     case DW_TAG_common_block:
4700       read_common_block (die, cu);
4701       break;
4702     case DW_TAG_common_inclusion:
4703       break;
4704     case DW_TAG_namespace:
4705       processing_has_namespace_info = 1;
4706       read_namespace (die, cu);
4707       break;
4708     case DW_TAG_module:
4709       processing_has_namespace_info = 1;
4710       read_module (die, cu);
4711       break;
4712     case DW_TAG_imported_declaration:
4713     case DW_TAG_imported_module:
4714       processing_has_namespace_info = 1;
4715       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4716                                  || cu->language != language_fortran))
4717         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4718                    dwarf_tag_name (die->tag));
4719       read_import_statement (die, cu);
4720       break;
4721     default:
4722       new_symbol (die, NULL, cu);
4723       break;
4724     }
4725 }
4726
4727 /* A helper function for dwarf2_compute_name which determines whether DIE
4728    needs to have the name of the scope prepended to the name listed in the
4729    die.  */
4730
4731 static int
4732 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4733 {
4734   struct attribute *attr;
4735
4736   switch (die->tag)
4737     {
4738     case DW_TAG_namespace:
4739     case DW_TAG_typedef:
4740     case DW_TAG_class_type:
4741     case DW_TAG_interface_type:
4742     case DW_TAG_structure_type:
4743     case DW_TAG_union_type:
4744     case DW_TAG_enumeration_type:
4745     case DW_TAG_enumerator:
4746     case DW_TAG_subprogram:
4747     case DW_TAG_member:
4748       return 1;
4749
4750     case DW_TAG_variable:
4751     case DW_TAG_constant:
4752       /* We only need to prefix "globally" visible variables.  These include
4753          any variable marked with DW_AT_external or any variable that
4754          lives in a namespace.  [Variables in anonymous namespaces
4755          require prefixing, but they are not DW_AT_external.]  */
4756
4757       if (dwarf2_attr (die, DW_AT_specification, cu))
4758         {
4759           struct dwarf2_cu *spec_cu = cu;
4760
4761           return die_needs_namespace (die_specification (die, &spec_cu),
4762                                       spec_cu);
4763         }
4764
4765       attr = dwarf2_attr (die, DW_AT_external, cu);
4766       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4767           && die->parent->tag != DW_TAG_module)
4768         return 0;
4769       /* A variable in a lexical block of some kind does not need a
4770          namespace, even though in C++ such variables may be external
4771          and have a mangled name.  */
4772       if (die->parent->tag ==  DW_TAG_lexical_block
4773           || die->parent->tag ==  DW_TAG_try_block
4774           || die->parent->tag ==  DW_TAG_catch_block
4775           || die->parent->tag == DW_TAG_subprogram)
4776         return 0;
4777       return 1;
4778
4779     default:
4780       return 0;
4781     }
4782 }
4783
4784 /* Retrieve the last character from a mem_file.  */
4785
4786 static void
4787 do_ui_file_peek_last (void *object, const char *buffer, long length)
4788 {
4789   char *last_char_p = (char *) object;
4790
4791   if (length > 0)
4792     *last_char_p = buffer[length - 1];
4793 }
4794
4795 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4796    compute the physname for the object, which include a method's
4797    formal parameters (C++/Java) and return type (Java).
4798
4799    For Ada, return the DIE's linkage name rather than the fully qualified
4800    name.  PHYSNAME is ignored..
4801
4802    The result is allocated on the objfile_obstack and canonicalized.  */
4803
4804 static const char *
4805 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4806                      int physname)
4807 {
4808   if (name == NULL)
4809     name = dwarf2_name (die, cu);
4810
4811   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4812      compute it by typename_concat inside GDB.  */
4813   if (cu->language == language_ada
4814       || (cu->language == language_fortran && physname))
4815     {
4816       /* For Ada unit, we prefer the linkage name over the name, as
4817          the former contains the exported name, which the user expects
4818          to be able to reference.  Ideally, we want the user to be able
4819          to reference this entity using either natural or linkage name,
4820          but we haven't started looking at this enhancement yet.  */
4821       struct attribute *attr;
4822
4823       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4824       if (attr == NULL)
4825         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4826       if (attr && DW_STRING (attr))
4827         return DW_STRING (attr);
4828     }
4829
4830   /* These are the only languages we know how to qualify names in.  */
4831   if (name != NULL
4832       && (cu->language == language_cplus || cu->language == language_java
4833           || cu->language == language_fortran))
4834     {
4835       if (die_needs_namespace (die, cu))
4836         {
4837           long length;
4838           char *prefix;
4839           struct ui_file *buf;
4840
4841           prefix = determine_prefix (die, cu);
4842           buf = mem_fileopen ();
4843           if (*prefix != '\0')
4844             {
4845               char *prefixed_name = typename_concat (NULL, prefix, name,
4846                                                      physname, cu);
4847
4848               fputs_unfiltered (prefixed_name, buf);
4849               xfree (prefixed_name);
4850             }
4851           else
4852             fputs_unfiltered (name, buf);
4853
4854           /* Template parameters may be specified in the DIE's DW_AT_name, or
4855              as children with DW_TAG_template_type_param or
4856              DW_TAG_value_type_param.  If the latter, add them to the name
4857              here.  If the name already has template parameters, then
4858              skip this step; some versions of GCC emit both, and
4859              it is more efficient to use the pre-computed name.
4860
4861              Something to keep in mind about this process: it is very
4862              unlikely, or in some cases downright impossible, to produce
4863              something that will match the mangled name of a function.
4864              If the definition of the function has the same debug info,
4865              we should be able to match up with it anyway.  But fallbacks
4866              using the minimal symbol, for instance to find a method
4867              implemented in a stripped copy of libstdc++, will not work.
4868              If we do not have debug info for the definition, we will have to
4869              match them up some other way.
4870
4871              When we do name matching there is a related problem with function
4872              templates; two instantiated function templates are allowed to
4873              differ only by their return types, which we do not add here.  */
4874
4875           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4876             {
4877               struct attribute *attr;
4878               struct die_info *child;
4879               int first = 1;
4880
4881               die->building_fullname = 1;
4882
4883               for (child = die->child; child != NULL; child = child->sibling)
4884                 {
4885                   struct type *type;
4886                   long value;
4887                   gdb_byte *bytes;
4888                   struct dwarf2_locexpr_baton *baton;
4889                   struct value *v;
4890
4891                   if (child->tag != DW_TAG_template_type_param
4892                       && child->tag != DW_TAG_template_value_param)
4893                     continue;
4894
4895                   if (first)
4896                     {
4897                       fputs_unfiltered ("<", buf);
4898                       first = 0;
4899                     }
4900                   else
4901                     fputs_unfiltered (", ", buf);
4902
4903                   attr = dwarf2_attr (child, DW_AT_type, cu);
4904                   if (attr == NULL)
4905                     {
4906                       complaint (&symfile_complaints,
4907                                  _("template parameter missing DW_AT_type"));
4908                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4909                       continue;
4910                     }
4911                   type = die_type (child, cu);
4912
4913                   if (child->tag == DW_TAG_template_type_param)
4914                     {
4915                       c_print_type (type, "", buf, -1, 0);
4916                       continue;
4917                     }
4918
4919                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4920                   if (attr == NULL)
4921                     {
4922                       complaint (&symfile_complaints,
4923                                  _("template parameter missing "
4924                                    "DW_AT_const_value"));
4925                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4926                       continue;
4927                     }
4928
4929                   dwarf2_const_value_attr (attr, type, name,
4930                                            &cu->comp_unit_obstack, cu,
4931                                            &value, &bytes, &baton);
4932
4933                   if (TYPE_NOSIGN (type))
4934                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4935                        changed, this can use value_print instead.  */
4936                     c_printchar (value, type, buf);
4937                   else
4938                     {
4939                       struct value_print_options opts;
4940
4941                       if (baton != NULL)
4942                         v = dwarf2_evaluate_loc_desc (type, NULL,
4943                                                       baton->data,
4944                                                       baton->size,
4945                                                       baton->per_cu);
4946                       else if (bytes != NULL)
4947                         {
4948                           v = allocate_value (type);
4949                           memcpy (value_contents_writeable (v), bytes,
4950                                   TYPE_LENGTH (type));
4951                         }
4952                       else
4953                         v = value_from_longest (type, value);
4954
4955                       /* Specify decimal so that we do not depend on
4956                          the radix.  */
4957                       get_formatted_print_options (&opts, 'd');
4958                       opts.raw = 1;
4959                       value_print (v, buf, &opts);
4960                       release_value (v);
4961                       value_free (v);
4962                     }
4963                 }
4964
4965               die->building_fullname = 0;
4966
4967               if (!first)
4968                 {
4969                   /* Close the argument list, with a space if necessary
4970                      (nested templates).  */
4971                   char last_char = '\0';
4972                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4973                   if (last_char == '>')
4974                     fputs_unfiltered (" >", buf);
4975                   else
4976                     fputs_unfiltered (">", buf);
4977                 }
4978             }
4979
4980           /* For Java and C++ methods, append formal parameter type
4981              information, if PHYSNAME.  */
4982
4983           if (physname && die->tag == DW_TAG_subprogram
4984               && (cu->language == language_cplus
4985                   || cu->language == language_java))
4986             {
4987               struct type *type = read_type_die (die, cu);
4988
4989               c_type_print_args (type, buf, 0, cu->language);
4990
4991               if (cu->language == language_java)
4992                 {
4993                   /* For java, we must append the return type to method
4994                      names.  */
4995                   if (die->tag == DW_TAG_subprogram)
4996                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4997                                      0, 0);
4998                 }
4999               else if (cu->language == language_cplus)
5000                 {
5001                   /* Assume that an artificial first parameter is
5002                      "this", but do not crash if it is not.  RealView
5003                      marks unnamed (and thus unused) parameters as
5004                      artificial; there is no way to differentiate
5005                      the two cases.  */
5006                   if (TYPE_NFIELDS (type) > 0
5007                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5008                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5009                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5010                                                                         0))))
5011                     fputs_unfiltered (" const", buf);
5012                 }
5013             }
5014
5015           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5016                                        &length);
5017           ui_file_delete (buf);
5018
5019           if (cu->language == language_cplus)
5020             {
5021               char *cname
5022                 = dwarf2_canonicalize_name (name, cu,
5023                                             &cu->objfile->objfile_obstack);
5024
5025               if (cname != NULL)
5026                 name = cname;
5027             }
5028         }
5029     }
5030
5031   return name;
5032 }
5033
5034 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5035    If scope qualifiers are appropriate they will be added.  The result
5036    will be allocated on the objfile_obstack, or NULL if the DIE does
5037    not have a name.  NAME may either be from a previous call to
5038    dwarf2_name or NULL.
5039
5040    The output string will be canonicalized (if C++/Java).  */
5041
5042 static const char *
5043 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5044 {
5045   return dwarf2_compute_name (name, die, cu, 0);
5046 }
5047
5048 /* Construct a physname for the given DIE in CU.  NAME may either be
5049    from a previous call to dwarf2_name or NULL.  The result will be
5050    allocated on the objfile_objstack or NULL if the DIE does not have a
5051    name.
5052
5053    The output string will be canonicalized (if C++/Java).  */
5054
5055 static const char *
5056 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5057 {
5058   return dwarf2_compute_name (name, die, cu, 1);
5059 }
5060
5061 /* Read the import statement specified by the given die and record it.  */
5062
5063 static void
5064 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5065 {
5066   struct attribute *import_attr;
5067   struct die_info *imported_die;
5068   struct dwarf2_cu *imported_cu;
5069   const char *imported_name;
5070   const char *imported_name_prefix;
5071   const char *canonical_name;
5072   const char *import_alias;
5073   const char *imported_declaration = NULL;
5074   const char *import_prefix;
5075
5076   char *temp;
5077
5078   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5079   if (import_attr == NULL)
5080     {
5081       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5082                  dwarf_tag_name (die->tag));
5083       return;
5084     }
5085
5086   imported_cu = cu;
5087   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5088   imported_name = dwarf2_name (imported_die, imported_cu);
5089   if (imported_name == NULL)
5090     {
5091       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5092
5093         The import in the following code:
5094         namespace A
5095           {
5096             typedef int B;
5097           }
5098
5099         int main ()
5100           {
5101             using A::B;
5102             B b;
5103             return b;
5104           }
5105
5106         ...
5107          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5108             <52>   DW_AT_decl_file   : 1
5109             <53>   DW_AT_decl_line   : 6
5110             <54>   DW_AT_import      : <0x75>
5111          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5112             <59>   DW_AT_name        : B
5113             <5b>   DW_AT_decl_file   : 1
5114             <5c>   DW_AT_decl_line   : 2
5115             <5d>   DW_AT_type        : <0x6e>
5116         ...
5117          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5118             <76>   DW_AT_byte_size   : 4
5119             <77>   DW_AT_encoding    : 5        (signed)
5120
5121         imports the wrong die ( 0x75 instead of 0x58 ).
5122         This case will be ignored until the gcc bug is fixed.  */
5123       return;
5124     }
5125
5126   /* Figure out the local name after import.  */
5127   import_alias = dwarf2_name (die, cu);
5128
5129   /* Figure out where the statement is being imported to.  */
5130   import_prefix = determine_prefix (die, cu);
5131
5132   /* Figure out what the scope of the imported die is and prepend it
5133      to the name of the imported die.  */
5134   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5135
5136   if (imported_die->tag != DW_TAG_namespace
5137       && imported_die->tag != DW_TAG_module)
5138     {
5139       imported_declaration = imported_name;
5140       canonical_name = imported_name_prefix;
5141     }
5142   else if (strlen (imported_name_prefix) > 0)
5143     {
5144       temp = alloca (strlen (imported_name_prefix)
5145                      + 2 + strlen (imported_name) + 1);
5146       strcpy (temp, imported_name_prefix);
5147       strcat (temp, "::");
5148       strcat (temp, imported_name);
5149       canonical_name = temp;
5150     }
5151   else
5152     canonical_name = imported_name;
5153
5154   cp_add_using_directive (import_prefix,
5155                           canonical_name,
5156                           import_alias,
5157                           imported_declaration,
5158                           &cu->objfile->objfile_obstack);
5159 }
5160
5161 static void
5162 initialize_cu_func_list (struct dwarf2_cu *cu)
5163 {
5164   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5165 }
5166
5167 /* Cleanup function for read_file_scope.  */
5168
5169 static void
5170 free_cu_line_header (void *arg)
5171 {
5172   struct dwarf2_cu *cu = arg;
5173
5174   free_line_header (cu->line_header);
5175   cu->line_header = NULL;
5176 }
5177
5178 static void
5179 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5180                          char **name, char **comp_dir)
5181 {
5182   struct attribute *attr;
5183
5184   *name = NULL;
5185   *comp_dir = NULL;
5186
5187   /* Find the filename.  Do not use dwarf2_name here, since the filename
5188      is not a source language identifier.  */
5189   attr = dwarf2_attr (die, DW_AT_name, cu);
5190   if (attr)
5191     {
5192       *name = DW_STRING (attr);
5193     }
5194
5195   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5196   if (attr)
5197     *comp_dir = DW_STRING (attr);
5198   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5199     {
5200       *comp_dir = ldirname (*name);
5201       if (*comp_dir != NULL)
5202         make_cleanup (xfree, *comp_dir);
5203     }
5204   if (*comp_dir != NULL)
5205     {
5206       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5207          directory, get rid of it.  */
5208       char *cp = strchr (*comp_dir, ':');
5209
5210       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5211         *comp_dir = cp + 1;
5212     }
5213
5214   if (*name == NULL)
5215     *name = "<unknown>";
5216 }
5217
5218 /* Process DW_TAG_compile_unit.  */
5219
5220 static void
5221 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5222 {
5223   struct objfile *objfile = cu->objfile;
5224   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5225   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5226   CORE_ADDR highpc = ((CORE_ADDR) 0);
5227   struct attribute *attr;
5228   char *name = NULL;
5229   char *comp_dir = NULL;
5230   struct die_info *child_die;
5231   bfd *abfd = objfile->obfd;
5232   struct line_header *line_header = 0;
5233   CORE_ADDR baseaddr;
5234
5235   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5236
5237   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5238
5239   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5240      from finish_block.  */
5241   if (lowpc == ((CORE_ADDR) -1))
5242     lowpc = highpc;
5243   lowpc += baseaddr;
5244   highpc += baseaddr;
5245
5246   find_file_and_directory (die, cu, &name, &comp_dir);
5247
5248   attr = dwarf2_attr (die, DW_AT_language, cu);
5249   if (attr)
5250     {
5251       set_cu_language (DW_UNSND (attr), cu);
5252     }
5253
5254   attr = dwarf2_attr (die, DW_AT_producer, cu);
5255   if (attr)
5256     cu->producer = DW_STRING (attr);
5257
5258   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5259      standardised yet.  As a workaround for the language detection we fall
5260      back to the DW_AT_producer string.  */
5261   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5262     cu->language = language_opencl;
5263
5264   /* We assume that we're processing GCC output.  */
5265   processing_gcc_compilation = 2;
5266
5267   processing_has_namespace_info = 0;
5268
5269   start_symtab (name, comp_dir, lowpc);
5270   record_debugformat ("DWARF 2");
5271   record_producer (cu->producer);
5272
5273   initialize_cu_func_list (cu);
5274
5275   /* Decode line number information if present.  We do this before
5276      processing child DIEs, so that the line header table is available
5277      for DW_AT_decl_file.  */
5278   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5279   if (attr)
5280     {
5281       unsigned int line_offset = DW_UNSND (attr);
5282       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5283       if (line_header)
5284         {
5285           cu->line_header = line_header;
5286           make_cleanup (free_cu_line_header, cu);
5287           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5288         }
5289     }
5290
5291   /* Process all dies in compilation unit.  */
5292   if (die->child != NULL)
5293     {
5294       child_die = die->child;
5295       while (child_die && child_die->tag)
5296         {
5297           process_die (child_die, cu);
5298           child_die = sibling_die (child_die);
5299         }
5300     }
5301
5302   /* Decode macro information, if present.  Dwarf 2 macro information
5303      refers to information in the line number info statement program
5304      header, so we can only read it if we've read the header
5305      successfully.  */
5306   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5307   if (attr && line_header)
5308     {
5309       unsigned int macro_offset = DW_UNSND (attr);
5310
5311       dwarf_decode_macros (line_header, macro_offset,
5312                            comp_dir, abfd, cu);
5313     }
5314   do_cleanups (back_to);
5315 }
5316
5317 /* Process DW_TAG_type_unit.
5318    For TUs we want to skip the first top level sibling if it's not the
5319    actual type being defined by this TU.  In this case the first top
5320    level sibling is there to provide context only.  */
5321
5322 static void
5323 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5324 {
5325   struct objfile *objfile = cu->objfile;
5326   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5327   CORE_ADDR lowpc;
5328   struct attribute *attr;
5329   char *name = NULL;
5330   char *comp_dir = NULL;
5331   struct die_info *child_die;
5332   bfd *abfd = objfile->obfd;
5333
5334   /* start_symtab needs a low pc, but we don't really have one.
5335      Do what read_file_scope would do in the absence of such info.  */
5336   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5337
5338   /* Find the filename.  Do not use dwarf2_name here, since the filename
5339      is not a source language identifier.  */
5340   attr = dwarf2_attr (die, DW_AT_name, cu);
5341   if (attr)
5342     name = DW_STRING (attr);
5343
5344   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5345   if (attr)
5346     comp_dir = DW_STRING (attr);
5347   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5348     {
5349       comp_dir = ldirname (name);
5350       if (comp_dir != NULL)
5351         make_cleanup (xfree, comp_dir);
5352     }
5353
5354   if (name == NULL)
5355     name = "<unknown>";
5356
5357   attr = dwarf2_attr (die, DW_AT_language, cu);
5358   if (attr)
5359     set_cu_language (DW_UNSND (attr), cu);
5360
5361   /* This isn't technically needed today.  It is done for symmetry
5362      with read_file_scope.  */
5363   attr = dwarf2_attr (die, DW_AT_producer, cu);
5364   if (attr)
5365     cu->producer = DW_STRING (attr);
5366
5367   /* We assume that we're processing GCC output.  */
5368   processing_gcc_compilation = 2;
5369
5370   processing_has_namespace_info = 0;
5371
5372   start_symtab (name, comp_dir, lowpc);
5373   record_debugformat ("DWARF 2");
5374   record_producer (cu->producer);
5375
5376   /* Process the dies in the type unit.  */
5377   if (die->child == NULL)
5378     {
5379       dump_die_for_error (die);
5380       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5381              bfd_get_filename (abfd));
5382     }
5383
5384   child_die = die->child;
5385
5386   while (child_die && child_die->tag)
5387     {
5388       process_die (child_die, cu);
5389
5390       child_die = sibling_die (child_die);
5391     }
5392
5393   do_cleanups (back_to);
5394 }
5395
5396 static void
5397 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5398                      struct dwarf2_cu *cu)
5399 {
5400   struct function_range *thisfn;
5401
5402   thisfn = (struct function_range *)
5403     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5404   thisfn->name = name;
5405   thisfn->lowpc = lowpc;
5406   thisfn->highpc = highpc;
5407   thisfn->seen_line = 0;
5408   thisfn->next = NULL;
5409
5410   if (cu->last_fn == NULL)
5411       cu->first_fn = thisfn;
5412   else
5413       cu->last_fn->next = thisfn;
5414
5415   cu->last_fn = thisfn;
5416 }
5417
5418 /* qsort helper for inherit_abstract_dies.  */
5419
5420 static int
5421 unsigned_int_compar (const void *ap, const void *bp)
5422 {
5423   unsigned int a = *(unsigned int *) ap;
5424   unsigned int b = *(unsigned int *) bp;
5425
5426   return (a > b) - (b > a);
5427 }
5428
5429 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5430    Inherit only the children of the DW_AT_abstract_origin DIE not being
5431    already referenced by DW_AT_abstract_origin from the children of the
5432    current DIE.  */
5433
5434 static void
5435 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5436 {
5437   struct die_info *child_die;
5438   unsigned die_children_count;
5439   /* CU offsets which were referenced by children of the current DIE.  */
5440   unsigned *offsets;
5441   unsigned *offsets_end, *offsetp;
5442   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5443   struct die_info *origin_die;
5444   /* Iterator of the ORIGIN_DIE children.  */
5445   struct die_info *origin_child_die;
5446   struct cleanup *cleanups;
5447   struct attribute *attr;
5448   struct dwarf2_cu *origin_cu;
5449   struct pending **origin_previous_list_in_scope;
5450
5451   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5452   if (!attr)
5453     return;
5454
5455   /* Note that following die references may follow to a die in a
5456      different cu.  */
5457
5458   origin_cu = cu;
5459   origin_die = follow_die_ref (die, attr, &origin_cu);
5460
5461   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5462      symbols in.  */
5463   origin_previous_list_in_scope = origin_cu->list_in_scope;
5464   origin_cu->list_in_scope = cu->list_in_scope;
5465
5466   if (die->tag != origin_die->tag
5467       && !(die->tag == DW_TAG_inlined_subroutine
5468            && origin_die->tag == DW_TAG_subprogram))
5469     complaint (&symfile_complaints,
5470                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5471                die->offset, origin_die->offset);
5472
5473   child_die = die->child;
5474   die_children_count = 0;
5475   while (child_die && child_die->tag)
5476     {
5477       child_die = sibling_die (child_die);
5478       die_children_count++;
5479     }
5480   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5481   cleanups = make_cleanup (xfree, offsets);
5482
5483   offsets_end = offsets;
5484   child_die = die->child;
5485   while (child_die && child_die->tag)
5486     {
5487       /* For each CHILD_DIE, find the corresponding child of
5488          ORIGIN_DIE.  If there is more than one layer of
5489          DW_AT_abstract_origin, follow them all; there shouldn't be,
5490          but GCC versions at least through 4.4 generate this (GCC PR
5491          40573).  */
5492       struct die_info *child_origin_die = child_die;
5493       struct dwarf2_cu *child_origin_cu = cu;
5494
5495       while (1)
5496         {
5497           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5498                               child_origin_cu);
5499           if (attr == NULL)
5500             break;
5501           child_origin_die = follow_die_ref (child_origin_die, attr,
5502                                              &child_origin_cu);
5503         }
5504
5505       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5506          counterpart may exist.  */
5507       if (child_origin_die != child_die)
5508         {
5509           if (child_die->tag != child_origin_die->tag
5510               && !(child_die->tag == DW_TAG_inlined_subroutine
5511                    && child_origin_die->tag == DW_TAG_subprogram))
5512             complaint (&symfile_complaints,
5513                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5514                          "different tags"), child_die->offset,
5515                        child_origin_die->offset);
5516           if (child_origin_die->parent != origin_die)
5517             complaint (&symfile_complaints,
5518                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5519                          "different parents"), child_die->offset,
5520                        child_origin_die->offset);
5521           else
5522             *offsets_end++ = child_origin_die->offset;
5523         }
5524       child_die = sibling_die (child_die);
5525     }
5526   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5527          unsigned_int_compar);
5528   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5529     if (offsetp[-1] == *offsetp)
5530       complaint (&symfile_complaints,
5531                  _("Multiple children of DIE 0x%x refer "
5532                    "to DIE 0x%x as their abstract origin"),
5533                  die->offset, *offsetp);
5534
5535   offsetp = offsets;
5536   origin_child_die = origin_die->child;
5537   while (origin_child_die && origin_child_die->tag)
5538     {
5539       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5540       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5541         offsetp++;
5542       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5543         {
5544           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5545           process_die (origin_child_die, origin_cu);
5546         }
5547       origin_child_die = sibling_die (origin_child_die);
5548     }
5549   origin_cu->list_in_scope = origin_previous_list_in_scope;
5550
5551   do_cleanups (cleanups);
5552 }
5553
5554 static void
5555 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5556 {
5557   struct objfile *objfile = cu->objfile;
5558   struct context_stack *new;
5559   CORE_ADDR lowpc;
5560   CORE_ADDR highpc;
5561   struct die_info *child_die;
5562   struct attribute *attr, *call_line, *call_file;
5563   char *name;
5564   CORE_ADDR baseaddr;
5565   struct block *block;
5566   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5567   VEC (symbolp) *template_args = NULL;
5568   struct template_symbol *templ_func = NULL;
5569
5570   if (inlined_func)
5571     {
5572       /* If we do not have call site information, we can't show the
5573          caller of this inlined function.  That's too confusing, so
5574          only use the scope for local variables.  */
5575       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5576       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5577       if (call_line == NULL || call_file == NULL)
5578         {
5579           read_lexical_block_scope (die, cu);
5580           return;
5581         }
5582     }
5583
5584   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5585
5586   name = dwarf2_name (die, cu);
5587
5588   /* Ignore functions with missing or empty names.  These are actually
5589      illegal according to the DWARF standard.  */
5590   if (name == NULL)
5591     {
5592       complaint (&symfile_complaints,
5593                  _("missing name for subprogram DIE at %d"), die->offset);
5594       return;
5595     }
5596
5597   /* Ignore functions with missing or invalid low and high pc attributes.  */
5598   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5599     {
5600       attr = dwarf2_attr (die, DW_AT_external, cu);
5601       if (!attr || !DW_UNSND (attr))
5602         complaint (&symfile_complaints,
5603                    _("cannot get low and high bounds "
5604                      "for subprogram DIE at %d"),
5605                    die->offset);
5606       return;
5607     }
5608
5609   lowpc += baseaddr;
5610   highpc += baseaddr;
5611
5612   /* Record the function range for dwarf_decode_lines.  */
5613   add_to_cu_func_list (name, lowpc, highpc, cu);
5614
5615   /* If we have any template arguments, then we must allocate a
5616      different sort of symbol.  */
5617   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5618     {
5619       if (child_die->tag == DW_TAG_template_type_param
5620           || child_die->tag == DW_TAG_template_value_param)
5621         {
5622           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5623                                        struct template_symbol);
5624           templ_func->base.is_cplus_template_function = 1;
5625           break;
5626         }
5627     }
5628
5629   new = push_context (0, lowpc);
5630   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5631                                (struct symbol *) templ_func);
5632
5633   /* If there is a location expression for DW_AT_frame_base, record
5634      it.  */
5635   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5636   if (attr)
5637     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5638        expression is being recorded directly in the function's symbol
5639        and not in a separate frame-base object.  I guess this hack is
5640        to avoid adding some sort of frame-base adjunct/annex to the
5641        function's symbol :-(.  The problem with doing this is that it
5642        results in a function symbol with a location expression that
5643        has nothing to do with the location of the function, ouch!  The
5644        relationship should be: a function's symbol has-a frame base; a
5645        frame-base has-a location expression.  */
5646     dwarf2_symbol_mark_computed (attr, new->name, cu);
5647
5648   cu->list_in_scope = &local_symbols;
5649
5650   if (die->child != NULL)
5651     {
5652       child_die = die->child;
5653       while (child_die && child_die->tag)
5654         {
5655           if (child_die->tag == DW_TAG_template_type_param
5656               || child_die->tag == DW_TAG_template_value_param)
5657             {
5658               struct symbol *arg = new_symbol (child_die, NULL, cu);
5659
5660               if (arg != NULL)
5661                 VEC_safe_push (symbolp, template_args, arg);
5662             }
5663           else
5664             process_die (child_die, cu);
5665           child_die = sibling_die (child_die);
5666         }
5667     }
5668
5669   inherit_abstract_dies (die, cu);
5670
5671   /* If we have a DW_AT_specification, we might need to import using
5672      directives from the context of the specification DIE.  See the
5673      comment in determine_prefix.  */
5674   if (cu->language == language_cplus
5675       && dwarf2_attr (die, DW_AT_specification, cu))
5676     {
5677       struct dwarf2_cu *spec_cu = cu;
5678       struct die_info *spec_die = die_specification (die, &spec_cu);
5679
5680       while (spec_die)
5681         {
5682           child_die = spec_die->child;
5683           while (child_die && child_die->tag)
5684             {
5685               if (child_die->tag == DW_TAG_imported_module)
5686                 process_die (child_die, spec_cu);
5687               child_die = sibling_die (child_die);
5688             }
5689
5690           /* In some cases, GCC generates specification DIEs that
5691              themselves contain DW_AT_specification attributes.  */
5692           spec_die = die_specification (spec_die, &spec_cu);
5693         }
5694     }
5695
5696   new = pop_context ();
5697   /* Make a block for the local symbols within.  */
5698   block = finish_block (new->name, &local_symbols, new->old_blocks,
5699                         lowpc, highpc, objfile);
5700
5701   /* For C++, set the block's scope.  */
5702   if (cu->language == language_cplus || cu->language == language_fortran)
5703     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5704                         determine_prefix (die, cu),
5705                         processing_has_namespace_info);
5706
5707   /* If we have address ranges, record them.  */
5708   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5709
5710   /* Attach template arguments to function.  */
5711   if (! VEC_empty (symbolp, template_args))
5712     {
5713       gdb_assert (templ_func != NULL);
5714
5715       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5716       templ_func->template_arguments
5717         = obstack_alloc (&objfile->objfile_obstack,
5718                          (templ_func->n_template_arguments
5719                           * sizeof (struct symbol *)));
5720       memcpy (templ_func->template_arguments,
5721               VEC_address (symbolp, template_args),
5722               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5723       VEC_free (symbolp, template_args);
5724     }
5725
5726   /* In C++, we can have functions nested inside functions (e.g., when
5727      a function declares a class that has methods).  This means that
5728      when we finish processing a function scope, we may need to go
5729      back to building a containing block's symbol lists.  */
5730   local_symbols = new->locals;
5731   param_symbols = new->params;
5732   using_directives = new->using_directives;
5733
5734   /* If we've finished processing a top-level function, subsequent
5735      symbols go in the file symbol list.  */
5736   if (outermost_context_p ())
5737     cu->list_in_scope = &file_symbols;
5738 }
5739
5740 /* Process all the DIES contained within a lexical block scope.  Start
5741    a new scope, process the dies, and then close the scope.  */
5742
5743 static void
5744 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5745 {
5746   struct objfile *objfile = cu->objfile;
5747   struct context_stack *new;
5748   CORE_ADDR lowpc, highpc;
5749   struct die_info *child_die;
5750   CORE_ADDR baseaddr;
5751
5752   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5753
5754   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5755   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5756      as multiple lexical blocks?  Handling children in a sane way would
5757      be nasty.  Might be easier to properly extend generic blocks to
5758      describe ranges.  */
5759   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5760     return;
5761   lowpc += baseaddr;
5762   highpc += baseaddr;
5763
5764   push_context (0, lowpc);
5765   if (die->child != NULL)
5766     {
5767       child_die = die->child;
5768       while (child_die && child_die->tag)
5769         {
5770           process_die (child_die, cu);
5771           child_die = sibling_die (child_die);
5772         }
5773     }
5774   new = pop_context ();
5775
5776   if (local_symbols != NULL || using_directives != NULL)
5777     {
5778       struct block *block
5779         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5780                         highpc, objfile);
5781
5782       /* Note that recording ranges after traversing children, as we
5783          do here, means that recording a parent's ranges entails
5784          walking across all its children's ranges as they appear in
5785          the address map, which is quadratic behavior.
5786
5787          It would be nicer to record the parent's ranges before
5788          traversing its children, simply overriding whatever you find
5789          there.  But since we don't even decide whether to create a
5790          block until after we've traversed its children, that's hard
5791          to do.  */
5792       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5793     }
5794   local_symbols = new->locals;
5795   using_directives = new->using_directives;
5796 }
5797
5798 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5799    Return 1 if the attributes are present and valid, otherwise, return 0.
5800    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5801
5802 static int
5803 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5804                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5805                     struct partial_symtab *ranges_pst)
5806 {
5807   struct objfile *objfile = cu->objfile;
5808   struct comp_unit_head *cu_header = &cu->header;
5809   bfd *obfd = objfile->obfd;
5810   unsigned int addr_size = cu_header->addr_size;
5811   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5812   /* Base address selection entry.  */
5813   CORE_ADDR base;
5814   int found_base;
5815   unsigned int dummy;
5816   gdb_byte *buffer;
5817   CORE_ADDR marker;
5818   int low_set;
5819   CORE_ADDR low = 0;
5820   CORE_ADDR high = 0;
5821   CORE_ADDR baseaddr;
5822
5823   found_base = cu->base_known;
5824   base = cu->base_address;
5825
5826   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5827   if (offset >= dwarf2_per_objfile->ranges.size)
5828     {
5829       complaint (&symfile_complaints,
5830                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5831                  offset);
5832       return 0;
5833     }
5834   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5835
5836   /* Read in the largest possible address.  */
5837   marker = read_address (obfd, buffer, cu, &dummy);
5838   if ((marker & mask) == mask)
5839     {
5840       /* If we found the largest possible address, then
5841          read the base address.  */
5842       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5843       buffer += 2 * addr_size;
5844       offset += 2 * addr_size;
5845       found_base = 1;
5846     }
5847
5848   low_set = 0;
5849
5850   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5851
5852   while (1)
5853     {
5854       CORE_ADDR range_beginning, range_end;
5855
5856       range_beginning = read_address (obfd, buffer, cu, &dummy);
5857       buffer += addr_size;
5858       range_end = read_address (obfd, buffer, cu, &dummy);
5859       buffer += addr_size;
5860       offset += 2 * addr_size;
5861
5862       /* An end of list marker is a pair of zero addresses.  */
5863       if (range_beginning == 0 && range_end == 0)
5864         /* Found the end of list entry.  */
5865         break;
5866
5867       /* Each base address selection entry is a pair of 2 values.
5868          The first is the largest possible address, the second is
5869          the base address.  Check for a base address here.  */
5870       if ((range_beginning & mask) == mask)
5871         {
5872           /* If we found the largest possible address, then
5873              read the base address.  */
5874           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5875           found_base = 1;
5876           continue;
5877         }
5878
5879       if (!found_base)
5880         {
5881           /* We have no valid base address for the ranges
5882              data.  */
5883           complaint (&symfile_complaints,
5884                      _("Invalid .debug_ranges data (no base address)"));
5885           return 0;
5886         }
5887
5888       if (range_beginning > range_end)
5889         {
5890           /* Inverted range entries are invalid.  */
5891           complaint (&symfile_complaints,
5892                      _("Invalid .debug_ranges data (inverted range)"));
5893           return 0;
5894         }
5895
5896       /* Empty range entries have no effect.  */
5897       if (range_beginning == range_end)
5898         continue;
5899
5900       range_beginning += base;
5901       range_end += base;
5902
5903       if (ranges_pst != NULL)
5904         addrmap_set_empty (objfile->psymtabs_addrmap,
5905                            range_beginning + baseaddr,
5906                            range_end - 1 + baseaddr,
5907                            ranges_pst);
5908
5909       /* FIXME: This is recording everything as a low-high
5910          segment of consecutive addresses.  We should have a
5911          data structure for discontiguous block ranges
5912          instead.  */
5913       if (! low_set)
5914         {
5915           low = range_beginning;
5916           high = range_end;
5917           low_set = 1;
5918         }
5919       else
5920         {
5921           if (range_beginning < low)
5922             low = range_beginning;
5923           if (range_end > high)
5924             high = range_end;
5925         }
5926     }
5927
5928   if (! low_set)
5929     /* If the first entry is an end-of-list marker, the range
5930        describes an empty scope, i.e. no instructions.  */
5931     return 0;
5932
5933   if (low_return)
5934     *low_return = low;
5935   if (high_return)
5936     *high_return = high;
5937   return 1;
5938 }
5939
5940 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5941    are present and valid, otherwise, return 0.  Return -1 if the range is
5942    discontinuous, i.e. derived from DW_AT_ranges information.  */
5943 static int
5944 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5945                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5946                       struct partial_symtab *pst)
5947 {
5948   struct attribute *attr;
5949   CORE_ADDR low = 0;
5950   CORE_ADDR high = 0;
5951   int ret = 0;
5952
5953   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5954   if (attr)
5955     {
5956       high = DW_ADDR (attr);
5957       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5958       if (attr)
5959         low = DW_ADDR (attr);
5960       else
5961         /* Found high w/o low attribute.  */
5962         return 0;
5963
5964       /* Found consecutive range of addresses.  */
5965       ret = 1;
5966     }
5967   else
5968     {
5969       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5970       if (attr != NULL)
5971         {
5972           /* Value of the DW_AT_ranges attribute is the offset in the
5973              .debug_ranges section.  */
5974           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5975             return 0;
5976           /* Found discontinuous range of addresses.  */
5977           ret = -1;
5978         }
5979     }
5980
5981   if (high < low)
5982     return 0;
5983
5984   /* When using the GNU linker, .gnu.linkonce. sections are used to
5985      eliminate duplicate copies of functions and vtables and such.
5986      The linker will arbitrarily choose one and discard the others.
5987      The AT_*_pc values for such functions refer to local labels in
5988      these sections.  If the section from that file was discarded, the
5989      labels are not in the output, so the relocs get a value of 0.
5990      If this is a discarded function, mark the pc bounds as invalid,
5991      so that GDB will ignore it.  */
5992   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5993     return 0;
5994
5995   *lowpc = low;
5996   *highpc = high;
5997   return ret;
5998 }
5999
6000 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6001    its low and high PC addresses.  Do nothing if these addresses could not
6002    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6003    and HIGHPC to the high address if greater than HIGHPC.  */
6004
6005 static void
6006 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6007                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6008                                  struct dwarf2_cu *cu)
6009 {
6010   CORE_ADDR low, high;
6011   struct die_info *child = die->child;
6012
6013   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6014     {
6015       *lowpc = min (*lowpc, low);
6016       *highpc = max (*highpc, high);
6017     }
6018
6019   /* If the language does not allow nested subprograms (either inside
6020      subprograms or lexical blocks), we're done.  */
6021   if (cu->language != language_ada)
6022     return;
6023
6024   /* Check all the children of the given DIE.  If it contains nested
6025      subprograms, then check their pc bounds.  Likewise, we need to
6026      check lexical blocks as well, as they may also contain subprogram
6027      definitions.  */
6028   while (child && child->tag)
6029     {
6030       if (child->tag == DW_TAG_subprogram
6031           || child->tag == DW_TAG_lexical_block)
6032         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6033       child = sibling_die (child);
6034     }
6035 }
6036
6037 /* Get the low and high pc's represented by the scope DIE, and store
6038    them in *LOWPC and *HIGHPC.  If the correct values can't be
6039    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6040
6041 static void
6042 get_scope_pc_bounds (struct die_info *die,
6043                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6044                      struct dwarf2_cu *cu)
6045 {
6046   CORE_ADDR best_low = (CORE_ADDR) -1;
6047   CORE_ADDR best_high = (CORE_ADDR) 0;
6048   CORE_ADDR current_low, current_high;
6049
6050   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6051     {
6052       best_low = current_low;
6053       best_high = current_high;
6054     }
6055   else
6056     {
6057       struct die_info *child = die->child;
6058
6059       while (child && child->tag)
6060         {
6061           switch (child->tag) {
6062           case DW_TAG_subprogram:
6063             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6064             break;
6065           case DW_TAG_namespace:
6066           case DW_TAG_module:
6067             /* FIXME: carlton/2004-01-16: Should we do this for
6068                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6069                that current GCC's always emit the DIEs corresponding
6070                to definitions of methods of classes as children of a
6071                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6072                the DIEs giving the declarations, which could be
6073                anywhere).  But I don't see any reason why the
6074                standards says that they have to be there.  */
6075             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6076
6077             if (current_low != ((CORE_ADDR) -1))
6078               {
6079                 best_low = min (best_low, current_low);
6080                 best_high = max (best_high, current_high);
6081               }
6082             break;
6083           default:
6084             /* Ignore.  */
6085             break;
6086           }
6087
6088           child = sibling_die (child);
6089         }
6090     }
6091
6092   *lowpc = best_low;
6093   *highpc = best_high;
6094 }
6095
6096 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6097    in DIE.  */
6098 static void
6099 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6100                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6101 {
6102   struct attribute *attr;
6103
6104   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6105   if (attr)
6106     {
6107       CORE_ADDR high = DW_ADDR (attr);
6108
6109       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6110       if (attr)
6111         {
6112           CORE_ADDR low = DW_ADDR (attr);
6113
6114           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6115         }
6116     }
6117
6118   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6119   if (attr)
6120     {
6121       bfd *obfd = cu->objfile->obfd;
6122
6123       /* The value of the DW_AT_ranges attribute is the offset of the
6124          address range list in the .debug_ranges section.  */
6125       unsigned long offset = DW_UNSND (attr);
6126       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6127
6128       /* For some target architectures, but not others, the
6129          read_address function sign-extends the addresses it returns.
6130          To recognize base address selection entries, we need a
6131          mask.  */
6132       unsigned int addr_size = cu->header.addr_size;
6133       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6134
6135       /* The base address, to which the next pair is relative.  Note
6136          that this 'base' is a DWARF concept: most entries in a range
6137          list are relative, to reduce the number of relocs against the
6138          debugging information.  This is separate from this function's
6139          'baseaddr' argument, which GDB uses to relocate debugging
6140          information from a shared library based on the address at
6141          which the library was loaded.  */
6142       CORE_ADDR base = cu->base_address;
6143       int base_known = cu->base_known;
6144
6145       gdb_assert (dwarf2_per_objfile->ranges.readin);
6146       if (offset >= dwarf2_per_objfile->ranges.size)
6147         {
6148           complaint (&symfile_complaints,
6149                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6150                      offset);
6151           return;
6152         }
6153
6154       for (;;)
6155         {
6156           unsigned int bytes_read;
6157           CORE_ADDR start, end;
6158
6159           start = read_address (obfd, buffer, cu, &bytes_read);
6160           buffer += bytes_read;
6161           end = read_address (obfd, buffer, cu, &bytes_read);
6162           buffer += bytes_read;
6163
6164           /* Did we find the end of the range list?  */
6165           if (start == 0 && end == 0)
6166             break;
6167
6168           /* Did we find a base address selection entry?  */
6169           else if ((start & base_select_mask) == base_select_mask)
6170             {
6171               base = end;
6172               base_known = 1;
6173             }
6174
6175           /* We found an ordinary address range.  */
6176           else
6177             {
6178               if (!base_known)
6179                 {
6180                   complaint (&symfile_complaints,
6181                              _("Invalid .debug_ranges data "
6182                                "(no base address)"));
6183                   return;
6184                 }
6185
6186               if (start > end)
6187                 {
6188                   /* Inverted range entries are invalid.  */
6189                   complaint (&symfile_complaints,
6190                              _("Invalid .debug_ranges data "
6191                                "(inverted range)"));
6192                   return;
6193                 }
6194
6195               /* Empty range entries have no effect.  */
6196               if (start == end)
6197                 continue;
6198
6199               record_block_range (block,
6200                                   baseaddr + base + start,
6201                                   baseaddr + base + end - 1);
6202             }
6203         }
6204     }
6205 }
6206
6207 /* Add an aggregate field to the field list.  */
6208
6209 static void
6210 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6211                   struct dwarf2_cu *cu)
6212 {
6213   struct objfile *objfile = cu->objfile;
6214   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6215   struct nextfield *new_field;
6216   struct attribute *attr;
6217   struct field *fp;
6218   char *fieldname = "";
6219
6220   /* Allocate a new field list entry and link it in.  */
6221   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6222   make_cleanup (xfree, new_field);
6223   memset (new_field, 0, sizeof (struct nextfield));
6224
6225   if (die->tag == DW_TAG_inheritance)
6226     {
6227       new_field->next = fip->baseclasses;
6228       fip->baseclasses = new_field;
6229     }
6230   else
6231     {
6232       new_field->next = fip->fields;
6233       fip->fields = new_field;
6234     }
6235   fip->nfields++;
6236
6237   /* Handle accessibility and virtuality of field.
6238      The default accessibility for members is public, the default
6239      accessibility for inheritance is private.  */
6240   if (die->tag != DW_TAG_inheritance)
6241     new_field->accessibility = DW_ACCESS_public;
6242   else
6243     new_field->accessibility = DW_ACCESS_private;
6244   new_field->virtuality = DW_VIRTUALITY_none;
6245
6246   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6247   if (attr)
6248     new_field->accessibility = DW_UNSND (attr);
6249   if (new_field->accessibility != DW_ACCESS_public)
6250     fip->non_public_fields = 1;
6251   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6252   if (attr)
6253     new_field->virtuality = DW_UNSND (attr);
6254
6255   fp = &new_field->field;
6256
6257   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6258     {
6259       /* Data member other than a C++ static data member.  */
6260
6261       /* Get type of field.  */
6262       fp->type = die_type (die, cu);
6263
6264       SET_FIELD_BITPOS (*fp, 0);
6265
6266       /* Get bit size of field (zero if none).  */
6267       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6268       if (attr)
6269         {
6270           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6271         }
6272       else
6273         {
6274           FIELD_BITSIZE (*fp) = 0;
6275         }
6276
6277       /* Get bit offset of field.  */
6278       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6279       if (attr)
6280         {
6281           int byte_offset = 0;
6282
6283           if (attr_form_is_section_offset (attr))
6284             dwarf2_complex_location_expr_complaint ();
6285           else if (attr_form_is_constant (attr))
6286             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6287           else if (attr_form_is_block (attr))
6288             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6289           else
6290             dwarf2_complex_location_expr_complaint ();
6291
6292           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6293         }
6294       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6295       if (attr)
6296         {
6297           if (gdbarch_bits_big_endian (gdbarch))
6298             {
6299               /* For big endian bits, the DW_AT_bit_offset gives the
6300                  additional bit offset from the MSB of the containing
6301                  anonymous object to the MSB of the field.  We don't
6302                  have to do anything special since we don't need to
6303                  know the size of the anonymous object.  */
6304               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6305             }
6306           else
6307             {
6308               /* For little endian bits, compute the bit offset to the
6309                  MSB of the anonymous object, subtract off the number of
6310                  bits from the MSB of the field to the MSB of the
6311                  object, and then subtract off the number of bits of
6312                  the field itself.  The result is the bit offset of
6313                  the LSB of the field.  */
6314               int anonymous_size;
6315               int bit_offset = DW_UNSND (attr);
6316
6317               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6318               if (attr)
6319                 {
6320                   /* The size of the anonymous object containing
6321                      the bit field is explicit, so use the
6322                      indicated size (in bytes).  */
6323                   anonymous_size = DW_UNSND (attr);
6324                 }
6325               else
6326                 {
6327                   /* The size of the anonymous object containing
6328                      the bit field must be inferred from the type
6329                      attribute of the data member containing the
6330                      bit field.  */
6331                   anonymous_size = TYPE_LENGTH (fp->type);
6332                 }
6333               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6334                 - bit_offset - FIELD_BITSIZE (*fp);
6335             }
6336         }
6337
6338       /* Get name of field.  */
6339       fieldname = dwarf2_name (die, cu);
6340       if (fieldname == NULL)
6341         fieldname = "";
6342
6343       /* The name is already allocated along with this objfile, so we don't
6344          need to duplicate it for the type.  */
6345       fp->name = fieldname;
6346
6347       /* Change accessibility for artificial fields (e.g. virtual table
6348          pointer or virtual base class pointer) to private.  */
6349       if (dwarf2_attr (die, DW_AT_artificial, cu))
6350         {
6351           FIELD_ARTIFICIAL (*fp) = 1;
6352           new_field->accessibility = DW_ACCESS_private;
6353           fip->non_public_fields = 1;
6354         }
6355     }
6356   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6357     {
6358       /* C++ static member.  */
6359
6360       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6361          is a declaration, but all versions of G++ as of this writing
6362          (so through at least 3.2.1) incorrectly generate
6363          DW_TAG_variable tags.  */
6364
6365       char *physname;
6366
6367       /* Get name of field.  */
6368       fieldname = dwarf2_name (die, cu);
6369       if (fieldname == NULL)
6370         return;
6371
6372       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6373       if (attr
6374           /* Only create a symbol if this is an external value.
6375              new_symbol checks this and puts the value in the global symbol
6376              table, which we want.  If it is not external, new_symbol
6377              will try to put the value in cu->list_in_scope which is wrong.  */
6378           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6379         {
6380           /* A static const member, not much different than an enum as far as
6381              we're concerned, except that we can support more types.  */
6382           new_symbol (die, NULL, cu);
6383         }
6384
6385       /* Get physical name.  */
6386       physname = (char *) dwarf2_physname (fieldname, die, cu);
6387
6388       /* The name is already allocated along with this objfile, so we don't
6389          need to duplicate it for the type.  */
6390       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6391       FIELD_TYPE (*fp) = die_type (die, cu);
6392       FIELD_NAME (*fp) = fieldname;
6393     }
6394   else if (die->tag == DW_TAG_inheritance)
6395     {
6396       /* C++ base class field.  */
6397       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6398       if (attr)
6399         {
6400           int byte_offset = 0;
6401
6402           if (attr_form_is_section_offset (attr))
6403             dwarf2_complex_location_expr_complaint ();
6404           else if (attr_form_is_constant (attr))
6405             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6406           else if (attr_form_is_block (attr))
6407             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6408           else
6409             dwarf2_complex_location_expr_complaint ();
6410
6411           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6412         }
6413       FIELD_BITSIZE (*fp) = 0;
6414       FIELD_TYPE (*fp) = die_type (die, cu);
6415       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6416       fip->nbaseclasses++;
6417     }
6418 }
6419
6420 /* Add a typedef defined in the scope of the FIP's class.  */
6421
6422 static void
6423 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6424                     struct dwarf2_cu *cu)
6425 {
6426   struct objfile *objfile = cu->objfile;
6427   struct typedef_field_list *new_field;
6428   struct attribute *attr;
6429   struct typedef_field *fp;
6430   char *fieldname = "";
6431
6432   /* Allocate a new field list entry and link it in.  */
6433   new_field = xzalloc (sizeof (*new_field));
6434   make_cleanup (xfree, new_field);
6435
6436   gdb_assert (die->tag == DW_TAG_typedef);
6437
6438   fp = &new_field->field;
6439
6440   /* Get name of field.  */
6441   fp->name = dwarf2_name (die, cu);
6442   if (fp->name == NULL)
6443     return;
6444
6445   fp->type = read_type_die (die, cu);
6446
6447   new_field->next = fip->typedef_field_list;
6448   fip->typedef_field_list = new_field;
6449   fip->typedef_field_list_count++;
6450 }
6451
6452 /* Create the vector of fields, and attach it to the type.  */
6453
6454 static void
6455 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6456                               struct dwarf2_cu *cu)
6457 {
6458   int nfields = fip->nfields;
6459
6460   /* Record the field count, allocate space for the array of fields,
6461      and create blank accessibility bitfields if necessary.  */
6462   TYPE_NFIELDS (type) = nfields;
6463   TYPE_FIELDS (type) = (struct field *)
6464     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6465   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6466
6467   if (fip->non_public_fields && cu->language != language_ada)
6468     {
6469       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6470
6471       TYPE_FIELD_PRIVATE_BITS (type) =
6472         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6473       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6474
6475       TYPE_FIELD_PROTECTED_BITS (type) =
6476         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6477       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6478
6479       TYPE_FIELD_IGNORE_BITS (type) =
6480         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6481       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6482     }
6483
6484   /* If the type has baseclasses, allocate and clear a bit vector for
6485      TYPE_FIELD_VIRTUAL_BITS.  */
6486   if (fip->nbaseclasses && cu->language != language_ada)
6487     {
6488       int num_bytes = B_BYTES (fip->nbaseclasses);
6489       unsigned char *pointer;
6490
6491       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6492       pointer = TYPE_ALLOC (type, num_bytes);
6493       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6494       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6495       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6496     }
6497
6498   /* Copy the saved-up fields into the field vector.  Start from the head of
6499      the list, adding to the tail of the field array, so that they end up in
6500      the same order in the array in which they were added to the list.  */
6501   while (nfields-- > 0)
6502     {
6503       struct nextfield *fieldp;
6504
6505       if (fip->fields)
6506         {
6507           fieldp = fip->fields;
6508           fip->fields = fieldp->next;
6509         }
6510       else
6511         {
6512           fieldp = fip->baseclasses;
6513           fip->baseclasses = fieldp->next;
6514         }
6515
6516       TYPE_FIELD (type, nfields) = fieldp->field;
6517       switch (fieldp->accessibility)
6518         {
6519         case DW_ACCESS_private:
6520           if (cu->language != language_ada)
6521             SET_TYPE_FIELD_PRIVATE (type, nfields);
6522           break;
6523
6524         case DW_ACCESS_protected:
6525           if (cu->language != language_ada)
6526             SET_TYPE_FIELD_PROTECTED (type, nfields);
6527           break;
6528
6529         case DW_ACCESS_public:
6530           break;
6531
6532         default:
6533           /* Unknown accessibility.  Complain and treat it as public.  */
6534           {
6535             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6536                        fieldp->accessibility);
6537           }
6538           break;
6539         }
6540       if (nfields < fip->nbaseclasses)
6541         {
6542           switch (fieldp->virtuality)
6543             {
6544             case DW_VIRTUALITY_virtual:
6545             case DW_VIRTUALITY_pure_virtual:
6546               if (cu->language == language_ada)
6547                 error (_("unexpected virtuality in component of Ada type"));
6548               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6549               break;
6550             }
6551         }
6552     }
6553 }
6554
6555 /* Add a member function to the proper fieldlist.  */
6556
6557 static void
6558 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6559                       struct type *type, struct dwarf2_cu *cu)
6560 {
6561   struct objfile *objfile = cu->objfile;
6562   struct attribute *attr;
6563   struct fnfieldlist *flp;
6564   int i;
6565   struct fn_field *fnp;
6566   char *fieldname;
6567   struct nextfnfield *new_fnfield;
6568   struct type *this_type;
6569
6570   if (cu->language == language_ada)
6571     error (_("unexpected member function in Ada type"));
6572
6573   /* Get name of member function.  */
6574   fieldname = dwarf2_name (die, cu);
6575   if (fieldname == NULL)
6576     return;
6577
6578   /* Look up member function name in fieldlist.  */
6579   for (i = 0; i < fip->nfnfields; i++)
6580     {
6581       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6582         break;
6583     }
6584
6585   /* Create new list element if necessary.  */
6586   if (i < fip->nfnfields)
6587     flp = &fip->fnfieldlists[i];
6588   else
6589     {
6590       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6591         {
6592           fip->fnfieldlists = (struct fnfieldlist *)
6593             xrealloc (fip->fnfieldlists,
6594                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6595                       * sizeof (struct fnfieldlist));
6596           if (fip->nfnfields == 0)
6597             make_cleanup (free_current_contents, &fip->fnfieldlists);
6598         }
6599       flp = &fip->fnfieldlists[fip->nfnfields];
6600       flp->name = fieldname;
6601       flp->length = 0;
6602       flp->head = NULL;
6603       i = fip->nfnfields++;
6604     }
6605
6606   /* Create a new member function field and chain it to the field list
6607      entry.  */
6608   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6609   make_cleanup (xfree, new_fnfield);
6610   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6611   new_fnfield->next = flp->head;
6612   flp->head = new_fnfield;
6613   flp->length++;
6614
6615   /* Fill in the member function field info.  */
6616   fnp = &new_fnfield->fnfield;
6617
6618   /* Delay processing of the physname until later.  */
6619   if (cu->language == language_cplus || cu->language == language_java)
6620     {
6621       add_to_method_list (type, i, flp->length - 1, fieldname,
6622                           die, cu);
6623     }
6624   else
6625     {
6626       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6627       fnp->physname = physname ? physname : "";
6628     }
6629
6630   fnp->type = alloc_type (objfile);
6631   this_type = read_type_die (die, cu);
6632   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6633     {
6634       int nparams = TYPE_NFIELDS (this_type);
6635
6636       /* TYPE is the domain of this method, and THIS_TYPE is the type
6637            of the method itself (TYPE_CODE_METHOD).  */
6638       smash_to_method_type (fnp->type, type,
6639                             TYPE_TARGET_TYPE (this_type),
6640                             TYPE_FIELDS (this_type),
6641                             TYPE_NFIELDS (this_type),
6642                             TYPE_VARARGS (this_type));
6643
6644       /* Handle static member functions.
6645          Dwarf2 has no clean way to discern C++ static and non-static
6646          member functions.  G++ helps GDB by marking the first
6647          parameter for non-static member functions (which is the this
6648          pointer) as artificial.  We obtain this information from
6649          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6650       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6651         fnp->voffset = VOFFSET_STATIC;
6652     }
6653   else
6654     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6655                dwarf2_full_name (fieldname, die, cu));
6656
6657   /* Get fcontext from DW_AT_containing_type if present.  */
6658   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6659     fnp->fcontext = die_containing_type (die, cu);
6660
6661   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6662      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6663
6664   /* Get accessibility.  */
6665   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6666   if (attr)
6667     {
6668       switch (DW_UNSND (attr))
6669         {
6670         case DW_ACCESS_private:
6671           fnp->is_private = 1;
6672           break;
6673         case DW_ACCESS_protected:
6674           fnp->is_protected = 1;
6675           break;
6676         }
6677     }
6678
6679   /* Check for artificial methods.  */
6680   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6681   if (attr && DW_UNSND (attr) != 0)
6682     fnp->is_artificial = 1;
6683
6684   /* Get index in virtual function table if it is a virtual member
6685      function.  For older versions of GCC, this is an offset in the
6686      appropriate virtual table, as specified by DW_AT_containing_type.
6687      For everyone else, it is an expression to be evaluated relative
6688      to the object address.  */
6689
6690   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6691   if (attr)
6692     {
6693       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6694         {
6695           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6696             {
6697               /* Old-style GCC.  */
6698               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6699             }
6700           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6701                    || (DW_BLOCK (attr)->size > 1
6702                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6703                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6704             {
6705               struct dwarf_block blk;
6706               int offset;
6707
6708               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6709                         ? 1 : 2);
6710               blk.size = DW_BLOCK (attr)->size - offset;
6711               blk.data = DW_BLOCK (attr)->data + offset;
6712               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6713               if ((fnp->voffset % cu->header.addr_size) != 0)
6714                 dwarf2_complex_location_expr_complaint ();
6715               else
6716                 fnp->voffset /= cu->header.addr_size;
6717               fnp->voffset += 2;
6718             }
6719           else
6720             dwarf2_complex_location_expr_complaint ();
6721
6722           if (!fnp->fcontext)
6723             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6724         }
6725       else if (attr_form_is_section_offset (attr))
6726         {
6727           dwarf2_complex_location_expr_complaint ();
6728         }
6729       else
6730         {
6731           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6732                                                  fieldname);
6733         }
6734     }
6735   else
6736     {
6737       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6738       if (attr && DW_UNSND (attr))
6739         {
6740           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6741           complaint (&symfile_complaints,
6742                      _("Member function \"%s\" (offset %d) is virtual "
6743                        "but the vtable offset is not specified"),
6744                      fieldname, die->offset);
6745           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6746           TYPE_CPLUS_DYNAMIC (type) = 1;
6747         }
6748     }
6749 }
6750
6751 /* Create the vector of member function fields, and attach it to the type.  */
6752
6753 static void
6754 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6755                                  struct dwarf2_cu *cu)
6756 {
6757   struct fnfieldlist *flp;
6758   int total_length = 0;
6759   int i;
6760
6761   if (cu->language == language_ada)
6762     error (_("unexpected member functions in Ada type"));
6763
6764   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6765   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6766     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6767
6768   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6769     {
6770       struct nextfnfield *nfp = flp->head;
6771       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6772       int k;
6773
6774       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6775       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6776       fn_flp->fn_fields = (struct fn_field *)
6777         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6778       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6779         fn_flp->fn_fields[k] = nfp->fnfield;
6780
6781       total_length += flp->length;
6782     }
6783
6784   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6785   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6786 }
6787
6788 /* Returns non-zero if NAME is the name of a vtable member in CU's
6789    language, zero otherwise.  */
6790 static int
6791 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6792 {
6793   static const char vptr[] = "_vptr";
6794   static const char vtable[] = "vtable";
6795
6796   /* Look for the C++ and Java forms of the vtable.  */
6797   if ((cu->language == language_java
6798        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6799        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6800        && is_cplus_marker (name[sizeof (vptr) - 1])))
6801     return 1;
6802
6803   return 0;
6804 }
6805
6806 /* GCC outputs unnamed structures that are really pointers to member
6807    functions, with the ABI-specified layout.  If TYPE describes
6808    such a structure, smash it into a member function type.
6809
6810    GCC shouldn't do this; it should just output pointer to member DIEs.
6811    This is GCC PR debug/28767.  */
6812
6813 static void
6814 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6815 {
6816   struct type *pfn_type, *domain_type, *new_type;
6817
6818   /* Check for a structure with no name and two children.  */
6819   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6820     return;
6821
6822   /* Check for __pfn and __delta members.  */
6823   if (TYPE_FIELD_NAME (type, 0) == NULL
6824       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6825       || TYPE_FIELD_NAME (type, 1) == NULL
6826       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6827     return;
6828
6829   /* Find the type of the method.  */
6830   pfn_type = TYPE_FIELD_TYPE (type, 0);
6831   if (pfn_type == NULL
6832       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6833       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6834     return;
6835
6836   /* Look for the "this" argument.  */
6837   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6838   if (TYPE_NFIELDS (pfn_type) == 0
6839       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6840       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6841     return;
6842
6843   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6844   new_type = alloc_type (objfile);
6845   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6846                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6847                         TYPE_VARARGS (pfn_type));
6848   smash_to_methodptr_type (type, new_type);
6849 }
6850
6851 /* Called when we find the DIE that starts a structure or union scope
6852    (definition) to create a type for the structure or union.  Fill in
6853    the type's name and general properties; the members will not be
6854    processed until process_structure_type.
6855
6856    NOTE: we need to call these functions regardless of whether or not the
6857    DIE has a DW_AT_name attribute, since it might be an anonymous
6858    structure or union.  This gets the type entered into our set of
6859    user defined types.
6860
6861    However, if the structure is incomplete (an opaque struct/union)
6862    then suppress creating a symbol table entry for it since gdb only
6863    wants to find the one with the complete definition.  Note that if
6864    it is complete, we just call new_symbol, which does it's own
6865    checking about whether the struct/union is anonymous or not (and
6866    suppresses creating a symbol table entry itself).  */
6867
6868 static struct type *
6869 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6870 {
6871   struct objfile *objfile = cu->objfile;
6872   struct type *type;
6873   struct attribute *attr;
6874   char *name;
6875
6876   /* If the definition of this type lives in .debug_types, read that type.
6877      Don't follow DW_AT_specification though, that will take us back up
6878      the chain and we want to go down.  */
6879   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6880   if (attr)
6881     {
6882       struct dwarf2_cu *type_cu = cu;
6883       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6884
6885       /* We could just recurse on read_structure_type, but we need to call
6886          get_die_type to ensure only one type for this DIE is created.
6887          This is important, for example, because for c++ classes we need
6888          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6889       type = read_type_die (type_die, type_cu);
6890
6891       /* TYPE_CU may not be the same as CU.
6892          Ensure TYPE is recorded in CU's type_hash table.  */
6893       return set_die_type (die, type, cu);
6894     }
6895
6896   type = alloc_type (objfile);
6897   INIT_CPLUS_SPECIFIC (type);
6898
6899   name = dwarf2_name (die, cu);
6900   if (name != NULL)
6901     {
6902       if (cu->language == language_cplus
6903           || cu->language == language_java)
6904         {
6905           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6906
6907           /* dwarf2_full_name might have already finished building the DIE's
6908              type.  If so, there is no need to continue.  */
6909           if (get_die_type (die, cu) != NULL)
6910             return get_die_type (die, cu);
6911
6912           TYPE_TAG_NAME (type) = full_name;
6913           if (die->tag == DW_TAG_structure_type
6914               || die->tag == DW_TAG_class_type)
6915             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6916         }
6917       else
6918         {
6919           /* The name is already allocated along with this objfile, so
6920              we don't need to duplicate it for the type.  */
6921           TYPE_TAG_NAME (type) = (char *) name;
6922           if (die->tag == DW_TAG_class_type)
6923             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6924         }
6925     }
6926
6927   if (die->tag == DW_TAG_structure_type)
6928     {
6929       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6930     }
6931   else if (die->tag == DW_TAG_union_type)
6932     {
6933       TYPE_CODE (type) = TYPE_CODE_UNION;
6934     }
6935   else
6936     {
6937       TYPE_CODE (type) = TYPE_CODE_CLASS;
6938     }
6939
6940   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6941     TYPE_DECLARED_CLASS (type) = 1;
6942
6943   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6944   if (attr)
6945     {
6946       TYPE_LENGTH (type) = DW_UNSND (attr);
6947     }
6948   else
6949     {
6950       TYPE_LENGTH (type) = 0;
6951     }
6952
6953   TYPE_STUB_SUPPORTED (type) = 1;
6954   if (die_is_declaration (die, cu))
6955     TYPE_STUB (type) = 1;
6956   else if (attr == NULL && die->child == NULL
6957            && producer_is_realview (cu->producer))
6958     /* RealView does not output the required DW_AT_declaration
6959        on incomplete types.  */
6960     TYPE_STUB (type) = 1;
6961
6962   /* We need to add the type field to the die immediately so we don't
6963      infinitely recurse when dealing with pointers to the structure
6964      type within the structure itself.  */
6965   set_die_type (die, type, cu);
6966
6967   /* set_die_type should be already done.  */
6968   set_descriptive_type (type, die, cu);
6969
6970   return type;
6971 }
6972
6973 /* Finish creating a structure or union type, including filling in
6974    its members and creating a symbol for it.  */
6975
6976 static void
6977 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6978 {
6979   struct objfile *objfile = cu->objfile;
6980   struct die_info *child_die = die->child;
6981   struct type *type;
6982
6983   type = get_die_type (die, cu);
6984   if (type == NULL)
6985     type = read_structure_type (die, cu);
6986
6987   if (die->child != NULL && ! die_is_declaration (die, cu))
6988     {
6989       struct field_info fi;
6990       struct die_info *child_die;
6991       VEC (symbolp) *template_args = NULL;
6992       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6993
6994       memset (&fi, 0, sizeof (struct field_info));
6995
6996       child_die = die->child;
6997
6998       while (child_die && child_die->tag)
6999         {
7000           if (child_die->tag == DW_TAG_member
7001               || child_die->tag == DW_TAG_variable)
7002             {
7003               /* NOTE: carlton/2002-11-05: A C++ static data member
7004                  should be a DW_TAG_member that is a declaration, but
7005                  all versions of G++ as of this writing (so through at
7006                  least 3.2.1) incorrectly generate DW_TAG_variable
7007                  tags for them instead.  */
7008               dwarf2_add_field (&fi, child_die, cu);
7009             }
7010           else if (child_die->tag == DW_TAG_subprogram)
7011             {
7012               /* C++ member function.  */
7013               dwarf2_add_member_fn (&fi, child_die, type, cu);
7014             }
7015           else if (child_die->tag == DW_TAG_inheritance)
7016             {
7017               /* C++ base class field.  */
7018               dwarf2_add_field (&fi, child_die, cu);
7019             }
7020           else if (child_die->tag == DW_TAG_typedef)
7021             dwarf2_add_typedef (&fi, child_die, cu);
7022           else if (child_die->tag == DW_TAG_template_type_param
7023                    || child_die->tag == DW_TAG_template_value_param)
7024             {
7025               struct symbol *arg = new_symbol (child_die, NULL, cu);
7026
7027               if (arg != NULL)
7028                 VEC_safe_push (symbolp, template_args, arg);
7029             }
7030
7031           child_die = sibling_die (child_die);
7032         }
7033
7034       /* Attach template arguments to type.  */
7035       if (! VEC_empty (symbolp, template_args))
7036         {
7037           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7038           TYPE_N_TEMPLATE_ARGUMENTS (type)
7039             = VEC_length (symbolp, template_args);
7040           TYPE_TEMPLATE_ARGUMENTS (type)
7041             = obstack_alloc (&objfile->objfile_obstack,
7042                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7043                               * sizeof (struct symbol *)));
7044           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7045                   VEC_address (symbolp, template_args),
7046                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7047                    * sizeof (struct symbol *)));
7048           VEC_free (symbolp, template_args);
7049         }
7050
7051       /* Attach fields and member functions to the type.  */
7052       if (fi.nfields)
7053         dwarf2_attach_fields_to_type (&fi, type, cu);
7054       if (fi.nfnfields)
7055         {
7056           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7057
7058           /* Get the type which refers to the base class (possibly this
7059              class itself) which contains the vtable pointer for the current
7060              class from the DW_AT_containing_type attribute.  This use of
7061              DW_AT_containing_type is a GNU extension.  */
7062
7063           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7064             {
7065               struct type *t = die_containing_type (die, cu);
7066
7067               TYPE_VPTR_BASETYPE (type) = t;
7068               if (type == t)
7069                 {
7070                   int i;
7071
7072                   /* Our own class provides vtbl ptr.  */
7073                   for (i = TYPE_NFIELDS (t) - 1;
7074                        i >= TYPE_N_BASECLASSES (t);
7075                        --i)
7076                     {
7077                       char *fieldname = TYPE_FIELD_NAME (t, i);
7078
7079                       if (is_vtable_name (fieldname, cu))
7080                         {
7081                           TYPE_VPTR_FIELDNO (type) = i;
7082                           break;
7083                         }
7084                     }
7085
7086                   /* Complain if virtual function table field not found.  */
7087                   if (i < TYPE_N_BASECLASSES (t))
7088                     complaint (&symfile_complaints,
7089                                _("virtual function table pointer "
7090                                  "not found when defining class '%s'"),
7091                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7092                                "");
7093                 }
7094               else
7095                 {
7096                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7097                 }
7098             }
7099           else if (cu->producer
7100                    && strncmp (cu->producer,
7101                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7102             {
7103               /* The IBM XLC compiler does not provide direct indication
7104                  of the containing type, but the vtable pointer is
7105                  always named __vfp.  */
7106
7107               int i;
7108
7109               for (i = TYPE_NFIELDS (type) - 1;
7110                    i >= TYPE_N_BASECLASSES (type);
7111                    --i)
7112                 {
7113                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7114                     {
7115                       TYPE_VPTR_FIELDNO (type) = i;
7116                       TYPE_VPTR_BASETYPE (type) = type;
7117                       break;
7118                     }
7119                 }
7120             }
7121         }
7122
7123       /* Copy fi.typedef_field_list linked list elements content into the
7124          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7125       if (fi.typedef_field_list)
7126         {
7127           int i = fi.typedef_field_list_count;
7128
7129           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7130           TYPE_TYPEDEF_FIELD_ARRAY (type)
7131             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7132           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7133
7134           /* Reverse the list order to keep the debug info elements order.  */
7135           while (--i >= 0)
7136             {
7137               struct typedef_field *dest, *src;
7138
7139               dest = &TYPE_TYPEDEF_FIELD (type, i);
7140               src = &fi.typedef_field_list->field;
7141               fi.typedef_field_list = fi.typedef_field_list->next;
7142               *dest = *src;
7143             }
7144         }
7145
7146       do_cleanups (back_to);
7147     }
7148
7149   quirk_gcc_member_function_pointer (type, cu->objfile);
7150
7151   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7152      snapshots) has been known to create a die giving a declaration
7153      for a class that has, as a child, a die giving a definition for a
7154      nested class.  So we have to process our children even if the
7155      current die is a declaration.  Normally, of course, a declaration
7156      won't have any children at all.  */
7157
7158   while (child_die != NULL && child_die->tag)
7159     {
7160       if (child_die->tag == DW_TAG_member
7161           || child_die->tag == DW_TAG_variable
7162           || child_die->tag == DW_TAG_inheritance
7163           || child_die->tag == DW_TAG_template_value_param
7164           || child_die->tag == DW_TAG_template_type_param)
7165         {
7166           /* Do nothing.  */
7167         }
7168       else
7169         process_die (child_die, cu);
7170
7171       child_die = sibling_die (child_die);
7172     }
7173
7174   /* Do not consider external references.  According to the DWARF standard,
7175      these DIEs are identified by the fact that they have no byte_size
7176      attribute, and a declaration attribute.  */
7177   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7178       || !die_is_declaration (die, cu))
7179     new_symbol (die, type, cu);
7180 }
7181
7182 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7183    complete the type's fields yet, or create any symbols.  */
7184
7185 static struct type *
7186 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7187 {
7188   struct objfile *objfile = cu->objfile;
7189   struct type *type;
7190   struct attribute *attr;
7191   const char *name;
7192
7193   /* If the definition of this type lives in .debug_types, read that type.
7194      Don't follow DW_AT_specification though, that will take us back up
7195      the chain and we want to go down.  */
7196   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7197   if (attr)
7198     {
7199       struct dwarf2_cu *type_cu = cu;
7200       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7201
7202       type = read_type_die (type_die, type_cu);
7203
7204       /* TYPE_CU may not be the same as CU.
7205          Ensure TYPE is recorded in CU's type_hash table.  */
7206       return set_die_type (die, type, cu);
7207     }
7208
7209   type = alloc_type (objfile);
7210
7211   TYPE_CODE (type) = TYPE_CODE_ENUM;
7212   name = dwarf2_full_name (NULL, die, cu);
7213   if (name != NULL)
7214     TYPE_TAG_NAME (type) = (char *) name;
7215
7216   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7217   if (attr)
7218     {
7219       TYPE_LENGTH (type) = DW_UNSND (attr);
7220     }
7221   else
7222     {
7223       TYPE_LENGTH (type) = 0;
7224     }
7225
7226   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7227      declared as private in the package spec, and then defined only
7228      inside the package body.  Such types are known as Taft Amendment
7229      Types.  When another package uses such a type, an incomplete DIE
7230      may be generated by the compiler.  */
7231   if (die_is_declaration (die, cu))
7232     TYPE_STUB (type) = 1;
7233
7234   return set_die_type (die, type, cu);
7235 }
7236
7237 /* Given a pointer to a die which begins an enumeration, process all
7238    the dies that define the members of the enumeration, and create the
7239    symbol for the enumeration type.
7240
7241    NOTE: We reverse the order of the element list.  */
7242
7243 static void
7244 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7245 {
7246   struct type *this_type;
7247
7248   this_type = get_die_type (die, cu);
7249   if (this_type == NULL)
7250     this_type = read_enumeration_type (die, cu);
7251
7252   if (die->child != NULL)
7253     {
7254       struct die_info *child_die;
7255       struct symbol *sym;
7256       struct field *fields = NULL;
7257       int num_fields = 0;
7258       int unsigned_enum = 1;
7259       char *name;
7260
7261       child_die = die->child;
7262       while (child_die && child_die->tag)
7263         {
7264           if (child_die->tag != DW_TAG_enumerator)
7265             {
7266               process_die (child_die, cu);
7267             }
7268           else
7269             {
7270               name = dwarf2_name (child_die, cu);
7271               if (name)
7272                 {
7273                   sym = new_symbol (child_die, this_type, cu);
7274                   if (SYMBOL_VALUE (sym) < 0)
7275                     unsigned_enum = 0;
7276
7277                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7278                     {
7279                       fields = (struct field *)
7280                         xrealloc (fields,
7281                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7282                                   * sizeof (struct field));
7283                     }
7284
7285                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7286                   FIELD_TYPE (fields[num_fields]) = NULL;
7287                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7288                   FIELD_BITSIZE (fields[num_fields]) = 0;
7289
7290                   num_fields++;
7291                 }
7292             }
7293
7294           child_die = sibling_die (child_die);
7295         }
7296
7297       if (num_fields)
7298         {
7299           TYPE_NFIELDS (this_type) = num_fields;
7300           TYPE_FIELDS (this_type) = (struct field *)
7301             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7302           memcpy (TYPE_FIELDS (this_type), fields,
7303                   sizeof (struct field) * num_fields);
7304           xfree (fields);
7305         }
7306       if (unsigned_enum)
7307         TYPE_UNSIGNED (this_type) = 1;
7308     }
7309
7310   new_symbol (die, this_type, cu);
7311 }
7312
7313 /* Extract all information from a DW_TAG_array_type DIE and put it in
7314    the DIE's type field.  For now, this only handles one dimensional
7315    arrays.  */
7316
7317 static struct type *
7318 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7319 {
7320   struct objfile *objfile = cu->objfile;
7321   struct die_info *child_die;
7322   struct type *type;
7323   struct type *element_type, *range_type, *index_type;
7324   struct type **range_types = NULL;
7325   struct attribute *attr;
7326   int ndim = 0;
7327   struct cleanup *back_to;
7328   char *name;
7329
7330   element_type = die_type (die, cu);
7331
7332   /* The die_type call above may have already set the type for this DIE.  */
7333   type = get_die_type (die, cu);
7334   if (type)
7335     return type;
7336
7337   /* Irix 6.2 native cc creates array types without children for
7338      arrays with unspecified length.  */
7339   if (die->child == NULL)
7340     {
7341       index_type = objfile_type (objfile)->builtin_int;
7342       range_type = create_range_type (NULL, index_type, 0, -1);
7343       type = create_array_type (NULL, element_type, range_type);
7344       return set_die_type (die, type, cu);
7345     }
7346
7347   back_to = make_cleanup (null_cleanup, NULL);
7348   child_die = die->child;
7349   while (child_die && child_die->tag)
7350     {
7351       if (child_die->tag == DW_TAG_subrange_type)
7352         {
7353           struct type *child_type = read_type_die (child_die, cu);
7354
7355           if (child_type != NULL)
7356             {
7357               /* The range type was succesfully read.  Save it for the
7358                  array type creation.  */
7359               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7360                 {
7361                   range_types = (struct type **)
7362                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7363                               * sizeof (struct type *));
7364                   if (ndim == 0)
7365                     make_cleanup (free_current_contents, &range_types);
7366                 }
7367               range_types[ndim++] = child_type;
7368             }
7369         }
7370       child_die = sibling_die (child_die);
7371     }
7372
7373   /* Dwarf2 dimensions are output from left to right, create the
7374      necessary array types in backwards order.  */
7375
7376   type = element_type;
7377
7378   if (read_array_order (die, cu) == DW_ORD_col_major)
7379     {
7380       int i = 0;
7381
7382       while (i < ndim)
7383         type = create_array_type (NULL, type, range_types[i++]);
7384     }
7385   else
7386     {
7387       while (ndim-- > 0)
7388         type = create_array_type (NULL, type, range_types[ndim]);
7389     }
7390
7391   /* Understand Dwarf2 support for vector types (like they occur on
7392      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7393      array type.  This is not part of the Dwarf2/3 standard yet, but a
7394      custom vendor extension.  The main difference between a regular
7395      array and the vector variant is that vectors are passed by value
7396      to functions.  */
7397   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7398   if (attr)
7399     make_vector_type (type);
7400
7401   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7402      implementation may choose to implement triple vectors using this
7403      attribute.  */
7404   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7405   if (attr)
7406     {
7407       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7408         TYPE_LENGTH (type) = DW_UNSND (attr);
7409       else
7410         complaint (&symfile_complaints,
7411                    _("DW_AT_byte_size for array type smaller "
7412                      "than the total size of elements"));
7413     }
7414
7415   name = dwarf2_name (die, cu);
7416   if (name)
7417     TYPE_NAME (type) = name;
7418
7419   /* Install the type in the die.  */
7420   set_die_type (die, type, cu);
7421
7422   /* set_die_type should be already done.  */
7423   set_descriptive_type (type, die, cu);
7424
7425   do_cleanups (back_to);
7426
7427   return type;
7428 }
7429
7430 static enum dwarf_array_dim_ordering
7431 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7432 {
7433   struct attribute *attr;
7434
7435   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7436
7437   if (attr) return DW_SND (attr);
7438
7439   /* GNU F77 is a special case, as at 08/2004 array type info is the
7440      opposite order to the dwarf2 specification, but data is still
7441      laid out as per normal fortran.
7442
7443      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7444      version checking.  */
7445
7446   if (cu->language == language_fortran
7447       && cu->producer && strstr (cu->producer, "GNU F77"))
7448     {
7449       return DW_ORD_row_major;
7450     }
7451
7452   switch (cu->language_defn->la_array_ordering)
7453     {
7454     case array_column_major:
7455       return DW_ORD_col_major;
7456     case array_row_major:
7457     default:
7458       return DW_ORD_row_major;
7459     };
7460 }
7461
7462 /* Extract all information from a DW_TAG_set_type DIE and put it in
7463    the DIE's type field.  */
7464
7465 static struct type *
7466 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7467 {
7468   struct type *domain_type, *set_type;
7469   struct attribute *attr;
7470
7471   domain_type = die_type (die, cu);
7472
7473   /* The die_type call above may have already set the type for this DIE.  */
7474   set_type = get_die_type (die, cu);
7475   if (set_type)
7476     return set_type;
7477
7478   set_type = create_set_type (NULL, domain_type);
7479
7480   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7481   if (attr)
7482     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7483
7484   return set_die_type (die, set_type, cu);
7485 }
7486
7487 /* First cut: install each common block member as a global variable.  */
7488
7489 static void
7490 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7491 {
7492   struct die_info *child_die;
7493   struct attribute *attr;
7494   struct symbol *sym;
7495   CORE_ADDR base = (CORE_ADDR) 0;
7496
7497   attr = dwarf2_attr (die, DW_AT_location, cu);
7498   if (attr)
7499     {
7500       /* Support the .debug_loc offsets.  */
7501       if (attr_form_is_block (attr))
7502         {
7503           base = decode_locdesc (DW_BLOCK (attr), cu);
7504         }
7505       else if (attr_form_is_section_offset (attr))
7506         {
7507           dwarf2_complex_location_expr_complaint ();
7508         }
7509       else
7510         {
7511           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7512                                                  "common block member");
7513         }
7514     }
7515   if (die->child != NULL)
7516     {
7517       child_die = die->child;
7518       while (child_die && child_die->tag)
7519         {
7520           sym = new_symbol (child_die, NULL, cu);
7521           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7522           if (sym != NULL && attr != NULL)
7523             {
7524               CORE_ADDR byte_offset = 0;
7525
7526               if (attr_form_is_section_offset (attr))
7527                 dwarf2_complex_location_expr_complaint ();
7528               else if (attr_form_is_constant (attr))
7529                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7530               else if (attr_form_is_block (attr))
7531                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7532               else
7533                 dwarf2_complex_location_expr_complaint ();
7534
7535               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7536               add_symbol_to_list (sym, &global_symbols);
7537             }
7538           child_die = sibling_die (child_die);
7539         }
7540     }
7541 }
7542
7543 /* Create a type for a C++ namespace.  */
7544
7545 static struct type *
7546 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7547 {
7548   struct objfile *objfile = cu->objfile;
7549   const char *previous_prefix, *name;
7550   int is_anonymous;
7551   struct type *type;
7552
7553   /* For extensions, reuse the type of the original namespace.  */
7554   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7555     {
7556       struct die_info *ext_die;
7557       struct dwarf2_cu *ext_cu = cu;
7558
7559       ext_die = dwarf2_extension (die, &ext_cu);
7560       type = read_type_die (ext_die, ext_cu);
7561
7562       /* EXT_CU may not be the same as CU.
7563          Ensure TYPE is recorded in CU's type_hash table.  */
7564       return set_die_type (die, type, cu);
7565     }
7566
7567   name = namespace_name (die, &is_anonymous, cu);
7568
7569   /* Now build the name of the current namespace.  */
7570
7571   previous_prefix = determine_prefix (die, cu);
7572   if (previous_prefix[0] != '\0')
7573     name = typename_concat (&objfile->objfile_obstack,
7574                             previous_prefix, name, 0, cu);
7575
7576   /* Create the type.  */
7577   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7578                     objfile);
7579   TYPE_NAME (type) = (char *) name;
7580   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7581
7582   return set_die_type (die, type, cu);
7583 }
7584
7585 /* Read a C++ namespace.  */
7586
7587 static void
7588 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7589 {
7590   struct objfile *objfile = cu->objfile;
7591   int is_anonymous;
7592
7593   /* Add a symbol associated to this if we haven't seen the namespace
7594      before.  Also, add a using directive if it's an anonymous
7595      namespace.  */
7596
7597   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7598     {
7599       struct type *type;
7600
7601       type = read_type_die (die, cu);
7602       new_symbol (die, type, cu);
7603
7604       namespace_name (die, &is_anonymous, cu);
7605       if (is_anonymous)
7606         {
7607           const char *previous_prefix = determine_prefix (die, cu);
7608
7609           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7610                                   NULL, &objfile->objfile_obstack);
7611         }
7612     }
7613
7614   if (die->child != NULL)
7615     {
7616       struct die_info *child_die = die->child;
7617
7618       while (child_die && child_die->tag)
7619         {
7620           process_die (child_die, cu);
7621           child_die = sibling_die (child_die);
7622         }
7623     }
7624 }
7625
7626 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7627    imported module.  Still we need that type as local Fortran "use ... only"
7628    declaration imports depend on the created type in determine_prefix.  */
7629
7630 static struct type *
7631 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7632 {
7633   struct objfile *objfile = cu->objfile;
7634   char *module_name;
7635   struct type *type;
7636
7637   module_name = dwarf2_name (die, cu);
7638   if (!module_name)
7639     complaint (&symfile_complaints,
7640                _("DW_TAG_module has no name, offset 0x%x"),
7641                die->offset);
7642   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7643
7644   /* determine_prefix uses TYPE_TAG_NAME.  */
7645   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7646
7647   return set_die_type (die, type, cu);
7648 }
7649
7650 /* Read a Fortran module.  */
7651
7652 static void
7653 read_module (struct die_info *die, struct dwarf2_cu *cu)
7654 {
7655   struct die_info *child_die = die->child;
7656
7657   while (child_die && child_die->tag)
7658     {
7659       process_die (child_die, cu);
7660       child_die = sibling_die (child_die);
7661     }
7662 }
7663
7664 /* Return the name of the namespace represented by DIE.  Set
7665    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7666    namespace.  */
7667
7668 static const char *
7669 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7670 {
7671   struct die_info *current_die;
7672   const char *name = NULL;
7673
7674   /* Loop through the extensions until we find a name.  */
7675
7676   for (current_die = die;
7677        current_die != NULL;
7678        current_die = dwarf2_extension (die, &cu))
7679     {
7680       name = dwarf2_name (current_die, cu);
7681       if (name != NULL)
7682         break;
7683     }
7684
7685   /* Is it an anonymous namespace?  */
7686
7687   *is_anonymous = (name == NULL);
7688   if (*is_anonymous)
7689     name = "(anonymous namespace)";
7690
7691   return name;
7692 }
7693
7694 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7695    the user defined type vector.  */
7696
7697 static struct type *
7698 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7699 {
7700   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7701   struct comp_unit_head *cu_header = &cu->header;
7702   struct type *type;
7703   struct attribute *attr_byte_size;
7704   struct attribute *attr_address_class;
7705   int byte_size, addr_class;
7706   struct type *target_type;
7707
7708   target_type = die_type (die, cu);
7709
7710   /* The die_type call above may have already set the type for this DIE.  */
7711   type = get_die_type (die, cu);
7712   if (type)
7713     return type;
7714
7715   type = lookup_pointer_type (target_type);
7716
7717   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7718   if (attr_byte_size)
7719     byte_size = DW_UNSND (attr_byte_size);
7720   else
7721     byte_size = cu_header->addr_size;
7722
7723   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7724   if (attr_address_class)
7725     addr_class = DW_UNSND (attr_address_class);
7726   else
7727     addr_class = DW_ADDR_none;
7728
7729   /* If the pointer size or address class is different than the
7730      default, create a type variant marked as such and set the
7731      length accordingly.  */
7732   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7733     {
7734       if (gdbarch_address_class_type_flags_p (gdbarch))
7735         {
7736           int type_flags;
7737
7738           type_flags = gdbarch_address_class_type_flags
7739                          (gdbarch, byte_size, addr_class);
7740           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7741                       == 0);
7742           type = make_type_with_address_space (type, type_flags);
7743         }
7744       else if (TYPE_LENGTH (type) != byte_size)
7745         {
7746           complaint (&symfile_complaints,
7747                      _("invalid pointer size %d"), byte_size);
7748         }
7749       else
7750         {
7751           /* Should we also complain about unhandled address classes?  */
7752         }
7753     }
7754
7755   TYPE_LENGTH (type) = byte_size;
7756   return set_die_type (die, type, cu);
7757 }
7758
7759 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7760    the user defined type vector.  */
7761
7762 static struct type *
7763 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7764 {
7765   struct type *type;
7766   struct type *to_type;
7767   struct type *domain;
7768
7769   to_type = die_type (die, cu);
7770   domain = die_containing_type (die, cu);
7771
7772   /* The calls above may have already set the type for this DIE.  */
7773   type = get_die_type (die, cu);
7774   if (type)
7775     return type;
7776
7777   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7778     type = lookup_methodptr_type (to_type);
7779   else
7780     type = lookup_memberptr_type (to_type, domain);
7781
7782   return set_die_type (die, type, cu);
7783 }
7784
7785 /* Extract all information from a DW_TAG_reference_type DIE and add to
7786    the user defined type vector.  */
7787
7788 static struct type *
7789 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7790 {
7791   struct comp_unit_head *cu_header = &cu->header;
7792   struct type *type, *target_type;
7793   struct attribute *attr;
7794
7795   target_type = die_type (die, cu);
7796
7797   /* The die_type call above may have already set the type for this DIE.  */
7798   type = get_die_type (die, cu);
7799   if (type)
7800     return type;
7801
7802   type = lookup_reference_type (target_type);
7803   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7804   if (attr)
7805     {
7806       TYPE_LENGTH (type) = DW_UNSND (attr);
7807     }
7808   else
7809     {
7810       TYPE_LENGTH (type) = cu_header->addr_size;
7811     }
7812   return set_die_type (die, type, cu);
7813 }
7814
7815 static struct type *
7816 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7817 {
7818   struct type *base_type, *cv_type;
7819
7820   base_type = die_type (die, cu);
7821
7822   /* The die_type call above may have already set the type for this DIE.  */
7823   cv_type = get_die_type (die, cu);
7824   if (cv_type)
7825     return cv_type;
7826
7827   /* In case the const qualifier is applied to an array type, the element type
7828      is so qualified, not the array type (section 6.7.3 of C99).  */
7829   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7830     {
7831       struct type *el_type, *inner_array;
7832
7833       base_type = copy_type (base_type);
7834       inner_array = base_type;
7835
7836       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7837         {
7838           TYPE_TARGET_TYPE (inner_array) =
7839             copy_type (TYPE_TARGET_TYPE (inner_array));
7840           inner_array = TYPE_TARGET_TYPE (inner_array);
7841         }
7842
7843       el_type = TYPE_TARGET_TYPE (inner_array);
7844       TYPE_TARGET_TYPE (inner_array) =
7845         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7846
7847       return set_die_type (die, base_type, cu);
7848     }
7849
7850   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7851   return set_die_type (die, cv_type, cu);
7852 }
7853
7854 static struct type *
7855 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7856 {
7857   struct type *base_type, *cv_type;
7858
7859   base_type = die_type (die, cu);
7860
7861   /* The die_type call above may have already set the type for this DIE.  */
7862   cv_type = get_die_type (die, cu);
7863   if (cv_type)
7864     return cv_type;
7865
7866   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7867   return set_die_type (die, cv_type, cu);
7868 }
7869
7870 /* Extract all information from a DW_TAG_string_type DIE and add to
7871    the user defined type vector.  It isn't really a user defined type,
7872    but it behaves like one, with other DIE's using an AT_user_def_type
7873    attribute to reference it.  */
7874
7875 static struct type *
7876 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7877 {
7878   struct objfile *objfile = cu->objfile;
7879   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7880   struct type *type, *range_type, *index_type, *char_type;
7881   struct attribute *attr;
7882   unsigned int length;
7883
7884   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7885   if (attr)
7886     {
7887       length = DW_UNSND (attr);
7888     }
7889   else
7890     {
7891       /* Check for the DW_AT_byte_size attribute.  */
7892       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7893       if (attr)
7894         {
7895           length = DW_UNSND (attr);
7896         }
7897       else
7898         {
7899           length = 1;
7900         }
7901     }
7902
7903   index_type = objfile_type (objfile)->builtin_int;
7904   range_type = create_range_type (NULL, index_type, 1, length);
7905   char_type = language_string_char_type (cu->language_defn, gdbarch);
7906   type = create_string_type (NULL, char_type, range_type);
7907
7908   return set_die_type (die, type, cu);
7909 }
7910
7911 /* Handle DIES due to C code like:
7912
7913    struct foo
7914    {
7915    int (*funcp)(int a, long l);
7916    int b;
7917    };
7918
7919    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
7920
7921 static struct type *
7922 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7923 {
7924   struct type *type;            /* Type that this function returns.  */
7925   struct type *ftype;           /* Function that returns above type.  */
7926   struct attribute *attr;
7927
7928   type = die_type (die, cu);
7929
7930   /* The die_type call above may have already set the type for this DIE.  */
7931   ftype = get_die_type (die, cu);
7932   if (ftype)
7933     return ftype;
7934
7935   ftype = lookup_function_type (type);
7936
7937   /* All functions in C++, Pascal and Java have prototypes.  */
7938   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7939   if ((attr && (DW_UNSND (attr) != 0))
7940       || cu->language == language_cplus
7941       || cu->language == language_java
7942       || cu->language == language_pascal)
7943     TYPE_PROTOTYPED (ftype) = 1;
7944   else if (producer_is_realview (cu->producer))
7945     /* RealView does not emit DW_AT_prototyped.  We can not
7946        distinguish prototyped and unprototyped functions; default to
7947        prototyped, since that is more common in modern code (and
7948        RealView warns about unprototyped functions).  */
7949     TYPE_PROTOTYPED (ftype) = 1;
7950
7951   /* Store the calling convention in the type if it's available in
7952      the subroutine die.  Otherwise set the calling convention to
7953      the default value DW_CC_normal.  */
7954   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7955   if (attr)
7956     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
7957   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
7958     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
7959   else
7960     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
7961
7962   /* We need to add the subroutine type to the die immediately so
7963      we don't infinitely recurse when dealing with parameters
7964      declared as the same subroutine type.  */
7965   set_die_type (die, ftype, cu);
7966
7967   if (die->child != NULL)
7968     {
7969       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7970       struct die_info *child_die;
7971       int nparams, iparams;
7972
7973       /* Count the number of parameters.
7974          FIXME: GDB currently ignores vararg functions, but knows about
7975          vararg member functions.  */
7976       nparams = 0;
7977       child_die = die->child;
7978       while (child_die && child_die->tag)
7979         {
7980           if (child_die->tag == DW_TAG_formal_parameter)
7981             nparams++;
7982           else if (child_die->tag == DW_TAG_unspecified_parameters)
7983             TYPE_VARARGS (ftype) = 1;
7984           child_die = sibling_die (child_die);
7985         }
7986
7987       /* Allocate storage for parameters and fill them in.  */
7988       TYPE_NFIELDS (ftype) = nparams;
7989       TYPE_FIELDS (ftype) = (struct field *)
7990         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7991
7992       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7993          even if we error out during the parameters reading below.  */
7994       for (iparams = 0; iparams < nparams; iparams++)
7995         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7996
7997       iparams = 0;
7998       child_die = die->child;
7999       while (child_die && child_die->tag)
8000         {
8001           if (child_die->tag == DW_TAG_formal_parameter)
8002             {
8003               struct type *arg_type;
8004
8005               /* DWARF version 2 has no clean way to discern C++
8006                  static and non-static member functions.  G++ helps
8007                  GDB by marking the first parameter for non-static
8008                  member functions (which is the this pointer) as
8009                  artificial.  We pass this information to
8010                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8011
8012                  DWARF version 3 added DW_AT_object_pointer, which GCC
8013                  4.5 does not yet generate.  */
8014               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8015               if (attr)
8016                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8017               else
8018                 {
8019                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8020
8021                   /* GCC/43521: In java, the formal parameter
8022                      "this" is sometimes not marked with DW_AT_artificial.  */
8023                   if (cu->language == language_java)
8024                     {
8025                       const char *name = dwarf2_name (child_die, cu);
8026
8027                       if (name && !strcmp (name, "this"))
8028                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8029                     }
8030                 }
8031               arg_type = die_type (child_die, cu);
8032
8033               /* RealView does not mark THIS as const, which the testsuite
8034                  expects.  GCC marks THIS as const in method definitions,
8035                  but not in the class specifications (GCC PR 43053).  */
8036               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8037                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8038                 {
8039                   int is_this = 0;
8040                   struct dwarf2_cu *arg_cu = cu;
8041                   const char *name = dwarf2_name (child_die, cu);
8042
8043                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8044                   if (attr)
8045                     {
8046                       /* If the compiler emits this, use it.  */
8047                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8048                         is_this = 1;
8049                     }
8050                   else if (name && strcmp (name, "this") == 0)
8051                     /* Function definitions will have the argument names.  */
8052                     is_this = 1;
8053                   else if (name == NULL && iparams == 0)
8054                     /* Declarations may not have the names, so like
8055                        elsewhere in GDB, assume an artificial first
8056                        argument is "this".  */
8057                     is_this = 1;
8058
8059                   if (is_this)
8060                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8061                                              arg_type, 0);
8062                 }
8063
8064               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8065               iparams++;
8066             }
8067           child_die = sibling_die (child_die);
8068         }
8069     }
8070
8071   return ftype;
8072 }
8073
8074 static struct type *
8075 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8076 {
8077   struct objfile *objfile = cu->objfile;
8078   const char *name = NULL;
8079   struct type *this_type;
8080
8081   name = dwarf2_full_name (NULL, die, cu);
8082   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8083                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8084   TYPE_NAME (this_type) = (char *) name;
8085   set_die_type (die, this_type, cu);
8086   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8087   return this_type;
8088 }
8089
8090 /* Find a representation of a given base type and install
8091    it in the TYPE field of the die.  */
8092
8093 static struct type *
8094 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8095 {
8096   struct objfile *objfile = cu->objfile;
8097   struct type *type;
8098   struct attribute *attr;
8099   int encoding = 0, size = 0;
8100   char *name;
8101   enum type_code code = TYPE_CODE_INT;
8102   int type_flags = 0;
8103   struct type *target_type = NULL;
8104
8105   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8106   if (attr)
8107     {
8108       encoding = DW_UNSND (attr);
8109     }
8110   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8111   if (attr)
8112     {
8113       size = DW_UNSND (attr);
8114     }
8115   name = dwarf2_name (die, cu);
8116   if (!name)
8117     {
8118       complaint (&symfile_complaints,
8119                  _("DW_AT_name missing from DW_TAG_base_type"));
8120     }
8121
8122   switch (encoding)
8123     {
8124       case DW_ATE_address:
8125         /* Turn DW_ATE_address into a void * pointer.  */
8126         code = TYPE_CODE_PTR;
8127         type_flags |= TYPE_FLAG_UNSIGNED;
8128         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8129         break;
8130       case DW_ATE_boolean:
8131         code = TYPE_CODE_BOOL;
8132         type_flags |= TYPE_FLAG_UNSIGNED;
8133         break;
8134       case DW_ATE_complex_float:
8135         code = TYPE_CODE_COMPLEX;
8136         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8137         break;
8138       case DW_ATE_decimal_float:
8139         code = TYPE_CODE_DECFLOAT;
8140         break;
8141       case DW_ATE_float:
8142         code = TYPE_CODE_FLT;
8143         break;
8144       case DW_ATE_signed:
8145         break;
8146       case DW_ATE_unsigned:
8147         type_flags |= TYPE_FLAG_UNSIGNED;
8148         break;
8149       case DW_ATE_signed_char:
8150         if (cu->language == language_ada || cu->language == language_m2
8151             || cu->language == language_pascal)
8152           code = TYPE_CODE_CHAR;
8153         break;
8154       case DW_ATE_unsigned_char:
8155         if (cu->language == language_ada || cu->language == language_m2
8156             || cu->language == language_pascal)
8157           code = TYPE_CODE_CHAR;
8158         type_flags |= TYPE_FLAG_UNSIGNED;
8159         break;
8160       case DW_ATE_UTF:
8161         /* We just treat this as an integer and then recognize the
8162            type by name elsewhere.  */
8163         break;
8164
8165       default:
8166         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8167                    dwarf_type_encoding_name (encoding));
8168         break;
8169     }
8170
8171   type = init_type (code, size, type_flags, NULL, objfile);
8172   TYPE_NAME (type) = name;
8173   TYPE_TARGET_TYPE (type) = target_type;
8174
8175   if (name && strcmp (name, "char") == 0)
8176     TYPE_NOSIGN (type) = 1;
8177
8178   return set_die_type (die, type, cu);
8179 }
8180
8181 /* Read the given DW_AT_subrange DIE.  */
8182
8183 static struct type *
8184 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8185 {
8186   struct type *base_type;
8187   struct type *range_type;
8188   struct attribute *attr;
8189   LONGEST low = 0;
8190   LONGEST high = -1;
8191   char *name;
8192   LONGEST negative_mask;
8193
8194   base_type = die_type (die, cu);
8195   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8196   check_typedef (base_type);
8197
8198   /* The die_type call above may have already set the type for this DIE.  */
8199   range_type = get_die_type (die, cu);
8200   if (range_type)
8201     return range_type;
8202
8203   if (cu->language == language_fortran)
8204     {
8205       /* FORTRAN implies a lower bound of 1, if not given.  */
8206       low = 1;
8207     }
8208
8209   /* FIXME: For variable sized arrays either of these could be
8210      a variable rather than a constant value.  We'll allow it,
8211      but we don't know how to handle it.  */
8212   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8213   if (attr)
8214     low = dwarf2_get_attr_constant_value (attr, 0);
8215
8216   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8217   if (attr)
8218     {
8219       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8220         {
8221           /* GCC encodes arrays with unspecified or dynamic length
8222              with a DW_FORM_block1 attribute or a reference attribute.
8223              FIXME: GDB does not yet know how to handle dynamic
8224              arrays properly, treat them as arrays with unspecified
8225              length for now.
8226
8227              FIXME: jimb/2003-09-22: GDB does not really know
8228              how to handle arrays of unspecified length
8229              either; we just represent them as zero-length
8230              arrays.  Choose an appropriate upper bound given
8231              the lower bound we've computed above.  */
8232           high = low - 1;
8233         }
8234       else
8235         high = dwarf2_get_attr_constant_value (attr, 1);
8236     }
8237   else
8238     {
8239       attr = dwarf2_attr (die, DW_AT_count, cu);
8240       if (attr)
8241         {
8242           int count = dwarf2_get_attr_constant_value (attr, 1);
8243           high = low + count - 1;
8244         }
8245       else
8246         {
8247           /* Unspecified array length.  */
8248           high = low - 1;
8249         }
8250     }
8251
8252   /* Dwarf-2 specifications explicitly allows to create subrange types
8253      without specifying a base type.
8254      In that case, the base type must be set to the type of
8255      the lower bound, upper bound or count, in that order, if any of these
8256      three attributes references an object that has a type.
8257      If no base type is found, the Dwarf-2 specifications say that
8258      a signed integer type of size equal to the size of an address should
8259      be used.
8260      For the following C code: `extern char gdb_int [];'
8261      GCC produces an empty range DIE.
8262      FIXME: muller/2010-05-28: Possible references to object for low bound,
8263      high bound or count are not yet handled by this code.  */
8264   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8265     {
8266       struct objfile *objfile = cu->objfile;
8267       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8268       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8269       struct type *int_type = objfile_type (objfile)->builtin_int;
8270
8271       /* Test "int", "long int", and "long long int" objfile types,
8272          and select the first one having a size above or equal to the
8273          architecture address size.  */
8274       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8275         base_type = int_type;
8276       else
8277         {
8278           int_type = objfile_type (objfile)->builtin_long;
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_long;
8284               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8285                 base_type = int_type;
8286             }
8287         }
8288     }
8289
8290   negative_mask =
8291     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8292   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8293     low |= negative_mask;
8294   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8295     high |= negative_mask;
8296
8297   range_type = create_range_type (NULL, base_type, low, high);
8298
8299   /* Mark arrays with dynamic length at least as an array of unspecified
8300      length.  GDB could check the boundary but before it gets implemented at
8301      least allow accessing the array elements.  */
8302   if (attr && attr->form == DW_FORM_block1)
8303     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8304
8305   /* Ada expects an empty array on no boundary attributes.  */
8306   if (attr == NULL && cu->language != language_ada)
8307     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8308
8309   name = dwarf2_name (die, cu);
8310   if (name)
8311     TYPE_NAME (range_type) = name;
8312
8313   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8314   if (attr)
8315     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8316
8317   set_die_type (die, range_type, cu);
8318
8319   /* set_die_type should be already done.  */
8320   set_descriptive_type (range_type, die, cu);
8321
8322   return range_type;
8323 }
8324
8325 static struct type *
8326 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8327 {
8328   struct type *type;
8329
8330   /* For now, we only support the C meaning of an unspecified type: void.  */
8331
8332   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8333   TYPE_NAME (type) = dwarf2_name (die, cu);
8334
8335   return set_die_type (die, type, cu);
8336 }
8337
8338 /* Trivial hash function for die_info: the hash value of a DIE
8339    is its offset in .debug_info for this objfile.  */
8340
8341 static hashval_t
8342 die_hash (const void *item)
8343 {
8344   const struct die_info *die = item;
8345
8346   return die->offset;
8347 }
8348
8349 /* Trivial comparison function for die_info structures: two DIEs
8350    are equal if they have the same offset.  */
8351
8352 static int
8353 die_eq (const void *item_lhs, const void *item_rhs)
8354 {
8355   const struct die_info *die_lhs = item_lhs;
8356   const struct die_info *die_rhs = item_rhs;
8357
8358   return die_lhs->offset == die_rhs->offset;
8359 }
8360
8361 /* Read a whole compilation unit into a linked list of dies.  */
8362
8363 static struct die_info *
8364 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8365 {
8366   struct die_reader_specs reader_specs;
8367   int read_abbrevs = 0;
8368   struct cleanup *back_to = NULL;
8369   struct die_info *die;
8370
8371   if (cu->dwarf2_abbrevs == NULL)
8372     {
8373       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8374       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8375       read_abbrevs = 1;
8376     }
8377
8378   gdb_assert (cu->die_hash == NULL);
8379   cu->die_hash
8380     = htab_create_alloc_ex (cu->header.length / 12,
8381                             die_hash,
8382                             die_eq,
8383                             NULL,
8384                             &cu->comp_unit_obstack,
8385                             hashtab_obstack_allocate,
8386                             dummy_obstack_deallocate);
8387
8388   init_cu_die_reader (&reader_specs, cu);
8389
8390   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8391
8392   if (read_abbrevs)
8393     do_cleanups (back_to);
8394
8395   return die;
8396 }
8397
8398 /* Main entry point for reading a DIE and all children.
8399    Read the DIE and dump it if requested.  */
8400
8401 static struct die_info *
8402 read_die_and_children (const struct die_reader_specs *reader,
8403                        gdb_byte *info_ptr,
8404                        gdb_byte **new_info_ptr,
8405                        struct die_info *parent)
8406 {
8407   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8408                                                      new_info_ptr, parent);
8409
8410   if (dwarf2_die_debug)
8411     {
8412       fprintf_unfiltered (gdb_stdlog,
8413                           "\nRead die from %s of %s:\n",
8414                           reader->buffer == dwarf2_per_objfile->info.buffer
8415                           ? ".debug_info"
8416                           : reader->buffer == dwarf2_per_objfile->types.buffer
8417                           ? ".debug_types"
8418                           : "unknown section",
8419                           reader->abfd->filename);
8420       dump_die (result, dwarf2_die_debug);
8421     }
8422
8423   return result;
8424 }
8425
8426 /* Read a single die and all its descendents.  Set the die's sibling
8427    field to NULL; set other fields in the die correctly, and set all
8428    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8429    location of the info_ptr after reading all of those dies.  PARENT
8430    is the parent of the die in question.  */
8431
8432 static struct die_info *
8433 read_die_and_children_1 (const struct die_reader_specs *reader,
8434                          gdb_byte *info_ptr,
8435                          gdb_byte **new_info_ptr,
8436                          struct die_info *parent)
8437 {
8438   struct die_info *die;
8439   gdb_byte *cur_ptr;
8440   int has_children;
8441
8442   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8443   if (die == NULL)
8444     {
8445       *new_info_ptr = cur_ptr;
8446       return NULL;
8447     }
8448   store_in_ref_table (die, reader->cu);
8449
8450   if (has_children)
8451     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8452   else
8453     {
8454       die->child = NULL;
8455       *new_info_ptr = cur_ptr;
8456     }
8457
8458   die->sibling = NULL;
8459   die->parent = parent;
8460   return die;
8461 }
8462
8463 /* Read a die, all of its descendents, and all of its siblings; set
8464    all of the fields of all of the dies correctly.  Arguments are as
8465    in read_die_and_children.  */
8466
8467 static struct die_info *
8468 read_die_and_siblings (const struct die_reader_specs *reader,
8469                        gdb_byte *info_ptr,
8470                        gdb_byte **new_info_ptr,
8471                        struct die_info *parent)
8472 {
8473   struct die_info *first_die, *last_sibling;
8474   gdb_byte *cur_ptr;
8475
8476   cur_ptr = info_ptr;
8477   first_die = last_sibling = NULL;
8478
8479   while (1)
8480     {
8481       struct die_info *die
8482         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8483
8484       if (die == NULL)
8485         {
8486           *new_info_ptr = cur_ptr;
8487           return first_die;
8488         }
8489
8490       if (!first_die)
8491         first_die = die;
8492       else
8493         last_sibling->sibling = die;
8494
8495       last_sibling = die;
8496     }
8497 }
8498
8499 /* Read the die from the .debug_info section buffer.  Set DIEP to
8500    point to a newly allocated die with its information, except for its
8501    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8502    whether the die has children or not.  */
8503
8504 static gdb_byte *
8505 read_full_die (const struct die_reader_specs *reader,
8506                struct die_info **diep, gdb_byte *info_ptr,
8507                int *has_children)
8508 {
8509   unsigned int abbrev_number, bytes_read, i, offset;
8510   struct abbrev_info *abbrev;
8511   struct die_info *die;
8512   struct dwarf2_cu *cu = reader->cu;
8513   bfd *abfd = reader->abfd;
8514
8515   offset = info_ptr - reader->buffer;
8516   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8517   info_ptr += bytes_read;
8518   if (!abbrev_number)
8519     {
8520       *diep = NULL;
8521       *has_children = 0;
8522       return info_ptr;
8523     }
8524
8525   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8526   if (!abbrev)
8527     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8528            abbrev_number,
8529            bfd_get_filename (abfd));
8530
8531   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8532   die->offset = offset;
8533   die->tag = abbrev->tag;
8534   die->abbrev = abbrev_number;
8535
8536   die->num_attrs = abbrev->num_attrs;
8537
8538   for (i = 0; i < abbrev->num_attrs; ++i)
8539     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8540                                abfd, info_ptr, cu);
8541
8542   *diep = die;
8543   *has_children = abbrev->has_children;
8544   return info_ptr;
8545 }
8546
8547 /* In DWARF version 2, the description of the debugging information is
8548    stored in a separate .debug_abbrev section.  Before we read any
8549    dies from a section we read in all abbreviations and install them
8550    in a hash table.  This function also sets flags in CU describing
8551    the data found in the abbrev table.  */
8552
8553 static void
8554 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8555 {
8556   struct comp_unit_head *cu_header = &cu->header;
8557   gdb_byte *abbrev_ptr;
8558   struct abbrev_info *cur_abbrev;
8559   unsigned int abbrev_number, bytes_read, abbrev_name;
8560   unsigned int abbrev_form, hash_number;
8561   struct attr_abbrev *cur_attrs;
8562   unsigned int allocated_attrs;
8563
8564   /* Initialize dwarf2 abbrevs.  */
8565   obstack_init (&cu->abbrev_obstack);
8566   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8567                                       (ABBREV_HASH_SIZE
8568                                        * sizeof (struct abbrev_info *)));
8569   memset (cu->dwarf2_abbrevs, 0,
8570           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8571
8572   dwarf2_read_section (dwarf2_per_objfile->objfile,
8573                        &dwarf2_per_objfile->abbrev);
8574   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8575   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8576   abbrev_ptr += bytes_read;
8577
8578   allocated_attrs = ATTR_ALLOC_CHUNK;
8579   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8580
8581   /* Loop until we reach an abbrev number of 0.  */
8582   while (abbrev_number)
8583     {
8584       cur_abbrev = dwarf_alloc_abbrev (cu);
8585
8586       /* read in abbrev header */
8587       cur_abbrev->number = abbrev_number;
8588       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8589       abbrev_ptr += bytes_read;
8590       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8591       abbrev_ptr += 1;
8592
8593       if (cur_abbrev->tag == DW_TAG_namespace)
8594         cu->has_namespace_info = 1;
8595
8596       /* now read in declarations */
8597       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8598       abbrev_ptr += bytes_read;
8599       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8600       abbrev_ptr += bytes_read;
8601       while (abbrev_name)
8602         {
8603           if (cur_abbrev->num_attrs == allocated_attrs)
8604             {
8605               allocated_attrs += ATTR_ALLOC_CHUNK;
8606               cur_attrs
8607                 = xrealloc (cur_attrs, (allocated_attrs
8608                                         * sizeof (struct attr_abbrev)));
8609             }
8610
8611           /* Record whether this compilation unit might have
8612              inter-compilation-unit references.  If we don't know what form
8613              this attribute will have, then it might potentially be a
8614              DW_FORM_ref_addr, so we conservatively expect inter-CU
8615              references.  */
8616
8617           if (abbrev_form == DW_FORM_ref_addr
8618               || abbrev_form == DW_FORM_indirect)
8619             cu->has_form_ref_addr = 1;
8620
8621           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8622           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8623           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8624           abbrev_ptr += bytes_read;
8625           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8626           abbrev_ptr += bytes_read;
8627         }
8628
8629       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8630                                          (cur_abbrev->num_attrs
8631                                           * sizeof (struct attr_abbrev)));
8632       memcpy (cur_abbrev->attrs, cur_attrs,
8633               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8634
8635       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8636       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8637       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8638
8639       /* Get next abbreviation.
8640          Under Irix6 the abbreviations for a compilation unit are not
8641          always properly terminated with an abbrev number of 0.
8642          Exit loop if we encounter an abbreviation which we have
8643          already read (which means we are about to read the abbreviations
8644          for the next compile unit) or if the end of the abbreviation
8645          table is reached.  */
8646       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8647           >= dwarf2_per_objfile->abbrev.size)
8648         break;
8649       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8650       abbrev_ptr += bytes_read;
8651       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8652         break;
8653     }
8654
8655   xfree (cur_attrs);
8656 }
8657
8658 /* Release the memory used by the abbrev table for a compilation unit.  */
8659
8660 static void
8661 dwarf2_free_abbrev_table (void *ptr_to_cu)
8662 {
8663   struct dwarf2_cu *cu = ptr_to_cu;
8664
8665   obstack_free (&cu->abbrev_obstack, NULL);
8666   cu->dwarf2_abbrevs = NULL;
8667 }
8668
8669 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8670
8671 static struct abbrev_info *
8672 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8673 {
8674   unsigned int hash_number;
8675   struct abbrev_info *abbrev;
8676
8677   hash_number = number % ABBREV_HASH_SIZE;
8678   abbrev = cu->dwarf2_abbrevs[hash_number];
8679
8680   while (abbrev)
8681     {
8682       if (abbrev->number == number)
8683         return abbrev;
8684       else
8685         abbrev = abbrev->next;
8686     }
8687   return NULL;
8688 }
8689
8690 /* Returns nonzero if TAG represents a type that we might generate a partial
8691    symbol for.  */
8692
8693 static int
8694 is_type_tag_for_partial (int tag)
8695 {
8696   switch (tag)
8697     {
8698 #if 0
8699     /* Some types that would be reasonable to generate partial symbols for,
8700        that we don't at present.  */
8701     case DW_TAG_array_type:
8702     case DW_TAG_file_type:
8703     case DW_TAG_ptr_to_member_type:
8704     case DW_TAG_set_type:
8705     case DW_TAG_string_type:
8706     case DW_TAG_subroutine_type:
8707 #endif
8708     case DW_TAG_base_type:
8709     case DW_TAG_class_type:
8710     case DW_TAG_interface_type:
8711     case DW_TAG_enumeration_type:
8712     case DW_TAG_structure_type:
8713     case DW_TAG_subrange_type:
8714     case DW_TAG_typedef:
8715     case DW_TAG_union_type:
8716       return 1;
8717     default:
8718       return 0;
8719     }
8720 }
8721
8722 /* Load all DIEs that are interesting for partial symbols into memory.  */
8723
8724 static struct partial_die_info *
8725 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8726                    int building_psymtab, struct dwarf2_cu *cu)
8727 {
8728   struct partial_die_info *part_die;
8729   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8730   struct abbrev_info *abbrev;
8731   unsigned int bytes_read;
8732   unsigned int load_all = 0;
8733
8734   int nesting_level = 1;
8735
8736   parent_die = NULL;
8737   last_die = NULL;
8738
8739   if (cu->per_cu && cu->per_cu->load_all_dies)
8740     load_all = 1;
8741
8742   cu->partial_dies
8743     = htab_create_alloc_ex (cu->header.length / 12,
8744                             partial_die_hash,
8745                             partial_die_eq,
8746                             NULL,
8747                             &cu->comp_unit_obstack,
8748                             hashtab_obstack_allocate,
8749                             dummy_obstack_deallocate);
8750
8751   part_die = obstack_alloc (&cu->comp_unit_obstack,
8752                             sizeof (struct partial_die_info));
8753
8754   while (1)
8755     {
8756       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8757
8758       /* A NULL abbrev means the end of a series of children.  */
8759       if (abbrev == NULL)
8760         {
8761           if (--nesting_level == 0)
8762             {
8763               /* PART_DIE was probably the last thing allocated on the
8764                  comp_unit_obstack, so we could call obstack_free
8765                  here.  We don't do that because the waste is small,
8766                  and will be cleaned up when we're done with this
8767                  compilation unit.  This way, we're also more robust
8768                  against other users of the comp_unit_obstack.  */
8769               return first_die;
8770             }
8771           info_ptr += bytes_read;
8772           last_die = parent_die;
8773           parent_die = parent_die->die_parent;
8774           continue;
8775         }
8776
8777       /* Check for template arguments.  We never save these; if
8778          they're seen, we just mark the parent, and go on our way.  */
8779       if (parent_die != NULL
8780           && cu->language == language_cplus
8781           && (abbrev->tag == DW_TAG_template_type_param
8782               || abbrev->tag == DW_TAG_template_value_param))
8783         {
8784           parent_die->has_template_arguments = 1;
8785
8786           if (!load_all)
8787             {
8788               /* We don't need a partial DIE for the template argument.  */
8789               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8790                                        cu);
8791               continue;
8792             }
8793         }
8794
8795       /* We only recurse into subprograms looking for template arguments.
8796          Skip their other children.  */
8797       if (!load_all
8798           && cu->language == language_cplus
8799           && parent_die != NULL
8800           && parent_die->tag == DW_TAG_subprogram)
8801         {
8802           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8803           continue;
8804         }
8805
8806       /* Check whether this DIE is interesting enough to save.  Normally
8807          we would not be interested in members here, but there may be
8808          later variables referencing them via DW_AT_specification (for
8809          static members).  */
8810       if (!load_all
8811           && !is_type_tag_for_partial (abbrev->tag)
8812           && abbrev->tag != DW_TAG_constant
8813           && abbrev->tag != DW_TAG_enumerator
8814           && abbrev->tag != DW_TAG_subprogram
8815           && abbrev->tag != DW_TAG_lexical_block
8816           && abbrev->tag != DW_TAG_variable
8817           && abbrev->tag != DW_TAG_namespace
8818           && abbrev->tag != DW_TAG_module
8819           && abbrev->tag != DW_TAG_member)
8820         {
8821           /* Otherwise we skip to the next sibling, if any.  */
8822           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8823           continue;
8824         }
8825
8826       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8827                                    buffer, info_ptr, cu);
8828
8829       /* This two-pass algorithm for processing partial symbols has a
8830          high cost in cache pressure.  Thus, handle some simple cases
8831          here which cover the majority of C partial symbols.  DIEs
8832          which neither have specification tags in them, nor could have
8833          specification tags elsewhere pointing at them, can simply be
8834          processed and discarded.
8835
8836          This segment is also optional; scan_partial_symbols and
8837          add_partial_symbol will handle these DIEs if we chain
8838          them in normally.  When compilers which do not emit large
8839          quantities of duplicate debug information are more common,
8840          this code can probably be removed.  */
8841
8842       /* Any complete simple types at the top level (pretty much all
8843          of them, for a language without namespaces), can be processed
8844          directly.  */
8845       if (parent_die == NULL
8846           && part_die->has_specification == 0
8847           && part_die->is_declaration == 0
8848           && (part_die->tag == DW_TAG_typedef
8849               || part_die->tag == DW_TAG_base_type
8850               || part_die->tag == DW_TAG_subrange_type))
8851         {
8852           if (building_psymtab && part_die->name != NULL)
8853             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8854                                  VAR_DOMAIN, LOC_TYPEDEF,
8855                                  &cu->objfile->static_psymbols,
8856                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8857           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8858           continue;
8859         }
8860
8861       /* If we're at the second level, and we're an enumerator, and
8862          our parent has no specification (meaning possibly lives in a
8863          namespace elsewhere), then we can add the partial symbol now
8864          instead of queueing it.  */
8865       if (part_die->tag == DW_TAG_enumerator
8866           && parent_die != NULL
8867           && parent_die->die_parent == NULL
8868           && parent_die->tag == DW_TAG_enumeration_type
8869           && parent_die->has_specification == 0)
8870         {
8871           if (part_die->name == NULL)
8872             complaint (&symfile_complaints,
8873                        _("malformed enumerator DIE ignored"));
8874           else if (building_psymtab)
8875             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8876                                  VAR_DOMAIN, LOC_CONST,
8877                                  (cu->language == language_cplus
8878                                   || cu->language == language_java)
8879                                  ? &cu->objfile->global_psymbols
8880                                  : &cu->objfile->static_psymbols,
8881                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8882
8883           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8884           continue;
8885         }
8886
8887       /* We'll save this DIE so link it in.  */
8888       part_die->die_parent = parent_die;
8889       part_die->die_sibling = NULL;
8890       part_die->die_child = NULL;
8891
8892       if (last_die && last_die == parent_die)
8893         last_die->die_child = part_die;
8894       else if (last_die)
8895         last_die->die_sibling = part_die;
8896
8897       last_die = part_die;
8898
8899       if (first_die == NULL)
8900         first_die = part_die;
8901
8902       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8903          find interesting need to be in the hash table, because we
8904          also have the parent/sibling/child chains; only those that we
8905          might refer to by offset later during partial symbol reading.
8906
8907          For now this means things that might have be the target of a
8908          DW_AT_specification, DW_AT_abstract_origin, or
8909          DW_AT_extension.  DW_AT_extension will refer only to
8910          namespaces; DW_AT_abstract_origin refers to functions (and
8911          many things under the function DIE, but we do not recurse
8912          into function DIEs during partial symbol reading) and
8913          possibly variables as well; DW_AT_specification refers to
8914          declarations.  Declarations ought to have the DW_AT_declaration
8915          flag.  It happens that GCC forgets to put it in sometimes, but
8916          only for functions, not for types.
8917
8918          Adding more things than necessary to the hash table is harmless
8919          except for the performance cost.  Adding too few will result in
8920          wasted time in find_partial_die, when we reread the compilation
8921          unit with load_all_dies set.  */
8922
8923       if (load_all
8924           || abbrev->tag == DW_TAG_constant
8925           || abbrev->tag == DW_TAG_subprogram
8926           || abbrev->tag == DW_TAG_variable
8927           || abbrev->tag == DW_TAG_namespace
8928           || part_die->is_declaration)
8929         {
8930           void **slot;
8931
8932           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8933                                            part_die->offset, INSERT);
8934           *slot = part_die;
8935         }
8936
8937       part_die = obstack_alloc (&cu->comp_unit_obstack,
8938                                 sizeof (struct partial_die_info));
8939
8940       /* For some DIEs we want to follow their children (if any).  For C
8941          we have no reason to follow the children of structures; for other
8942          languages we have to, so that we can get at method physnames
8943          to infer fully qualified class names, for DW_AT_specification,
8944          and for C++ template arguments.  For C++, we also look one level
8945          inside functions to find template arguments (if the name of the
8946          function does not already contain the template arguments).
8947
8948          For Ada, we need to scan the children of subprograms and lexical
8949          blocks as well because Ada allows the definition of nested
8950          entities that could be interesting for the debugger, such as
8951          nested subprograms for instance.  */
8952       if (last_die->has_children
8953           && (load_all
8954               || last_die->tag == DW_TAG_namespace
8955               || last_die->tag == DW_TAG_module
8956               || last_die->tag == DW_TAG_enumeration_type
8957               || (cu->language == language_cplus
8958                   && last_die->tag == DW_TAG_subprogram
8959                   && (last_die->name == NULL
8960                       || strchr (last_die->name, '<') == NULL))
8961               || (cu->language != language_c
8962                   && (last_die->tag == DW_TAG_class_type
8963                       || last_die->tag == DW_TAG_interface_type
8964                       || last_die->tag == DW_TAG_structure_type
8965                       || last_die->tag == DW_TAG_union_type))
8966               || (cu->language == language_ada
8967                   && (last_die->tag == DW_TAG_subprogram
8968                       || last_die->tag == DW_TAG_lexical_block))))
8969         {
8970           nesting_level++;
8971           parent_die = last_die;
8972           continue;
8973         }
8974
8975       /* Otherwise we skip to the next sibling, if any.  */
8976       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8977
8978       /* Back to the top, do it again.  */
8979     }
8980 }
8981
8982 /* Read a minimal amount of information into the minimal die structure.  */
8983
8984 static gdb_byte *
8985 read_partial_die (struct partial_die_info *part_die,
8986                   struct abbrev_info *abbrev,
8987                   unsigned int abbrev_len, bfd *abfd,
8988                   gdb_byte *buffer, gdb_byte *info_ptr,
8989                   struct dwarf2_cu *cu)
8990 {
8991   unsigned int i;
8992   struct attribute attr;
8993   int has_low_pc_attr = 0;
8994   int has_high_pc_attr = 0;
8995
8996   memset (part_die, 0, sizeof (struct partial_die_info));
8997
8998   part_die->offset = info_ptr - buffer;
8999
9000   info_ptr += abbrev_len;
9001
9002   if (abbrev == NULL)
9003     return info_ptr;
9004
9005   part_die->tag = abbrev->tag;
9006   part_die->has_children = abbrev->has_children;
9007
9008   for (i = 0; i < abbrev->num_attrs; ++i)
9009     {
9010       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9011
9012       /* Store the data if it is of an attribute we want to keep in a
9013          partial symbol table.  */
9014       switch (attr.name)
9015         {
9016         case DW_AT_name:
9017           switch (part_die->tag)
9018             {
9019             case DW_TAG_compile_unit:
9020             case DW_TAG_type_unit:
9021               /* Compilation units have a DW_AT_name that is a filename, not
9022                  a source language identifier.  */
9023             case DW_TAG_enumeration_type:
9024             case DW_TAG_enumerator:
9025               /* These tags always have simple identifiers already; no need
9026                  to canonicalize them.  */
9027               part_die->name = DW_STRING (&attr);
9028               break;
9029             default:
9030               part_die->name
9031                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9032                                             &cu->objfile->objfile_obstack);
9033               break;
9034             }
9035           break;
9036         case DW_AT_linkage_name:
9037         case DW_AT_MIPS_linkage_name:
9038           /* Note that both forms of linkage name might appear.  We
9039              assume they will be the same, and we only store the last
9040              one we see.  */
9041           if (cu->language == language_ada)
9042             part_die->name = DW_STRING (&attr);
9043           part_die->linkage_name = DW_STRING (&attr);
9044           break;
9045         case DW_AT_low_pc:
9046           has_low_pc_attr = 1;
9047           part_die->lowpc = DW_ADDR (&attr);
9048           break;
9049         case DW_AT_high_pc:
9050           has_high_pc_attr = 1;
9051           part_die->highpc = DW_ADDR (&attr);
9052           break;
9053         case DW_AT_location:
9054           /* Support the .debug_loc offsets.  */
9055           if (attr_form_is_block (&attr))
9056             {
9057                part_die->locdesc = DW_BLOCK (&attr);
9058             }
9059           else if (attr_form_is_section_offset (&attr))
9060             {
9061               dwarf2_complex_location_expr_complaint ();
9062             }
9063           else
9064             {
9065               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9066                                                      "partial symbol information");
9067             }
9068           break;
9069         case DW_AT_external:
9070           part_die->is_external = DW_UNSND (&attr);
9071           break;
9072         case DW_AT_declaration:
9073           part_die->is_declaration = DW_UNSND (&attr);
9074           break;
9075         case DW_AT_type:
9076           part_die->has_type = 1;
9077           break;
9078         case DW_AT_abstract_origin:
9079         case DW_AT_specification:
9080         case DW_AT_extension:
9081           part_die->has_specification = 1;
9082           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9083           break;
9084         case DW_AT_sibling:
9085           /* Ignore absolute siblings, they might point outside of
9086              the current compile unit.  */
9087           if (attr.form == DW_FORM_ref_addr)
9088             complaint (&symfile_complaints,
9089                        _("ignoring absolute DW_AT_sibling"));
9090           else
9091             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9092           break;
9093         case DW_AT_byte_size:
9094           part_die->has_byte_size = 1;
9095           break;
9096         case DW_AT_calling_convention:
9097           /* DWARF doesn't provide a way to identify a program's source-level
9098              entry point.  DW_AT_calling_convention attributes are only meant
9099              to describe functions' calling conventions.
9100
9101              However, because it's a necessary piece of information in
9102              Fortran, and because DW_CC_program is the only piece of debugging
9103              information whose definition refers to a 'main program' at all,
9104              several compilers have begun marking Fortran main programs with
9105              DW_CC_program --- even when those functions use the standard
9106              calling conventions.
9107
9108              So until DWARF specifies a way to provide this information and
9109              compilers pick up the new representation, we'll support this
9110              practice.  */
9111           if (DW_UNSND (&attr) == DW_CC_program
9112               && cu->language == language_fortran)
9113             {
9114               set_main_name (part_die->name);
9115
9116               /* As this DIE has a static linkage the name would be difficult
9117                  to look up later.  */
9118               language_of_main = language_fortran;
9119             }
9120           break;
9121         default:
9122           break;
9123         }
9124     }
9125
9126   /* When using the GNU linker, .gnu.linkonce. sections are used to
9127      eliminate duplicate copies of functions and vtables and such.
9128      The linker will arbitrarily choose one and discard the others.
9129      The AT_*_pc values for such functions refer to local labels in
9130      these sections.  If the section from that file was discarded, the
9131      labels are not in the output, so the relocs get a value of 0.
9132      If this is a discarded function, mark the pc bounds as invalid,
9133      so that GDB will ignore it.  */
9134   if (has_low_pc_attr && has_high_pc_attr
9135       && part_die->lowpc < part_die->highpc
9136       && (part_die->lowpc != 0
9137           || dwarf2_per_objfile->has_section_at_zero))
9138     part_die->has_pc_info = 1;
9139
9140   return info_ptr;
9141 }
9142
9143 /* Find a cached partial DIE at OFFSET in CU.  */
9144
9145 static struct partial_die_info *
9146 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9147 {
9148   struct partial_die_info *lookup_die = NULL;
9149   struct partial_die_info part_die;
9150
9151   part_die.offset = offset;
9152   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9153
9154   return lookup_die;
9155 }
9156
9157 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9158    except in the case of .debug_types DIEs which do not reference
9159    outside their CU (they do however referencing other types via
9160    DW_FORM_sig8).  */
9161
9162 static struct partial_die_info *
9163 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9164 {
9165   struct dwarf2_per_cu_data *per_cu = NULL;
9166   struct partial_die_info *pd = NULL;
9167
9168   if (cu->per_cu->from_debug_types)
9169     {
9170       pd = find_partial_die_in_comp_unit (offset, cu);
9171       if (pd != NULL)
9172         return pd;
9173       goto not_found;
9174     }
9175
9176   if (offset_in_cu_p (&cu->header, offset))
9177     {
9178       pd = find_partial_die_in_comp_unit (offset, cu);
9179       if (pd != NULL)
9180         return pd;
9181     }
9182
9183   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9184
9185   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9186     load_partial_comp_unit (per_cu, cu->objfile);
9187
9188   per_cu->cu->last_used = 0;
9189   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9190
9191   if (pd == NULL && per_cu->load_all_dies == 0)
9192     {
9193       struct cleanup *back_to;
9194       struct partial_die_info comp_unit_die;
9195       struct abbrev_info *abbrev;
9196       unsigned int bytes_read;
9197       char *info_ptr;
9198
9199       per_cu->load_all_dies = 1;
9200
9201       /* Re-read the DIEs.  */
9202       back_to = make_cleanup (null_cleanup, 0);
9203       if (per_cu->cu->dwarf2_abbrevs == NULL)
9204         {
9205           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9206           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9207         }
9208       info_ptr = (dwarf2_per_objfile->info.buffer
9209                   + per_cu->cu->header.offset
9210                   + per_cu->cu->header.first_die_offset);
9211       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9212       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9213                                    per_cu->cu->objfile->obfd,
9214                                    dwarf2_per_objfile->info.buffer, info_ptr,
9215                                    per_cu->cu);
9216       if (comp_unit_die.has_children)
9217         load_partial_dies (per_cu->cu->objfile->obfd,
9218                            dwarf2_per_objfile->info.buffer, info_ptr,
9219                            0, per_cu->cu);
9220       do_cleanups (back_to);
9221
9222       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9223     }
9224
9225  not_found:
9226
9227   if (pd == NULL)
9228     internal_error (__FILE__, __LINE__,
9229                     _("could not find partial DIE 0x%x "
9230                       "in cache [from module %s]\n"),
9231                     offset, bfd_get_filename (cu->objfile->obfd));
9232   return pd;
9233 }
9234
9235 /* See if we can figure out if the class lives in a namespace.  We do
9236    this by looking for a member function; its demangled name will
9237    contain namespace info, if there is any.  */
9238
9239 static void
9240 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9241                                   struct dwarf2_cu *cu)
9242 {
9243   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9244      what template types look like, because the demangler
9245      frequently doesn't give the same name as the debug info.  We
9246      could fix this by only using the demangled name to get the
9247      prefix (but see comment in read_structure_type).  */
9248
9249   struct partial_die_info *real_pdi;
9250   struct partial_die_info *child_pdi;
9251
9252   /* If this DIE (this DIE's specification, if any) has a parent, then
9253      we should not do this.  We'll prepend the parent's fully qualified
9254      name when we create the partial symbol.  */
9255
9256   real_pdi = struct_pdi;
9257   while (real_pdi->has_specification)
9258     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9259
9260   if (real_pdi->die_parent != NULL)
9261     return;
9262
9263   for (child_pdi = struct_pdi->die_child;
9264        child_pdi != NULL;
9265        child_pdi = child_pdi->die_sibling)
9266     {
9267       if (child_pdi->tag == DW_TAG_subprogram
9268           && child_pdi->linkage_name != NULL)
9269         {
9270           char *actual_class_name
9271             = language_class_name_from_physname (cu->language_defn,
9272                                                  child_pdi->linkage_name);
9273           if (actual_class_name != NULL)
9274             {
9275               struct_pdi->name
9276                 = obsavestring (actual_class_name,
9277                                 strlen (actual_class_name),
9278                                 &cu->objfile->objfile_obstack);
9279               xfree (actual_class_name);
9280             }
9281           break;
9282         }
9283     }
9284 }
9285
9286 /* Adjust PART_DIE before generating a symbol for it.  This function
9287    may set the is_external flag or change the DIE's name.  */
9288
9289 static void
9290 fixup_partial_die (struct partial_die_info *part_die,
9291                    struct dwarf2_cu *cu)
9292 {
9293   /* Once we've fixed up a die, there's no point in doing so again.
9294      This also avoids a memory leak if we were to call
9295      guess_partial_die_structure_name multiple times.  */
9296   if (part_die->fixup_called)
9297     return;
9298
9299   /* If we found a reference attribute and the DIE has no name, try
9300      to find a name in the referred to DIE.  */
9301
9302   if (part_die->name == NULL && part_die->has_specification)
9303     {
9304       struct partial_die_info *spec_die;
9305
9306       spec_die = find_partial_die (part_die->spec_offset, cu);
9307
9308       fixup_partial_die (spec_die, cu);
9309
9310       if (spec_die->name)
9311         {
9312           part_die->name = spec_die->name;
9313
9314           /* Copy DW_AT_external attribute if it is set.  */
9315           if (spec_die->is_external)
9316             part_die->is_external = spec_die->is_external;
9317         }
9318     }
9319
9320   /* Set default names for some unnamed DIEs.  */
9321
9322   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9323     part_die->name = "(anonymous namespace)";
9324
9325   /* If there is no parent die to provide a namespace, and there are
9326      children, see if we can determine the namespace from their linkage
9327      name.
9328      NOTE: We need to do this even if cu->has_namespace_info != 0.
9329      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9330   if (cu->language == language_cplus
9331       && dwarf2_per_objfile->types.asection != NULL
9332       && part_die->die_parent == NULL
9333       && part_die->has_children
9334       && (part_die->tag == DW_TAG_class_type
9335           || part_die->tag == DW_TAG_structure_type
9336           || part_die->tag == DW_TAG_union_type))
9337     guess_partial_die_structure_name (part_die, cu);
9338
9339   part_die->fixup_called = 1;
9340 }
9341
9342 /* Read an attribute value described by an attribute form.  */
9343
9344 static gdb_byte *
9345 read_attribute_value (struct attribute *attr, unsigned form,
9346                       bfd *abfd, gdb_byte *info_ptr,
9347                       struct dwarf2_cu *cu)
9348 {
9349   struct comp_unit_head *cu_header = &cu->header;
9350   unsigned int bytes_read;
9351   struct dwarf_block *blk;
9352
9353   attr->form = form;
9354   switch (form)
9355     {
9356     case DW_FORM_ref_addr:
9357       if (cu->header.version == 2)
9358         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9359       else
9360         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9361                                       &cu->header, &bytes_read);
9362       info_ptr += bytes_read;
9363       break;
9364     case DW_FORM_addr:
9365       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9366       info_ptr += bytes_read;
9367       break;
9368     case DW_FORM_block2:
9369       blk = dwarf_alloc_block (cu);
9370       blk->size = read_2_bytes (abfd, info_ptr);
9371       info_ptr += 2;
9372       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9373       info_ptr += blk->size;
9374       DW_BLOCK (attr) = blk;
9375       break;
9376     case DW_FORM_block4:
9377       blk = dwarf_alloc_block (cu);
9378       blk->size = read_4_bytes (abfd, info_ptr);
9379       info_ptr += 4;
9380       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9381       info_ptr += blk->size;
9382       DW_BLOCK (attr) = blk;
9383       break;
9384     case DW_FORM_data2:
9385       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9386       info_ptr += 2;
9387       break;
9388     case DW_FORM_data4:
9389       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9390       info_ptr += 4;
9391       break;
9392     case DW_FORM_data8:
9393       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9394       info_ptr += 8;
9395       break;
9396     case DW_FORM_sec_offset:
9397       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9398       info_ptr += bytes_read;
9399       break;
9400     case DW_FORM_string:
9401       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9402       DW_STRING_IS_CANONICAL (attr) = 0;
9403       info_ptr += bytes_read;
9404       break;
9405     case DW_FORM_strp:
9406       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9407                                                &bytes_read);
9408       DW_STRING_IS_CANONICAL (attr) = 0;
9409       info_ptr += bytes_read;
9410       break;
9411     case DW_FORM_exprloc:
9412     case DW_FORM_block:
9413       blk = dwarf_alloc_block (cu);
9414       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9415       info_ptr += bytes_read;
9416       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9417       info_ptr += blk->size;
9418       DW_BLOCK (attr) = blk;
9419       break;
9420     case DW_FORM_block1:
9421       blk = dwarf_alloc_block (cu);
9422       blk->size = read_1_byte (abfd, info_ptr);
9423       info_ptr += 1;
9424       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9425       info_ptr += blk->size;
9426       DW_BLOCK (attr) = blk;
9427       break;
9428     case DW_FORM_data1:
9429       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9430       info_ptr += 1;
9431       break;
9432     case DW_FORM_flag:
9433       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9434       info_ptr += 1;
9435       break;
9436     case DW_FORM_flag_present:
9437       DW_UNSND (attr) = 1;
9438       break;
9439     case DW_FORM_sdata:
9440       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9441       info_ptr += bytes_read;
9442       break;
9443     case DW_FORM_udata:
9444       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9445       info_ptr += bytes_read;
9446       break;
9447     case DW_FORM_ref1:
9448       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9449       info_ptr += 1;
9450       break;
9451     case DW_FORM_ref2:
9452       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9453       info_ptr += 2;
9454       break;
9455     case DW_FORM_ref4:
9456       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9457       info_ptr += 4;
9458       break;
9459     case DW_FORM_ref8:
9460       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9461       info_ptr += 8;
9462       break;
9463     case DW_FORM_sig8:
9464       /* Convert the signature to something we can record in DW_UNSND
9465          for later lookup.
9466          NOTE: This is NULL if the type wasn't found.  */
9467       DW_SIGNATURED_TYPE (attr) =
9468         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9469       info_ptr += 8;
9470       break;
9471     case DW_FORM_ref_udata:
9472       DW_ADDR (attr) = (cu->header.offset
9473                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9474       info_ptr += bytes_read;
9475       break;
9476     case DW_FORM_indirect:
9477       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9478       info_ptr += bytes_read;
9479       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9480       break;
9481     default:
9482       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9483              dwarf_form_name (form),
9484              bfd_get_filename (abfd));
9485     }
9486
9487   /* We have seen instances where the compiler tried to emit a byte
9488      size attribute of -1 which ended up being encoded as an unsigned
9489      0xffffffff.  Although 0xffffffff is technically a valid size value,
9490      an object of this size seems pretty unlikely so we can relatively
9491      safely treat these cases as if the size attribute was invalid and
9492      treat them as zero by default.  */
9493   if (attr->name == DW_AT_byte_size
9494       && form == DW_FORM_data4
9495       && DW_UNSND (attr) >= 0xffffffff)
9496     {
9497       complaint
9498         (&symfile_complaints,
9499          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9500          hex_string (DW_UNSND (attr)));
9501       DW_UNSND (attr) = 0;
9502     }
9503
9504   return info_ptr;
9505 }
9506
9507 /* Read an attribute described by an abbreviated attribute.  */
9508
9509 static gdb_byte *
9510 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9511                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9512 {
9513   attr->name = abbrev->name;
9514   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9515 }
9516
9517 /* Read dwarf information from a buffer.  */
9518
9519 static unsigned int
9520 read_1_byte (bfd *abfd, gdb_byte *buf)
9521 {
9522   return bfd_get_8 (abfd, buf);
9523 }
9524
9525 static int
9526 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9527 {
9528   return bfd_get_signed_8 (abfd, buf);
9529 }
9530
9531 static unsigned int
9532 read_2_bytes (bfd *abfd, gdb_byte *buf)
9533 {
9534   return bfd_get_16 (abfd, buf);
9535 }
9536
9537 static int
9538 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9539 {
9540   return bfd_get_signed_16 (abfd, buf);
9541 }
9542
9543 static unsigned int
9544 read_4_bytes (bfd *abfd, gdb_byte *buf)
9545 {
9546   return bfd_get_32 (abfd, buf);
9547 }
9548
9549 static int
9550 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9551 {
9552   return bfd_get_signed_32 (abfd, buf);
9553 }
9554
9555 static ULONGEST
9556 read_8_bytes (bfd *abfd, gdb_byte *buf)
9557 {
9558   return bfd_get_64 (abfd, buf);
9559 }
9560
9561 static CORE_ADDR
9562 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9563               unsigned int *bytes_read)
9564 {
9565   struct comp_unit_head *cu_header = &cu->header;
9566   CORE_ADDR retval = 0;
9567
9568   if (cu_header->signed_addr_p)
9569     {
9570       switch (cu_header->addr_size)
9571         {
9572         case 2:
9573           retval = bfd_get_signed_16 (abfd, buf);
9574           break;
9575         case 4:
9576           retval = bfd_get_signed_32 (abfd, buf);
9577           break;
9578         case 8:
9579           retval = bfd_get_signed_64 (abfd, buf);
9580           break;
9581         default:
9582           internal_error (__FILE__, __LINE__,
9583                           _("read_address: bad switch, signed [in module %s]"),
9584                           bfd_get_filename (abfd));
9585         }
9586     }
9587   else
9588     {
9589       switch (cu_header->addr_size)
9590         {
9591         case 2:
9592           retval = bfd_get_16 (abfd, buf);
9593           break;
9594         case 4:
9595           retval = bfd_get_32 (abfd, buf);
9596           break;
9597         case 8:
9598           retval = bfd_get_64 (abfd, buf);
9599           break;
9600         default:
9601           internal_error (__FILE__, __LINE__,
9602                           _("read_address: bad switch, "
9603                             "unsigned [in module %s]"),
9604                           bfd_get_filename (abfd));
9605         }
9606     }
9607
9608   *bytes_read = cu_header->addr_size;
9609   return retval;
9610 }
9611
9612 /* Read the initial length from a section.  The (draft) DWARF 3
9613    specification allows the initial length to take up either 4 bytes
9614    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9615    bytes describe the length and all offsets will be 8 bytes in length
9616    instead of 4.
9617
9618    An older, non-standard 64-bit format is also handled by this
9619    function.  The older format in question stores the initial length
9620    as an 8-byte quantity without an escape value.  Lengths greater
9621    than 2^32 aren't very common which means that the initial 4 bytes
9622    is almost always zero.  Since a length value of zero doesn't make
9623    sense for the 32-bit format, this initial zero can be considered to
9624    be an escape value which indicates the presence of the older 64-bit
9625    format.  As written, the code can't detect (old format) lengths
9626    greater than 4GB.  If it becomes necessary to handle lengths
9627    somewhat larger than 4GB, we could allow other small values (such
9628    as the non-sensical values of 1, 2, and 3) to also be used as
9629    escape values indicating the presence of the old format.
9630
9631    The value returned via bytes_read should be used to increment the
9632    relevant pointer after calling read_initial_length().
9633
9634    [ Note:  read_initial_length() and read_offset() are based on the
9635      document entitled "DWARF Debugging Information Format", revision
9636      3, draft 8, dated November 19, 2001.  This document was obtained
9637      from:
9638
9639         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9640
9641      This document is only a draft and is subject to change.  (So beware.)
9642
9643      Details regarding the older, non-standard 64-bit format were
9644      determined empirically by examining 64-bit ELF files produced by
9645      the SGI toolchain on an IRIX 6.5 machine.
9646
9647      - Kevin, July 16, 2002
9648    ] */
9649
9650 static LONGEST
9651 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9652 {
9653   LONGEST length = bfd_get_32 (abfd, buf);
9654
9655   if (length == 0xffffffff)
9656     {
9657       length = bfd_get_64 (abfd, buf + 4);
9658       *bytes_read = 12;
9659     }
9660   else if (length == 0)
9661     {
9662       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9663       length = bfd_get_64 (abfd, buf);
9664       *bytes_read = 8;
9665     }
9666   else
9667     {
9668       *bytes_read = 4;
9669     }
9670
9671   return length;
9672 }
9673
9674 /* Cover function for read_initial_length.
9675    Returns the length of the object at BUF, and stores the size of the
9676    initial length in *BYTES_READ and stores the size that offsets will be in
9677    *OFFSET_SIZE.
9678    If the initial length size is not equivalent to that specified in
9679    CU_HEADER then issue a complaint.
9680    This is useful when reading non-comp-unit headers.  */
9681
9682 static LONGEST
9683 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9684                                         const struct comp_unit_head *cu_header,
9685                                         unsigned int *bytes_read,
9686                                         unsigned int *offset_size)
9687 {
9688   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9689
9690   gdb_assert (cu_header->initial_length_size == 4
9691               || cu_header->initial_length_size == 8
9692               || cu_header->initial_length_size == 12);
9693
9694   if (cu_header->initial_length_size != *bytes_read)
9695     complaint (&symfile_complaints,
9696                _("intermixed 32-bit and 64-bit DWARF sections"));
9697
9698   *offset_size = (*bytes_read == 4) ? 4 : 8;
9699   return length;
9700 }
9701
9702 /* Read an offset from the data stream.  The size of the offset is
9703    given by cu_header->offset_size.  */
9704
9705 static LONGEST
9706 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9707              unsigned int *bytes_read)
9708 {
9709   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9710
9711   *bytes_read = cu_header->offset_size;
9712   return offset;
9713 }
9714
9715 /* Read an offset from the data stream.  */
9716
9717 static LONGEST
9718 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9719 {
9720   LONGEST retval = 0;
9721
9722   switch (offset_size)
9723     {
9724     case 4:
9725       retval = bfd_get_32 (abfd, buf);
9726       break;
9727     case 8:
9728       retval = bfd_get_64 (abfd, buf);
9729       break;
9730     default:
9731       internal_error (__FILE__, __LINE__,
9732                       _("read_offset_1: bad switch [in module %s]"),
9733                       bfd_get_filename (abfd));
9734     }
9735
9736   return retval;
9737 }
9738
9739 static gdb_byte *
9740 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9741 {
9742   /* If the size of a host char is 8 bits, we can return a pointer
9743      to the buffer, otherwise we have to copy the data to a buffer
9744      allocated on the temporary obstack.  */
9745   gdb_assert (HOST_CHAR_BIT == 8);
9746   return buf;
9747 }
9748
9749 static char *
9750 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9751 {
9752   /* If the size of a host char is 8 bits, we can return a pointer
9753      to the string, otherwise we have to copy the string to a buffer
9754      allocated on the temporary obstack.  */
9755   gdb_assert (HOST_CHAR_BIT == 8);
9756   if (*buf == '\0')
9757     {
9758       *bytes_read_ptr = 1;
9759       return NULL;
9760     }
9761   *bytes_read_ptr = strlen ((char *) buf) + 1;
9762   return (char *) buf;
9763 }
9764
9765 static char *
9766 read_indirect_string (bfd *abfd, gdb_byte *buf,
9767                       const struct comp_unit_head *cu_header,
9768                       unsigned int *bytes_read_ptr)
9769 {
9770   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9771
9772   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9773   if (dwarf2_per_objfile->str.buffer == NULL)
9774     {
9775       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9776                       bfd_get_filename (abfd));
9777       return NULL;
9778     }
9779   if (str_offset >= dwarf2_per_objfile->str.size)
9780     {
9781       error (_("DW_FORM_strp pointing outside of "
9782                ".debug_str section [in module %s]"),
9783              bfd_get_filename (abfd));
9784       return NULL;
9785     }
9786   gdb_assert (HOST_CHAR_BIT == 8);
9787   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9788     return NULL;
9789   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9790 }
9791
9792 static unsigned long
9793 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9794 {
9795   unsigned long result;
9796   unsigned int num_read;
9797   int i, shift;
9798   unsigned char byte;
9799
9800   result = 0;
9801   shift = 0;
9802   num_read = 0;
9803   i = 0;
9804   while (1)
9805     {
9806       byte = bfd_get_8 (abfd, buf);
9807       buf++;
9808       num_read++;
9809       result |= ((unsigned long)(byte & 127) << shift);
9810       if ((byte & 128) == 0)
9811         {
9812           break;
9813         }
9814       shift += 7;
9815     }
9816   *bytes_read_ptr = num_read;
9817   return result;
9818 }
9819
9820 static long
9821 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9822 {
9823   long result;
9824   int i, shift, num_read;
9825   unsigned char byte;
9826
9827   result = 0;
9828   shift = 0;
9829   num_read = 0;
9830   i = 0;
9831   while (1)
9832     {
9833       byte = bfd_get_8 (abfd, buf);
9834       buf++;
9835       num_read++;
9836       result |= ((long)(byte & 127) << shift);
9837       shift += 7;
9838       if ((byte & 128) == 0)
9839         {
9840           break;
9841         }
9842     }
9843   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9844     result |= -(((long)1) << shift);
9845   *bytes_read_ptr = num_read;
9846   return result;
9847 }
9848
9849 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9850
9851 static gdb_byte *
9852 skip_leb128 (bfd *abfd, gdb_byte *buf)
9853 {
9854   int byte;
9855
9856   while (1)
9857     {
9858       byte = bfd_get_8 (abfd, buf);
9859       buf++;
9860       if ((byte & 128) == 0)
9861         return buf;
9862     }
9863 }
9864
9865 static void
9866 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9867 {
9868   switch (lang)
9869     {
9870     case DW_LANG_C89:
9871     case DW_LANG_C99:
9872     case DW_LANG_C:
9873       cu->language = language_c;
9874       break;
9875     case DW_LANG_C_plus_plus:
9876       cu->language = language_cplus;
9877       break;
9878     case DW_LANG_D:
9879       cu->language = language_d;
9880       break;
9881     case DW_LANG_Fortran77:
9882     case DW_LANG_Fortran90:
9883     case DW_LANG_Fortran95:
9884       cu->language = language_fortran;
9885       break;
9886     case DW_LANG_Mips_Assembler:
9887       cu->language = language_asm;
9888       break;
9889     case DW_LANG_Java:
9890       cu->language = language_java;
9891       break;
9892     case DW_LANG_Ada83:
9893     case DW_LANG_Ada95:
9894       cu->language = language_ada;
9895       break;
9896     case DW_LANG_Modula2:
9897       cu->language = language_m2;
9898       break;
9899     case DW_LANG_Pascal83:
9900       cu->language = language_pascal;
9901       break;
9902     case DW_LANG_ObjC:
9903       cu->language = language_objc;
9904       break;
9905     case DW_LANG_Cobol74:
9906     case DW_LANG_Cobol85:
9907     default:
9908       cu->language = language_minimal;
9909       break;
9910     }
9911   cu->language_defn = language_def (cu->language);
9912 }
9913
9914 /* Return the named attribute or NULL if not there.  */
9915
9916 static struct attribute *
9917 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9918 {
9919   unsigned int i;
9920   struct attribute *spec = NULL;
9921
9922   for (i = 0; i < die->num_attrs; ++i)
9923     {
9924       if (die->attrs[i].name == name)
9925         return &die->attrs[i];
9926       if (die->attrs[i].name == DW_AT_specification
9927           || die->attrs[i].name == DW_AT_abstract_origin)
9928         spec = &die->attrs[i];
9929     }
9930
9931   if (spec)
9932     {
9933       die = follow_die_ref (die, spec, &cu);
9934       return dwarf2_attr (die, name, cu);
9935     }
9936
9937   return NULL;
9938 }
9939
9940 /* Return the named attribute or NULL if not there,
9941    but do not follow DW_AT_specification, etc.
9942    This is for use in contexts where we're reading .debug_types dies.
9943    Following DW_AT_specification, DW_AT_abstract_origin will take us
9944    back up the chain, and we want to go down.  */
9945
9946 static struct attribute *
9947 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9948                        struct dwarf2_cu *cu)
9949 {
9950   unsigned int i;
9951
9952   for (i = 0; i < die->num_attrs; ++i)
9953     if (die->attrs[i].name == name)
9954       return &die->attrs[i];
9955
9956   return NULL;
9957 }
9958
9959 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9960    and holds a non-zero value.  This function should only be used for
9961    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9962
9963 static int
9964 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9965 {
9966   struct attribute *attr = dwarf2_attr (die, name, cu);
9967
9968   return (attr && DW_UNSND (attr));
9969 }
9970
9971 static int
9972 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9973 {
9974   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9975      which value is non-zero.  However, we have to be careful with
9976      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9977      (via dwarf2_flag_true_p) follows this attribute.  So we may
9978      end up accidently finding a declaration attribute that belongs
9979      to a different DIE referenced by the specification attribute,
9980      even though the given DIE does not have a declaration attribute.  */
9981   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9982           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9983 }
9984
9985 /* Return the die giving the specification for DIE, if there is
9986    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9987    containing the return value on output.  If there is no
9988    specification, but there is an abstract origin, that is
9989    returned.  */
9990
9991 static struct die_info *
9992 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9993 {
9994   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9995                                              *spec_cu);
9996
9997   if (spec_attr == NULL)
9998     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9999
10000   if (spec_attr == NULL)
10001     return NULL;
10002   else
10003     return follow_die_ref (die, spec_attr, spec_cu);
10004 }
10005
10006 /* Free the line_header structure *LH, and any arrays and strings it
10007    refers to.
10008    NOTE: This is also used as a "cleanup" function.  */
10009
10010 static void
10011 free_line_header (struct line_header *lh)
10012 {
10013   if (lh->standard_opcode_lengths)
10014     xfree (lh->standard_opcode_lengths);
10015
10016   /* Remember that all the lh->file_names[i].name pointers are
10017      pointers into debug_line_buffer, and don't need to be freed.  */
10018   if (lh->file_names)
10019     xfree (lh->file_names);
10020
10021   /* Similarly for the include directory names.  */
10022   if (lh->include_dirs)
10023     xfree (lh->include_dirs);
10024
10025   xfree (lh);
10026 }
10027
10028 /* Add an entry to LH's include directory table.  */
10029
10030 static void
10031 add_include_dir (struct line_header *lh, char *include_dir)
10032 {
10033   /* Grow the array if necessary.  */
10034   if (lh->include_dirs_size == 0)
10035     {
10036       lh->include_dirs_size = 1; /* for testing */
10037       lh->include_dirs = xmalloc (lh->include_dirs_size
10038                                   * sizeof (*lh->include_dirs));
10039     }
10040   else if (lh->num_include_dirs >= lh->include_dirs_size)
10041     {
10042       lh->include_dirs_size *= 2;
10043       lh->include_dirs = xrealloc (lh->include_dirs,
10044                                    (lh->include_dirs_size
10045                                     * sizeof (*lh->include_dirs)));
10046     }
10047
10048   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10049 }
10050
10051 /* Add an entry to LH's file name table.  */
10052
10053 static void
10054 add_file_name (struct line_header *lh,
10055                char *name,
10056                unsigned int dir_index,
10057                unsigned int mod_time,
10058                unsigned int length)
10059 {
10060   struct file_entry *fe;
10061
10062   /* Grow the array if necessary.  */
10063   if (lh->file_names_size == 0)
10064     {
10065       lh->file_names_size = 1; /* for testing */
10066       lh->file_names = xmalloc (lh->file_names_size
10067                                 * sizeof (*lh->file_names));
10068     }
10069   else if (lh->num_file_names >= lh->file_names_size)
10070     {
10071       lh->file_names_size *= 2;
10072       lh->file_names = xrealloc (lh->file_names,
10073                                  (lh->file_names_size
10074                                   * sizeof (*lh->file_names)));
10075     }
10076
10077   fe = &lh->file_names[lh->num_file_names++];
10078   fe->name = name;
10079   fe->dir_index = dir_index;
10080   fe->mod_time = mod_time;
10081   fe->length = length;
10082   fe->included_p = 0;
10083   fe->symtab = NULL;
10084 }
10085
10086 /* Read the statement program header starting at OFFSET in
10087    .debug_line, according to the endianness of ABFD.  Return a pointer
10088    to a struct line_header, allocated using xmalloc.
10089
10090    NOTE: the strings in the include directory and file name tables of
10091    the returned object point into debug_line_buffer, and must not be
10092    freed.  */
10093
10094 static struct line_header *
10095 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10096                           struct dwarf2_cu *cu)
10097 {
10098   struct cleanup *back_to;
10099   struct line_header *lh;
10100   gdb_byte *line_ptr;
10101   unsigned int bytes_read, offset_size;
10102   int i;
10103   char *cur_dir, *cur_file;
10104
10105   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10106   if (dwarf2_per_objfile->line.buffer == NULL)
10107     {
10108       complaint (&symfile_complaints, _("missing .debug_line section"));
10109       return 0;
10110     }
10111
10112   /* Make sure that at least there's room for the total_length field.
10113      That could be 12 bytes long, but we're just going to fudge that.  */
10114   if (offset + 4 >= dwarf2_per_objfile->line.size)
10115     {
10116       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10117       return 0;
10118     }
10119
10120   lh = xmalloc (sizeof (*lh));
10121   memset (lh, 0, sizeof (*lh));
10122   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10123                           (void *) lh);
10124
10125   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10126
10127   /* Read in the header.  */
10128   lh->total_length =
10129     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10130                                             &bytes_read, &offset_size);
10131   line_ptr += bytes_read;
10132   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10133                                      + dwarf2_per_objfile->line.size))
10134     {
10135       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10136       return 0;
10137     }
10138   lh->statement_program_end = line_ptr + lh->total_length;
10139   lh->version = read_2_bytes (abfd, line_ptr);
10140   line_ptr += 2;
10141   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10142   line_ptr += offset_size;
10143   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10144   line_ptr += 1;
10145   if (lh->version >= 4)
10146     {
10147       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10148       line_ptr += 1;
10149     }
10150   else
10151     lh->maximum_ops_per_instruction = 1;
10152
10153   if (lh->maximum_ops_per_instruction == 0)
10154     {
10155       lh->maximum_ops_per_instruction = 1;
10156       complaint (&symfile_complaints,
10157                  _("invalid maximum_ops_per_instruction "
10158                    "in `.debug_line' section"));
10159     }
10160
10161   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10162   line_ptr += 1;
10163   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10164   line_ptr += 1;
10165   lh->line_range = read_1_byte (abfd, line_ptr);
10166   line_ptr += 1;
10167   lh->opcode_base = read_1_byte (abfd, line_ptr);
10168   line_ptr += 1;
10169   lh->standard_opcode_lengths
10170     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10171
10172   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10173   for (i = 1; i < lh->opcode_base; ++i)
10174     {
10175       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10176       line_ptr += 1;
10177     }
10178
10179   /* Read directory table.  */
10180   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10181     {
10182       line_ptr += bytes_read;
10183       add_include_dir (lh, cur_dir);
10184     }
10185   line_ptr += bytes_read;
10186
10187   /* Read file name table.  */
10188   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10189     {
10190       unsigned int dir_index, mod_time, length;
10191
10192       line_ptr += bytes_read;
10193       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10194       line_ptr += bytes_read;
10195       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10196       line_ptr += bytes_read;
10197       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10198       line_ptr += bytes_read;
10199
10200       add_file_name (lh, cur_file, dir_index, mod_time, length);
10201     }
10202   line_ptr += bytes_read;
10203   lh->statement_program_start = line_ptr;
10204
10205   if (line_ptr > (dwarf2_per_objfile->line.buffer
10206                   + dwarf2_per_objfile->line.size))
10207     complaint (&symfile_complaints,
10208                _("line number info header doesn't "
10209                  "fit in `.debug_line' section"));
10210
10211   discard_cleanups (back_to);
10212   return lh;
10213 }
10214
10215 /* This function exists to work around a bug in certain compilers
10216    (particularly GCC 2.95), in which the first line number marker of a
10217    function does not show up until after the prologue, right before
10218    the second line number marker.  This function shifts ADDRESS down
10219    to the beginning of the function if necessary, and is called on
10220    addresses passed to record_line.  */
10221
10222 static CORE_ADDR
10223 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10224 {
10225   struct function_range *fn;
10226
10227   /* Find the function_range containing address.  */
10228   if (!cu->first_fn)
10229     return address;
10230
10231   if (!cu->cached_fn)
10232     cu->cached_fn = cu->first_fn;
10233
10234   fn = cu->cached_fn;
10235   while (fn)
10236     if (fn->lowpc <= address && fn->highpc > address)
10237       goto found;
10238     else
10239       fn = fn->next;
10240
10241   fn = cu->first_fn;
10242   while (fn && fn != cu->cached_fn)
10243     if (fn->lowpc <= address && fn->highpc > address)
10244       goto found;
10245     else
10246       fn = fn->next;
10247
10248   return address;
10249
10250  found:
10251   if (fn->seen_line)
10252     return address;
10253   if (address != fn->lowpc)
10254     complaint (&symfile_complaints,
10255                _("misplaced first line number at 0x%lx for '%s'"),
10256                (unsigned long) address, fn->name);
10257   fn->seen_line = 1;
10258   return fn->lowpc;
10259 }
10260
10261 /* Subroutine of dwarf_decode_lines to simplify it.
10262    Return the file name of the psymtab for included file FILE_INDEX
10263    in line header LH of PST.
10264    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10265    If space for the result is malloc'd, it will be freed by a cleanup.
10266    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10267
10268 static char *
10269 psymtab_include_file_name (const struct line_header *lh, int file_index,
10270                            const struct partial_symtab *pst,
10271                            const char *comp_dir)
10272 {
10273   const struct file_entry fe = lh->file_names [file_index];
10274   char *include_name = fe.name;
10275   char *include_name_to_compare = include_name;
10276   char *dir_name = NULL;
10277   const char *pst_filename;
10278   char *copied_name = NULL;
10279   int file_is_pst;
10280
10281   if (fe.dir_index)
10282     dir_name = lh->include_dirs[fe.dir_index - 1];
10283
10284   if (!IS_ABSOLUTE_PATH (include_name)
10285       && (dir_name != NULL || comp_dir != NULL))
10286     {
10287       /* Avoid creating a duplicate psymtab for PST.
10288          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10289          Before we do the comparison, however, we need to account
10290          for DIR_NAME and COMP_DIR.
10291          First prepend dir_name (if non-NULL).  If we still don't
10292          have an absolute path prepend comp_dir (if non-NULL).
10293          However, the directory we record in the include-file's
10294          psymtab does not contain COMP_DIR (to match the
10295          corresponding symtab(s)).
10296
10297          Example:
10298
10299          bash$ cd /tmp
10300          bash$ gcc -g ./hello.c
10301          include_name = "hello.c"
10302          dir_name = "."
10303          DW_AT_comp_dir = comp_dir = "/tmp"
10304          DW_AT_name = "./hello.c"  */
10305
10306       if (dir_name != NULL)
10307         {
10308           include_name = concat (dir_name, SLASH_STRING,
10309                                  include_name, (char *)NULL);
10310           include_name_to_compare = include_name;
10311           make_cleanup (xfree, include_name);
10312         }
10313       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10314         {
10315           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10316                                             include_name, (char *)NULL);
10317         }
10318     }
10319
10320   pst_filename = pst->filename;
10321   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10322     {
10323       copied_name = concat (pst->dirname, SLASH_STRING,
10324                             pst_filename, (char *)NULL);
10325       pst_filename = copied_name;
10326     }
10327
10328   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10329
10330   if (include_name_to_compare != include_name)
10331     xfree (include_name_to_compare);
10332   if (copied_name != NULL)
10333     xfree (copied_name);
10334
10335   if (file_is_pst)
10336     return NULL;
10337   return include_name;
10338 }
10339
10340 /* Decode the Line Number Program (LNP) for the given line_header
10341    structure and CU.  The actual information extracted and the type
10342    of structures created from the LNP depends on the value of PST.
10343
10344    1. If PST is NULL, then this procedure uses the data from the program
10345       to create all necessary symbol tables, and their linetables.
10346
10347    2. If PST is not NULL, this procedure reads the program to determine
10348       the list of files included by the unit represented by PST, and
10349       builds all the associated partial symbol tables.
10350
10351    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10352    It is used for relative paths in the line table.
10353    NOTE: When processing partial symtabs (pst != NULL),
10354    comp_dir == pst->dirname.
10355
10356    NOTE: It is important that psymtabs have the same file name (via strcmp)
10357    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10358    symtab we don't use it in the name of the psymtabs we create.
10359    E.g. expand_line_sal requires this when finding psymtabs to expand.
10360    A good testcase for this is mb-inline.exp.  */
10361
10362 static void
10363 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10364                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10365 {
10366   gdb_byte *line_ptr, *extended_end;
10367   gdb_byte *line_end;
10368   unsigned int bytes_read, extended_len;
10369   unsigned char op_code, extended_op, adj_opcode;
10370   CORE_ADDR baseaddr;
10371   struct objfile *objfile = cu->objfile;
10372   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10373   const int decode_for_pst_p = (pst != NULL);
10374   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10375
10376   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10377
10378   line_ptr = lh->statement_program_start;
10379   line_end = lh->statement_program_end;
10380
10381   /* Read the statement sequences until there's nothing left.  */
10382   while (line_ptr < line_end)
10383     {
10384       /* state machine registers  */
10385       CORE_ADDR address = 0;
10386       unsigned int file = 1;
10387       unsigned int line = 1;
10388       unsigned int column = 0;
10389       int is_stmt = lh->default_is_stmt;
10390       int basic_block = 0;
10391       int end_sequence = 0;
10392       CORE_ADDR addr;
10393       unsigned char op_index = 0;
10394
10395       if (!decode_for_pst_p && lh->num_file_names >= file)
10396         {
10397           /* Start a subfile for the current file of the state machine.  */
10398           /* lh->include_dirs and lh->file_names are 0-based, but the
10399              directory and file name numbers in the statement program
10400              are 1-based.  */
10401           struct file_entry *fe = &lh->file_names[file - 1];
10402           char *dir = NULL;
10403
10404           if (fe->dir_index)
10405             dir = lh->include_dirs[fe->dir_index - 1];
10406
10407           dwarf2_start_subfile (fe->name, dir, comp_dir);
10408         }
10409
10410       /* Decode the table.  */
10411       while (!end_sequence)
10412         {
10413           op_code = read_1_byte (abfd, line_ptr);
10414           line_ptr += 1;
10415           if (line_ptr > line_end)
10416             {
10417               dwarf2_debug_line_missing_end_sequence_complaint ();
10418               break;
10419             }
10420
10421           if (op_code >= lh->opcode_base)
10422             {
10423               /* Special operand.  */
10424               adj_opcode = op_code - lh->opcode_base;
10425               address += (((op_index + (adj_opcode / lh->line_range))
10426                            / lh->maximum_ops_per_instruction)
10427                           * lh->minimum_instruction_length);
10428               op_index = ((op_index + (adj_opcode / lh->line_range))
10429                           % lh->maximum_ops_per_instruction);
10430               line += lh->line_base + (adj_opcode % lh->line_range);
10431               if (lh->num_file_names < file || file == 0)
10432                 dwarf2_debug_line_missing_file_complaint ();
10433               /* For now we ignore lines not starting on an
10434                  instruction boundary.  */
10435               else if (op_index == 0)
10436                 {
10437                   lh->file_names[file - 1].included_p = 1;
10438                   if (!decode_for_pst_p && is_stmt)
10439                     {
10440                       if (last_subfile != current_subfile)
10441                         {
10442                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10443                           if (last_subfile)
10444                             record_line (last_subfile, 0, addr);
10445                           last_subfile = current_subfile;
10446                         }
10447                       /* Append row to matrix using current values.  */
10448                       addr = check_cu_functions (address, cu);
10449                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10450                       record_line (current_subfile, line, addr);
10451                     }
10452                 }
10453               basic_block = 0;
10454             }
10455           else switch (op_code)
10456             {
10457             case DW_LNS_extended_op:
10458               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10459                                                    &bytes_read);
10460               line_ptr += bytes_read;
10461               extended_end = line_ptr + extended_len;
10462               extended_op = read_1_byte (abfd, line_ptr);
10463               line_ptr += 1;
10464               switch (extended_op)
10465                 {
10466                 case DW_LNE_end_sequence:
10467                   end_sequence = 1;
10468                   break;
10469                 case DW_LNE_set_address:
10470                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10471                   op_index = 0;
10472                   line_ptr += bytes_read;
10473                   address += baseaddr;
10474                   break;
10475                 case DW_LNE_define_file:
10476                   {
10477                     char *cur_file;
10478                     unsigned int dir_index, mod_time, length;
10479
10480                     cur_file = read_direct_string (abfd, line_ptr,
10481                                                    &bytes_read);
10482                     line_ptr += bytes_read;
10483                     dir_index =
10484                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10485                     line_ptr += bytes_read;
10486                     mod_time =
10487                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10488                     line_ptr += bytes_read;
10489                     length =
10490                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10491                     line_ptr += bytes_read;
10492                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10493                   }
10494                   break;
10495                 case DW_LNE_set_discriminator:
10496                   /* The discriminator is not interesting to the debugger;
10497                      just ignore it.  */
10498                   line_ptr = extended_end;
10499                   break;
10500                 default:
10501                   complaint (&symfile_complaints,
10502                              _("mangled .debug_line section"));
10503                   return;
10504                 }
10505               /* Make sure that we parsed the extended op correctly.  If e.g.
10506                  we expected a different address size than the producer used,
10507                  we may have read the wrong number of bytes.  */
10508               if (line_ptr != extended_end)
10509                 {
10510                   complaint (&symfile_complaints,
10511                              _("mangled .debug_line section"));
10512                   return;
10513                 }
10514               break;
10515             case DW_LNS_copy:
10516               if (lh->num_file_names < file || file == 0)
10517                 dwarf2_debug_line_missing_file_complaint ();
10518               else
10519                 {
10520                   lh->file_names[file - 1].included_p = 1;
10521                   if (!decode_for_pst_p && is_stmt)
10522                     {
10523                       if (last_subfile != current_subfile)
10524                         {
10525                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10526                           if (last_subfile)
10527                             record_line (last_subfile, 0, addr);
10528                           last_subfile = current_subfile;
10529                         }
10530                       addr = check_cu_functions (address, cu);
10531                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10532                       record_line (current_subfile, line, addr);
10533                     }
10534                 }
10535               basic_block = 0;
10536               break;
10537             case DW_LNS_advance_pc:
10538               {
10539                 CORE_ADDR adjust
10540                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10541
10542                 address += (((op_index + adjust)
10543                              / lh->maximum_ops_per_instruction)
10544                             * lh->minimum_instruction_length);
10545                 op_index = ((op_index + adjust)
10546                             % lh->maximum_ops_per_instruction);
10547                 line_ptr += bytes_read;
10548               }
10549               break;
10550             case DW_LNS_advance_line:
10551               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10552               line_ptr += bytes_read;
10553               break;
10554             case DW_LNS_set_file:
10555               {
10556                 /* The arrays lh->include_dirs and lh->file_names are
10557                    0-based, but the directory and file name numbers in
10558                    the statement program are 1-based.  */
10559                 struct file_entry *fe;
10560                 char *dir = NULL;
10561
10562                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10563                 line_ptr += bytes_read;
10564                 if (lh->num_file_names < file || file == 0)
10565                   dwarf2_debug_line_missing_file_complaint ();
10566                 else
10567                   {
10568                     fe = &lh->file_names[file - 1];
10569                     if (fe->dir_index)
10570                       dir = lh->include_dirs[fe->dir_index - 1];
10571                     if (!decode_for_pst_p)
10572                       {
10573                         last_subfile = current_subfile;
10574                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10575                       }
10576                   }
10577               }
10578               break;
10579             case DW_LNS_set_column:
10580               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10581               line_ptr += bytes_read;
10582               break;
10583             case DW_LNS_negate_stmt:
10584               is_stmt = (!is_stmt);
10585               break;
10586             case DW_LNS_set_basic_block:
10587               basic_block = 1;
10588               break;
10589             /* Add to the address register of the state machine the
10590                address increment value corresponding to special opcode
10591                255.  I.e., this value is scaled by the minimum
10592                instruction length since special opcode 255 would have
10593                scaled the increment.  */
10594             case DW_LNS_const_add_pc:
10595               {
10596                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10597
10598                 address += (((op_index + adjust)
10599                              / lh->maximum_ops_per_instruction)
10600                             * lh->minimum_instruction_length);
10601                 op_index = ((op_index + adjust)
10602                             % lh->maximum_ops_per_instruction);
10603               }
10604               break;
10605             case DW_LNS_fixed_advance_pc:
10606               address += read_2_bytes (abfd, line_ptr);
10607               op_index = 0;
10608               line_ptr += 2;
10609               break;
10610             default:
10611               {
10612                 /* Unknown standard opcode, ignore it.  */
10613                 int i;
10614
10615                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10616                   {
10617                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10618                     line_ptr += bytes_read;
10619                   }
10620               }
10621             }
10622         }
10623       if (lh->num_file_names < file || file == 0)
10624         dwarf2_debug_line_missing_file_complaint ();
10625       else
10626         {
10627           lh->file_names[file - 1].included_p = 1;
10628           if (!decode_for_pst_p)
10629             {
10630               addr = gdbarch_addr_bits_remove (gdbarch, address);
10631               record_line (current_subfile, 0, addr);
10632             }
10633         }
10634     }
10635
10636   if (decode_for_pst_p)
10637     {
10638       int file_index;
10639
10640       /* Now that we're done scanning the Line Header Program, we can
10641          create the psymtab of each included file.  */
10642       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10643         if (lh->file_names[file_index].included_p == 1)
10644           {
10645             char *include_name =
10646               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10647             if (include_name != NULL)
10648               dwarf2_create_include_psymtab (include_name, pst, objfile);
10649           }
10650     }
10651   else
10652     {
10653       /* Make sure a symtab is created for every file, even files
10654          which contain only variables (i.e. no code with associated
10655          line numbers).  */
10656
10657       int i;
10658       struct file_entry *fe;
10659
10660       for (i = 0; i < lh->num_file_names; i++)
10661         {
10662           char *dir = NULL;
10663
10664           fe = &lh->file_names[i];
10665           if (fe->dir_index)
10666             dir = lh->include_dirs[fe->dir_index - 1];
10667           dwarf2_start_subfile (fe->name, dir, comp_dir);
10668
10669           /* Skip the main file; we don't need it, and it must be
10670              allocated last, so that it will show up before the
10671              non-primary symtabs in the objfile's symtab list.  */
10672           if (current_subfile == first_subfile)
10673             continue;
10674
10675           if (current_subfile->symtab == NULL)
10676             current_subfile->symtab = allocate_symtab (current_subfile->name,
10677                                                        cu->objfile);
10678           fe->symtab = current_subfile->symtab;
10679         }
10680     }
10681 }
10682
10683 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10684    DIRNAME the name of the source directory which contains FILENAME
10685    or NULL if not known.  COMP_DIR is the compilation directory for the
10686    linetable's compilation unit or NULL if not known.
10687    This routine tries to keep line numbers from identical absolute and
10688    relative file names in a common subfile.
10689
10690    Using the `list' example from the GDB testsuite, which resides in
10691    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10692    of /srcdir/list0.c yields the following debugging information for list0.c:
10693
10694    DW_AT_name:          /srcdir/list0.c
10695    DW_AT_comp_dir:              /compdir
10696    files.files[0].name: list0.h
10697    files.files[0].dir:  /srcdir
10698    files.files[1].name: list0.c
10699    files.files[1].dir:  /srcdir
10700
10701    The line number information for list0.c has to end up in a single
10702    subfile, so that `break /srcdir/list0.c:1' works as expected.
10703    start_subfile will ensure that this happens provided that we pass the
10704    concatenation of files.files[1].dir and files.files[1].name as the
10705    subfile's name.  */
10706
10707 static void
10708 dwarf2_start_subfile (char *filename, const char *dirname,
10709                       const char *comp_dir)
10710 {
10711   char *fullname;
10712
10713   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10714      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10715      second argument to start_subfile.  To be consistent, we do the
10716      same here.  In order not to lose the line information directory,
10717      we concatenate it to the filename when it makes sense.
10718      Note that the Dwarf3 standard says (speaking of filenames in line
10719      information): ``The directory index is ignored for file names
10720      that represent full path names''.  Thus ignoring dirname in the
10721      `else' branch below isn't an issue.  */
10722
10723   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10724     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10725   else
10726     fullname = filename;
10727
10728   start_subfile (fullname, comp_dir);
10729
10730   if (fullname != filename)
10731     xfree (fullname);
10732 }
10733
10734 static void
10735 var_decode_location (struct attribute *attr, struct symbol *sym,
10736                      struct dwarf2_cu *cu)
10737 {
10738   struct objfile *objfile = cu->objfile;
10739   struct comp_unit_head *cu_header = &cu->header;
10740
10741   /* NOTE drow/2003-01-30: There used to be a comment and some special
10742      code here to turn a symbol with DW_AT_external and a
10743      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10744      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10745      with some versions of binutils) where shared libraries could have
10746      relocations against symbols in their debug information - the
10747      minimal symbol would have the right address, but the debug info
10748      would not.  It's no longer necessary, because we will explicitly
10749      apply relocations when we read in the debug information now.  */
10750
10751   /* A DW_AT_location attribute with no contents indicates that a
10752      variable has been optimized away.  */
10753   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10754     {
10755       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10756       return;
10757     }
10758
10759   /* Handle one degenerate form of location expression specially, to
10760      preserve GDB's previous behavior when section offsets are
10761      specified.  If this is just a DW_OP_addr then mark this symbol
10762      as LOC_STATIC.  */
10763
10764   if (attr_form_is_block (attr)
10765       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10766       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10767     {
10768       unsigned int dummy;
10769
10770       SYMBOL_VALUE_ADDRESS (sym) =
10771         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10772       SYMBOL_CLASS (sym) = LOC_STATIC;
10773       fixup_symbol_section (sym, objfile);
10774       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10775                                               SYMBOL_SECTION (sym));
10776       return;
10777     }
10778
10779   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10780      expression evaluator, and use LOC_COMPUTED only when necessary
10781      (i.e. when the value of a register or memory location is
10782      referenced, or a thread-local block, etc.).  Then again, it might
10783      not be worthwhile.  I'm assuming that it isn't unless performance
10784      or memory numbers show me otherwise.  */
10785
10786   dwarf2_symbol_mark_computed (attr, sym, cu);
10787   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10788 }
10789
10790 /* Given a pointer to a DWARF information entry, figure out if we need
10791    to make a symbol table entry for it, and if so, create a new entry
10792    and return a pointer to it.
10793    If TYPE is NULL, determine symbol type from the die, otherwise
10794    used the passed type.
10795    If SPACE is not NULL, use it to hold the new symbol.  If it is
10796    NULL, allocate a new symbol on the objfile's obstack.  */
10797
10798 static struct symbol *
10799 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10800                  struct symbol *space)
10801 {
10802   struct objfile *objfile = cu->objfile;
10803   struct symbol *sym = NULL;
10804   char *name;
10805   struct attribute *attr = NULL;
10806   struct attribute *attr2 = NULL;
10807   CORE_ADDR baseaddr;
10808   struct pending **list_to_add = NULL;
10809
10810   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10811
10812   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10813
10814   name = dwarf2_name (die, cu);
10815   if (name)
10816     {
10817       const char *linkagename;
10818       int suppress_add = 0;
10819
10820       if (space)
10821         sym = space;
10822       else
10823         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10824       OBJSTAT (objfile, n_syms++);
10825
10826       /* Cache this symbol's name and the name's demangled form (if any).  */
10827       SYMBOL_SET_LANGUAGE (sym, cu->language);
10828       linkagename = dwarf2_physname (name, die, cu);
10829       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10830
10831       /* Fortran does not have mangling standard and the mangling does differ
10832          between gfortran, iFort etc.  */
10833       if (cu->language == language_fortran
10834           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10835         symbol_set_demangled_name (&(sym->ginfo),
10836                                    (char *) dwarf2_full_name (name, die, cu),
10837                                    NULL);
10838
10839       /* Default assumptions.
10840          Use the passed type or decode it from the die.  */
10841       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10842       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10843       if (type != NULL)
10844         SYMBOL_TYPE (sym) = type;
10845       else
10846         SYMBOL_TYPE (sym) = die_type (die, cu);
10847       attr = dwarf2_attr (die,
10848                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10849                           cu);
10850       if (attr)
10851         {
10852           SYMBOL_LINE (sym) = DW_UNSND (attr);
10853         }
10854
10855       attr = dwarf2_attr (die,
10856                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10857                           cu);
10858       if (attr)
10859         {
10860           int file_index = DW_UNSND (attr);
10861
10862           if (cu->line_header == NULL
10863               || file_index > cu->line_header->num_file_names)
10864             complaint (&symfile_complaints,
10865                        _("file index out of range"));
10866           else if (file_index > 0)
10867             {
10868               struct file_entry *fe;
10869
10870               fe = &cu->line_header->file_names[file_index - 1];
10871               SYMBOL_SYMTAB (sym) = fe->symtab;
10872             }
10873         }
10874
10875       switch (die->tag)
10876         {
10877         case DW_TAG_label:
10878           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10879           if (attr)
10880             {
10881               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10882             }
10883           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10884           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10885           SYMBOL_CLASS (sym) = LOC_LABEL;
10886           add_symbol_to_list (sym, cu->list_in_scope);
10887           break;
10888         case DW_TAG_subprogram:
10889           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10890              finish_block.  */
10891           SYMBOL_CLASS (sym) = LOC_BLOCK;
10892           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10893           if ((attr2 && (DW_UNSND (attr2) != 0))
10894               || cu->language == language_ada)
10895             {
10896               /* Subprograms marked external are stored as a global symbol.
10897                  Ada subprograms, whether marked external or not, are always
10898                  stored as a global symbol, because we want to be able to
10899                  access them globally.  For instance, we want to be able
10900                  to break on a nested subprogram without having to
10901                  specify the context.  */
10902               list_to_add = &global_symbols;
10903             }
10904           else
10905             {
10906               list_to_add = cu->list_in_scope;
10907             }
10908           break;
10909         case DW_TAG_inlined_subroutine:
10910           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10911              finish_block.  */
10912           SYMBOL_CLASS (sym) = LOC_BLOCK;
10913           SYMBOL_INLINED (sym) = 1;
10914           /* Do not add the symbol to any lists.  It will be found via
10915              BLOCK_FUNCTION from the blockvector.  */
10916           break;
10917         case DW_TAG_template_value_param:
10918           suppress_add = 1;
10919           /* Fall through.  */
10920         case DW_TAG_constant:
10921         case DW_TAG_variable:
10922         case DW_TAG_member:
10923           /* Compilation with minimal debug info may result in
10924              variables with missing type entries.  Change the
10925              misleading `void' type to something sensible.  */
10926           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10927             SYMBOL_TYPE (sym)
10928               = objfile_type (objfile)->nodebug_data_symbol;
10929
10930           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10931           /* In the case of DW_TAG_member, we should only be called for
10932              static const members.  */
10933           if (die->tag == DW_TAG_member)
10934             {
10935               /* dwarf2_add_field uses die_is_declaration,
10936                  so we do the same.  */
10937               gdb_assert (die_is_declaration (die, cu));
10938               gdb_assert (attr);
10939             }
10940           if (attr)
10941             {
10942               dwarf2_const_value (attr, sym, cu);
10943               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10944               if (!suppress_add)
10945                 {
10946                   if (attr2 && (DW_UNSND (attr2) != 0))
10947                     list_to_add = &global_symbols;
10948                   else
10949                     list_to_add = cu->list_in_scope;
10950                 }
10951               break;
10952             }
10953           attr = dwarf2_attr (die, DW_AT_location, cu);
10954           if (attr)
10955             {
10956               var_decode_location (attr, sym, cu);
10957               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10958               if (SYMBOL_CLASS (sym) == LOC_STATIC
10959                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10960                   && !dwarf2_per_objfile->has_section_at_zero)
10961                 {
10962                   /* When a static variable is eliminated by the linker,
10963                      the corresponding debug information is not stripped
10964                      out, but the variable address is set to null;
10965                      do not add such variables into symbol table.  */
10966                 }
10967               else if (attr2 && (DW_UNSND (attr2) != 0))
10968                 {
10969                   /* Workaround gfortran PR debug/40040 - it uses
10970                      DW_AT_location for variables in -fPIC libraries which may
10971                      get overriden by other libraries/executable and get
10972                      a different address.  Resolve it by the minimal symbol
10973                      which may come from inferior's executable using copy
10974                      relocation.  Make this workaround only for gfortran as for
10975                      other compilers GDB cannot guess the minimal symbol
10976                      Fortran mangling kind.  */
10977                   if (cu->language == language_fortran && die->parent
10978                       && die->parent->tag == DW_TAG_module
10979                       && cu->producer
10980                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10981                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10982
10983                   /* A variable with DW_AT_external is never static,
10984                      but it may be block-scoped.  */
10985                   list_to_add = (cu->list_in_scope == &file_symbols
10986                                  ? &global_symbols : cu->list_in_scope);
10987                 }
10988               else
10989                 list_to_add = cu->list_in_scope;
10990             }
10991           else
10992             {
10993               /* We do not know the address of this symbol.
10994                  If it is an external symbol and we have type information
10995                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10996                  The address of the variable will then be determined from
10997                  the minimal symbol table whenever the variable is
10998                  referenced.  */
10999               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11000               if (attr2 && (DW_UNSND (attr2) != 0)
11001                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11002                 {
11003                   /* A variable with DW_AT_external is never static, but it
11004                      may be block-scoped.  */
11005                   list_to_add = (cu->list_in_scope == &file_symbols
11006                                  ? &global_symbols : cu->list_in_scope);
11007
11008                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11009                 }
11010               else if (!die_is_declaration (die, cu))
11011                 {
11012                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11013                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11014                   if (!suppress_add)
11015                     list_to_add = cu->list_in_scope;
11016                 }
11017             }
11018           break;
11019         case DW_TAG_formal_parameter:
11020           /* If we are inside a function, mark this as an argument.  If
11021              not, we might be looking at an argument to an inlined function
11022              when we do not have enough information to show inlined frames;
11023              pretend it's a local variable in that case so that the user can
11024              still see it.  */
11025           if (context_stack_depth > 0
11026               && context_stack[context_stack_depth - 1].name != NULL)
11027             SYMBOL_IS_ARGUMENT (sym) = 1;
11028           attr = dwarf2_attr (die, DW_AT_location, cu);
11029           if (attr)
11030             {
11031               var_decode_location (attr, sym, cu);
11032             }
11033           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11034           if (attr)
11035             {
11036               dwarf2_const_value (attr, sym, cu);
11037             }
11038           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11039           if (attr && DW_UNSND (attr))
11040             {
11041               struct type *ref_type;
11042
11043               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11044               SYMBOL_TYPE (sym) = ref_type;
11045             }
11046
11047           list_to_add = cu->list_in_scope;
11048           break;
11049         case DW_TAG_unspecified_parameters:
11050           /* From varargs functions; gdb doesn't seem to have any
11051              interest in this information, so just ignore it for now.
11052              (FIXME?) */
11053           break;
11054         case DW_TAG_template_type_param:
11055           suppress_add = 1;
11056           /* Fall through.  */
11057         case DW_TAG_class_type:
11058         case DW_TAG_interface_type:
11059         case DW_TAG_structure_type:
11060         case DW_TAG_union_type:
11061         case DW_TAG_set_type:
11062         case DW_TAG_enumeration_type:
11063           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11064           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11065
11066           {
11067             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11068                really ever be static objects: otherwise, if you try
11069                to, say, break of a class's method and you're in a file
11070                which doesn't mention that class, it won't work unless
11071                the check for all static symbols in lookup_symbol_aux
11072                saves you.  See the OtherFileClass tests in
11073                gdb.c++/namespace.exp.  */
11074
11075             if (!suppress_add)
11076               {
11077                 list_to_add = (cu->list_in_scope == &file_symbols
11078                                && (cu->language == language_cplus
11079                                    || cu->language == language_java)
11080                                ? &global_symbols : cu->list_in_scope);
11081
11082                 /* The semantics of C++ state that "struct foo {
11083                    ... }" also defines a typedef for "foo".  A Java
11084                    class declaration also defines a typedef for the
11085                    class.  */
11086                 if (cu->language == language_cplus
11087                     || cu->language == language_java
11088                     || cu->language == language_ada)
11089                   {
11090                     /* The symbol's name is already allocated along
11091                        with this objfile, so we don't need to
11092                        duplicate it for the type.  */
11093                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11094                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11095                   }
11096               }
11097           }
11098           break;
11099         case DW_TAG_typedef:
11100           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11101           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11102           list_to_add = cu->list_in_scope;
11103           break;
11104         case DW_TAG_base_type:
11105         case DW_TAG_subrange_type:
11106           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11107           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11108           list_to_add = cu->list_in_scope;
11109           break;
11110         case DW_TAG_enumerator:
11111           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11112           if (attr)
11113             {
11114               dwarf2_const_value (attr, sym, cu);
11115             }
11116           {
11117             /* NOTE: carlton/2003-11-10: See comment above in the
11118                DW_TAG_class_type, etc. block.  */
11119
11120             list_to_add = (cu->list_in_scope == &file_symbols
11121                            && (cu->language == language_cplus
11122                                || cu->language == language_java)
11123                            ? &global_symbols : cu->list_in_scope);
11124           }
11125           break;
11126         case DW_TAG_namespace:
11127           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11128           list_to_add = &global_symbols;
11129           break;
11130         default:
11131           /* Not a tag we recognize.  Hopefully we aren't processing
11132              trash data, but since we must specifically ignore things
11133              we don't recognize, there is nothing else we should do at
11134              this point.  */
11135           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11136                      dwarf_tag_name (die->tag));
11137           break;
11138         }
11139
11140       if (suppress_add)
11141         {
11142           sym->hash_next = objfile->template_symbols;
11143           objfile->template_symbols = sym;
11144           list_to_add = NULL;
11145         }
11146
11147       if (list_to_add != NULL)
11148         add_symbol_to_list (sym, list_to_add);
11149
11150       /* For the benefit of old versions of GCC, check for anonymous
11151          namespaces based on the demangled name.  */
11152       if (!processing_has_namespace_info
11153           && cu->language == language_cplus)
11154         cp_scan_for_anonymous_namespaces (sym);
11155     }
11156   return (sym);
11157 }
11158
11159 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11160
11161 static struct symbol *
11162 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11163 {
11164   return new_symbol_full (die, type, cu, NULL);
11165 }
11166
11167 /* Given an attr with a DW_FORM_dataN value in host byte order,
11168    zero-extend it as appropriate for the symbol's type.  The DWARF
11169    standard (v4) is not entirely clear about the meaning of using
11170    DW_FORM_dataN for a constant with a signed type, where the type is
11171    wider than the data.  The conclusion of a discussion on the DWARF
11172    list was that this is unspecified.  We choose to always zero-extend
11173    because that is the interpretation long in use by GCC.  */
11174
11175 static gdb_byte *
11176 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11177                          const char *name, struct obstack *obstack,
11178                          struct dwarf2_cu *cu, long *value, int bits)
11179 {
11180   struct objfile *objfile = cu->objfile;
11181   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11182                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11183   LONGEST l = DW_UNSND (attr);
11184
11185   if (bits < sizeof (*value) * 8)
11186     {
11187       l &= ((LONGEST) 1 << bits) - 1;
11188       *value = l;
11189     }
11190   else if (bits == sizeof (*value) * 8)
11191     *value = l;
11192   else
11193     {
11194       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11195       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11196       return bytes;
11197     }
11198
11199   return NULL;
11200 }
11201
11202 /* Read a constant value from an attribute.  Either set *VALUE, or if
11203    the value does not fit in *VALUE, set *BYTES - either already
11204    allocated on the objfile obstack, or newly allocated on OBSTACK,
11205    or, set *BATON, if we translated the constant to a location
11206    expression.  */
11207
11208 static void
11209 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11210                          const char *name, struct obstack *obstack,
11211                          struct dwarf2_cu *cu,
11212                          long *value, gdb_byte **bytes,
11213                          struct dwarf2_locexpr_baton **baton)
11214 {
11215   struct objfile *objfile = cu->objfile;
11216   struct comp_unit_head *cu_header = &cu->header;
11217   struct dwarf_block *blk;
11218   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11219                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11220
11221   *value = 0;
11222   *bytes = NULL;
11223   *baton = NULL;
11224
11225   switch (attr->form)
11226     {
11227     case DW_FORM_addr:
11228       {
11229         gdb_byte *data;
11230
11231         if (TYPE_LENGTH (type) != cu_header->addr_size)
11232           dwarf2_const_value_length_mismatch_complaint (name,
11233                                                         cu_header->addr_size,
11234                                                         TYPE_LENGTH (type));
11235         /* Symbols of this form are reasonably rare, so we just
11236            piggyback on the existing location code rather than writing
11237            a new implementation of symbol_computed_ops.  */
11238         *baton = obstack_alloc (&objfile->objfile_obstack,
11239                                 sizeof (struct dwarf2_locexpr_baton));
11240         (*baton)->per_cu = cu->per_cu;
11241         gdb_assert ((*baton)->per_cu);
11242
11243         (*baton)->size = 2 + cu_header->addr_size;
11244         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11245         (*baton)->data = data;
11246
11247         data[0] = DW_OP_addr;
11248         store_unsigned_integer (&data[1], cu_header->addr_size,
11249                                 byte_order, DW_ADDR (attr));
11250         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11251       }
11252       break;
11253     case DW_FORM_string:
11254     case DW_FORM_strp:
11255       /* DW_STRING is already allocated on the objfile obstack, point
11256          directly to it.  */
11257       *bytes = (gdb_byte *) DW_STRING (attr);
11258       break;
11259     case DW_FORM_block1:
11260     case DW_FORM_block2:
11261     case DW_FORM_block4:
11262     case DW_FORM_block:
11263     case DW_FORM_exprloc:
11264       blk = DW_BLOCK (attr);
11265       if (TYPE_LENGTH (type) != blk->size)
11266         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11267                                                       TYPE_LENGTH (type));
11268       *bytes = blk->data;
11269       break;
11270
11271       /* The DW_AT_const_value attributes are supposed to carry the
11272          symbol's value "represented as it would be on the target
11273          architecture."  By the time we get here, it's already been
11274          converted to host endianness, so we just need to sign- or
11275          zero-extend it as appropriate.  */
11276     case DW_FORM_data1:
11277       *bytes = dwarf2_const_value_data (attr, type, name,
11278                                         obstack, cu, value, 8);
11279       break;
11280     case DW_FORM_data2:
11281       *bytes = dwarf2_const_value_data (attr, type, name,
11282                                         obstack, cu, value, 16);
11283       break;
11284     case DW_FORM_data4:
11285       *bytes = dwarf2_const_value_data (attr, type, name,
11286                                         obstack, cu, value, 32);
11287       break;
11288     case DW_FORM_data8:
11289       *bytes = dwarf2_const_value_data (attr, type, name,
11290                                         obstack, cu, value, 64);
11291       break;
11292
11293     case DW_FORM_sdata:
11294       *value = DW_SND (attr);
11295       break;
11296
11297     case DW_FORM_udata:
11298       *value = DW_UNSND (attr);
11299       break;
11300
11301     default:
11302       complaint (&symfile_complaints,
11303                  _("unsupported const value attribute form: '%s'"),
11304                  dwarf_form_name (attr->form));
11305       *value = 0;
11306       break;
11307     }
11308 }
11309
11310
11311 /* Copy constant value from an attribute to a symbol.  */
11312
11313 static void
11314 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11315                     struct dwarf2_cu *cu)
11316 {
11317   struct objfile *objfile = cu->objfile;
11318   struct comp_unit_head *cu_header = &cu->header;
11319   long value;
11320   gdb_byte *bytes;
11321   struct dwarf2_locexpr_baton *baton;
11322
11323   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11324                            SYMBOL_PRINT_NAME (sym),
11325                            &objfile->objfile_obstack, cu,
11326                            &value, &bytes, &baton);
11327
11328   if (baton != NULL)
11329     {
11330       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11331       SYMBOL_LOCATION_BATON (sym) = baton;
11332       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11333     }
11334   else if (bytes != NULL)
11335      {
11336       SYMBOL_VALUE_BYTES (sym) = bytes;
11337       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11338     }
11339   else
11340     {
11341       SYMBOL_VALUE (sym) = value;
11342       SYMBOL_CLASS (sym) = LOC_CONST;
11343     }
11344 }
11345
11346 /* Return the type of the die in question using its DW_AT_type attribute.  */
11347
11348 static struct type *
11349 die_type (struct die_info *die, struct dwarf2_cu *cu)
11350 {
11351   struct attribute *type_attr;
11352
11353   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11354   if (!type_attr)
11355     {
11356       /* A missing DW_AT_type represents a void type.  */
11357       return objfile_type (cu->objfile)->builtin_void;
11358     }
11359
11360   return lookup_die_type (die, type_attr, cu);
11361 }
11362
11363 /* True iff CU's producer generates GNAT Ada auxiliary information
11364    that allows to find parallel types through that information instead
11365    of having to do expensive parallel lookups by type name.  */
11366
11367 static int
11368 need_gnat_info (struct dwarf2_cu *cu)
11369 {
11370   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11371      of GNAT produces this auxiliary information, without any indication
11372      that it is produced.  Part of enhancing the FSF version of GNAT
11373      to produce that information will be to put in place an indicator
11374      that we can use in order to determine whether the descriptive type
11375      info is available or not.  One suggestion that has been made is
11376      to use a new attribute, attached to the CU die.  For now, assume
11377      that the descriptive type info is not available.  */
11378   return 0;
11379 }
11380
11381 /* Return the auxiliary type of the die in question using its
11382    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11383    attribute is not present.  */
11384
11385 static struct type *
11386 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11387 {
11388   struct attribute *type_attr;
11389
11390   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11391   if (!type_attr)
11392     return NULL;
11393
11394   return lookup_die_type (die, type_attr, cu);
11395 }
11396
11397 /* If DIE has a descriptive_type attribute, then set the TYPE's
11398    descriptive type accordingly.  */
11399
11400 static void
11401 set_descriptive_type (struct type *type, struct die_info *die,
11402                       struct dwarf2_cu *cu)
11403 {
11404   struct type *descriptive_type = die_descriptive_type (die, cu);
11405
11406   if (descriptive_type)
11407     {
11408       ALLOCATE_GNAT_AUX_TYPE (type);
11409       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11410     }
11411 }
11412
11413 /* Return the containing type of the die in question using its
11414    DW_AT_containing_type attribute.  */
11415
11416 static struct type *
11417 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11418 {
11419   struct attribute *type_attr;
11420
11421   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11422   if (!type_attr)
11423     error (_("Dwarf Error: Problem turning containing type into gdb type "
11424              "[in module %s]"), cu->objfile->name);
11425
11426   return lookup_die_type (die, type_attr, cu);
11427 }
11428
11429 /* Look up the type of DIE in CU using its type attribute ATTR.
11430    If there is no type substitute an error marker.  */
11431
11432 static struct type *
11433 lookup_die_type (struct die_info *die, struct attribute *attr,
11434                  struct dwarf2_cu *cu)
11435 {
11436   struct type *this_type;
11437
11438   /* First see if we have it cached.  */
11439
11440   if (is_ref_attr (attr))
11441     {
11442       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11443
11444       this_type = get_die_type_at_offset (offset, cu->per_cu);
11445     }
11446   else if (attr->form == DW_FORM_sig8)
11447     {
11448       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11449       struct dwarf2_cu *sig_cu;
11450       unsigned int offset;
11451
11452       /* sig_type will be NULL if the signatured type is missing from
11453          the debug info.  */
11454       if (sig_type == NULL)
11455         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11456                  "at 0x%x [in module %s]"),
11457                die->offset, cu->objfile->name);
11458
11459       gdb_assert (sig_type->per_cu.from_debug_types);
11460       offset = sig_type->offset + sig_type->type_offset;
11461       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11462     }
11463   else
11464     {
11465       dump_die_for_error (die);
11466       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11467              dwarf_attr_name (attr->name), cu->objfile->name);
11468     }
11469
11470   /* If not cached we need to read it in.  */
11471
11472   if (this_type == NULL)
11473     {
11474       struct die_info *type_die;
11475       struct dwarf2_cu *type_cu = cu;
11476
11477       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11478       /* If the type is cached, we should have found it above.  */
11479       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11480       this_type = read_type_die_1 (type_die, type_cu);
11481     }
11482
11483   /* If we still don't have a type use an error marker.  */
11484
11485   if (this_type == NULL)
11486     {
11487       char *message, *saved;
11488
11489       /* read_type_die already issued a complaint.  */
11490       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11491                             cu->objfile->name,
11492                             cu->header.offset,
11493                             die->offset);
11494       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11495                              message, strlen (message));
11496       xfree (message);
11497
11498       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11499     }
11500
11501   return this_type;
11502 }
11503
11504 /* Return the type in DIE, CU.
11505    Returns NULL for invalid types.
11506
11507    This first does a lookup in the appropriate type_hash table,
11508    and only reads the die in if necessary.
11509
11510    NOTE: This can be called when reading in partial or full symbols.  */
11511
11512 static struct type *
11513 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11514 {
11515   struct type *this_type;
11516
11517   this_type = get_die_type (die, cu);
11518   if (this_type)
11519     return this_type;
11520
11521   return read_type_die_1 (die, cu);
11522 }
11523
11524 /* Read the type in DIE, CU.
11525    Returns NULL for invalid types.  */
11526
11527 static struct type *
11528 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11529 {
11530   struct type *this_type = NULL;
11531
11532   switch (die->tag)
11533     {
11534     case DW_TAG_class_type:
11535     case DW_TAG_interface_type:
11536     case DW_TAG_structure_type:
11537     case DW_TAG_union_type:
11538       this_type = read_structure_type (die, cu);
11539       break;
11540     case DW_TAG_enumeration_type:
11541       this_type = read_enumeration_type (die, cu);
11542       break;
11543     case DW_TAG_subprogram:
11544     case DW_TAG_subroutine_type:
11545     case DW_TAG_inlined_subroutine:
11546       this_type = read_subroutine_type (die, cu);
11547       break;
11548     case DW_TAG_array_type:
11549       this_type = read_array_type (die, cu);
11550       break;
11551     case DW_TAG_set_type:
11552       this_type = read_set_type (die, cu);
11553       break;
11554     case DW_TAG_pointer_type:
11555       this_type = read_tag_pointer_type (die, cu);
11556       break;
11557     case DW_TAG_ptr_to_member_type:
11558       this_type = read_tag_ptr_to_member_type (die, cu);
11559       break;
11560     case DW_TAG_reference_type:
11561       this_type = read_tag_reference_type (die, cu);
11562       break;
11563     case DW_TAG_const_type:
11564       this_type = read_tag_const_type (die, cu);
11565       break;
11566     case DW_TAG_volatile_type:
11567       this_type = read_tag_volatile_type (die, cu);
11568       break;
11569     case DW_TAG_string_type:
11570       this_type = read_tag_string_type (die, cu);
11571       break;
11572     case DW_TAG_typedef:
11573       this_type = read_typedef (die, cu);
11574       break;
11575     case DW_TAG_subrange_type:
11576       this_type = read_subrange_type (die, cu);
11577       break;
11578     case DW_TAG_base_type:
11579       this_type = read_base_type (die, cu);
11580       break;
11581     case DW_TAG_unspecified_type:
11582       this_type = read_unspecified_type (die, cu);
11583       break;
11584     case DW_TAG_namespace:
11585       this_type = read_namespace_type (die, cu);
11586       break;
11587     case DW_TAG_module:
11588       this_type = read_module_type (die, cu);
11589       break;
11590     default:
11591       complaint (&symfile_complaints,
11592                  _("unexpected tag in read_type_die: '%s'"),
11593                  dwarf_tag_name (die->tag));
11594       break;
11595     }
11596
11597   return this_type;
11598 }
11599
11600 /* See if we can figure out if the class lives in a namespace.  We do
11601    this by looking for a member function; its demangled name will
11602    contain namespace info, if there is any.
11603    Return the computed name or NULL.
11604    Space for the result is allocated on the objfile's obstack.
11605    This is the full-die version of guess_partial_die_structure_name.
11606    In this case we know DIE has no useful parent.  */
11607
11608 static char *
11609 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11610 {
11611   struct die_info *spec_die;
11612   struct dwarf2_cu *spec_cu;
11613   struct die_info *child;
11614
11615   spec_cu = cu;
11616   spec_die = die_specification (die, &spec_cu);
11617   if (spec_die != NULL)
11618     {
11619       die = spec_die;
11620       cu = spec_cu;
11621     }
11622
11623   for (child = die->child;
11624        child != NULL;
11625        child = child->sibling)
11626     {
11627       if (child->tag == DW_TAG_subprogram)
11628         {
11629           struct attribute *attr;
11630
11631           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11632           if (attr == NULL)
11633             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11634           if (attr != NULL)
11635             {
11636               char *actual_name
11637                 = language_class_name_from_physname (cu->language_defn,
11638                                                      DW_STRING (attr));
11639               char *name = NULL;
11640
11641               if (actual_name != NULL)
11642                 {
11643                   char *die_name = dwarf2_name (die, cu);
11644
11645                   if (die_name != NULL
11646                       && strcmp (die_name, actual_name) != 0)
11647                     {
11648                       /* Strip off the class name from the full name.
11649                          We want the prefix.  */
11650                       int die_name_len = strlen (die_name);
11651                       int actual_name_len = strlen (actual_name);
11652
11653                       /* Test for '::' as a sanity check.  */
11654                       if (actual_name_len > die_name_len + 2
11655                           && actual_name[actual_name_len
11656                                          - die_name_len - 1] == ':')
11657                         name =
11658                           obsavestring (actual_name,
11659                                         actual_name_len - die_name_len - 2,
11660                                         &cu->objfile->objfile_obstack);
11661                     }
11662                 }
11663               xfree (actual_name);
11664               return name;
11665             }
11666         }
11667     }
11668
11669   return NULL;
11670 }
11671
11672 /* Return the name of the namespace/class that DIE is defined within,
11673    or "" if we can't tell.  The caller should not xfree the result.
11674
11675    For example, if we're within the method foo() in the following
11676    code:
11677
11678    namespace N {
11679      class C {
11680        void foo () {
11681        }
11682      };
11683    }
11684
11685    then determine_prefix on foo's die will return "N::C".  */
11686
11687 static char *
11688 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11689 {
11690   struct die_info *parent, *spec_die;
11691   struct dwarf2_cu *spec_cu;
11692   struct type *parent_type;
11693
11694   if (cu->language != language_cplus && cu->language != language_java
11695       && cu->language != language_fortran)
11696     return "";
11697
11698   /* We have to be careful in the presence of DW_AT_specification.
11699      For example, with GCC 3.4, given the code
11700
11701      namespace N {
11702        void foo() {
11703          // Definition of N::foo.
11704        }
11705      }
11706
11707      then we'll have a tree of DIEs like this:
11708
11709      1: DW_TAG_compile_unit
11710        2: DW_TAG_namespace        // N
11711          3: DW_TAG_subprogram     // declaration of N::foo
11712        4: DW_TAG_subprogram       // definition of N::foo
11713             DW_AT_specification   // refers to die #3
11714
11715      Thus, when processing die #4, we have to pretend that we're in
11716      the context of its DW_AT_specification, namely the contex of die
11717      #3.  */
11718   spec_cu = cu;
11719   spec_die = die_specification (die, &spec_cu);
11720   if (spec_die == NULL)
11721     parent = die->parent;
11722   else
11723     {
11724       parent = spec_die->parent;
11725       cu = spec_cu;
11726     }
11727
11728   if (parent == NULL)
11729     return "";
11730   else if (parent->building_fullname)
11731     {
11732       const char *name;
11733       const char *parent_name;
11734
11735       /* It has been seen on RealView 2.2 built binaries,
11736          DW_TAG_template_type_param types actually _defined_ as
11737          children of the parent class:
11738
11739          enum E {};
11740          template class <class Enum> Class{};
11741          Class<enum E> class_e;
11742
11743          1: DW_TAG_class_type (Class)
11744            2: DW_TAG_enumeration_type (E)
11745              3: DW_TAG_enumerator (enum1:0)
11746              3: DW_TAG_enumerator (enum2:1)
11747              ...
11748            2: DW_TAG_template_type_param
11749               DW_AT_type  DW_FORM_ref_udata (E)
11750
11751          Besides being broken debug info, it can put GDB into an
11752          infinite loop.  Consider:
11753
11754          When we're building the full name for Class<E>, we'll start
11755          at Class, and go look over its template type parameters,
11756          finding E.  We'll then try to build the full name of E, and
11757          reach here.  We're now trying to build the full name of E,
11758          and look over the parent DIE for containing scope.  In the
11759          broken case, if we followed the parent DIE of E, we'd again
11760          find Class, and once again go look at its template type
11761          arguments, etc., etc.  Simply don't consider such parent die
11762          as source-level parent of this die (it can't be, the language
11763          doesn't allow it), and break the loop here.  */
11764       name = dwarf2_name (die, cu);
11765       parent_name = dwarf2_name (parent, cu);
11766       complaint (&symfile_complaints,
11767                  _("template param type '%s' defined within parent '%s'"),
11768                  name ? name : "<unknown>",
11769                  parent_name ? parent_name : "<unknown>");
11770       return "";
11771     }
11772   else
11773     switch (parent->tag)
11774       {
11775       case DW_TAG_namespace:
11776         parent_type = read_type_die (parent, cu);
11777         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11778            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11779            Work around this problem here.  */
11780         if (cu->language == language_cplus
11781             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11782           return "";
11783         /* We give a name to even anonymous namespaces.  */
11784         return TYPE_TAG_NAME (parent_type);
11785       case DW_TAG_class_type:
11786       case DW_TAG_interface_type:
11787       case DW_TAG_structure_type:
11788       case DW_TAG_union_type:
11789       case DW_TAG_module:
11790         parent_type = read_type_die (parent, cu);
11791         if (TYPE_TAG_NAME (parent_type) != NULL)
11792           return TYPE_TAG_NAME (parent_type);
11793         else
11794           /* An anonymous structure is only allowed non-static data
11795              members; no typedefs, no member functions, et cetera.
11796              So it does not need a prefix.  */
11797           return "";
11798       case DW_TAG_compile_unit:
11799         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
11800         if (cu->language == language_cplus
11801             && dwarf2_per_objfile->types.asection != NULL
11802             && die->child != NULL
11803             && (die->tag == DW_TAG_class_type
11804                 || die->tag == DW_TAG_structure_type
11805                 || die->tag == DW_TAG_union_type))
11806           {
11807             char *name = guess_full_die_structure_name (die, cu);
11808             if (name != NULL)
11809               return name;
11810           }
11811         return "";
11812       default:
11813         return determine_prefix (parent, cu);
11814       }
11815 }
11816
11817 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11818    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11819    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
11820    an obconcat, otherwise allocate storage for the result.  The CU argument is
11821    used to determine the language and hence, the appropriate separator.  */
11822
11823 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11824
11825 static char *
11826 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11827                  int physname, struct dwarf2_cu *cu)
11828 {
11829   const char *lead = "";
11830   const char *sep;
11831
11832   if (suffix == NULL || suffix[0] == '\0'
11833       || prefix == NULL || prefix[0] == '\0')
11834     sep = "";
11835   else if (cu->language == language_java)
11836     sep = ".";
11837   else if (cu->language == language_fortran && physname)
11838     {
11839       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11840          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11841
11842       lead = "__";
11843       sep = "_MOD_";
11844     }
11845   else
11846     sep = "::";
11847
11848   if (prefix == NULL)
11849     prefix = "";
11850   if (suffix == NULL)
11851     suffix = "";
11852
11853   if (obs == NULL)
11854     {
11855       char *retval
11856         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11857
11858       strcpy (retval, lead);
11859       strcat (retval, prefix);
11860       strcat (retval, sep);
11861       strcat (retval, suffix);
11862       return retval;
11863     }
11864   else
11865     {
11866       /* We have an obstack.  */
11867       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11868     }
11869 }
11870
11871 /* Return sibling of die, NULL if no sibling.  */
11872
11873 static struct die_info *
11874 sibling_die (struct die_info *die)
11875 {
11876   return die->sibling;
11877 }
11878
11879 /* Get name of a die, return NULL if not found.  */
11880
11881 static char *
11882 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11883                           struct obstack *obstack)
11884 {
11885   if (name && cu->language == language_cplus)
11886     {
11887       char *canon_name = cp_canonicalize_string (name);
11888
11889       if (canon_name != NULL)
11890         {
11891           if (strcmp (canon_name, name) != 0)
11892             name = obsavestring (canon_name, strlen (canon_name),
11893                                  obstack);
11894           xfree (canon_name);
11895         }
11896     }
11897
11898   return name;
11899 }
11900
11901 /* Get name of a die, return NULL if not found.  */
11902
11903 static char *
11904 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11905 {
11906   struct attribute *attr;
11907
11908   attr = dwarf2_attr (die, DW_AT_name, cu);
11909   if (!attr || !DW_STRING (attr))
11910     return NULL;
11911
11912   switch (die->tag)
11913     {
11914     case DW_TAG_compile_unit:
11915       /* Compilation units have a DW_AT_name that is a filename, not
11916          a source language identifier.  */
11917     case DW_TAG_enumeration_type:
11918     case DW_TAG_enumerator:
11919       /* These tags always have simple identifiers already; no need
11920          to canonicalize them.  */
11921       return DW_STRING (attr);
11922
11923     case DW_TAG_subprogram:
11924       /* Java constructors will all be named "<init>", so return
11925          the class name when we see this special case.  */
11926       if (cu->language == language_java
11927           && DW_STRING (attr) != NULL
11928           && strcmp (DW_STRING (attr), "<init>") == 0)
11929         {
11930           struct dwarf2_cu *spec_cu = cu;
11931           struct die_info *spec_die;
11932
11933           /* GCJ will output '<init>' for Java constructor names.
11934              For this special case, return the name of the parent class.  */
11935
11936           /* GCJ may output suprogram DIEs with AT_specification set.
11937              If so, use the name of the specified DIE.  */
11938           spec_die = die_specification (die, &spec_cu);
11939           if (spec_die != NULL)
11940             return dwarf2_name (spec_die, spec_cu);
11941
11942           do
11943             {
11944               die = die->parent;
11945               if (die->tag == DW_TAG_class_type)
11946                 return dwarf2_name (die, cu);
11947             }
11948           while (die->tag != DW_TAG_compile_unit);
11949         }
11950       break;
11951
11952     case DW_TAG_class_type:
11953     case DW_TAG_interface_type:
11954     case DW_TAG_structure_type:
11955     case DW_TAG_union_type:
11956       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11957          structures or unions.  These were of the form "._%d" in GCC 4.1,
11958          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11959          and GCC 4.4.  We work around this problem by ignoring these.  */
11960       if (strncmp (DW_STRING (attr), "._", 2) == 0
11961           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11962         return NULL;
11963       break;
11964
11965     default:
11966       break;
11967     }
11968
11969   if (!DW_STRING_IS_CANONICAL (attr))
11970     {
11971       DW_STRING (attr)
11972         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11973                                     &cu->objfile->objfile_obstack);
11974       DW_STRING_IS_CANONICAL (attr) = 1;
11975     }
11976   return DW_STRING (attr);
11977 }
11978
11979 /* Return the die that this die in an extension of, or NULL if there
11980    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11981    containing the return value on output.  */
11982
11983 static struct die_info *
11984 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11985 {
11986   struct attribute *attr;
11987
11988   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11989   if (attr == NULL)
11990     return NULL;
11991
11992   return follow_die_ref (die, attr, ext_cu);
11993 }
11994
11995 /* Convert a DIE tag into its string name.  */
11996
11997 static char *
11998 dwarf_tag_name (unsigned tag)
11999 {
12000   switch (tag)
12001     {
12002     case DW_TAG_padding:
12003       return "DW_TAG_padding";
12004     case DW_TAG_array_type:
12005       return "DW_TAG_array_type";
12006     case DW_TAG_class_type:
12007       return "DW_TAG_class_type";
12008     case DW_TAG_entry_point:
12009       return "DW_TAG_entry_point";
12010     case DW_TAG_enumeration_type:
12011       return "DW_TAG_enumeration_type";
12012     case DW_TAG_formal_parameter:
12013       return "DW_TAG_formal_parameter";
12014     case DW_TAG_imported_declaration:
12015       return "DW_TAG_imported_declaration";
12016     case DW_TAG_label:
12017       return "DW_TAG_label";
12018     case DW_TAG_lexical_block:
12019       return "DW_TAG_lexical_block";
12020     case DW_TAG_member:
12021       return "DW_TAG_member";
12022     case DW_TAG_pointer_type:
12023       return "DW_TAG_pointer_type";
12024     case DW_TAG_reference_type:
12025       return "DW_TAG_reference_type";
12026     case DW_TAG_compile_unit:
12027       return "DW_TAG_compile_unit";
12028     case DW_TAG_string_type:
12029       return "DW_TAG_string_type";
12030     case DW_TAG_structure_type:
12031       return "DW_TAG_structure_type";
12032     case DW_TAG_subroutine_type:
12033       return "DW_TAG_subroutine_type";
12034     case DW_TAG_typedef:
12035       return "DW_TAG_typedef";
12036     case DW_TAG_union_type:
12037       return "DW_TAG_union_type";
12038     case DW_TAG_unspecified_parameters:
12039       return "DW_TAG_unspecified_parameters";
12040     case DW_TAG_variant:
12041       return "DW_TAG_variant";
12042     case DW_TAG_common_block:
12043       return "DW_TAG_common_block";
12044     case DW_TAG_common_inclusion:
12045       return "DW_TAG_common_inclusion";
12046     case DW_TAG_inheritance:
12047       return "DW_TAG_inheritance";
12048     case DW_TAG_inlined_subroutine:
12049       return "DW_TAG_inlined_subroutine";
12050     case DW_TAG_module:
12051       return "DW_TAG_module";
12052     case DW_TAG_ptr_to_member_type:
12053       return "DW_TAG_ptr_to_member_type";
12054     case DW_TAG_set_type:
12055       return "DW_TAG_set_type";
12056     case DW_TAG_subrange_type:
12057       return "DW_TAG_subrange_type";
12058     case DW_TAG_with_stmt:
12059       return "DW_TAG_with_stmt";
12060     case DW_TAG_access_declaration:
12061       return "DW_TAG_access_declaration";
12062     case DW_TAG_base_type:
12063       return "DW_TAG_base_type";
12064     case DW_TAG_catch_block:
12065       return "DW_TAG_catch_block";
12066     case DW_TAG_const_type:
12067       return "DW_TAG_const_type";
12068     case DW_TAG_constant:
12069       return "DW_TAG_constant";
12070     case DW_TAG_enumerator:
12071       return "DW_TAG_enumerator";
12072     case DW_TAG_file_type:
12073       return "DW_TAG_file_type";
12074     case DW_TAG_friend:
12075       return "DW_TAG_friend";
12076     case DW_TAG_namelist:
12077       return "DW_TAG_namelist";
12078     case DW_TAG_namelist_item:
12079       return "DW_TAG_namelist_item";
12080     case DW_TAG_packed_type:
12081       return "DW_TAG_packed_type";
12082     case DW_TAG_subprogram:
12083       return "DW_TAG_subprogram";
12084     case DW_TAG_template_type_param:
12085       return "DW_TAG_template_type_param";
12086     case DW_TAG_template_value_param:
12087       return "DW_TAG_template_value_param";
12088     case DW_TAG_thrown_type:
12089       return "DW_TAG_thrown_type";
12090     case DW_TAG_try_block:
12091       return "DW_TAG_try_block";
12092     case DW_TAG_variant_part:
12093       return "DW_TAG_variant_part";
12094     case DW_TAG_variable:
12095       return "DW_TAG_variable";
12096     case DW_TAG_volatile_type:
12097       return "DW_TAG_volatile_type";
12098     case DW_TAG_dwarf_procedure:
12099       return "DW_TAG_dwarf_procedure";
12100     case DW_TAG_restrict_type:
12101       return "DW_TAG_restrict_type";
12102     case DW_TAG_interface_type:
12103       return "DW_TAG_interface_type";
12104     case DW_TAG_namespace:
12105       return "DW_TAG_namespace";
12106     case DW_TAG_imported_module:
12107       return "DW_TAG_imported_module";
12108     case DW_TAG_unspecified_type:
12109       return "DW_TAG_unspecified_type";
12110     case DW_TAG_partial_unit:
12111       return "DW_TAG_partial_unit";
12112     case DW_TAG_imported_unit:
12113       return "DW_TAG_imported_unit";
12114     case DW_TAG_condition:
12115       return "DW_TAG_condition";
12116     case DW_TAG_shared_type:
12117       return "DW_TAG_shared_type";
12118     case DW_TAG_type_unit:
12119       return "DW_TAG_type_unit";
12120     case DW_TAG_MIPS_loop:
12121       return "DW_TAG_MIPS_loop";
12122     case DW_TAG_HP_array_descriptor:
12123       return "DW_TAG_HP_array_descriptor";
12124     case DW_TAG_format_label:
12125       return "DW_TAG_format_label";
12126     case DW_TAG_function_template:
12127       return "DW_TAG_function_template";
12128     case DW_TAG_class_template:
12129       return "DW_TAG_class_template";
12130     case DW_TAG_GNU_BINCL:
12131       return "DW_TAG_GNU_BINCL";
12132     case DW_TAG_GNU_EINCL:
12133       return "DW_TAG_GNU_EINCL";
12134     case DW_TAG_upc_shared_type:
12135       return "DW_TAG_upc_shared_type";
12136     case DW_TAG_upc_strict_type:
12137       return "DW_TAG_upc_strict_type";
12138     case DW_TAG_upc_relaxed_type:
12139       return "DW_TAG_upc_relaxed_type";
12140     case DW_TAG_PGI_kanji_type:
12141       return "DW_TAG_PGI_kanji_type";
12142     case DW_TAG_PGI_interface_block:
12143       return "DW_TAG_PGI_interface_block";
12144     default:
12145       return "DW_TAG_<unknown>";
12146     }
12147 }
12148
12149 /* Convert a DWARF attribute code into its string name.  */
12150
12151 static char *
12152 dwarf_attr_name (unsigned attr)
12153 {
12154   switch (attr)
12155     {
12156     case DW_AT_sibling:
12157       return "DW_AT_sibling";
12158     case DW_AT_location:
12159       return "DW_AT_location";
12160     case DW_AT_name:
12161       return "DW_AT_name";
12162     case DW_AT_ordering:
12163       return "DW_AT_ordering";
12164     case DW_AT_subscr_data:
12165       return "DW_AT_subscr_data";
12166     case DW_AT_byte_size:
12167       return "DW_AT_byte_size";
12168     case DW_AT_bit_offset:
12169       return "DW_AT_bit_offset";
12170     case DW_AT_bit_size:
12171       return "DW_AT_bit_size";
12172     case DW_AT_element_list:
12173       return "DW_AT_element_list";
12174     case DW_AT_stmt_list:
12175       return "DW_AT_stmt_list";
12176     case DW_AT_low_pc:
12177       return "DW_AT_low_pc";
12178     case DW_AT_high_pc:
12179       return "DW_AT_high_pc";
12180     case DW_AT_language:
12181       return "DW_AT_language";
12182     case DW_AT_member:
12183       return "DW_AT_member";
12184     case DW_AT_discr:
12185       return "DW_AT_discr";
12186     case DW_AT_discr_value:
12187       return "DW_AT_discr_value";
12188     case DW_AT_visibility:
12189       return "DW_AT_visibility";
12190     case DW_AT_import:
12191       return "DW_AT_import";
12192     case DW_AT_string_length:
12193       return "DW_AT_string_length";
12194     case DW_AT_common_reference:
12195       return "DW_AT_common_reference";
12196     case DW_AT_comp_dir:
12197       return "DW_AT_comp_dir";
12198     case DW_AT_const_value:
12199       return "DW_AT_const_value";
12200     case DW_AT_containing_type:
12201       return "DW_AT_containing_type";
12202     case DW_AT_default_value:
12203       return "DW_AT_default_value";
12204     case DW_AT_inline:
12205       return "DW_AT_inline";
12206     case DW_AT_is_optional:
12207       return "DW_AT_is_optional";
12208     case DW_AT_lower_bound:
12209       return "DW_AT_lower_bound";
12210     case DW_AT_producer:
12211       return "DW_AT_producer";
12212     case DW_AT_prototyped:
12213       return "DW_AT_prototyped";
12214     case DW_AT_return_addr:
12215       return "DW_AT_return_addr";
12216     case DW_AT_start_scope:
12217       return "DW_AT_start_scope";
12218     case DW_AT_bit_stride:
12219       return "DW_AT_bit_stride";
12220     case DW_AT_upper_bound:
12221       return "DW_AT_upper_bound";
12222     case DW_AT_abstract_origin:
12223       return "DW_AT_abstract_origin";
12224     case DW_AT_accessibility:
12225       return "DW_AT_accessibility";
12226     case DW_AT_address_class:
12227       return "DW_AT_address_class";
12228     case DW_AT_artificial:
12229       return "DW_AT_artificial";
12230     case DW_AT_base_types:
12231       return "DW_AT_base_types";
12232     case DW_AT_calling_convention:
12233       return "DW_AT_calling_convention";
12234     case DW_AT_count:
12235       return "DW_AT_count";
12236     case DW_AT_data_member_location:
12237       return "DW_AT_data_member_location";
12238     case DW_AT_decl_column:
12239       return "DW_AT_decl_column";
12240     case DW_AT_decl_file:
12241       return "DW_AT_decl_file";
12242     case DW_AT_decl_line:
12243       return "DW_AT_decl_line";
12244     case DW_AT_declaration:
12245       return "DW_AT_declaration";
12246     case DW_AT_discr_list:
12247       return "DW_AT_discr_list";
12248     case DW_AT_encoding:
12249       return "DW_AT_encoding";
12250     case DW_AT_external:
12251       return "DW_AT_external";
12252     case DW_AT_frame_base:
12253       return "DW_AT_frame_base";
12254     case DW_AT_friend:
12255       return "DW_AT_friend";
12256     case DW_AT_identifier_case:
12257       return "DW_AT_identifier_case";
12258     case DW_AT_macro_info:
12259       return "DW_AT_macro_info";
12260     case DW_AT_namelist_items:
12261       return "DW_AT_namelist_items";
12262     case DW_AT_priority:
12263       return "DW_AT_priority";
12264     case DW_AT_segment:
12265       return "DW_AT_segment";
12266     case DW_AT_specification:
12267       return "DW_AT_specification";
12268     case DW_AT_static_link:
12269       return "DW_AT_static_link";
12270     case DW_AT_type:
12271       return "DW_AT_type";
12272     case DW_AT_use_location:
12273       return "DW_AT_use_location";
12274     case DW_AT_variable_parameter:
12275       return "DW_AT_variable_parameter";
12276     case DW_AT_virtuality:
12277       return "DW_AT_virtuality";
12278     case DW_AT_vtable_elem_location:
12279       return "DW_AT_vtable_elem_location";
12280     /* DWARF 3 values.  */
12281     case DW_AT_allocated:
12282       return "DW_AT_allocated";
12283     case DW_AT_associated:
12284       return "DW_AT_associated";
12285     case DW_AT_data_location:
12286       return "DW_AT_data_location";
12287     case DW_AT_byte_stride:
12288       return "DW_AT_byte_stride";
12289     case DW_AT_entry_pc:
12290       return "DW_AT_entry_pc";
12291     case DW_AT_use_UTF8:
12292       return "DW_AT_use_UTF8";
12293     case DW_AT_extension:
12294       return "DW_AT_extension";
12295     case DW_AT_ranges:
12296       return "DW_AT_ranges";
12297     case DW_AT_trampoline:
12298       return "DW_AT_trampoline";
12299     case DW_AT_call_column:
12300       return "DW_AT_call_column";
12301     case DW_AT_call_file:
12302       return "DW_AT_call_file";
12303     case DW_AT_call_line:
12304       return "DW_AT_call_line";
12305     case DW_AT_description:
12306       return "DW_AT_description";
12307     case DW_AT_binary_scale:
12308       return "DW_AT_binary_scale";
12309     case DW_AT_decimal_scale:
12310       return "DW_AT_decimal_scale";
12311     case DW_AT_small:
12312       return "DW_AT_small";
12313     case DW_AT_decimal_sign:
12314       return "DW_AT_decimal_sign";
12315     case DW_AT_digit_count:
12316       return "DW_AT_digit_count";
12317     case DW_AT_picture_string:
12318       return "DW_AT_picture_string";
12319     case DW_AT_mutable:
12320       return "DW_AT_mutable";
12321     case DW_AT_threads_scaled:
12322       return "DW_AT_threads_scaled";
12323     case DW_AT_explicit:
12324       return "DW_AT_explicit";
12325     case DW_AT_object_pointer:
12326       return "DW_AT_object_pointer";
12327     case DW_AT_endianity:
12328       return "DW_AT_endianity";
12329     case DW_AT_elemental:
12330       return "DW_AT_elemental";
12331     case DW_AT_pure:
12332       return "DW_AT_pure";
12333     case DW_AT_recursive:
12334       return "DW_AT_recursive";
12335     /* DWARF 4 values.  */
12336     case DW_AT_signature:
12337       return "DW_AT_signature";
12338     case DW_AT_linkage_name:
12339       return "DW_AT_linkage_name";
12340     /* SGI/MIPS extensions.  */
12341 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12342     case DW_AT_MIPS_fde:
12343       return "DW_AT_MIPS_fde";
12344 #endif
12345     case DW_AT_MIPS_loop_begin:
12346       return "DW_AT_MIPS_loop_begin";
12347     case DW_AT_MIPS_tail_loop_begin:
12348       return "DW_AT_MIPS_tail_loop_begin";
12349     case DW_AT_MIPS_epilog_begin:
12350       return "DW_AT_MIPS_epilog_begin";
12351     case DW_AT_MIPS_loop_unroll_factor:
12352       return "DW_AT_MIPS_loop_unroll_factor";
12353     case DW_AT_MIPS_software_pipeline_depth:
12354       return "DW_AT_MIPS_software_pipeline_depth";
12355     case DW_AT_MIPS_linkage_name:
12356       return "DW_AT_MIPS_linkage_name";
12357     case DW_AT_MIPS_stride:
12358       return "DW_AT_MIPS_stride";
12359     case DW_AT_MIPS_abstract_name:
12360       return "DW_AT_MIPS_abstract_name";
12361     case DW_AT_MIPS_clone_origin:
12362       return "DW_AT_MIPS_clone_origin";
12363     case DW_AT_MIPS_has_inlines:
12364       return "DW_AT_MIPS_has_inlines";
12365     /* HP extensions.  */
12366 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12367     case DW_AT_HP_block_index:
12368       return "DW_AT_HP_block_index";
12369 #endif
12370     case DW_AT_HP_unmodifiable:
12371       return "DW_AT_HP_unmodifiable";
12372     case DW_AT_HP_actuals_stmt_list:
12373       return "DW_AT_HP_actuals_stmt_list";
12374     case DW_AT_HP_proc_per_section:
12375       return "DW_AT_HP_proc_per_section";
12376     case DW_AT_HP_raw_data_ptr:
12377       return "DW_AT_HP_raw_data_ptr";
12378     case DW_AT_HP_pass_by_reference:
12379       return "DW_AT_HP_pass_by_reference";
12380     case DW_AT_HP_opt_level:
12381       return "DW_AT_HP_opt_level";
12382     case DW_AT_HP_prof_version_id:
12383       return "DW_AT_HP_prof_version_id";
12384     case DW_AT_HP_opt_flags:
12385       return "DW_AT_HP_opt_flags";
12386     case DW_AT_HP_cold_region_low_pc:
12387       return "DW_AT_HP_cold_region_low_pc";
12388     case DW_AT_HP_cold_region_high_pc:
12389       return "DW_AT_HP_cold_region_high_pc";
12390     case DW_AT_HP_all_variables_modifiable:
12391       return "DW_AT_HP_all_variables_modifiable";
12392     case DW_AT_HP_linkage_name:
12393       return "DW_AT_HP_linkage_name";
12394     case DW_AT_HP_prof_flags:
12395       return "DW_AT_HP_prof_flags";
12396     /* GNU extensions.  */
12397     case DW_AT_sf_names:
12398       return "DW_AT_sf_names";
12399     case DW_AT_src_info:
12400       return "DW_AT_src_info";
12401     case DW_AT_mac_info:
12402       return "DW_AT_mac_info";
12403     case DW_AT_src_coords:
12404       return "DW_AT_src_coords";
12405     case DW_AT_body_begin:
12406       return "DW_AT_body_begin";
12407     case DW_AT_body_end:
12408       return "DW_AT_body_end";
12409     case DW_AT_GNU_vector:
12410       return "DW_AT_GNU_vector";
12411     case DW_AT_GNU_odr_signature:
12412       return "DW_AT_GNU_odr_signature";
12413     /* VMS extensions.  */
12414     case DW_AT_VMS_rtnbeg_pd_address:
12415       return "DW_AT_VMS_rtnbeg_pd_address";
12416     /* UPC extension.  */
12417     case DW_AT_upc_threads_scaled:
12418       return "DW_AT_upc_threads_scaled";
12419     /* PGI (STMicroelectronics) extensions.  */
12420     case DW_AT_PGI_lbase:
12421       return "DW_AT_PGI_lbase";
12422     case DW_AT_PGI_soffset:
12423       return "DW_AT_PGI_soffset";
12424     case DW_AT_PGI_lstride:
12425       return "DW_AT_PGI_lstride";
12426     default:
12427       return "DW_AT_<unknown>";
12428     }
12429 }
12430
12431 /* Convert a DWARF value form code into its string name.  */
12432
12433 static char *
12434 dwarf_form_name (unsigned form)
12435 {
12436   switch (form)
12437     {
12438     case DW_FORM_addr:
12439       return "DW_FORM_addr";
12440     case DW_FORM_block2:
12441       return "DW_FORM_block2";
12442     case DW_FORM_block4:
12443       return "DW_FORM_block4";
12444     case DW_FORM_data2:
12445       return "DW_FORM_data2";
12446     case DW_FORM_data4:
12447       return "DW_FORM_data4";
12448     case DW_FORM_data8:
12449       return "DW_FORM_data8";
12450     case DW_FORM_string:
12451       return "DW_FORM_string";
12452     case DW_FORM_block:
12453       return "DW_FORM_block";
12454     case DW_FORM_block1:
12455       return "DW_FORM_block1";
12456     case DW_FORM_data1:
12457       return "DW_FORM_data1";
12458     case DW_FORM_flag:
12459       return "DW_FORM_flag";
12460     case DW_FORM_sdata:
12461       return "DW_FORM_sdata";
12462     case DW_FORM_strp:
12463       return "DW_FORM_strp";
12464     case DW_FORM_udata:
12465       return "DW_FORM_udata";
12466     case DW_FORM_ref_addr:
12467       return "DW_FORM_ref_addr";
12468     case DW_FORM_ref1:
12469       return "DW_FORM_ref1";
12470     case DW_FORM_ref2:
12471       return "DW_FORM_ref2";
12472     case DW_FORM_ref4:
12473       return "DW_FORM_ref4";
12474     case DW_FORM_ref8:
12475       return "DW_FORM_ref8";
12476     case DW_FORM_ref_udata:
12477       return "DW_FORM_ref_udata";
12478     case DW_FORM_indirect:
12479       return "DW_FORM_indirect";
12480     case DW_FORM_sec_offset:
12481       return "DW_FORM_sec_offset";
12482     case DW_FORM_exprloc:
12483       return "DW_FORM_exprloc";
12484     case DW_FORM_flag_present:
12485       return "DW_FORM_flag_present";
12486     case DW_FORM_sig8:
12487       return "DW_FORM_sig8";
12488     default:
12489       return "DW_FORM_<unknown>";
12490     }
12491 }
12492
12493 /* Convert a DWARF stack opcode into its string name.  */
12494
12495 const char *
12496 dwarf_stack_op_name (unsigned op)
12497 {
12498   switch (op)
12499     {
12500     case DW_OP_addr:
12501       return "DW_OP_addr";
12502     case DW_OP_deref:
12503       return "DW_OP_deref";
12504     case DW_OP_const1u:
12505       return "DW_OP_const1u";
12506     case DW_OP_const1s:
12507       return "DW_OP_const1s";
12508     case DW_OP_const2u:
12509       return "DW_OP_const2u";
12510     case DW_OP_const2s:
12511       return "DW_OP_const2s";
12512     case DW_OP_const4u:
12513       return "DW_OP_const4u";
12514     case DW_OP_const4s:
12515       return "DW_OP_const4s";
12516     case DW_OP_const8u:
12517       return "DW_OP_const8u";
12518     case DW_OP_const8s:
12519       return "DW_OP_const8s";
12520     case DW_OP_constu:
12521       return "DW_OP_constu";
12522     case DW_OP_consts:
12523       return "DW_OP_consts";
12524     case DW_OP_dup:
12525       return "DW_OP_dup";
12526     case DW_OP_drop:
12527       return "DW_OP_drop";
12528     case DW_OP_over:
12529       return "DW_OP_over";
12530     case DW_OP_pick:
12531       return "DW_OP_pick";
12532     case DW_OP_swap:
12533       return "DW_OP_swap";
12534     case DW_OP_rot:
12535       return "DW_OP_rot";
12536     case DW_OP_xderef:
12537       return "DW_OP_xderef";
12538     case DW_OP_abs:
12539       return "DW_OP_abs";
12540     case DW_OP_and:
12541       return "DW_OP_and";
12542     case DW_OP_div:
12543       return "DW_OP_div";
12544     case DW_OP_minus:
12545       return "DW_OP_minus";
12546     case DW_OP_mod:
12547       return "DW_OP_mod";
12548     case DW_OP_mul:
12549       return "DW_OP_mul";
12550     case DW_OP_neg:
12551       return "DW_OP_neg";
12552     case DW_OP_not:
12553       return "DW_OP_not";
12554     case DW_OP_or:
12555       return "DW_OP_or";
12556     case DW_OP_plus:
12557       return "DW_OP_plus";
12558     case DW_OP_plus_uconst:
12559       return "DW_OP_plus_uconst";
12560     case DW_OP_shl:
12561       return "DW_OP_shl";
12562     case DW_OP_shr:
12563       return "DW_OP_shr";
12564     case DW_OP_shra:
12565       return "DW_OP_shra";
12566     case DW_OP_xor:
12567       return "DW_OP_xor";
12568     case DW_OP_bra:
12569       return "DW_OP_bra";
12570     case DW_OP_eq:
12571       return "DW_OP_eq";
12572     case DW_OP_ge:
12573       return "DW_OP_ge";
12574     case DW_OP_gt:
12575       return "DW_OP_gt";
12576     case DW_OP_le:
12577       return "DW_OP_le";
12578     case DW_OP_lt:
12579       return "DW_OP_lt";
12580     case DW_OP_ne:
12581       return "DW_OP_ne";
12582     case DW_OP_skip:
12583       return "DW_OP_skip";
12584     case DW_OP_lit0:
12585       return "DW_OP_lit0";
12586     case DW_OP_lit1:
12587       return "DW_OP_lit1";
12588     case DW_OP_lit2:
12589       return "DW_OP_lit2";
12590     case DW_OP_lit3:
12591       return "DW_OP_lit3";
12592     case DW_OP_lit4:
12593       return "DW_OP_lit4";
12594     case DW_OP_lit5:
12595       return "DW_OP_lit5";
12596     case DW_OP_lit6:
12597       return "DW_OP_lit6";
12598     case DW_OP_lit7:
12599       return "DW_OP_lit7";
12600     case DW_OP_lit8:
12601       return "DW_OP_lit8";
12602     case DW_OP_lit9:
12603       return "DW_OP_lit9";
12604     case DW_OP_lit10:
12605       return "DW_OP_lit10";
12606     case DW_OP_lit11:
12607       return "DW_OP_lit11";
12608     case DW_OP_lit12:
12609       return "DW_OP_lit12";
12610     case DW_OP_lit13:
12611       return "DW_OP_lit13";
12612     case DW_OP_lit14:
12613       return "DW_OP_lit14";
12614     case DW_OP_lit15:
12615       return "DW_OP_lit15";
12616     case DW_OP_lit16:
12617       return "DW_OP_lit16";
12618     case DW_OP_lit17:
12619       return "DW_OP_lit17";
12620     case DW_OP_lit18:
12621       return "DW_OP_lit18";
12622     case DW_OP_lit19:
12623       return "DW_OP_lit19";
12624     case DW_OP_lit20:
12625       return "DW_OP_lit20";
12626     case DW_OP_lit21:
12627       return "DW_OP_lit21";
12628     case DW_OP_lit22:
12629       return "DW_OP_lit22";
12630     case DW_OP_lit23:
12631       return "DW_OP_lit23";
12632     case DW_OP_lit24:
12633       return "DW_OP_lit24";
12634     case DW_OP_lit25:
12635       return "DW_OP_lit25";
12636     case DW_OP_lit26:
12637       return "DW_OP_lit26";
12638     case DW_OP_lit27:
12639       return "DW_OP_lit27";
12640     case DW_OP_lit28:
12641       return "DW_OP_lit28";
12642     case DW_OP_lit29:
12643       return "DW_OP_lit29";
12644     case DW_OP_lit30:
12645       return "DW_OP_lit30";
12646     case DW_OP_lit31:
12647       return "DW_OP_lit31";
12648     case DW_OP_reg0:
12649       return "DW_OP_reg0";
12650     case DW_OP_reg1:
12651       return "DW_OP_reg1";
12652     case DW_OP_reg2:
12653       return "DW_OP_reg2";
12654     case DW_OP_reg3:
12655       return "DW_OP_reg3";
12656     case DW_OP_reg4:
12657       return "DW_OP_reg4";
12658     case DW_OP_reg5:
12659       return "DW_OP_reg5";
12660     case DW_OP_reg6:
12661       return "DW_OP_reg6";
12662     case DW_OP_reg7:
12663       return "DW_OP_reg7";
12664     case DW_OP_reg8:
12665       return "DW_OP_reg8";
12666     case DW_OP_reg9:
12667       return "DW_OP_reg9";
12668     case DW_OP_reg10:
12669       return "DW_OP_reg10";
12670     case DW_OP_reg11:
12671       return "DW_OP_reg11";
12672     case DW_OP_reg12:
12673       return "DW_OP_reg12";
12674     case DW_OP_reg13:
12675       return "DW_OP_reg13";
12676     case DW_OP_reg14:
12677       return "DW_OP_reg14";
12678     case DW_OP_reg15:
12679       return "DW_OP_reg15";
12680     case DW_OP_reg16:
12681       return "DW_OP_reg16";
12682     case DW_OP_reg17:
12683       return "DW_OP_reg17";
12684     case DW_OP_reg18:
12685       return "DW_OP_reg18";
12686     case DW_OP_reg19:
12687       return "DW_OP_reg19";
12688     case DW_OP_reg20:
12689       return "DW_OP_reg20";
12690     case DW_OP_reg21:
12691       return "DW_OP_reg21";
12692     case DW_OP_reg22:
12693       return "DW_OP_reg22";
12694     case DW_OP_reg23:
12695       return "DW_OP_reg23";
12696     case DW_OP_reg24:
12697       return "DW_OP_reg24";
12698     case DW_OP_reg25:
12699       return "DW_OP_reg25";
12700     case DW_OP_reg26:
12701       return "DW_OP_reg26";
12702     case DW_OP_reg27:
12703       return "DW_OP_reg27";
12704     case DW_OP_reg28:
12705       return "DW_OP_reg28";
12706     case DW_OP_reg29:
12707       return "DW_OP_reg29";
12708     case DW_OP_reg30:
12709       return "DW_OP_reg30";
12710     case DW_OP_reg31:
12711       return "DW_OP_reg31";
12712     case DW_OP_breg0:
12713       return "DW_OP_breg0";
12714     case DW_OP_breg1:
12715       return "DW_OP_breg1";
12716     case DW_OP_breg2:
12717       return "DW_OP_breg2";
12718     case DW_OP_breg3:
12719       return "DW_OP_breg3";
12720     case DW_OP_breg4:
12721       return "DW_OP_breg4";
12722     case DW_OP_breg5:
12723       return "DW_OP_breg5";
12724     case DW_OP_breg6:
12725       return "DW_OP_breg6";
12726     case DW_OP_breg7:
12727       return "DW_OP_breg7";
12728     case DW_OP_breg8:
12729       return "DW_OP_breg8";
12730     case DW_OP_breg9:
12731       return "DW_OP_breg9";
12732     case DW_OP_breg10:
12733       return "DW_OP_breg10";
12734     case DW_OP_breg11:
12735       return "DW_OP_breg11";
12736     case DW_OP_breg12:
12737       return "DW_OP_breg12";
12738     case DW_OP_breg13:
12739       return "DW_OP_breg13";
12740     case DW_OP_breg14:
12741       return "DW_OP_breg14";
12742     case DW_OP_breg15:
12743       return "DW_OP_breg15";
12744     case DW_OP_breg16:
12745       return "DW_OP_breg16";
12746     case DW_OP_breg17:
12747       return "DW_OP_breg17";
12748     case DW_OP_breg18:
12749       return "DW_OP_breg18";
12750     case DW_OP_breg19:
12751       return "DW_OP_breg19";
12752     case DW_OP_breg20:
12753       return "DW_OP_breg20";
12754     case DW_OP_breg21:
12755       return "DW_OP_breg21";
12756     case DW_OP_breg22:
12757       return "DW_OP_breg22";
12758     case DW_OP_breg23:
12759       return "DW_OP_breg23";
12760     case DW_OP_breg24:
12761       return "DW_OP_breg24";
12762     case DW_OP_breg25:
12763       return "DW_OP_breg25";
12764     case DW_OP_breg26:
12765       return "DW_OP_breg26";
12766     case DW_OP_breg27:
12767       return "DW_OP_breg27";
12768     case DW_OP_breg28:
12769       return "DW_OP_breg28";
12770     case DW_OP_breg29:
12771       return "DW_OP_breg29";
12772     case DW_OP_breg30:
12773       return "DW_OP_breg30";
12774     case DW_OP_breg31:
12775       return "DW_OP_breg31";
12776     case DW_OP_regx:
12777       return "DW_OP_regx";
12778     case DW_OP_fbreg:
12779       return "DW_OP_fbreg";
12780     case DW_OP_bregx:
12781       return "DW_OP_bregx";
12782     case DW_OP_piece:
12783       return "DW_OP_piece";
12784     case DW_OP_deref_size:
12785       return "DW_OP_deref_size";
12786     case DW_OP_xderef_size:
12787       return "DW_OP_xderef_size";
12788     case DW_OP_nop:
12789       return "DW_OP_nop";
12790     /* DWARF 3 extensions.  */
12791     case DW_OP_push_object_address:
12792       return "DW_OP_push_object_address";
12793     case DW_OP_call2:
12794       return "DW_OP_call2";
12795     case DW_OP_call4:
12796       return "DW_OP_call4";
12797     case DW_OP_call_ref:
12798       return "DW_OP_call_ref";
12799     case DW_OP_form_tls_address:
12800       return "DW_OP_form_tls_address";
12801     case DW_OP_call_frame_cfa:
12802       return "DW_OP_call_frame_cfa";
12803     case DW_OP_bit_piece:
12804       return "DW_OP_bit_piece";
12805     /* DWARF 4 extensions.  */
12806     case DW_OP_implicit_value:
12807       return "DW_OP_implicit_value";
12808     case DW_OP_stack_value:
12809       return "DW_OP_stack_value";
12810     /* GNU extensions.  */
12811     case DW_OP_GNU_push_tls_address:
12812       return "DW_OP_GNU_push_tls_address";
12813     case DW_OP_GNU_uninit:
12814       return "DW_OP_GNU_uninit";
12815     case DW_OP_GNU_implicit_pointer:
12816       return "DW_OP_GNU_implicit_pointer";
12817     default:
12818       return NULL;
12819     }
12820 }
12821
12822 static char *
12823 dwarf_bool_name (unsigned mybool)
12824 {
12825   if (mybool)
12826     return "TRUE";
12827   else
12828     return "FALSE";
12829 }
12830
12831 /* Convert a DWARF type code into its string name.  */
12832
12833 static char *
12834 dwarf_type_encoding_name (unsigned enc)
12835 {
12836   switch (enc)
12837     {
12838     case DW_ATE_void:
12839       return "DW_ATE_void";
12840     case DW_ATE_address:
12841       return "DW_ATE_address";
12842     case DW_ATE_boolean:
12843       return "DW_ATE_boolean";
12844     case DW_ATE_complex_float:
12845       return "DW_ATE_complex_float";
12846     case DW_ATE_float:
12847       return "DW_ATE_float";
12848     case DW_ATE_signed:
12849       return "DW_ATE_signed";
12850     case DW_ATE_signed_char:
12851       return "DW_ATE_signed_char";
12852     case DW_ATE_unsigned:
12853       return "DW_ATE_unsigned";
12854     case DW_ATE_unsigned_char:
12855       return "DW_ATE_unsigned_char";
12856     /* DWARF 3.  */
12857     case DW_ATE_imaginary_float:
12858       return "DW_ATE_imaginary_float";
12859     case DW_ATE_packed_decimal:
12860       return "DW_ATE_packed_decimal";
12861     case DW_ATE_numeric_string:
12862       return "DW_ATE_numeric_string";
12863     case DW_ATE_edited:
12864       return "DW_ATE_edited";
12865     case DW_ATE_signed_fixed:
12866       return "DW_ATE_signed_fixed";
12867     case DW_ATE_unsigned_fixed:
12868       return "DW_ATE_unsigned_fixed";
12869     case DW_ATE_decimal_float:
12870       return "DW_ATE_decimal_float";
12871     /* DWARF 4.  */
12872     case DW_ATE_UTF:
12873       return "DW_ATE_UTF";
12874     /* HP extensions.  */
12875     case DW_ATE_HP_float80:
12876       return "DW_ATE_HP_float80";
12877     case DW_ATE_HP_complex_float80:
12878       return "DW_ATE_HP_complex_float80";
12879     case DW_ATE_HP_float128:
12880       return "DW_ATE_HP_float128";
12881     case DW_ATE_HP_complex_float128:
12882       return "DW_ATE_HP_complex_float128";
12883     case DW_ATE_HP_floathpintel:
12884       return "DW_ATE_HP_floathpintel";
12885     case DW_ATE_HP_imaginary_float80:
12886       return "DW_ATE_HP_imaginary_float80";
12887     case DW_ATE_HP_imaginary_float128:
12888       return "DW_ATE_HP_imaginary_float128";
12889     default:
12890       return "DW_ATE_<unknown>";
12891     }
12892 }
12893
12894 /* Convert a DWARF call frame info operation to its string name.  */
12895
12896 #if 0
12897 static char *
12898 dwarf_cfi_name (unsigned cfi_opc)
12899 {
12900   switch (cfi_opc)
12901     {
12902     case DW_CFA_advance_loc:
12903       return "DW_CFA_advance_loc";
12904     case DW_CFA_offset:
12905       return "DW_CFA_offset";
12906     case DW_CFA_restore:
12907       return "DW_CFA_restore";
12908     case DW_CFA_nop:
12909       return "DW_CFA_nop";
12910     case DW_CFA_set_loc:
12911       return "DW_CFA_set_loc";
12912     case DW_CFA_advance_loc1:
12913       return "DW_CFA_advance_loc1";
12914     case DW_CFA_advance_loc2:
12915       return "DW_CFA_advance_loc2";
12916     case DW_CFA_advance_loc4:
12917       return "DW_CFA_advance_loc4";
12918     case DW_CFA_offset_extended:
12919       return "DW_CFA_offset_extended";
12920     case DW_CFA_restore_extended:
12921       return "DW_CFA_restore_extended";
12922     case DW_CFA_undefined:
12923       return "DW_CFA_undefined";
12924     case DW_CFA_same_value:
12925       return "DW_CFA_same_value";
12926     case DW_CFA_register:
12927       return "DW_CFA_register";
12928     case DW_CFA_remember_state:
12929       return "DW_CFA_remember_state";
12930     case DW_CFA_restore_state:
12931       return "DW_CFA_restore_state";
12932     case DW_CFA_def_cfa:
12933       return "DW_CFA_def_cfa";
12934     case DW_CFA_def_cfa_register:
12935       return "DW_CFA_def_cfa_register";
12936     case DW_CFA_def_cfa_offset:
12937       return "DW_CFA_def_cfa_offset";
12938     /* DWARF 3.  */
12939     case DW_CFA_def_cfa_expression:
12940       return "DW_CFA_def_cfa_expression";
12941     case DW_CFA_expression:
12942       return "DW_CFA_expression";
12943     case DW_CFA_offset_extended_sf:
12944       return "DW_CFA_offset_extended_sf";
12945     case DW_CFA_def_cfa_sf:
12946       return "DW_CFA_def_cfa_sf";
12947     case DW_CFA_def_cfa_offset_sf:
12948       return "DW_CFA_def_cfa_offset_sf";
12949     case DW_CFA_val_offset:
12950       return "DW_CFA_val_offset";
12951     case DW_CFA_val_offset_sf:
12952       return "DW_CFA_val_offset_sf";
12953     case DW_CFA_val_expression:
12954       return "DW_CFA_val_expression";
12955     /* SGI/MIPS specific.  */
12956     case DW_CFA_MIPS_advance_loc8:
12957       return "DW_CFA_MIPS_advance_loc8";
12958     /* GNU extensions.  */
12959     case DW_CFA_GNU_window_save:
12960       return "DW_CFA_GNU_window_save";
12961     case DW_CFA_GNU_args_size:
12962       return "DW_CFA_GNU_args_size";
12963     case DW_CFA_GNU_negative_offset_extended:
12964       return "DW_CFA_GNU_negative_offset_extended";
12965     default:
12966       return "DW_CFA_<unknown>";
12967     }
12968 }
12969 #endif
12970
12971 static void
12972 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12973 {
12974   unsigned int i;
12975
12976   print_spaces (indent, f);
12977   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12978            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12979
12980   if (die->parent != NULL)
12981     {
12982       print_spaces (indent, f);
12983       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12984                           die->parent->offset);
12985     }
12986
12987   print_spaces (indent, f);
12988   fprintf_unfiltered (f, "  has children: %s\n",
12989            dwarf_bool_name (die->child != NULL));
12990
12991   print_spaces (indent, f);
12992   fprintf_unfiltered (f, "  attributes:\n");
12993
12994   for (i = 0; i < die->num_attrs; ++i)
12995     {
12996       print_spaces (indent, f);
12997       fprintf_unfiltered (f, "    %s (%s) ",
12998                dwarf_attr_name (die->attrs[i].name),
12999                dwarf_form_name (die->attrs[i].form));
13000
13001       switch (die->attrs[i].form)
13002         {
13003         case DW_FORM_ref_addr:
13004         case DW_FORM_addr:
13005           fprintf_unfiltered (f, "address: ");
13006           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13007           break;
13008         case DW_FORM_block2:
13009         case DW_FORM_block4:
13010         case DW_FORM_block:
13011         case DW_FORM_block1:
13012           fprintf_unfiltered (f, "block: size %d",
13013                               DW_BLOCK (&die->attrs[i])->size);
13014           break;
13015         case DW_FORM_exprloc:
13016           fprintf_unfiltered (f, "expression: size %u",
13017                               DW_BLOCK (&die->attrs[i])->size);
13018           break;
13019         case DW_FORM_ref1:
13020         case DW_FORM_ref2:
13021         case DW_FORM_ref4:
13022           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13023                               (long) (DW_ADDR (&die->attrs[i])));
13024           break;
13025         case DW_FORM_data1:
13026         case DW_FORM_data2:
13027         case DW_FORM_data4:
13028         case DW_FORM_data8:
13029         case DW_FORM_udata:
13030         case DW_FORM_sdata:
13031           fprintf_unfiltered (f, "constant: %s",
13032                               pulongest (DW_UNSND (&die->attrs[i])));
13033           break;
13034         case DW_FORM_sec_offset:
13035           fprintf_unfiltered (f, "section offset: %s",
13036                               pulongest (DW_UNSND (&die->attrs[i])));
13037           break;
13038         case DW_FORM_sig8:
13039           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13040             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13041                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13042           else
13043             fprintf_unfiltered (f, "signatured type, offset: unknown");
13044           break;
13045         case DW_FORM_string:
13046         case DW_FORM_strp:
13047           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13048                    DW_STRING (&die->attrs[i])
13049                    ? DW_STRING (&die->attrs[i]) : "",
13050                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13051           break;
13052         case DW_FORM_flag:
13053           if (DW_UNSND (&die->attrs[i]))
13054             fprintf_unfiltered (f, "flag: TRUE");
13055           else
13056             fprintf_unfiltered (f, "flag: FALSE");
13057           break;
13058         case DW_FORM_flag_present:
13059           fprintf_unfiltered (f, "flag: TRUE");
13060           break;
13061         case DW_FORM_indirect:
13062           /* The reader will have reduced the indirect form to
13063              the "base form" so this form should not occur.  */
13064           fprintf_unfiltered (f, 
13065                               "unexpected attribute form: DW_FORM_indirect");
13066           break;
13067         default:
13068           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13069                    die->attrs[i].form);
13070           break;
13071         }
13072       fprintf_unfiltered (f, "\n");
13073     }
13074 }
13075
13076 static void
13077 dump_die_for_error (struct die_info *die)
13078 {
13079   dump_die_shallow (gdb_stderr, 0, die);
13080 }
13081
13082 static void
13083 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13084 {
13085   int indent = level * 4;
13086
13087   gdb_assert (die != NULL);
13088
13089   if (level >= max_level)
13090     return;
13091
13092   dump_die_shallow (f, indent, die);
13093
13094   if (die->child != NULL)
13095     {
13096       print_spaces (indent, f);
13097       fprintf_unfiltered (f, "  Children:");
13098       if (level + 1 < max_level)
13099         {
13100           fprintf_unfiltered (f, "\n");
13101           dump_die_1 (f, level + 1, max_level, die->child);
13102         }
13103       else
13104         {
13105           fprintf_unfiltered (f,
13106                               " [not printed, max nesting level reached]\n");
13107         }
13108     }
13109
13110   if (die->sibling != NULL && level > 0)
13111     {
13112       dump_die_1 (f, level, max_level, die->sibling);
13113     }
13114 }
13115
13116 /* This is called from the pdie macro in gdbinit.in.
13117    It's not static so gcc will keep a copy callable from gdb.  */
13118
13119 void
13120 dump_die (struct die_info *die, int max_level)
13121 {
13122   dump_die_1 (gdb_stdlog, 0, max_level, die);
13123 }
13124
13125 static void
13126 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13127 {
13128   void **slot;
13129
13130   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13131
13132   *slot = die;
13133 }
13134
13135 static int
13136 is_ref_attr (struct attribute *attr)
13137 {
13138   switch (attr->form)
13139     {
13140     case DW_FORM_ref_addr:
13141     case DW_FORM_ref1:
13142     case DW_FORM_ref2:
13143     case DW_FORM_ref4:
13144     case DW_FORM_ref8:
13145     case DW_FORM_ref_udata:
13146       return 1;
13147     default:
13148       return 0;
13149     }
13150 }
13151
13152 static unsigned int
13153 dwarf2_get_ref_die_offset (struct attribute *attr)
13154 {
13155   if (is_ref_attr (attr))
13156     return DW_ADDR (attr);
13157
13158   complaint (&symfile_complaints,
13159              _("unsupported die ref attribute form: '%s'"),
13160              dwarf_form_name (attr->form));
13161   return 0;
13162 }
13163
13164 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13165  * the value held by the attribute is not constant.  */
13166
13167 static LONGEST
13168 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13169 {
13170   if (attr->form == DW_FORM_sdata)
13171     return DW_SND (attr);
13172   else if (attr->form == DW_FORM_udata
13173            || attr->form == DW_FORM_data1
13174            || attr->form == DW_FORM_data2
13175            || attr->form == DW_FORM_data4
13176            || attr->form == DW_FORM_data8)
13177     return DW_UNSND (attr);
13178   else
13179     {
13180       complaint (&symfile_complaints,
13181                  _("Attribute value is not a constant (%s)"),
13182                  dwarf_form_name (attr->form));
13183       return default_value;
13184     }
13185 }
13186
13187 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13188    unit and add it to our queue.
13189    The result is non-zero if PER_CU was queued, otherwise the result is zero
13190    meaning either PER_CU is already queued or it is already loaded.  */
13191
13192 static int
13193 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13194                        struct dwarf2_per_cu_data *per_cu)
13195 {
13196   /* We may arrive here during partial symbol reading, if we need full
13197      DIEs to process an unusual case (e.g. template arguments).  Do
13198      not queue PER_CU, just tell our caller to load its DIEs.  */
13199   if (dwarf2_per_objfile->reading_partial_symbols)
13200     {
13201       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13202         return 1;
13203       return 0;
13204     }
13205
13206   /* Mark the dependence relation so that we don't flush PER_CU
13207      too early.  */
13208   dwarf2_add_dependence (this_cu, per_cu);
13209
13210   /* If it's already on the queue, we have nothing to do.  */
13211   if (per_cu->queued)
13212     return 0;
13213
13214   /* If the compilation unit is already loaded, just mark it as
13215      used.  */
13216   if (per_cu->cu != NULL)
13217     {
13218       per_cu->cu->last_used = 0;
13219       return 0;
13220     }
13221
13222   /* Add it to the queue.  */
13223   queue_comp_unit (per_cu, this_cu->objfile);
13224
13225   return 1;
13226 }
13227
13228 /* Follow reference or signature attribute ATTR of SRC_DIE.
13229    On entry *REF_CU is the CU of SRC_DIE.
13230    On exit *REF_CU is the CU of the result.  */
13231
13232 static struct die_info *
13233 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13234                        struct dwarf2_cu **ref_cu)
13235 {
13236   struct die_info *die;
13237
13238   if (is_ref_attr (attr))
13239     die = follow_die_ref (src_die, attr, ref_cu);
13240   else if (attr->form == DW_FORM_sig8)
13241     die = follow_die_sig (src_die, attr, ref_cu);
13242   else
13243     {
13244       dump_die_for_error (src_die);
13245       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13246              (*ref_cu)->objfile->name);
13247     }
13248
13249   return die;
13250 }
13251
13252 /* Follow reference OFFSET.
13253    On entry *REF_CU is the CU of the source die referencing OFFSET.
13254    On exit *REF_CU is the CU of the result.
13255    Returns NULL if OFFSET is invalid.  */
13256
13257 static struct die_info *
13258 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13259 {
13260   struct die_info temp_die;
13261   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13262
13263   gdb_assert (cu->per_cu != NULL);
13264
13265   target_cu = cu;
13266
13267   if (cu->per_cu->from_debug_types)
13268     {
13269       /* .debug_types CUs cannot reference anything outside their CU.
13270          If they need to, they have to reference a signatured type via
13271          DW_FORM_sig8.  */
13272       if (! offset_in_cu_p (&cu->header, offset))
13273         return NULL;
13274     }
13275   else if (! offset_in_cu_p (&cu->header, offset))
13276     {
13277       struct dwarf2_per_cu_data *per_cu;
13278
13279       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13280
13281       /* If necessary, add it to the queue and load its DIEs.  */
13282       if (maybe_queue_comp_unit (cu, per_cu))
13283         load_full_comp_unit (per_cu, cu->objfile);
13284
13285       target_cu = per_cu->cu;
13286     }
13287   else if (cu->dies == NULL)
13288     {
13289       /* We're loading full DIEs during partial symbol reading.  */
13290       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13291       load_full_comp_unit (cu->per_cu, cu->objfile);
13292     }
13293
13294   *ref_cu = target_cu;
13295   temp_die.offset = offset;
13296   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13297 }
13298
13299 /* Follow reference attribute ATTR of SRC_DIE.
13300    On entry *REF_CU is the CU of SRC_DIE.
13301    On exit *REF_CU is the CU of the result.  */
13302
13303 static struct die_info *
13304 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13305                 struct dwarf2_cu **ref_cu)
13306 {
13307   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13308   struct dwarf2_cu *cu = *ref_cu;
13309   struct die_info *die;
13310
13311   die = follow_die_offset (offset, ref_cu);
13312   if (!die)
13313     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13314            "at 0x%x [in module %s]"),
13315            offset, src_die->offset, cu->objfile->name);
13316
13317   return die;
13318 }
13319
13320 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13321    value is intended for DW_OP_call*.  */
13322
13323 struct dwarf2_locexpr_baton
13324 dwarf2_fetch_die_location_block (unsigned int offset,
13325                                  struct dwarf2_per_cu_data *per_cu,
13326                                  CORE_ADDR (*get_frame_pc) (void *baton),
13327                                  void *baton)
13328 {
13329   struct dwarf2_cu *cu = per_cu->cu;
13330   struct die_info *die;
13331   struct attribute *attr;
13332   struct dwarf2_locexpr_baton retval;
13333
13334   dw2_setup (per_cu->objfile);
13335
13336   die = follow_die_offset (offset, &cu);
13337   if (!die)
13338     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13339            offset, per_cu->cu->objfile->name);
13340
13341   attr = dwarf2_attr (die, DW_AT_location, cu);
13342   if (!attr)
13343     {
13344       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13345
13346       retval.data = NULL;
13347       retval.size = 0;
13348     }
13349   else if (attr_form_is_section_offset (attr))
13350     {
13351       struct dwarf2_loclist_baton loclist_baton;
13352       CORE_ADDR pc = (*get_frame_pc) (baton);
13353       size_t size;
13354
13355       fill_in_loclist_baton (cu, &loclist_baton, attr);
13356
13357       retval.data = dwarf2_find_location_expression (&loclist_baton,
13358                                                      &size, pc);
13359       retval.size = size;
13360     }
13361   else
13362     {
13363       if (!attr_form_is_block (attr))
13364         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13365                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13366                offset, per_cu->cu->objfile->name);
13367
13368       retval.data = DW_BLOCK (attr)->data;
13369       retval.size = DW_BLOCK (attr)->size;
13370     }
13371   retval.per_cu = cu->per_cu;
13372   return retval;
13373 }
13374
13375 /* Follow the signature attribute ATTR in SRC_DIE.
13376    On entry *REF_CU is the CU of SRC_DIE.
13377    On exit *REF_CU is the CU of the result.  */
13378
13379 static struct die_info *
13380 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13381                 struct dwarf2_cu **ref_cu)
13382 {
13383   struct objfile *objfile = (*ref_cu)->objfile;
13384   struct die_info temp_die;
13385   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13386   struct dwarf2_cu *sig_cu;
13387   struct die_info *die;
13388
13389   /* sig_type will be NULL if the signatured type is missing from
13390      the debug info.  */
13391   if (sig_type == NULL)
13392     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13393              "at 0x%x [in module %s]"),
13394            src_die->offset, objfile->name);
13395
13396   /* If necessary, add it to the queue and load its DIEs.  */
13397
13398   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13399     read_signatured_type (objfile, sig_type);
13400
13401   gdb_assert (sig_type->per_cu.cu != NULL);
13402
13403   sig_cu = sig_type->per_cu.cu;
13404   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13405   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13406   if (die)
13407     {
13408       *ref_cu = sig_cu;
13409       return die;
13410     }
13411
13412   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13413          "from DIE at 0x%x [in module %s]"),
13414          sig_type->type_offset, src_die->offset, objfile->name);
13415 }
13416
13417 /* Given an offset of a signatured type, return its signatured_type.  */
13418
13419 static struct signatured_type *
13420 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13421 {
13422   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13423   unsigned int length, initial_length_size;
13424   unsigned int sig_offset;
13425   struct signatured_type find_entry, *type_sig;
13426
13427   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13428   sig_offset = (initial_length_size
13429                 + 2 /*version*/
13430                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13431                 + 1 /*address_size*/);
13432   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13433   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13434
13435   /* This is only used to lookup previously recorded types.
13436      If we didn't find it, it's our bug.  */
13437   gdb_assert (type_sig != NULL);
13438   gdb_assert (offset == type_sig->offset);
13439
13440   return type_sig;
13441 }
13442
13443 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13444
13445 static void
13446 read_signatured_type_at_offset (struct objfile *objfile,
13447                                 unsigned int offset)
13448 {
13449   struct signatured_type *type_sig;
13450
13451   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13452
13453   /* We have the section offset, but we need the signature to do the
13454      hash table lookup.  */
13455   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13456
13457   gdb_assert (type_sig->per_cu.cu == NULL);
13458
13459   read_signatured_type (objfile, type_sig);
13460
13461   gdb_assert (type_sig->per_cu.cu != NULL);
13462 }
13463
13464 /* Read in a signatured type and build its CU and DIEs.  */
13465
13466 static void
13467 read_signatured_type (struct objfile *objfile,
13468                       struct signatured_type *type_sig)
13469 {
13470   gdb_byte *types_ptr;
13471   struct die_reader_specs reader_specs;
13472   struct dwarf2_cu *cu;
13473   ULONGEST signature;
13474   struct cleanup *back_to, *free_cu_cleanup;
13475
13476   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13477   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13478
13479   gdb_assert (type_sig->per_cu.cu == NULL);
13480
13481   cu = xmalloc (sizeof (*cu));
13482   init_one_comp_unit (cu, objfile);
13483
13484   type_sig->per_cu.cu = cu;
13485   cu->per_cu = &type_sig->per_cu;
13486
13487   /* If an error occurs while loading, release our storage.  */
13488   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13489
13490   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13491                                         types_ptr, objfile->obfd);
13492   gdb_assert (signature == type_sig->signature);
13493
13494   cu->die_hash
13495     = htab_create_alloc_ex (cu->header.length / 12,
13496                             die_hash,
13497                             die_eq,
13498                             NULL,
13499                             &cu->comp_unit_obstack,
13500                             hashtab_obstack_allocate,
13501                             dummy_obstack_deallocate);
13502
13503   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13504   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13505
13506   init_cu_die_reader (&reader_specs, cu);
13507
13508   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13509                                     NULL /*parent*/);
13510
13511   /* We try not to read any attributes in this function, because not
13512      all objfiles needed for references have been loaded yet, and symbol
13513      table processing isn't initialized.  But we have to set the CU language,
13514      or we won't be able to build types correctly.  */
13515   prepare_one_comp_unit (cu, cu->dies);
13516
13517   do_cleanups (back_to);
13518
13519   /* We've successfully allocated this compilation unit.  Let our caller
13520      clean it up when finished with it.  */
13521   discard_cleanups (free_cu_cleanup);
13522
13523   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13524   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13525 }
13526
13527 /* Decode simple location descriptions.
13528    Given a pointer to a dwarf block that defines a location, compute
13529    the location and return the value.
13530
13531    NOTE drow/2003-11-18: This function is called in two situations
13532    now: for the address of static or global variables (partial symbols
13533    only) and for offsets into structures which are expected to be
13534    (more or less) constant.  The partial symbol case should go away,
13535    and only the constant case should remain.  That will let this
13536    function complain more accurately.  A few special modes are allowed
13537    without complaint for global variables (for instance, global
13538    register values and thread-local values).
13539
13540    A location description containing no operations indicates that the
13541    object is optimized out.  The return value is 0 for that case.
13542    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13543    callers will only want a very basic result and this can become a
13544    complaint.
13545
13546    Note that stack[0] is unused except as a default error return.  */
13547
13548 static CORE_ADDR
13549 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13550 {
13551   struct objfile *objfile = cu->objfile;
13552   int i;
13553   int size = blk->size;
13554   gdb_byte *data = blk->data;
13555   CORE_ADDR stack[64];
13556   int stacki;
13557   unsigned int bytes_read, unsnd;
13558   gdb_byte op;
13559
13560   i = 0;
13561   stacki = 0;
13562   stack[stacki] = 0;
13563   stack[++stacki] = 0;
13564
13565   while (i < size)
13566     {
13567       op = data[i++];
13568       switch (op)
13569         {
13570         case DW_OP_lit0:
13571         case DW_OP_lit1:
13572         case DW_OP_lit2:
13573         case DW_OP_lit3:
13574         case DW_OP_lit4:
13575         case DW_OP_lit5:
13576         case DW_OP_lit6:
13577         case DW_OP_lit7:
13578         case DW_OP_lit8:
13579         case DW_OP_lit9:
13580         case DW_OP_lit10:
13581         case DW_OP_lit11:
13582         case DW_OP_lit12:
13583         case DW_OP_lit13:
13584         case DW_OP_lit14:
13585         case DW_OP_lit15:
13586         case DW_OP_lit16:
13587         case DW_OP_lit17:
13588         case DW_OP_lit18:
13589         case DW_OP_lit19:
13590         case DW_OP_lit20:
13591         case DW_OP_lit21:
13592         case DW_OP_lit22:
13593         case DW_OP_lit23:
13594         case DW_OP_lit24:
13595         case DW_OP_lit25:
13596         case DW_OP_lit26:
13597         case DW_OP_lit27:
13598         case DW_OP_lit28:
13599         case DW_OP_lit29:
13600         case DW_OP_lit30:
13601         case DW_OP_lit31:
13602           stack[++stacki] = op - DW_OP_lit0;
13603           break;
13604
13605         case DW_OP_reg0:
13606         case DW_OP_reg1:
13607         case DW_OP_reg2:
13608         case DW_OP_reg3:
13609         case DW_OP_reg4:
13610         case DW_OP_reg5:
13611         case DW_OP_reg6:
13612         case DW_OP_reg7:
13613         case DW_OP_reg8:
13614         case DW_OP_reg9:
13615         case DW_OP_reg10:
13616         case DW_OP_reg11:
13617         case DW_OP_reg12:
13618         case DW_OP_reg13:
13619         case DW_OP_reg14:
13620         case DW_OP_reg15:
13621         case DW_OP_reg16:
13622         case DW_OP_reg17:
13623         case DW_OP_reg18:
13624         case DW_OP_reg19:
13625         case DW_OP_reg20:
13626         case DW_OP_reg21:
13627         case DW_OP_reg22:
13628         case DW_OP_reg23:
13629         case DW_OP_reg24:
13630         case DW_OP_reg25:
13631         case DW_OP_reg26:
13632         case DW_OP_reg27:
13633         case DW_OP_reg28:
13634         case DW_OP_reg29:
13635         case DW_OP_reg30:
13636         case DW_OP_reg31:
13637           stack[++stacki] = op - DW_OP_reg0;
13638           if (i < size)
13639             dwarf2_complex_location_expr_complaint ();
13640           break;
13641
13642         case DW_OP_regx:
13643           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13644           i += bytes_read;
13645           stack[++stacki] = unsnd;
13646           if (i < size)
13647             dwarf2_complex_location_expr_complaint ();
13648           break;
13649
13650         case DW_OP_addr:
13651           stack[++stacki] = read_address (objfile->obfd, &data[i],
13652                                           cu, &bytes_read);
13653           i += bytes_read;
13654           break;
13655
13656         case DW_OP_const1u:
13657           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13658           i += 1;
13659           break;
13660
13661         case DW_OP_const1s:
13662           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13663           i += 1;
13664           break;
13665
13666         case DW_OP_const2u:
13667           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13668           i += 2;
13669           break;
13670
13671         case DW_OP_const2s:
13672           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13673           i += 2;
13674           break;
13675
13676         case DW_OP_const4u:
13677           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13678           i += 4;
13679           break;
13680
13681         case DW_OP_const4s:
13682           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13683           i += 4;
13684           break;
13685
13686         case DW_OP_constu:
13687           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13688                                                   &bytes_read);
13689           i += bytes_read;
13690           break;
13691
13692         case DW_OP_consts:
13693           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13694           i += bytes_read;
13695           break;
13696
13697         case DW_OP_dup:
13698           stack[stacki + 1] = stack[stacki];
13699           stacki++;
13700           break;
13701
13702         case DW_OP_plus:
13703           stack[stacki - 1] += stack[stacki];
13704           stacki--;
13705           break;
13706
13707         case DW_OP_plus_uconst:
13708           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13709                                                  &bytes_read);
13710           i += bytes_read;
13711           break;
13712
13713         case DW_OP_minus:
13714           stack[stacki - 1] -= stack[stacki];
13715           stacki--;
13716           break;
13717
13718         case DW_OP_deref:
13719           /* If we're not the last op, then we definitely can't encode
13720              this using GDB's address_class enum.  This is valid for partial
13721              global symbols, although the variable's address will be bogus
13722              in the psymtab.  */
13723           if (i < size)
13724             dwarf2_complex_location_expr_complaint ();
13725           break;
13726
13727         case DW_OP_GNU_push_tls_address:
13728           /* The top of the stack has the offset from the beginning
13729              of the thread control block at which the variable is located.  */
13730           /* Nothing should follow this operator, so the top of stack would
13731              be returned.  */
13732           /* This is valid for partial global symbols, but the variable's
13733              address will be bogus in the psymtab.  */
13734           if (i < size)
13735             dwarf2_complex_location_expr_complaint ();
13736           break;
13737
13738         case DW_OP_GNU_uninit:
13739           break;
13740
13741         default:
13742           {
13743             const char *name = dwarf_stack_op_name (op);
13744
13745             if (name)
13746               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13747                          name);
13748             else
13749               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13750                          op);
13751           }
13752
13753           return (stack[stacki]);
13754         }
13755
13756       /* Enforce maximum stack depth of SIZE-1 to avoid writing
13757          outside of the allocated space.  Also enforce minimum>0.  */
13758       if (stacki >= ARRAY_SIZE (stack) - 1)
13759         {
13760           complaint (&symfile_complaints,
13761                      _("location description stack overflow"));
13762           return 0;
13763         }
13764
13765       if (stacki <= 0)
13766         {
13767           complaint (&symfile_complaints,
13768                      _("location description stack underflow"));
13769           return 0;
13770         }
13771     }
13772   return (stack[stacki]);
13773 }
13774
13775 /* memory allocation interface */
13776
13777 static struct dwarf_block *
13778 dwarf_alloc_block (struct dwarf2_cu *cu)
13779 {
13780   struct dwarf_block *blk;
13781
13782   blk = (struct dwarf_block *)
13783     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13784   return (blk);
13785 }
13786
13787 static struct abbrev_info *
13788 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13789 {
13790   struct abbrev_info *abbrev;
13791
13792   abbrev = (struct abbrev_info *)
13793     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13794   memset (abbrev, 0, sizeof (struct abbrev_info));
13795   return (abbrev);
13796 }
13797
13798 static struct die_info *
13799 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13800 {
13801   struct die_info *die;
13802   size_t size = sizeof (struct die_info);
13803
13804   if (num_attrs > 1)
13805     size += (num_attrs - 1) * sizeof (struct attribute);
13806
13807   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13808   memset (die, 0, sizeof (struct die_info));
13809   return (die);
13810 }
13811
13812 \f
13813 /* Macro support.  */
13814
13815 /* Return the full name of file number I in *LH's file name table.
13816    Use COMP_DIR as the name of the current directory of the
13817    compilation.  The result is allocated using xmalloc; the caller is
13818    responsible for freeing it.  */
13819 static char *
13820 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13821 {
13822   /* Is the file number a valid index into the line header's file name
13823      table?  Remember that file numbers start with one, not zero.  */
13824   if (1 <= file && file <= lh->num_file_names)
13825     {
13826       struct file_entry *fe = &lh->file_names[file - 1];
13827
13828       if (IS_ABSOLUTE_PATH (fe->name))
13829         return xstrdup (fe->name);
13830       else
13831         {
13832           const char *dir;
13833           int dir_len;
13834           char *full_name;
13835
13836           if (fe->dir_index)
13837             dir = lh->include_dirs[fe->dir_index - 1];
13838           else
13839             dir = comp_dir;
13840
13841           if (dir)
13842             {
13843               dir_len = strlen (dir);
13844               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13845               strcpy (full_name, dir);
13846               full_name[dir_len] = '/';
13847               strcpy (full_name + dir_len + 1, fe->name);
13848               return full_name;
13849             }
13850           else
13851             return xstrdup (fe->name);
13852         }
13853     }
13854   else
13855     {
13856       /* The compiler produced a bogus file number.  We can at least
13857          record the macro definitions made in the file, even if we
13858          won't be able to find the file by name.  */
13859       char fake_name[80];
13860
13861       sprintf (fake_name, "<bad macro file number %d>", file);
13862
13863       complaint (&symfile_complaints,
13864                  _("bad file number in macro information (%d)"),
13865                  file);
13866
13867       return xstrdup (fake_name);
13868     }
13869 }
13870
13871
13872 static struct macro_source_file *
13873 macro_start_file (int file, int line,
13874                   struct macro_source_file *current_file,
13875                   const char *comp_dir,
13876                   struct line_header *lh, struct objfile *objfile)
13877 {
13878   /* The full name of this source file.  */
13879   char *full_name = file_full_name (file, lh, comp_dir);
13880
13881   /* We don't create a macro table for this compilation unit
13882      at all until we actually get a filename.  */
13883   if (! pending_macros)
13884     pending_macros = new_macro_table (&objfile->objfile_obstack,
13885                                       objfile->macro_cache);
13886
13887   if (! current_file)
13888     /* If we have no current file, then this must be the start_file
13889        directive for the compilation unit's main source file.  */
13890     current_file = macro_set_main (pending_macros, full_name);
13891   else
13892     current_file = macro_include (current_file, line, full_name);
13893
13894   xfree (full_name);
13895
13896   return current_file;
13897 }
13898
13899
13900 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13901    followed by a null byte.  */
13902 static char *
13903 copy_string (const char *buf, int len)
13904 {
13905   char *s = xmalloc (len + 1);
13906
13907   memcpy (s, buf, len);
13908   s[len] = '\0';
13909   return s;
13910 }
13911
13912
13913 static const char *
13914 consume_improper_spaces (const char *p, const char *body)
13915 {
13916   if (*p == ' ')
13917     {
13918       complaint (&symfile_complaints,
13919                  _("macro definition contains spaces "
13920                    "in formal argument list:\n`%s'"),
13921                  body);
13922
13923       while (*p == ' ')
13924         p++;
13925     }
13926
13927   return p;
13928 }
13929
13930
13931 static void
13932 parse_macro_definition (struct macro_source_file *file, int line,
13933                         const char *body)
13934 {
13935   const char *p;
13936
13937   /* The body string takes one of two forms.  For object-like macro
13938      definitions, it should be:
13939
13940         <macro name> " " <definition>
13941
13942      For function-like macro definitions, it should be:
13943
13944         <macro name> "() " <definition>
13945      or
13946         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13947
13948      Spaces may appear only where explicitly indicated, and in the
13949      <definition>.
13950
13951      The Dwarf 2 spec says that an object-like macro's name is always
13952      followed by a space, but versions of GCC around March 2002 omit
13953      the space when the macro's definition is the empty string.
13954
13955      The Dwarf 2 spec says that there should be no spaces between the
13956      formal arguments in a function-like macro's formal argument list,
13957      but versions of GCC around March 2002 include spaces after the
13958      commas.  */
13959
13960
13961   /* Find the extent of the macro name.  The macro name is terminated
13962      by either a space or null character (for an object-like macro) or
13963      an opening paren (for a function-like macro).  */
13964   for (p = body; *p; p++)
13965     if (*p == ' ' || *p == '(')
13966       break;
13967
13968   if (*p == ' ' || *p == '\0')
13969     {
13970       /* It's an object-like macro.  */
13971       int name_len = p - body;
13972       char *name = copy_string (body, name_len);
13973       const char *replacement;
13974
13975       if (*p == ' ')
13976         replacement = body + name_len + 1;
13977       else
13978         {
13979           dwarf2_macro_malformed_definition_complaint (body);
13980           replacement = body + name_len;
13981         }
13982
13983       macro_define_object (file, line, name, replacement);
13984
13985       xfree (name);
13986     }
13987   else if (*p == '(')
13988     {
13989       /* It's a function-like macro.  */
13990       char *name = copy_string (body, p - body);
13991       int argc = 0;
13992       int argv_size = 1;
13993       char **argv = xmalloc (argv_size * sizeof (*argv));
13994
13995       p++;
13996
13997       p = consume_improper_spaces (p, body);
13998
13999       /* Parse the formal argument list.  */
14000       while (*p && *p != ')')
14001         {
14002           /* Find the extent of the current argument name.  */
14003           const char *arg_start = p;
14004
14005           while (*p && *p != ',' && *p != ')' && *p != ' ')
14006             p++;
14007
14008           if (! *p || p == arg_start)
14009             dwarf2_macro_malformed_definition_complaint (body);
14010           else
14011             {
14012               /* Make sure argv has room for the new argument.  */
14013               if (argc >= argv_size)
14014                 {
14015                   argv_size *= 2;
14016                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14017                 }
14018
14019               argv[argc++] = copy_string (arg_start, p - arg_start);
14020             }
14021
14022           p = consume_improper_spaces (p, body);
14023
14024           /* Consume the comma, if present.  */
14025           if (*p == ',')
14026             {
14027               p++;
14028
14029               p = consume_improper_spaces (p, body);
14030             }
14031         }
14032
14033       if (*p == ')')
14034         {
14035           p++;
14036
14037           if (*p == ' ')
14038             /* Perfectly formed definition, no complaints.  */
14039             macro_define_function (file, line, name,
14040                                    argc, (const char **) argv,
14041                                    p + 1);
14042           else if (*p == '\0')
14043             {
14044               /* Complain, but do define it.  */
14045               dwarf2_macro_malformed_definition_complaint (body);
14046               macro_define_function (file, line, name,
14047                                      argc, (const char **) argv,
14048                                      p);
14049             }
14050           else
14051             /* Just complain.  */
14052             dwarf2_macro_malformed_definition_complaint (body);
14053         }
14054       else
14055         /* Just complain.  */
14056         dwarf2_macro_malformed_definition_complaint (body);
14057
14058       xfree (name);
14059       {
14060         int i;
14061
14062         for (i = 0; i < argc; i++)
14063           xfree (argv[i]);
14064       }
14065       xfree (argv);
14066     }
14067   else
14068     dwarf2_macro_malformed_definition_complaint (body);
14069 }
14070
14071
14072 static void
14073 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14074                      char *comp_dir, bfd *abfd,
14075                      struct dwarf2_cu *cu)
14076 {
14077   gdb_byte *mac_ptr, *mac_end;
14078   struct macro_source_file *current_file = 0;
14079   enum dwarf_macinfo_record_type macinfo_type;
14080   int at_commandline;
14081
14082   dwarf2_read_section (dwarf2_per_objfile->objfile,
14083                        &dwarf2_per_objfile->macinfo);
14084   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14085     {
14086       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14087       return;
14088     }
14089
14090   /* First pass: Find the name of the base filename.
14091      This filename is needed in order to process all macros whose definition
14092      (or undefinition) comes from the command line.  These macros are defined
14093      before the first DW_MACINFO_start_file entry, and yet still need to be
14094      associated to the base file.
14095
14096      To determine the base file name, we scan the macro definitions until we
14097      reach the first DW_MACINFO_start_file entry.  We then initialize
14098      CURRENT_FILE accordingly so that any macro definition found before the
14099      first DW_MACINFO_start_file can still be associated to the base file.  */
14100
14101   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14102   mac_end = dwarf2_per_objfile->macinfo.buffer
14103     + dwarf2_per_objfile->macinfo.size;
14104
14105   do
14106     {
14107       /* Do we at least have room for a macinfo type byte?  */
14108       if (mac_ptr >= mac_end)
14109         {
14110           /* Complaint is printed during the second pass as GDB will probably
14111              stop the first pass earlier upon finding
14112              DW_MACINFO_start_file.  */
14113           break;
14114         }
14115
14116       macinfo_type = read_1_byte (abfd, mac_ptr);
14117       mac_ptr++;
14118
14119       switch (macinfo_type)
14120         {
14121           /* A zero macinfo type indicates the end of the macro
14122              information.  */
14123         case 0:
14124           break;
14125
14126         case DW_MACINFO_define:
14127         case DW_MACINFO_undef:
14128           /* Only skip the data by MAC_PTR.  */
14129           {
14130             unsigned int bytes_read;
14131
14132             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14133             mac_ptr += bytes_read;
14134             read_direct_string (abfd, mac_ptr, &bytes_read);
14135             mac_ptr += bytes_read;
14136           }
14137           break;
14138
14139         case DW_MACINFO_start_file:
14140           {
14141             unsigned int bytes_read;
14142             int line, file;
14143
14144             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14145             mac_ptr += bytes_read;
14146             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14147             mac_ptr += bytes_read;
14148
14149             current_file = macro_start_file (file, line, current_file,
14150                                              comp_dir, lh, cu->objfile);
14151           }
14152           break;
14153
14154         case DW_MACINFO_end_file:
14155           /* No data to skip by MAC_PTR.  */
14156           break;
14157
14158         case DW_MACINFO_vendor_ext:
14159           /* Only skip the data by MAC_PTR.  */
14160           {
14161             unsigned int bytes_read;
14162
14163             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14164             mac_ptr += bytes_read;
14165             read_direct_string (abfd, mac_ptr, &bytes_read);
14166             mac_ptr += bytes_read;
14167           }
14168           break;
14169
14170         default:
14171           break;
14172         }
14173     } while (macinfo_type != 0 && current_file == NULL);
14174
14175   /* Second pass: Process all entries.
14176
14177      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14178      command-line macro definitions/undefinitions.  This flag is unset when we
14179      reach the first DW_MACINFO_start_file entry.  */
14180
14181   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14182
14183   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14184      GDB is still reading the definitions from command line.  First
14185      DW_MACINFO_start_file will need to be ignored as it was already executed
14186      to create CURRENT_FILE for the main source holding also the command line
14187      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14188      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14189
14190   at_commandline = 1;
14191
14192   do
14193     {
14194       /* Do we at least have room for a macinfo type byte?  */
14195       if (mac_ptr >= mac_end)
14196         {
14197           dwarf2_macros_too_long_complaint ();
14198           break;
14199         }
14200
14201       macinfo_type = read_1_byte (abfd, mac_ptr);
14202       mac_ptr++;
14203
14204       switch (macinfo_type)
14205         {
14206           /* A zero macinfo type indicates the end of the macro
14207              information.  */
14208         case 0:
14209           break;
14210
14211         case DW_MACINFO_define:
14212         case DW_MACINFO_undef:
14213           {
14214             unsigned int bytes_read;
14215             int line;
14216             char *body;
14217
14218             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14219             mac_ptr += bytes_read;
14220             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14221             mac_ptr += bytes_read;
14222
14223             if (! current_file)
14224               {
14225                 /* DWARF violation as no main source is present.  */
14226                 complaint (&symfile_complaints,
14227                            _("debug info with no main source gives macro %s "
14228                              "on line %d: %s"),
14229                            macinfo_type == DW_MACINFO_define ?
14230                              _("definition") :
14231                                macinfo_type == DW_MACINFO_undef ?
14232                                  _("undefinition") :
14233                                  _("something-or-other"), line, body);
14234                 break;
14235               }
14236             if ((line == 0 && !at_commandline)
14237                 || (line != 0 && at_commandline))
14238               complaint (&symfile_complaints,
14239                          _("debug info gives %s macro %s with %s line %d: %s"),
14240                          at_commandline ? _("command-line") : _("in-file"),
14241                          macinfo_type == DW_MACINFO_define ?
14242                            _("definition") :
14243                              macinfo_type == DW_MACINFO_undef ?
14244                                _("undefinition") :
14245                                _("something-or-other"),
14246                          line == 0 ? _("zero") : _("non-zero"), line, body);
14247
14248             if (macinfo_type == DW_MACINFO_define)
14249               parse_macro_definition (current_file, line, body);
14250             else if (macinfo_type == DW_MACINFO_undef)
14251               macro_undef (current_file, line, body);
14252           }
14253           break;
14254
14255         case DW_MACINFO_start_file:
14256           {
14257             unsigned int bytes_read;
14258             int line, file;
14259
14260             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14261             mac_ptr += bytes_read;
14262             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14263             mac_ptr += bytes_read;
14264
14265             if ((line == 0 && !at_commandline)
14266                 || (line != 0 && at_commandline))
14267               complaint (&symfile_complaints,
14268                          _("debug info gives source %d included "
14269                            "from %s at %s line %d"),
14270                          file, at_commandline ? _("command-line") : _("file"),
14271                          line == 0 ? _("zero") : _("non-zero"), line);
14272
14273             if (at_commandline)
14274               {
14275                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14276                 at_commandline = 0;
14277               }
14278             else
14279               current_file = macro_start_file (file, line,
14280                                                current_file, comp_dir,
14281                                                lh, cu->objfile);
14282           }
14283           break;
14284
14285         case DW_MACINFO_end_file:
14286           if (! current_file)
14287             complaint (&symfile_complaints,
14288                        _("macro debug info has an unmatched "
14289                          "`close_file' directive"));
14290           else
14291             {
14292               current_file = current_file->included_by;
14293               if (! current_file)
14294                 {
14295                   enum dwarf_macinfo_record_type next_type;
14296
14297                   /* GCC circa March 2002 doesn't produce the zero
14298                      type byte marking the end of the compilation
14299                      unit.  Complain if it's not there, but exit no
14300                      matter what.  */
14301
14302                   /* Do we at least have room for a macinfo type byte?  */
14303                   if (mac_ptr >= mac_end)
14304                     {
14305                       dwarf2_macros_too_long_complaint ();
14306                       return;
14307                     }
14308
14309                   /* We don't increment mac_ptr here, so this is just
14310                      a look-ahead.  */
14311                   next_type = read_1_byte (abfd, mac_ptr);
14312                   if (next_type != 0)
14313                     complaint (&symfile_complaints,
14314                                _("no terminating 0-type entry for "
14315                                  "macros in `.debug_macinfo' section"));
14316
14317                   return;
14318                 }
14319             }
14320           break;
14321
14322         case DW_MACINFO_vendor_ext:
14323           {
14324             unsigned int bytes_read;
14325             int constant;
14326
14327             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14328             mac_ptr += bytes_read;
14329             read_direct_string (abfd, mac_ptr, &bytes_read);
14330             mac_ptr += bytes_read;
14331
14332             /* We don't recognize any vendor extensions.  */
14333           }
14334           break;
14335         }
14336     } while (macinfo_type != 0);
14337 }
14338
14339 /* Check if the attribute's form is a DW_FORM_block*
14340    if so return true else false.  */
14341 static int
14342 attr_form_is_block (struct attribute *attr)
14343 {
14344   return (attr == NULL ? 0 :
14345       attr->form == DW_FORM_block1
14346       || attr->form == DW_FORM_block2
14347       || attr->form == DW_FORM_block4
14348       || attr->form == DW_FORM_block
14349       || attr->form == DW_FORM_exprloc);
14350 }
14351
14352 /* Return non-zero if ATTR's value is a section offset --- classes
14353    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14354    You may use DW_UNSND (attr) to retrieve such offsets.
14355
14356    Section 7.5.4, "Attribute Encodings", explains that no attribute
14357    may have a value that belongs to more than one of these classes; it
14358    would be ambiguous if we did, because we use the same forms for all
14359    of them.  */
14360 static int
14361 attr_form_is_section_offset (struct attribute *attr)
14362 {
14363   return (attr->form == DW_FORM_data4
14364           || attr->form == DW_FORM_data8
14365           || attr->form == DW_FORM_sec_offset);
14366 }
14367
14368
14369 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14370    zero otherwise.  When this function returns true, you can apply
14371    dwarf2_get_attr_constant_value to it.
14372
14373    However, note that for some attributes you must check
14374    attr_form_is_section_offset before using this test.  DW_FORM_data4
14375    and DW_FORM_data8 are members of both the constant class, and of
14376    the classes that contain offsets into other debug sections
14377    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14378    that, if an attribute's can be either a constant or one of the
14379    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14380    taken as section offsets, not constants.  */
14381 static int
14382 attr_form_is_constant (struct attribute *attr)
14383 {
14384   switch (attr->form)
14385     {
14386     case DW_FORM_sdata:
14387     case DW_FORM_udata:
14388     case DW_FORM_data1:
14389     case DW_FORM_data2:
14390     case DW_FORM_data4:
14391     case DW_FORM_data8:
14392       return 1;
14393     default:
14394       return 0;
14395     }
14396 }
14397
14398 /* A helper function that fills in a dwarf2_loclist_baton.  */
14399
14400 static void
14401 fill_in_loclist_baton (struct dwarf2_cu *cu,
14402                        struct dwarf2_loclist_baton *baton,
14403                        struct attribute *attr)
14404 {
14405   dwarf2_read_section (dwarf2_per_objfile->objfile,
14406                        &dwarf2_per_objfile->loc);
14407
14408   baton->per_cu = cu->per_cu;
14409   gdb_assert (baton->per_cu);
14410   /* We don't know how long the location list is, but make sure we
14411      don't run off the edge of the section.  */
14412   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14413   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14414   baton->base_address = cu->base_address;
14415 }
14416
14417 static void
14418 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14419                              struct dwarf2_cu *cu)
14420 {
14421   if (attr_form_is_section_offset (attr)
14422       /* ".debug_loc" may not exist at all, or the offset may be outside
14423          the section.  If so, fall through to the complaint in the
14424          other branch.  */
14425       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14426                                                 &dwarf2_per_objfile->loc))
14427     {
14428       struct dwarf2_loclist_baton *baton;
14429
14430       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14431                              sizeof (struct dwarf2_loclist_baton));
14432
14433       fill_in_loclist_baton (cu, baton, attr);
14434
14435       if (cu->base_known == 0)
14436         complaint (&symfile_complaints,
14437                    _("Location list used without "
14438                      "specifying the CU base address."));
14439
14440       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14441       SYMBOL_LOCATION_BATON (sym) = baton;
14442     }
14443   else
14444     {
14445       struct dwarf2_locexpr_baton *baton;
14446
14447       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14448                              sizeof (struct dwarf2_locexpr_baton));
14449       baton->per_cu = cu->per_cu;
14450       gdb_assert (baton->per_cu);
14451
14452       if (attr_form_is_block (attr))
14453         {
14454           /* Note that we're just copying the block's data pointer
14455              here, not the actual data.  We're still pointing into the
14456              info_buffer for SYM's objfile; right now we never release
14457              that buffer, but when we do clean up properly this may
14458              need to change.  */
14459           baton->size = DW_BLOCK (attr)->size;
14460           baton->data = DW_BLOCK (attr)->data;
14461         }
14462       else
14463         {
14464           dwarf2_invalid_attrib_class_complaint ("location description",
14465                                                  SYMBOL_NATURAL_NAME (sym));
14466           baton->size = 0;
14467           baton->data = NULL;
14468         }
14469
14470       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14471       SYMBOL_LOCATION_BATON (sym) = baton;
14472     }
14473 }
14474
14475 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14476    came from a separate debuginfo file, then the master objfile is
14477    returned.  */
14478
14479 struct objfile *
14480 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14481 {
14482   struct objfile *objfile = per_cu->objfile;
14483
14484   /* Return the master objfile, so that we can report and look up the
14485      correct file containing this variable.  */
14486   if (objfile->separate_debug_objfile_backlink)
14487     objfile = objfile->separate_debug_objfile_backlink;
14488
14489   return objfile;
14490 }
14491
14492 /* Return the address size given in the compilation unit header for CU.  */
14493
14494 CORE_ADDR
14495 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14496 {
14497   if (per_cu->cu)
14498     return per_cu->cu->header.addr_size;
14499   else
14500     {
14501       /* If the CU is not currently read in, we re-read its header.  */
14502       struct objfile *objfile = per_cu->objfile;
14503       struct dwarf2_per_objfile *per_objfile
14504         = objfile_data (objfile, dwarf2_objfile_data_key);
14505       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14506       struct comp_unit_head cu_header;
14507
14508       memset (&cu_header, 0, sizeof cu_header);
14509       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14510       return cu_header.addr_size;
14511     }
14512 }
14513
14514 /* Return the offset size given in the compilation unit header for CU.  */
14515
14516 int
14517 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14518 {
14519   if (per_cu->cu)
14520     return per_cu->cu->header.offset_size;
14521   else
14522     {
14523       /* If the CU is not currently read in, we re-read its header.  */
14524       struct objfile *objfile = per_cu->objfile;
14525       struct dwarf2_per_objfile *per_objfile
14526         = objfile_data (objfile, dwarf2_objfile_data_key);
14527       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14528       struct comp_unit_head cu_header;
14529
14530       memset (&cu_header, 0, sizeof cu_header);
14531       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14532       return cu_header.offset_size;
14533     }
14534 }
14535
14536 /* Return the text offset of the CU.  The returned offset comes from
14537    this CU's objfile.  If this objfile came from a separate debuginfo
14538    file, then the offset may be different from the corresponding
14539    offset in the parent objfile.  */
14540
14541 CORE_ADDR
14542 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14543 {
14544   struct objfile *objfile = per_cu->objfile;
14545
14546   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14547 }
14548
14549 /* Locate the .debug_info compilation unit from CU's objfile which contains
14550    the DIE at OFFSET.  Raises an error on failure.  */
14551
14552 static struct dwarf2_per_cu_data *
14553 dwarf2_find_containing_comp_unit (unsigned int offset,
14554                                   struct objfile *objfile)
14555 {
14556   struct dwarf2_per_cu_data *this_cu;
14557   int low, high;
14558
14559   low = 0;
14560   high = dwarf2_per_objfile->n_comp_units - 1;
14561   while (high > low)
14562     {
14563       int mid = low + (high - low) / 2;
14564
14565       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14566         high = mid;
14567       else
14568         low = mid + 1;
14569     }
14570   gdb_assert (low == high);
14571   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14572     {
14573       if (low == 0)
14574         error (_("Dwarf Error: could not find partial DIE containing "
14575                "offset 0x%lx [in module %s]"),
14576                (long) offset, bfd_get_filename (objfile->obfd));
14577
14578       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14579       return dwarf2_per_objfile->all_comp_units[low-1];
14580     }
14581   else
14582     {
14583       this_cu = dwarf2_per_objfile->all_comp_units[low];
14584       if (low == dwarf2_per_objfile->n_comp_units - 1
14585           && offset >= this_cu->offset + this_cu->length)
14586         error (_("invalid dwarf2 offset %u"), offset);
14587       gdb_assert (offset < this_cu->offset + this_cu->length);
14588       return this_cu;
14589     }
14590 }
14591
14592 /* Locate the compilation unit from OBJFILE which is located at exactly
14593    OFFSET.  Raises an error on failure.  */
14594
14595 static struct dwarf2_per_cu_data *
14596 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14597 {
14598   struct dwarf2_per_cu_data *this_cu;
14599
14600   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14601   if (this_cu->offset != offset)
14602     error (_("no compilation unit with offset %u."), offset);
14603   return this_cu;
14604 }
14605
14606 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14607
14608 static void
14609 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14610 {
14611   memset (cu, 0, sizeof (*cu));
14612   cu->objfile = objfile;
14613   obstack_init (&cu->comp_unit_obstack);
14614 }
14615
14616 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14617
14618 static void
14619 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14620 {
14621   struct attribute *attr;
14622
14623   /* Set the language we're debugging.  */
14624   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14625   if (attr)
14626     set_cu_language (DW_UNSND (attr), cu);
14627   else
14628     set_cu_language (language_minimal, cu);
14629 }
14630
14631 /* Release one cached compilation unit, CU.  We unlink it from the tree
14632    of compilation units, but we don't remove it from the read_in_chain;
14633    the caller is responsible for that.
14634    NOTE: DATA is a void * because this function is also used as a
14635    cleanup routine.  */
14636
14637 static void
14638 free_one_comp_unit (void *data)
14639 {
14640   struct dwarf2_cu *cu = data;
14641
14642   if (cu->per_cu != NULL)
14643     cu->per_cu->cu = NULL;
14644   cu->per_cu = NULL;
14645
14646   obstack_free (&cu->comp_unit_obstack, NULL);
14647
14648   xfree (cu);
14649 }
14650
14651 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14652    when we're finished with it.  We can't free the pointer itself, but be
14653    sure to unlink it from the cache.  Also release any associated storage
14654    and perform cache maintenance.
14655
14656    Only used during partial symbol parsing.  */
14657
14658 static void
14659 free_stack_comp_unit (void *data)
14660 {
14661   struct dwarf2_cu *cu = data;
14662
14663   obstack_free (&cu->comp_unit_obstack, NULL);
14664   cu->partial_dies = NULL;
14665
14666   if (cu->per_cu != NULL)
14667     {
14668       /* This compilation unit is on the stack in our caller, so we
14669          should not xfree it.  Just unlink it.  */
14670       cu->per_cu->cu = NULL;
14671       cu->per_cu = NULL;
14672
14673       /* If we had a per-cu pointer, then we may have other compilation
14674          units loaded, so age them now.  */
14675       age_cached_comp_units ();
14676     }
14677 }
14678
14679 /* Free all cached compilation units.  */
14680
14681 static void
14682 free_cached_comp_units (void *data)
14683 {
14684   struct dwarf2_per_cu_data *per_cu, **last_chain;
14685
14686   per_cu = dwarf2_per_objfile->read_in_chain;
14687   last_chain = &dwarf2_per_objfile->read_in_chain;
14688   while (per_cu != NULL)
14689     {
14690       struct dwarf2_per_cu_data *next_cu;
14691
14692       next_cu = per_cu->cu->read_in_chain;
14693
14694       free_one_comp_unit (per_cu->cu);
14695       *last_chain = next_cu;
14696
14697       per_cu = next_cu;
14698     }
14699 }
14700
14701 /* Increase the age counter on each cached compilation unit, and free
14702    any that are too old.  */
14703
14704 static void
14705 age_cached_comp_units (void)
14706 {
14707   struct dwarf2_per_cu_data *per_cu, **last_chain;
14708
14709   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14710   per_cu = dwarf2_per_objfile->read_in_chain;
14711   while (per_cu != NULL)
14712     {
14713       per_cu->cu->last_used ++;
14714       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14715         dwarf2_mark (per_cu->cu);
14716       per_cu = per_cu->cu->read_in_chain;
14717     }
14718
14719   per_cu = dwarf2_per_objfile->read_in_chain;
14720   last_chain = &dwarf2_per_objfile->read_in_chain;
14721   while (per_cu != NULL)
14722     {
14723       struct dwarf2_per_cu_data *next_cu;
14724
14725       next_cu = per_cu->cu->read_in_chain;
14726
14727       if (!per_cu->cu->mark)
14728         {
14729           free_one_comp_unit (per_cu->cu);
14730           *last_chain = next_cu;
14731         }
14732       else
14733         last_chain = &per_cu->cu->read_in_chain;
14734
14735       per_cu = next_cu;
14736     }
14737 }
14738
14739 /* Remove a single compilation unit from the cache.  */
14740
14741 static void
14742 free_one_cached_comp_unit (void *target_cu)
14743 {
14744   struct dwarf2_per_cu_data *per_cu, **last_chain;
14745
14746   per_cu = dwarf2_per_objfile->read_in_chain;
14747   last_chain = &dwarf2_per_objfile->read_in_chain;
14748   while (per_cu != NULL)
14749     {
14750       struct dwarf2_per_cu_data *next_cu;
14751
14752       next_cu = per_cu->cu->read_in_chain;
14753
14754       if (per_cu->cu == target_cu)
14755         {
14756           free_one_comp_unit (per_cu->cu);
14757           *last_chain = next_cu;
14758           break;
14759         }
14760       else
14761         last_chain = &per_cu->cu->read_in_chain;
14762
14763       per_cu = next_cu;
14764     }
14765 }
14766
14767 /* Release all extra memory associated with OBJFILE.  */
14768
14769 void
14770 dwarf2_free_objfile (struct objfile *objfile)
14771 {
14772   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14773
14774   if (dwarf2_per_objfile == NULL)
14775     return;
14776
14777   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14778   free_cached_comp_units (NULL);
14779
14780   if (dwarf2_per_objfile->quick_file_names_table)
14781     htab_delete (dwarf2_per_objfile->quick_file_names_table);
14782
14783   /* Everything else should be on the objfile obstack.  */
14784 }
14785
14786 /* A pair of DIE offset and GDB type pointer.  We store these
14787    in a hash table separate from the DIEs, and preserve them
14788    when the DIEs are flushed out of cache.  */
14789
14790 struct dwarf2_offset_and_type
14791 {
14792   unsigned int offset;
14793   struct type *type;
14794 };
14795
14796 /* Hash function for a dwarf2_offset_and_type.  */
14797
14798 static hashval_t
14799 offset_and_type_hash (const void *item)
14800 {
14801   const struct dwarf2_offset_and_type *ofs = item;
14802
14803   return ofs->offset;
14804 }
14805
14806 /* Equality function for a dwarf2_offset_and_type.  */
14807
14808 static int
14809 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14810 {
14811   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14812   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14813
14814   return ofs_lhs->offset == ofs_rhs->offset;
14815 }
14816
14817 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14818    table if necessary.  For convenience, return TYPE.
14819
14820    The DIEs reading must have careful ordering to:
14821     * Not cause infite loops trying to read in DIEs as a prerequisite for
14822       reading current DIE.
14823     * Not trying to dereference contents of still incompletely read in types
14824       while reading in other DIEs.
14825     * Enable referencing still incompletely read in types just by a pointer to
14826       the type without accessing its fields.
14827
14828    Therefore caller should follow these rules:
14829      * Try to fetch any prerequisite types we may need to build this DIE type
14830        before building the type and calling set_die_type.
14831      * After building type call set_die_type for current DIE as soon as
14832        possible before fetching more types to complete the current type.
14833      * Make the type as complete as possible before fetching more types.  */
14834
14835 static struct type *
14836 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14837 {
14838   struct dwarf2_offset_and_type **slot, ofs;
14839   struct objfile *objfile = cu->objfile;
14840   htab_t *type_hash_ptr;
14841
14842   /* For Ada types, make sure that the gnat-specific data is always
14843      initialized (if not already set).  There are a few types where
14844      we should not be doing so, because the type-specific area is
14845      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14846      where the type-specific area is used to store the floatformat).
14847      But this is not a problem, because the gnat-specific information
14848      is actually not needed for these types.  */
14849   if (need_gnat_info (cu)
14850       && TYPE_CODE (type) != TYPE_CODE_FUNC
14851       && TYPE_CODE (type) != TYPE_CODE_FLT
14852       && !HAVE_GNAT_AUX_INFO (type))
14853     INIT_GNAT_SPECIFIC (type);
14854
14855   if (cu->per_cu->from_debug_types)
14856     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14857   else
14858     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14859
14860   if (*type_hash_ptr == NULL)
14861     {
14862       *type_hash_ptr
14863         = htab_create_alloc_ex (127,
14864                                 offset_and_type_hash,
14865                                 offset_and_type_eq,
14866                                 NULL,
14867                                 &objfile->objfile_obstack,
14868                                 hashtab_obstack_allocate,
14869                                 dummy_obstack_deallocate);
14870     }
14871
14872   ofs.offset = die->offset;
14873   ofs.type = type;
14874   slot = (struct dwarf2_offset_and_type **)
14875     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14876   if (*slot)
14877     complaint (&symfile_complaints,
14878                _("A problem internal to GDB: DIE 0x%x has type already set"),
14879                die->offset);
14880   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14881   **slot = ofs;
14882   return type;
14883 }
14884
14885 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14886    table, or return NULL if the die does not have a saved type.  */
14887
14888 static struct type *
14889 get_die_type_at_offset (unsigned int offset,
14890                         struct dwarf2_per_cu_data *per_cu)
14891 {
14892   struct dwarf2_offset_and_type *slot, ofs;
14893   htab_t type_hash;
14894
14895   if (per_cu->from_debug_types)
14896     type_hash = dwarf2_per_objfile->debug_types_type_hash;
14897   else
14898     type_hash = dwarf2_per_objfile->debug_info_type_hash;
14899   if (type_hash == NULL)
14900     return NULL;
14901
14902   ofs.offset = offset;
14903   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14904   if (slot)
14905     return slot->type;
14906   else
14907     return NULL;
14908 }
14909
14910 /* Look up the type for DIE in the appropriate type_hash table,
14911    or return NULL if DIE does not have a saved type.  */
14912
14913 static struct type *
14914 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14915 {
14916   return get_die_type_at_offset (die->offset, cu->per_cu);
14917 }
14918
14919 /* Add a dependence relationship from CU to REF_PER_CU.  */
14920
14921 static void
14922 dwarf2_add_dependence (struct dwarf2_cu *cu,
14923                        struct dwarf2_per_cu_data *ref_per_cu)
14924 {
14925   void **slot;
14926
14927   if (cu->dependencies == NULL)
14928     cu->dependencies
14929       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14930                               NULL, &cu->comp_unit_obstack,
14931                               hashtab_obstack_allocate,
14932                               dummy_obstack_deallocate);
14933
14934   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14935   if (*slot == NULL)
14936     *slot = ref_per_cu;
14937 }
14938
14939 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14940    Set the mark field in every compilation unit in the
14941    cache that we must keep because we are keeping CU.  */
14942
14943 static int
14944 dwarf2_mark_helper (void **slot, void *data)
14945 {
14946   struct dwarf2_per_cu_data *per_cu;
14947
14948   per_cu = (struct dwarf2_per_cu_data *) *slot;
14949   if (per_cu->cu->mark)
14950     return 1;
14951   per_cu->cu->mark = 1;
14952
14953   if (per_cu->cu->dependencies != NULL)
14954     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14955
14956   return 1;
14957 }
14958
14959 /* Set the mark field in CU and in every other compilation unit in the
14960    cache that we must keep because we are keeping CU.  */
14961
14962 static void
14963 dwarf2_mark (struct dwarf2_cu *cu)
14964 {
14965   if (cu->mark)
14966     return;
14967   cu->mark = 1;
14968   if (cu->dependencies != NULL)
14969     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14970 }
14971
14972 static void
14973 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14974 {
14975   while (per_cu)
14976     {
14977       per_cu->cu->mark = 0;
14978       per_cu = per_cu->cu->read_in_chain;
14979     }
14980 }
14981
14982 /* Trivial hash function for partial_die_info: the hash value of a DIE
14983    is its offset in .debug_info for this objfile.  */
14984
14985 static hashval_t
14986 partial_die_hash (const void *item)
14987 {
14988   const struct partial_die_info *part_die = item;
14989
14990   return part_die->offset;
14991 }
14992
14993 /* Trivial comparison function for partial_die_info structures: two DIEs
14994    are equal if they have the same offset.  */
14995
14996 static int
14997 partial_die_eq (const void *item_lhs, const void *item_rhs)
14998 {
14999   const struct partial_die_info *part_die_lhs = item_lhs;
15000   const struct partial_die_info *part_die_rhs = item_rhs;
15001
15002   return part_die_lhs->offset == part_die_rhs->offset;
15003 }
15004
15005 static struct cmd_list_element *set_dwarf2_cmdlist;
15006 static struct cmd_list_element *show_dwarf2_cmdlist;
15007
15008 static void
15009 set_dwarf2_cmd (char *args, int from_tty)
15010 {
15011   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15012 }
15013
15014 static void
15015 show_dwarf2_cmd (char *args, int from_tty)
15016 {
15017   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15018 }
15019
15020 /* If section described by INFO was mmapped, munmap it now.  */
15021
15022 static void
15023 munmap_section_buffer (struct dwarf2_section_info *info)
15024 {
15025   if (info->was_mmapped)
15026     {
15027 #ifdef HAVE_MMAP
15028       intptr_t begin = (intptr_t) info->buffer;
15029       intptr_t map_begin = begin & ~(pagesize - 1);
15030       size_t map_length = info->size + begin - map_begin;
15031
15032       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15033 #else
15034       /* Without HAVE_MMAP, we should never be here to begin with.  */
15035       gdb_assert_not_reached ("no mmap support");
15036 #endif
15037     }
15038 }
15039
15040 /* munmap debug sections for OBJFILE, if necessary.  */
15041
15042 static void
15043 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15044 {
15045   struct dwarf2_per_objfile *data = d;
15046
15047   /* This is sorted according to the order they're defined in to make it easier
15048      to keep in sync.  */
15049   munmap_section_buffer (&data->info);
15050   munmap_section_buffer (&data->abbrev);
15051   munmap_section_buffer (&data->line);
15052   munmap_section_buffer (&data->loc);
15053   munmap_section_buffer (&data->macinfo);
15054   munmap_section_buffer (&data->str);
15055   munmap_section_buffer (&data->ranges);
15056   munmap_section_buffer (&data->types);
15057   munmap_section_buffer (&data->frame);
15058   munmap_section_buffer (&data->eh_frame);
15059   munmap_section_buffer (&data->gdb_index);
15060 }
15061
15062 \f
15063 /* The "save gdb-index" command.  */
15064
15065 /* The contents of the hash table we create when building the string
15066    table.  */
15067 struct strtab_entry
15068 {
15069   offset_type offset;
15070   const char *str;
15071 };
15072
15073 /* Hash function for a strtab_entry.  */
15074
15075 static hashval_t
15076 hash_strtab_entry (const void *e)
15077 {
15078   const struct strtab_entry *entry = e;
15079   return mapped_index_string_hash (entry->str);
15080 }
15081
15082 /* Equality function for a strtab_entry.  */
15083
15084 static int
15085 eq_strtab_entry (const void *a, const void *b)
15086 {
15087   const struct strtab_entry *ea = a;
15088   const struct strtab_entry *eb = b;
15089   return !strcmp (ea->str, eb->str);
15090 }
15091
15092 /* Create a strtab_entry hash table.  */
15093
15094 static htab_t
15095 create_strtab (void)
15096 {
15097   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15098                             xfree, xcalloc, xfree);
15099 }
15100
15101 /* Add a string to the constant pool.  Return the string's offset in
15102    host order.  */
15103
15104 static offset_type
15105 add_string (htab_t table, struct obstack *cpool, const char *str)
15106 {
15107   void **slot;
15108   struct strtab_entry entry;
15109   struct strtab_entry *result;
15110
15111   entry.str = str;
15112   slot = htab_find_slot (table, &entry, INSERT);
15113   if (*slot)
15114     result = *slot;
15115   else
15116     {
15117       result = XNEW (struct strtab_entry);
15118       result->offset = obstack_object_size (cpool);
15119       result->str = str;
15120       obstack_grow_str0 (cpool, str);
15121       *slot = result;
15122     }
15123   return result->offset;
15124 }
15125
15126 /* An entry in the symbol table.  */
15127 struct symtab_index_entry
15128 {
15129   /* The name of the symbol.  */
15130   const char *name;
15131   /* The offset of the name in the constant pool.  */
15132   offset_type index_offset;
15133   /* A sorted vector of the indices of all the CUs that hold an object
15134      of this name.  */
15135   VEC (offset_type) *cu_indices;
15136 };
15137
15138 /* The symbol table.  This is a power-of-2-sized hash table.  */
15139 struct mapped_symtab
15140 {
15141   offset_type n_elements;
15142   offset_type size;
15143   struct symtab_index_entry **data;
15144 };
15145
15146 /* Hash function for a symtab_index_entry.  */
15147
15148 static hashval_t
15149 hash_symtab_entry (const void *e)
15150 {
15151   const struct symtab_index_entry *entry = e;
15152   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15153                          sizeof (offset_type) * VEC_length (offset_type,
15154                                                             entry->cu_indices),
15155                          0);
15156 }
15157
15158 /* Equality function for a symtab_index_entry.  */
15159
15160 static int
15161 eq_symtab_entry (const void *a, const void *b)
15162 {
15163   const struct symtab_index_entry *ea = a;
15164   const struct symtab_index_entry *eb = b;
15165   int len = VEC_length (offset_type, ea->cu_indices);
15166   if (len != VEC_length (offset_type, eb->cu_indices))
15167     return 0;
15168   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15169                   VEC_address (offset_type, eb->cu_indices),
15170                   sizeof (offset_type) * len);
15171 }
15172
15173 /* Destroy a symtab_index_entry.  */
15174
15175 static void
15176 delete_symtab_entry (void *p)
15177 {
15178   struct symtab_index_entry *entry = p;
15179   VEC_free (offset_type, entry->cu_indices);
15180   xfree (entry);
15181 }
15182
15183 /* Create a hash table holding symtab_index_entry objects.  */
15184
15185 static htab_t
15186 create_symbol_hash_table (void)
15187 {
15188   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15189                             delete_symtab_entry, xcalloc, xfree);
15190 }
15191
15192 /* Create a new mapped symtab object.  */
15193
15194 static struct mapped_symtab *
15195 create_mapped_symtab (void)
15196 {
15197   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15198   symtab->n_elements = 0;
15199   symtab->size = 1024;
15200   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15201   return symtab;
15202 }
15203
15204 /* Destroy a mapped_symtab.  */
15205
15206 static void
15207 cleanup_mapped_symtab (void *p)
15208 {
15209   struct mapped_symtab *symtab = p;
15210   /* The contents of the array are freed when the other hash table is
15211      destroyed.  */
15212   xfree (symtab->data);
15213   xfree (symtab);
15214 }
15215
15216 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15217    the slot.  */
15218
15219 static struct symtab_index_entry **
15220 find_slot (struct mapped_symtab *symtab, const char *name)
15221 {
15222   offset_type index, step, hash = mapped_index_string_hash (name);
15223
15224   index = hash & (symtab->size - 1);
15225   step = ((hash * 17) & (symtab->size - 1)) | 1;
15226
15227   for (;;)
15228     {
15229       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15230         return &symtab->data[index];
15231       index = (index + step) & (symtab->size - 1);
15232     }
15233 }
15234
15235 /* Expand SYMTAB's hash table.  */
15236
15237 static void
15238 hash_expand (struct mapped_symtab *symtab)
15239 {
15240   offset_type old_size = symtab->size;
15241   offset_type i;
15242   struct symtab_index_entry **old_entries = symtab->data;
15243
15244   symtab->size *= 2;
15245   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15246
15247   for (i = 0; i < old_size; ++i)
15248     {
15249       if (old_entries[i])
15250         {
15251           struct symtab_index_entry **slot = find_slot (symtab,
15252                                                         old_entries[i]->name);
15253           *slot = old_entries[i];
15254         }
15255     }
15256
15257   xfree (old_entries);
15258 }
15259
15260 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15261    is the index of the CU in which the symbol appears.  */
15262
15263 static void
15264 add_index_entry (struct mapped_symtab *symtab, const char *name,
15265                  offset_type cu_index)
15266 {
15267   struct symtab_index_entry **slot;
15268
15269   ++symtab->n_elements;
15270   if (4 * symtab->n_elements / 3 >= symtab->size)
15271     hash_expand (symtab);
15272
15273   slot = find_slot (symtab, name);
15274   if (!*slot)
15275     {
15276       *slot = XNEW (struct symtab_index_entry);
15277       (*slot)->name = name;
15278       (*slot)->cu_indices = NULL;
15279     }
15280   /* Don't push an index twice.  Due to how we add entries we only
15281      have to check the last one.  */ 
15282   if (VEC_empty (offset_type, (*slot)->cu_indices)
15283       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15284     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15285 }
15286
15287 /* Add a vector of indices to the constant pool.  */
15288
15289 static offset_type
15290 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15291                       struct symtab_index_entry *entry)
15292 {
15293   void **slot;
15294
15295   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15296   if (!*slot)
15297     {
15298       offset_type len = VEC_length (offset_type, entry->cu_indices);
15299       offset_type val = MAYBE_SWAP (len);
15300       offset_type iter;
15301       int i;
15302
15303       *slot = entry;
15304       entry->index_offset = obstack_object_size (cpool);
15305
15306       obstack_grow (cpool, &val, sizeof (val));
15307       for (i = 0;
15308            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15309            ++i)
15310         {
15311           val = MAYBE_SWAP (iter);
15312           obstack_grow (cpool, &val, sizeof (val));
15313         }
15314     }
15315   else
15316     {
15317       struct symtab_index_entry *old_entry = *slot;
15318       entry->index_offset = old_entry->index_offset;
15319       entry = old_entry;
15320     }
15321   return entry->index_offset;
15322 }
15323
15324 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15325    constant pool entries going into the obstack CPOOL.  */
15326
15327 static void
15328 write_hash_table (struct mapped_symtab *symtab,
15329                   struct obstack *output, struct obstack *cpool)
15330 {
15331   offset_type i;
15332   htab_t symbol_hash_table;
15333   htab_t str_table;
15334
15335   symbol_hash_table = create_symbol_hash_table ();
15336   str_table = create_strtab ();
15337
15338   /* We add all the index vectors to the constant pool first, to
15339      ensure alignment is ok.  */
15340   for (i = 0; i < symtab->size; ++i)
15341     {
15342       if (symtab->data[i])
15343         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15344     }
15345
15346   /* Now write out the hash table.  */
15347   for (i = 0; i < symtab->size; ++i)
15348     {
15349       offset_type str_off, vec_off;
15350
15351       if (symtab->data[i])
15352         {
15353           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15354           vec_off = symtab->data[i]->index_offset;
15355         }
15356       else
15357         {
15358           /* While 0 is a valid constant pool index, it is not valid
15359              to have 0 for both offsets.  */
15360           str_off = 0;
15361           vec_off = 0;
15362         }
15363
15364       str_off = MAYBE_SWAP (str_off);
15365       vec_off = MAYBE_SWAP (vec_off);
15366
15367       obstack_grow (output, &str_off, sizeof (str_off));
15368       obstack_grow (output, &vec_off, sizeof (vec_off));
15369     }
15370
15371   htab_delete (str_table);
15372   htab_delete (symbol_hash_table);
15373 }
15374
15375 /* Struct to map psymtab to CU index in the index file.  */
15376 struct psymtab_cu_index_map
15377 {
15378   struct partial_symtab *psymtab;
15379   unsigned int cu_index;
15380 };
15381
15382 static hashval_t
15383 hash_psymtab_cu_index (const void *item)
15384 {
15385   const struct psymtab_cu_index_map *map = item;
15386
15387   return htab_hash_pointer (map->psymtab);
15388 }
15389
15390 static int
15391 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15392 {
15393   const struct psymtab_cu_index_map *lhs = item_lhs;
15394   const struct psymtab_cu_index_map *rhs = item_rhs;
15395
15396   return lhs->psymtab == rhs->psymtab;
15397 }
15398
15399 /* Helper struct for building the address table.  */
15400 struct addrmap_index_data
15401 {
15402   struct objfile *objfile;
15403   struct obstack *addr_obstack;
15404   htab_t cu_index_htab;
15405
15406   /* Non-zero if the previous_* fields are valid.
15407      We can't write an entry until we see the next entry (since it is only then
15408      that we know the end of the entry).  */
15409   int previous_valid;
15410   /* Index of the CU in the table of all CUs in the index file.  */
15411   unsigned int previous_cu_index;
15412   /* Start address of the CU.  */
15413   CORE_ADDR previous_cu_start;
15414 };
15415
15416 /* Write an address entry to OBSTACK.  */
15417
15418 static void
15419 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15420                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15421 {
15422   offset_type cu_index_to_write;
15423   char addr[8];
15424   CORE_ADDR baseaddr;
15425
15426   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15427
15428   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15429   obstack_grow (obstack, addr, 8);
15430   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15431   obstack_grow (obstack, addr, 8);
15432   cu_index_to_write = MAYBE_SWAP (cu_index);
15433   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15434 }
15435
15436 /* Worker function for traversing an addrmap to build the address table.  */
15437
15438 static int
15439 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15440 {
15441   struct addrmap_index_data *data = datap;
15442   struct partial_symtab *pst = obj;
15443   offset_type cu_index;
15444   void **slot;
15445
15446   if (data->previous_valid)
15447     add_address_entry (data->objfile, data->addr_obstack,
15448                        data->previous_cu_start, start_addr,
15449                        data->previous_cu_index);
15450
15451   data->previous_cu_start = start_addr;
15452   if (pst != NULL)
15453     {
15454       struct psymtab_cu_index_map find_map, *map;
15455       find_map.psymtab = pst;
15456       map = htab_find (data->cu_index_htab, &find_map);
15457       gdb_assert (map != NULL);
15458       data->previous_cu_index = map->cu_index;
15459       data->previous_valid = 1;
15460     }
15461   else
15462       data->previous_valid = 0;
15463
15464   return 0;
15465 }
15466
15467 /* Write OBJFILE's address map to OBSTACK.
15468    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15469    in the index file.  */
15470
15471 static void
15472 write_address_map (struct objfile *objfile, struct obstack *obstack,
15473                    htab_t cu_index_htab)
15474 {
15475   struct addrmap_index_data addrmap_index_data;
15476
15477   /* When writing the address table, we have to cope with the fact that
15478      the addrmap iterator only provides the start of a region; we have to
15479      wait until the next invocation to get the start of the next region.  */
15480
15481   addrmap_index_data.objfile = objfile;
15482   addrmap_index_data.addr_obstack = obstack;
15483   addrmap_index_data.cu_index_htab = cu_index_htab;
15484   addrmap_index_data.previous_valid = 0;
15485
15486   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15487                    &addrmap_index_data);
15488
15489   /* It's highly unlikely the last entry (end address = 0xff...ff)
15490      is valid, but we should still handle it.
15491      The end address is recorded as the start of the next region, but that
15492      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15493      anyway.  */
15494   if (addrmap_index_data.previous_valid)
15495     add_address_entry (objfile, obstack,
15496                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15497                        addrmap_index_data.previous_cu_index);
15498 }
15499
15500 /* Add a list of partial symbols to SYMTAB.  */
15501
15502 static void
15503 write_psymbols (struct mapped_symtab *symtab,
15504                 htab_t psyms_seen,
15505                 struct partial_symbol **psymp,
15506                 int count,
15507                 offset_type cu_index,
15508                 int is_static)
15509 {
15510   for (; count-- > 0; ++psymp)
15511     {
15512       void **slot, *lookup;
15513
15514       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15515         error (_("Ada is not currently supported by the index"));
15516
15517       /* We only want to add a given psymbol once.  However, we also
15518          want to account for whether it is global or static.  So, we
15519          may add it twice, using slightly different values.  */
15520       if (is_static)
15521         {
15522           uintptr_t val = 1 | (uintptr_t) *psymp;
15523
15524           lookup = (void *) val;
15525         }
15526       else
15527         lookup = *psymp;
15528
15529       /* Only add a given psymbol once.  */
15530       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15531       if (!*slot)
15532         {
15533           *slot = lookup;
15534           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15535         }
15536     }
15537 }
15538
15539 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15540    exception if there is an error.  */
15541
15542 static void
15543 write_obstack (FILE *file, struct obstack *obstack)
15544 {
15545   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15546               file)
15547       != obstack_object_size (obstack))
15548     error (_("couldn't data write to file"));
15549 }
15550
15551 /* Unlink a file if the argument is not NULL.  */
15552
15553 static void
15554 unlink_if_set (void *p)
15555 {
15556   char **filename = p;
15557   if (*filename)
15558     unlink (*filename);
15559 }
15560
15561 /* A helper struct used when iterating over debug_types.  */
15562 struct signatured_type_index_data
15563 {
15564   struct objfile *objfile;
15565   struct mapped_symtab *symtab;
15566   struct obstack *types_list;
15567   htab_t psyms_seen;
15568   int cu_index;
15569 };
15570
15571 /* A helper function that writes a single signatured_type to an
15572    obstack.  */
15573
15574 static int
15575 write_one_signatured_type (void **slot, void *d)
15576 {
15577   struct signatured_type_index_data *info = d;
15578   struct signatured_type *entry = (struct signatured_type *) *slot;
15579   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15580   struct partial_symtab *psymtab = per_cu->v.psymtab;
15581   gdb_byte val[8];
15582
15583   write_psymbols (info->symtab,
15584                   info->psyms_seen,
15585                   info->objfile->global_psymbols.list
15586                   + psymtab->globals_offset,
15587                   psymtab->n_global_syms, info->cu_index,
15588                   0);
15589   write_psymbols (info->symtab,
15590                   info->psyms_seen,
15591                   info->objfile->static_psymbols.list
15592                   + psymtab->statics_offset,
15593                   psymtab->n_static_syms, info->cu_index,
15594                   1);
15595
15596   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15597   obstack_grow (info->types_list, val, 8);
15598   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15599   obstack_grow (info->types_list, val, 8);
15600   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15601   obstack_grow (info->types_list, val, 8);
15602
15603   ++info->cu_index;
15604
15605   return 1;
15606 }
15607
15608 /* A cleanup function for an htab_t.  */
15609
15610 static void
15611 cleanup_htab (void *arg)
15612 {
15613   htab_delete (arg);
15614 }
15615
15616 /* Create an index file for OBJFILE in the directory DIR.  */
15617
15618 static void
15619 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15620 {
15621   struct cleanup *cleanup;
15622   char *filename, *cleanup_filename;
15623   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15624   struct obstack cu_list, types_cu_list;
15625   int i;
15626   FILE *out_file;
15627   struct mapped_symtab *symtab;
15628   offset_type val, size_of_contents, total_len;
15629   struct stat st;
15630   char buf[8];
15631   htab_t psyms_seen;
15632   htab_t cu_index_htab;
15633   struct psymtab_cu_index_map *psymtab_cu_index_map;
15634
15635   if (!objfile->psymtabs)
15636     return;
15637   if (dwarf2_per_objfile->using_index)
15638     error (_("Cannot use an index to create the index"));
15639
15640   if (stat (objfile->name, &st) < 0)
15641     perror_with_name (objfile->name);
15642
15643   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15644                      INDEX_SUFFIX, (char *) NULL);
15645   cleanup = make_cleanup (xfree, filename);
15646
15647   out_file = fopen (filename, "wb");
15648   if (!out_file)
15649     error (_("Can't open `%s' for writing"), filename);
15650
15651   cleanup_filename = filename;
15652   make_cleanup (unlink_if_set, &cleanup_filename);
15653
15654   symtab = create_mapped_symtab ();
15655   make_cleanup (cleanup_mapped_symtab, symtab);
15656
15657   obstack_init (&addr_obstack);
15658   make_cleanup_obstack_free (&addr_obstack);
15659
15660   obstack_init (&cu_list);
15661   make_cleanup_obstack_free (&cu_list);
15662
15663   obstack_init (&types_cu_list);
15664   make_cleanup_obstack_free (&types_cu_list);
15665
15666   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15667                                   NULL, xcalloc, xfree);
15668   make_cleanup (cleanup_htab, psyms_seen);
15669
15670   /* While we're scanning CU's create a table that maps a psymtab pointer
15671      (which is what addrmap records) to its index (which is what is recorded
15672      in the index file).  This will later be needed to write the address
15673      table.  */
15674   cu_index_htab = htab_create_alloc (100,
15675                                      hash_psymtab_cu_index,
15676                                      eq_psymtab_cu_index,
15677                                      NULL, xcalloc, xfree);
15678   make_cleanup (cleanup_htab, cu_index_htab);
15679   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15680     xmalloc (sizeof (struct psymtab_cu_index_map)
15681              * dwarf2_per_objfile->n_comp_units);
15682   make_cleanup (xfree, psymtab_cu_index_map);
15683
15684   /* The CU list is already sorted, so we don't need to do additional
15685      work here.  Also, the debug_types entries do not appear in
15686      all_comp_units, but only in their own hash table.  */
15687   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15688     {
15689       struct dwarf2_per_cu_data *per_cu
15690         = dwarf2_per_objfile->all_comp_units[i];
15691       struct partial_symtab *psymtab = per_cu->v.psymtab;
15692       gdb_byte val[8];
15693       struct psymtab_cu_index_map *map;
15694       void **slot;
15695
15696       write_psymbols (symtab,
15697                       psyms_seen,
15698                       objfile->global_psymbols.list + psymtab->globals_offset,
15699                       psymtab->n_global_syms, i,
15700                       0);
15701       write_psymbols (symtab,
15702                       psyms_seen,
15703                       objfile->static_psymbols.list + psymtab->statics_offset,
15704                       psymtab->n_static_syms, i,
15705                       1);
15706
15707       map = &psymtab_cu_index_map[i];
15708       map->psymtab = psymtab;
15709       map->cu_index = i;
15710       slot = htab_find_slot (cu_index_htab, map, INSERT);
15711       gdb_assert (slot != NULL);
15712       gdb_assert (*slot == NULL);
15713       *slot = map;
15714
15715       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15716       obstack_grow (&cu_list, val, 8);
15717       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15718       obstack_grow (&cu_list, val, 8);
15719     }
15720
15721   /* Dump the address map.  */
15722   write_address_map (objfile, &addr_obstack, cu_index_htab);
15723
15724   /* Write out the .debug_type entries, if any.  */
15725   if (dwarf2_per_objfile->signatured_types)
15726     {
15727       struct signatured_type_index_data sig_data;
15728
15729       sig_data.objfile = objfile;
15730       sig_data.symtab = symtab;
15731       sig_data.types_list = &types_cu_list;
15732       sig_data.psyms_seen = psyms_seen;
15733       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15734       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15735                               write_one_signatured_type, &sig_data);
15736     }
15737
15738   obstack_init (&constant_pool);
15739   make_cleanup_obstack_free (&constant_pool);
15740   obstack_init (&symtab_obstack);
15741   make_cleanup_obstack_free (&symtab_obstack);
15742   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15743
15744   obstack_init (&contents);
15745   make_cleanup_obstack_free (&contents);
15746   size_of_contents = 6 * sizeof (offset_type);
15747   total_len = size_of_contents;
15748
15749   /* The version number.  */
15750   val = MAYBE_SWAP (4);
15751   obstack_grow (&contents, &val, sizeof (val));
15752
15753   /* The offset of the CU list from the start of the file.  */
15754   val = MAYBE_SWAP (total_len);
15755   obstack_grow (&contents, &val, sizeof (val));
15756   total_len += obstack_object_size (&cu_list);
15757
15758   /* The offset of the types 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 (&types_cu_list);
15762
15763   /* The offset of the address table 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 (&addr_obstack);
15767
15768   /* The offset of the symbol 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 (&symtab_obstack);
15772
15773   /* The offset of the constant pool 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 (&constant_pool);
15777
15778   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15779
15780   write_obstack (out_file, &contents);
15781   write_obstack (out_file, &cu_list);
15782   write_obstack (out_file, &types_cu_list);
15783   write_obstack (out_file, &addr_obstack);
15784   write_obstack (out_file, &symtab_obstack);
15785   write_obstack (out_file, &constant_pool);
15786
15787   fclose (out_file);
15788
15789   /* We want to keep the file, so we set cleanup_filename to NULL
15790      here.  See unlink_if_set.  */
15791   cleanup_filename = NULL;
15792
15793   do_cleanups (cleanup);
15794 }
15795
15796 /* The mapped index file format is designed to be directly mmap()able
15797    on any architecture.  In most cases, a datum is represented using a
15798    little-endian 32-bit integer value, called an offset_type.  Big
15799    endian machines must byte-swap the values before using them.
15800    Exceptions to this rule are noted.  The data is laid out such that
15801    alignment is always respected.
15802
15803    A mapped index consists of several sections.
15804
15805    1. The file header.  This is a sequence of values, of offset_type
15806    unless otherwise noted:
15807
15808    [0] The version number, currently 4.  Versions 1, 2 and 3 are
15809    obsolete.
15810    [1] The offset, from the start of the file, of the CU list.
15811    [2] The offset, from the start of the file, of the types CU list.
15812    Note that this section can be empty, in which case this offset will
15813    be equal to the next offset.
15814    [3] The offset, from the start of the file, of the address section.
15815    [4] The offset, from the start of the file, of the symbol table.
15816    [5] The offset, from the start of the file, of the constant pool.
15817
15818    2. The CU list.  This is a sequence of pairs of 64-bit
15819    little-endian values, sorted by the CU offset.  The first element
15820    in each pair is the offset of a CU in the .debug_info section.  The
15821    second element in each pair is the length of that CU.  References
15822    to a CU elsewhere in the map are done using a CU index, which is
15823    just the 0-based index into this table.  Note that if there are
15824    type CUs, then conceptually CUs and type CUs form a single list for
15825    the purposes of CU indices.
15826
15827    3. The types CU list.  This is a sequence of triplets of 64-bit
15828    little-endian values.  In a triplet, the first value is the CU
15829    offset, the second value is the type offset in the CU, and the
15830    third value is the type signature.  The types CU list is not
15831    sorted.
15832
15833    4. The address section.  The address section consists of a sequence
15834    of address entries.  Each address entry has three elements.
15835    [0] The low address.  This is a 64-bit little-endian value.
15836    [1] The high address.  This is a 64-bit little-endian value.
15837        Like DW_AT_high_pc, the value is one byte beyond the end.
15838    [2] The CU index.  This is an offset_type value.
15839
15840    5. The symbol table.  This is a hash table.  The size of the hash
15841    table is always a power of 2.  The initial hash and the step are
15842    currently defined by the `find_slot' function.
15843
15844    Each slot in the hash table consists of a pair of offset_type
15845    values.  The first value is the offset of the symbol's name in the
15846    constant pool.  The second value is the offset of the CU vector in
15847    the constant pool.
15848
15849    If both values are 0, then this slot in the hash table is empty.
15850    This is ok because while 0 is a valid constant pool index, it
15851    cannot be a valid index for both a string and a CU vector.
15852
15853    A string in the constant pool is stored as a \0-terminated string,
15854    as you'd expect.
15855
15856    A CU vector in the constant pool is a sequence of offset_type
15857    values.  The first value is the number of CU indices in the vector.
15858    Each subsequent value is the index of a CU in the CU list.  This
15859    element in the hash table is used to indicate which CUs define the
15860    symbol.
15861
15862    6. The constant pool.  This is simply a bunch of bytes.  It is
15863    organized so that alignment is correct: CU vectors are stored
15864    first, followed by strings.  */
15865
15866 static void
15867 save_gdb_index_command (char *arg, int from_tty)
15868 {
15869   struct objfile *objfile;
15870
15871   if (!arg || !*arg)
15872     error (_("usage: save gdb-index DIRECTORY"));
15873
15874   ALL_OBJFILES (objfile)
15875   {
15876     struct stat st;
15877
15878     /* If the objfile does not correspond to an actual file, skip it.  */
15879     if (stat (objfile->name, &st) < 0)
15880       continue;
15881
15882     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15883     if (dwarf2_per_objfile)
15884       {
15885         volatile struct gdb_exception except;
15886
15887         TRY_CATCH (except, RETURN_MASK_ERROR)
15888           {
15889             write_psymtabs_to_index (objfile, arg);
15890           }
15891         if (except.reason < 0)
15892           exception_fprintf (gdb_stderr, except,
15893                              _("Error while writing index for `%s': "),
15894                              objfile->name);
15895       }
15896   }
15897 }
15898
15899 \f
15900
15901 int dwarf2_always_disassemble;
15902
15903 static void
15904 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15905                                 struct cmd_list_element *c, const char *value)
15906 {
15907   fprintf_filtered (file,
15908                     _("Whether to always disassemble "
15909                       "DWARF expressions is %s.\n"),
15910                     value);
15911 }
15912
15913 void _initialize_dwarf2_read (void);
15914
15915 void
15916 _initialize_dwarf2_read (void)
15917 {
15918   struct cmd_list_element *c;
15919
15920   dwarf2_objfile_data_key
15921     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15922
15923   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15924 Set DWARF 2 specific variables.\n\
15925 Configure DWARF 2 variables such as the cache size"),
15926                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15927                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15928
15929   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15930 Show DWARF 2 specific variables\n\
15931 Show DWARF 2 variables such as the cache size"),
15932                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15933                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15934
15935   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15936                             &dwarf2_max_cache_age, _("\
15937 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15938 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15939 A higher limit means that cached compilation units will be stored\n\
15940 in memory longer, and more total memory will be used.  Zero disables\n\
15941 caching, which can slow down startup."),
15942                             NULL,
15943                             show_dwarf2_max_cache_age,
15944                             &set_dwarf2_cmdlist,
15945                             &show_dwarf2_cmdlist);
15946
15947   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15948                            &dwarf2_always_disassemble, _("\
15949 Set whether `info address' always disassembles DWARF expressions."), _("\
15950 Show whether `info address' always disassembles DWARF expressions."), _("\
15951 When enabled, DWARF expressions are always printed in an assembly-like\n\
15952 syntax.  When disabled, expressions will be printed in a more\n\
15953 conversational style, when possible."),
15954                            NULL,
15955                            show_dwarf2_always_disassemble,
15956                            &set_dwarf2_cmdlist,
15957                            &show_dwarf2_cmdlist);
15958
15959   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15960 Set debugging of the dwarf2 DIE reader."), _("\
15961 Show debugging of the dwarf2 DIE reader."), _("\
15962 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15963 The value is the maximum depth to print."),
15964                             NULL,
15965                             NULL,
15966                             &setdebuglist, &showdebuglist);
15967
15968   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15969                _("\
15970 Save a gdb-index file.\n\
15971 Usage: save gdb-index DIRECTORY"),
15972                &save_cmdlist);
15973   set_cmd_completer (c, filename_completer);
15974 }