Import gdb 7.3 into vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81    Because of alignment constraints, this structure has padding and cannot
82    be mapped directly onto the beginning of the .debug_info section.  */
83 typedef struct comp_unit_header
84   {
85     unsigned int length;        /* length of the .debug_info
86                                    contribution */
87     unsigned short version;     /* version number -- 2 for DWARF
88                                    version 2 */
89     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90     unsigned char addr_size;    /* byte size of an address -- 4 */
91   }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97    Because of alignment constraints, this structure has padding and cannot
98    be mapped directly onto the beginning of the .debug_info section.  */
99 typedef struct statement_prologue
100   {
101     unsigned int total_length;  /* byte length of the statement
102                                    information */
103     unsigned short version;     /* version number -- 2 for DWARF
104                                    version 2 */
105     unsigned int prologue_length;       /* # bytes between prologue &
106                                            stmt program */
107     unsigned char minimum_instruction_length;   /* byte size of
108                                                    smallest instr */
109     unsigned char default_is_stmt;      /* initial value of is_stmt
110                                            register */
111     char line_base;
112     unsigned char line_range;
113     unsigned char opcode_base;  /* number assigned to first special
114                                    opcode */
115     unsigned char *standard_opcode_lengths;
116   }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in.  */
120 static int dwarf2_die_debug = 0;
121
122 /* When non-zero, cross-check physname against demangler.  */
123 static int check_physname = 0;
124
125 static int pagesize;
126
127 /* When set, the file that we're processing is known to have debugging
128    info for C++ namespaces.  GCC 3.3.x did not produce this information,
129    but later versions do.  */
130
131 static int processing_has_namespace_info;
132
133 static const struct objfile_data *dwarf2_objfile_data_key;
134
135 struct dwarf2_section_info
136 {
137   asection *asection;
138   gdb_byte *buffer;
139   bfd_size_type size;
140   int was_mmapped;
141   /* True if we have tried to read this section.  */
142   int readin;
143 };
144
145 /* All offsets in the index are of this type.  It must be
146    architecture-independent.  */
147 typedef uint32_t offset_type;
148
149 DEF_VEC_I (offset_type);
150
151 /* A description of the mapped index.  The file format is described in
152    a comment by the code that writes the index.  */
153 struct mapped_index
154 {
155   /* The total length of the buffer.  */
156   off_t total_size;
157   /* A pointer to the address table data.  */
158   const gdb_byte *address_table;
159   /* Size of the address table data in bytes.  */
160   offset_type address_table_size;
161   /* The symbol table, implemented as a hash table.  */
162   const offset_type *symbol_table;
163   /* Size in slots, each slot is 2 offset_types.  */
164   offset_type symbol_table_slots;
165   /* A pointer to the constant pool.  */
166   const char *constant_pool;
167 };
168
169 struct dwarf2_per_objfile
170 {
171   struct dwarf2_section_info info;
172   struct dwarf2_section_info abbrev;
173   struct dwarf2_section_info line;
174   struct dwarf2_section_info loc;
175   struct dwarf2_section_info macinfo;
176   struct dwarf2_section_info str;
177   struct dwarf2_section_info ranges;
178   struct dwarf2_section_info types;
179   struct dwarf2_section_info frame;
180   struct dwarf2_section_info eh_frame;
181   struct dwarf2_section_info gdb_index;
182
183   /* Back link.  */
184   struct objfile *objfile;
185
186   /* A list of all the compilation units.  This is used to locate
187      the target compilation unit of a particular reference.  */
188   struct dwarf2_per_cu_data **all_comp_units;
189
190   /* The number of compilation units in ALL_COMP_UNITS.  */
191   int n_comp_units;
192
193   /* The number of .debug_types-related CUs.  */
194   int n_type_comp_units;
195
196   /* The .debug_types-related CUs.  */
197   struct dwarf2_per_cu_data **type_comp_units;
198
199   /* A chain of compilation units that are currently read in, so that
200      they can be freed later.  */
201   struct dwarf2_per_cu_data *read_in_chain;
202
203   /* A table mapping .debug_types signatures to its signatured_type entry.
204      This is NULL if the .debug_types section hasn't been read in yet.  */
205   htab_t signatured_types;
206
207   /* A flag indicating wether this objfile has a section loaded at a
208      VMA of 0.  */
209   int has_section_at_zero;
210
211   /* True if we are using the mapped index,
212      or we are faking it for OBJF_READNOW's sake.  */
213   unsigned char using_index;
214
215   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
216   struct mapped_index *index_table;
217
218   /* When using index_table, this keeps track of all quick_file_names entries.
219      TUs can share line table entries with CUs or other TUs, and there can be
220      a lot more TUs than unique line tables, so we maintain a separate table
221      of all line table entries to support the sharing.  */
222   htab_t quick_file_names_table;
223
224   /* Set during partial symbol reading, to prevent queueing of full
225      symbols.  */
226   int reading_partial_symbols;
227
228   /* Table mapping type .debug_info DIE offsets to types.
229      This is NULL if not allocated yet.
230      It (currently) makes sense to allocate debug_types_type_hash lazily.
231      To keep things simple we allocate both lazily.  */
232   htab_t debug_info_type_hash;
233
234   /* Table mapping type .debug_types DIE offsets to types.
235      This is NULL if not allocated yet.  */
236   htab_t debug_types_type_hash;
237 };
238
239 static struct dwarf2_per_objfile *dwarf2_per_objfile;
240
241 /* names of the debugging sections */
242
243 /* Note that if the debugging section has been compressed, it might
244    have a name like .zdebug_info.  */
245
246 #define INFO_SECTION     "debug_info"
247 #define ABBREV_SECTION   "debug_abbrev"
248 #define LINE_SECTION     "debug_line"
249 #define LOC_SECTION      "debug_loc"
250 #define MACINFO_SECTION  "debug_macinfo"
251 #define STR_SECTION      "debug_str"
252 #define RANGES_SECTION   "debug_ranges"
253 #define TYPES_SECTION    "debug_types"
254 #define FRAME_SECTION    "debug_frame"
255 #define EH_FRAME_SECTION "eh_frame"
256 #define GDB_INDEX_SECTION "gdb_index"
257
258 /* local data types */
259
260 /* We hold several abbreviation tables in memory at the same time.  */
261 #ifndef ABBREV_HASH_SIZE
262 #define ABBREV_HASH_SIZE 121
263 #endif
264
265 /* The data in a compilation unit header, after target2host
266    translation, looks like this.  */
267 struct comp_unit_head
268 {
269   unsigned int length;
270   short version;
271   unsigned char addr_size;
272   unsigned char signed_addr_p;
273   unsigned int abbrev_offset;
274
275   /* Size of file offsets; either 4 or 8.  */
276   unsigned int offset_size;
277
278   /* Size of the length field; either 4 or 12.  */
279   unsigned int initial_length_size;
280
281   /* Offset to the first byte of this compilation unit header in the
282      .debug_info section, for resolving relative reference dies.  */
283   unsigned int offset;
284
285   /* Offset to first die in this cu from the start of the cu.
286      This will be the first byte following the compilation unit header.  */
287   unsigned int first_die_offset;
288 };
289
290 /* Type used for delaying computation of method physnames.
291    See comments for compute_delayed_physnames.  */
292 struct delayed_method_info
293 {
294   /* The type to which the method is attached, i.e., its parent class.  */
295   struct type *type;
296
297   /* The index of the method in the type's function fieldlists.  */
298   int fnfield_index;
299
300   /* The index of the method in the fieldlist.  */
301   int index;
302
303   /* The name of the DIE.  */
304   const char *name;
305
306   /*  The DIE associated with this method.  */
307   struct die_info *die;
308 };
309
310 typedef struct delayed_method_info delayed_method_info;
311 DEF_VEC_O (delayed_method_info);
312
313 /* Internal state when decoding a particular compilation unit.  */
314 struct dwarf2_cu
315 {
316   /* The objfile containing this compilation unit.  */
317   struct objfile *objfile;
318
319   /* The header of the compilation unit.  */
320   struct comp_unit_head header;
321
322   /* Base address of this compilation unit.  */
323   CORE_ADDR base_address;
324
325   /* Non-zero if base_address has been set.  */
326   int base_known;
327
328   struct function_range *first_fn, *last_fn, *cached_fn;
329
330   /* The language we are debugging.  */
331   enum language language;
332   const struct language_defn *language_defn;
333
334   const char *producer;
335
336   /* The generic symbol table building routines have separate lists for
337      file scope symbols and all all other scopes (local scopes).  So
338      we need to select the right one to pass to add_symbol_to_list().
339      We do it by keeping a pointer to the correct list in list_in_scope.
340
341      FIXME: The original dwarf code just treated the file scope as the
342      first local scope, and all other local scopes as nested local
343      scopes, and worked fine.  Check to see if we really need to
344      distinguish these in buildsym.c.  */
345   struct pending **list_in_scope;
346
347   /* DWARF abbreviation table associated with this compilation unit.  */
348   struct abbrev_info **dwarf2_abbrevs;
349
350   /* Storage for the abbrev table.  */
351   struct obstack abbrev_obstack;
352
353   /* Hash table holding all the loaded partial DIEs.  */
354   htab_t partial_dies;
355
356   /* Storage for things with the same lifetime as this read-in compilation
357      unit, including partial DIEs.  */
358   struct obstack comp_unit_obstack;
359
360   /* When multiple dwarf2_cu structures are living in memory, this field
361      chains them all together, so that they can be released efficiently.
362      We will probably also want a generation counter so that most-recently-used
363      compilation units are cached...  */
364   struct dwarf2_per_cu_data *read_in_chain;
365
366   /* Backchain to our per_cu entry if the tree has been built.  */
367   struct dwarf2_per_cu_data *per_cu;
368
369   /* How many compilation units ago was this CU last referenced?  */
370   int last_used;
371
372   /* A hash table of die offsets for following references.  */
373   htab_t die_hash;
374
375   /* Full DIEs if read in.  */
376   struct die_info *dies;
377
378   /* A set of pointers to dwarf2_per_cu_data objects for compilation
379      units referenced by this one.  Only set during full symbol processing;
380      partial symbol tables do not have dependencies.  */
381   htab_t dependencies;
382
383   /* Header data from the line table, during full symbol processing.  */
384   struct line_header *line_header;
385
386   /* A list of methods which need to have physnames computed
387      after all type information has been read.  */
388   VEC (delayed_method_info) *method_list;
389
390   /* Mark used when releasing cached dies.  */
391   unsigned int mark : 1;
392
393   /* This flag will be set if this compilation unit might include
394      inter-compilation-unit references.  */
395   unsigned int has_form_ref_addr : 1;
396
397   /* This flag will be set if this compilation unit includes any
398      DW_TAG_namespace DIEs.  If we know that there are explicit
399      DIEs for namespaces, we don't need to try to infer them
400      from mangled names.  */
401   unsigned int has_namespace_info : 1;
402
403   /* This CU references .debug_loc.  See the symtab->locations_valid field.
404      This test is imperfect as there may exist optimized debug code not using
405      any location list and still facing inlining issues if handled as
406      unoptimized code.  For a future better test see GCC PR other/32998.  */
407
408   unsigned int has_loclist : 1;
409 };
410
411 /* Persistent data held for a compilation unit, even when not
412    processing it.  We put a pointer to this structure in the
413    read_symtab_private field of the psymtab.  If we encounter
414    inter-compilation-unit references, we also maintain a sorted
415    list of all compilation units.  */
416
417 struct dwarf2_per_cu_data
418 {
419   /* The start offset and length of this compilation unit.  2**29-1
420      bytes should suffice to store the length of any compilation unit
421      - if it doesn't, GDB will fall over anyway.
422      NOTE: Unlike comp_unit_head.length, this length includes
423      initial_length_size.  */
424   unsigned int offset;
425   unsigned int length : 29;
426
427   /* Flag indicating this compilation unit will be read in before
428      any of the current compilation units are processed.  */
429   unsigned int queued : 1;
430
431   /* This flag will be set if we need to load absolutely all DIEs
432      for this compilation unit, instead of just the ones we think
433      are interesting.  It gets set if we look for a DIE in the
434      hash table and don't find it.  */
435   unsigned int load_all_dies : 1;
436
437   /* Non-zero if this CU is from .debug_types.
438      Otherwise it's from .debug_info.  */
439   unsigned int from_debug_types : 1;
440
441   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
442      of the CU cache it gets reset to NULL again.  */
443   struct dwarf2_cu *cu;
444
445   /* The corresponding objfile.  */
446   struct objfile *objfile;
447
448   /* When using partial symbol tables, the 'psymtab' field is active.
449      Otherwise the 'quick' field is active.  */
450   union
451   {
452     /* The partial symbol table associated with this compilation unit,
453        or NULL for partial units (which do not have an associated
454        symtab).  */
455     struct partial_symtab *psymtab;
456
457     /* Data needed by the "quick" functions.  */
458     struct dwarf2_per_cu_quick_data *quick;
459   } v;
460 };
461
462 /* Entry in the signatured_types hash table.  */
463
464 struct signatured_type
465 {
466   ULONGEST signature;
467
468   /* Offset in .debug_types of the TU (type_unit) for this type.  */
469   unsigned int offset;
470
471   /* Offset in .debug_types of the type defined by this TU.  */
472   unsigned int type_offset;
473
474   /* The CU(/TU) of this type.  */
475   struct dwarf2_per_cu_data per_cu;
476 };
477
478 /* Struct used to pass misc. parameters to read_die_and_children, et
479    al.  which are used for both .debug_info and .debug_types dies.
480    All parameters here are unchanging for the life of the call.  This
481    struct exists to abstract away the constant parameters of die
482    reading.  */
483
484 struct die_reader_specs
485 {
486   /* The bfd of this objfile.  */
487   bfd* abfd;
488
489   /* The CU of the DIE we are parsing.  */
490   struct dwarf2_cu *cu;
491
492   /* Pointer to start of section buffer.
493      This is either the start of .debug_info or .debug_types.  */
494   const gdb_byte *buffer;
495 };
496
497 /* The line number information for a compilation unit (found in the
498    .debug_line section) begins with a "statement program header",
499    which contains the following information.  */
500 struct line_header
501 {
502   unsigned int total_length;
503   unsigned short version;
504   unsigned int header_length;
505   unsigned char minimum_instruction_length;
506   unsigned char maximum_ops_per_instruction;
507   unsigned char default_is_stmt;
508   int line_base;
509   unsigned char line_range;
510   unsigned char opcode_base;
511
512   /* standard_opcode_lengths[i] is the number of operands for the
513      standard opcode whose value is i.  This means that
514      standard_opcode_lengths[0] is unused, and the last meaningful
515      element is standard_opcode_lengths[opcode_base - 1].  */
516   unsigned char *standard_opcode_lengths;
517
518   /* The include_directories table.  NOTE!  These strings are not
519      allocated with xmalloc; instead, they are pointers into
520      debug_line_buffer.  If you try to free them, `free' will get
521      indigestion.  */
522   unsigned int num_include_dirs, include_dirs_size;
523   char **include_dirs;
524
525   /* The file_names table.  NOTE!  These strings are not allocated
526      with xmalloc; instead, they are pointers into debug_line_buffer.
527      Don't try to free them directly.  */
528   unsigned int num_file_names, file_names_size;
529   struct file_entry
530   {
531     char *name;
532     unsigned int dir_index;
533     unsigned int mod_time;
534     unsigned int length;
535     int included_p; /* Non-zero if referenced by the Line Number Program.  */
536     struct symtab *symtab; /* The associated symbol table, if any.  */
537   } *file_names;
538
539   /* The start and end of the statement program following this
540      header.  These point into dwarf2_per_objfile->line_buffer.  */
541   gdb_byte *statement_program_start, *statement_program_end;
542 };
543
544 /* When we construct a partial symbol table entry we only
545    need this much information.  */
546 struct partial_die_info
547   {
548     /* Offset of this DIE.  */
549     unsigned int offset;
550
551     /* DWARF-2 tag for this DIE.  */
552     ENUM_BITFIELD(dwarf_tag) tag : 16;
553
554     /* Assorted flags describing the data found in this DIE.  */
555     unsigned int has_children : 1;
556     unsigned int is_external : 1;
557     unsigned int is_declaration : 1;
558     unsigned int has_type : 1;
559     unsigned int has_specification : 1;
560     unsigned int has_pc_info : 1;
561
562     /* Flag set if the SCOPE field of this structure has been
563        computed.  */
564     unsigned int scope_set : 1;
565
566     /* Flag set if the DIE has a byte_size attribute.  */
567     unsigned int has_byte_size : 1;
568
569     /* Flag set if any of the DIE's children are template arguments.  */
570     unsigned int has_template_arguments : 1;
571
572     /* Flag set if fixup_partial_die has been called on this die.  */
573     unsigned int fixup_called : 1;
574
575     /* The name of this DIE.  Normally the value of DW_AT_name, but
576        sometimes a default name for unnamed DIEs.  */
577     char *name;
578
579     /* The linkage name, if present.  */
580     const char *linkage_name;
581
582     /* The scope to prepend to our children.  This is generally
583        allocated on the comp_unit_obstack, so will disappear
584        when this compilation unit leaves the cache.  */
585     char *scope;
586
587     /* The location description associated with this DIE, if any.  */
588     struct dwarf_block *locdesc;
589
590     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
591     CORE_ADDR lowpc;
592     CORE_ADDR highpc;
593
594     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
595        DW_AT_sibling, if any.  */
596     /* NOTE: This member isn't strictly necessary, read_partial_die could
597        return DW_AT_sibling values to its caller load_partial_dies.  */
598     gdb_byte *sibling;
599
600     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601        DW_AT_specification (or DW_AT_abstract_origin or
602        DW_AT_extension).  */
603     unsigned int spec_offset;
604
605     /* Pointers to this DIE's parent, first child, and next sibling,
606        if any.  */
607     struct partial_die_info *die_parent, *die_child, *die_sibling;
608   };
609
610 /* This data structure holds the information of an abbrev.  */
611 struct abbrev_info
612   {
613     unsigned int number;        /* number identifying abbrev */
614     enum dwarf_tag tag;         /* dwarf tag */
615     unsigned short has_children;                /* boolean */
616     unsigned short num_attrs;   /* number of attributes */
617     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
618     struct abbrev_info *next;   /* next in chain */
619   };
620
621 struct attr_abbrev
622   {
623     ENUM_BITFIELD(dwarf_attribute) name : 16;
624     ENUM_BITFIELD(dwarf_form) form : 16;
625   };
626
627 /* Attributes have a name and a value.  */
628 struct attribute
629   {
630     ENUM_BITFIELD(dwarf_attribute) name : 16;
631     ENUM_BITFIELD(dwarf_form) form : 15;
632
633     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
634        field should be in u.str (existing only for DW_STRING) but it is kept
635        here for better struct attribute alignment.  */
636     unsigned int string_is_canonical : 1;
637
638     union
639       {
640         char *str;
641         struct dwarf_block *blk;
642         ULONGEST unsnd;
643         LONGEST snd;
644         CORE_ADDR addr;
645         struct signatured_type *signatured_type;
646       }
647     u;
648   };
649
650 /* This data structure holds a complete die structure.  */
651 struct die_info
652   {
653     /* DWARF-2 tag for this DIE.  */
654     ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656     /* Number of attributes */
657     unsigned char num_attrs;
658
659     /* True if we're presently building the full type name for the
660        type derived from this DIE.  */
661     unsigned char building_fullname : 1;
662
663     /* Abbrev number */
664     unsigned int abbrev;
665
666     /* Offset in .debug_info or .debug_types section.  */
667     unsigned int offset;
668
669     /* The dies in a compilation unit form an n-ary tree.  PARENT
670        points to this die's parent; CHILD points to the first child of
671        this node; and all the children of a given node are chained
672        together via their SIBLING fields.  */
673     struct die_info *child;     /* Its first child, if any.  */
674     struct die_info *sibling;   /* Its next sibling, if any.  */
675     struct die_info *parent;    /* Its parent, if any.  */
676
677     /* An array of attributes, with NUM_ATTRS elements.  There may be
678        zero, but it's not common and zero-sized arrays are not
679        sufficiently portable C.  */
680     struct attribute attrs[1];
681   };
682
683 struct function_range
684 {
685   const char *name;
686   CORE_ADDR lowpc, highpc;
687   int seen_line;
688   struct function_range *next;
689 };
690
691 /* Get at parts of an attribute structure.  */
692
693 #define DW_STRING(attr)    ((attr)->u.str)
694 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
695 #define DW_UNSND(attr)     ((attr)->u.unsnd)
696 #define DW_BLOCK(attr)     ((attr)->u.blk)
697 #define DW_SND(attr)       ((attr)->u.snd)
698 #define DW_ADDR(attr)      ((attr)->u.addr)
699 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
700
701 /* Blocks are a bunch of untyped bytes.  */
702 struct dwarf_block
703   {
704     unsigned int size;
705     gdb_byte *data;
706   };
707
708 #ifndef ATTR_ALLOC_CHUNK
709 #define ATTR_ALLOC_CHUNK 4
710 #endif
711
712 /* Allocate fields for structs, unions and enums in this size.  */
713 #ifndef DW_FIELD_ALLOC_CHUNK
714 #define DW_FIELD_ALLOC_CHUNK 4
715 #endif
716
717 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
718    but this would require a corresponding change in unpack_field_as_long
719    and friends.  */
720 static int bits_per_byte = 8;
721
722 /* The routines that read and process dies for a C struct or C++ class
723    pass lists of data member fields and lists of member function fields
724    in an instance of a field_info structure, as defined below.  */
725 struct field_info
726   {
727     /* List of data member and baseclasses fields.  */
728     struct nextfield
729       {
730         struct nextfield *next;
731         int accessibility;
732         int virtuality;
733         struct field field;
734       }
735      *fields, *baseclasses;
736
737     /* Number of fields (including baseclasses).  */
738     int nfields;
739
740     /* Number of baseclasses.  */
741     int nbaseclasses;
742
743     /* Set if the accesibility of one of the fields is not public.  */
744     int non_public_fields;
745
746     /* Member function fields array, entries are allocated in the order they
747        are encountered in the object file.  */
748     struct nextfnfield
749       {
750         struct nextfnfield *next;
751         struct fn_field fnfield;
752       }
753      *fnfields;
754
755     /* Member function fieldlist array, contains name of possibly overloaded
756        member function, number of overloaded member functions and a pointer
757        to the head of the member function field chain.  */
758     struct fnfieldlist
759       {
760         char *name;
761         int length;
762         struct nextfnfield *head;
763       }
764      *fnfieldlists;
765
766     /* Number of entries in the fnfieldlists array.  */
767     int nfnfields;
768
769     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
770        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
771     struct typedef_field_list
772       {
773         struct typedef_field field;
774         struct typedef_field_list *next;
775       }
776     *typedef_field_list;
777     unsigned typedef_field_list_count;
778   };
779
780 /* One item on the queue of compilation units to read in full symbols
781    for.  */
782 struct dwarf2_queue_item
783 {
784   struct dwarf2_per_cu_data *per_cu;
785   struct dwarf2_queue_item *next;
786 };
787
788 /* The current queue.  */
789 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
790
791 /* Loaded secondary compilation units are kept in memory until they
792    have not been referenced for the processing of this many
793    compilation units.  Set this to zero to disable caching.  Cache
794    sizes of up to at least twenty will improve startup time for
795    typical inter-CU-reference binaries, at an obvious memory cost.  */
796 static int dwarf2_max_cache_age = 5;
797 static void
798 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
799                            struct cmd_list_element *c, const char *value)
800 {
801   fprintf_filtered (file, _("The upper bound on the age of cached "
802                             "dwarf2 compilation units is %s.\n"),
803                     value);
804 }
805
806
807 /* Various complaints about symbol reading that don't abort the process.  */
808
809 static void
810 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
811 {
812   complaint (&symfile_complaints,
813              _("statement list doesn't fit in .debug_line section"));
814 }
815
816 static void
817 dwarf2_debug_line_missing_file_complaint (void)
818 {
819   complaint (&symfile_complaints,
820              _(".debug_line section has line data without a file"));
821 }
822
823 static void
824 dwarf2_debug_line_missing_end_sequence_complaint (void)
825 {
826   complaint (&symfile_complaints,
827              _(".debug_line section has line "
828                "program sequence without an end"));
829 }
830
831 static void
832 dwarf2_complex_location_expr_complaint (void)
833 {
834   complaint (&symfile_complaints, _("location expression too complex"));
835 }
836
837 static void
838 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839                                               int arg3)
840 {
841   complaint (&symfile_complaints,
842              _("const value length mismatch for '%s', got %d, expected %d"),
843              arg1, arg2, arg3);
844 }
845
846 static void
847 dwarf2_macros_too_long_complaint (void)
848 {
849   complaint (&symfile_complaints,
850              _("macro info runs off end of `.debug_macinfo' section"));
851 }
852
853 static void
854 dwarf2_macro_malformed_definition_complaint (const char *arg1)
855 {
856   complaint (&symfile_complaints,
857              _("macro debug info contains a "
858                "malformed macro definition:\n`%s'"),
859              arg1);
860 }
861
862 static void
863 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
864 {
865   complaint (&symfile_complaints,
866              _("invalid attribute class or form for '%s' in '%s'"),
867              arg1, arg2);
868 }
869
870 /* local function prototypes */
871
872 static void dwarf2_locate_sections (bfd *, asection *, void *);
873
874 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
875                                            struct objfile *);
876
877 static void dwarf2_build_psymtabs_hard (struct objfile *);
878
879 static void scan_partial_symbols (struct partial_die_info *,
880                                   CORE_ADDR *, CORE_ADDR *,
881                                   int, struct dwarf2_cu *);
882
883 static void add_partial_symbol (struct partial_die_info *,
884                                 struct dwarf2_cu *);
885
886 static void add_partial_namespace (struct partial_die_info *pdi,
887                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
888                                    int need_pc, struct dwarf2_cu *cu);
889
890 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
891                                 CORE_ADDR *highpc, int need_pc,
892                                 struct dwarf2_cu *cu);
893
894 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
895                                      struct dwarf2_cu *cu);
896
897 static void add_partial_subprogram (struct partial_die_info *pdi,
898                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
899                                     int need_pc, struct dwarf2_cu *cu);
900
901 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
902                                      gdb_byte *buffer, gdb_byte *info_ptr,
903                                      bfd *abfd, struct dwarf2_cu *cu);
904
905 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
906
907 static void psymtab_to_symtab_1 (struct partial_symtab *);
908
909 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
910
911 static void dwarf2_free_abbrev_table (void *);
912
913 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
914                                             struct dwarf2_cu *);
915
916 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
917                                                  struct dwarf2_cu *);
918
919 static struct partial_die_info *load_partial_dies (bfd *,
920                                                    gdb_byte *, gdb_byte *,
921                                                    int, struct dwarf2_cu *);
922
923 static gdb_byte *read_partial_die (struct partial_die_info *,
924                                    struct abbrev_info *abbrev,
925                                    unsigned int, bfd *,
926                                    gdb_byte *, gdb_byte *,
927                                    struct dwarf2_cu *);
928
929 static struct partial_die_info *find_partial_die (unsigned int,
930                                                   struct dwarf2_cu *);
931
932 static void fixup_partial_die (struct partial_die_info *,
933                                struct dwarf2_cu *);
934
935 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
936                                  bfd *, gdb_byte *, struct dwarf2_cu *);
937
938 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
939                                        bfd *, gdb_byte *, struct dwarf2_cu *);
940
941 static unsigned int read_1_byte (bfd *, gdb_byte *);
942
943 static int read_1_signed_byte (bfd *, gdb_byte *);
944
945 static unsigned int read_2_bytes (bfd *, gdb_byte *);
946
947 static unsigned int read_4_bytes (bfd *, gdb_byte *);
948
949 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
950
951 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
952                                unsigned int *);
953
954 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
955
956 static LONGEST read_checked_initial_length_and_offset
957   (bfd *, gdb_byte *, const struct comp_unit_head *,
958    unsigned int *, unsigned int *);
959
960 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
961                             unsigned int *);
962
963 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
964
965 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
966
967 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
968
969 static char *read_indirect_string (bfd *, gdb_byte *,
970                                    const struct comp_unit_head *,
971                                    unsigned int *);
972
973 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
974
975 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
976
977 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
978
979 static void set_cu_language (unsigned int, struct dwarf2_cu *);
980
981 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
982                                       struct dwarf2_cu *);
983
984 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
985                                                 unsigned int,
986                                                 struct dwarf2_cu *);
987
988 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
989                                struct dwarf2_cu *cu);
990
991 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
992
993 static struct die_info *die_specification (struct die_info *die,
994                                            struct dwarf2_cu **);
995
996 static void free_line_header (struct line_header *lh);
997
998 static void add_file_name (struct line_header *, char *, unsigned int,
999                            unsigned int, unsigned int);
1000
1001 static struct line_header *(dwarf_decode_line_header
1002                             (unsigned int offset,
1003                              bfd *abfd, struct dwarf2_cu *cu));
1004
1005 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1006                                 struct dwarf2_cu *, struct partial_symtab *);
1007
1008 static void dwarf2_start_subfile (char *, const char *, const char *);
1009
1010 static struct symbol *new_symbol (struct die_info *, struct type *,
1011                                   struct dwarf2_cu *);
1012
1013 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1014                                        struct dwarf2_cu *, struct symbol *);
1015
1016 static void dwarf2_const_value (struct attribute *, struct symbol *,
1017                                 struct dwarf2_cu *);
1018
1019 static void dwarf2_const_value_attr (struct attribute *attr,
1020                                      struct type *type,
1021                                      const char *name,
1022                                      struct obstack *obstack,
1023                                      struct dwarf2_cu *cu, long *value,
1024                                      gdb_byte **bytes,
1025                                      struct dwarf2_locexpr_baton **baton);
1026
1027 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1028
1029 static int need_gnat_info (struct dwarf2_cu *);
1030
1031 static struct type *die_descriptive_type (struct die_info *,
1032                                           struct dwarf2_cu *);
1033
1034 static void set_descriptive_type (struct type *, struct die_info *,
1035                                   struct dwarf2_cu *);
1036
1037 static struct type *die_containing_type (struct die_info *,
1038                                          struct dwarf2_cu *);
1039
1040 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1041                                      struct dwarf2_cu *);
1042
1043 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1044
1045 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1046
1047 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1048
1049 static char *typename_concat (struct obstack *obs, const char *prefix,
1050                               const char *suffix, int physname,
1051                               struct dwarf2_cu *cu);
1052
1053 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1054
1055 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1056
1057 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1062                                struct dwarf2_cu *, struct partial_symtab *);
1063
1064 static int dwarf2_get_pc_bounds (struct die_info *,
1065                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1066                                  struct partial_symtab *);
1067
1068 static void get_scope_pc_bounds (struct die_info *,
1069                                  CORE_ADDR *, CORE_ADDR *,
1070                                  struct dwarf2_cu *);
1071
1072 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1073                                         CORE_ADDR, struct dwarf2_cu *);
1074
1075 static void dwarf2_add_field (struct field_info *, struct die_info *,
1076                               struct dwarf2_cu *);
1077
1078 static void dwarf2_attach_fields_to_type (struct field_info *,
1079                                           struct type *, struct dwarf2_cu *);
1080
1081 static void dwarf2_add_member_fn (struct field_info *,
1082                                   struct die_info *, struct type *,
1083                                   struct dwarf2_cu *);
1084
1085 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1086                                              struct type *,
1087                                              struct dwarf2_cu *);
1088
1089 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1090
1091 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1092
1093 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1094
1095 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1096
1097 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1098
1099 static struct type *read_module_type (struct die_info *die,
1100                                       struct dwarf2_cu *cu);
1101
1102 static const char *namespace_name (struct die_info *die,
1103                                    int *is_anonymous, struct dwarf2_cu *);
1104
1105 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1106
1107 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1108
1109 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1110                                                        struct dwarf2_cu *);
1111
1112 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1113
1114 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1115                                                  gdb_byte *info_ptr,
1116                                                  gdb_byte **new_info_ptr,
1117                                                  struct die_info *parent);
1118
1119 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1120                                                gdb_byte *info_ptr,
1121                                                gdb_byte **new_info_ptr,
1122                                                struct die_info *parent);
1123
1124 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1125                                                gdb_byte *info_ptr,
1126                                                gdb_byte **new_info_ptr,
1127                                                struct die_info *parent);
1128
1129 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1130                                 struct die_info **, gdb_byte *,
1131                                 int *);
1132
1133 static void process_die (struct die_info *, struct dwarf2_cu *);
1134
1135 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1136                                        struct obstack *);
1137
1138 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1139
1140 static const char *dwarf2_full_name (char *name,
1141                                      struct die_info *die,
1142                                      struct dwarf2_cu *cu);
1143
1144 static struct die_info *dwarf2_extension (struct die_info *die,
1145                                           struct dwarf2_cu **);
1146
1147 static char *dwarf_tag_name (unsigned int);
1148
1149 static char *dwarf_attr_name (unsigned int);
1150
1151 static char *dwarf_form_name (unsigned int);
1152
1153 static char *dwarf_bool_name (unsigned int);
1154
1155 static char *dwarf_type_encoding_name (unsigned int);
1156
1157 #if 0
1158 static char *dwarf_cfi_name (unsigned int);
1159 #endif
1160
1161 static struct die_info *sibling_die (struct die_info *);
1162
1163 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1164
1165 static void dump_die_for_error (struct die_info *);
1166
1167 static void dump_die_1 (struct ui_file *, int level, int max_level,
1168                         struct die_info *);
1169
1170 /*static*/ void dump_die (struct die_info *, int max_level);
1171
1172 static void store_in_ref_table (struct die_info *,
1173                                 struct dwarf2_cu *);
1174
1175 static int is_ref_attr (struct attribute *);
1176
1177 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1178
1179 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1180
1181 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1182                                                struct attribute *,
1183                                                struct dwarf2_cu **);
1184
1185 static struct die_info *follow_die_ref (struct die_info *,
1186                                         struct attribute *,
1187                                         struct dwarf2_cu **);
1188
1189 static struct die_info *follow_die_sig (struct die_info *,
1190                                         struct attribute *,
1191                                         struct dwarf2_cu **);
1192
1193 static void read_signatured_type_at_offset (struct objfile *objfile,
1194                                             unsigned int offset);
1195
1196 static void read_signatured_type (struct objfile *,
1197                                   struct signatured_type *type_sig);
1198
1199 /* memory allocation interface */
1200
1201 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1202
1203 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1204
1205 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1206
1207 static void initialize_cu_func_list (struct dwarf2_cu *);
1208
1209 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1210                                  struct dwarf2_cu *);
1211
1212 static void dwarf_decode_macros (struct line_header *, unsigned int,
1213                                  char *, bfd *, struct dwarf2_cu *);
1214
1215 static int attr_form_is_block (struct attribute *);
1216
1217 static int attr_form_is_section_offset (struct attribute *);
1218
1219 static int attr_form_is_constant (struct attribute *);
1220
1221 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1222                                    struct dwarf2_loclist_baton *baton,
1223                                    struct attribute *attr);
1224
1225 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1226                                          struct symbol *sym,
1227                                          struct dwarf2_cu *cu);
1228
1229 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1230                                struct abbrev_info *abbrev,
1231                                struct dwarf2_cu *cu);
1232
1233 static void free_stack_comp_unit (void *);
1234
1235 static hashval_t partial_die_hash (const void *item);
1236
1237 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1238
1239 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1240   (unsigned int offset, struct objfile *objfile);
1241
1242 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1243   (unsigned int offset, struct objfile *objfile);
1244
1245 static void init_one_comp_unit (struct dwarf2_cu *cu,
1246                                 struct objfile *objfile);
1247
1248 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1249                                    struct die_info *comp_unit_die);
1250
1251 static void free_one_comp_unit (void *);
1252
1253 static void free_cached_comp_units (void *);
1254
1255 static void age_cached_comp_units (void);
1256
1257 static void free_one_cached_comp_unit (void *);
1258
1259 static struct type *set_die_type (struct die_info *, struct type *,
1260                                   struct dwarf2_cu *);
1261
1262 static void create_all_comp_units (struct objfile *);
1263
1264 static int create_debug_types_hash_table (struct objfile *objfile);
1265
1266 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1267                                  struct objfile *);
1268
1269 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1270
1271 static void dwarf2_add_dependence (struct dwarf2_cu *,
1272                                    struct dwarf2_per_cu_data *);
1273
1274 static void dwarf2_mark (struct dwarf2_cu *);
1275
1276 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1277
1278 static struct type *get_die_type_at_offset (unsigned int,
1279                                             struct dwarf2_per_cu_data *per_cu);
1280
1281 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1282
1283 static void dwarf2_release_queue (void *dummy);
1284
1285 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1286                              struct objfile *objfile);
1287
1288 static void process_queue (struct objfile *objfile);
1289
1290 static void find_file_and_directory (struct die_info *die,
1291                                      struct dwarf2_cu *cu,
1292                                      char **name, char **comp_dir);
1293
1294 static char *file_full_name (int file, struct line_header *lh,
1295                              const char *comp_dir);
1296
1297 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1298                                               gdb_byte *info_ptr,
1299                                               gdb_byte *buffer,
1300                                               unsigned int buffer_size,
1301                                               bfd *abfd);
1302
1303 static void init_cu_die_reader (struct die_reader_specs *reader,
1304                                 struct dwarf2_cu *cu);
1305
1306 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1307
1308 #if WORDS_BIGENDIAN
1309
1310 /* Convert VALUE between big- and little-endian.  */
1311 static offset_type
1312 byte_swap (offset_type value)
1313 {
1314   offset_type result;
1315
1316   result = (value & 0xff) << 24;
1317   result |= (value & 0xff00) << 8;
1318   result |= (value & 0xff0000) >> 8;
1319   result |= (value & 0xff000000) >> 24;
1320   return result;
1321 }
1322
1323 #define MAYBE_SWAP(V)  byte_swap (V)
1324
1325 #else
1326 #define MAYBE_SWAP(V) (V)
1327 #endif /* WORDS_BIGENDIAN */
1328
1329 /* The suffix for an index file.  */
1330 #define INDEX_SUFFIX ".gdb-index"
1331
1332 static const char *dwarf2_physname (char *name, struct die_info *die,
1333                                     struct dwarf2_cu *cu);
1334
1335 /* Try to locate the sections we need for DWARF 2 debugging
1336    information and return true if we have enough to do something.  */
1337
1338 int
1339 dwarf2_has_info (struct objfile *objfile)
1340 {
1341   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1342   if (!dwarf2_per_objfile)
1343     {
1344       /* Initialize per-objfile state.  */
1345       struct dwarf2_per_objfile *data
1346         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1347
1348       memset (data, 0, sizeof (*data));
1349       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1350       dwarf2_per_objfile = data;
1351
1352       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1353       dwarf2_per_objfile->objfile = objfile;
1354     }
1355   return (dwarf2_per_objfile->info.asection != NULL
1356           && dwarf2_per_objfile->abbrev.asection != NULL);
1357 }
1358
1359 /* When loading sections, we can either look for ".<name>", or for
1360  * ".z<name>", which indicates a compressed section.  */
1361
1362 static int
1363 section_is_p (const char *section_name, const char *name)
1364 {
1365   return (section_name[0] == '.'
1366           && (strcmp (section_name + 1, name) == 0
1367               || (section_name[1] == 'z'
1368                   && strcmp (section_name + 2, name) == 0)));
1369 }
1370
1371 /* This function is mapped across the sections and remembers the
1372    offset and size of each of the debugging sections we are interested
1373    in.  */
1374
1375 static void
1376 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1377 {
1378   if (section_is_p (sectp->name, INFO_SECTION))
1379     {
1380       dwarf2_per_objfile->info.asection = sectp;
1381       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1382     }
1383   else if (section_is_p (sectp->name, ABBREV_SECTION))
1384     {
1385       dwarf2_per_objfile->abbrev.asection = sectp;
1386       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1387     }
1388   else if (section_is_p (sectp->name, LINE_SECTION))
1389     {
1390       dwarf2_per_objfile->line.asection = sectp;
1391       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1392     }
1393   else if (section_is_p (sectp->name, LOC_SECTION))
1394     {
1395       dwarf2_per_objfile->loc.asection = sectp;
1396       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1397     }
1398   else if (section_is_p (sectp->name, MACINFO_SECTION))
1399     {
1400       dwarf2_per_objfile->macinfo.asection = sectp;
1401       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1402     }
1403   else if (section_is_p (sectp->name, STR_SECTION))
1404     {
1405       dwarf2_per_objfile->str.asection = sectp;
1406       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1407     }
1408   else if (section_is_p (sectp->name, FRAME_SECTION))
1409     {
1410       dwarf2_per_objfile->frame.asection = sectp;
1411       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1412     }
1413   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1414     {
1415       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1416
1417       if (aflag & SEC_HAS_CONTENTS)
1418         {
1419           dwarf2_per_objfile->eh_frame.asection = sectp;
1420           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1421         }
1422     }
1423   else if (section_is_p (sectp->name, RANGES_SECTION))
1424     {
1425       dwarf2_per_objfile->ranges.asection = sectp;
1426       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1427     }
1428   else if (section_is_p (sectp->name, TYPES_SECTION))
1429     {
1430       dwarf2_per_objfile->types.asection = sectp;
1431       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1432     }
1433   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1434     {
1435       dwarf2_per_objfile->gdb_index.asection = sectp;
1436       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1437     }
1438
1439   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1440       && bfd_section_vma (abfd, sectp) == 0)
1441     dwarf2_per_objfile->has_section_at_zero = 1;
1442 }
1443
1444 /* Decompress a section that was compressed using zlib.  Store the
1445    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1446
1447 static void
1448 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1449                          gdb_byte **outbuf, bfd_size_type *outsize)
1450 {
1451   bfd *abfd = objfile->obfd;
1452 #ifndef HAVE_ZLIB_H
1453   error (_("Support for zlib-compressed DWARF data (from '%s') "
1454            "is disabled in this copy of GDB"),
1455          bfd_get_filename (abfd));
1456 #else
1457   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1458   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1459   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1460   bfd_size_type uncompressed_size;
1461   gdb_byte *uncompressed_buffer;
1462   z_stream strm;
1463   int rc;
1464   int header_size = 12;
1465
1466   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1467       || bfd_bread (compressed_buffer,
1468                     compressed_size, abfd) != compressed_size)
1469     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1470            bfd_get_filename (abfd));
1471
1472   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1473      by the uncompressed section size, 8 bytes in big-endian order.  */
1474   if (compressed_size < header_size
1475       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1476     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1477            bfd_get_filename (abfd));
1478   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1479   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1480   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1481   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1482   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1483   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1484   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1485   uncompressed_size += compressed_buffer[11];
1486
1487   /* It is possible the section consists of several compressed
1488      buffers concatenated together, so we uncompress in a loop.  */
1489   strm.zalloc = NULL;
1490   strm.zfree = NULL;
1491   strm.opaque = NULL;
1492   strm.avail_in = compressed_size - header_size;
1493   strm.next_in = (Bytef*) compressed_buffer + header_size;
1494   strm.avail_out = uncompressed_size;
1495   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1496                                        uncompressed_size);
1497   rc = inflateInit (&strm);
1498   while (strm.avail_in > 0)
1499     {
1500       if (rc != Z_OK)
1501         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1502                bfd_get_filename (abfd), rc);
1503       strm.next_out = ((Bytef*) uncompressed_buffer
1504                        + (uncompressed_size - strm.avail_out));
1505       rc = inflate (&strm, Z_FINISH);
1506       if (rc != Z_STREAM_END)
1507         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1508                bfd_get_filename (abfd), rc);
1509       rc = inflateReset (&strm);
1510     }
1511   rc = inflateEnd (&strm);
1512   if (rc != Z_OK
1513       || strm.avail_out != 0)
1514     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1515            bfd_get_filename (abfd), rc);
1516
1517   do_cleanups (cleanup);
1518   *outbuf = uncompressed_buffer;
1519   *outsize = uncompressed_size;
1520 #endif
1521 }
1522
1523 /* A helper function that decides whether a section is empty.  */
1524
1525 static int
1526 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1527 {
1528   return info->asection == NULL || info->size == 0;
1529 }
1530
1531 /* Read the contents of the section SECTP from object file specified by
1532    OBJFILE, store info about the section into INFO.
1533    If the section is compressed, uncompress it before returning.  */
1534
1535 static void
1536 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1537 {
1538   bfd *abfd = objfile->obfd;
1539   asection *sectp = info->asection;
1540   gdb_byte *buf, *retbuf;
1541   unsigned char header[4];
1542
1543   if (info->readin)
1544     return;
1545   info->buffer = NULL;
1546   info->was_mmapped = 0;
1547   info->readin = 1;
1548
1549   if (dwarf2_section_empty_p (info))
1550     return;
1551
1552   /* Check if the file has a 4-byte header indicating compression.  */
1553   if (info->size > sizeof (header)
1554       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1555       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1556     {
1557       /* Upon decompression, update the buffer and its size.  */
1558       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1559         {
1560           zlib_decompress_section (objfile, sectp, &info->buffer,
1561                                    &info->size);
1562           return;
1563         }
1564     }
1565
1566 #ifdef HAVE_MMAP
1567   if (pagesize == 0)
1568     pagesize = getpagesize ();
1569
1570   /* Only try to mmap sections which are large enough: we don't want to
1571      waste space due to fragmentation.  Also, only try mmap for sections
1572      without relocations.  */
1573
1574   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1575     {
1576       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1577       size_t map_length = info->size + sectp->filepos - pg_offset;
1578       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1579                                  MAP_PRIVATE, pg_offset);
1580
1581       if (retbuf != MAP_FAILED)
1582         {
1583           info->was_mmapped = 1;
1584           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1585 #if HAVE_POSIX_MADVISE
1586           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1587 #endif
1588           return;
1589         }
1590     }
1591 #endif
1592
1593   /* If we get here, we are a normal, not-compressed section.  */
1594   info->buffer = buf
1595     = obstack_alloc (&objfile->objfile_obstack, info->size);
1596
1597   /* When debugging .o files, we may need to apply relocations; see
1598      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1599      We never compress sections in .o files, so we only need to
1600      try this when the section is not compressed.  */
1601   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1602   if (retbuf != NULL)
1603     {
1604       info->buffer = retbuf;
1605       return;
1606     }
1607
1608   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1609       || bfd_bread (buf, info->size, abfd) != info->size)
1610     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1611            bfd_get_filename (abfd));
1612 }
1613
1614 /* A helper function that returns the size of a section in a safe way.
1615    If you are positive that the section has been read before using the
1616    size, then it is safe to refer to the dwarf2_section_info object's
1617    "size" field directly.  In other cases, you must call this
1618    function, because for compressed sections the size field is not set
1619    correctly until the section has been read.  */
1620
1621 static bfd_size_type
1622 dwarf2_section_size (struct objfile *objfile,
1623                      struct dwarf2_section_info *info)
1624 {
1625   if (!info->readin)
1626     dwarf2_read_section (objfile, info);
1627   return info->size;
1628 }
1629
1630 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1631    SECTION_NAME.  */
1632
1633 void
1634 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1635                          asection **sectp, gdb_byte **bufp,
1636                          bfd_size_type *sizep)
1637 {
1638   struct dwarf2_per_objfile *data
1639     = objfile_data (objfile, dwarf2_objfile_data_key);
1640   struct dwarf2_section_info *info;
1641
1642   /* We may see an objfile without any DWARF, in which case we just
1643      return nothing.  */
1644   if (data == NULL)
1645     {
1646       *sectp = NULL;
1647       *bufp = NULL;
1648       *sizep = 0;
1649       return;
1650     }
1651   if (section_is_p (section_name, EH_FRAME_SECTION))
1652     info = &data->eh_frame;
1653   else if (section_is_p (section_name, FRAME_SECTION))
1654     info = &data->frame;
1655   else
1656     gdb_assert_not_reached ("unexpected section");
1657
1658   dwarf2_read_section (objfile, info);
1659
1660   *sectp = info->asection;
1661   *bufp = info->buffer;
1662   *sizep = info->size;
1663 }
1664
1665 \f
1666 /* DWARF quick_symbols_functions support.  */
1667
1668 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1669    unique line tables, so we maintain a separate table of all .debug_line
1670    derived entries to support the sharing.
1671    All the quick functions need is the list of file names.  We discard the
1672    line_header when we're done and don't need to record it here.  */
1673 struct quick_file_names
1674 {
1675   /* The offset in .debug_line of the line table.  We hash on this.  */
1676   unsigned int offset;
1677
1678   /* The number of entries in file_names, real_names.  */
1679   unsigned int num_file_names;
1680
1681   /* The file names from the line table, after being run through
1682      file_full_name.  */
1683   const char **file_names;
1684
1685   /* The file names from the line table after being run through
1686      gdb_realpath.  These are computed lazily.  */
1687   const char **real_names;
1688 };
1689
1690 /* When using the index (and thus not using psymtabs), each CU has an
1691    object of this type.  This is used to hold information needed by
1692    the various "quick" methods.  */
1693 struct dwarf2_per_cu_quick_data
1694 {
1695   /* The file table.  This can be NULL if there was no file table
1696      or it's currently not read in.
1697      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1698   struct quick_file_names *file_names;
1699
1700   /* The corresponding symbol table.  This is NULL if symbols for this
1701      CU have not yet been read.  */
1702   struct symtab *symtab;
1703
1704   /* A temporary mark bit used when iterating over all CUs in
1705      expand_symtabs_matching.  */
1706   unsigned int mark : 1;
1707
1708   /* True if we've tried to read the file table and found there isn't one.
1709      There will be no point in trying to read it again next time.  */
1710   unsigned int no_file_data : 1;
1711 };
1712
1713 /* Hash function for a quick_file_names.  */
1714
1715 static hashval_t
1716 hash_file_name_entry (const void *e)
1717 {
1718   const struct quick_file_names *file_data = e;
1719
1720   return file_data->offset;
1721 }
1722
1723 /* Equality function for a quick_file_names.  */
1724
1725 static int
1726 eq_file_name_entry (const void *a, const void *b)
1727 {
1728   const struct quick_file_names *ea = a;
1729   const struct quick_file_names *eb = b;
1730
1731   return ea->offset == eb->offset;
1732 }
1733
1734 /* Delete function for a quick_file_names.  */
1735
1736 static void
1737 delete_file_name_entry (void *e)
1738 {
1739   struct quick_file_names *file_data = e;
1740   int i;
1741
1742   for (i = 0; i < file_data->num_file_names; ++i)
1743     {
1744       xfree ((void*) file_data->file_names[i]);
1745       if (file_data->real_names)
1746         xfree ((void*) file_data->real_names[i]);
1747     }
1748
1749   /* The space for the struct itself lives on objfile_obstack,
1750      so we don't free it here.  */
1751 }
1752
1753 /* Create a quick_file_names hash table.  */
1754
1755 static htab_t
1756 create_quick_file_names_table (unsigned int nr_initial_entries)
1757 {
1758   return htab_create_alloc (nr_initial_entries,
1759                             hash_file_name_entry, eq_file_name_entry,
1760                             delete_file_name_entry, xcalloc, xfree);
1761 }
1762
1763 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1764    this CU came.  */
1765
1766 static void
1767 dw2_do_instantiate_symtab (struct objfile *objfile,
1768                            struct dwarf2_per_cu_data *per_cu)
1769 {
1770   struct cleanup *back_to;
1771
1772   back_to = make_cleanup (dwarf2_release_queue, NULL);
1773
1774   queue_comp_unit (per_cu, objfile);
1775
1776   if (per_cu->from_debug_types)
1777     read_signatured_type_at_offset (objfile, per_cu->offset);
1778   else
1779     load_full_comp_unit (per_cu, objfile);
1780
1781   process_queue (objfile);
1782
1783   /* Age the cache, releasing compilation units that have not
1784      been used recently.  */
1785   age_cached_comp_units ();
1786
1787   do_cleanups (back_to);
1788 }
1789
1790 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1791    the objfile from which this CU came.  Returns the resulting symbol
1792    table.  */
1793
1794 static struct symtab *
1795 dw2_instantiate_symtab (struct objfile *objfile,
1796                         struct dwarf2_per_cu_data *per_cu)
1797 {
1798   if (!per_cu->v.quick->symtab)
1799     {
1800       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1801       increment_reading_symtab ();
1802       dw2_do_instantiate_symtab (objfile, per_cu);
1803       do_cleanups (back_to);
1804     }
1805   return per_cu->v.quick->symtab;
1806 }
1807
1808 /* Return the CU given its index.  */
1809
1810 static struct dwarf2_per_cu_data *
1811 dw2_get_cu (int index)
1812 {
1813   if (index >= dwarf2_per_objfile->n_comp_units)
1814     {
1815       index -= dwarf2_per_objfile->n_comp_units;
1816       return dwarf2_per_objfile->type_comp_units[index];
1817     }
1818   return dwarf2_per_objfile->all_comp_units[index];
1819 }
1820
1821 /* A helper function that knows how to read a 64-bit value in a way
1822    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1823    otherwise.  */
1824
1825 static int
1826 extract_cu_value (const char *bytes, ULONGEST *result)
1827 {
1828   if (sizeof (ULONGEST) < 8)
1829     {
1830       int i;
1831
1832       /* Ignore the upper 4 bytes if they are all zero.  */
1833       for (i = 0; i < 4; ++i)
1834         if (bytes[i + 4] != 0)
1835           return 0;
1836
1837       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1838     }
1839   else
1840     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1841   return 1;
1842 }
1843
1844 /* Read the CU list from the mapped index, and use it to create all
1845    the CU objects for this objfile.  Return 0 if something went wrong,
1846    1 if everything went ok.  */
1847
1848 static int
1849 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1850                        offset_type cu_list_elements)
1851 {
1852   offset_type i;
1853
1854   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1855   dwarf2_per_objfile->all_comp_units
1856     = obstack_alloc (&objfile->objfile_obstack,
1857                      dwarf2_per_objfile->n_comp_units
1858                      * sizeof (struct dwarf2_per_cu_data *));
1859
1860   for (i = 0; i < cu_list_elements; i += 2)
1861     {
1862       struct dwarf2_per_cu_data *the_cu;
1863       ULONGEST offset, length;
1864
1865       if (!extract_cu_value (cu_list, &offset)
1866           || !extract_cu_value (cu_list + 8, &length))
1867         return 0;
1868       cu_list += 2 * 8;
1869
1870       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1871                                struct dwarf2_per_cu_data);
1872       the_cu->offset = offset;
1873       the_cu->length = length;
1874       the_cu->objfile = objfile;
1875       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1876                                         struct dwarf2_per_cu_quick_data);
1877       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1878     }
1879
1880   return 1;
1881 }
1882
1883 /* Create the signatured type hash table from the index.  */
1884
1885 static int
1886 create_signatured_type_table_from_index (struct objfile *objfile,
1887                                          const gdb_byte *bytes,
1888                                          offset_type elements)
1889 {
1890   offset_type i;
1891   htab_t sig_types_hash;
1892
1893   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1894   dwarf2_per_objfile->type_comp_units
1895     = obstack_alloc (&objfile->objfile_obstack,
1896                      dwarf2_per_objfile->n_type_comp_units
1897                      * sizeof (struct dwarf2_per_cu_data *));
1898
1899   sig_types_hash = allocate_signatured_type_table (objfile);
1900
1901   for (i = 0; i < elements; i += 3)
1902     {
1903       struct signatured_type *type_sig;
1904       ULONGEST offset, type_offset, signature;
1905       void **slot;
1906
1907       if (!extract_cu_value (bytes, &offset)
1908           || !extract_cu_value (bytes + 8, &type_offset))
1909         return 0;
1910       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1911       bytes += 3 * 8;
1912
1913       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1914                                  struct signatured_type);
1915       type_sig->signature = signature;
1916       type_sig->offset = offset;
1917       type_sig->type_offset = type_offset;
1918       type_sig->per_cu.from_debug_types = 1;
1919       type_sig->per_cu.offset = offset;
1920       type_sig->per_cu.objfile = objfile;
1921       type_sig->per_cu.v.quick
1922         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1923                           struct dwarf2_per_cu_quick_data);
1924
1925       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1926       *slot = type_sig;
1927
1928       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1929     }
1930
1931   dwarf2_per_objfile->signatured_types = sig_types_hash;
1932
1933   return 1;
1934 }
1935
1936 /* Read the address map data from the mapped index, and use it to
1937    populate the objfile's psymtabs_addrmap.  */
1938
1939 static void
1940 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1941 {
1942   const gdb_byte *iter, *end;
1943   struct obstack temp_obstack;
1944   struct addrmap *mutable_map;
1945   struct cleanup *cleanup;
1946   CORE_ADDR baseaddr;
1947
1948   obstack_init (&temp_obstack);
1949   cleanup = make_cleanup_obstack_free (&temp_obstack);
1950   mutable_map = addrmap_create_mutable (&temp_obstack);
1951
1952   iter = index->address_table;
1953   end = iter + index->address_table_size;
1954
1955   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1956
1957   while (iter < end)
1958     {
1959       ULONGEST hi, lo, cu_index;
1960       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1961       iter += 8;
1962       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1963       iter += 8;
1964       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1965       iter += 4;
1966       
1967       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1968                          dw2_get_cu (cu_index));
1969     }
1970
1971   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1972                                                     &objfile->objfile_obstack);
1973   do_cleanups (cleanup);
1974 }
1975
1976 /* The hash function for strings in the mapped index.  This is the
1977    same as the hashtab.c hash function, but we keep a separate copy to
1978    maintain control over the implementation.  This is necessary
1979    because the hash function is tied to the format of the mapped index
1980    file.  */
1981
1982 static hashval_t
1983 mapped_index_string_hash (const void *p)
1984 {
1985   const unsigned char *str = (const unsigned char *) p;
1986   hashval_t r = 0;
1987   unsigned char c;
1988
1989   while ((c = *str++) != 0)
1990     r = r * 67 + c - 113;
1991
1992   return r;
1993 }
1994
1995 /* Find a slot in the mapped index INDEX for the object named NAME.
1996    If NAME is found, set *VEC_OUT to point to the CU vector in the
1997    constant pool and return 1.  If NAME cannot be found, return 0.  */
1998
1999 static int
2000 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2001                           offset_type **vec_out)
2002 {
2003   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2004   offset_type hash;
2005   offset_type slot, step;
2006
2007   if (current_language->la_language == language_cplus
2008       || current_language->la_language == language_java
2009       || current_language->la_language == language_fortran)
2010     {
2011       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2012          not contain any.  */
2013       const char *paren = strchr (name, '(');
2014
2015       if (paren)
2016         {
2017           char *dup;
2018
2019           dup = xmalloc (paren - name + 1);
2020           memcpy (dup, name, paren - name);
2021           dup[paren - name] = 0;
2022
2023           make_cleanup (xfree, dup);
2024           name = dup;
2025         }
2026     }
2027
2028   hash = mapped_index_string_hash (name);
2029   slot = hash & (index->symbol_table_slots - 1);
2030   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2031
2032   for (;;)
2033     {
2034       /* Convert a slot number to an offset into the table.  */
2035       offset_type i = 2 * slot;
2036       const char *str;
2037       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2038         {
2039           do_cleanups (back_to);
2040           return 0;
2041         }
2042
2043       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2044       if (!strcmp (name, str))
2045         {
2046           *vec_out = (offset_type *) (index->constant_pool
2047                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2048           do_cleanups (back_to);
2049           return 1;
2050         }
2051
2052       slot = (slot + step) & (index->symbol_table_slots - 1);
2053     }
2054 }
2055
2056 /* Read the index file.  If everything went ok, initialize the "quick"
2057    elements of all the CUs and return 1.  Otherwise, return 0.  */
2058
2059 static int
2060 dwarf2_read_index (struct objfile *objfile)
2061 {
2062   char *addr;
2063   struct mapped_index *map;
2064   offset_type *metadata;
2065   const gdb_byte *cu_list;
2066   const gdb_byte *types_list = NULL;
2067   offset_type version, cu_list_elements;
2068   offset_type types_list_elements = 0;
2069   int i;
2070
2071   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2072     return 0;
2073
2074   /* Older elfutils strip versions could keep the section in the main
2075      executable while splitting it for the separate debug info file.  */
2076   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2077        & SEC_HAS_CONTENTS) == 0)
2078     return 0;
2079
2080   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2081
2082   addr = dwarf2_per_objfile->gdb_index.buffer;
2083   /* Version check.  */
2084   version = MAYBE_SWAP (*(offset_type *) addr);
2085   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2086      causes the index to behave very poorly for certain requests.  Version 3
2087      contained incomplete addrmap.  So, it seems better to just ignore such
2088      indices.  */
2089   if (version < 4)
2090     return 0;
2091   /* Indexes with higher version than the one supported by GDB may be no
2092      longer backward compatible.  */
2093   if (version > 4)
2094     return 0;
2095
2096   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2097   map->total_size = dwarf2_per_objfile->gdb_index.size;
2098
2099   metadata = (offset_type *) (addr + sizeof (offset_type));
2100
2101   i = 0;
2102   cu_list = addr + MAYBE_SWAP (metadata[i]);
2103   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2104                       / 8);
2105   ++i;
2106
2107   types_list = addr + MAYBE_SWAP (metadata[i]);
2108   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2109                           - MAYBE_SWAP (metadata[i]))
2110                          / 8);
2111   ++i;
2112
2113   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2114   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2115                              - MAYBE_SWAP (metadata[i]));
2116   ++i;
2117
2118   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2119   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2120                               - MAYBE_SWAP (metadata[i]))
2121                              / (2 * sizeof (offset_type)));
2122   ++i;
2123
2124   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2125
2126   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2127     return 0;
2128
2129   if (types_list_elements
2130       && !create_signatured_type_table_from_index (objfile, types_list,
2131                                                    types_list_elements))
2132     return 0;
2133
2134   create_addrmap_from_index (objfile, map);
2135
2136   dwarf2_per_objfile->index_table = map;
2137   dwarf2_per_objfile->using_index = 1;
2138   dwarf2_per_objfile->quick_file_names_table =
2139     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2140
2141   return 1;
2142 }
2143
2144 /* A helper for the "quick" functions which sets the global
2145    dwarf2_per_objfile according to OBJFILE.  */
2146
2147 static void
2148 dw2_setup (struct objfile *objfile)
2149 {
2150   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2151   gdb_assert (dwarf2_per_objfile);
2152 }
2153
2154 /* A helper for the "quick" functions which attempts to read the line
2155    table for THIS_CU.  */
2156
2157 static struct quick_file_names *
2158 dw2_get_file_names (struct objfile *objfile,
2159                     struct dwarf2_per_cu_data *this_cu)
2160 {
2161   bfd *abfd = objfile->obfd;
2162   struct line_header *lh;
2163   struct attribute *attr;
2164   struct cleanup *cleanups;
2165   struct die_info *comp_unit_die;
2166   struct dwarf2_section_info* sec;
2167   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2168   int has_children, i;
2169   struct dwarf2_cu cu;
2170   unsigned int bytes_read, buffer_size;
2171   struct die_reader_specs reader_specs;
2172   char *name, *comp_dir;
2173   void **slot;
2174   struct quick_file_names *qfn;
2175   unsigned int line_offset;
2176
2177   if (this_cu->v.quick->file_names != NULL)
2178     return this_cu->v.quick->file_names;
2179   /* If we know there is no line data, no point in looking again.  */
2180   if (this_cu->v.quick->no_file_data)
2181     return NULL;
2182
2183   init_one_comp_unit (&cu, objfile);
2184   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2185
2186   if (this_cu->from_debug_types)
2187     sec = &dwarf2_per_objfile->types;
2188   else
2189     sec = &dwarf2_per_objfile->info;
2190   dwarf2_read_section (objfile, sec);
2191   buffer_size = sec->size;
2192   buffer = sec->buffer;
2193   info_ptr = buffer + this_cu->offset;
2194   beg_of_comp_unit = info_ptr;
2195
2196   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2197                                           buffer, buffer_size,
2198                                           abfd);
2199
2200   /* Complete the cu_header.  */
2201   cu.header.offset = beg_of_comp_unit - buffer;
2202   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2203
2204   this_cu->cu = &cu;
2205   cu.per_cu = this_cu;
2206
2207   dwarf2_read_abbrevs (abfd, &cu);
2208   make_cleanup (dwarf2_free_abbrev_table, &cu);
2209
2210   if (this_cu->from_debug_types)
2211     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2212   init_cu_die_reader (&reader_specs, &cu);
2213   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2214                  &has_children);
2215
2216   lh = NULL;
2217   slot = NULL;
2218   line_offset = 0;
2219   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2220   if (attr)
2221     {
2222       struct quick_file_names find_entry;
2223
2224       line_offset = DW_UNSND (attr);
2225
2226       /* We may have already read in this line header (TU line header sharing).
2227          If we have we're done.  */
2228       find_entry.offset = line_offset;
2229       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2230                              &find_entry, INSERT);
2231       if (*slot != NULL)
2232         {
2233           do_cleanups (cleanups);
2234           this_cu->v.quick->file_names = *slot;
2235           return *slot;
2236         }
2237
2238       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2239     }
2240   if (lh == NULL)
2241     {
2242       do_cleanups (cleanups);
2243       this_cu->v.quick->no_file_data = 1;
2244       return NULL;
2245     }
2246
2247   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2248   qfn->offset = line_offset;
2249   gdb_assert (slot != NULL);
2250   *slot = qfn;
2251
2252   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2253
2254   qfn->num_file_names = lh->num_file_names;
2255   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2256                                    lh->num_file_names * sizeof (char *));
2257   for (i = 0; i < lh->num_file_names; ++i)
2258     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2259   qfn->real_names = NULL;
2260
2261   free_line_header (lh);
2262   do_cleanups (cleanups);
2263
2264   this_cu->v.quick->file_names = qfn;
2265   return qfn;
2266 }
2267
2268 /* A helper for the "quick" functions which computes and caches the
2269    real path for a given file name from the line table.  */
2270
2271 static const char *
2272 dw2_get_real_path (struct objfile *objfile,
2273                    struct quick_file_names *qfn, int index)
2274 {
2275   if (qfn->real_names == NULL)
2276     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2277                                       qfn->num_file_names, sizeof (char *));
2278
2279   if (qfn->real_names[index] == NULL)
2280     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2281
2282   return qfn->real_names[index];
2283 }
2284
2285 static struct symtab *
2286 dw2_find_last_source_symtab (struct objfile *objfile)
2287 {
2288   int index;
2289
2290   dw2_setup (objfile);
2291   index = dwarf2_per_objfile->n_comp_units - 1;
2292   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2293 }
2294
2295 /* Traversal function for dw2_forget_cached_source_info.  */
2296
2297 static int
2298 dw2_free_cached_file_names (void **slot, void *info)
2299 {
2300   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2301
2302   if (file_data->real_names)
2303     {
2304       int i;
2305
2306       for (i = 0; i < file_data->num_file_names; ++i)
2307         {
2308           xfree ((void*) file_data->real_names[i]);
2309           file_data->real_names[i] = NULL;
2310         }
2311     }
2312
2313   return 1;
2314 }
2315
2316 static void
2317 dw2_forget_cached_source_info (struct objfile *objfile)
2318 {
2319   dw2_setup (objfile);
2320
2321   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2322                           dw2_free_cached_file_names, NULL);
2323 }
2324
2325 static int
2326 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2327                    const char *full_path, const char *real_path,
2328                    struct symtab **result)
2329 {
2330   int i;
2331   int check_basename = lbasename (name) == name;
2332   struct dwarf2_per_cu_data *base_cu = NULL;
2333
2334   dw2_setup (objfile);
2335
2336   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2337                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2338     {
2339       int j;
2340       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2341       struct quick_file_names *file_data;
2342
2343       if (per_cu->v.quick->symtab)
2344         continue;
2345
2346       file_data = dw2_get_file_names (objfile, per_cu);
2347       if (file_data == NULL)
2348         continue;
2349
2350       for (j = 0; j < file_data->num_file_names; ++j)
2351         {
2352           const char *this_name = file_data->file_names[j];
2353
2354           if (FILENAME_CMP (name, this_name) == 0)
2355             {
2356               *result = dw2_instantiate_symtab (objfile, per_cu);
2357               return 1;
2358             }
2359
2360           if (check_basename && ! base_cu
2361               && FILENAME_CMP (lbasename (this_name), name) == 0)
2362             base_cu = per_cu;
2363
2364           if (full_path != NULL)
2365             {
2366               const char *this_real_name = dw2_get_real_path (objfile,
2367                                                               file_data, j);
2368
2369               if (this_real_name != NULL
2370                   && FILENAME_CMP (full_path, this_real_name) == 0)
2371                 {
2372                   *result = dw2_instantiate_symtab (objfile, per_cu);
2373                   return 1;
2374                 }
2375             }
2376
2377           if (real_path != NULL)
2378             {
2379               const char *this_real_name = dw2_get_real_path (objfile,
2380                                                               file_data, j);
2381
2382               if (this_real_name != NULL
2383                   && FILENAME_CMP (real_path, this_real_name) == 0)
2384                 {
2385                   *result = dw2_instantiate_symtab (objfile, per_cu);
2386                   return 1;
2387                 }
2388             }
2389         }
2390     }
2391
2392   if (base_cu)
2393     {
2394       *result = dw2_instantiate_symtab (objfile, base_cu);
2395       return 1;
2396     }
2397
2398   return 0;
2399 }
2400
2401 static struct symtab *
2402 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2403                    const char *name, domain_enum domain)
2404 {
2405   /* We do all the work in the pre_expand_symtabs_matching hook
2406      instead.  */
2407   return NULL;
2408 }
2409
2410 /* A helper function that expands all symtabs that hold an object
2411    named NAME.  */
2412
2413 static void
2414 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2415 {
2416   dw2_setup (objfile);
2417
2418   /* index_table is NULL if OBJF_READNOW.  */
2419   if (dwarf2_per_objfile->index_table)
2420     {
2421       offset_type *vec;
2422
2423       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2424                                     name, &vec))
2425         {
2426           offset_type i, len = MAYBE_SWAP (*vec);
2427           for (i = 0; i < len; ++i)
2428             {
2429               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2430               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2431
2432               dw2_instantiate_symtab (objfile, per_cu);
2433             }
2434         }
2435     }
2436 }
2437
2438 static void
2439 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2440                                  int kind, const char *name,
2441                                  domain_enum domain)
2442 {
2443   dw2_do_expand_symtabs_matching (objfile, name);
2444 }
2445
2446 static void
2447 dw2_print_stats (struct objfile *objfile)
2448 {
2449   int i, count;
2450
2451   dw2_setup (objfile);
2452   count = 0;
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       if (!per_cu->v.quick->symtab)
2459         ++count;
2460     }
2461   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2462 }
2463
2464 static void
2465 dw2_dump (struct objfile *objfile)
2466 {
2467   /* Nothing worth printing.  */
2468 }
2469
2470 static void
2471 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2472               struct section_offsets *delta)
2473 {
2474   /* There's nothing to relocate here.  */
2475 }
2476
2477 static void
2478 dw2_expand_symtabs_for_function (struct objfile *objfile,
2479                                  const char *func_name)
2480 {
2481   dw2_do_expand_symtabs_matching (objfile, func_name);
2482 }
2483
2484 static void
2485 dw2_expand_all_symtabs (struct objfile *objfile)
2486 {
2487   int i;
2488
2489   dw2_setup (objfile);
2490
2491   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2492                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2493     {
2494       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2495
2496       dw2_instantiate_symtab (objfile, per_cu);
2497     }
2498 }
2499
2500 static void
2501 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2502                                   const char *filename)
2503 {
2504   int i;
2505
2506   dw2_setup (objfile);
2507
2508   /* We don't need to consider type units here.
2509      This is only called for examining code, e.g. expand_line_sal.
2510      There can be an order of magnitude (or more) more type units
2511      than comp units, and we avoid them if we can.  */
2512
2513   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2514     {
2515       int j;
2516       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2517       struct quick_file_names *file_data;
2518
2519       if (per_cu->v.quick->symtab)
2520         continue;
2521
2522       file_data = dw2_get_file_names (objfile, per_cu);
2523       if (file_data == NULL)
2524         continue;
2525
2526       for (j = 0; j < file_data->num_file_names; ++j)
2527         {
2528           const char *this_name = file_data->file_names[j];
2529           if (FILENAME_CMP (this_name, filename) == 0)
2530             {
2531               dw2_instantiate_symtab (objfile, per_cu);
2532               break;
2533             }
2534         }
2535     }
2536 }
2537
2538 static const char *
2539 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2540 {
2541   struct dwarf2_per_cu_data *per_cu;
2542   offset_type *vec;
2543   struct quick_file_names *file_data;
2544
2545   dw2_setup (objfile);
2546
2547   /* index_table is NULL if OBJF_READNOW.  */
2548   if (!dwarf2_per_objfile->index_table)
2549     return NULL;
2550
2551   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2552                                  name, &vec))
2553     return NULL;
2554
2555   /* Note that this just looks at the very first one named NAME -- but
2556      actually we are looking for a function.  find_main_filename
2557      should be rewritten so that it doesn't require a custom hook.  It
2558      could just use the ordinary symbol tables.  */
2559   /* vec[0] is the length, which must always be >0.  */
2560   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2561
2562   file_data = dw2_get_file_names (objfile, per_cu);
2563   if (file_data == NULL)
2564     return NULL;
2565
2566   return file_data->file_names[file_data->num_file_names - 1];
2567 }
2568
2569 static void
2570 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2571                           struct objfile *objfile, int global,
2572                           int (*callback) (struct block *,
2573                                            struct symbol *, void *),
2574                           void *data, symbol_compare_ftype *match,
2575                           symbol_compare_ftype *ordered_compare)
2576 {
2577   /* Currently unimplemented; used for Ada.  The function can be called if the
2578      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2579      does not look for non-Ada symbols this function should just return.  */
2580 }
2581
2582 static void
2583 dw2_expand_symtabs_matching (struct objfile *objfile,
2584                              int (*file_matcher) (const char *, void *),
2585                              int (*name_matcher) (const char *, void *),
2586                              domain_enum kind,
2587                              void *data)
2588 {
2589   int i;
2590   offset_type iter;
2591   struct mapped_index *index;
2592
2593   dw2_setup (objfile);
2594
2595   /* index_table is NULL if OBJF_READNOW.  */
2596   if (!dwarf2_per_objfile->index_table)
2597     return;
2598   index = dwarf2_per_objfile->index_table;
2599
2600   if (file_matcher != NULL)
2601     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2602                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2603       {
2604         int j;
2605         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2606         struct quick_file_names *file_data;
2607
2608         per_cu->v.quick->mark = 0;
2609         if (per_cu->v.quick->symtab)
2610           continue;
2611
2612         file_data = dw2_get_file_names (objfile, per_cu);
2613         if (file_data == NULL)
2614           continue;
2615
2616         for (j = 0; j < file_data->num_file_names; ++j)
2617           {
2618             if (file_matcher (file_data->file_names[j], data))
2619               {
2620                 per_cu->v.quick->mark = 1;
2621                 break;
2622               }
2623           }
2624       }
2625
2626   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2627     {
2628       offset_type idx = 2 * iter;
2629       const char *name;
2630       offset_type *vec, vec_len, vec_idx;
2631
2632       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2633         continue;
2634
2635       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2636
2637       if (! (*name_matcher) (name, data))
2638         continue;
2639
2640       /* The name was matched, now expand corresponding CUs that were
2641          marked.  */
2642       vec = (offset_type *) (index->constant_pool
2643                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2644       vec_len = MAYBE_SWAP (vec[0]);
2645       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2646         {
2647           struct dwarf2_per_cu_data *per_cu;
2648
2649           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2650           if (file_matcher == NULL || per_cu->v.quick->mark)
2651             dw2_instantiate_symtab (objfile, per_cu);
2652         }
2653     }
2654 }
2655
2656 static struct symtab *
2657 dw2_find_pc_sect_symtab (struct objfile *objfile,
2658                          struct minimal_symbol *msymbol,
2659                          CORE_ADDR pc,
2660                          struct obj_section *section,
2661                          int warn_if_readin)
2662 {
2663   struct dwarf2_per_cu_data *data;
2664
2665   dw2_setup (objfile);
2666
2667   if (!objfile->psymtabs_addrmap)
2668     return NULL;
2669
2670   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2671   if (!data)
2672     return NULL;
2673
2674   if (warn_if_readin && data->v.quick->symtab)
2675     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2676              paddress (get_objfile_arch (objfile), pc));
2677
2678   return dw2_instantiate_symtab (objfile, data);
2679 }
2680
2681 static void
2682 dw2_map_symbol_filenames (struct objfile *objfile,
2683                           void (*fun) (const char *, const char *, void *),
2684                           void *data)
2685 {
2686   int i;
2687
2688   dw2_setup (objfile);
2689
2690   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2691                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2692     {
2693       int j;
2694       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2695       struct quick_file_names *file_data;
2696
2697       if (per_cu->v.quick->symtab)
2698         continue;
2699
2700       file_data = dw2_get_file_names (objfile, per_cu);
2701       if (file_data == NULL)
2702         continue;
2703
2704       for (j = 0; j < file_data->num_file_names; ++j)
2705         {
2706           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2707                                                           j);
2708           (*fun) (file_data->file_names[j], this_real_name, data);
2709         }
2710     }
2711 }
2712
2713 static int
2714 dw2_has_symbols (struct objfile *objfile)
2715 {
2716   return 1;
2717 }
2718
2719 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2720 {
2721   dw2_has_symbols,
2722   dw2_find_last_source_symtab,
2723   dw2_forget_cached_source_info,
2724   dw2_lookup_symtab,
2725   dw2_lookup_symbol,
2726   dw2_pre_expand_symtabs_matching,
2727   dw2_print_stats,
2728   dw2_dump,
2729   dw2_relocate,
2730   dw2_expand_symtabs_for_function,
2731   dw2_expand_all_symtabs,
2732   dw2_expand_symtabs_with_filename,
2733   dw2_find_symbol_file,
2734   dw2_map_matching_symbols,
2735   dw2_expand_symtabs_matching,
2736   dw2_find_pc_sect_symtab,
2737   dw2_map_symbol_filenames
2738 };
2739
2740 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2741    file will use psymtabs, or 1 if using the GNU index.  */
2742
2743 int
2744 dwarf2_initialize_objfile (struct objfile *objfile)
2745 {
2746   /* If we're about to read full symbols, don't bother with the
2747      indices.  In this case we also don't care if some other debug
2748      format is making psymtabs, because they are all about to be
2749      expanded anyway.  */
2750   if ((objfile->flags & OBJF_READNOW))
2751     {
2752       int i;
2753
2754       dwarf2_per_objfile->using_index = 1;
2755       create_all_comp_units (objfile);
2756       create_debug_types_hash_table (objfile);
2757       dwarf2_per_objfile->quick_file_names_table =
2758         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2759
2760       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2761                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2762         {
2763           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2764
2765           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2766                                             struct dwarf2_per_cu_quick_data);
2767         }
2768
2769       /* Return 1 so that gdb sees the "quick" functions.  However,
2770          these functions will be no-ops because we will have expanded
2771          all symtabs.  */
2772       return 1;
2773     }
2774
2775   if (dwarf2_read_index (objfile))
2776     return 1;
2777
2778   return 0;
2779 }
2780
2781 \f
2782
2783 /* Build a partial symbol table.  */
2784
2785 void
2786 dwarf2_build_psymtabs (struct objfile *objfile)
2787 {
2788   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2789     {
2790       init_psymbol_list (objfile, 1024);
2791     }
2792
2793   dwarf2_build_psymtabs_hard (objfile);
2794 }
2795
2796 /* Return TRUE if OFFSET is within CU_HEADER.  */
2797
2798 static inline int
2799 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2800 {
2801   unsigned int bottom = cu_header->offset;
2802   unsigned int top = (cu_header->offset
2803                       + cu_header->length
2804                       + cu_header->initial_length_size);
2805
2806   return (offset >= bottom && offset < top);
2807 }
2808
2809 /* Read in the comp unit header information from the debug_info at info_ptr.
2810    NOTE: This leaves members offset, first_die_offset to be filled in
2811    by the caller.  */
2812
2813 static gdb_byte *
2814 read_comp_unit_head (struct comp_unit_head *cu_header,
2815                      gdb_byte *info_ptr, bfd *abfd)
2816 {
2817   int signed_addr;
2818   unsigned int bytes_read;
2819
2820   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2821   cu_header->initial_length_size = bytes_read;
2822   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2823   info_ptr += bytes_read;
2824   cu_header->version = read_2_bytes (abfd, info_ptr);
2825   info_ptr += 2;
2826   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2827                                           &bytes_read);
2828   info_ptr += bytes_read;
2829   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2830   info_ptr += 1;
2831   signed_addr = bfd_get_sign_extend_vma (abfd);
2832   if (signed_addr < 0)
2833     internal_error (__FILE__, __LINE__,
2834                     _("read_comp_unit_head: dwarf from non elf file"));
2835   cu_header->signed_addr_p = signed_addr;
2836
2837   return info_ptr;
2838 }
2839
2840 static gdb_byte *
2841 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2842                              gdb_byte *buffer, unsigned int buffer_size,
2843                              bfd *abfd)
2844 {
2845   gdb_byte *beg_of_comp_unit = info_ptr;
2846
2847   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2848
2849   if (header->version != 2 && header->version != 3 && header->version != 4)
2850     error (_("Dwarf Error: wrong version in compilation unit header "
2851            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2852            bfd_get_filename (abfd));
2853
2854   if (header->abbrev_offset
2855       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2856                               &dwarf2_per_objfile->abbrev))
2857     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2858            "(offset 0x%lx + 6) [in module %s]"),
2859            (long) header->abbrev_offset,
2860            (long) (beg_of_comp_unit - buffer),
2861            bfd_get_filename (abfd));
2862
2863   if (beg_of_comp_unit + header->length + header->initial_length_size
2864       > buffer + buffer_size)
2865     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2866            "(offset 0x%lx + 0) [in module %s]"),
2867            (long) header->length,
2868            (long) (beg_of_comp_unit - buffer),
2869            bfd_get_filename (abfd));
2870
2871   return info_ptr;
2872 }
2873
2874 /* Read in the types comp unit header information from .debug_types entry at
2875    types_ptr.  The result is a pointer to one past the end of the header.  */
2876
2877 static gdb_byte *
2878 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2879                           ULONGEST *signature,
2880                           gdb_byte *types_ptr, bfd *abfd)
2881 {
2882   gdb_byte *initial_types_ptr = types_ptr;
2883
2884   dwarf2_read_section (dwarf2_per_objfile->objfile,
2885                        &dwarf2_per_objfile->types);
2886   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2887
2888   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2889
2890   *signature = read_8_bytes (abfd, types_ptr);
2891   types_ptr += 8;
2892   types_ptr += cu_header->offset_size;
2893   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2894
2895   return types_ptr;
2896 }
2897
2898 /* Allocate a new partial symtab for file named NAME and mark this new
2899    partial symtab as being an include of PST.  */
2900
2901 static void
2902 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2903                                struct objfile *objfile)
2904 {
2905   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2906
2907   subpst->section_offsets = pst->section_offsets;
2908   subpst->textlow = 0;
2909   subpst->texthigh = 0;
2910
2911   subpst->dependencies = (struct partial_symtab **)
2912     obstack_alloc (&objfile->objfile_obstack,
2913                    sizeof (struct partial_symtab *));
2914   subpst->dependencies[0] = pst;
2915   subpst->number_of_dependencies = 1;
2916
2917   subpst->globals_offset = 0;
2918   subpst->n_global_syms = 0;
2919   subpst->statics_offset = 0;
2920   subpst->n_static_syms = 0;
2921   subpst->symtab = NULL;
2922   subpst->read_symtab = pst->read_symtab;
2923   subpst->readin = 0;
2924
2925   /* No private part is necessary for include psymtabs.  This property
2926      can be used to differentiate between such include psymtabs and
2927      the regular ones.  */
2928   subpst->read_symtab_private = NULL;
2929 }
2930
2931 /* Read the Line Number Program data and extract the list of files
2932    included by the source file represented by PST.  Build an include
2933    partial symtab for each of these included files.  */
2934
2935 static void
2936 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2937                                struct die_info *die,
2938                                struct partial_symtab *pst)
2939 {
2940   struct objfile *objfile = cu->objfile;
2941   bfd *abfd = objfile->obfd;
2942   struct line_header *lh = NULL;
2943   struct attribute *attr;
2944
2945   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2946   if (attr)
2947     {
2948       unsigned int line_offset = DW_UNSND (attr);
2949
2950       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2951     }
2952   if (lh == NULL)
2953     return;  /* No linetable, so no includes.  */
2954
2955   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2956   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2957
2958   free_line_header (lh);
2959 }
2960
2961 static hashval_t
2962 hash_type_signature (const void *item)
2963 {
2964   const struct signatured_type *type_sig = item;
2965
2966   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2967   return type_sig->signature;
2968 }
2969
2970 static int
2971 eq_type_signature (const void *item_lhs, const void *item_rhs)
2972 {
2973   const struct signatured_type *lhs = item_lhs;
2974   const struct signatured_type *rhs = item_rhs;
2975
2976   return lhs->signature == rhs->signature;
2977 }
2978
2979 /* Allocate a hash table for signatured types.  */
2980
2981 static htab_t
2982 allocate_signatured_type_table (struct objfile *objfile)
2983 {
2984   return htab_create_alloc_ex (41,
2985                                hash_type_signature,
2986                                eq_type_signature,
2987                                NULL,
2988                                &objfile->objfile_obstack,
2989                                hashtab_obstack_allocate,
2990                                dummy_obstack_deallocate);
2991 }
2992
2993 /* A helper function to add a signatured type CU to a list.  */
2994
2995 static int
2996 add_signatured_type_cu_to_list (void **slot, void *datum)
2997 {
2998   struct signatured_type *sigt = *slot;
2999   struct dwarf2_per_cu_data ***datap = datum;
3000
3001   **datap = &sigt->per_cu;
3002   ++*datap;
3003
3004   return 1;
3005 }
3006
3007 /* Create the hash table of all entries in the .debug_types section.
3008    The result is zero if there is an error (e.g. missing .debug_types section),
3009    otherwise non-zero.  */
3010
3011 static int
3012 create_debug_types_hash_table (struct objfile *objfile)
3013 {
3014   gdb_byte *info_ptr;
3015   htab_t types_htab;
3016   struct dwarf2_per_cu_data **iter;
3017
3018   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3019   info_ptr = dwarf2_per_objfile->types.buffer;
3020
3021   if (info_ptr == NULL)
3022     {
3023       dwarf2_per_objfile->signatured_types = NULL;
3024       return 0;
3025     }
3026
3027   types_htab = allocate_signatured_type_table (objfile);
3028
3029   if (dwarf2_die_debug)
3030     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3031
3032   while (info_ptr < dwarf2_per_objfile->types.buffer
3033          + dwarf2_per_objfile->types.size)
3034     {
3035       unsigned int offset;
3036       unsigned int offset_size;
3037       unsigned int type_offset;
3038       unsigned int length, initial_length_size;
3039       unsigned short version;
3040       ULONGEST signature;
3041       struct signatured_type *type_sig;
3042       void **slot;
3043       gdb_byte *ptr = info_ptr;
3044
3045       offset = ptr - dwarf2_per_objfile->types.buffer;
3046
3047       /* We need to read the type's signature in order to build the hash
3048          table, but we don't need to read anything else just yet.  */
3049
3050       /* Sanity check to ensure entire cu is present.  */
3051       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3052       if (ptr + length + initial_length_size
3053           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3054         {
3055           complaint (&symfile_complaints,
3056                      _("debug type entry runs off end "
3057                        "of `.debug_types' section, ignored"));
3058           break;
3059         }
3060
3061       offset_size = initial_length_size == 4 ? 4 : 8;
3062       ptr += initial_length_size;
3063       version = bfd_get_16 (objfile->obfd, ptr);
3064       ptr += 2;
3065       ptr += offset_size; /* abbrev offset */
3066       ptr += 1; /* address size */
3067       signature = bfd_get_64 (objfile->obfd, ptr);
3068       ptr += 8;
3069       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3070
3071       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3072       memset (type_sig, 0, sizeof (*type_sig));
3073       type_sig->signature = signature;
3074       type_sig->offset = offset;
3075       type_sig->type_offset = type_offset;
3076       type_sig->per_cu.objfile = objfile;
3077       type_sig->per_cu.from_debug_types = 1;
3078
3079       slot = htab_find_slot (types_htab, type_sig, INSERT);
3080       gdb_assert (slot != NULL);
3081       *slot = type_sig;
3082
3083       if (dwarf2_die_debug)
3084         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3085                             offset, phex (signature, sizeof (signature)));
3086
3087       info_ptr = info_ptr + initial_length_size + length;
3088     }
3089
3090   dwarf2_per_objfile->signatured_types = types_htab;
3091
3092   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3093   dwarf2_per_objfile->type_comp_units
3094     = obstack_alloc (&objfile->objfile_obstack,
3095                      dwarf2_per_objfile->n_type_comp_units
3096                      * sizeof (struct dwarf2_per_cu_data *));
3097   iter = &dwarf2_per_objfile->type_comp_units[0];
3098   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3099   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3100               == dwarf2_per_objfile->n_type_comp_units);
3101
3102   return 1;
3103 }
3104
3105 /* Lookup a signature based type.
3106    Returns NULL if SIG is not present in the table.  */
3107
3108 static struct signatured_type *
3109 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3110 {
3111   struct signatured_type find_entry, *entry;
3112
3113   if (dwarf2_per_objfile->signatured_types == NULL)
3114     {
3115       complaint (&symfile_complaints,
3116                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3117       return 0;
3118     }
3119
3120   find_entry.signature = sig;
3121   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3122   return entry;
3123 }
3124
3125 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3126
3127 static void
3128 init_cu_die_reader (struct die_reader_specs *reader,
3129                     struct dwarf2_cu *cu)
3130 {
3131   reader->abfd = cu->objfile->obfd;
3132   reader->cu = cu;
3133   if (cu->per_cu->from_debug_types)
3134     {
3135       gdb_assert (dwarf2_per_objfile->types.readin);
3136       reader->buffer = dwarf2_per_objfile->types.buffer;
3137     }
3138   else
3139     {
3140       gdb_assert (dwarf2_per_objfile->info.readin);
3141       reader->buffer = dwarf2_per_objfile->info.buffer;
3142     }
3143 }
3144
3145 /* Find the base address of the compilation unit for range lists and
3146    location lists.  It will normally be specified by DW_AT_low_pc.
3147    In DWARF-3 draft 4, the base address could be overridden by
3148    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3149    compilation units with discontinuous ranges.  */
3150
3151 static void
3152 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3153 {
3154   struct attribute *attr;
3155
3156   cu->base_known = 0;
3157   cu->base_address = 0;
3158
3159   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3160   if (attr)
3161     {
3162       cu->base_address = DW_ADDR (attr);
3163       cu->base_known = 1;
3164     }
3165   else
3166     {
3167       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3168       if (attr)
3169         {
3170           cu->base_address = DW_ADDR (attr);
3171           cu->base_known = 1;
3172         }
3173     }
3174 }
3175
3176 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3177    to combine the common parts.
3178    Process a compilation unit for a psymtab.
3179    BUFFER is a pointer to the beginning of the dwarf section buffer,
3180    either .debug_info or debug_types.
3181    INFO_PTR is a pointer to the start of the CU.
3182    Returns a pointer to the next CU.  */
3183
3184 static gdb_byte *
3185 process_psymtab_comp_unit (struct objfile *objfile,
3186                            struct dwarf2_per_cu_data *this_cu,
3187                            gdb_byte *buffer, gdb_byte *info_ptr,
3188                            unsigned int buffer_size)
3189 {
3190   bfd *abfd = objfile->obfd;
3191   gdb_byte *beg_of_comp_unit = info_ptr;
3192   struct die_info *comp_unit_die;
3193   struct partial_symtab *pst;
3194   CORE_ADDR baseaddr;
3195   struct cleanup *back_to_inner;
3196   struct dwarf2_cu cu;
3197   int has_children, has_pc_info;
3198   struct attribute *attr;
3199   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3200   struct die_reader_specs reader_specs;
3201   const char *filename;
3202
3203   init_one_comp_unit (&cu, objfile);
3204   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3205
3206   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3207                                           buffer, buffer_size,
3208                                           abfd);
3209
3210   /* Complete the cu_header.  */
3211   cu.header.offset = beg_of_comp_unit - buffer;
3212   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3213
3214   cu.list_in_scope = &file_symbols;
3215
3216   /* If this compilation unit was already read in, free the
3217      cached copy in order to read it in again.  This is
3218      necessary because we skipped some symbols when we first
3219      read in the compilation unit (see load_partial_dies).
3220      This problem could be avoided, but the benefit is
3221      unclear.  */
3222   if (this_cu->cu != NULL)
3223     free_one_cached_comp_unit (this_cu->cu);
3224
3225   /* Note that this is a pointer to our stack frame, being
3226      added to a global data structure.  It will be cleaned up
3227      in free_stack_comp_unit when we finish with this
3228      compilation unit.  */
3229   this_cu->cu = &cu;
3230   cu.per_cu = this_cu;
3231
3232   /* Read the abbrevs for this compilation unit into a table.  */
3233   dwarf2_read_abbrevs (abfd, &cu);
3234   make_cleanup (dwarf2_free_abbrev_table, &cu);
3235
3236   /* Read the compilation unit die.  */
3237   if (this_cu->from_debug_types)
3238     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3239   init_cu_die_reader (&reader_specs, &cu);
3240   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3241                             &has_children);
3242
3243   if (this_cu->from_debug_types)
3244     {
3245       /* offset,length haven't been set yet for type units.  */
3246       this_cu->offset = cu.header.offset;
3247       this_cu->length = cu.header.length + cu.header.initial_length_size;
3248     }
3249   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3250     {
3251       info_ptr = (beg_of_comp_unit + cu.header.length
3252                   + cu.header.initial_length_size);
3253       do_cleanups (back_to_inner);
3254       return info_ptr;
3255     }
3256
3257   prepare_one_comp_unit (&cu, comp_unit_die);
3258
3259   /* Allocate a new partial symbol table structure.  */
3260   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3261   if (attr == NULL || !DW_STRING (attr))
3262     filename = "";
3263   else
3264     filename = DW_STRING (attr);
3265   pst = start_psymtab_common (objfile, objfile->section_offsets,
3266                               filename,
3267                               /* TEXTLOW and TEXTHIGH are set below.  */
3268                               0,
3269                               objfile->global_psymbols.next,
3270                               objfile->static_psymbols.next);
3271
3272   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3273   if (attr != NULL)
3274     pst->dirname = DW_STRING (attr);
3275
3276   pst->read_symtab_private = this_cu;
3277
3278   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3279
3280   /* Store the function that reads in the rest of the symbol table.  */
3281   pst->read_symtab = dwarf2_psymtab_to_symtab;
3282
3283   this_cu->v.psymtab = pst;
3284
3285   dwarf2_find_base_address (comp_unit_die, &cu);
3286
3287   /* Possibly set the default values of LOWPC and HIGHPC from
3288      `DW_AT_ranges'.  */
3289   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3290                                       &best_highpc, &cu, pst);
3291   if (has_pc_info == 1 && best_lowpc < best_highpc)
3292     /* Store the contiguous range if it is not empty; it can be empty for
3293        CUs with no code.  */
3294     addrmap_set_empty (objfile->psymtabs_addrmap,
3295                        best_lowpc + baseaddr,
3296                        best_highpc + baseaddr - 1, pst);
3297
3298   /* Check if comp unit has_children.
3299      If so, read the rest of the partial symbols from this comp unit.
3300      If not, there's no more debug_info for this comp unit.  */
3301   if (has_children)
3302     {
3303       struct partial_die_info *first_die;
3304       CORE_ADDR lowpc, highpc;
3305
3306       lowpc = ((CORE_ADDR) -1);
3307       highpc = ((CORE_ADDR) 0);
3308
3309       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3310
3311       scan_partial_symbols (first_die, &lowpc, &highpc,
3312                             ! has_pc_info, &cu);
3313
3314       /* If we didn't find a lowpc, set it to highpc to avoid
3315          complaints from `maint check'.  */
3316       if (lowpc == ((CORE_ADDR) -1))
3317         lowpc = highpc;
3318
3319       /* If the compilation unit didn't have an explicit address range,
3320          then use the information extracted from its child dies.  */
3321       if (! has_pc_info)
3322         {
3323           best_lowpc = lowpc;
3324           best_highpc = highpc;
3325         }
3326     }
3327   pst->textlow = best_lowpc + baseaddr;
3328   pst->texthigh = best_highpc + baseaddr;
3329
3330   pst->n_global_syms = objfile->global_psymbols.next -
3331     (objfile->global_psymbols.list + pst->globals_offset);
3332   pst->n_static_syms = objfile->static_psymbols.next -
3333     (objfile->static_psymbols.list + pst->statics_offset);
3334   sort_pst_symbols (pst);
3335
3336   info_ptr = (beg_of_comp_unit + cu.header.length
3337               + cu.header.initial_length_size);
3338
3339   if (this_cu->from_debug_types)
3340     {
3341       /* It's not clear we want to do anything with stmt lists here.
3342          Waiting to see what gcc ultimately does.  */
3343     }
3344   else
3345     {
3346       /* Get the list of files included in the current compilation unit,
3347          and build a psymtab for each of them.  */
3348       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3349     }
3350
3351   do_cleanups (back_to_inner);
3352
3353   return info_ptr;
3354 }
3355
3356 /* Traversal function for htab_traverse_noresize.
3357    Process one .debug_types comp-unit.  */
3358
3359 static int
3360 process_type_comp_unit (void **slot, void *info)
3361 {
3362   struct signatured_type *entry = (struct signatured_type *) *slot;
3363   struct objfile *objfile = (struct objfile *) info;
3364   struct dwarf2_per_cu_data *this_cu;
3365
3366   this_cu = &entry->per_cu;
3367
3368   gdb_assert (dwarf2_per_objfile->types.readin);
3369   process_psymtab_comp_unit (objfile, this_cu,
3370                              dwarf2_per_objfile->types.buffer,
3371                              dwarf2_per_objfile->types.buffer + entry->offset,
3372                              dwarf2_per_objfile->types.size);
3373
3374   return 1;
3375 }
3376
3377 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3378    Build partial symbol tables for the .debug_types comp-units.  */
3379
3380 static void
3381 build_type_psymtabs (struct objfile *objfile)
3382 {
3383   if (! create_debug_types_hash_table (objfile))
3384     return;
3385
3386   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3387                           process_type_comp_unit, objfile);
3388 }
3389
3390 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3391
3392 static void
3393 psymtabs_addrmap_cleanup (void *o)
3394 {
3395   struct objfile *objfile = o;
3396
3397   objfile->psymtabs_addrmap = NULL;
3398 }
3399
3400 /* Build the partial symbol table by doing a quick pass through the
3401    .debug_info and .debug_abbrev sections.  */
3402
3403 static void
3404 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3405 {
3406   gdb_byte *info_ptr;
3407   struct cleanup *back_to, *addrmap_cleanup;
3408   struct obstack temp_obstack;
3409
3410   dwarf2_per_objfile->reading_partial_symbols = 1;
3411
3412   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3413   info_ptr = dwarf2_per_objfile->info.buffer;
3414
3415   /* Any cached compilation units will be linked by the per-objfile
3416      read_in_chain.  Make sure to free them when we're done.  */
3417   back_to = make_cleanup (free_cached_comp_units, NULL);
3418
3419   build_type_psymtabs (objfile);
3420
3421   create_all_comp_units (objfile);
3422
3423   /* Create a temporary address map on a temporary obstack.  We later
3424      copy this to the final obstack.  */
3425   obstack_init (&temp_obstack);
3426   make_cleanup_obstack_free (&temp_obstack);
3427   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3428   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3429
3430   /* Since the objects we're extracting from .debug_info vary in
3431      length, only the individual functions to extract them (like
3432      read_comp_unit_head and load_partial_die) can really know whether
3433      the buffer is large enough to hold another complete object.
3434
3435      At the moment, they don't actually check that.  If .debug_info
3436      holds just one extra byte after the last compilation unit's dies,
3437      then read_comp_unit_head will happily read off the end of the
3438      buffer.  read_partial_die is similarly casual.  Those functions
3439      should be fixed.
3440
3441      For this loop condition, simply checking whether there's any data
3442      left at all should be sufficient.  */
3443
3444   while (info_ptr < (dwarf2_per_objfile->info.buffer
3445                      + dwarf2_per_objfile->info.size))
3446     {
3447       struct dwarf2_per_cu_data *this_cu;
3448
3449       this_cu = dwarf2_find_comp_unit (info_ptr
3450                                        - dwarf2_per_objfile->info.buffer,
3451                                        objfile);
3452
3453       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3454                                             dwarf2_per_objfile->info.buffer,
3455                                             info_ptr,
3456                                             dwarf2_per_objfile->info.size);
3457     }
3458
3459   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3460                                                     &objfile->objfile_obstack);
3461   discard_cleanups (addrmap_cleanup);
3462
3463   do_cleanups (back_to);
3464 }
3465
3466 /* Load the partial DIEs for a secondary CU into memory.  */
3467
3468 static void
3469 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3470                         struct objfile *objfile)
3471 {
3472   bfd *abfd = objfile->obfd;
3473   gdb_byte *info_ptr, *beg_of_comp_unit;
3474   struct die_info *comp_unit_die;
3475   struct dwarf2_cu *cu;
3476   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3477   int has_children;
3478   struct die_reader_specs reader_specs;
3479   int read_cu = 0;
3480
3481   gdb_assert (! this_cu->from_debug_types);
3482
3483   gdb_assert (dwarf2_per_objfile->info.readin);
3484   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3485   beg_of_comp_unit = info_ptr;
3486
3487   if (this_cu->cu == NULL)
3488     {
3489       cu = xmalloc (sizeof (*cu));
3490       init_one_comp_unit (cu, objfile);
3491
3492       read_cu = 1;
3493
3494       /* If an error occurs while loading, release our storage.  */
3495       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3496
3497       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3498                                               dwarf2_per_objfile->info.buffer,
3499                                               dwarf2_per_objfile->info.size,
3500                                               abfd);
3501
3502       /* Complete the cu_header.  */
3503       cu->header.offset = this_cu->offset;
3504       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3505
3506       /* Link this compilation unit into the compilation unit tree.  */
3507       this_cu->cu = cu;
3508       cu->per_cu = this_cu;
3509
3510       /* Link this CU into read_in_chain.  */
3511       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3512       dwarf2_per_objfile->read_in_chain = this_cu;
3513     }
3514   else
3515     {
3516       cu = this_cu->cu;
3517       info_ptr += cu->header.first_die_offset;
3518     }
3519
3520   /* Read the abbrevs for this compilation unit into a table.  */
3521   gdb_assert (cu->dwarf2_abbrevs == NULL);
3522   dwarf2_read_abbrevs (abfd, cu);
3523   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3524
3525   /* Read the compilation unit die.  */
3526   init_cu_die_reader (&reader_specs, cu);
3527   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3528                             &has_children);
3529
3530   prepare_one_comp_unit (cu, comp_unit_die);
3531
3532   /* Check if comp unit has_children.
3533      If so, read the rest of the partial symbols from this comp unit.
3534      If not, there's no more debug_info for this comp unit.  */
3535   if (has_children)
3536     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3537
3538   do_cleanups (free_abbrevs_cleanup);
3539
3540   if (read_cu)
3541     {
3542       /* We've successfully allocated this compilation unit.  Let our
3543          caller clean it up when finished with it.  */
3544       discard_cleanups (free_cu_cleanup);
3545     }
3546 }
3547
3548 /* Create a list of all compilation units in OBJFILE.  We do this only
3549    if an inter-comp-unit reference is found; presumably if there is one,
3550    there will be many, and one will occur early in the .debug_info section.
3551    So there's no point in building this list incrementally.  */
3552
3553 static void
3554 create_all_comp_units (struct objfile *objfile)
3555 {
3556   int n_allocated;
3557   int n_comp_units;
3558   struct dwarf2_per_cu_data **all_comp_units;
3559   gdb_byte *info_ptr;
3560
3561   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3562   info_ptr = dwarf2_per_objfile->info.buffer;
3563
3564   n_comp_units = 0;
3565   n_allocated = 10;
3566   all_comp_units = xmalloc (n_allocated
3567                             * sizeof (struct dwarf2_per_cu_data *));
3568
3569   while (info_ptr < dwarf2_per_objfile->info.buffer
3570          + dwarf2_per_objfile->info.size)
3571     {
3572       unsigned int length, initial_length_size;
3573       struct dwarf2_per_cu_data *this_cu;
3574       unsigned int offset;
3575
3576       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3577
3578       /* Read just enough information to find out where the next
3579          compilation unit is.  */
3580       length = read_initial_length (objfile->obfd, info_ptr,
3581                                     &initial_length_size);
3582
3583       /* Save the compilation unit for later lookup.  */
3584       this_cu = obstack_alloc (&objfile->objfile_obstack,
3585                                sizeof (struct dwarf2_per_cu_data));
3586       memset (this_cu, 0, sizeof (*this_cu));
3587       this_cu->offset = offset;
3588       this_cu->length = length + initial_length_size;
3589       this_cu->objfile = objfile;
3590
3591       if (n_comp_units == n_allocated)
3592         {
3593           n_allocated *= 2;
3594           all_comp_units = xrealloc (all_comp_units,
3595                                      n_allocated
3596                                      * sizeof (struct dwarf2_per_cu_data *));
3597         }
3598       all_comp_units[n_comp_units++] = this_cu;
3599
3600       info_ptr = info_ptr + this_cu->length;
3601     }
3602
3603   dwarf2_per_objfile->all_comp_units
3604     = obstack_alloc (&objfile->objfile_obstack,
3605                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3606   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3607           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3608   xfree (all_comp_units);
3609   dwarf2_per_objfile->n_comp_units = n_comp_units;
3610 }
3611
3612 /* Process all loaded DIEs for compilation unit CU, starting at
3613    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3614    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3615    DW_AT_ranges).  If NEED_PC is set, then this function will set
3616    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3617    and record the covered ranges in the addrmap.  */
3618
3619 static void
3620 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3621                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3622 {
3623   struct partial_die_info *pdi;
3624
3625   /* Now, march along the PDI's, descending into ones which have
3626      interesting children but skipping the children of the other ones,
3627      until we reach the end of the compilation unit.  */
3628
3629   pdi = first_die;
3630
3631   while (pdi != NULL)
3632     {
3633       fixup_partial_die (pdi, cu);
3634
3635       /* Anonymous namespaces or modules have no name but have interesting
3636          children, so we need to look at them.  Ditto for anonymous
3637          enums.  */
3638
3639       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3640           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3641         {
3642           switch (pdi->tag)
3643             {
3644             case DW_TAG_subprogram:
3645               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3646               break;
3647             case DW_TAG_constant:
3648             case DW_TAG_variable:
3649             case DW_TAG_typedef:
3650             case DW_TAG_union_type:
3651               if (!pdi->is_declaration)
3652                 {
3653                   add_partial_symbol (pdi, cu);
3654                 }
3655               break;
3656             case DW_TAG_class_type:
3657             case DW_TAG_interface_type:
3658             case DW_TAG_structure_type:
3659               if (!pdi->is_declaration)
3660                 {
3661                   add_partial_symbol (pdi, cu);
3662                 }
3663               break;
3664             case DW_TAG_enumeration_type:
3665               if (!pdi->is_declaration)
3666                 add_partial_enumeration (pdi, cu);
3667               break;
3668             case DW_TAG_base_type:
3669             case DW_TAG_subrange_type:
3670               /* File scope base type definitions are added to the partial
3671                  symbol table.  */
3672               add_partial_symbol (pdi, cu);
3673               break;
3674             case DW_TAG_namespace:
3675               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3676               break;
3677             case DW_TAG_module:
3678               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3679               break;
3680             default:
3681               break;
3682             }
3683         }
3684
3685       /* If the die has a sibling, skip to the sibling.  */
3686
3687       pdi = pdi->die_sibling;
3688     }
3689 }
3690
3691 /* Functions used to compute the fully scoped name of a partial DIE.
3692
3693    Normally, this is simple.  For C++, the parent DIE's fully scoped
3694    name is concatenated with "::" and the partial DIE's name.  For
3695    Java, the same thing occurs except that "." is used instead of "::".
3696    Enumerators are an exception; they use the scope of their parent
3697    enumeration type, i.e. the name of the enumeration type is not
3698    prepended to the enumerator.
3699
3700    There are two complexities.  One is DW_AT_specification; in this
3701    case "parent" means the parent of the target of the specification,
3702    instead of the direct parent of the DIE.  The other is compilers
3703    which do not emit DW_TAG_namespace; in this case we try to guess
3704    the fully qualified name of structure types from their members'
3705    linkage names.  This must be done using the DIE's children rather
3706    than the children of any DW_AT_specification target.  We only need
3707    to do this for structures at the top level, i.e. if the target of
3708    any DW_AT_specification (if any; otherwise the DIE itself) does not
3709    have a parent.  */
3710
3711 /* Compute the scope prefix associated with PDI's parent, in
3712    compilation unit CU.  The result will be allocated on CU's
3713    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3714    field.  NULL is returned if no prefix is necessary.  */
3715 static char *
3716 partial_die_parent_scope (struct partial_die_info *pdi,
3717                           struct dwarf2_cu *cu)
3718 {
3719   char *grandparent_scope;
3720   struct partial_die_info *parent, *real_pdi;
3721
3722   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3723      then this means the parent of the specification DIE.  */
3724
3725   real_pdi = pdi;
3726   while (real_pdi->has_specification)
3727     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3728
3729   parent = real_pdi->die_parent;
3730   if (parent == NULL)
3731     return NULL;
3732
3733   if (parent->scope_set)
3734     return parent->scope;
3735
3736   fixup_partial_die (parent, cu);
3737
3738   grandparent_scope = partial_die_parent_scope (parent, cu);
3739
3740   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3741      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3742      Work around this problem here.  */
3743   if (cu->language == language_cplus
3744       && parent->tag == DW_TAG_namespace
3745       && strcmp (parent->name, "::") == 0
3746       && grandparent_scope == NULL)
3747     {
3748       parent->scope = NULL;
3749       parent->scope_set = 1;
3750       return NULL;
3751     }
3752
3753   if (parent->tag == DW_TAG_namespace
3754       || parent->tag == DW_TAG_module
3755       || parent->tag == DW_TAG_structure_type
3756       || parent->tag == DW_TAG_class_type
3757       || parent->tag == DW_TAG_interface_type
3758       || parent->tag == DW_TAG_union_type
3759       || parent->tag == DW_TAG_enumeration_type)
3760     {
3761       if (grandparent_scope == NULL)
3762         parent->scope = parent->name;
3763       else
3764         parent->scope = typename_concat (&cu->comp_unit_obstack,
3765                                          grandparent_scope,
3766                                          parent->name, 0, cu);
3767     }
3768   else if (parent->tag == DW_TAG_enumerator)
3769     /* Enumerators should not get the name of the enumeration as a prefix.  */
3770     parent->scope = grandparent_scope;
3771   else
3772     {
3773       /* FIXME drow/2004-04-01: What should we be doing with
3774          function-local names?  For partial symbols, we should probably be
3775          ignoring them.  */
3776       complaint (&symfile_complaints,
3777                  _("unhandled containing DIE tag %d for DIE at %d"),
3778                  parent->tag, pdi->offset);
3779       parent->scope = grandparent_scope;
3780     }
3781
3782   parent->scope_set = 1;
3783   return parent->scope;
3784 }
3785
3786 /* Return the fully scoped name associated with PDI, from compilation unit
3787    CU.  The result will be allocated with malloc.  */
3788 static char *
3789 partial_die_full_name (struct partial_die_info *pdi,
3790                        struct dwarf2_cu *cu)
3791 {
3792   char *parent_scope;
3793
3794   /* If this is a template instantiation, we can not work out the
3795      template arguments from partial DIEs.  So, unfortunately, we have
3796      to go through the full DIEs.  At least any work we do building
3797      types here will be reused if full symbols are loaded later.  */
3798   if (pdi->has_template_arguments)
3799     {
3800       fixup_partial_die (pdi, cu);
3801
3802       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3803         {
3804           struct die_info *die;
3805           struct attribute attr;
3806           struct dwarf2_cu *ref_cu = cu;
3807
3808           attr.name = 0;
3809           attr.form = DW_FORM_ref_addr;
3810           attr.u.addr = pdi->offset;
3811           die = follow_die_ref (NULL, &attr, &ref_cu);
3812
3813           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3814         }
3815     }
3816
3817   parent_scope = partial_die_parent_scope (pdi, cu);
3818   if (parent_scope == NULL)
3819     return NULL;
3820   else
3821     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3822 }
3823
3824 static void
3825 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3826 {
3827   struct objfile *objfile = cu->objfile;
3828   CORE_ADDR addr = 0;
3829   char *actual_name = NULL;
3830   const struct partial_symbol *psym = NULL;
3831   CORE_ADDR baseaddr;
3832   int built_actual_name = 0;
3833
3834   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3835
3836   actual_name = partial_die_full_name (pdi, cu);
3837   if (actual_name)
3838     built_actual_name = 1;
3839
3840   if (actual_name == NULL)
3841     actual_name = pdi->name;
3842
3843   switch (pdi->tag)
3844     {
3845     case DW_TAG_subprogram:
3846       if (pdi->is_external || cu->language == language_ada)
3847         {
3848           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3849              of the global scope.  But in Ada, we want to be able to access
3850              nested procedures globally.  So all Ada subprograms are stored
3851              in the global scope.  */
3852           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3853              mst_text, objfile); */
3854           add_psymbol_to_list (actual_name, strlen (actual_name),
3855                                built_actual_name,
3856                                VAR_DOMAIN, LOC_BLOCK,
3857                                &objfile->global_psymbols,
3858                                0, pdi->lowpc + baseaddr,
3859                                cu->language, objfile);
3860         }
3861       else
3862         {
3863           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3864              mst_file_text, objfile); */
3865           add_psymbol_to_list (actual_name, strlen (actual_name),
3866                                built_actual_name,
3867                                VAR_DOMAIN, LOC_BLOCK,
3868                                &objfile->static_psymbols,
3869                                0, pdi->lowpc + baseaddr,
3870                                cu->language, objfile);
3871         }
3872       break;
3873     case DW_TAG_constant:
3874       {
3875         struct psymbol_allocation_list *list;
3876
3877         if (pdi->is_external)
3878           list = &objfile->global_psymbols;
3879         else
3880           list = &objfile->static_psymbols;
3881         add_psymbol_to_list (actual_name, strlen (actual_name),
3882                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3883                              list, 0, 0, cu->language, objfile);
3884       }
3885       break;
3886     case DW_TAG_variable:
3887       if (pdi->locdesc)
3888         addr = decode_locdesc (pdi->locdesc, cu);
3889
3890       if (pdi->locdesc
3891           && addr == 0
3892           && !dwarf2_per_objfile->has_section_at_zero)
3893         {
3894           /* A global or static variable may also have been stripped
3895              out by the linker if unused, in which case its address
3896              will be nullified; do not add such variables into partial
3897              symbol table then.  */
3898         }
3899       else if (pdi->is_external)
3900         {
3901           /* Global Variable.
3902              Don't enter into the minimal symbol tables as there is
3903              a minimal symbol table entry from the ELF symbols already.
3904              Enter into partial symbol table if it has a location
3905              descriptor or a type.
3906              If the location descriptor is missing, new_symbol will create
3907              a LOC_UNRESOLVED symbol, the address of the variable will then
3908              be determined from the minimal symbol table whenever the variable
3909              is referenced.
3910              The address for the partial symbol table entry is not
3911              used by GDB, but it comes in handy for debugging partial symbol
3912              table building.  */
3913
3914           if (pdi->locdesc || pdi->has_type)
3915             add_psymbol_to_list (actual_name, strlen (actual_name),
3916                                  built_actual_name,
3917                                  VAR_DOMAIN, LOC_STATIC,
3918                                  &objfile->global_psymbols,
3919                                  0, addr + baseaddr,
3920                                  cu->language, objfile);
3921         }
3922       else
3923         {
3924           /* Static Variable.  Skip symbols without location descriptors.  */
3925           if (pdi->locdesc == NULL)
3926             {
3927               if (built_actual_name)
3928                 xfree (actual_name);
3929               return;
3930             }
3931           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3932              mst_file_data, objfile); */
3933           add_psymbol_to_list (actual_name, strlen (actual_name),
3934                                built_actual_name,
3935                                VAR_DOMAIN, LOC_STATIC,
3936                                &objfile->static_psymbols,
3937                                0, addr + baseaddr,
3938                                cu->language, objfile);
3939         }
3940       break;
3941     case DW_TAG_typedef:
3942     case DW_TAG_base_type:
3943     case DW_TAG_subrange_type:
3944       add_psymbol_to_list (actual_name, strlen (actual_name),
3945                            built_actual_name,
3946                            VAR_DOMAIN, LOC_TYPEDEF,
3947                            &objfile->static_psymbols,
3948                            0, (CORE_ADDR) 0, cu->language, objfile);
3949       break;
3950     case DW_TAG_namespace:
3951       add_psymbol_to_list (actual_name, strlen (actual_name),
3952                            built_actual_name,
3953                            VAR_DOMAIN, LOC_TYPEDEF,
3954                            &objfile->global_psymbols,
3955                            0, (CORE_ADDR) 0, cu->language, objfile);
3956       break;
3957     case DW_TAG_class_type:
3958     case DW_TAG_interface_type:
3959     case DW_TAG_structure_type:
3960     case DW_TAG_union_type:
3961     case DW_TAG_enumeration_type:
3962       /* Skip external references.  The DWARF standard says in the section
3963          about "Structure, Union, and Class Type Entries": "An incomplete
3964          structure, union or class type is represented by a structure,
3965          union or class entry that does not have a byte size attribute
3966          and that has a DW_AT_declaration attribute."  */
3967       if (!pdi->has_byte_size && pdi->is_declaration)
3968         {
3969           if (built_actual_name)
3970             xfree (actual_name);
3971           return;
3972         }
3973
3974       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3975          static vs. global.  */
3976       add_psymbol_to_list (actual_name, strlen (actual_name),
3977                            built_actual_name,
3978                            STRUCT_DOMAIN, LOC_TYPEDEF,
3979                            (cu->language == language_cplus
3980                             || cu->language == language_java)
3981                            ? &objfile->global_psymbols
3982                            : &objfile->static_psymbols,
3983                            0, (CORE_ADDR) 0, cu->language, objfile);
3984
3985       break;
3986     case DW_TAG_enumerator:
3987       add_psymbol_to_list (actual_name, strlen (actual_name),
3988                            built_actual_name,
3989                            VAR_DOMAIN, LOC_CONST,
3990                            (cu->language == language_cplus
3991                             || cu->language == language_java)
3992                            ? &objfile->global_psymbols
3993                            : &objfile->static_psymbols,
3994                            0, (CORE_ADDR) 0, cu->language, objfile);
3995       break;
3996     default:
3997       break;
3998     }
3999
4000   if (built_actual_name)
4001     xfree (actual_name);
4002 }
4003
4004 /* Read a partial die corresponding to a namespace; also, add a symbol
4005    corresponding to that namespace to the symbol table.  NAMESPACE is
4006    the name of the enclosing namespace.  */
4007
4008 static void
4009 add_partial_namespace (struct partial_die_info *pdi,
4010                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4011                        int need_pc, struct dwarf2_cu *cu)
4012 {
4013   /* Add a symbol for the namespace.  */
4014
4015   add_partial_symbol (pdi, cu);
4016
4017   /* Now scan partial symbols in that namespace.  */
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 Fortran module.  */
4024
4025 static void
4026 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4027                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4028 {
4029   /* Now scan partial symbols in that module.  */
4030
4031   if (pdi->has_children)
4032     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4033 }
4034
4035 /* Read a partial die corresponding to a subprogram and create a partial
4036    symbol for that subprogram.  When the CU language allows it, this
4037    routine also defines a partial symbol for each nested subprogram
4038    that this subprogram contains.
4039
4040    DIE my also be a lexical block, in which case we simply search
4041    recursively for suprograms defined inside that lexical block.
4042    Again, this is only performed when the CU language allows this
4043    type of definitions.  */
4044
4045 static void
4046 add_partial_subprogram (struct partial_die_info *pdi,
4047                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4048                         int need_pc, struct dwarf2_cu *cu)
4049 {
4050   if (pdi->tag == DW_TAG_subprogram)
4051     {
4052       if (pdi->has_pc_info)
4053         {
4054           if (pdi->lowpc < *lowpc)
4055             *lowpc = pdi->lowpc;
4056           if (pdi->highpc > *highpc)
4057             *highpc = pdi->highpc;
4058           if (need_pc)
4059             {
4060               CORE_ADDR baseaddr;
4061               struct objfile *objfile = cu->objfile;
4062
4063               baseaddr = ANOFFSET (objfile->section_offsets,
4064                                    SECT_OFF_TEXT (objfile));
4065               addrmap_set_empty (objfile->psymtabs_addrmap,
4066                                  pdi->lowpc + baseaddr,
4067                                  pdi->highpc - 1 + baseaddr,
4068                                  cu->per_cu->v.psymtab);
4069             }
4070           if (!pdi->is_declaration)
4071             /* Ignore subprogram DIEs that do not have a name, they are
4072                illegal.  Do not emit a complaint at this point, we will
4073                do so when we convert this psymtab into a symtab.  */
4074             if (pdi->name)
4075               add_partial_symbol (pdi, cu);
4076         }
4077     }
4078
4079   if (! pdi->has_children)
4080     return;
4081
4082   if (cu->language == language_ada)
4083     {
4084       pdi = pdi->die_child;
4085       while (pdi != NULL)
4086         {
4087           fixup_partial_die (pdi, cu);
4088           if (pdi->tag == DW_TAG_subprogram
4089               || pdi->tag == DW_TAG_lexical_block)
4090             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4091           pdi = pdi->die_sibling;
4092         }
4093     }
4094 }
4095
4096 /* Read a partial die corresponding to an enumeration type.  */
4097
4098 static void
4099 add_partial_enumeration (struct partial_die_info *enum_pdi,
4100                          struct dwarf2_cu *cu)
4101 {
4102   struct partial_die_info *pdi;
4103
4104   if (enum_pdi->name != NULL)
4105     add_partial_symbol (enum_pdi, cu);
4106
4107   pdi = enum_pdi->die_child;
4108   while (pdi)
4109     {
4110       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4111         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4112       else
4113         add_partial_symbol (pdi, cu);
4114       pdi = pdi->die_sibling;
4115     }
4116 }
4117
4118 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4119    Return the corresponding abbrev, or NULL if the number is zero (indicating
4120    an empty DIE).  In either case *BYTES_READ will be set to the length of
4121    the initial number.  */
4122
4123 static struct abbrev_info *
4124 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4125                  struct dwarf2_cu *cu)
4126 {
4127   bfd *abfd = cu->objfile->obfd;
4128   unsigned int abbrev_number;
4129   struct abbrev_info *abbrev;
4130
4131   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4132
4133   if (abbrev_number == 0)
4134     return NULL;
4135
4136   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4137   if (!abbrev)
4138     {
4139       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4140              abbrev_number, bfd_get_filename (abfd));
4141     }
4142
4143   return abbrev;
4144 }
4145
4146 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4147    Returns a pointer to the end of a series of DIEs, terminated by an empty
4148    DIE.  Any children of the skipped DIEs will also be skipped.  */
4149
4150 static gdb_byte *
4151 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4152 {
4153   struct abbrev_info *abbrev;
4154   unsigned int bytes_read;
4155
4156   while (1)
4157     {
4158       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4159       if (abbrev == NULL)
4160         return info_ptr + bytes_read;
4161       else
4162         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4163     }
4164 }
4165
4166 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4167    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4168    abbrev corresponding to that skipped uleb128 should be passed in
4169    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4170    children.  */
4171
4172 static gdb_byte *
4173 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4174               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4175 {
4176   unsigned int bytes_read;
4177   struct attribute attr;
4178   bfd *abfd = cu->objfile->obfd;
4179   unsigned int form, i;
4180
4181   for (i = 0; i < abbrev->num_attrs; i++)
4182     {
4183       /* The only abbrev we care about is DW_AT_sibling.  */
4184       if (abbrev->attrs[i].name == DW_AT_sibling)
4185         {
4186           read_attribute (&attr, &abbrev->attrs[i],
4187                           abfd, info_ptr, cu);
4188           if (attr.form == DW_FORM_ref_addr)
4189             complaint (&symfile_complaints,
4190                        _("ignoring absolute DW_AT_sibling"));
4191           else
4192             return buffer + dwarf2_get_ref_die_offset (&attr);
4193         }
4194
4195       /* If it isn't DW_AT_sibling, skip this attribute.  */
4196       form = abbrev->attrs[i].form;
4197     skip_attribute:
4198       switch (form)
4199         {
4200         case DW_FORM_ref_addr:
4201           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4202              and later it is offset sized.  */
4203           if (cu->header.version == 2)
4204             info_ptr += cu->header.addr_size;
4205           else
4206             info_ptr += cu->header.offset_size;
4207           break;
4208         case DW_FORM_addr:
4209           info_ptr += cu->header.addr_size;
4210           break;
4211         case DW_FORM_data1:
4212         case DW_FORM_ref1:
4213         case DW_FORM_flag:
4214           info_ptr += 1;
4215           break;
4216         case DW_FORM_flag_present:
4217           break;
4218         case DW_FORM_data2:
4219         case DW_FORM_ref2:
4220           info_ptr += 2;
4221           break;
4222         case DW_FORM_data4:
4223         case DW_FORM_ref4:
4224           info_ptr += 4;
4225           break;
4226         case DW_FORM_data8:
4227         case DW_FORM_ref8:
4228         case DW_FORM_ref_sig8:
4229           info_ptr += 8;
4230           break;
4231         case DW_FORM_string:
4232           read_direct_string (abfd, info_ptr, &bytes_read);
4233           info_ptr += bytes_read;
4234           break;
4235         case DW_FORM_sec_offset:
4236         case DW_FORM_strp:
4237           info_ptr += cu->header.offset_size;
4238           break;
4239         case DW_FORM_exprloc:
4240         case DW_FORM_block:
4241           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4242           info_ptr += bytes_read;
4243           break;
4244         case DW_FORM_block1:
4245           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4246           break;
4247         case DW_FORM_block2:
4248           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4249           break;
4250         case DW_FORM_block4:
4251           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4252           break;
4253         case DW_FORM_sdata:
4254         case DW_FORM_udata:
4255         case DW_FORM_ref_udata:
4256           info_ptr = skip_leb128 (abfd, info_ptr);
4257           break;
4258         case DW_FORM_indirect:
4259           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4260           info_ptr += bytes_read;
4261           /* We need to continue parsing from here, so just go back to
4262              the top.  */
4263           goto skip_attribute;
4264
4265         default:
4266           error (_("Dwarf Error: Cannot handle %s "
4267                    "in DWARF reader [in module %s]"),
4268                  dwarf_form_name (form),
4269                  bfd_get_filename (abfd));
4270         }
4271     }
4272
4273   if (abbrev->has_children)
4274     return skip_children (buffer, info_ptr, cu);
4275   else
4276     return info_ptr;
4277 }
4278
4279 /* Locate ORIG_PDI's sibling.
4280    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4281    in BUFFER.  */
4282
4283 static gdb_byte *
4284 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4285                     gdb_byte *buffer, gdb_byte *info_ptr,
4286                     bfd *abfd, struct dwarf2_cu *cu)
4287 {
4288   /* Do we know the sibling already?  */
4289
4290   if (orig_pdi->sibling)
4291     return orig_pdi->sibling;
4292
4293   /* Are there any children to deal with?  */
4294
4295   if (!orig_pdi->has_children)
4296     return info_ptr;
4297
4298   /* Skip the children the long way.  */
4299
4300   return skip_children (buffer, info_ptr, cu);
4301 }
4302
4303 /* Expand this partial symbol table into a full symbol table.  */
4304
4305 static void
4306 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4307 {
4308   if (pst != NULL)
4309     {
4310       if (pst->readin)
4311         {
4312           warning (_("bug: psymtab for %s is already read in."),
4313                    pst->filename);
4314         }
4315       else
4316         {
4317           if (info_verbose)
4318             {
4319               printf_filtered (_("Reading in symbols for %s..."),
4320                                pst->filename);
4321               gdb_flush (gdb_stdout);
4322             }
4323
4324           /* Restore our global data.  */
4325           dwarf2_per_objfile = objfile_data (pst->objfile,
4326                                              dwarf2_objfile_data_key);
4327
4328           /* If this psymtab is constructed from a debug-only objfile, the
4329              has_section_at_zero flag will not necessarily be correct.  We
4330              can get the correct value for this flag by looking at the data
4331              associated with the (presumably stripped) associated objfile.  */
4332           if (pst->objfile->separate_debug_objfile_backlink)
4333             {
4334               struct dwarf2_per_objfile *dpo_backlink
4335                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4336                                 dwarf2_objfile_data_key);
4337
4338               dwarf2_per_objfile->has_section_at_zero
4339                 = dpo_backlink->has_section_at_zero;
4340             }
4341
4342           dwarf2_per_objfile->reading_partial_symbols = 0;
4343
4344           psymtab_to_symtab_1 (pst);
4345
4346           /* Finish up the debug error message.  */
4347           if (info_verbose)
4348             printf_filtered (_("done.\n"));
4349         }
4350     }
4351 }
4352
4353 /* Add PER_CU to the queue.  */
4354
4355 static void
4356 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4357 {
4358   struct dwarf2_queue_item *item;
4359
4360   per_cu->queued = 1;
4361   item = xmalloc (sizeof (*item));
4362   item->per_cu = per_cu;
4363   item->next = NULL;
4364
4365   if (dwarf2_queue == NULL)
4366     dwarf2_queue = item;
4367   else
4368     dwarf2_queue_tail->next = item;
4369
4370   dwarf2_queue_tail = item;
4371 }
4372
4373 /* Process the queue.  */
4374
4375 static void
4376 process_queue (struct objfile *objfile)
4377 {
4378   struct dwarf2_queue_item *item, *next_item;
4379
4380   /* The queue starts out with one item, but following a DIE reference
4381      may load a new CU, adding it to the end of the queue.  */
4382   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4383     {
4384       if (dwarf2_per_objfile->using_index
4385           ? !item->per_cu->v.quick->symtab
4386           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4387         process_full_comp_unit (item->per_cu);
4388
4389       item->per_cu->queued = 0;
4390       next_item = item->next;
4391       xfree (item);
4392     }
4393
4394   dwarf2_queue_tail = NULL;
4395 }
4396
4397 /* Free all allocated queue entries.  This function only releases anything if
4398    an error was thrown; if the queue was processed then it would have been
4399    freed as we went along.  */
4400
4401 static void
4402 dwarf2_release_queue (void *dummy)
4403 {
4404   struct dwarf2_queue_item *item, *last;
4405
4406   item = dwarf2_queue;
4407   while (item)
4408     {
4409       /* Anything still marked queued is likely to be in an
4410          inconsistent state, so discard it.  */
4411       if (item->per_cu->queued)
4412         {
4413           if (item->per_cu->cu != NULL)
4414             free_one_cached_comp_unit (item->per_cu->cu);
4415           item->per_cu->queued = 0;
4416         }
4417
4418       last = item;
4419       item = item->next;
4420       xfree (last);
4421     }
4422
4423   dwarf2_queue = dwarf2_queue_tail = NULL;
4424 }
4425
4426 /* Read in full symbols for PST, and anything it depends on.  */
4427
4428 static void
4429 psymtab_to_symtab_1 (struct partial_symtab *pst)
4430 {
4431   struct dwarf2_per_cu_data *per_cu;
4432   struct cleanup *back_to;
4433   int i;
4434
4435   for (i = 0; i < pst->number_of_dependencies; i++)
4436     if (!pst->dependencies[i]->readin)
4437       {
4438         /* Inform about additional files that need to be read in.  */
4439         if (info_verbose)
4440           {
4441             /* FIXME: i18n: Need to make this a single string.  */
4442             fputs_filtered (" ", gdb_stdout);
4443             wrap_here ("");
4444             fputs_filtered ("and ", gdb_stdout);
4445             wrap_here ("");
4446             printf_filtered ("%s...", pst->dependencies[i]->filename);
4447             wrap_here ("");     /* Flush output.  */
4448             gdb_flush (gdb_stdout);
4449           }
4450         psymtab_to_symtab_1 (pst->dependencies[i]);
4451       }
4452
4453   per_cu = pst->read_symtab_private;
4454
4455   if (per_cu == NULL)
4456     {
4457       /* It's an include file, no symbols to read for it.
4458          Everything is in the parent symtab.  */
4459       pst->readin = 1;
4460       return;
4461     }
4462
4463   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4464 }
4465
4466 /* Load the DIEs associated with PER_CU into memory.  */
4467
4468 static void
4469 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4470                      struct objfile *objfile)
4471 {
4472   bfd *abfd = objfile->obfd;
4473   struct dwarf2_cu *cu;
4474   unsigned int offset;
4475   gdb_byte *info_ptr, *beg_of_comp_unit;
4476   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4477   struct attribute *attr;
4478   int read_cu = 0;
4479
4480   gdb_assert (! per_cu->from_debug_types);
4481
4482   /* Set local variables from the partial symbol table info.  */
4483   offset = per_cu->offset;
4484
4485   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4486   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4487   beg_of_comp_unit = info_ptr;
4488
4489   if (per_cu->cu == NULL)
4490     {
4491       cu = xmalloc (sizeof (*cu));
4492       init_one_comp_unit (cu, objfile);
4493
4494       read_cu = 1;
4495
4496       /* If an error occurs while loading, release our storage.  */
4497       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4498
4499       /* Read in the comp_unit header.  */
4500       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4501
4502       /* Complete the cu_header.  */
4503       cu->header.offset = offset;
4504       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4505
4506       /* Read the abbrevs for this compilation unit.  */
4507       dwarf2_read_abbrevs (abfd, cu);
4508       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4509
4510       /* Link this compilation unit into the compilation unit tree.  */
4511       per_cu->cu = cu;
4512       cu->per_cu = per_cu;
4513
4514       /* Link this CU into read_in_chain.  */
4515       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4516       dwarf2_per_objfile->read_in_chain = per_cu;
4517     }
4518   else
4519     {
4520       cu = per_cu->cu;
4521       info_ptr += cu->header.first_die_offset;
4522     }
4523
4524   cu->dies = read_comp_unit (info_ptr, cu);
4525
4526   /* We try not to read any attributes in this function, because not
4527      all objfiles needed for references have been loaded yet, and symbol
4528      table processing isn't initialized.  But we have to set the CU language,
4529      or we won't be able to build types correctly.  */
4530   prepare_one_comp_unit (cu, cu->dies);
4531
4532   /* Similarly, if we do not read the producer, we can not apply
4533      producer-specific interpretation.  */
4534   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4535   if (attr)
4536     cu->producer = DW_STRING (attr);
4537
4538   if (read_cu)
4539     {
4540       do_cleanups (free_abbrevs_cleanup);
4541
4542       /* We've successfully allocated this compilation unit.  Let our
4543          caller clean it up when finished with it.  */
4544       discard_cleanups (free_cu_cleanup);
4545     }
4546 }
4547
4548 /* Add a DIE to the delayed physname list.  */
4549
4550 static void
4551 add_to_method_list (struct type *type, int fnfield_index, int index,
4552                     const char *name, struct die_info *die,
4553                     struct dwarf2_cu *cu)
4554 {
4555   struct delayed_method_info mi;
4556   mi.type = type;
4557   mi.fnfield_index = fnfield_index;
4558   mi.index = index;
4559   mi.name = name;
4560   mi.die = die;
4561   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4562 }
4563
4564 /* A cleanup for freeing the delayed method list.  */
4565
4566 static void
4567 free_delayed_list (void *ptr)
4568 {
4569   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4570   if (cu->method_list != NULL)
4571     {
4572       VEC_free (delayed_method_info, cu->method_list);
4573       cu->method_list = NULL;
4574     }
4575 }
4576
4577 /* Compute the physnames of any methods on the CU's method list.
4578
4579    The computation of method physnames is delayed in order to avoid the
4580    (bad) condition that one of the method's formal parameters is of an as yet
4581    incomplete type.  */
4582
4583 static void
4584 compute_delayed_physnames (struct dwarf2_cu *cu)
4585 {
4586   int i;
4587   struct delayed_method_info *mi;
4588   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4589     {
4590       char *physname;
4591       struct fn_fieldlist *fn_flp
4592         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4593       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4594       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4595     }
4596 }
4597
4598 /* Check for GCC >= 4.0.  */
4599
4600 static int
4601 producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4602 {
4603   const char *cs;
4604   int major, minor;
4605
4606   if (cu->producer == NULL)
4607     {
4608       /* For unknown compilers expect their behavior is not compliant.  For GCC
4609          this case can also happen for -gdwarf-4 type units supported since
4610          gcc-4.5.  */
4611
4612       return 0;
4613     }
4614
4615   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
4616
4617   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4618     {
4619       /* For non-GCC compilers expect their behavior is not compliant.  */
4620
4621       return 0;
4622     }
4623   cs = &cu->producer[strlen ("GNU ")];
4624   while (*cs && !isdigit (*cs))
4625     cs++;
4626   if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4627     {
4628       /* Not recognized as GCC.  */
4629
4630       return 0;
4631     }
4632
4633   return major >= 4;
4634 }
4635
4636 /* Generate full symbol information for PST and CU, whose DIEs have
4637    already been loaded into memory.  */
4638
4639 static void
4640 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4641 {
4642   struct dwarf2_cu *cu = per_cu->cu;
4643   struct objfile *objfile = per_cu->objfile;
4644   CORE_ADDR lowpc, highpc;
4645   struct symtab *symtab;
4646   struct cleanup *back_to, *delayed_list_cleanup;
4647   CORE_ADDR baseaddr;
4648
4649   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4650
4651   buildsym_init ();
4652   back_to = make_cleanup (really_free_pendings, NULL);
4653   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4654
4655   cu->list_in_scope = &file_symbols;
4656
4657   dwarf2_find_base_address (cu->dies, cu);
4658
4659   /* Do line number decoding in read_file_scope () */
4660   process_die (cu->dies, cu);
4661
4662   /* Now that we have processed all the DIEs in the CU, all the types 
4663      should be complete, and it should now be safe to compute all of the
4664      physnames.  */
4665   compute_delayed_physnames (cu);
4666   do_cleanups (delayed_list_cleanup);
4667
4668   /* Some compilers don't define a DW_AT_high_pc attribute for the
4669      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4670      it, by scanning the DIE's below the compilation unit.  */
4671   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4672
4673   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4674
4675   if (symtab != NULL)
4676     {
4677       /* Set symtab language to language from DW_AT_language.  If the
4678          compilation is from a C file generated by language preprocessors, do
4679          not set the language if it was already deduced by start_subfile.  */
4680       if (!(cu->language == language_c && symtab->language != language_c))
4681         symtab->language = cu->language;
4682
4683       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4684          produce DW_AT_location with location lists but it can be possibly
4685          invalid without -fvar-tracking.
4686
4687          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4688          needed, it would be wrong due to missing DW_AT_producer there.
4689
4690          Still one can confuse GDB by using non-standard GCC compilation
4691          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4692          */ 
4693       if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4694         symtab->locations_valid = 1;
4695     }
4696
4697   if (dwarf2_per_objfile->using_index)
4698     per_cu->v.quick->symtab = symtab;
4699   else
4700     {
4701       struct partial_symtab *pst = per_cu->v.psymtab;
4702       pst->symtab = symtab;
4703       pst->readin = 1;
4704     }
4705
4706   do_cleanups (back_to);
4707 }
4708
4709 /* Process a die and its children.  */
4710
4711 static void
4712 process_die (struct die_info *die, struct dwarf2_cu *cu)
4713 {
4714   switch (die->tag)
4715     {
4716     case DW_TAG_padding:
4717       break;
4718     case DW_TAG_compile_unit:
4719       read_file_scope (die, cu);
4720       break;
4721     case DW_TAG_type_unit:
4722       read_type_unit_scope (die, cu);
4723       break;
4724     case DW_TAG_subprogram:
4725     case DW_TAG_inlined_subroutine:
4726       read_func_scope (die, cu);
4727       break;
4728     case DW_TAG_lexical_block:
4729     case DW_TAG_try_block:
4730     case DW_TAG_catch_block:
4731       read_lexical_block_scope (die, cu);
4732       break;
4733     case DW_TAG_class_type:
4734     case DW_TAG_interface_type:
4735     case DW_TAG_structure_type:
4736     case DW_TAG_union_type:
4737       process_structure_scope (die, cu);
4738       break;
4739     case DW_TAG_enumeration_type:
4740       process_enumeration_scope (die, cu);
4741       break;
4742
4743     /* These dies have a type, but processing them does not create
4744        a symbol or recurse to process the children.  Therefore we can
4745        read them on-demand through read_type_die.  */
4746     case DW_TAG_subroutine_type:
4747     case DW_TAG_set_type:
4748     case DW_TAG_array_type:
4749     case DW_TAG_pointer_type:
4750     case DW_TAG_ptr_to_member_type:
4751     case DW_TAG_reference_type:
4752     case DW_TAG_string_type:
4753       break;
4754
4755     case DW_TAG_base_type:
4756     case DW_TAG_subrange_type:
4757     case DW_TAG_typedef:
4758       /* Add a typedef symbol for the type definition, if it has a
4759          DW_AT_name.  */
4760       new_symbol (die, read_type_die (die, cu), cu);
4761       break;
4762     case DW_TAG_common_block:
4763       read_common_block (die, cu);
4764       break;
4765     case DW_TAG_common_inclusion:
4766       break;
4767     case DW_TAG_namespace:
4768       processing_has_namespace_info = 1;
4769       read_namespace (die, cu);
4770       break;
4771     case DW_TAG_module:
4772       processing_has_namespace_info = 1;
4773       read_module (die, cu);
4774       break;
4775     case DW_TAG_imported_declaration:
4776     case DW_TAG_imported_module:
4777       processing_has_namespace_info = 1;
4778       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4779                                  || cu->language != language_fortran))
4780         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4781                    dwarf_tag_name (die->tag));
4782       read_import_statement (die, cu);
4783       break;
4784     default:
4785       new_symbol (die, NULL, cu);
4786       break;
4787     }
4788 }
4789
4790 /* A helper function for dwarf2_compute_name which determines whether DIE
4791    needs to have the name of the scope prepended to the name listed in the
4792    die.  */
4793
4794 static int
4795 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4796 {
4797   struct attribute *attr;
4798
4799   switch (die->tag)
4800     {
4801     case DW_TAG_namespace:
4802     case DW_TAG_typedef:
4803     case DW_TAG_class_type:
4804     case DW_TAG_interface_type:
4805     case DW_TAG_structure_type:
4806     case DW_TAG_union_type:
4807     case DW_TAG_enumeration_type:
4808     case DW_TAG_enumerator:
4809     case DW_TAG_subprogram:
4810     case DW_TAG_member:
4811       return 1;
4812
4813     case DW_TAG_variable:
4814     case DW_TAG_constant:
4815       /* We only need to prefix "globally" visible variables.  These include
4816          any variable marked with DW_AT_external or any variable that
4817          lives in a namespace.  [Variables in anonymous namespaces
4818          require prefixing, but they are not DW_AT_external.]  */
4819
4820       if (dwarf2_attr (die, DW_AT_specification, cu))
4821         {
4822           struct dwarf2_cu *spec_cu = cu;
4823
4824           return die_needs_namespace (die_specification (die, &spec_cu),
4825                                       spec_cu);
4826         }
4827
4828       attr = dwarf2_attr (die, DW_AT_external, cu);
4829       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4830           && die->parent->tag != DW_TAG_module)
4831         return 0;
4832       /* A variable in a lexical block of some kind does not need a
4833          namespace, even though in C++ such variables may be external
4834          and have a mangled name.  */
4835       if (die->parent->tag ==  DW_TAG_lexical_block
4836           || die->parent->tag ==  DW_TAG_try_block
4837           || die->parent->tag ==  DW_TAG_catch_block
4838           || die->parent->tag == DW_TAG_subprogram)
4839         return 0;
4840       return 1;
4841
4842     default:
4843       return 0;
4844     }
4845 }
4846
4847 /* Retrieve the last character from a mem_file.  */
4848
4849 static void
4850 do_ui_file_peek_last (void *object, const char *buffer, long length)
4851 {
4852   char *last_char_p = (char *) object;
4853
4854   if (length > 0)
4855     *last_char_p = buffer[length - 1];
4856 }
4857
4858 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4859    compute the physname for the object, which include a method's
4860    formal parameters (C++/Java) and return type (Java).
4861
4862    For Ada, return the DIE's linkage name rather than the fully qualified
4863    name.  PHYSNAME is ignored..
4864
4865    The result is allocated on the objfile_obstack and canonicalized.  */
4866
4867 static const char *
4868 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4869                      int physname)
4870 {
4871   if (name == NULL)
4872     name = dwarf2_name (die, cu);
4873
4874   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4875      compute it by typename_concat inside GDB.  */
4876   if (cu->language == language_ada
4877       || (cu->language == language_fortran && physname))
4878     {
4879       /* For Ada unit, we prefer the linkage name over the name, as
4880          the former contains the exported name, which the user expects
4881          to be able to reference.  Ideally, we want the user to be able
4882          to reference this entity using either natural or linkage name,
4883          but we haven't started looking at this enhancement yet.  */
4884       struct attribute *attr;
4885
4886       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4887       if (attr == NULL)
4888         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4889       if (attr && DW_STRING (attr))
4890         return DW_STRING (attr);
4891     }
4892
4893   /* These are the only languages we know how to qualify names in.  */
4894   if (name != NULL
4895       && (cu->language == language_cplus || cu->language == language_java
4896           || cu->language == language_fortran))
4897     {
4898       if (die_needs_namespace (die, cu))
4899         {
4900           long length;
4901           char *prefix;
4902           struct ui_file *buf;
4903
4904           prefix = determine_prefix (die, cu);
4905           buf = mem_fileopen ();
4906           if (*prefix != '\0')
4907             {
4908               char *prefixed_name = typename_concat (NULL, prefix, name,
4909                                                      physname, cu);
4910
4911               fputs_unfiltered (prefixed_name, buf);
4912               xfree (prefixed_name);
4913             }
4914           else
4915             fputs_unfiltered (name, buf);
4916
4917           /* Template parameters may be specified in the DIE's DW_AT_name, or
4918              as children with DW_TAG_template_type_param or
4919              DW_TAG_value_type_param.  If the latter, add them to the name
4920              here.  If the name already has template parameters, then
4921              skip this step; some versions of GCC emit both, and
4922              it is more efficient to use the pre-computed name.
4923
4924              Something to keep in mind about this process: it is very
4925              unlikely, or in some cases downright impossible, to produce
4926              something that will match the mangled name of a function.
4927              If the definition of the function has the same debug info,
4928              we should be able to match up with it anyway.  But fallbacks
4929              using the minimal symbol, for instance to find a method
4930              implemented in a stripped copy of libstdc++, will not work.
4931              If we do not have debug info for the definition, we will have to
4932              match them up some other way.
4933
4934              When we do name matching there is a related problem with function
4935              templates; two instantiated function templates are allowed to
4936              differ only by their return types, which we do not add here.  */
4937
4938           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4939             {
4940               struct attribute *attr;
4941               struct die_info *child;
4942               int first = 1;
4943
4944               die->building_fullname = 1;
4945
4946               for (child = die->child; child != NULL; child = child->sibling)
4947                 {
4948                   struct type *type;
4949                   long value;
4950                   gdb_byte *bytes;
4951                   struct dwarf2_locexpr_baton *baton;
4952                   struct value *v;
4953
4954                   if (child->tag != DW_TAG_template_type_param
4955                       && child->tag != DW_TAG_template_value_param)
4956                     continue;
4957
4958                   if (first)
4959                     {
4960                       fputs_unfiltered ("<", buf);
4961                       first = 0;
4962                     }
4963                   else
4964                     fputs_unfiltered (", ", buf);
4965
4966                   attr = dwarf2_attr (child, DW_AT_type, cu);
4967                   if (attr == NULL)
4968                     {
4969                       complaint (&symfile_complaints,
4970                                  _("template parameter missing DW_AT_type"));
4971                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4972                       continue;
4973                     }
4974                   type = die_type (child, cu);
4975
4976                   if (child->tag == DW_TAG_template_type_param)
4977                     {
4978                       c_print_type (type, "", buf, -1, 0);
4979                       continue;
4980                     }
4981
4982                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4983                   if (attr == NULL)
4984                     {
4985                       complaint (&symfile_complaints,
4986                                  _("template parameter missing "
4987                                    "DW_AT_const_value"));
4988                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4989                       continue;
4990                     }
4991
4992                   dwarf2_const_value_attr (attr, type, name,
4993                                            &cu->comp_unit_obstack, cu,
4994                                            &value, &bytes, &baton);
4995
4996                   if (TYPE_NOSIGN (type))
4997                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4998                        changed, this can use value_print instead.  */
4999                     c_printchar (value, type, buf);
5000                   else
5001                     {
5002                       struct value_print_options opts;
5003
5004                       if (baton != NULL)
5005                         v = dwarf2_evaluate_loc_desc (type, NULL,
5006                                                       baton->data,
5007                                                       baton->size,
5008                                                       baton->per_cu);
5009                       else if (bytes != NULL)
5010                         {
5011                           v = allocate_value (type);
5012                           memcpy (value_contents_writeable (v), bytes,
5013                                   TYPE_LENGTH (type));
5014                         }
5015                       else
5016                         v = value_from_longest (type, value);
5017
5018                       /* Specify decimal so that we do not depend on
5019                          the radix.  */
5020                       get_formatted_print_options (&opts, 'd');
5021                       opts.raw = 1;
5022                       value_print (v, buf, &opts);
5023                       release_value (v);
5024                       value_free (v);
5025                     }
5026                 }
5027
5028               die->building_fullname = 0;
5029
5030               if (!first)
5031                 {
5032                   /* Close the argument list, with a space if necessary
5033                      (nested templates).  */
5034                   char last_char = '\0';
5035                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5036                   if (last_char == '>')
5037                     fputs_unfiltered (" >", buf);
5038                   else
5039                     fputs_unfiltered (">", buf);
5040                 }
5041             }
5042
5043           /* For Java and C++ methods, append formal parameter type
5044              information, if PHYSNAME.  */
5045
5046           if (physname && die->tag == DW_TAG_subprogram
5047               && (cu->language == language_cplus
5048                   || cu->language == language_java))
5049             {
5050               struct type *type = read_type_die (die, cu);
5051
5052               c_type_print_args (type, buf, 1, cu->language);
5053
5054               if (cu->language == language_java)
5055                 {
5056                   /* For java, we must append the return type to method
5057                      names.  */
5058                   if (die->tag == DW_TAG_subprogram)
5059                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5060                                      0, 0);
5061                 }
5062               else if (cu->language == language_cplus)
5063                 {
5064                   /* Assume that an artificial first parameter is
5065                      "this", but do not crash if it is not.  RealView
5066                      marks unnamed (and thus unused) parameters as
5067                      artificial; there is no way to differentiate
5068                      the two cases.  */
5069                   if (TYPE_NFIELDS (type) > 0
5070                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5071                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5072                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5073                                                                         0))))
5074                     fputs_unfiltered (" const", buf);
5075                 }
5076             }
5077
5078           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5079                                        &length);
5080           ui_file_delete (buf);
5081
5082           if (cu->language == language_cplus)
5083             {
5084               char *cname
5085                 = dwarf2_canonicalize_name (name, cu,
5086                                             &cu->objfile->objfile_obstack);
5087
5088               if (cname != NULL)
5089                 name = cname;
5090             }
5091         }
5092     }
5093
5094   return name;
5095 }
5096
5097 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5098    If scope qualifiers are appropriate they will be added.  The result
5099    will be allocated on the objfile_obstack, or NULL if the DIE does
5100    not have a name.  NAME may either be from a previous call to
5101    dwarf2_name or NULL.
5102
5103    The output string will be canonicalized (if C++/Java).  */
5104
5105 static const char *
5106 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5107 {
5108   return dwarf2_compute_name (name, die, cu, 0);
5109 }
5110
5111 /* Construct a physname for the given DIE in CU.  NAME may either be
5112    from a previous call to dwarf2_name or NULL.  The result will be
5113    allocated on the objfile_objstack or NULL if the DIE does not have a
5114    name.
5115
5116    The output string will be canonicalized (if C++/Java).  */
5117
5118 static const char *
5119 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5120 {
5121   struct attribute *attr;
5122   const char *retval, *mangled = NULL, *canon = NULL;
5123   struct cleanup *back_to;
5124   int need_copy = 1;
5125
5126   /* In this case dwarf2_compute_name is just a shortcut not building anything
5127      on its own.  */
5128   if (!die_needs_namespace (die, cu))
5129     return dwarf2_compute_name (name, die, cu, 1);
5130
5131   back_to = make_cleanup (null_cleanup, NULL);
5132
5133   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5134   if (!attr)
5135     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5136
5137   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5138      has computed.  */
5139   if (attr && DW_STRING (attr))
5140     {
5141       char *demangled;
5142
5143       mangled = DW_STRING (attr);
5144
5145       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5146          type.  It is easier for GDB users to search for such functions as
5147          `name(params)' than `long name(params)'.  In such case the minimal
5148          symbol names do not match the full symbol names but for template
5149          functions there is never a need to look up their definition from their
5150          declaration so the only disadvantage remains the minimal symbol
5151          variant `long name(params)' does not have the proper inferior type.
5152          */
5153
5154       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5155                                             | (cu->language == language_java
5156                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5157                                                : DMGL_RET_DROP)));
5158       if (demangled)
5159         {
5160           make_cleanup (xfree, demangled);
5161           canon = demangled;
5162         }
5163       else
5164         {
5165           canon = mangled;
5166           need_copy = 0;
5167         }
5168     }
5169
5170   if (canon == NULL || check_physname)
5171     {
5172       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5173
5174       if (canon != NULL && strcmp (physname, canon) != 0)
5175         {
5176           /* It may not mean a bug in GDB.  The compiler could also
5177              compute DW_AT_linkage_name incorrectly.  But in such case
5178              GDB would need to be bug-to-bug compatible.  */
5179
5180           complaint (&symfile_complaints,
5181                      _("Computed physname <%s> does not match demangled <%s> "
5182                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5183                      physname, canon, mangled, die->offset, cu->objfile->name);
5184
5185           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5186              is available here - over computed PHYSNAME.  It is safer
5187              against both buggy GDB and buggy compilers.  */
5188
5189           retval = canon;
5190         }
5191       else
5192         {
5193           retval = physname;
5194           need_copy = 0;
5195         }
5196     }
5197   else
5198     retval = canon;
5199
5200   if (need_copy)
5201     retval = obsavestring (retval, strlen (retval),
5202                            &cu->objfile->objfile_obstack);
5203
5204   do_cleanups (back_to);
5205   return retval;
5206 }
5207
5208 /* Read the import statement specified by the given die and record it.  */
5209
5210 static void
5211 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5212 {
5213   struct attribute *import_attr;
5214   struct die_info *imported_die;
5215   struct dwarf2_cu *imported_cu;
5216   const char *imported_name;
5217   const char *imported_name_prefix;
5218   const char *canonical_name;
5219   const char *import_alias;
5220   const char *imported_declaration = NULL;
5221   const char *import_prefix;
5222
5223   char *temp;
5224
5225   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5226   if (import_attr == NULL)
5227     {
5228       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5229                  dwarf_tag_name (die->tag));
5230       return;
5231     }
5232
5233   imported_cu = cu;
5234   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5235   imported_name = dwarf2_name (imported_die, imported_cu);
5236   if (imported_name == NULL)
5237     {
5238       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5239
5240         The import in the following code:
5241         namespace A
5242           {
5243             typedef int B;
5244           }
5245
5246         int main ()
5247           {
5248             using A::B;
5249             B b;
5250             return b;
5251           }
5252
5253         ...
5254          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5255             <52>   DW_AT_decl_file   : 1
5256             <53>   DW_AT_decl_line   : 6
5257             <54>   DW_AT_import      : <0x75>
5258          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5259             <59>   DW_AT_name        : B
5260             <5b>   DW_AT_decl_file   : 1
5261             <5c>   DW_AT_decl_line   : 2
5262             <5d>   DW_AT_type        : <0x6e>
5263         ...
5264          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5265             <76>   DW_AT_byte_size   : 4
5266             <77>   DW_AT_encoding    : 5        (signed)
5267
5268         imports the wrong die ( 0x75 instead of 0x58 ).
5269         This case will be ignored until the gcc bug is fixed.  */
5270       return;
5271     }
5272
5273   /* Figure out the local name after import.  */
5274   import_alias = dwarf2_name (die, cu);
5275
5276   /* Figure out where the statement is being imported to.  */
5277   import_prefix = determine_prefix (die, cu);
5278
5279   /* Figure out what the scope of the imported die is and prepend it
5280      to the name of the imported die.  */
5281   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5282
5283   if (imported_die->tag != DW_TAG_namespace
5284       && imported_die->tag != DW_TAG_module)
5285     {
5286       imported_declaration = imported_name;
5287       canonical_name = imported_name_prefix;
5288     }
5289   else if (strlen (imported_name_prefix) > 0)
5290     {
5291       temp = alloca (strlen (imported_name_prefix)
5292                      + 2 + strlen (imported_name) + 1);
5293       strcpy (temp, imported_name_prefix);
5294       strcat (temp, "::");
5295       strcat (temp, imported_name);
5296       canonical_name = temp;
5297     }
5298   else
5299     canonical_name = imported_name;
5300
5301   cp_add_using_directive (import_prefix,
5302                           canonical_name,
5303                           import_alias,
5304                           imported_declaration,
5305                           &cu->objfile->objfile_obstack);
5306 }
5307
5308 static void
5309 initialize_cu_func_list (struct dwarf2_cu *cu)
5310 {
5311   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5312 }
5313
5314 /* Cleanup function for read_file_scope.  */
5315
5316 static void
5317 free_cu_line_header (void *arg)
5318 {
5319   struct dwarf2_cu *cu = arg;
5320
5321   free_line_header (cu->line_header);
5322   cu->line_header = NULL;
5323 }
5324
5325 static void
5326 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5327                          char **name, char **comp_dir)
5328 {
5329   struct attribute *attr;
5330
5331   *name = NULL;
5332   *comp_dir = NULL;
5333
5334   /* Find the filename.  Do not use dwarf2_name here, since the filename
5335      is not a source language identifier.  */
5336   attr = dwarf2_attr (die, DW_AT_name, cu);
5337   if (attr)
5338     {
5339       *name = DW_STRING (attr);
5340     }
5341
5342   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5343   if (attr)
5344     *comp_dir = DW_STRING (attr);
5345   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5346     {
5347       *comp_dir = ldirname (*name);
5348       if (*comp_dir != NULL)
5349         make_cleanup (xfree, *comp_dir);
5350     }
5351   if (*comp_dir != NULL)
5352     {
5353       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5354          directory, get rid of it.  */
5355       char *cp = strchr (*comp_dir, ':');
5356
5357       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5358         *comp_dir = cp + 1;
5359     }
5360
5361   if (*name == NULL)
5362     *name = "<unknown>";
5363 }
5364
5365 /* Process DW_TAG_compile_unit.  */
5366
5367 static void
5368 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5369 {
5370   struct objfile *objfile = cu->objfile;
5371   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5372   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5373   CORE_ADDR highpc = ((CORE_ADDR) 0);
5374   struct attribute *attr;
5375   char *name = NULL;
5376   char *comp_dir = NULL;
5377   struct die_info *child_die;
5378   bfd *abfd = objfile->obfd;
5379   struct line_header *line_header = 0;
5380   CORE_ADDR baseaddr;
5381
5382   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5383
5384   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5385
5386   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5387      from finish_block.  */
5388   if (lowpc == ((CORE_ADDR) -1))
5389     lowpc = highpc;
5390   lowpc += baseaddr;
5391   highpc += baseaddr;
5392
5393   find_file_and_directory (die, cu, &name, &comp_dir);
5394
5395   attr = dwarf2_attr (die, DW_AT_language, cu);
5396   if (attr)
5397     {
5398       set_cu_language (DW_UNSND (attr), cu);
5399     }
5400
5401   attr = dwarf2_attr (die, DW_AT_producer, cu);
5402   if (attr)
5403     cu->producer = DW_STRING (attr);
5404
5405   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5406      standardised yet.  As a workaround for the language detection we fall
5407      back to the DW_AT_producer string.  */
5408   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5409     cu->language = language_opencl;
5410
5411   /* We assume that we're processing GCC output.  */
5412   processing_gcc_compilation = 2;
5413
5414   processing_has_namespace_info = 0;
5415
5416   start_symtab (name, comp_dir, lowpc);
5417   record_debugformat ("DWARF 2");
5418   record_producer (cu->producer);
5419
5420   initialize_cu_func_list (cu);
5421
5422   /* Decode line number information if present.  We do this before
5423      processing child DIEs, so that the line header table is available
5424      for DW_AT_decl_file.  */
5425   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5426   if (attr)
5427     {
5428       unsigned int line_offset = DW_UNSND (attr);
5429       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5430       if (line_header)
5431         {
5432           cu->line_header = line_header;
5433           make_cleanup (free_cu_line_header, cu);
5434           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5435         }
5436     }
5437
5438   /* Process all dies in compilation unit.  */
5439   if (die->child != NULL)
5440     {
5441       child_die = die->child;
5442       while (child_die && child_die->tag)
5443         {
5444           process_die (child_die, cu);
5445           child_die = sibling_die (child_die);
5446         }
5447     }
5448
5449   /* Decode macro information, if present.  Dwarf 2 macro information
5450      refers to information in the line number info statement program
5451      header, so we can only read it if we've read the header
5452      successfully.  */
5453   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5454   if (attr && line_header)
5455     {
5456       unsigned int macro_offset = DW_UNSND (attr);
5457
5458       dwarf_decode_macros (line_header, macro_offset,
5459                            comp_dir, abfd, cu);
5460     }
5461   do_cleanups (back_to);
5462 }
5463
5464 /* Process DW_TAG_type_unit.
5465    For TUs we want to skip the first top level sibling if it's not the
5466    actual type being defined by this TU.  In this case the first top
5467    level sibling is there to provide context only.  */
5468
5469 static void
5470 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5471 {
5472   struct objfile *objfile = cu->objfile;
5473   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5474   CORE_ADDR lowpc;
5475   struct attribute *attr;
5476   char *name = NULL;
5477   char *comp_dir = NULL;
5478   struct die_info *child_die;
5479   bfd *abfd = objfile->obfd;
5480
5481   /* start_symtab needs a low pc, but we don't really have one.
5482      Do what read_file_scope would do in the absence of such info.  */
5483   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5484
5485   /* Find the filename.  Do not use dwarf2_name here, since the filename
5486      is not a source language identifier.  */
5487   attr = dwarf2_attr (die, DW_AT_name, cu);
5488   if (attr)
5489     name = DW_STRING (attr);
5490
5491   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5492   if (attr)
5493     comp_dir = DW_STRING (attr);
5494   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5495     {
5496       comp_dir = ldirname (name);
5497       if (comp_dir != NULL)
5498         make_cleanup (xfree, comp_dir);
5499     }
5500
5501   if (name == NULL)
5502     name = "<unknown>";
5503
5504   attr = dwarf2_attr (die, DW_AT_language, cu);
5505   if (attr)
5506     set_cu_language (DW_UNSND (attr), cu);
5507
5508   /* This isn't technically needed today.  It is done for symmetry
5509      with read_file_scope.  */
5510   attr = dwarf2_attr (die, DW_AT_producer, cu);
5511   if (attr)
5512     cu->producer = DW_STRING (attr);
5513
5514   /* We assume that we're processing GCC output.  */
5515   processing_gcc_compilation = 2;
5516
5517   processing_has_namespace_info = 0;
5518
5519   start_symtab (name, comp_dir, lowpc);
5520   record_debugformat ("DWARF 2");
5521   record_producer (cu->producer);
5522
5523   /* Process the dies in the type unit.  */
5524   if (die->child == NULL)
5525     {
5526       dump_die_for_error (die);
5527       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5528              bfd_get_filename (abfd));
5529     }
5530
5531   child_die = die->child;
5532
5533   while (child_die && child_die->tag)
5534     {
5535       process_die (child_die, cu);
5536
5537       child_die = sibling_die (child_die);
5538     }
5539
5540   do_cleanups (back_to);
5541 }
5542
5543 static void
5544 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5545                      struct dwarf2_cu *cu)
5546 {
5547   struct function_range *thisfn;
5548
5549   thisfn = (struct function_range *)
5550     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5551   thisfn->name = name;
5552   thisfn->lowpc = lowpc;
5553   thisfn->highpc = highpc;
5554   thisfn->seen_line = 0;
5555   thisfn->next = NULL;
5556
5557   if (cu->last_fn == NULL)
5558       cu->first_fn = thisfn;
5559   else
5560       cu->last_fn->next = thisfn;
5561
5562   cu->last_fn = thisfn;
5563 }
5564
5565 /* qsort helper for inherit_abstract_dies.  */
5566
5567 static int
5568 unsigned_int_compar (const void *ap, const void *bp)
5569 {
5570   unsigned int a = *(unsigned int *) ap;
5571   unsigned int b = *(unsigned int *) bp;
5572
5573   return (a > b) - (b > a);
5574 }
5575
5576 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5577    Inherit only the children of the DW_AT_abstract_origin DIE not being
5578    already referenced by DW_AT_abstract_origin from the children of the
5579    current DIE.  */
5580
5581 static void
5582 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5583 {
5584   struct die_info *child_die;
5585   unsigned die_children_count;
5586   /* CU offsets which were referenced by children of the current DIE.  */
5587   unsigned *offsets;
5588   unsigned *offsets_end, *offsetp;
5589   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5590   struct die_info *origin_die;
5591   /* Iterator of the ORIGIN_DIE children.  */
5592   struct die_info *origin_child_die;
5593   struct cleanup *cleanups;
5594   struct attribute *attr;
5595   struct dwarf2_cu *origin_cu;
5596   struct pending **origin_previous_list_in_scope;
5597
5598   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5599   if (!attr)
5600     return;
5601
5602   /* Note that following die references may follow to a die in a
5603      different cu.  */
5604
5605   origin_cu = cu;
5606   origin_die = follow_die_ref (die, attr, &origin_cu);
5607
5608   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5609      symbols in.  */
5610   origin_previous_list_in_scope = origin_cu->list_in_scope;
5611   origin_cu->list_in_scope = cu->list_in_scope;
5612
5613   if (die->tag != origin_die->tag
5614       && !(die->tag == DW_TAG_inlined_subroutine
5615            && origin_die->tag == DW_TAG_subprogram))
5616     complaint (&symfile_complaints,
5617                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5618                die->offset, origin_die->offset);
5619
5620   child_die = die->child;
5621   die_children_count = 0;
5622   while (child_die && child_die->tag)
5623     {
5624       child_die = sibling_die (child_die);
5625       die_children_count++;
5626     }
5627   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5628   cleanups = make_cleanup (xfree, offsets);
5629
5630   offsets_end = offsets;
5631   child_die = die->child;
5632   while (child_die && child_die->tag)
5633     {
5634       /* For each CHILD_DIE, find the corresponding child of
5635          ORIGIN_DIE.  If there is more than one layer of
5636          DW_AT_abstract_origin, follow them all; there shouldn't be,
5637          but GCC versions at least through 4.4 generate this (GCC PR
5638          40573).  */
5639       struct die_info *child_origin_die = child_die;
5640       struct dwarf2_cu *child_origin_cu = cu;
5641
5642       while (1)
5643         {
5644           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5645                               child_origin_cu);
5646           if (attr == NULL)
5647             break;
5648           child_origin_die = follow_die_ref (child_origin_die, attr,
5649                                              &child_origin_cu);
5650         }
5651
5652       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5653          counterpart may exist.  */
5654       if (child_origin_die != child_die)
5655         {
5656           if (child_die->tag != child_origin_die->tag
5657               && !(child_die->tag == DW_TAG_inlined_subroutine
5658                    && child_origin_die->tag == DW_TAG_subprogram))
5659             complaint (&symfile_complaints,
5660                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5661                          "different tags"), child_die->offset,
5662                        child_origin_die->offset);
5663           if (child_origin_die->parent != origin_die)
5664             complaint (&symfile_complaints,
5665                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5666                          "different parents"), child_die->offset,
5667                        child_origin_die->offset);
5668           else
5669             *offsets_end++ = child_origin_die->offset;
5670         }
5671       child_die = sibling_die (child_die);
5672     }
5673   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5674          unsigned_int_compar);
5675   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5676     if (offsetp[-1] == *offsetp)
5677       complaint (&symfile_complaints,
5678                  _("Multiple children of DIE 0x%x refer "
5679                    "to DIE 0x%x as their abstract origin"),
5680                  die->offset, *offsetp);
5681
5682   offsetp = offsets;
5683   origin_child_die = origin_die->child;
5684   while (origin_child_die && origin_child_die->tag)
5685     {
5686       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5687       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5688         offsetp++;
5689       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5690         {
5691           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5692           process_die (origin_child_die, origin_cu);
5693         }
5694       origin_child_die = sibling_die (origin_child_die);
5695     }
5696   origin_cu->list_in_scope = origin_previous_list_in_scope;
5697
5698   do_cleanups (cleanups);
5699 }
5700
5701 static void
5702 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5703 {
5704   struct objfile *objfile = cu->objfile;
5705   struct context_stack *new;
5706   CORE_ADDR lowpc;
5707   CORE_ADDR highpc;
5708   struct die_info *child_die;
5709   struct attribute *attr, *call_line, *call_file;
5710   char *name;
5711   CORE_ADDR baseaddr;
5712   struct block *block;
5713   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5714   VEC (symbolp) *template_args = NULL;
5715   struct template_symbol *templ_func = NULL;
5716
5717   if (inlined_func)
5718     {
5719       /* If we do not have call site information, we can't show the
5720          caller of this inlined function.  That's too confusing, so
5721          only use the scope for local variables.  */
5722       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5723       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5724       if (call_line == NULL || call_file == NULL)
5725         {
5726           read_lexical_block_scope (die, cu);
5727           return;
5728         }
5729     }
5730
5731   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5732
5733   name = dwarf2_name (die, cu);
5734
5735   /* Ignore functions with missing or empty names.  These are actually
5736      illegal according to the DWARF standard.  */
5737   if (name == NULL)
5738     {
5739       complaint (&symfile_complaints,
5740                  _("missing name for subprogram DIE at %d"), die->offset);
5741       return;
5742     }
5743
5744   /* Ignore functions with missing or invalid low and high pc attributes.  */
5745   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5746     {
5747       attr = dwarf2_attr (die, DW_AT_external, cu);
5748       if (!attr || !DW_UNSND (attr))
5749         complaint (&symfile_complaints,
5750                    _("cannot get low and high bounds "
5751                      "for subprogram DIE at %d"),
5752                    die->offset);
5753       return;
5754     }
5755
5756   lowpc += baseaddr;
5757   highpc += baseaddr;
5758
5759   /* Record the function range for dwarf_decode_lines.  */
5760   add_to_cu_func_list (name, lowpc, highpc, cu);
5761
5762   /* If we have any template arguments, then we must allocate a
5763      different sort of symbol.  */
5764   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5765     {
5766       if (child_die->tag == DW_TAG_template_type_param
5767           || child_die->tag == DW_TAG_template_value_param)
5768         {
5769           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5770                                        struct template_symbol);
5771           templ_func->base.is_cplus_template_function = 1;
5772           break;
5773         }
5774     }
5775
5776   new = push_context (0, lowpc);
5777   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5778                                (struct symbol *) templ_func);
5779
5780   /* If there is a location expression for DW_AT_frame_base, record
5781      it.  */
5782   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5783   if (attr)
5784     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5785        expression is being recorded directly in the function's symbol
5786        and not in a separate frame-base object.  I guess this hack is
5787        to avoid adding some sort of frame-base adjunct/annex to the
5788        function's symbol :-(.  The problem with doing this is that it
5789        results in a function symbol with a location expression that
5790        has nothing to do with the location of the function, ouch!  The
5791        relationship should be: a function's symbol has-a frame base; a
5792        frame-base has-a location expression.  */
5793     dwarf2_symbol_mark_computed (attr, new->name, cu);
5794
5795   cu->list_in_scope = &local_symbols;
5796
5797   if (die->child != NULL)
5798     {
5799       child_die = die->child;
5800       while (child_die && child_die->tag)
5801         {
5802           if (child_die->tag == DW_TAG_template_type_param
5803               || child_die->tag == DW_TAG_template_value_param)
5804             {
5805               struct symbol *arg = new_symbol (child_die, NULL, cu);
5806
5807               if (arg != NULL)
5808                 VEC_safe_push (symbolp, template_args, arg);
5809             }
5810           else
5811             process_die (child_die, cu);
5812           child_die = sibling_die (child_die);
5813         }
5814     }
5815
5816   inherit_abstract_dies (die, cu);
5817
5818   /* If we have a DW_AT_specification, we might need to import using
5819      directives from the context of the specification DIE.  See the
5820      comment in determine_prefix.  */
5821   if (cu->language == language_cplus
5822       && dwarf2_attr (die, DW_AT_specification, cu))
5823     {
5824       struct dwarf2_cu *spec_cu = cu;
5825       struct die_info *spec_die = die_specification (die, &spec_cu);
5826
5827       while (spec_die)
5828         {
5829           child_die = spec_die->child;
5830           while (child_die && child_die->tag)
5831             {
5832               if (child_die->tag == DW_TAG_imported_module)
5833                 process_die (child_die, spec_cu);
5834               child_die = sibling_die (child_die);
5835             }
5836
5837           /* In some cases, GCC generates specification DIEs that
5838              themselves contain DW_AT_specification attributes.  */
5839           spec_die = die_specification (spec_die, &spec_cu);
5840         }
5841     }
5842
5843   new = pop_context ();
5844   /* Make a block for the local symbols within.  */
5845   block = finish_block (new->name, &local_symbols, new->old_blocks,
5846                         lowpc, highpc, objfile);
5847
5848   /* For C++, set the block's scope.  */
5849   if (cu->language == language_cplus || cu->language == language_fortran)
5850     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5851                         determine_prefix (die, cu),
5852                         processing_has_namespace_info);
5853
5854   /* If we have address ranges, record them.  */
5855   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5856
5857   /* Attach template arguments to function.  */
5858   if (! VEC_empty (symbolp, template_args))
5859     {
5860       gdb_assert (templ_func != NULL);
5861
5862       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5863       templ_func->template_arguments
5864         = obstack_alloc (&objfile->objfile_obstack,
5865                          (templ_func->n_template_arguments
5866                           * sizeof (struct symbol *)));
5867       memcpy (templ_func->template_arguments,
5868               VEC_address (symbolp, template_args),
5869               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5870       VEC_free (symbolp, template_args);
5871     }
5872
5873   /* In C++, we can have functions nested inside functions (e.g., when
5874      a function declares a class that has methods).  This means that
5875      when we finish processing a function scope, we may need to go
5876      back to building a containing block's symbol lists.  */
5877   local_symbols = new->locals;
5878   param_symbols = new->params;
5879   using_directives = new->using_directives;
5880
5881   /* If we've finished processing a top-level function, subsequent
5882      symbols go in the file symbol list.  */
5883   if (outermost_context_p ())
5884     cu->list_in_scope = &file_symbols;
5885 }
5886
5887 /* Process all the DIES contained within a lexical block scope.  Start
5888    a new scope, process the dies, and then close the scope.  */
5889
5890 static void
5891 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5892 {
5893   struct objfile *objfile = cu->objfile;
5894   struct context_stack *new;
5895   CORE_ADDR lowpc, highpc;
5896   struct die_info *child_die;
5897   CORE_ADDR baseaddr;
5898
5899   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5900
5901   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5902   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5903      as multiple lexical blocks?  Handling children in a sane way would
5904      be nasty.  Might be easier to properly extend generic blocks to
5905      describe ranges.  */
5906   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5907     return;
5908   lowpc += baseaddr;
5909   highpc += baseaddr;
5910
5911   push_context (0, lowpc);
5912   if (die->child != NULL)
5913     {
5914       child_die = die->child;
5915       while (child_die && child_die->tag)
5916         {
5917           process_die (child_die, cu);
5918           child_die = sibling_die (child_die);
5919         }
5920     }
5921   new = pop_context ();
5922
5923   if (local_symbols != NULL || using_directives != NULL)
5924     {
5925       struct block *block
5926         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5927                         highpc, objfile);
5928
5929       /* Note that recording ranges after traversing children, as we
5930          do here, means that recording a parent's ranges entails
5931          walking across all its children's ranges as they appear in
5932          the address map, which is quadratic behavior.
5933
5934          It would be nicer to record the parent's ranges before
5935          traversing its children, simply overriding whatever you find
5936          there.  But since we don't even decide whether to create a
5937          block until after we've traversed its children, that's hard
5938          to do.  */
5939       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5940     }
5941   local_symbols = new->locals;
5942   using_directives = new->using_directives;
5943 }
5944
5945 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5946    Return 1 if the attributes are present and valid, otherwise, return 0.
5947    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5948
5949 static int
5950 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5951                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5952                     struct partial_symtab *ranges_pst)
5953 {
5954   struct objfile *objfile = cu->objfile;
5955   struct comp_unit_head *cu_header = &cu->header;
5956   bfd *obfd = objfile->obfd;
5957   unsigned int addr_size = cu_header->addr_size;
5958   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5959   /* Base address selection entry.  */
5960   CORE_ADDR base;
5961   int found_base;
5962   unsigned int dummy;
5963   gdb_byte *buffer;
5964   CORE_ADDR marker;
5965   int low_set;
5966   CORE_ADDR low = 0;
5967   CORE_ADDR high = 0;
5968   CORE_ADDR baseaddr;
5969
5970   found_base = cu->base_known;
5971   base = cu->base_address;
5972
5973   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5974   if (offset >= dwarf2_per_objfile->ranges.size)
5975     {
5976       complaint (&symfile_complaints,
5977                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5978                  offset);
5979       return 0;
5980     }
5981   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5982
5983   /* Read in the largest possible address.  */
5984   marker = read_address (obfd, buffer, cu, &dummy);
5985   if ((marker & mask) == mask)
5986     {
5987       /* If we found the largest possible address, then
5988          read the base address.  */
5989       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5990       buffer += 2 * addr_size;
5991       offset += 2 * addr_size;
5992       found_base = 1;
5993     }
5994
5995   low_set = 0;
5996
5997   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5998
5999   while (1)
6000     {
6001       CORE_ADDR range_beginning, range_end;
6002
6003       range_beginning = read_address (obfd, buffer, cu, &dummy);
6004       buffer += addr_size;
6005       range_end = read_address (obfd, buffer, cu, &dummy);
6006       buffer += addr_size;
6007       offset += 2 * addr_size;
6008
6009       /* An end of list marker is a pair of zero addresses.  */
6010       if (range_beginning == 0 && range_end == 0)
6011         /* Found the end of list entry.  */
6012         break;
6013
6014       /* Each base address selection entry is a pair of 2 values.
6015          The first is the largest possible address, the second is
6016          the base address.  Check for a base address here.  */
6017       if ((range_beginning & mask) == mask)
6018         {
6019           /* If we found the largest possible address, then
6020              read the base address.  */
6021           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6022           found_base = 1;
6023           continue;
6024         }
6025
6026       if (!found_base)
6027         {
6028           /* We have no valid base address for the ranges
6029              data.  */
6030           complaint (&symfile_complaints,
6031                      _("Invalid .debug_ranges data (no base address)"));
6032           return 0;
6033         }
6034
6035       if (range_beginning > range_end)
6036         {
6037           /* Inverted range entries are invalid.  */
6038           complaint (&symfile_complaints,
6039                      _("Invalid .debug_ranges data (inverted range)"));
6040           return 0;
6041         }
6042
6043       /* Empty range entries have no effect.  */
6044       if (range_beginning == range_end)
6045         continue;
6046
6047       range_beginning += base;
6048       range_end += base;
6049
6050       if (ranges_pst != NULL)
6051         addrmap_set_empty (objfile->psymtabs_addrmap,
6052                            range_beginning + baseaddr,
6053                            range_end - 1 + baseaddr,
6054                            ranges_pst);
6055
6056       /* FIXME: This is recording everything as a low-high
6057          segment of consecutive addresses.  We should have a
6058          data structure for discontiguous block ranges
6059          instead.  */
6060       if (! low_set)
6061         {
6062           low = range_beginning;
6063           high = range_end;
6064           low_set = 1;
6065         }
6066       else
6067         {
6068           if (range_beginning < low)
6069             low = range_beginning;
6070           if (range_end > high)
6071             high = range_end;
6072         }
6073     }
6074
6075   if (! low_set)
6076     /* If the first entry is an end-of-list marker, the range
6077        describes an empty scope, i.e. no instructions.  */
6078     return 0;
6079
6080   if (low_return)
6081     *low_return = low;
6082   if (high_return)
6083     *high_return = high;
6084   return 1;
6085 }
6086
6087 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6088    are present and valid, otherwise, return 0.  Return -1 if the range is
6089    discontinuous, i.e. derived from DW_AT_ranges information.  */
6090 static int
6091 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6092                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6093                       struct partial_symtab *pst)
6094 {
6095   struct attribute *attr;
6096   CORE_ADDR low = 0;
6097   CORE_ADDR high = 0;
6098   int ret = 0;
6099
6100   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6101   if (attr)
6102     {
6103       high = DW_ADDR (attr);
6104       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6105       if (attr)
6106         low = DW_ADDR (attr);
6107       else
6108         /* Found high w/o low attribute.  */
6109         return 0;
6110
6111       /* Found consecutive range of addresses.  */
6112       ret = 1;
6113     }
6114   else
6115     {
6116       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6117       if (attr != NULL)
6118         {
6119           /* Value of the DW_AT_ranges attribute is the offset in the
6120              .debug_ranges section.  */
6121           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6122             return 0;
6123           /* Found discontinuous range of addresses.  */
6124           ret = -1;
6125         }
6126     }
6127
6128   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6129   if (high <= low)
6130     return 0;
6131
6132   /* When using the GNU linker, .gnu.linkonce. sections are used to
6133      eliminate duplicate copies of functions and vtables and such.
6134      The linker will arbitrarily choose one and discard the others.
6135      The AT_*_pc values for such functions refer to local labels in
6136      these sections.  If the section from that file was discarded, the
6137      labels are not in the output, so the relocs get a value of 0.
6138      If this is a discarded function, mark the pc bounds as invalid,
6139      so that GDB will ignore it.  */
6140   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6141     return 0;
6142
6143   *lowpc = low;
6144   *highpc = high;
6145   return ret;
6146 }
6147
6148 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6149    its low and high PC addresses.  Do nothing if these addresses could not
6150    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6151    and HIGHPC to the high address if greater than HIGHPC.  */
6152
6153 static void
6154 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6155                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6156                                  struct dwarf2_cu *cu)
6157 {
6158   CORE_ADDR low, high;
6159   struct die_info *child = die->child;
6160
6161   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6162     {
6163       *lowpc = min (*lowpc, low);
6164       *highpc = max (*highpc, high);
6165     }
6166
6167   /* If the language does not allow nested subprograms (either inside
6168      subprograms or lexical blocks), we're done.  */
6169   if (cu->language != language_ada)
6170     return;
6171
6172   /* Check all the children of the given DIE.  If it contains nested
6173      subprograms, then check their pc bounds.  Likewise, we need to
6174      check lexical blocks as well, as they may also contain subprogram
6175      definitions.  */
6176   while (child && child->tag)
6177     {
6178       if (child->tag == DW_TAG_subprogram
6179           || child->tag == DW_TAG_lexical_block)
6180         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6181       child = sibling_die (child);
6182     }
6183 }
6184
6185 /* Get the low and high pc's represented by the scope DIE, and store
6186    them in *LOWPC and *HIGHPC.  If the correct values can't be
6187    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6188
6189 static void
6190 get_scope_pc_bounds (struct die_info *die,
6191                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6192                      struct dwarf2_cu *cu)
6193 {
6194   CORE_ADDR best_low = (CORE_ADDR) -1;
6195   CORE_ADDR best_high = (CORE_ADDR) 0;
6196   CORE_ADDR current_low, current_high;
6197
6198   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6199     {
6200       best_low = current_low;
6201       best_high = current_high;
6202     }
6203   else
6204     {
6205       struct die_info *child = die->child;
6206
6207       while (child && child->tag)
6208         {
6209           switch (child->tag) {
6210           case DW_TAG_subprogram:
6211             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6212             break;
6213           case DW_TAG_namespace:
6214           case DW_TAG_module:
6215             /* FIXME: carlton/2004-01-16: Should we do this for
6216                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6217                that current GCC's always emit the DIEs corresponding
6218                to definitions of methods of classes as children of a
6219                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6220                the DIEs giving the declarations, which could be
6221                anywhere).  But I don't see any reason why the
6222                standards says that they have to be there.  */
6223             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6224
6225             if (current_low != ((CORE_ADDR) -1))
6226               {
6227                 best_low = min (best_low, current_low);
6228                 best_high = max (best_high, current_high);
6229               }
6230             break;
6231           default:
6232             /* Ignore.  */
6233             break;
6234           }
6235
6236           child = sibling_die (child);
6237         }
6238     }
6239
6240   *lowpc = best_low;
6241   *highpc = best_high;
6242 }
6243
6244 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6245    in DIE.  */
6246 static void
6247 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6248                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6249 {
6250   struct attribute *attr;
6251
6252   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6253   if (attr)
6254     {
6255       CORE_ADDR high = DW_ADDR (attr);
6256
6257       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6258       if (attr)
6259         {
6260           CORE_ADDR low = DW_ADDR (attr);
6261
6262           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6263         }
6264     }
6265
6266   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6267   if (attr)
6268     {
6269       bfd *obfd = cu->objfile->obfd;
6270
6271       /* The value of the DW_AT_ranges attribute is the offset of the
6272          address range list in the .debug_ranges section.  */
6273       unsigned long offset = DW_UNSND (attr);
6274       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6275
6276       /* For some target architectures, but not others, the
6277          read_address function sign-extends the addresses it returns.
6278          To recognize base address selection entries, we need a
6279          mask.  */
6280       unsigned int addr_size = cu->header.addr_size;
6281       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6282
6283       /* The base address, to which the next pair is relative.  Note
6284          that this 'base' is a DWARF concept: most entries in a range
6285          list are relative, to reduce the number of relocs against the
6286          debugging information.  This is separate from this function's
6287          'baseaddr' argument, which GDB uses to relocate debugging
6288          information from a shared library based on the address at
6289          which the library was loaded.  */
6290       CORE_ADDR base = cu->base_address;
6291       int base_known = cu->base_known;
6292
6293       gdb_assert (dwarf2_per_objfile->ranges.readin);
6294       if (offset >= dwarf2_per_objfile->ranges.size)
6295         {
6296           complaint (&symfile_complaints,
6297                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6298                      offset);
6299           return;
6300         }
6301
6302       for (;;)
6303         {
6304           unsigned int bytes_read;
6305           CORE_ADDR start, end;
6306
6307           start = read_address (obfd, buffer, cu, &bytes_read);
6308           buffer += bytes_read;
6309           end = read_address (obfd, buffer, cu, &bytes_read);
6310           buffer += bytes_read;
6311
6312           /* Did we find the end of the range list?  */
6313           if (start == 0 && end == 0)
6314             break;
6315
6316           /* Did we find a base address selection entry?  */
6317           else if ((start & base_select_mask) == base_select_mask)
6318             {
6319               base = end;
6320               base_known = 1;
6321             }
6322
6323           /* We found an ordinary address range.  */
6324           else
6325             {
6326               if (!base_known)
6327                 {
6328                   complaint (&symfile_complaints,
6329                              _("Invalid .debug_ranges data "
6330                                "(no base address)"));
6331                   return;
6332                 }
6333
6334               if (start > end)
6335                 {
6336                   /* Inverted range entries are invalid.  */
6337                   complaint (&symfile_complaints,
6338                              _("Invalid .debug_ranges data "
6339                                "(inverted range)"));
6340                   return;
6341                 }
6342
6343               /* Empty range entries have no effect.  */
6344               if (start == end)
6345                 continue;
6346
6347               record_block_range (block,
6348                                   baseaddr + base + start,
6349                                   baseaddr + base + end - 1);
6350             }
6351         }
6352     }
6353 }
6354
6355 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6356    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6357    during 4.6.0 experimental.  */
6358
6359 static int
6360 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6361 {
6362   const char *cs;
6363   int major, minor, release;
6364
6365   if (cu->producer == NULL)
6366     {
6367       /* For unknown compilers expect their behavior is DWARF version
6368          compliant.
6369
6370          GCC started to support .debug_types sections by -gdwarf-4 since
6371          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6372          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6373          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6374          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6375
6376       return 0;
6377     }
6378
6379   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6380
6381   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6382     {
6383       /* For non-GCC compilers expect their behavior is DWARF version
6384          compliant.  */
6385
6386       return 0;
6387     }
6388   cs = &cu->producer[strlen ("GNU ")];
6389   while (*cs && !isdigit (*cs))
6390     cs++;
6391   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6392     {
6393       /* Not recognized as GCC.  */
6394
6395       return 0;
6396     }
6397
6398   return major < 4 || (major == 4 && minor < 6);
6399 }
6400
6401 /* Return the default accessibility type if it is not overriden by
6402    DW_AT_accessibility.  */
6403
6404 static enum dwarf_access_attribute
6405 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6406 {
6407   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6408     {
6409       /* The default DWARF 2 accessibility for members is public, the default
6410          accessibility for inheritance is private.  */
6411
6412       if (die->tag != DW_TAG_inheritance)
6413         return DW_ACCESS_public;
6414       else
6415         return DW_ACCESS_private;
6416     }
6417   else
6418     {
6419       /* DWARF 3+ defines the default accessibility a different way.  The same
6420          rules apply now for DW_TAG_inheritance as for the members and it only
6421          depends on the container kind.  */
6422
6423       if (die->parent->tag == DW_TAG_class_type)
6424         return DW_ACCESS_private;
6425       else
6426         return DW_ACCESS_public;
6427     }
6428 }
6429
6430 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6431    offset.  If the attribute was not found return 0, otherwise return
6432    1.  If it was found but could not properly be handled, set *OFFSET
6433    to 0.  */
6434
6435 static int
6436 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6437                              LONGEST *offset)
6438 {
6439   struct attribute *attr;
6440
6441   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6442   if (attr != NULL)
6443     {
6444       *offset = 0;
6445
6446       /* Note that we do not check for a section offset first here.
6447          This is because DW_AT_data_member_location is new in DWARF 4,
6448          so if we see it, we can assume that a constant form is really
6449          a constant and not a section offset.  */
6450       if (attr_form_is_constant (attr))
6451         *offset = dwarf2_get_attr_constant_value (attr, 0);
6452       else if (attr_form_is_section_offset (attr))
6453         dwarf2_complex_location_expr_complaint ();
6454       else if (attr_form_is_block (attr))
6455         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6456       else
6457         dwarf2_complex_location_expr_complaint ();
6458
6459       return 1;
6460     }
6461
6462   return 0;
6463 }
6464
6465 /* Add an aggregate field to the field list.  */
6466
6467 static void
6468 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6469                   struct dwarf2_cu *cu)
6470 {
6471   struct objfile *objfile = cu->objfile;
6472   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6473   struct nextfield *new_field;
6474   struct attribute *attr;
6475   struct field *fp;
6476   char *fieldname = "";
6477
6478   /* Allocate a new field list entry and link it in.  */
6479   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6480   make_cleanup (xfree, new_field);
6481   memset (new_field, 0, sizeof (struct nextfield));
6482
6483   if (die->tag == DW_TAG_inheritance)
6484     {
6485       new_field->next = fip->baseclasses;
6486       fip->baseclasses = new_field;
6487     }
6488   else
6489     {
6490       new_field->next = fip->fields;
6491       fip->fields = new_field;
6492     }
6493   fip->nfields++;
6494
6495   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6496   if (attr)
6497     new_field->accessibility = DW_UNSND (attr);
6498   else
6499     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6500   if (new_field->accessibility != DW_ACCESS_public)
6501     fip->non_public_fields = 1;
6502
6503   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6504   if (attr)
6505     new_field->virtuality = DW_UNSND (attr);
6506   else
6507     new_field->virtuality = DW_VIRTUALITY_none;
6508
6509   fp = &new_field->field;
6510
6511   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6512     {
6513       LONGEST offset;
6514
6515       /* Data member other than a C++ static data member.  */
6516
6517       /* Get type of field.  */
6518       fp->type = die_type (die, cu);
6519
6520       SET_FIELD_BITPOS (*fp, 0);
6521
6522       /* Get bit size of field (zero if none).  */
6523       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6524       if (attr)
6525         {
6526           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6527         }
6528       else
6529         {
6530           FIELD_BITSIZE (*fp) = 0;
6531         }
6532
6533       /* Get bit offset of field.  */
6534       if (handle_data_member_location (die, cu, &offset))
6535         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6536       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6537       if (attr)
6538         {
6539           if (gdbarch_bits_big_endian (gdbarch))
6540             {
6541               /* For big endian bits, the DW_AT_bit_offset gives the
6542                  additional bit offset from the MSB of the containing
6543                  anonymous object to the MSB of the field.  We don't
6544                  have to do anything special since we don't need to
6545                  know the size of the anonymous object.  */
6546               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6547             }
6548           else
6549             {
6550               /* For little endian bits, compute the bit offset to the
6551                  MSB of the anonymous object, subtract off the number of
6552                  bits from the MSB of the field to the MSB of the
6553                  object, and then subtract off the number of bits of
6554                  the field itself.  The result is the bit offset of
6555                  the LSB of the field.  */
6556               int anonymous_size;
6557               int bit_offset = DW_UNSND (attr);
6558
6559               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6560               if (attr)
6561                 {
6562                   /* The size of the anonymous object containing
6563                      the bit field is explicit, so use the
6564                      indicated size (in bytes).  */
6565                   anonymous_size = DW_UNSND (attr);
6566                 }
6567               else
6568                 {
6569                   /* The size of the anonymous object containing
6570                      the bit field must be inferred from the type
6571                      attribute of the data member containing the
6572                      bit field.  */
6573                   anonymous_size = TYPE_LENGTH (fp->type);
6574                 }
6575               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6576                 - bit_offset - FIELD_BITSIZE (*fp);
6577             }
6578         }
6579
6580       /* Get name of field.  */
6581       fieldname = dwarf2_name (die, cu);
6582       if (fieldname == NULL)
6583         fieldname = "";
6584
6585       /* The name is already allocated along with this objfile, so we don't
6586          need to duplicate it for the type.  */
6587       fp->name = fieldname;
6588
6589       /* Change accessibility for artificial fields (e.g. virtual table
6590          pointer or virtual base class pointer) to private.  */
6591       if (dwarf2_attr (die, DW_AT_artificial, cu))
6592         {
6593           FIELD_ARTIFICIAL (*fp) = 1;
6594           new_field->accessibility = DW_ACCESS_private;
6595           fip->non_public_fields = 1;
6596         }
6597     }
6598   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6599     {
6600       /* C++ static member.  */
6601
6602       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6603          is a declaration, but all versions of G++ as of this writing
6604          (so through at least 3.2.1) incorrectly generate
6605          DW_TAG_variable tags.  */
6606
6607       char *physname;
6608
6609       /* Get name of field.  */
6610       fieldname = dwarf2_name (die, cu);
6611       if (fieldname == NULL)
6612         return;
6613
6614       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6615       if (attr
6616           /* Only create a symbol if this is an external value.
6617              new_symbol checks this and puts the value in the global symbol
6618              table, which we want.  If it is not external, new_symbol
6619              will try to put the value in cu->list_in_scope which is wrong.  */
6620           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6621         {
6622           /* A static const member, not much different than an enum as far as
6623              we're concerned, except that we can support more types.  */
6624           new_symbol (die, NULL, cu);
6625         }
6626
6627       /* Get physical name.  */
6628       physname = (char *) dwarf2_physname (fieldname, die, cu);
6629
6630       /* The name is already allocated along with this objfile, so we don't
6631          need to duplicate it for the type.  */
6632       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6633       FIELD_TYPE (*fp) = die_type (die, cu);
6634       FIELD_NAME (*fp) = fieldname;
6635     }
6636   else if (die->tag == DW_TAG_inheritance)
6637     {
6638       LONGEST offset;
6639
6640       /* C++ base class field.  */
6641       if (handle_data_member_location (die, cu, &offset))
6642         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6643       FIELD_BITSIZE (*fp) = 0;
6644       FIELD_TYPE (*fp) = die_type (die, cu);
6645       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6646       fip->nbaseclasses++;
6647     }
6648 }
6649
6650 /* Add a typedef defined in the scope of the FIP's class.  */
6651
6652 static void
6653 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6654                     struct dwarf2_cu *cu)
6655 {
6656   struct objfile *objfile = cu->objfile;
6657   struct typedef_field_list *new_field;
6658   struct attribute *attr;
6659   struct typedef_field *fp;
6660   char *fieldname = "";
6661
6662   /* Allocate a new field list entry and link it in.  */
6663   new_field = xzalloc (sizeof (*new_field));
6664   make_cleanup (xfree, new_field);
6665
6666   gdb_assert (die->tag == DW_TAG_typedef);
6667
6668   fp = &new_field->field;
6669
6670   /* Get name of field.  */
6671   fp->name = dwarf2_name (die, cu);
6672   if (fp->name == NULL)
6673     return;
6674
6675   fp->type = read_type_die (die, cu);
6676
6677   new_field->next = fip->typedef_field_list;
6678   fip->typedef_field_list = new_field;
6679   fip->typedef_field_list_count++;
6680 }
6681
6682 /* Create the vector of fields, and attach it to the type.  */
6683
6684 static void
6685 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6686                               struct dwarf2_cu *cu)
6687 {
6688   int nfields = fip->nfields;
6689
6690   /* Record the field count, allocate space for the array of fields,
6691      and create blank accessibility bitfields if necessary.  */
6692   TYPE_NFIELDS (type) = nfields;
6693   TYPE_FIELDS (type) = (struct field *)
6694     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6695   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6696
6697   if (fip->non_public_fields && cu->language != language_ada)
6698     {
6699       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6700
6701       TYPE_FIELD_PRIVATE_BITS (type) =
6702         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6703       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6704
6705       TYPE_FIELD_PROTECTED_BITS (type) =
6706         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6707       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6708
6709       TYPE_FIELD_IGNORE_BITS (type) =
6710         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6711       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6712     }
6713
6714   /* If the type has baseclasses, allocate and clear a bit vector for
6715      TYPE_FIELD_VIRTUAL_BITS.  */
6716   if (fip->nbaseclasses && cu->language != language_ada)
6717     {
6718       int num_bytes = B_BYTES (fip->nbaseclasses);
6719       unsigned char *pointer;
6720
6721       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6722       pointer = TYPE_ALLOC (type, num_bytes);
6723       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6724       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6725       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6726     }
6727
6728   /* Copy the saved-up fields into the field vector.  Start from the head of
6729      the list, adding to the tail of the field array, so that they end up in
6730      the same order in the array in which they were added to the list.  */
6731   while (nfields-- > 0)
6732     {
6733       struct nextfield *fieldp;
6734
6735       if (fip->fields)
6736         {
6737           fieldp = fip->fields;
6738           fip->fields = fieldp->next;
6739         }
6740       else
6741         {
6742           fieldp = fip->baseclasses;
6743           fip->baseclasses = fieldp->next;
6744         }
6745
6746       TYPE_FIELD (type, nfields) = fieldp->field;
6747       switch (fieldp->accessibility)
6748         {
6749         case DW_ACCESS_private:
6750           if (cu->language != language_ada)
6751             SET_TYPE_FIELD_PRIVATE (type, nfields);
6752           break;
6753
6754         case DW_ACCESS_protected:
6755           if (cu->language != language_ada)
6756             SET_TYPE_FIELD_PROTECTED (type, nfields);
6757           break;
6758
6759         case DW_ACCESS_public:
6760           break;
6761
6762         default:
6763           /* Unknown accessibility.  Complain and treat it as public.  */
6764           {
6765             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6766                        fieldp->accessibility);
6767           }
6768           break;
6769         }
6770       if (nfields < fip->nbaseclasses)
6771         {
6772           switch (fieldp->virtuality)
6773             {
6774             case DW_VIRTUALITY_virtual:
6775             case DW_VIRTUALITY_pure_virtual:
6776               if (cu->language == language_ada)
6777                 error (_("unexpected virtuality in component of Ada type"));
6778               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6779               break;
6780             }
6781         }
6782     }
6783 }
6784
6785 /* Add a member function to the proper fieldlist.  */
6786
6787 static void
6788 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6789                       struct type *type, struct dwarf2_cu *cu)
6790 {
6791   struct objfile *objfile = cu->objfile;
6792   struct attribute *attr;
6793   struct fnfieldlist *flp;
6794   int i;
6795   struct fn_field *fnp;
6796   char *fieldname;
6797   struct nextfnfield *new_fnfield;
6798   struct type *this_type;
6799   enum dwarf_access_attribute accessibility;
6800
6801   if (cu->language == language_ada)
6802     error (_("unexpected member function in Ada type"));
6803
6804   /* Get name of member function.  */
6805   fieldname = dwarf2_name (die, cu);
6806   if (fieldname == NULL)
6807     return;
6808
6809   /* Look up member function name in fieldlist.  */
6810   for (i = 0; i < fip->nfnfields; i++)
6811     {
6812       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6813         break;
6814     }
6815
6816   /* Create new list element if necessary.  */
6817   if (i < fip->nfnfields)
6818     flp = &fip->fnfieldlists[i];
6819   else
6820     {
6821       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6822         {
6823           fip->fnfieldlists = (struct fnfieldlist *)
6824             xrealloc (fip->fnfieldlists,
6825                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6826                       * sizeof (struct fnfieldlist));
6827           if (fip->nfnfields == 0)
6828             make_cleanup (free_current_contents, &fip->fnfieldlists);
6829         }
6830       flp = &fip->fnfieldlists[fip->nfnfields];
6831       flp->name = fieldname;
6832       flp->length = 0;
6833       flp->head = NULL;
6834       i = fip->nfnfields++;
6835     }
6836
6837   /* Create a new member function field and chain it to the field list
6838      entry.  */
6839   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6840   make_cleanup (xfree, new_fnfield);
6841   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6842   new_fnfield->next = flp->head;
6843   flp->head = new_fnfield;
6844   flp->length++;
6845
6846   /* Fill in the member function field info.  */
6847   fnp = &new_fnfield->fnfield;
6848
6849   /* Delay processing of the physname until later.  */
6850   if (cu->language == language_cplus || cu->language == language_java)
6851     {
6852       add_to_method_list (type, i, flp->length - 1, fieldname,
6853                           die, cu);
6854     }
6855   else
6856     {
6857       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6858       fnp->physname = physname ? physname : "";
6859     }
6860
6861   fnp->type = alloc_type (objfile);
6862   this_type = read_type_die (die, cu);
6863   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6864     {
6865       int nparams = TYPE_NFIELDS (this_type);
6866
6867       /* TYPE is the domain of this method, and THIS_TYPE is the type
6868            of the method itself (TYPE_CODE_METHOD).  */
6869       smash_to_method_type (fnp->type, type,
6870                             TYPE_TARGET_TYPE (this_type),
6871                             TYPE_FIELDS (this_type),
6872                             TYPE_NFIELDS (this_type),
6873                             TYPE_VARARGS (this_type));
6874
6875       /* Handle static member functions.
6876          Dwarf2 has no clean way to discern C++ static and non-static
6877          member functions.  G++ helps GDB by marking the first
6878          parameter for non-static member functions (which is the this
6879          pointer) as artificial.  We obtain this information from
6880          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6881       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6882         fnp->voffset = VOFFSET_STATIC;
6883     }
6884   else
6885     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6886                dwarf2_full_name (fieldname, die, cu));
6887
6888   /* Get fcontext from DW_AT_containing_type if present.  */
6889   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6890     fnp->fcontext = die_containing_type (die, cu);
6891
6892   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6893      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6894
6895   /* Get accessibility.  */
6896   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6897   if (attr)
6898     accessibility = DW_UNSND (attr);
6899   else
6900     accessibility = dwarf2_default_access_attribute (die, cu);
6901   switch (accessibility)
6902     {
6903     case DW_ACCESS_private:
6904       fnp->is_private = 1;
6905       break;
6906     case DW_ACCESS_protected:
6907       fnp->is_protected = 1;
6908       break;
6909     }
6910
6911   /* Check for artificial methods.  */
6912   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6913   if (attr && DW_UNSND (attr) != 0)
6914     fnp->is_artificial = 1;
6915
6916   /* Get index in virtual function table if it is a virtual member
6917      function.  For older versions of GCC, this is an offset in the
6918      appropriate virtual table, as specified by DW_AT_containing_type.
6919      For everyone else, it is an expression to be evaluated relative
6920      to the object address.  */
6921
6922   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6923   if (attr)
6924     {
6925       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6926         {
6927           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6928             {
6929               /* Old-style GCC.  */
6930               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6931             }
6932           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6933                    || (DW_BLOCK (attr)->size > 1
6934                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6935                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6936             {
6937               struct dwarf_block blk;
6938               int offset;
6939
6940               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6941                         ? 1 : 2);
6942               blk.size = DW_BLOCK (attr)->size - offset;
6943               blk.data = DW_BLOCK (attr)->data + offset;
6944               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6945               if ((fnp->voffset % cu->header.addr_size) != 0)
6946                 dwarf2_complex_location_expr_complaint ();
6947               else
6948                 fnp->voffset /= cu->header.addr_size;
6949               fnp->voffset += 2;
6950             }
6951           else
6952             dwarf2_complex_location_expr_complaint ();
6953
6954           if (!fnp->fcontext)
6955             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6956         }
6957       else if (attr_form_is_section_offset (attr))
6958         {
6959           dwarf2_complex_location_expr_complaint ();
6960         }
6961       else
6962         {
6963           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6964                                                  fieldname);
6965         }
6966     }
6967   else
6968     {
6969       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6970       if (attr && DW_UNSND (attr))
6971         {
6972           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6973           complaint (&symfile_complaints,
6974                      _("Member function \"%s\" (offset %d) is virtual "
6975                        "but the vtable offset is not specified"),
6976                      fieldname, die->offset);
6977           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6978           TYPE_CPLUS_DYNAMIC (type) = 1;
6979         }
6980     }
6981 }
6982
6983 /* Create the vector of member function fields, and attach it to the type.  */
6984
6985 static void
6986 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6987                                  struct dwarf2_cu *cu)
6988 {
6989   struct fnfieldlist *flp;
6990   int total_length = 0;
6991   int i;
6992
6993   if (cu->language == language_ada)
6994     error (_("unexpected member functions in Ada type"));
6995
6996   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6997   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6998     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6999
7000   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7001     {
7002       struct nextfnfield *nfp = flp->head;
7003       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7004       int k;
7005
7006       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7007       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7008       fn_flp->fn_fields = (struct fn_field *)
7009         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7010       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7011         fn_flp->fn_fields[k] = nfp->fnfield;
7012
7013       total_length += flp->length;
7014     }
7015
7016   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7017   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7018 }
7019
7020 /* Returns non-zero if NAME is the name of a vtable member in CU's
7021    language, zero otherwise.  */
7022 static int
7023 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7024 {
7025   static const char vptr[] = "_vptr";
7026   static const char vtable[] = "vtable";
7027
7028   /* Look for the C++ and Java forms of the vtable.  */
7029   if ((cu->language == language_java
7030        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7031        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7032        && is_cplus_marker (name[sizeof (vptr) - 1])))
7033     return 1;
7034
7035   return 0;
7036 }
7037
7038 /* GCC outputs unnamed structures that are really pointers to member
7039    functions, with the ABI-specified layout.  If TYPE describes
7040    such a structure, smash it into a member function type.
7041
7042    GCC shouldn't do this; it should just output pointer to member DIEs.
7043    This is GCC PR debug/28767.  */
7044
7045 static void
7046 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7047 {
7048   struct type *pfn_type, *domain_type, *new_type;
7049
7050   /* Check for a structure with no name and two children.  */
7051   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7052     return;
7053
7054   /* Check for __pfn and __delta members.  */
7055   if (TYPE_FIELD_NAME (type, 0) == NULL
7056       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7057       || TYPE_FIELD_NAME (type, 1) == NULL
7058       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7059     return;
7060
7061   /* Find the type of the method.  */
7062   pfn_type = TYPE_FIELD_TYPE (type, 0);
7063   if (pfn_type == NULL
7064       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7065       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7066     return;
7067
7068   /* Look for the "this" argument.  */
7069   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7070   if (TYPE_NFIELDS (pfn_type) == 0
7071       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7072       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7073     return;
7074
7075   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7076   new_type = alloc_type (objfile);
7077   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7078                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7079                         TYPE_VARARGS (pfn_type));
7080   smash_to_methodptr_type (type, new_type);
7081 }
7082
7083 /* Called when we find the DIE that starts a structure or union scope
7084    (definition) to create a type for the structure or union.  Fill in
7085    the type's name and general properties; the members will not be
7086    processed until process_structure_type.
7087
7088    NOTE: we need to call these functions regardless of whether or not the
7089    DIE has a DW_AT_name attribute, since it might be an anonymous
7090    structure or union.  This gets the type entered into our set of
7091    user defined types.
7092
7093    However, if the structure is incomplete (an opaque struct/union)
7094    then suppress creating a symbol table entry for it since gdb only
7095    wants to find the one with the complete definition.  Note that if
7096    it is complete, we just call new_symbol, which does it's own
7097    checking about whether the struct/union is anonymous or not (and
7098    suppresses creating a symbol table entry itself).  */
7099
7100 static struct type *
7101 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7102 {
7103   struct objfile *objfile = cu->objfile;
7104   struct type *type;
7105   struct attribute *attr;
7106   char *name;
7107
7108   /* If the definition of this type lives in .debug_types, read that type.
7109      Don't follow DW_AT_specification though, that will take us back up
7110      the chain and we want to go down.  */
7111   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7112   if (attr)
7113     {
7114       struct dwarf2_cu *type_cu = cu;
7115       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7116
7117       /* We could just recurse on read_structure_type, but we need to call
7118          get_die_type to ensure only one type for this DIE is created.
7119          This is important, for example, because for c++ classes we need
7120          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7121       type = read_type_die (type_die, type_cu);
7122
7123       /* TYPE_CU may not be the same as CU.
7124          Ensure TYPE is recorded in CU's type_hash table.  */
7125       return set_die_type (die, type, cu);
7126     }
7127
7128   type = alloc_type (objfile);
7129   INIT_CPLUS_SPECIFIC (type);
7130
7131   name = dwarf2_name (die, cu);
7132   if (name != NULL)
7133     {
7134       if (cu->language == language_cplus
7135           || cu->language == language_java)
7136         {
7137           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7138
7139           /* dwarf2_full_name might have already finished building the DIE's
7140              type.  If so, there is no need to continue.  */
7141           if (get_die_type (die, cu) != NULL)
7142             return get_die_type (die, cu);
7143
7144           TYPE_TAG_NAME (type) = full_name;
7145           if (die->tag == DW_TAG_structure_type
7146               || die->tag == DW_TAG_class_type)
7147             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7148         }
7149       else
7150         {
7151           /* The name is already allocated along with this objfile, so
7152              we don't need to duplicate it for the type.  */
7153           TYPE_TAG_NAME (type) = (char *) name;
7154           if (die->tag == DW_TAG_class_type)
7155             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7156         }
7157     }
7158
7159   if (die->tag == DW_TAG_structure_type)
7160     {
7161       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7162     }
7163   else if (die->tag == DW_TAG_union_type)
7164     {
7165       TYPE_CODE (type) = TYPE_CODE_UNION;
7166     }
7167   else
7168     {
7169       TYPE_CODE (type) = TYPE_CODE_CLASS;
7170     }
7171
7172   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7173     TYPE_DECLARED_CLASS (type) = 1;
7174
7175   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7176   if (attr)
7177     {
7178       TYPE_LENGTH (type) = DW_UNSND (attr);
7179     }
7180   else
7181     {
7182       TYPE_LENGTH (type) = 0;
7183     }
7184
7185   TYPE_STUB_SUPPORTED (type) = 1;
7186   if (die_is_declaration (die, cu))
7187     TYPE_STUB (type) = 1;
7188   else if (attr == NULL && die->child == NULL
7189            && producer_is_realview (cu->producer))
7190     /* RealView does not output the required DW_AT_declaration
7191        on incomplete types.  */
7192     TYPE_STUB (type) = 1;
7193
7194   /* We need to add the type field to the die immediately so we don't
7195      infinitely recurse when dealing with pointers to the structure
7196      type within the structure itself.  */
7197   set_die_type (die, type, cu);
7198
7199   /* set_die_type should be already done.  */
7200   set_descriptive_type (type, die, cu);
7201
7202   return type;
7203 }
7204
7205 /* Finish creating a structure or union type, including filling in
7206    its members and creating a symbol for it.  */
7207
7208 static void
7209 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7210 {
7211   struct objfile *objfile = cu->objfile;
7212   struct die_info *child_die = die->child;
7213   struct type *type;
7214
7215   type = get_die_type (die, cu);
7216   if (type == NULL)
7217     type = read_structure_type (die, cu);
7218
7219   if (die->child != NULL && ! die_is_declaration (die, cu))
7220     {
7221       struct field_info fi;
7222       struct die_info *child_die;
7223       VEC (symbolp) *template_args = NULL;
7224       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7225
7226       memset (&fi, 0, sizeof (struct field_info));
7227
7228       child_die = die->child;
7229
7230       while (child_die && child_die->tag)
7231         {
7232           if (child_die->tag == DW_TAG_member
7233               || child_die->tag == DW_TAG_variable)
7234             {
7235               /* NOTE: carlton/2002-11-05: A C++ static data member
7236                  should be a DW_TAG_member that is a declaration, but
7237                  all versions of G++ as of this writing (so through at
7238                  least 3.2.1) incorrectly generate DW_TAG_variable
7239                  tags for them instead.  */
7240               dwarf2_add_field (&fi, child_die, cu);
7241             }
7242           else if (child_die->tag == DW_TAG_subprogram)
7243             {
7244               /* C++ member function.  */
7245               dwarf2_add_member_fn (&fi, child_die, type, cu);
7246             }
7247           else if (child_die->tag == DW_TAG_inheritance)
7248             {
7249               /* C++ base class field.  */
7250               dwarf2_add_field (&fi, child_die, cu);
7251             }
7252           else if (child_die->tag == DW_TAG_typedef)
7253             dwarf2_add_typedef (&fi, child_die, cu);
7254           else if (child_die->tag == DW_TAG_template_type_param
7255                    || child_die->tag == DW_TAG_template_value_param)
7256             {
7257               struct symbol *arg = new_symbol (child_die, NULL, cu);
7258
7259               if (arg != NULL)
7260                 VEC_safe_push (symbolp, template_args, arg);
7261             }
7262
7263           child_die = sibling_die (child_die);
7264         }
7265
7266       /* Attach template arguments to type.  */
7267       if (! VEC_empty (symbolp, template_args))
7268         {
7269           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7270           TYPE_N_TEMPLATE_ARGUMENTS (type)
7271             = VEC_length (symbolp, template_args);
7272           TYPE_TEMPLATE_ARGUMENTS (type)
7273             = obstack_alloc (&objfile->objfile_obstack,
7274                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7275                               * sizeof (struct symbol *)));
7276           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7277                   VEC_address (symbolp, template_args),
7278                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7279                    * sizeof (struct symbol *)));
7280           VEC_free (symbolp, template_args);
7281         }
7282
7283       /* Attach fields and member functions to the type.  */
7284       if (fi.nfields)
7285         dwarf2_attach_fields_to_type (&fi, type, cu);
7286       if (fi.nfnfields)
7287         {
7288           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7289
7290           /* Get the type which refers to the base class (possibly this
7291              class itself) which contains the vtable pointer for the current
7292              class from the DW_AT_containing_type attribute.  This use of
7293              DW_AT_containing_type is a GNU extension.  */
7294
7295           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7296             {
7297               struct type *t = die_containing_type (die, cu);
7298
7299               TYPE_VPTR_BASETYPE (type) = t;
7300               if (type == t)
7301                 {
7302                   int i;
7303
7304                   /* Our own class provides vtbl ptr.  */
7305                   for (i = TYPE_NFIELDS (t) - 1;
7306                        i >= TYPE_N_BASECLASSES (t);
7307                        --i)
7308                     {
7309                       char *fieldname = TYPE_FIELD_NAME (t, i);
7310
7311                       if (is_vtable_name (fieldname, cu))
7312                         {
7313                           TYPE_VPTR_FIELDNO (type) = i;
7314                           break;
7315                         }
7316                     }
7317
7318                   /* Complain if virtual function table field not found.  */
7319                   if (i < TYPE_N_BASECLASSES (t))
7320                     complaint (&symfile_complaints,
7321                                _("virtual function table pointer "
7322                                  "not found when defining class '%s'"),
7323                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7324                                "");
7325                 }
7326               else
7327                 {
7328                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7329                 }
7330             }
7331           else if (cu->producer
7332                    && strncmp (cu->producer,
7333                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7334             {
7335               /* The IBM XLC compiler does not provide direct indication
7336                  of the containing type, but the vtable pointer is
7337                  always named __vfp.  */
7338
7339               int i;
7340
7341               for (i = TYPE_NFIELDS (type) - 1;
7342                    i >= TYPE_N_BASECLASSES (type);
7343                    --i)
7344                 {
7345                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7346                     {
7347                       TYPE_VPTR_FIELDNO (type) = i;
7348                       TYPE_VPTR_BASETYPE (type) = type;
7349                       break;
7350                     }
7351                 }
7352             }
7353         }
7354
7355       /* Copy fi.typedef_field_list linked list elements content into the
7356          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7357       if (fi.typedef_field_list)
7358         {
7359           int i = fi.typedef_field_list_count;
7360
7361           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7362           TYPE_TYPEDEF_FIELD_ARRAY (type)
7363             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7364           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7365
7366           /* Reverse the list order to keep the debug info elements order.  */
7367           while (--i >= 0)
7368             {
7369               struct typedef_field *dest, *src;
7370
7371               dest = &TYPE_TYPEDEF_FIELD (type, i);
7372               src = &fi.typedef_field_list->field;
7373               fi.typedef_field_list = fi.typedef_field_list->next;
7374               *dest = *src;
7375             }
7376         }
7377
7378       do_cleanups (back_to);
7379     }
7380
7381   quirk_gcc_member_function_pointer (type, cu->objfile);
7382
7383   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7384      snapshots) has been known to create a die giving a declaration
7385      for a class that has, as a child, a die giving a definition for a
7386      nested class.  So we have to process our children even if the
7387      current die is a declaration.  Normally, of course, a declaration
7388      won't have any children at all.  */
7389
7390   while (child_die != NULL && child_die->tag)
7391     {
7392       if (child_die->tag == DW_TAG_member
7393           || child_die->tag == DW_TAG_variable
7394           || child_die->tag == DW_TAG_inheritance
7395           || child_die->tag == DW_TAG_template_value_param
7396           || child_die->tag == DW_TAG_template_type_param)
7397         {
7398           /* Do nothing.  */
7399         }
7400       else
7401         process_die (child_die, cu);
7402
7403       child_die = sibling_die (child_die);
7404     }
7405
7406   /* Do not consider external references.  According to the DWARF standard,
7407      these DIEs are identified by the fact that they have no byte_size
7408      attribute, and a declaration attribute.  */
7409   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7410       || !die_is_declaration (die, cu))
7411     new_symbol (die, type, cu);
7412 }
7413
7414 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7415    complete the type's fields yet, or create any symbols.  */
7416
7417 static struct type *
7418 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7419 {
7420   struct objfile *objfile = cu->objfile;
7421   struct type *type;
7422   struct attribute *attr;
7423   const char *name;
7424
7425   /* If the definition of this type lives in .debug_types, read that type.
7426      Don't follow DW_AT_specification though, that will take us back up
7427      the chain and we want to go down.  */
7428   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7429   if (attr)
7430     {
7431       struct dwarf2_cu *type_cu = cu;
7432       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7433
7434       type = read_type_die (type_die, type_cu);
7435
7436       /* TYPE_CU may not be the same as CU.
7437          Ensure TYPE is recorded in CU's type_hash table.  */
7438       return set_die_type (die, type, cu);
7439     }
7440
7441   type = alloc_type (objfile);
7442
7443   TYPE_CODE (type) = TYPE_CODE_ENUM;
7444   name = dwarf2_full_name (NULL, die, cu);
7445   if (name != NULL)
7446     TYPE_TAG_NAME (type) = (char *) name;
7447
7448   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7449   if (attr)
7450     {
7451       TYPE_LENGTH (type) = DW_UNSND (attr);
7452     }
7453   else
7454     {
7455       TYPE_LENGTH (type) = 0;
7456     }
7457
7458   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7459      declared as private in the package spec, and then defined only
7460      inside the package body.  Such types are known as Taft Amendment
7461      Types.  When another package uses such a type, an incomplete DIE
7462      may be generated by the compiler.  */
7463   if (die_is_declaration (die, cu))
7464     TYPE_STUB (type) = 1;
7465
7466   return set_die_type (die, type, cu);
7467 }
7468
7469 /* Given a pointer to a die which begins an enumeration, process all
7470    the dies that define the members of the enumeration, and create the
7471    symbol for the enumeration type.
7472
7473    NOTE: We reverse the order of the element list.  */
7474
7475 static void
7476 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7477 {
7478   struct type *this_type;
7479
7480   this_type = get_die_type (die, cu);
7481   if (this_type == NULL)
7482     this_type = read_enumeration_type (die, cu);
7483
7484   if (die->child != NULL)
7485     {
7486       struct die_info *child_die;
7487       struct symbol *sym;
7488       struct field *fields = NULL;
7489       int num_fields = 0;
7490       int unsigned_enum = 1;
7491       char *name;
7492
7493       child_die = die->child;
7494       while (child_die && child_die->tag)
7495         {
7496           if (child_die->tag != DW_TAG_enumerator)
7497             {
7498               process_die (child_die, cu);
7499             }
7500           else
7501             {
7502               name = dwarf2_name (child_die, cu);
7503               if (name)
7504                 {
7505                   sym = new_symbol (child_die, this_type, cu);
7506                   if (SYMBOL_VALUE (sym) < 0)
7507                     unsigned_enum = 0;
7508
7509                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7510                     {
7511                       fields = (struct field *)
7512                         xrealloc (fields,
7513                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7514                                   * sizeof (struct field));
7515                     }
7516
7517                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7518                   FIELD_TYPE (fields[num_fields]) = NULL;
7519                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7520                   FIELD_BITSIZE (fields[num_fields]) = 0;
7521
7522                   num_fields++;
7523                 }
7524             }
7525
7526           child_die = sibling_die (child_die);
7527         }
7528
7529       if (num_fields)
7530         {
7531           TYPE_NFIELDS (this_type) = num_fields;
7532           TYPE_FIELDS (this_type) = (struct field *)
7533             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7534           memcpy (TYPE_FIELDS (this_type), fields,
7535                   sizeof (struct field) * num_fields);
7536           xfree (fields);
7537         }
7538       if (unsigned_enum)
7539         TYPE_UNSIGNED (this_type) = 1;
7540     }
7541
7542   new_symbol (die, this_type, cu);
7543 }
7544
7545 /* Extract all information from a DW_TAG_array_type DIE and put it in
7546    the DIE's type field.  For now, this only handles one dimensional
7547    arrays.  */
7548
7549 static struct type *
7550 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7551 {
7552   struct objfile *objfile = cu->objfile;
7553   struct die_info *child_die;
7554   struct type *type;
7555   struct type *element_type, *range_type, *index_type;
7556   struct type **range_types = NULL;
7557   struct attribute *attr;
7558   int ndim = 0;
7559   struct cleanup *back_to;
7560   char *name;
7561
7562   element_type = die_type (die, cu);
7563
7564   /* The die_type call above may have already set the type for this DIE.  */
7565   type = get_die_type (die, cu);
7566   if (type)
7567     return type;
7568
7569   /* Irix 6.2 native cc creates array types without children for
7570      arrays with unspecified length.  */
7571   if (die->child == NULL)
7572     {
7573       index_type = objfile_type (objfile)->builtin_int;
7574       range_type = create_range_type (NULL, index_type, 0, -1);
7575       type = create_array_type (NULL, element_type, range_type);
7576       return set_die_type (die, type, cu);
7577     }
7578
7579   back_to = make_cleanup (null_cleanup, NULL);
7580   child_die = die->child;
7581   while (child_die && child_die->tag)
7582     {
7583       if (child_die->tag == DW_TAG_subrange_type)
7584         {
7585           struct type *child_type = read_type_die (child_die, cu);
7586
7587           if (child_type != NULL)
7588             {
7589               /* The range type was succesfully read.  Save it for the
7590                  array type creation.  */
7591               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7592                 {
7593                   range_types = (struct type **)
7594                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7595                               * sizeof (struct type *));
7596                   if (ndim == 0)
7597                     make_cleanup (free_current_contents, &range_types);
7598                 }
7599               range_types[ndim++] = child_type;
7600             }
7601         }
7602       child_die = sibling_die (child_die);
7603     }
7604
7605   /* Dwarf2 dimensions are output from left to right, create the
7606      necessary array types in backwards order.  */
7607
7608   type = element_type;
7609
7610   if (read_array_order (die, cu) == DW_ORD_col_major)
7611     {
7612       int i = 0;
7613
7614       while (i < ndim)
7615         type = create_array_type (NULL, type, range_types[i++]);
7616     }
7617   else
7618     {
7619       while (ndim-- > 0)
7620         type = create_array_type (NULL, type, range_types[ndim]);
7621     }
7622
7623   /* Understand Dwarf2 support for vector types (like they occur on
7624      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7625      array type.  This is not part of the Dwarf2/3 standard yet, but a
7626      custom vendor extension.  The main difference between a regular
7627      array and the vector variant is that vectors are passed by value
7628      to functions.  */
7629   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7630   if (attr)
7631     make_vector_type (type);
7632
7633   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7634      implementation may choose to implement triple vectors using this
7635      attribute.  */
7636   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7637   if (attr)
7638     {
7639       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7640         TYPE_LENGTH (type) = DW_UNSND (attr);
7641       else
7642         complaint (&symfile_complaints,
7643                    _("DW_AT_byte_size for array type smaller "
7644                      "than the total size of elements"));
7645     }
7646
7647   name = dwarf2_name (die, cu);
7648   if (name)
7649     TYPE_NAME (type) = name;
7650
7651   /* Install the type in the die.  */
7652   set_die_type (die, type, cu);
7653
7654   /* set_die_type should be already done.  */
7655   set_descriptive_type (type, die, cu);
7656
7657   do_cleanups (back_to);
7658
7659   return type;
7660 }
7661
7662 static enum dwarf_array_dim_ordering
7663 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7664 {
7665   struct attribute *attr;
7666
7667   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7668
7669   if (attr) return DW_SND (attr);
7670
7671   /* GNU F77 is a special case, as at 08/2004 array type info is the
7672      opposite order to the dwarf2 specification, but data is still
7673      laid out as per normal fortran.
7674
7675      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7676      version checking.  */
7677
7678   if (cu->language == language_fortran
7679       && cu->producer && strstr (cu->producer, "GNU F77"))
7680     {
7681       return DW_ORD_row_major;
7682     }
7683
7684   switch (cu->language_defn->la_array_ordering)
7685     {
7686     case array_column_major:
7687       return DW_ORD_col_major;
7688     case array_row_major:
7689     default:
7690       return DW_ORD_row_major;
7691     };
7692 }
7693
7694 /* Extract all information from a DW_TAG_set_type DIE and put it in
7695    the DIE's type field.  */
7696
7697 static struct type *
7698 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7699 {
7700   struct type *domain_type, *set_type;
7701   struct attribute *attr;
7702
7703   domain_type = die_type (die, cu);
7704
7705   /* The die_type call above may have already set the type for this DIE.  */
7706   set_type = get_die_type (die, cu);
7707   if (set_type)
7708     return set_type;
7709
7710   set_type = create_set_type (NULL, domain_type);
7711
7712   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7713   if (attr)
7714     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7715
7716   return set_die_type (die, set_type, cu);
7717 }
7718
7719 /* First cut: install each common block member as a global variable.  */
7720
7721 static void
7722 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7723 {
7724   struct die_info *child_die;
7725   struct attribute *attr;
7726   struct symbol *sym;
7727   CORE_ADDR base = (CORE_ADDR) 0;
7728
7729   attr = dwarf2_attr (die, DW_AT_location, cu);
7730   if (attr)
7731     {
7732       /* Support the .debug_loc offsets.  */
7733       if (attr_form_is_block (attr))
7734         {
7735           base = decode_locdesc (DW_BLOCK (attr), cu);
7736         }
7737       else if (attr_form_is_section_offset (attr))
7738         {
7739           dwarf2_complex_location_expr_complaint ();
7740         }
7741       else
7742         {
7743           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7744                                                  "common block member");
7745         }
7746     }
7747   if (die->child != NULL)
7748     {
7749       child_die = die->child;
7750       while (child_die && child_die->tag)
7751         {
7752           LONGEST offset;
7753
7754           sym = new_symbol (child_die, NULL, cu);
7755           if (sym != NULL
7756               && handle_data_member_location (child_die, cu, &offset))
7757             {
7758               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7759               add_symbol_to_list (sym, &global_symbols);
7760             }
7761           child_die = sibling_die (child_die);
7762         }
7763     }
7764 }
7765
7766 /* Create a type for a C++ namespace.  */
7767
7768 static struct type *
7769 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7770 {
7771   struct objfile *objfile = cu->objfile;
7772   const char *previous_prefix, *name;
7773   int is_anonymous;
7774   struct type *type;
7775
7776   /* For extensions, reuse the type of the original namespace.  */
7777   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7778     {
7779       struct die_info *ext_die;
7780       struct dwarf2_cu *ext_cu = cu;
7781
7782       ext_die = dwarf2_extension (die, &ext_cu);
7783       type = read_type_die (ext_die, ext_cu);
7784
7785       /* EXT_CU may not be the same as CU.
7786          Ensure TYPE is recorded in CU's type_hash table.  */
7787       return set_die_type (die, type, cu);
7788     }
7789
7790   name = namespace_name (die, &is_anonymous, cu);
7791
7792   /* Now build the name of the current namespace.  */
7793
7794   previous_prefix = determine_prefix (die, cu);
7795   if (previous_prefix[0] != '\0')
7796     name = typename_concat (&objfile->objfile_obstack,
7797                             previous_prefix, name, 0, cu);
7798
7799   /* Create the type.  */
7800   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7801                     objfile);
7802   TYPE_NAME (type) = (char *) name;
7803   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7804
7805   return set_die_type (die, type, cu);
7806 }
7807
7808 /* Read a C++ namespace.  */
7809
7810 static void
7811 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7812 {
7813   struct objfile *objfile = cu->objfile;
7814   int is_anonymous;
7815
7816   /* Add a symbol associated to this if we haven't seen the namespace
7817      before.  Also, add a using directive if it's an anonymous
7818      namespace.  */
7819
7820   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7821     {
7822       struct type *type;
7823
7824       type = read_type_die (die, cu);
7825       new_symbol (die, type, cu);
7826
7827       namespace_name (die, &is_anonymous, cu);
7828       if (is_anonymous)
7829         {
7830           const char *previous_prefix = determine_prefix (die, cu);
7831
7832           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7833                                   NULL, &objfile->objfile_obstack);
7834         }
7835     }
7836
7837   if (die->child != NULL)
7838     {
7839       struct die_info *child_die = die->child;
7840
7841       while (child_die && child_die->tag)
7842         {
7843           process_die (child_die, cu);
7844           child_die = sibling_die (child_die);
7845         }
7846     }
7847 }
7848
7849 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7850    imported module.  Still we need that type as local Fortran "use ... only"
7851    declaration imports depend on the created type in determine_prefix.  */
7852
7853 static struct type *
7854 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7855 {
7856   struct objfile *objfile = cu->objfile;
7857   char *module_name;
7858   struct type *type;
7859
7860   module_name = dwarf2_name (die, cu);
7861   if (!module_name)
7862     complaint (&symfile_complaints,
7863                _("DW_TAG_module has no name, offset 0x%x"),
7864                die->offset);
7865   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7866
7867   /* determine_prefix uses TYPE_TAG_NAME.  */
7868   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7869
7870   return set_die_type (die, type, cu);
7871 }
7872
7873 /* Read a Fortran module.  */
7874
7875 static void
7876 read_module (struct die_info *die, struct dwarf2_cu *cu)
7877 {
7878   struct die_info *child_die = die->child;
7879
7880   while (child_die && child_die->tag)
7881     {
7882       process_die (child_die, cu);
7883       child_die = sibling_die (child_die);
7884     }
7885 }
7886
7887 /* Return the name of the namespace represented by DIE.  Set
7888    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7889    namespace.  */
7890
7891 static const char *
7892 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7893 {
7894   struct die_info *current_die;
7895   const char *name = NULL;
7896
7897   /* Loop through the extensions until we find a name.  */
7898
7899   for (current_die = die;
7900        current_die != NULL;
7901        current_die = dwarf2_extension (die, &cu))
7902     {
7903       name = dwarf2_name (current_die, cu);
7904       if (name != NULL)
7905         break;
7906     }
7907
7908   /* Is it an anonymous namespace?  */
7909
7910   *is_anonymous = (name == NULL);
7911   if (*is_anonymous)
7912     name = CP_ANONYMOUS_NAMESPACE_STR;
7913
7914   return name;
7915 }
7916
7917 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7918    the user defined type vector.  */
7919
7920 static struct type *
7921 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7922 {
7923   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7924   struct comp_unit_head *cu_header = &cu->header;
7925   struct type *type;
7926   struct attribute *attr_byte_size;
7927   struct attribute *attr_address_class;
7928   int byte_size, addr_class;
7929   struct type *target_type;
7930
7931   target_type = die_type (die, cu);
7932
7933   /* The die_type call above may have already set the type for this DIE.  */
7934   type = get_die_type (die, cu);
7935   if (type)
7936     return type;
7937
7938   type = lookup_pointer_type (target_type);
7939
7940   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7941   if (attr_byte_size)
7942     byte_size = DW_UNSND (attr_byte_size);
7943   else
7944     byte_size = cu_header->addr_size;
7945
7946   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7947   if (attr_address_class)
7948     addr_class = DW_UNSND (attr_address_class);
7949   else
7950     addr_class = DW_ADDR_none;
7951
7952   /* If the pointer size or address class is different than the
7953      default, create a type variant marked as such and set the
7954      length accordingly.  */
7955   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7956     {
7957       if (gdbarch_address_class_type_flags_p (gdbarch))
7958         {
7959           int type_flags;
7960
7961           type_flags = gdbarch_address_class_type_flags
7962                          (gdbarch, byte_size, addr_class);
7963           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7964                       == 0);
7965           type = make_type_with_address_space (type, type_flags);
7966         }
7967       else if (TYPE_LENGTH (type) != byte_size)
7968         {
7969           complaint (&symfile_complaints,
7970                      _("invalid pointer size %d"), byte_size);
7971         }
7972       else
7973         {
7974           /* Should we also complain about unhandled address classes?  */
7975         }
7976     }
7977
7978   TYPE_LENGTH (type) = byte_size;
7979   return set_die_type (die, type, cu);
7980 }
7981
7982 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7983    the user defined type vector.  */
7984
7985 static struct type *
7986 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7987 {
7988   struct type *type;
7989   struct type *to_type;
7990   struct type *domain;
7991
7992   to_type = die_type (die, cu);
7993   domain = die_containing_type (die, cu);
7994
7995   /* The calls above may have already set the type for this DIE.  */
7996   type = get_die_type (die, cu);
7997   if (type)
7998     return type;
7999
8000   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8001     type = lookup_methodptr_type (to_type);
8002   else
8003     type = lookup_memberptr_type (to_type, domain);
8004
8005   return set_die_type (die, type, cu);
8006 }
8007
8008 /* Extract all information from a DW_TAG_reference_type DIE and add to
8009    the user defined type vector.  */
8010
8011 static struct type *
8012 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8013 {
8014   struct comp_unit_head *cu_header = &cu->header;
8015   struct type *type, *target_type;
8016   struct attribute *attr;
8017
8018   target_type = die_type (die, cu);
8019
8020   /* The die_type call above may have already set the type for this DIE.  */
8021   type = get_die_type (die, cu);
8022   if (type)
8023     return type;
8024
8025   type = lookup_reference_type (target_type);
8026   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8027   if (attr)
8028     {
8029       TYPE_LENGTH (type) = DW_UNSND (attr);
8030     }
8031   else
8032     {
8033       TYPE_LENGTH (type) = cu_header->addr_size;
8034     }
8035   return set_die_type (die, type, cu);
8036 }
8037
8038 static struct type *
8039 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8040 {
8041   struct type *base_type, *cv_type;
8042
8043   base_type = die_type (die, cu);
8044
8045   /* The die_type call above may have already set the type for this DIE.  */
8046   cv_type = get_die_type (die, cu);
8047   if (cv_type)
8048     return cv_type;
8049
8050   /* In case the const qualifier is applied to an array type, the element type
8051      is so qualified, not the array type (section 6.7.3 of C99).  */
8052   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8053     {
8054       struct type *el_type, *inner_array;
8055
8056       base_type = copy_type (base_type);
8057       inner_array = base_type;
8058
8059       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8060         {
8061           TYPE_TARGET_TYPE (inner_array) =
8062             copy_type (TYPE_TARGET_TYPE (inner_array));
8063           inner_array = TYPE_TARGET_TYPE (inner_array);
8064         }
8065
8066       el_type = TYPE_TARGET_TYPE (inner_array);
8067       TYPE_TARGET_TYPE (inner_array) =
8068         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8069
8070       return set_die_type (die, base_type, cu);
8071     }
8072
8073   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8074   return set_die_type (die, cv_type, cu);
8075 }
8076
8077 static struct type *
8078 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8079 {
8080   struct type *base_type, *cv_type;
8081
8082   base_type = die_type (die, cu);
8083
8084   /* The die_type call above may have already set the type for this DIE.  */
8085   cv_type = get_die_type (die, cu);
8086   if (cv_type)
8087     return cv_type;
8088
8089   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8090   return set_die_type (die, cv_type, cu);
8091 }
8092
8093 /* Extract all information from a DW_TAG_string_type DIE and add to
8094    the user defined type vector.  It isn't really a user defined type,
8095    but it behaves like one, with other DIE's using an AT_user_def_type
8096    attribute to reference it.  */
8097
8098 static struct type *
8099 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8100 {
8101   struct objfile *objfile = cu->objfile;
8102   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8103   struct type *type, *range_type, *index_type, *char_type;
8104   struct attribute *attr;
8105   unsigned int length;
8106
8107   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8108   if (attr)
8109     {
8110       length = DW_UNSND (attr);
8111     }
8112   else
8113     {
8114       /* Check for the DW_AT_byte_size attribute.  */
8115       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8116       if (attr)
8117         {
8118           length = DW_UNSND (attr);
8119         }
8120       else
8121         {
8122           length = 1;
8123         }
8124     }
8125
8126   index_type = objfile_type (objfile)->builtin_int;
8127   range_type = create_range_type (NULL, index_type, 1, length);
8128   char_type = language_string_char_type (cu->language_defn, gdbarch);
8129   type = create_string_type (NULL, char_type, range_type);
8130
8131   return set_die_type (die, type, cu);
8132 }
8133
8134 /* Handle DIES due to C code like:
8135
8136    struct foo
8137    {
8138    int (*funcp)(int a, long l);
8139    int b;
8140    };
8141
8142    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8143
8144 static struct type *
8145 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8146 {
8147   struct type *type;            /* Type that this function returns.  */
8148   struct type *ftype;           /* Function that returns above type.  */
8149   struct attribute *attr;
8150
8151   type = die_type (die, cu);
8152
8153   /* The die_type call above may have already set the type for this DIE.  */
8154   ftype = get_die_type (die, cu);
8155   if (ftype)
8156     return ftype;
8157
8158   ftype = lookup_function_type (type);
8159
8160   /* All functions in C++, Pascal and Java have prototypes.  */
8161   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8162   if ((attr && (DW_UNSND (attr) != 0))
8163       || cu->language == language_cplus
8164       || cu->language == language_java
8165       || cu->language == language_pascal)
8166     TYPE_PROTOTYPED (ftype) = 1;
8167   else if (producer_is_realview (cu->producer))
8168     /* RealView does not emit DW_AT_prototyped.  We can not
8169        distinguish prototyped and unprototyped functions; default to
8170        prototyped, since that is more common in modern code (and
8171        RealView warns about unprototyped functions).  */
8172     TYPE_PROTOTYPED (ftype) = 1;
8173
8174   /* Store the calling convention in the type if it's available in
8175      the subroutine die.  Otherwise set the calling convention to
8176      the default value DW_CC_normal.  */
8177   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8178   if (attr)
8179     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8180   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8181     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8182   else
8183     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8184
8185   /* We need to add the subroutine type to the die immediately so
8186      we don't infinitely recurse when dealing with parameters
8187      declared as the same subroutine type.  */
8188   set_die_type (die, ftype, cu);
8189
8190   if (die->child != NULL)
8191     {
8192       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8193       struct die_info *child_die;
8194       int nparams, iparams;
8195
8196       /* Count the number of parameters.
8197          FIXME: GDB currently ignores vararg functions, but knows about
8198          vararg member functions.  */
8199       nparams = 0;
8200       child_die = die->child;
8201       while (child_die && child_die->tag)
8202         {
8203           if (child_die->tag == DW_TAG_formal_parameter)
8204             nparams++;
8205           else if (child_die->tag == DW_TAG_unspecified_parameters)
8206             TYPE_VARARGS (ftype) = 1;
8207           child_die = sibling_die (child_die);
8208         }
8209
8210       /* Allocate storage for parameters and fill them in.  */
8211       TYPE_NFIELDS (ftype) = nparams;
8212       TYPE_FIELDS (ftype) = (struct field *)
8213         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8214
8215       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8216          even if we error out during the parameters reading below.  */
8217       for (iparams = 0; iparams < nparams; iparams++)
8218         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8219
8220       iparams = 0;
8221       child_die = die->child;
8222       while (child_die && child_die->tag)
8223         {
8224           if (child_die->tag == DW_TAG_formal_parameter)
8225             {
8226               struct type *arg_type;
8227
8228               /* DWARF version 2 has no clean way to discern C++
8229                  static and non-static member functions.  G++ helps
8230                  GDB by marking the first parameter for non-static
8231                  member functions (which is the this pointer) as
8232                  artificial.  We pass this information to
8233                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8234
8235                  DWARF version 3 added DW_AT_object_pointer, which GCC
8236                  4.5 does not yet generate.  */
8237               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8238               if (attr)
8239                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8240               else
8241                 {
8242                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8243
8244                   /* GCC/43521: In java, the formal parameter
8245                      "this" is sometimes not marked with DW_AT_artificial.  */
8246                   if (cu->language == language_java)
8247                     {
8248                       const char *name = dwarf2_name (child_die, cu);
8249
8250                       if (name && !strcmp (name, "this"))
8251                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8252                     }
8253                 }
8254               arg_type = die_type (child_die, cu);
8255
8256               /* RealView does not mark THIS as const, which the testsuite
8257                  expects.  GCC marks THIS as const in method definitions,
8258                  but not in the class specifications (GCC PR 43053).  */
8259               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8260                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8261                 {
8262                   int is_this = 0;
8263                   struct dwarf2_cu *arg_cu = cu;
8264                   const char *name = dwarf2_name (child_die, cu);
8265
8266                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8267                   if (attr)
8268                     {
8269                       /* If the compiler emits this, use it.  */
8270                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8271                         is_this = 1;
8272                     }
8273                   else if (name && strcmp (name, "this") == 0)
8274                     /* Function definitions will have the argument names.  */
8275                     is_this = 1;
8276                   else if (name == NULL && iparams == 0)
8277                     /* Declarations may not have the names, so like
8278                        elsewhere in GDB, assume an artificial first
8279                        argument is "this".  */
8280                     is_this = 1;
8281
8282                   if (is_this)
8283                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8284                                              arg_type, 0);
8285                 }
8286
8287               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8288               iparams++;
8289             }
8290           child_die = sibling_die (child_die);
8291         }
8292     }
8293
8294   return ftype;
8295 }
8296
8297 static struct type *
8298 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8299 {
8300   struct objfile *objfile = cu->objfile;
8301   const char *name = NULL;
8302   struct type *this_type;
8303
8304   name = dwarf2_full_name (NULL, die, cu);
8305   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8306                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8307   TYPE_NAME (this_type) = (char *) name;
8308   set_die_type (die, this_type, cu);
8309   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8310   return this_type;
8311 }
8312
8313 /* Find a representation of a given base type and install
8314    it in the TYPE field of the die.  */
8315
8316 static struct type *
8317 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8318 {
8319   struct objfile *objfile = cu->objfile;
8320   struct type *type;
8321   struct attribute *attr;
8322   int encoding = 0, size = 0;
8323   char *name;
8324   enum type_code code = TYPE_CODE_INT;
8325   int type_flags = 0;
8326   struct type *target_type = NULL;
8327
8328   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8329   if (attr)
8330     {
8331       encoding = DW_UNSND (attr);
8332     }
8333   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8334   if (attr)
8335     {
8336       size = DW_UNSND (attr);
8337     }
8338   name = dwarf2_name (die, cu);
8339   if (!name)
8340     {
8341       complaint (&symfile_complaints,
8342                  _("DW_AT_name missing from DW_TAG_base_type"));
8343     }
8344
8345   switch (encoding)
8346     {
8347       case DW_ATE_address:
8348         /* Turn DW_ATE_address into a void * pointer.  */
8349         code = TYPE_CODE_PTR;
8350         type_flags |= TYPE_FLAG_UNSIGNED;
8351         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8352         break;
8353       case DW_ATE_boolean:
8354         code = TYPE_CODE_BOOL;
8355         type_flags |= TYPE_FLAG_UNSIGNED;
8356         break;
8357       case DW_ATE_complex_float:
8358         code = TYPE_CODE_COMPLEX;
8359         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8360         break;
8361       case DW_ATE_decimal_float:
8362         code = TYPE_CODE_DECFLOAT;
8363         break;
8364       case DW_ATE_float:
8365         code = TYPE_CODE_FLT;
8366         break;
8367       case DW_ATE_signed:
8368         break;
8369       case DW_ATE_unsigned:
8370         type_flags |= TYPE_FLAG_UNSIGNED;
8371         break;
8372       case DW_ATE_signed_char:
8373         if (cu->language == language_ada || cu->language == language_m2
8374             || cu->language == language_pascal)
8375           code = TYPE_CODE_CHAR;
8376         break;
8377       case DW_ATE_unsigned_char:
8378         if (cu->language == language_ada || cu->language == language_m2
8379             || cu->language == language_pascal)
8380           code = TYPE_CODE_CHAR;
8381         type_flags |= TYPE_FLAG_UNSIGNED;
8382         break;
8383       case DW_ATE_UTF:
8384         /* We just treat this as an integer and then recognize the
8385            type by name elsewhere.  */
8386         break;
8387
8388       default:
8389         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8390                    dwarf_type_encoding_name (encoding));
8391         break;
8392     }
8393
8394   type = init_type (code, size, type_flags, NULL, objfile);
8395   TYPE_NAME (type) = name;
8396   TYPE_TARGET_TYPE (type) = target_type;
8397
8398   if (name && strcmp (name, "char") == 0)
8399     TYPE_NOSIGN (type) = 1;
8400
8401   return set_die_type (die, type, cu);
8402 }
8403
8404 /* Read the given DW_AT_subrange DIE.  */
8405
8406 static struct type *
8407 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8408 {
8409   struct type *base_type;
8410   struct type *range_type;
8411   struct attribute *attr;
8412   LONGEST low = 0;
8413   LONGEST high = -1;
8414   char *name;
8415   LONGEST negative_mask;
8416
8417   base_type = die_type (die, cu);
8418   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8419   check_typedef (base_type);
8420
8421   /* The die_type call above may have already set the type for this DIE.  */
8422   range_type = get_die_type (die, cu);
8423   if (range_type)
8424     return range_type;
8425
8426   if (cu->language == language_fortran)
8427     {
8428       /* FORTRAN implies a lower bound of 1, if not given.  */
8429       low = 1;
8430     }
8431
8432   /* FIXME: For variable sized arrays either of these could be
8433      a variable rather than a constant value.  We'll allow it,
8434      but we don't know how to handle it.  */
8435   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8436   if (attr)
8437     low = dwarf2_get_attr_constant_value (attr, 0);
8438
8439   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8440   if (attr)
8441     {
8442       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8443         {
8444           /* GCC encodes arrays with unspecified or dynamic length
8445              with a DW_FORM_block1 attribute or a reference attribute.
8446              FIXME: GDB does not yet know how to handle dynamic
8447              arrays properly, treat them as arrays with unspecified
8448              length for now.
8449
8450              FIXME: jimb/2003-09-22: GDB does not really know
8451              how to handle arrays of unspecified length
8452              either; we just represent them as zero-length
8453              arrays.  Choose an appropriate upper bound given
8454              the lower bound we've computed above.  */
8455           high = low - 1;
8456         }
8457       else
8458         high = dwarf2_get_attr_constant_value (attr, 1);
8459     }
8460   else
8461     {
8462       attr = dwarf2_attr (die, DW_AT_count, cu);
8463       if (attr)
8464         {
8465           int count = dwarf2_get_attr_constant_value (attr, 1);
8466           high = low + count - 1;
8467         }
8468       else
8469         {
8470           /* Unspecified array length.  */
8471           high = low - 1;
8472         }
8473     }
8474
8475   /* Dwarf-2 specifications explicitly allows to create subrange types
8476      without specifying a base type.
8477      In that case, the base type must be set to the type of
8478      the lower bound, upper bound or count, in that order, if any of these
8479      three attributes references an object that has a type.
8480      If no base type is found, the Dwarf-2 specifications say that
8481      a signed integer type of size equal to the size of an address should
8482      be used.
8483      For the following C code: `extern char gdb_int [];'
8484      GCC produces an empty range DIE.
8485      FIXME: muller/2010-05-28: Possible references to object for low bound,
8486      high bound or count are not yet handled by this code.  */
8487   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8488     {
8489       struct objfile *objfile = cu->objfile;
8490       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8491       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8492       struct type *int_type = objfile_type (objfile)->builtin_int;
8493
8494       /* Test "int", "long int", and "long long int" objfile types,
8495          and select the first one having a size above or equal to the
8496          architecture address size.  */
8497       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8498         base_type = int_type;
8499       else
8500         {
8501           int_type = objfile_type (objfile)->builtin_long;
8502           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8503             base_type = int_type;
8504           else
8505             {
8506               int_type = objfile_type (objfile)->builtin_long_long;
8507               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8508                 base_type = int_type;
8509             }
8510         }
8511     }
8512
8513   negative_mask =
8514     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8515   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8516     low |= negative_mask;
8517   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8518     high |= negative_mask;
8519
8520   range_type = create_range_type (NULL, base_type, low, high);
8521
8522   /* Mark arrays with dynamic length at least as an array of unspecified
8523      length.  GDB could check the boundary but before it gets implemented at
8524      least allow accessing the array elements.  */
8525   if (attr && attr->form == DW_FORM_block1)
8526     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8527
8528   /* Ada expects an empty array on no boundary attributes.  */
8529   if (attr == NULL && cu->language != language_ada)
8530     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8531
8532   name = dwarf2_name (die, cu);
8533   if (name)
8534     TYPE_NAME (range_type) = name;
8535
8536   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8537   if (attr)
8538     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8539
8540   set_die_type (die, range_type, cu);
8541
8542   /* set_die_type should be already done.  */
8543   set_descriptive_type (range_type, die, cu);
8544
8545   return range_type;
8546 }
8547
8548 static struct type *
8549 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8550 {
8551   struct type *type;
8552
8553   /* For now, we only support the C meaning of an unspecified type: void.  */
8554
8555   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8556   TYPE_NAME (type) = dwarf2_name (die, cu);
8557
8558   return set_die_type (die, type, cu);
8559 }
8560
8561 /* Trivial hash function for die_info: the hash value of a DIE
8562    is its offset in .debug_info for this objfile.  */
8563
8564 static hashval_t
8565 die_hash (const void *item)
8566 {
8567   const struct die_info *die = item;
8568
8569   return die->offset;
8570 }
8571
8572 /* Trivial comparison function for die_info structures: two DIEs
8573    are equal if they have the same offset.  */
8574
8575 static int
8576 die_eq (const void *item_lhs, const void *item_rhs)
8577 {
8578   const struct die_info *die_lhs = item_lhs;
8579   const struct die_info *die_rhs = item_rhs;
8580
8581   return die_lhs->offset == die_rhs->offset;
8582 }
8583
8584 /* Read a whole compilation unit into a linked list of dies.  */
8585
8586 static struct die_info *
8587 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8588 {
8589   struct die_reader_specs reader_specs;
8590   int read_abbrevs = 0;
8591   struct cleanup *back_to = NULL;
8592   struct die_info *die;
8593
8594   if (cu->dwarf2_abbrevs == NULL)
8595     {
8596       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8597       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8598       read_abbrevs = 1;
8599     }
8600
8601   gdb_assert (cu->die_hash == NULL);
8602   cu->die_hash
8603     = htab_create_alloc_ex (cu->header.length / 12,
8604                             die_hash,
8605                             die_eq,
8606                             NULL,
8607                             &cu->comp_unit_obstack,
8608                             hashtab_obstack_allocate,
8609                             dummy_obstack_deallocate);
8610
8611   init_cu_die_reader (&reader_specs, cu);
8612
8613   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8614
8615   if (read_abbrevs)
8616     do_cleanups (back_to);
8617
8618   return die;
8619 }
8620
8621 /* Main entry point for reading a DIE and all children.
8622    Read the DIE and dump it if requested.  */
8623
8624 static struct die_info *
8625 read_die_and_children (const struct die_reader_specs *reader,
8626                        gdb_byte *info_ptr,
8627                        gdb_byte **new_info_ptr,
8628                        struct die_info *parent)
8629 {
8630   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8631                                                      new_info_ptr, parent);
8632
8633   if (dwarf2_die_debug)
8634     {
8635       fprintf_unfiltered (gdb_stdlog,
8636                           "\nRead die from %s of %s:\n",
8637                           reader->buffer == dwarf2_per_objfile->info.buffer
8638                           ? ".debug_info"
8639                           : reader->buffer == dwarf2_per_objfile->types.buffer
8640                           ? ".debug_types"
8641                           : "unknown section",
8642                           reader->abfd->filename);
8643       dump_die (result, dwarf2_die_debug);
8644     }
8645
8646   return result;
8647 }
8648
8649 /* Read a single die and all its descendents.  Set the die's sibling
8650    field to NULL; set other fields in the die correctly, and set all
8651    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8652    location of the info_ptr after reading all of those dies.  PARENT
8653    is the parent of the die in question.  */
8654
8655 static struct die_info *
8656 read_die_and_children_1 (const struct die_reader_specs *reader,
8657                          gdb_byte *info_ptr,
8658                          gdb_byte **new_info_ptr,
8659                          struct die_info *parent)
8660 {
8661   struct die_info *die;
8662   gdb_byte *cur_ptr;
8663   int has_children;
8664
8665   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8666   if (die == NULL)
8667     {
8668       *new_info_ptr = cur_ptr;
8669       return NULL;
8670     }
8671   store_in_ref_table (die, reader->cu);
8672
8673   if (has_children)
8674     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8675   else
8676     {
8677       die->child = NULL;
8678       *new_info_ptr = cur_ptr;
8679     }
8680
8681   die->sibling = NULL;
8682   die->parent = parent;
8683   return die;
8684 }
8685
8686 /* Read a die, all of its descendents, and all of its siblings; set
8687    all of the fields of all of the dies correctly.  Arguments are as
8688    in read_die_and_children.  */
8689
8690 static struct die_info *
8691 read_die_and_siblings (const struct die_reader_specs *reader,
8692                        gdb_byte *info_ptr,
8693                        gdb_byte **new_info_ptr,
8694                        struct die_info *parent)
8695 {
8696   struct die_info *first_die, *last_sibling;
8697   gdb_byte *cur_ptr;
8698
8699   cur_ptr = info_ptr;
8700   first_die = last_sibling = NULL;
8701
8702   while (1)
8703     {
8704       struct die_info *die
8705         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8706
8707       if (die == NULL)
8708         {
8709           *new_info_ptr = cur_ptr;
8710           return first_die;
8711         }
8712
8713       if (!first_die)
8714         first_die = die;
8715       else
8716         last_sibling->sibling = die;
8717
8718       last_sibling = die;
8719     }
8720 }
8721
8722 /* Read the die from the .debug_info section buffer.  Set DIEP to
8723    point to a newly allocated die with its information, except for its
8724    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8725    whether the die has children or not.  */
8726
8727 static gdb_byte *
8728 read_full_die (const struct die_reader_specs *reader,
8729                struct die_info **diep, gdb_byte *info_ptr,
8730                int *has_children)
8731 {
8732   unsigned int abbrev_number, bytes_read, i, offset;
8733   struct abbrev_info *abbrev;
8734   struct die_info *die;
8735   struct dwarf2_cu *cu = reader->cu;
8736   bfd *abfd = reader->abfd;
8737
8738   offset = info_ptr - reader->buffer;
8739   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8740   info_ptr += bytes_read;
8741   if (!abbrev_number)
8742     {
8743       *diep = NULL;
8744       *has_children = 0;
8745       return info_ptr;
8746     }
8747
8748   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8749   if (!abbrev)
8750     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8751            abbrev_number,
8752            bfd_get_filename (abfd));
8753
8754   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8755   die->offset = offset;
8756   die->tag = abbrev->tag;
8757   die->abbrev = abbrev_number;
8758
8759   die->num_attrs = abbrev->num_attrs;
8760
8761   for (i = 0; i < abbrev->num_attrs; ++i)
8762     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8763                                abfd, info_ptr, cu);
8764
8765   *diep = die;
8766   *has_children = abbrev->has_children;
8767   return info_ptr;
8768 }
8769
8770 /* In DWARF version 2, the description of the debugging information is
8771    stored in a separate .debug_abbrev section.  Before we read any
8772    dies from a section we read in all abbreviations and install them
8773    in a hash table.  This function also sets flags in CU describing
8774    the data found in the abbrev table.  */
8775
8776 static void
8777 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8778 {
8779   struct comp_unit_head *cu_header = &cu->header;
8780   gdb_byte *abbrev_ptr;
8781   struct abbrev_info *cur_abbrev;
8782   unsigned int abbrev_number, bytes_read, abbrev_name;
8783   unsigned int abbrev_form, hash_number;
8784   struct attr_abbrev *cur_attrs;
8785   unsigned int allocated_attrs;
8786
8787   /* Initialize dwarf2 abbrevs.  */
8788   obstack_init (&cu->abbrev_obstack);
8789   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8790                                       (ABBREV_HASH_SIZE
8791                                        * sizeof (struct abbrev_info *)));
8792   memset (cu->dwarf2_abbrevs, 0,
8793           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8794
8795   dwarf2_read_section (dwarf2_per_objfile->objfile,
8796                        &dwarf2_per_objfile->abbrev);
8797   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8798   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8799   abbrev_ptr += bytes_read;
8800
8801   allocated_attrs = ATTR_ALLOC_CHUNK;
8802   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8803
8804   /* Loop until we reach an abbrev number of 0.  */
8805   while (abbrev_number)
8806     {
8807       cur_abbrev = dwarf_alloc_abbrev (cu);
8808
8809       /* read in abbrev header */
8810       cur_abbrev->number = abbrev_number;
8811       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8812       abbrev_ptr += bytes_read;
8813       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8814       abbrev_ptr += 1;
8815
8816       if (cur_abbrev->tag == DW_TAG_namespace)
8817         cu->has_namespace_info = 1;
8818
8819       /* now read in declarations */
8820       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8821       abbrev_ptr += bytes_read;
8822       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8823       abbrev_ptr += bytes_read;
8824       while (abbrev_name)
8825         {
8826           if (cur_abbrev->num_attrs == allocated_attrs)
8827             {
8828               allocated_attrs += ATTR_ALLOC_CHUNK;
8829               cur_attrs
8830                 = xrealloc (cur_attrs, (allocated_attrs
8831                                         * sizeof (struct attr_abbrev)));
8832             }
8833
8834           /* Record whether this compilation unit might have
8835              inter-compilation-unit references.  If we don't know what form
8836              this attribute will have, then it might potentially be a
8837              DW_FORM_ref_addr, so we conservatively expect inter-CU
8838              references.  */
8839
8840           if (abbrev_form == DW_FORM_ref_addr
8841               || abbrev_form == DW_FORM_indirect)
8842             cu->has_form_ref_addr = 1;
8843
8844           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8845           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8846           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8847           abbrev_ptr += bytes_read;
8848           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8849           abbrev_ptr += bytes_read;
8850         }
8851
8852       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8853                                          (cur_abbrev->num_attrs
8854                                           * sizeof (struct attr_abbrev)));
8855       memcpy (cur_abbrev->attrs, cur_attrs,
8856               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8857
8858       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8859       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8860       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8861
8862       /* Get next abbreviation.
8863          Under Irix6 the abbreviations for a compilation unit are not
8864          always properly terminated with an abbrev number of 0.
8865          Exit loop if we encounter an abbreviation which we have
8866          already read (which means we are about to read the abbreviations
8867          for the next compile unit) or if the end of the abbreviation
8868          table is reached.  */
8869       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8870           >= dwarf2_per_objfile->abbrev.size)
8871         break;
8872       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8873       abbrev_ptr += bytes_read;
8874       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8875         break;
8876     }
8877
8878   xfree (cur_attrs);
8879 }
8880
8881 /* Release the memory used by the abbrev table for a compilation unit.  */
8882
8883 static void
8884 dwarf2_free_abbrev_table (void *ptr_to_cu)
8885 {
8886   struct dwarf2_cu *cu = ptr_to_cu;
8887
8888   obstack_free (&cu->abbrev_obstack, NULL);
8889   cu->dwarf2_abbrevs = NULL;
8890 }
8891
8892 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8893
8894 static struct abbrev_info *
8895 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8896 {
8897   unsigned int hash_number;
8898   struct abbrev_info *abbrev;
8899
8900   hash_number = number % ABBREV_HASH_SIZE;
8901   abbrev = cu->dwarf2_abbrevs[hash_number];
8902
8903   while (abbrev)
8904     {
8905       if (abbrev->number == number)
8906         return abbrev;
8907       else
8908         abbrev = abbrev->next;
8909     }
8910   return NULL;
8911 }
8912
8913 /* Returns nonzero if TAG represents a type that we might generate a partial
8914    symbol for.  */
8915
8916 static int
8917 is_type_tag_for_partial (int tag)
8918 {
8919   switch (tag)
8920     {
8921 #if 0
8922     /* Some types that would be reasonable to generate partial symbols for,
8923        that we don't at present.  */
8924     case DW_TAG_array_type:
8925     case DW_TAG_file_type:
8926     case DW_TAG_ptr_to_member_type:
8927     case DW_TAG_set_type:
8928     case DW_TAG_string_type:
8929     case DW_TAG_subroutine_type:
8930 #endif
8931     case DW_TAG_base_type:
8932     case DW_TAG_class_type:
8933     case DW_TAG_interface_type:
8934     case DW_TAG_enumeration_type:
8935     case DW_TAG_structure_type:
8936     case DW_TAG_subrange_type:
8937     case DW_TAG_typedef:
8938     case DW_TAG_union_type:
8939       return 1;
8940     default:
8941       return 0;
8942     }
8943 }
8944
8945 /* Load all DIEs that are interesting for partial symbols into memory.  */
8946
8947 static struct partial_die_info *
8948 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8949                    int building_psymtab, struct dwarf2_cu *cu)
8950 {
8951   struct partial_die_info *part_die;
8952   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8953   struct abbrev_info *abbrev;
8954   unsigned int bytes_read;
8955   unsigned int load_all = 0;
8956
8957   int nesting_level = 1;
8958
8959   parent_die = NULL;
8960   last_die = NULL;
8961
8962   if (cu->per_cu && cu->per_cu->load_all_dies)
8963     load_all = 1;
8964
8965   cu->partial_dies
8966     = htab_create_alloc_ex (cu->header.length / 12,
8967                             partial_die_hash,
8968                             partial_die_eq,
8969                             NULL,
8970                             &cu->comp_unit_obstack,
8971                             hashtab_obstack_allocate,
8972                             dummy_obstack_deallocate);
8973
8974   part_die = obstack_alloc (&cu->comp_unit_obstack,
8975                             sizeof (struct partial_die_info));
8976
8977   while (1)
8978     {
8979       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8980
8981       /* A NULL abbrev means the end of a series of children.  */
8982       if (abbrev == NULL)
8983         {
8984           if (--nesting_level == 0)
8985             {
8986               /* PART_DIE was probably the last thing allocated on the
8987                  comp_unit_obstack, so we could call obstack_free
8988                  here.  We don't do that because the waste is small,
8989                  and will be cleaned up when we're done with this
8990                  compilation unit.  This way, we're also more robust
8991                  against other users of the comp_unit_obstack.  */
8992               return first_die;
8993             }
8994           info_ptr += bytes_read;
8995           last_die = parent_die;
8996           parent_die = parent_die->die_parent;
8997           continue;
8998         }
8999
9000       /* Check for template arguments.  We never save these; if
9001          they're seen, we just mark the parent, and go on our way.  */
9002       if (parent_die != NULL
9003           && cu->language == language_cplus
9004           && (abbrev->tag == DW_TAG_template_type_param
9005               || abbrev->tag == DW_TAG_template_value_param))
9006         {
9007           parent_die->has_template_arguments = 1;
9008
9009           if (!load_all)
9010             {
9011               /* We don't need a partial DIE for the template argument.  */
9012               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9013                                        cu);
9014               continue;
9015             }
9016         }
9017
9018       /* We only recurse into subprograms looking for template arguments.
9019          Skip their other children.  */
9020       if (!load_all
9021           && cu->language == language_cplus
9022           && parent_die != NULL
9023           && parent_die->tag == DW_TAG_subprogram)
9024         {
9025           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9026           continue;
9027         }
9028
9029       /* Check whether this DIE is interesting enough to save.  Normally
9030          we would not be interested in members here, but there may be
9031          later variables referencing them via DW_AT_specification (for
9032          static members).  */
9033       if (!load_all
9034           && !is_type_tag_for_partial (abbrev->tag)
9035           && abbrev->tag != DW_TAG_constant
9036           && abbrev->tag != DW_TAG_enumerator
9037           && abbrev->tag != DW_TAG_subprogram
9038           && abbrev->tag != DW_TAG_lexical_block
9039           && abbrev->tag != DW_TAG_variable
9040           && abbrev->tag != DW_TAG_namespace
9041           && abbrev->tag != DW_TAG_module
9042           && abbrev->tag != DW_TAG_member)
9043         {
9044           /* Otherwise we skip to the next sibling, if any.  */
9045           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9046           continue;
9047         }
9048
9049       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9050                                    buffer, info_ptr, cu);
9051
9052       /* This two-pass algorithm for processing partial symbols has a
9053          high cost in cache pressure.  Thus, handle some simple cases
9054          here which cover the majority of C partial symbols.  DIEs
9055          which neither have specification tags in them, nor could have
9056          specification tags elsewhere pointing at them, can simply be
9057          processed and discarded.
9058
9059          This segment is also optional; scan_partial_symbols and
9060          add_partial_symbol will handle these DIEs if we chain
9061          them in normally.  When compilers which do not emit large
9062          quantities of duplicate debug information are more common,
9063          this code can probably be removed.  */
9064
9065       /* Any complete simple types at the top level (pretty much all
9066          of them, for a language without namespaces), can be processed
9067          directly.  */
9068       if (parent_die == NULL
9069           && part_die->has_specification == 0
9070           && part_die->is_declaration == 0
9071           && (part_die->tag == DW_TAG_typedef
9072               || part_die->tag == DW_TAG_base_type
9073               || part_die->tag == DW_TAG_subrange_type))
9074         {
9075           if (building_psymtab && part_die->name != NULL)
9076             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9077                                  VAR_DOMAIN, LOC_TYPEDEF,
9078                                  &cu->objfile->static_psymbols,
9079                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9080           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9081           continue;
9082         }
9083
9084       /* If we're at the second level, and we're an enumerator, and
9085          our parent has no specification (meaning possibly lives in a
9086          namespace elsewhere), then we can add the partial symbol now
9087          instead of queueing it.  */
9088       if (part_die->tag == DW_TAG_enumerator
9089           && parent_die != NULL
9090           && parent_die->die_parent == NULL
9091           && parent_die->tag == DW_TAG_enumeration_type
9092           && parent_die->has_specification == 0)
9093         {
9094           if (part_die->name == NULL)
9095             complaint (&symfile_complaints,
9096                        _("malformed enumerator DIE ignored"));
9097           else if (building_psymtab)
9098             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9099                                  VAR_DOMAIN, LOC_CONST,
9100                                  (cu->language == language_cplus
9101                                   || cu->language == language_java)
9102                                  ? &cu->objfile->global_psymbols
9103                                  : &cu->objfile->static_psymbols,
9104                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9105
9106           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9107           continue;
9108         }
9109
9110       /* We'll save this DIE so link it in.  */
9111       part_die->die_parent = parent_die;
9112       part_die->die_sibling = NULL;
9113       part_die->die_child = NULL;
9114
9115       if (last_die && last_die == parent_die)
9116         last_die->die_child = part_die;
9117       else if (last_die)
9118         last_die->die_sibling = part_die;
9119
9120       last_die = part_die;
9121
9122       if (first_die == NULL)
9123         first_die = part_die;
9124
9125       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9126          find interesting need to be in the hash table, because we
9127          also have the parent/sibling/child chains; only those that we
9128          might refer to by offset later during partial symbol reading.
9129
9130          For now this means things that might have be the target of a
9131          DW_AT_specification, DW_AT_abstract_origin, or
9132          DW_AT_extension.  DW_AT_extension will refer only to
9133          namespaces; DW_AT_abstract_origin refers to functions (and
9134          many things under the function DIE, but we do not recurse
9135          into function DIEs during partial symbol reading) and
9136          possibly variables as well; DW_AT_specification refers to
9137          declarations.  Declarations ought to have the DW_AT_declaration
9138          flag.  It happens that GCC forgets to put it in sometimes, but
9139          only for functions, not for types.
9140
9141          Adding more things than necessary to the hash table is harmless
9142          except for the performance cost.  Adding too few will result in
9143          wasted time in find_partial_die, when we reread the compilation
9144          unit with load_all_dies set.  */
9145
9146       if (load_all
9147           || abbrev->tag == DW_TAG_constant
9148           || abbrev->tag == DW_TAG_subprogram
9149           || abbrev->tag == DW_TAG_variable
9150           || abbrev->tag == DW_TAG_namespace
9151           || part_die->is_declaration)
9152         {
9153           void **slot;
9154
9155           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9156                                            part_die->offset, INSERT);
9157           *slot = part_die;
9158         }
9159
9160       part_die = obstack_alloc (&cu->comp_unit_obstack,
9161                                 sizeof (struct partial_die_info));
9162
9163       /* For some DIEs we want to follow their children (if any).  For C
9164          we have no reason to follow the children of structures; for other
9165          languages we have to, so that we can get at method physnames
9166          to infer fully qualified class names, for DW_AT_specification,
9167          and for C++ template arguments.  For C++, we also look one level
9168          inside functions to find template arguments (if the name of the
9169          function does not already contain the template arguments).
9170
9171          For Ada, we need to scan the children of subprograms and lexical
9172          blocks as well because Ada allows the definition of nested
9173          entities that could be interesting for the debugger, such as
9174          nested subprograms for instance.  */
9175       if (last_die->has_children
9176           && (load_all
9177               || last_die->tag == DW_TAG_namespace
9178               || last_die->tag == DW_TAG_module
9179               || last_die->tag == DW_TAG_enumeration_type
9180               || (cu->language == language_cplus
9181                   && last_die->tag == DW_TAG_subprogram
9182                   && (last_die->name == NULL
9183                       || strchr (last_die->name, '<') == NULL))
9184               || (cu->language != language_c
9185                   && (last_die->tag == DW_TAG_class_type
9186                       || last_die->tag == DW_TAG_interface_type
9187                       || last_die->tag == DW_TAG_structure_type
9188                       || last_die->tag == DW_TAG_union_type))
9189               || (cu->language == language_ada
9190                   && (last_die->tag == DW_TAG_subprogram
9191                       || last_die->tag == DW_TAG_lexical_block))))
9192         {
9193           nesting_level++;
9194           parent_die = last_die;
9195           continue;
9196         }
9197
9198       /* Otherwise we skip to the next sibling, if any.  */
9199       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9200
9201       /* Back to the top, do it again.  */
9202     }
9203 }
9204
9205 /* Read a minimal amount of information into the minimal die structure.  */
9206
9207 static gdb_byte *
9208 read_partial_die (struct partial_die_info *part_die,
9209                   struct abbrev_info *abbrev,
9210                   unsigned int abbrev_len, bfd *abfd,
9211                   gdb_byte *buffer, gdb_byte *info_ptr,
9212                   struct dwarf2_cu *cu)
9213 {
9214   unsigned int i;
9215   struct attribute attr;
9216   int has_low_pc_attr = 0;
9217   int has_high_pc_attr = 0;
9218
9219   memset (part_die, 0, sizeof (struct partial_die_info));
9220
9221   part_die->offset = info_ptr - buffer;
9222
9223   info_ptr += abbrev_len;
9224
9225   if (abbrev == NULL)
9226     return info_ptr;
9227
9228   part_die->tag = abbrev->tag;
9229   part_die->has_children = abbrev->has_children;
9230
9231   for (i = 0; i < abbrev->num_attrs; ++i)
9232     {
9233       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9234
9235       /* Store the data if it is of an attribute we want to keep in a
9236          partial symbol table.  */
9237       switch (attr.name)
9238         {
9239         case DW_AT_name:
9240           switch (part_die->tag)
9241             {
9242             case DW_TAG_compile_unit:
9243             case DW_TAG_type_unit:
9244               /* Compilation units have a DW_AT_name that is a filename, not
9245                  a source language identifier.  */
9246             case DW_TAG_enumeration_type:
9247             case DW_TAG_enumerator:
9248               /* These tags always have simple identifiers already; no need
9249                  to canonicalize them.  */
9250               part_die->name = DW_STRING (&attr);
9251               break;
9252             default:
9253               part_die->name
9254                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9255                                             &cu->objfile->objfile_obstack);
9256               break;
9257             }
9258           break;
9259         case DW_AT_linkage_name:
9260         case DW_AT_MIPS_linkage_name:
9261           /* Note that both forms of linkage name might appear.  We
9262              assume they will be the same, and we only store the last
9263              one we see.  */
9264           if (cu->language == language_ada)
9265             part_die->name = DW_STRING (&attr);
9266           part_die->linkage_name = DW_STRING (&attr);
9267           break;
9268         case DW_AT_low_pc:
9269           has_low_pc_attr = 1;
9270           part_die->lowpc = DW_ADDR (&attr);
9271           break;
9272         case DW_AT_high_pc:
9273           has_high_pc_attr = 1;
9274           part_die->highpc = DW_ADDR (&attr);
9275           break;
9276         case DW_AT_location:
9277           /* Support the .debug_loc offsets.  */
9278           if (attr_form_is_block (&attr))
9279             {
9280                part_die->locdesc = DW_BLOCK (&attr);
9281             }
9282           else if (attr_form_is_section_offset (&attr))
9283             {
9284               dwarf2_complex_location_expr_complaint ();
9285             }
9286           else
9287             {
9288               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9289                                                      "partial symbol information");
9290             }
9291           break;
9292         case DW_AT_external:
9293           part_die->is_external = DW_UNSND (&attr);
9294           break;
9295         case DW_AT_declaration:
9296           part_die->is_declaration = DW_UNSND (&attr);
9297           break;
9298         case DW_AT_type:
9299           part_die->has_type = 1;
9300           break;
9301         case DW_AT_abstract_origin:
9302         case DW_AT_specification:
9303         case DW_AT_extension:
9304           part_die->has_specification = 1;
9305           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9306           break;
9307         case DW_AT_sibling:
9308           /* Ignore absolute siblings, they might point outside of
9309              the current compile unit.  */
9310           if (attr.form == DW_FORM_ref_addr)
9311             complaint (&symfile_complaints,
9312                        _("ignoring absolute DW_AT_sibling"));
9313           else
9314             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9315           break;
9316         case DW_AT_byte_size:
9317           part_die->has_byte_size = 1;
9318           break;
9319         case DW_AT_calling_convention:
9320           /* DWARF doesn't provide a way to identify a program's source-level
9321              entry point.  DW_AT_calling_convention attributes are only meant
9322              to describe functions' calling conventions.
9323
9324              However, because it's a necessary piece of information in
9325              Fortran, and because DW_CC_program is the only piece of debugging
9326              information whose definition refers to a 'main program' at all,
9327              several compilers have begun marking Fortran main programs with
9328              DW_CC_program --- even when those functions use the standard
9329              calling conventions.
9330
9331              So until DWARF specifies a way to provide this information and
9332              compilers pick up the new representation, we'll support this
9333              practice.  */
9334           if (DW_UNSND (&attr) == DW_CC_program
9335               && cu->language == language_fortran)
9336             {
9337               set_main_name (part_die->name);
9338
9339               /* As this DIE has a static linkage the name would be difficult
9340                  to look up later.  */
9341               language_of_main = language_fortran;
9342             }
9343           break;
9344         default:
9345           break;
9346         }
9347     }
9348
9349   if (has_low_pc_attr && has_high_pc_attr)
9350     {
9351       /* When using the GNU linker, .gnu.linkonce. sections are used to
9352          eliminate duplicate copies of functions and vtables and such.
9353          The linker will arbitrarily choose one and discard the others.
9354          The AT_*_pc values for such functions refer to local labels in
9355          these sections.  If the section from that file was discarded, the
9356          labels are not in the output, so the relocs get a value of 0.
9357          If this is a discarded function, mark the pc bounds as invalid,
9358          so that GDB will ignore it.  */
9359       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9360         {
9361           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9362
9363           complaint (&symfile_complaints,
9364                      _("DW_AT_low_pc %s is zero "
9365                        "for DIE at 0x%x [in module %s]"),
9366                      paddress (gdbarch, part_die->lowpc),
9367                      part_die->offset, cu->objfile->name);
9368         }
9369       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9370       else if (part_die->lowpc >= part_die->highpc)
9371         {
9372           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9373
9374           complaint (&symfile_complaints,
9375                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9376                        "for DIE at 0x%x [in module %s]"),
9377                      paddress (gdbarch, part_die->lowpc),
9378                      paddress (gdbarch, part_die->highpc),
9379                      part_die->offset, cu->objfile->name);
9380         }
9381       else
9382         part_die->has_pc_info = 1;
9383     }
9384
9385   return info_ptr;
9386 }
9387
9388 /* Find a cached partial DIE at OFFSET in CU.  */
9389
9390 static struct partial_die_info *
9391 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9392 {
9393   struct partial_die_info *lookup_die = NULL;
9394   struct partial_die_info part_die;
9395
9396   part_die.offset = offset;
9397   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9398
9399   return lookup_die;
9400 }
9401
9402 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9403    except in the case of .debug_types DIEs which do not reference
9404    outside their CU (they do however referencing other types via
9405    DW_FORM_ref_sig8).  */
9406
9407 static struct partial_die_info *
9408 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9409 {
9410   struct dwarf2_per_cu_data *per_cu = NULL;
9411   struct partial_die_info *pd = NULL;
9412
9413   if (cu->per_cu->from_debug_types)
9414     {
9415       pd = find_partial_die_in_comp_unit (offset, cu);
9416       if (pd != NULL)
9417         return pd;
9418       goto not_found;
9419     }
9420
9421   if (offset_in_cu_p (&cu->header, offset))
9422     {
9423       pd = find_partial_die_in_comp_unit (offset, cu);
9424       if (pd != NULL)
9425         return pd;
9426     }
9427
9428   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9429
9430   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9431     load_partial_comp_unit (per_cu, cu->objfile);
9432
9433   per_cu->cu->last_used = 0;
9434   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9435
9436   if (pd == NULL && per_cu->load_all_dies == 0)
9437     {
9438       struct cleanup *back_to;
9439       struct partial_die_info comp_unit_die;
9440       struct abbrev_info *abbrev;
9441       unsigned int bytes_read;
9442       char *info_ptr;
9443
9444       per_cu->load_all_dies = 1;
9445
9446       /* Re-read the DIEs.  */
9447       back_to = make_cleanup (null_cleanup, 0);
9448       if (per_cu->cu->dwarf2_abbrevs == NULL)
9449         {
9450           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9451           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9452         }
9453       info_ptr = (dwarf2_per_objfile->info.buffer
9454                   + per_cu->cu->header.offset
9455                   + per_cu->cu->header.first_die_offset);
9456       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9457       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9458                                    per_cu->cu->objfile->obfd,
9459                                    dwarf2_per_objfile->info.buffer, info_ptr,
9460                                    per_cu->cu);
9461       if (comp_unit_die.has_children)
9462         load_partial_dies (per_cu->cu->objfile->obfd,
9463                            dwarf2_per_objfile->info.buffer, info_ptr,
9464                            0, per_cu->cu);
9465       do_cleanups (back_to);
9466
9467       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9468     }
9469
9470  not_found:
9471
9472   if (pd == NULL)
9473     internal_error (__FILE__, __LINE__,
9474                     _("could not find partial DIE 0x%x "
9475                       "in cache [from module %s]\n"),
9476                     offset, bfd_get_filename (cu->objfile->obfd));
9477   return pd;
9478 }
9479
9480 /* See if we can figure out if the class lives in a namespace.  We do
9481    this by looking for a member function; its demangled name will
9482    contain namespace info, if there is any.  */
9483
9484 static void
9485 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9486                                   struct dwarf2_cu *cu)
9487 {
9488   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9489      what template types look like, because the demangler
9490      frequently doesn't give the same name as the debug info.  We
9491      could fix this by only using the demangled name to get the
9492      prefix (but see comment in read_structure_type).  */
9493
9494   struct partial_die_info *real_pdi;
9495   struct partial_die_info *child_pdi;
9496
9497   /* If this DIE (this DIE's specification, if any) has a parent, then
9498      we should not do this.  We'll prepend the parent's fully qualified
9499      name when we create the partial symbol.  */
9500
9501   real_pdi = struct_pdi;
9502   while (real_pdi->has_specification)
9503     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9504
9505   if (real_pdi->die_parent != NULL)
9506     return;
9507
9508   for (child_pdi = struct_pdi->die_child;
9509        child_pdi != NULL;
9510        child_pdi = child_pdi->die_sibling)
9511     {
9512       if (child_pdi->tag == DW_TAG_subprogram
9513           && child_pdi->linkage_name != NULL)
9514         {
9515           char *actual_class_name
9516             = language_class_name_from_physname (cu->language_defn,
9517                                                  child_pdi->linkage_name);
9518           if (actual_class_name != NULL)
9519             {
9520               struct_pdi->name
9521                 = obsavestring (actual_class_name,
9522                                 strlen (actual_class_name),
9523                                 &cu->objfile->objfile_obstack);
9524               xfree (actual_class_name);
9525             }
9526           break;
9527         }
9528     }
9529 }
9530
9531 /* Adjust PART_DIE before generating a symbol for it.  This function
9532    may set the is_external flag or change the DIE's name.  */
9533
9534 static void
9535 fixup_partial_die (struct partial_die_info *part_die,
9536                    struct dwarf2_cu *cu)
9537 {
9538   /* Once we've fixed up a die, there's no point in doing so again.
9539      This also avoids a memory leak if we were to call
9540      guess_partial_die_structure_name multiple times.  */
9541   if (part_die->fixup_called)
9542     return;
9543
9544   /* If we found a reference attribute and the DIE has no name, try
9545      to find a name in the referred to DIE.  */
9546
9547   if (part_die->name == NULL && part_die->has_specification)
9548     {
9549       struct partial_die_info *spec_die;
9550
9551       spec_die = find_partial_die (part_die->spec_offset, cu);
9552
9553       fixup_partial_die (spec_die, cu);
9554
9555       if (spec_die->name)
9556         {
9557           part_die->name = spec_die->name;
9558
9559           /* Copy DW_AT_external attribute if it is set.  */
9560           if (spec_die->is_external)
9561             part_die->is_external = spec_die->is_external;
9562         }
9563     }
9564
9565   /* Set default names for some unnamed DIEs.  */
9566
9567   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9568     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9569
9570   /* If there is no parent die to provide a namespace, and there are
9571      children, see if we can determine the namespace from their linkage
9572      name.
9573      NOTE: We need to do this even if cu->has_namespace_info != 0.
9574      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9575   if (cu->language == language_cplus
9576       && dwarf2_per_objfile->types.asection != NULL
9577       && part_die->die_parent == NULL
9578       && part_die->has_children
9579       && (part_die->tag == DW_TAG_class_type
9580           || part_die->tag == DW_TAG_structure_type
9581           || part_die->tag == DW_TAG_union_type))
9582     guess_partial_die_structure_name (part_die, cu);
9583
9584   /* GCC might emit a nameless struct or union that has a linkage
9585      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
9586   if (part_die->name == NULL
9587       && (part_die->tag == DW_TAG_structure_type
9588           || part_die->tag == DW_TAG_union_type
9589           || part_die->tag == DW_TAG_class_type)
9590       && part_die->linkage_name != NULL)
9591     {
9592       char *demangled;
9593
9594       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9595       if (demangled)
9596         {
9597           part_die->name = obsavestring (demangled, strlen (demangled),
9598                                          &cu->objfile->objfile_obstack);
9599           xfree (demangled);
9600         }
9601     }
9602
9603   part_die->fixup_called = 1;
9604 }
9605
9606 /* Read an attribute value described by an attribute form.  */
9607
9608 static gdb_byte *
9609 read_attribute_value (struct attribute *attr, unsigned form,
9610                       bfd *abfd, gdb_byte *info_ptr,
9611                       struct dwarf2_cu *cu)
9612 {
9613   struct comp_unit_head *cu_header = &cu->header;
9614   unsigned int bytes_read;
9615   struct dwarf_block *blk;
9616
9617   attr->form = form;
9618   switch (form)
9619     {
9620     case DW_FORM_ref_addr:
9621       if (cu->header.version == 2)
9622         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9623       else
9624         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9625                                       &cu->header, &bytes_read);
9626       info_ptr += bytes_read;
9627       break;
9628     case DW_FORM_addr:
9629       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9630       info_ptr += bytes_read;
9631       break;
9632     case DW_FORM_block2:
9633       blk = dwarf_alloc_block (cu);
9634       blk->size = read_2_bytes (abfd, info_ptr);
9635       info_ptr += 2;
9636       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9637       info_ptr += blk->size;
9638       DW_BLOCK (attr) = blk;
9639       break;
9640     case DW_FORM_block4:
9641       blk = dwarf_alloc_block (cu);
9642       blk->size = read_4_bytes (abfd, info_ptr);
9643       info_ptr += 4;
9644       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9645       info_ptr += blk->size;
9646       DW_BLOCK (attr) = blk;
9647       break;
9648     case DW_FORM_data2:
9649       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9650       info_ptr += 2;
9651       break;
9652     case DW_FORM_data4:
9653       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9654       info_ptr += 4;
9655       break;
9656     case DW_FORM_data8:
9657       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9658       info_ptr += 8;
9659       break;
9660     case DW_FORM_sec_offset:
9661       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9662       info_ptr += bytes_read;
9663       break;
9664     case DW_FORM_string:
9665       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9666       DW_STRING_IS_CANONICAL (attr) = 0;
9667       info_ptr += bytes_read;
9668       break;
9669     case DW_FORM_strp:
9670       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9671                                                &bytes_read);
9672       DW_STRING_IS_CANONICAL (attr) = 0;
9673       info_ptr += bytes_read;
9674       break;
9675     case DW_FORM_exprloc:
9676     case DW_FORM_block:
9677       blk = dwarf_alloc_block (cu);
9678       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9679       info_ptr += bytes_read;
9680       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9681       info_ptr += blk->size;
9682       DW_BLOCK (attr) = blk;
9683       break;
9684     case DW_FORM_block1:
9685       blk = dwarf_alloc_block (cu);
9686       blk->size = read_1_byte (abfd, info_ptr);
9687       info_ptr += 1;
9688       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9689       info_ptr += blk->size;
9690       DW_BLOCK (attr) = blk;
9691       break;
9692     case DW_FORM_data1:
9693       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9694       info_ptr += 1;
9695       break;
9696     case DW_FORM_flag:
9697       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9698       info_ptr += 1;
9699       break;
9700     case DW_FORM_flag_present:
9701       DW_UNSND (attr) = 1;
9702       break;
9703     case DW_FORM_sdata:
9704       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9705       info_ptr += bytes_read;
9706       break;
9707     case DW_FORM_udata:
9708       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9709       info_ptr += bytes_read;
9710       break;
9711     case DW_FORM_ref1:
9712       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9713       info_ptr += 1;
9714       break;
9715     case DW_FORM_ref2:
9716       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9717       info_ptr += 2;
9718       break;
9719     case DW_FORM_ref4:
9720       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9721       info_ptr += 4;
9722       break;
9723     case DW_FORM_ref8:
9724       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9725       info_ptr += 8;
9726       break;
9727     case DW_FORM_ref_sig8:
9728       /* Convert the signature to something we can record in DW_UNSND
9729          for later lookup.
9730          NOTE: This is NULL if the type wasn't found.  */
9731       DW_SIGNATURED_TYPE (attr) =
9732         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9733       info_ptr += 8;
9734       break;
9735     case DW_FORM_ref_udata:
9736       DW_ADDR (attr) = (cu->header.offset
9737                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9738       info_ptr += bytes_read;
9739       break;
9740     case DW_FORM_indirect:
9741       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9742       info_ptr += bytes_read;
9743       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9744       break;
9745     default:
9746       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9747              dwarf_form_name (form),
9748              bfd_get_filename (abfd));
9749     }
9750
9751   /* We have seen instances where the compiler tried to emit a byte
9752      size attribute of -1 which ended up being encoded as an unsigned
9753      0xffffffff.  Although 0xffffffff is technically a valid size value,
9754      an object of this size seems pretty unlikely so we can relatively
9755      safely treat these cases as if the size attribute was invalid and
9756      treat them as zero by default.  */
9757   if (attr->name == DW_AT_byte_size
9758       && form == DW_FORM_data4
9759       && DW_UNSND (attr) >= 0xffffffff)
9760     {
9761       complaint
9762         (&symfile_complaints,
9763          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9764          hex_string (DW_UNSND (attr)));
9765       DW_UNSND (attr) = 0;
9766     }
9767
9768   return info_ptr;
9769 }
9770
9771 /* Read an attribute described by an abbreviated attribute.  */
9772
9773 static gdb_byte *
9774 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9775                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9776 {
9777   attr->name = abbrev->name;
9778   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9779 }
9780
9781 /* Read dwarf information from a buffer.  */
9782
9783 static unsigned int
9784 read_1_byte (bfd *abfd, gdb_byte *buf)
9785 {
9786   return bfd_get_8 (abfd, buf);
9787 }
9788
9789 static int
9790 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9791 {
9792   return bfd_get_signed_8 (abfd, buf);
9793 }
9794
9795 static unsigned int
9796 read_2_bytes (bfd *abfd, gdb_byte *buf)
9797 {
9798   return bfd_get_16 (abfd, buf);
9799 }
9800
9801 static int
9802 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9803 {
9804   return bfd_get_signed_16 (abfd, buf);
9805 }
9806
9807 static unsigned int
9808 read_4_bytes (bfd *abfd, gdb_byte *buf)
9809 {
9810   return bfd_get_32 (abfd, buf);
9811 }
9812
9813 static int
9814 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9815 {
9816   return bfd_get_signed_32 (abfd, buf);
9817 }
9818
9819 static ULONGEST
9820 read_8_bytes (bfd *abfd, gdb_byte *buf)
9821 {
9822   return bfd_get_64 (abfd, buf);
9823 }
9824
9825 static CORE_ADDR
9826 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9827               unsigned int *bytes_read)
9828 {
9829   struct comp_unit_head *cu_header = &cu->header;
9830   CORE_ADDR retval = 0;
9831
9832   if (cu_header->signed_addr_p)
9833     {
9834       switch (cu_header->addr_size)
9835         {
9836         case 2:
9837           retval = bfd_get_signed_16 (abfd, buf);
9838           break;
9839         case 4:
9840           retval = bfd_get_signed_32 (abfd, buf);
9841           break;
9842         case 8:
9843           retval = bfd_get_signed_64 (abfd, buf);
9844           break;
9845         default:
9846           internal_error (__FILE__, __LINE__,
9847                           _("read_address: bad switch, signed [in module %s]"),
9848                           bfd_get_filename (abfd));
9849         }
9850     }
9851   else
9852     {
9853       switch (cu_header->addr_size)
9854         {
9855         case 2:
9856           retval = bfd_get_16 (abfd, buf);
9857           break;
9858         case 4:
9859           retval = bfd_get_32 (abfd, buf);
9860           break;
9861         case 8:
9862           retval = bfd_get_64 (abfd, buf);
9863           break;
9864         default:
9865           internal_error (__FILE__, __LINE__,
9866                           _("read_address: bad switch, "
9867                             "unsigned [in module %s]"),
9868                           bfd_get_filename (abfd));
9869         }
9870     }
9871
9872   *bytes_read = cu_header->addr_size;
9873   return retval;
9874 }
9875
9876 /* Read the initial length from a section.  The (draft) DWARF 3
9877    specification allows the initial length to take up either 4 bytes
9878    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9879    bytes describe the length and all offsets will be 8 bytes in length
9880    instead of 4.
9881
9882    An older, non-standard 64-bit format is also handled by this
9883    function.  The older format in question stores the initial length
9884    as an 8-byte quantity without an escape value.  Lengths greater
9885    than 2^32 aren't very common which means that the initial 4 bytes
9886    is almost always zero.  Since a length value of zero doesn't make
9887    sense for the 32-bit format, this initial zero can be considered to
9888    be an escape value which indicates the presence of the older 64-bit
9889    format.  As written, the code can't detect (old format) lengths
9890    greater than 4GB.  If it becomes necessary to handle lengths
9891    somewhat larger than 4GB, we could allow other small values (such
9892    as the non-sensical values of 1, 2, and 3) to also be used as
9893    escape values indicating the presence of the old format.
9894
9895    The value returned via bytes_read should be used to increment the
9896    relevant pointer after calling read_initial_length().
9897
9898    [ Note:  read_initial_length() and read_offset() are based on the
9899      document entitled "DWARF Debugging Information Format", revision
9900      3, draft 8, dated November 19, 2001.  This document was obtained
9901      from:
9902
9903         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9904
9905      This document is only a draft and is subject to change.  (So beware.)
9906
9907      Details regarding the older, non-standard 64-bit format were
9908      determined empirically by examining 64-bit ELF files produced by
9909      the SGI toolchain on an IRIX 6.5 machine.
9910
9911      - Kevin, July 16, 2002
9912    ] */
9913
9914 static LONGEST
9915 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9916 {
9917   LONGEST length = bfd_get_32 (abfd, buf);
9918
9919   if (length == 0xffffffff)
9920     {
9921       length = bfd_get_64 (abfd, buf + 4);
9922       *bytes_read = 12;
9923     }
9924   else if (length == 0)
9925     {
9926       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9927       length = bfd_get_64 (abfd, buf);
9928       *bytes_read = 8;
9929     }
9930   else
9931     {
9932       *bytes_read = 4;
9933     }
9934
9935   return length;
9936 }
9937
9938 /* Cover function for read_initial_length.
9939    Returns the length of the object at BUF, and stores the size of the
9940    initial length in *BYTES_READ and stores the size that offsets will be in
9941    *OFFSET_SIZE.
9942    If the initial length size is not equivalent to that specified in
9943    CU_HEADER then issue a complaint.
9944    This is useful when reading non-comp-unit headers.  */
9945
9946 static LONGEST
9947 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9948                                         const struct comp_unit_head *cu_header,
9949                                         unsigned int *bytes_read,
9950                                         unsigned int *offset_size)
9951 {
9952   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9953
9954   gdb_assert (cu_header->initial_length_size == 4
9955               || cu_header->initial_length_size == 8
9956               || cu_header->initial_length_size == 12);
9957
9958   if (cu_header->initial_length_size != *bytes_read)
9959     complaint (&symfile_complaints,
9960                _("intermixed 32-bit and 64-bit DWARF sections"));
9961
9962   *offset_size = (*bytes_read == 4) ? 4 : 8;
9963   return length;
9964 }
9965
9966 /* Read an offset from the data stream.  The size of the offset is
9967    given by cu_header->offset_size.  */
9968
9969 static LONGEST
9970 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9971              unsigned int *bytes_read)
9972 {
9973   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9974
9975   *bytes_read = cu_header->offset_size;
9976   return offset;
9977 }
9978
9979 /* Read an offset from the data stream.  */
9980
9981 static LONGEST
9982 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9983 {
9984   LONGEST retval = 0;
9985
9986   switch (offset_size)
9987     {
9988     case 4:
9989       retval = bfd_get_32 (abfd, buf);
9990       break;
9991     case 8:
9992       retval = bfd_get_64 (abfd, buf);
9993       break;
9994     default:
9995       internal_error (__FILE__, __LINE__,
9996                       _("read_offset_1: bad switch [in module %s]"),
9997                       bfd_get_filename (abfd));
9998     }
9999
10000   return retval;
10001 }
10002
10003 static gdb_byte *
10004 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10005 {
10006   /* If the size of a host char is 8 bits, we can return a pointer
10007      to the buffer, otherwise we have to copy the data to a buffer
10008      allocated on the temporary obstack.  */
10009   gdb_assert (HOST_CHAR_BIT == 8);
10010   return buf;
10011 }
10012
10013 static char *
10014 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10015 {
10016   /* If the size of a host char is 8 bits, we can return a pointer
10017      to the string, otherwise we have to copy the string to a buffer
10018      allocated on the temporary obstack.  */
10019   gdb_assert (HOST_CHAR_BIT == 8);
10020   if (*buf == '\0')
10021     {
10022       *bytes_read_ptr = 1;
10023       return NULL;
10024     }
10025   *bytes_read_ptr = strlen ((char *) buf) + 1;
10026   return (char *) buf;
10027 }
10028
10029 static char *
10030 read_indirect_string (bfd *abfd, gdb_byte *buf,
10031                       const struct comp_unit_head *cu_header,
10032                       unsigned int *bytes_read_ptr)
10033 {
10034   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10035
10036   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10037   if (dwarf2_per_objfile->str.buffer == NULL)
10038     {
10039       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10040                       bfd_get_filename (abfd));
10041       return NULL;
10042     }
10043   if (str_offset >= dwarf2_per_objfile->str.size)
10044     {
10045       error (_("DW_FORM_strp pointing outside of "
10046                ".debug_str section [in module %s]"),
10047              bfd_get_filename (abfd));
10048       return NULL;
10049     }
10050   gdb_assert (HOST_CHAR_BIT == 8);
10051   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10052     return NULL;
10053   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10054 }
10055
10056 static unsigned long
10057 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10058 {
10059   unsigned long result;
10060   unsigned int num_read;
10061   int i, shift;
10062   unsigned char byte;
10063
10064   result = 0;
10065   shift = 0;
10066   num_read = 0;
10067   i = 0;
10068   while (1)
10069     {
10070       byte = bfd_get_8 (abfd, buf);
10071       buf++;
10072       num_read++;
10073       result |= ((unsigned long)(byte & 127) << shift);
10074       if ((byte & 128) == 0)
10075         {
10076           break;
10077         }
10078       shift += 7;
10079     }
10080   *bytes_read_ptr = num_read;
10081   return result;
10082 }
10083
10084 static long
10085 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10086 {
10087   long result;
10088   int i, shift, num_read;
10089   unsigned char byte;
10090
10091   result = 0;
10092   shift = 0;
10093   num_read = 0;
10094   i = 0;
10095   while (1)
10096     {
10097       byte = bfd_get_8 (abfd, buf);
10098       buf++;
10099       num_read++;
10100       result |= ((long)(byte & 127) << shift);
10101       shift += 7;
10102       if ((byte & 128) == 0)
10103         {
10104           break;
10105         }
10106     }
10107   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10108     result |= -(((long)1) << shift);
10109   *bytes_read_ptr = num_read;
10110   return result;
10111 }
10112
10113 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10114
10115 static gdb_byte *
10116 skip_leb128 (bfd *abfd, gdb_byte *buf)
10117 {
10118   int byte;
10119
10120   while (1)
10121     {
10122       byte = bfd_get_8 (abfd, buf);
10123       buf++;
10124       if ((byte & 128) == 0)
10125         return buf;
10126     }
10127 }
10128
10129 static void
10130 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10131 {
10132   switch (lang)
10133     {
10134     case DW_LANG_C89:
10135     case DW_LANG_C99:
10136     case DW_LANG_C:
10137       cu->language = language_c;
10138       break;
10139     case DW_LANG_C_plus_plus:
10140       cu->language = language_cplus;
10141       break;
10142     case DW_LANG_D:
10143       cu->language = language_d;
10144       break;
10145     case DW_LANG_Fortran77:
10146     case DW_LANG_Fortran90:
10147     case DW_LANG_Fortran95:
10148       cu->language = language_fortran;
10149       break;
10150     case DW_LANG_Mips_Assembler:
10151       cu->language = language_asm;
10152       break;
10153     case DW_LANG_Java:
10154       cu->language = language_java;
10155       break;
10156     case DW_LANG_Ada83:
10157     case DW_LANG_Ada95:
10158       cu->language = language_ada;
10159       break;
10160     case DW_LANG_Modula2:
10161       cu->language = language_m2;
10162       break;
10163     case DW_LANG_Pascal83:
10164       cu->language = language_pascal;
10165       break;
10166     case DW_LANG_ObjC:
10167       cu->language = language_objc;
10168       break;
10169     case DW_LANG_Cobol74:
10170     case DW_LANG_Cobol85:
10171     default:
10172       cu->language = language_minimal;
10173       break;
10174     }
10175   cu->language_defn = language_def (cu->language);
10176 }
10177
10178 /* Return the named attribute or NULL if not there.  */
10179
10180 static struct attribute *
10181 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10182 {
10183   unsigned int i;
10184   struct attribute *spec = NULL;
10185
10186   for (i = 0; i < die->num_attrs; ++i)
10187     {
10188       if (die->attrs[i].name == name)
10189         return &die->attrs[i];
10190       if (die->attrs[i].name == DW_AT_specification
10191           || die->attrs[i].name == DW_AT_abstract_origin)
10192         spec = &die->attrs[i];
10193     }
10194
10195   if (spec)
10196     {
10197       die = follow_die_ref (die, spec, &cu);
10198       return dwarf2_attr (die, name, cu);
10199     }
10200
10201   return NULL;
10202 }
10203
10204 /* Return the named attribute or NULL if not there,
10205    but do not follow DW_AT_specification, etc.
10206    This is for use in contexts where we're reading .debug_types dies.
10207    Following DW_AT_specification, DW_AT_abstract_origin will take us
10208    back up the chain, and we want to go down.  */
10209
10210 static struct attribute *
10211 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10212                        struct dwarf2_cu *cu)
10213 {
10214   unsigned int i;
10215
10216   for (i = 0; i < die->num_attrs; ++i)
10217     if (die->attrs[i].name == name)
10218       return &die->attrs[i];
10219
10220   return NULL;
10221 }
10222
10223 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10224    and holds a non-zero value.  This function should only be used for
10225    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10226
10227 static int
10228 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10229 {
10230   struct attribute *attr = dwarf2_attr (die, name, cu);
10231
10232   return (attr && DW_UNSND (attr));
10233 }
10234
10235 static int
10236 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10237 {
10238   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10239      which value is non-zero.  However, we have to be careful with
10240      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10241      (via dwarf2_flag_true_p) follows this attribute.  So we may
10242      end up accidently finding a declaration attribute that belongs
10243      to a different DIE referenced by the specification attribute,
10244      even though the given DIE does not have a declaration attribute.  */
10245   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10246           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10247 }
10248
10249 /* Return the die giving the specification for DIE, if there is
10250    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10251    containing the return value on output.  If there is no
10252    specification, but there is an abstract origin, that is
10253    returned.  */
10254
10255 static struct die_info *
10256 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10257 {
10258   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10259                                              *spec_cu);
10260
10261   if (spec_attr == NULL)
10262     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10263
10264   if (spec_attr == NULL)
10265     return NULL;
10266   else
10267     return follow_die_ref (die, spec_attr, spec_cu);
10268 }
10269
10270 /* Free the line_header structure *LH, and any arrays and strings it
10271    refers to.
10272    NOTE: This is also used as a "cleanup" function.  */
10273
10274 static void
10275 free_line_header (struct line_header *lh)
10276 {
10277   if (lh->standard_opcode_lengths)
10278     xfree (lh->standard_opcode_lengths);
10279
10280   /* Remember that all the lh->file_names[i].name pointers are
10281      pointers into debug_line_buffer, and don't need to be freed.  */
10282   if (lh->file_names)
10283     xfree (lh->file_names);
10284
10285   /* Similarly for the include directory names.  */
10286   if (lh->include_dirs)
10287     xfree (lh->include_dirs);
10288
10289   xfree (lh);
10290 }
10291
10292 /* Add an entry to LH's include directory table.  */
10293
10294 static void
10295 add_include_dir (struct line_header *lh, char *include_dir)
10296 {
10297   /* Grow the array if necessary.  */
10298   if (lh->include_dirs_size == 0)
10299     {
10300       lh->include_dirs_size = 1; /* for testing */
10301       lh->include_dirs = xmalloc (lh->include_dirs_size
10302                                   * sizeof (*lh->include_dirs));
10303     }
10304   else if (lh->num_include_dirs >= lh->include_dirs_size)
10305     {
10306       lh->include_dirs_size *= 2;
10307       lh->include_dirs = xrealloc (lh->include_dirs,
10308                                    (lh->include_dirs_size
10309                                     * sizeof (*lh->include_dirs)));
10310     }
10311
10312   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10313 }
10314
10315 /* Add an entry to LH's file name table.  */
10316
10317 static void
10318 add_file_name (struct line_header *lh,
10319                char *name,
10320                unsigned int dir_index,
10321                unsigned int mod_time,
10322                unsigned int length)
10323 {
10324   struct file_entry *fe;
10325
10326   /* Grow the array if necessary.  */
10327   if (lh->file_names_size == 0)
10328     {
10329       lh->file_names_size = 1; /* for testing */
10330       lh->file_names = xmalloc (lh->file_names_size
10331                                 * sizeof (*lh->file_names));
10332     }
10333   else if (lh->num_file_names >= lh->file_names_size)
10334     {
10335       lh->file_names_size *= 2;
10336       lh->file_names = xrealloc (lh->file_names,
10337                                  (lh->file_names_size
10338                                   * sizeof (*lh->file_names)));
10339     }
10340
10341   fe = &lh->file_names[lh->num_file_names++];
10342   fe->name = name;
10343   fe->dir_index = dir_index;
10344   fe->mod_time = mod_time;
10345   fe->length = length;
10346   fe->included_p = 0;
10347   fe->symtab = NULL;
10348 }
10349
10350 /* Read the statement program header starting at OFFSET in
10351    .debug_line, according to the endianness of ABFD.  Return a pointer
10352    to a struct line_header, allocated using xmalloc.
10353
10354    NOTE: the strings in the include directory and file name tables of
10355    the returned object point into debug_line_buffer, and must not be
10356    freed.  */
10357
10358 static struct line_header *
10359 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10360                           struct dwarf2_cu *cu)
10361 {
10362   struct cleanup *back_to;
10363   struct line_header *lh;
10364   gdb_byte *line_ptr;
10365   unsigned int bytes_read, offset_size;
10366   int i;
10367   char *cur_dir, *cur_file;
10368
10369   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10370   if (dwarf2_per_objfile->line.buffer == NULL)
10371     {
10372       complaint (&symfile_complaints, _("missing .debug_line section"));
10373       return 0;
10374     }
10375
10376   /* Make sure that at least there's room for the total_length field.
10377      That could be 12 bytes long, but we're just going to fudge that.  */
10378   if (offset + 4 >= dwarf2_per_objfile->line.size)
10379     {
10380       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10381       return 0;
10382     }
10383
10384   lh = xmalloc (sizeof (*lh));
10385   memset (lh, 0, sizeof (*lh));
10386   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10387                           (void *) lh);
10388
10389   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10390
10391   /* Read in the header.  */
10392   lh->total_length =
10393     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10394                                             &bytes_read, &offset_size);
10395   line_ptr += bytes_read;
10396   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10397                                      + dwarf2_per_objfile->line.size))
10398     {
10399       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10400       return 0;
10401     }
10402   lh->statement_program_end = line_ptr + lh->total_length;
10403   lh->version = read_2_bytes (abfd, line_ptr);
10404   line_ptr += 2;
10405   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10406   line_ptr += offset_size;
10407   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10408   line_ptr += 1;
10409   if (lh->version >= 4)
10410     {
10411       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10412       line_ptr += 1;
10413     }
10414   else
10415     lh->maximum_ops_per_instruction = 1;
10416
10417   if (lh->maximum_ops_per_instruction == 0)
10418     {
10419       lh->maximum_ops_per_instruction = 1;
10420       complaint (&symfile_complaints,
10421                  _("invalid maximum_ops_per_instruction "
10422                    "in `.debug_line' section"));
10423     }
10424
10425   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10426   line_ptr += 1;
10427   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10428   line_ptr += 1;
10429   lh->line_range = read_1_byte (abfd, line_ptr);
10430   line_ptr += 1;
10431   lh->opcode_base = read_1_byte (abfd, line_ptr);
10432   line_ptr += 1;
10433   lh->standard_opcode_lengths
10434     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10435
10436   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10437   for (i = 1; i < lh->opcode_base; ++i)
10438     {
10439       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10440       line_ptr += 1;
10441     }
10442
10443   /* Read directory table.  */
10444   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10445     {
10446       line_ptr += bytes_read;
10447       add_include_dir (lh, cur_dir);
10448     }
10449   line_ptr += bytes_read;
10450
10451   /* Read file name table.  */
10452   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10453     {
10454       unsigned int dir_index, mod_time, length;
10455
10456       line_ptr += bytes_read;
10457       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10458       line_ptr += bytes_read;
10459       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10460       line_ptr += bytes_read;
10461       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10462       line_ptr += bytes_read;
10463
10464       add_file_name (lh, cur_file, dir_index, mod_time, length);
10465     }
10466   line_ptr += bytes_read;
10467   lh->statement_program_start = line_ptr;
10468
10469   if (line_ptr > (dwarf2_per_objfile->line.buffer
10470                   + dwarf2_per_objfile->line.size))
10471     complaint (&symfile_complaints,
10472                _("line number info header doesn't "
10473                  "fit in `.debug_line' section"));
10474
10475   discard_cleanups (back_to);
10476   return lh;
10477 }
10478
10479 /* This function exists to work around a bug in certain compilers
10480    (particularly GCC 2.95), in which the first line number marker of a
10481    function does not show up until after the prologue, right before
10482    the second line number marker.  This function shifts ADDRESS down
10483    to the beginning of the function if necessary, and is called on
10484    addresses passed to record_line.  */
10485
10486 static CORE_ADDR
10487 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10488 {
10489   struct function_range *fn;
10490
10491   /* Find the function_range containing address.  */
10492   if (!cu->first_fn)
10493     return address;
10494
10495   if (!cu->cached_fn)
10496     cu->cached_fn = cu->first_fn;
10497
10498   fn = cu->cached_fn;
10499   while (fn)
10500     if (fn->lowpc <= address && fn->highpc > address)
10501       goto found;
10502     else
10503       fn = fn->next;
10504
10505   fn = cu->first_fn;
10506   while (fn && fn != cu->cached_fn)
10507     if (fn->lowpc <= address && fn->highpc > address)
10508       goto found;
10509     else
10510       fn = fn->next;
10511
10512   return address;
10513
10514  found:
10515   if (fn->seen_line)
10516     return address;
10517   if (address != fn->lowpc)
10518     complaint (&symfile_complaints,
10519                _("misplaced first line number at 0x%lx for '%s'"),
10520                (unsigned long) address, fn->name);
10521   fn->seen_line = 1;
10522   return fn->lowpc;
10523 }
10524
10525 /* Subroutine of dwarf_decode_lines to simplify it.
10526    Return the file name of the psymtab for included file FILE_INDEX
10527    in line header LH of PST.
10528    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10529    If space for the result is malloc'd, it will be freed by a cleanup.
10530    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10531
10532 static char *
10533 psymtab_include_file_name (const struct line_header *lh, int file_index,
10534                            const struct partial_symtab *pst,
10535                            const char *comp_dir)
10536 {
10537   const struct file_entry fe = lh->file_names [file_index];
10538   char *include_name = fe.name;
10539   char *include_name_to_compare = include_name;
10540   char *dir_name = NULL;
10541   const char *pst_filename;
10542   char *copied_name = NULL;
10543   int file_is_pst;
10544
10545   if (fe.dir_index)
10546     dir_name = lh->include_dirs[fe.dir_index - 1];
10547
10548   if (!IS_ABSOLUTE_PATH (include_name)
10549       && (dir_name != NULL || comp_dir != NULL))
10550     {
10551       /* Avoid creating a duplicate psymtab for PST.
10552          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10553          Before we do the comparison, however, we need to account
10554          for DIR_NAME and COMP_DIR.
10555          First prepend dir_name (if non-NULL).  If we still don't
10556          have an absolute path prepend comp_dir (if non-NULL).
10557          However, the directory we record in the include-file's
10558          psymtab does not contain COMP_DIR (to match the
10559          corresponding symtab(s)).
10560
10561          Example:
10562
10563          bash$ cd /tmp
10564          bash$ gcc -g ./hello.c
10565          include_name = "hello.c"
10566          dir_name = "."
10567          DW_AT_comp_dir = comp_dir = "/tmp"
10568          DW_AT_name = "./hello.c"  */
10569
10570       if (dir_name != NULL)
10571         {
10572           include_name = concat (dir_name, SLASH_STRING,
10573                                  include_name, (char *)NULL);
10574           include_name_to_compare = include_name;
10575           make_cleanup (xfree, include_name);
10576         }
10577       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10578         {
10579           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10580                                             include_name, (char *)NULL);
10581         }
10582     }
10583
10584   pst_filename = pst->filename;
10585   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10586     {
10587       copied_name = concat (pst->dirname, SLASH_STRING,
10588                             pst_filename, (char *)NULL);
10589       pst_filename = copied_name;
10590     }
10591
10592   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10593
10594   if (include_name_to_compare != include_name)
10595     xfree (include_name_to_compare);
10596   if (copied_name != NULL)
10597     xfree (copied_name);
10598
10599   if (file_is_pst)
10600     return NULL;
10601   return include_name;
10602 }
10603
10604 /* Ignore this record_line request.  */
10605
10606 static void
10607 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10608 {
10609   return;
10610 }
10611
10612 /* Decode the Line Number Program (LNP) for the given line_header
10613    structure and CU.  The actual information extracted and the type
10614    of structures created from the LNP depends on the value of PST.
10615
10616    1. If PST is NULL, then this procedure uses the data from the program
10617       to create all necessary symbol tables, and their linetables.
10618
10619    2. If PST is not NULL, this procedure reads the program to determine
10620       the list of files included by the unit represented by PST, and
10621       builds all the associated partial symbol tables.
10622
10623    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10624    It is used for relative paths in the line table.
10625    NOTE: When processing partial symtabs (pst != NULL),
10626    comp_dir == pst->dirname.
10627
10628    NOTE: It is important that psymtabs have the same file name (via strcmp)
10629    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10630    symtab we don't use it in the name of the psymtabs we create.
10631    E.g. expand_line_sal requires this when finding psymtabs to expand.
10632    A good testcase for this is mb-inline.exp.  */
10633
10634 static void
10635 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10636                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10637 {
10638   gdb_byte *line_ptr, *extended_end;
10639   gdb_byte *line_end;
10640   unsigned int bytes_read, extended_len;
10641   unsigned char op_code, extended_op, adj_opcode;
10642   CORE_ADDR baseaddr;
10643   struct objfile *objfile = cu->objfile;
10644   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10645   const int decode_for_pst_p = (pst != NULL);
10646   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10647   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10648     = record_line;
10649
10650   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10651
10652   line_ptr = lh->statement_program_start;
10653   line_end = lh->statement_program_end;
10654
10655   /* Read the statement sequences until there's nothing left.  */
10656   while (line_ptr < line_end)
10657     {
10658       /* state machine registers  */
10659       CORE_ADDR address = 0;
10660       unsigned int file = 1;
10661       unsigned int line = 1;
10662       unsigned int column = 0;
10663       int is_stmt = lh->default_is_stmt;
10664       int basic_block = 0;
10665       int end_sequence = 0;
10666       CORE_ADDR addr;
10667       unsigned char op_index = 0;
10668
10669       if (!decode_for_pst_p && lh->num_file_names >= file)
10670         {
10671           /* Start a subfile for the current file of the state machine.  */
10672           /* lh->include_dirs and lh->file_names are 0-based, but the
10673              directory and file name numbers in the statement program
10674              are 1-based.  */
10675           struct file_entry *fe = &lh->file_names[file - 1];
10676           char *dir = NULL;
10677
10678           if (fe->dir_index)
10679             dir = lh->include_dirs[fe->dir_index - 1];
10680
10681           dwarf2_start_subfile (fe->name, dir, comp_dir);
10682         }
10683
10684       /* Decode the table.  */
10685       while (!end_sequence)
10686         {
10687           op_code = read_1_byte (abfd, line_ptr);
10688           line_ptr += 1;
10689           if (line_ptr > line_end)
10690             {
10691               dwarf2_debug_line_missing_end_sequence_complaint ();
10692               break;
10693             }
10694
10695           if (op_code >= lh->opcode_base)
10696             {
10697               /* Special operand.  */
10698               adj_opcode = op_code - lh->opcode_base;
10699               address += (((op_index + (adj_opcode / lh->line_range))
10700                            / lh->maximum_ops_per_instruction)
10701                           * lh->minimum_instruction_length);
10702               op_index = ((op_index + (adj_opcode / lh->line_range))
10703                           % lh->maximum_ops_per_instruction);
10704               line += lh->line_base + (adj_opcode % lh->line_range);
10705               if (lh->num_file_names < file || file == 0)
10706                 dwarf2_debug_line_missing_file_complaint ();
10707               /* For now we ignore lines not starting on an
10708                  instruction boundary.  */
10709               else if (op_index == 0)
10710                 {
10711                   lh->file_names[file - 1].included_p = 1;
10712                   if (!decode_for_pst_p && is_stmt)
10713                     {
10714                       if (last_subfile != current_subfile)
10715                         {
10716                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10717                           if (last_subfile)
10718                             (*p_record_line) (last_subfile, 0, addr);
10719                           last_subfile = current_subfile;
10720                         }
10721                       /* Append row to matrix using current values.  */
10722                       addr = check_cu_functions (address, cu);
10723                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10724                       (*p_record_line) (current_subfile, line, addr);
10725                     }
10726                 }
10727               basic_block = 0;
10728             }
10729           else switch (op_code)
10730             {
10731             case DW_LNS_extended_op:
10732               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10733                                                    &bytes_read);
10734               line_ptr += bytes_read;
10735               extended_end = line_ptr + extended_len;
10736               extended_op = read_1_byte (abfd, line_ptr);
10737               line_ptr += 1;
10738               switch (extended_op)
10739                 {
10740                 case DW_LNE_end_sequence:
10741                   p_record_line = record_line;
10742                   end_sequence = 1;
10743                   break;
10744                 case DW_LNE_set_address:
10745                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10746
10747                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10748                     {
10749                       /* This line table is for a function which has been
10750                          GCd by the linker.  Ignore it.  PR gdb/12528 */
10751
10752                       long line_offset
10753                         = line_ptr - dwarf2_per_objfile->line.buffer;
10754
10755                       complaint (&symfile_complaints,
10756                                  _(".debug_line address at offset 0x%lx is 0 "
10757                                    "[in module %s]"),
10758                                  line_offset, cu->objfile->name);
10759                       p_record_line = noop_record_line;
10760                     }
10761
10762                   op_index = 0;
10763                   line_ptr += bytes_read;
10764                   address += baseaddr;
10765                   break;
10766                 case DW_LNE_define_file:
10767                   {
10768                     char *cur_file;
10769                     unsigned int dir_index, mod_time, length;
10770
10771                     cur_file = read_direct_string (abfd, line_ptr,
10772                                                    &bytes_read);
10773                     line_ptr += bytes_read;
10774                     dir_index =
10775                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10776                     line_ptr += bytes_read;
10777                     mod_time =
10778                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10779                     line_ptr += bytes_read;
10780                     length =
10781                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10782                     line_ptr += bytes_read;
10783                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10784                   }
10785                   break;
10786                 case DW_LNE_set_discriminator:
10787                   /* The discriminator is not interesting to the debugger;
10788                      just ignore it.  */
10789                   line_ptr = extended_end;
10790                   break;
10791                 default:
10792                   complaint (&symfile_complaints,
10793                              _("mangled .debug_line section"));
10794                   return;
10795                 }
10796               /* Make sure that we parsed the extended op correctly.  If e.g.
10797                  we expected a different address size than the producer used,
10798                  we may have read the wrong number of bytes.  */
10799               if (line_ptr != extended_end)
10800                 {
10801                   complaint (&symfile_complaints,
10802                              _("mangled .debug_line section"));
10803                   return;
10804                 }
10805               break;
10806             case DW_LNS_copy:
10807               if (lh->num_file_names < file || file == 0)
10808                 dwarf2_debug_line_missing_file_complaint ();
10809               else
10810                 {
10811                   lh->file_names[file - 1].included_p = 1;
10812                   if (!decode_for_pst_p && is_stmt)
10813                     {
10814                       if (last_subfile != current_subfile)
10815                         {
10816                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10817                           if (last_subfile)
10818                             (*p_record_line) (last_subfile, 0, addr);
10819                           last_subfile = current_subfile;
10820                         }
10821                       addr = check_cu_functions (address, cu);
10822                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10823                       (*p_record_line) (current_subfile, line, addr);
10824                     }
10825                 }
10826               basic_block = 0;
10827               break;
10828             case DW_LNS_advance_pc:
10829               {
10830                 CORE_ADDR adjust
10831                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10832
10833                 address += (((op_index + adjust)
10834                              / lh->maximum_ops_per_instruction)
10835                             * lh->minimum_instruction_length);
10836                 op_index = ((op_index + adjust)
10837                             % lh->maximum_ops_per_instruction);
10838                 line_ptr += bytes_read;
10839               }
10840               break;
10841             case DW_LNS_advance_line:
10842               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10843               line_ptr += bytes_read;
10844               break;
10845             case DW_LNS_set_file:
10846               {
10847                 /* The arrays lh->include_dirs and lh->file_names are
10848                    0-based, but the directory and file name numbers in
10849                    the statement program are 1-based.  */
10850                 struct file_entry *fe;
10851                 char *dir = NULL;
10852
10853                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10854                 line_ptr += bytes_read;
10855                 if (lh->num_file_names < file || file == 0)
10856                   dwarf2_debug_line_missing_file_complaint ();
10857                 else
10858                   {
10859                     fe = &lh->file_names[file - 1];
10860                     if (fe->dir_index)
10861                       dir = lh->include_dirs[fe->dir_index - 1];
10862                     if (!decode_for_pst_p)
10863                       {
10864                         last_subfile = current_subfile;
10865                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10866                       }
10867                   }
10868               }
10869               break;
10870             case DW_LNS_set_column:
10871               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10872               line_ptr += bytes_read;
10873               break;
10874             case DW_LNS_negate_stmt:
10875               is_stmt = (!is_stmt);
10876               break;
10877             case DW_LNS_set_basic_block:
10878               basic_block = 1;
10879               break;
10880             /* Add to the address register of the state machine the
10881                address increment value corresponding to special opcode
10882                255.  I.e., this value is scaled by the minimum
10883                instruction length since special opcode 255 would have
10884                scaled the increment.  */
10885             case DW_LNS_const_add_pc:
10886               {
10887                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10888
10889                 address += (((op_index + adjust)
10890                              / lh->maximum_ops_per_instruction)
10891                             * lh->minimum_instruction_length);
10892                 op_index = ((op_index + adjust)
10893                             % lh->maximum_ops_per_instruction);
10894               }
10895               break;
10896             case DW_LNS_fixed_advance_pc:
10897               address += read_2_bytes (abfd, line_ptr);
10898               op_index = 0;
10899               line_ptr += 2;
10900               break;
10901             default:
10902               {
10903                 /* Unknown standard opcode, ignore it.  */
10904                 int i;
10905
10906                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10907                   {
10908                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10909                     line_ptr += bytes_read;
10910                   }
10911               }
10912             }
10913         }
10914       if (lh->num_file_names < file || file == 0)
10915         dwarf2_debug_line_missing_file_complaint ();
10916       else
10917         {
10918           lh->file_names[file - 1].included_p = 1;
10919           if (!decode_for_pst_p)
10920             {
10921               addr = gdbarch_addr_bits_remove (gdbarch, address);
10922               (*p_record_line) (current_subfile, 0, addr);
10923             }
10924         }
10925     }
10926
10927   if (decode_for_pst_p)
10928     {
10929       int file_index;
10930
10931       /* Now that we're done scanning the Line Header Program, we can
10932          create the psymtab of each included file.  */
10933       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10934         if (lh->file_names[file_index].included_p == 1)
10935           {
10936             char *include_name =
10937               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10938             if (include_name != NULL)
10939               dwarf2_create_include_psymtab (include_name, pst, objfile);
10940           }
10941     }
10942   else
10943     {
10944       /* Make sure a symtab is created for every file, even files
10945          which contain only variables (i.e. no code with associated
10946          line numbers).  */
10947
10948       int i;
10949       struct file_entry *fe;
10950
10951       for (i = 0; i < lh->num_file_names; i++)
10952         {
10953           char *dir = NULL;
10954
10955           fe = &lh->file_names[i];
10956           if (fe->dir_index)
10957             dir = lh->include_dirs[fe->dir_index - 1];
10958           dwarf2_start_subfile (fe->name, dir, comp_dir);
10959
10960           /* Skip the main file; we don't need it, and it must be
10961              allocated last, so that it will show up before the
10962              non-primary symtabs in the objfile's symtab list.  */
10963           if (current_subfile == first_subfile)
10964             continue;
10965
10966           if (current_subfile->symtab == NULL)
10967             current_subfile->symtab = allocate_symtab (current_subfile->name,
10968                                                        cu->objfile);
10969           fe->symtab = current_subfile->symtab;
10970         }
10971     }
10972 }
10973
10974 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10975    DIRNAME the name of the source directory which contains FILENAME
10976    or NULL if not known.  COMP_DIR is the compilation directory for the
10977    linetable's compilation unit or NULL if not known.
10978    This routine tries to keep line numbers from identical absolute and
10979    relative file names in a common subfile.
10980
10981    Using the `list' example from the GDB testsuite, which resides in
10982    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10983    of /srcdir/list0.c yields the following debugging information for list0.c:
10984
10985    DW_AT_name:          /srcdir/list0.c
10986    DW_AT_comp_dir:              /compdir
10987    files.files[0].name: list0.h
10988    files.files[0].dir:  /srcdir
10989    files.files[1].name: list0.c
10990    files.files[1].dir:  /srcdir
10991
10992    The line number information for list0.c has to end up in a single
10993    subfile, so that `break /srcdir/list0.c:1' works as expected.
10994    start_subfile will ensure that this happens provided that we pass the
10995    concatenation of files.files[1].dir and files.files[1].name as the
10996    subfile's name.  */
10997
10998 static void
10999 dwarf2_start_subfile (char *filename, const char *dirname,
11000                       const char *comp_dir)
11001 {
11002   char *fullname;
11003
11004   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11005      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11006      second argument to start_subfile.  To be consistent, we do the
11007      same here.  In order not to lose the line information directory,
11008      we concatenate it to the filename when it makes sense.
11009      Note that the Dwarf3 standard says (speaking of filenames in line
11010      information): ``The directory index is ignored for file names
11011      that represent full path names''.  Thus ignoring dirname in the
11012      `else' branch below isn't an issue.  */
11013
11014   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11015     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11016   else
11017     fullname = filename;
11018
11019   start_subfile (fullname, comp_dir);
11020
11021   if (fullname != filename)
11022     xfree (fullname);
11023 }
11024
11025 static void
11026 var_decode_location (struct attribute *attr, struct symbol *sym,
11027                      struct dwarf2_cu *cu)
11028 {
11029   struct objfile *objfile = cu->objfile;
11030   struct comp_unit_head *cu_header = &cu->header;
11031
11032   /* NOTE drow/2003-01-30: There used to be a comment and some special
11033      code here to turn a symbol with DW_AT_external and a
11034      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11035      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11036      with some versions of binutils) where shared libraries could have
11037      relocations against symbols in their debug information - the
11038      minimal symbol would have the right address, but the debug info
11039      would not.  It's no longer necessary, because we will explicitly
11040      apply relocations when we read in the debug information now.  */
11041
11042   /* A DW_AT_location attribute with no contents indicates that a
11043      variable has been optimized away.  */
11044   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11045     {
11046       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11047       return;
11048     }
11049
11050   /* Handle one degenerate form of location expression specially, to
11051      preserve GDB's previous behavior when section offsets are
11052      specified.  If this is just a DW_OP_addr then mark this symbol
11053      as LOC_STATIC.  */
11054
11055   if (attr_form_is_block (attr)
11056       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11057       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11058     {
11059       unsigned int dummy;
11060
11061       SYMBOL_VALUE_ADDRESS (sym) =
11062         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11063       SYMBOL_CLASS (sym) = LOC_STATIC;
11064       fixup_symbol_section (sym, objfile);
11065       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11066                                               SYMBOL_SECTION (sym));
11067       return;
11068     }
11069
11070   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11071      expression evaluator, and use LOC_COMPUTED only when necessary
11072      (i.e. when the value of a register or memory location is
11073      referenced, or a thread-local block, etc.).  Then again, it might
11074      not be worthwhile.  I'm assuming that it isn't unless performance
11075      or memory numbers show me otherwise.  */
11076
11077   dwarf2_symbol_mark_computed (attr, sym, cu);
11078   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11079
11080   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11081     cu->has_loclist = 1;
11082 }
11083
11084 /* Given a pointer to a DWARF information entry, figure out if we need
11085    to make a symbol table entry for it, and if so, create a new entry
11086    and return a pointer to it.
11087    If TYPE is NULL, determine symbol type from the die, otherwise
11088    used the passed type.
11089    If SPACE is not NULL, use it to hold the new symbol.  If it is
11090    NULL, allocate a new symbol on the objfile's obstack.  */
11091
11092 static struct symbol *
11093 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11094                  struct symbol *space)
11095 {
11096   struct objfile *objfile = cu->objfile;
11097   struct symbol *sym = NULL;
11098   char *name;
11099   struct attribute *attr = NULL;
11100   struct attribute *attr2 = NULL;
11101   CORE_ADDR baseaddr;
11102   struct pending **list_to_add = NULL;
11103
11104   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11105
11106   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11107
11108   name = dwarf2_name (die, cu);
11109   if (name)
11110     {
11111       const char *linkagename;
11112       int suppress_add = 0;
11113
11114       if (space)
11115         sym = space;
11116       else
11117         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11118       OBJSTAT (objfile, n_syms++);
11119
11120       /* Cache this symbol's name and the name's demangled form (if any).  */
11121       SYMBOL_SET_LANGUAGE (sym, cu->language);
11122       linkagename = dwarf2_physname (name, die, cu);
11123       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11124
11125       /* Fortran does not have mangling standard and the mangling does differ
11126          between gfortran, iFort etc.  */
11127       if (cu->language == language_fortran
11128           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11129         symbol_set_demangled_name (&(sym->ginfo),
11130                                    (char *) dwarf2_full_name (name, die, cu),
11131                                    NULL);
11132
11133       /* Default assumptions.
11134          Use the passed type or decode it from the die.  */
11135       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11136       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11137       if (type != NULL)
11138         SYMBOL_TYPE (sym) = type;
11139       else
11140         SYMBOL_TYPE (sym) = die_type (die, cu);
11141       attr = dwarf2_attr (die,
11142                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11143                           cu);
11144       if (attr)
11145         {
11146           SYMBOL_LINE (sym) = DW_UNSND (attr);
11147         }
11148
11149       attr = dwarf2_attr (die,
11150                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11151                           cu);
11152       if (attr)
11153         {
11154           int file_index = DW_UNSND (attr);
11155
11156           if (cu->line_header == NULL
11157               || file_index > cu->line_header->num_file_names)
11158             complaint (&symfile_complaints,
11159                        _("file index out of range"));
11160           else if (file_index > 0)
11161             {
11162               struct file_entry *fe;
11163
11164               fe = &cu->line_header->file_names[file_index - 1];
11165               SYMBOL_SYMTAB (sym) = fe->symtab;
11166             }
11167         }
11168
11169       switch (die->tag)
11170         {
11171         case DW_TAG_label:
11172           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11173           if (attr)
11174             {
11175               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11176             }
11177           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11178           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11179           SYMBOL_CLASS (sym) = LOC_LABEL;
11180           add_symbol_to_list (sym, cu->list_in_scope);
11181           break;
11182         case DW_TAG_subprogram:
11183           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11184              finish_block.  */
11185           SYMBOL_CLASS (sym) = LOC_BLOCK;
11186           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11187           if ((attr2 && (DW_UNSND (attr2) != 0))
11188               || cu->language == language_ada)
11189             {
11190               /* Subprograms marked external are stored as a global symbol.
11191                  Ada subprograms, whether marked external or not, are always
11192                  stored as a global symbol, because we want to be able to
11193                  access them globally.  For instance, we want to be able
11194                  to break on a nested subprogram without having to
11195                  specify the context.  */
11196               list_to_add = &global_symbols;
11197             }
11198           else
11199             {
11200               list_to_add = cu->list_in_scope;
11201             }
11202           break;
11203         case DW_TAG_inlined_subroutine:
11204           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11205              finish_block.  */
11206           SYMBOL_CLASS (sym) = LOC_BLOCK;
11207           SYMBOL_INLINED (sym) = 1;
11208           /* Do not add the symbol to any lists.  It will be found via
11209              BLOCK_FUNCTION from the blockvector.  */
11210           break;
11211         case DW_TAG_template_value_param:
11212           suppress_add = 1;
11213           /* Fall through.  */
11214         case DW_TAG_constant:
11215         case DW_TAG_variable:
11216         case DW_TAG_member:
11217           /* Compilation with minimal debug info may result in
11218              variables with missing type entries.  Change the
11219              misleading `void' type to something sensible.  */
11220           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11221             SYMBOL_TYPE (sym)
11222               = objfile_type (objfile)->nodebug_data_symbol;
11223
11224           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11225           /* In the case of DW_TAG_member, we should only be called for
11226              static const members.  */
11227           if (die->tag == DW_TAG_member)
11228             {
11229               /* dwarf2_add_field uses die_is_declaration,
11230                  so we do the same.  */
11231               gdb_assert (die_is_declaration (die, cu));
11232               gdb_assert (attr);
11233             }
11234           if (attr)
11235             {
11236               dwarf2_const_value (attr, sym, cu);
11237               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11238               if (!suppress_add)
11239                 {
11240                   if (attr2 && (DW_UNSND (attr2) != 0))
11241                     list_to_add = &global_symbols;
11242                   else
11243                     list_to_add = cu->list_in_scope;
11244                 }
11245               break;
11246             }
11247           attr = dwarf2_attr (die, DW_AT_location, cu);
11248           if (attr)
11249             {
11250               var_decode_location (attr, sym, cu);
11251               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11252               if (SYMBOL_CLASS (sym) == LOC_STATIC
11253                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11254                   && !dwarf2_per_objfile->has_section_at_zero)
11255                 {
11256                   /* When a static variable is eliminated by the linker,
11257                      the corresponding debug information is not stripped
11258                      out, but the variable address is set to null;
11259                      do not add such variables into symbol table.  */
11260                 }
11261               else if (attr2 && (DW_UNSND (attr2) != 0))
11262                 {
11263                   /* Workaround gfortran PR debug/40040 - it uses
11264                      DW_AT_location for variables in -fPIC libraries which may
11265                      get overriden by other libraries/executable and get
11266                      a different address.  Resolve it by the minimal symbol
11267                      which may come from inferior's executable using copy
11268                      relocation.  Make this workaround only for gfortran as for
11269                      other compilers GDB cannot guess the minimal symbol
11270                      Fortran mangling kind.  */
11271                   if (cu->language == language_fortran && die->parent
11272                       && die->parent->tag == DW_TAG_module
11273                       && cu->producer
11274                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11275                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11276
11277                   /* A variable with DW_AT_external is never static,
11278                      but it may be block-scoped.  */
11279                   list_to_add = (cu->list_in_scope == &file_symbols
11280                                  ? &global_symbols : cu->list_in_scope);
11281                 }
11282               else
11283                 list_to_add = cu->list_in_scope;
11284             }
11285           else
11286             {
11287               /* We do not know the address of this symbol.
11288                  If it is an external symbol and we have type information
11289                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11290                  The address of the variable will then be determined from
11291                  the minimal symbol table whenever the variable is
11292                  referenced.  */
11293               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11294               if (attr2 && (DW_UNSND (attr2) != 0)
11295                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11296                 {
11297                   /* A variable with DW_AT_external is never static, but it
11298                      may be block-scoped.  */
11299                   list_to_add = (cu->list_in_scope == &file_symbols
11300                                  ? &global_symbols : cu->list_in_scope);
11301
11302                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11303                 }
11304               else if (!die_is_declaration (die, cu))
11305                 {
11306                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11307                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11308                   if (!suppress_add)
11309                     list_to_add = cu->list_in_scope;
11310                 }
11311             }
11312           break;
11313         case DW_TAG_formal_parameter:
11314           /* If we are inside a function, mark this as an argument.  If
11315              not, we might be looking at an argument to an inlined function
11316              when we do not have enough information to show inlined frames;
11317              pretend it's a local variable in that case so that the user can
11318              still see it.  */
11319           if (context_stack_depth > 0
11320               && context_stack[context_stack_depth - 1].name != NULL)
11321             SYMBOL_IS_ARGUMENT (sym) = 1;
11322           attr = dwarf2_attr (die, DW_AT_location, cu);
11323           if (attr)
11324             {
11325               var_decode_location (attr, sym, cu);
11326             }
11327           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11328           if (attr)
11329             {
11330               dwarf2_const_value (attr, sym, cu);
11331             }
11332           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11333           if (attr && DW_UNSND (attr))
11334             {
11335               struct type *ref_type;
11336
11337               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11338               SYMBOL_TYPE (sym) = ref_type;
11339             }
11340
11341           list_to_add = cu->list_in_scope;
11342           break;
11343         case DW_TAG_unspecified_parameters:
11344           /* From varargs functions; gdb doesn't seem to have any
11345              interest in this information, so just ignore it for now.
11346              (FIXME?) */
11347           break;
11348         case DW_TAG_template_type_param:
11349           suppress_add = 1;
11350           /* Fall through.  */
11351         case DW_TAG_class_type:
11352         case DW_TAG_interface_type:
11353         case DW_TAG_structure_type:
11354         case DW_TAG_union_type:
11355         case DW_TAG_set_type:
11356         case DW_TAG_enumeration_type:
11357           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11358           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11359
11360           {
11361             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11362                really ever be static objects: otherwise, if you try
11363                to, say, break of a class's method and you're in a file
11364                which doesn't mention that class, it won't work unless
11365                the check for all static symbols in lookup_symbol_aux
11366                saves you.  See the OtherFileClass tests in
11367                gdb.c++/namespace.exp.  */
11368
11369             if (!suppress_add)
11370               {
11371                 list_to_add = (cu->list_in_scope == &file_symbols
11372                                && (cu->language == language_cplus
11373                                    || cu->language == language_java)
11374                                ? &global_symbols : cu->list_in_scope);
11375
11376                 /* The semantics of C++ state that "struct foo {
11377                    ... }" also defines a typedef for "foo".  A Java
11378                    class declaration also defines a typedef for the
11379                    class.  */
11380                 if (cu->language == language_cplus
11381                     || cu->language == language_java
11382                     || cu->language == language_ada)
11383                   {
11384                     /* The symbol's name is already allocated along
11385                        with this objfile, so we don't need to
11386                        duplicate it for the type.  */
11387                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11388                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11389                   }
11390               }
11391           }
11392           break;
11393         case DW_TAG_typedef:
11394           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11395           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11396           list_to_add = cu->list_in_scope;
11397           break;
11398         case DW_TAG_base_type:
11399         case DW_TAG_subrange_type:
11400           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11401           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11402           list_to_add = cu->list_in_scope;
11403           break;
11404         case DW_TAG_enumerator:
11405           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11406           if (attr)
11407             {
11408               dwarf2_const_value (attr, sym, cu);
11409             }
11410           {
11411             /* NOTE: carlton/2003-11-10: See comment above in the
11412                DW_TAG_class_type, etc. block.  */
11413
11414             list_to_add = (cu->list_in_scope == &file_symbols
11415                            && (cu->language == language_cplus
11416                                || cu->language == language_java)
11417                            ? &global_symbols : cu->list_in_scope);
11418           }
11419           break;
11420         case DW_TAG_namespace:
11421           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11422           list_to_add = &global_symbols;
11423           break;
11424         default:
11425           /* Not a tag we recognize.  Hopefully we aren't processing
11426              trash data, but since we must specifically ignore things
11427              we don't recognize, there is nothing else we should do at
11428              this point.  */
11429           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11430                      dwarf_tag_name (die->tag));
11431           break;
11432         }
11433
11434       if (suppress_add)
11435         {
11436           sym->hash_next = objfile->template_symbols;
11437           objfile->template_symbols = sym;
11438           list_to_add = NULL;
11439         }
11440
11441       if (list_to_add != NULL)
11442         add_symbol_to_list (sym, list_to_add);
11443
11444       /* For the benefit of old versions of GCC, check for anonymous
11445          namespaces based on the demangled name.  */
11446       if (!processing_has_namespace_info
11447           && cu->language == language_cplus)
11448         cp_scan_for_anonymous_namespaces (sym);
11449     }
11450   return (sym);
11451 }
11452
11453 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11454
11455 static struct symbol *
11456 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11457 {
11458   return new_symbol_full (die, type, cu, NULL);
11459 }
11460
11461 /* Given an attr with a DW_FORM_dataN value in host byte order,
11462    zero-extend it as appropriate for the symbol's type.  The DWARF
11463    standard (v4) is not entirely clear about the meaning of using
11464    DW_FORM_dataN for a constant with a signed type, where the type is
11465    wider than the data.  The conclusion of a discussion on the DWARF
11466    list was that this is unspecified.  We choose to always zero-extend
11467    because that is the interpretation long in use by GCC.  */
11468
11469 static gdb_byte *
11470 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11471                          const char *name, struct obstack *obstack,
11472                          struct dwarf2_cu *cu, long *value, int bits)
11473 {
11474   struct objfile *objfile = cu->objfile;
11475   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11476                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11477   LONGEST l = DW_UNSND (attr);
11478
11479   if (bits < sizeof (*value) * 8)
11480     {
11481       l &= ((LONGEST) 1 << bits) - 1;
11482       *value = l;
11483     }
11484   else if (bits == sizeof (*value) * 8)
11485     *value = l;
11486   else
11487     {
11488       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11489       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11490       return bytes;
11491     }
11492
11493   return NULL;
11494 }
11495
11496 /* Read a constant value from an attribute.  Either set *VALUE, or if
11497    the value does not fit in *VALUE, set *BYTES - either already
11498    allocated on the objfile obstack, or newly allocated on OBSTACK,
11499    or, set *BATON, if we translated the constant to a location
11500    expression.  */
11501
11502 static void
11503 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11504                          const char *name, struct obstack *obstack,
11505                          struct dwarf2_cu *cu,
11506                          long *value, gdb_byte **bytes,
11507                          struct dwarf2_locexpr_baton **baton)
11508 {
11509   struct objfile *objfile = cu->objfile;
11510   struct comp_unit_head *cu_header = &cu->header;
11511   struct dwarf_block *blk;
11512   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11513                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11514
11515   *value = 0;
11516   *bytes = NULL;
11517   *baton = NULL;
11518
11519   switch (attr->form)
11520     {
11521     case DW_FORM_addr:
11522       {
11523         gdb_byte *data;
11524
11525         if (TYPE_LENGTH (type) != cu_header->addr_size)
11526           dwarf2_const_value_length_mismatch_complaint (name,
11527                                                         cu_header->addr_size,
11528                                                         TYPE_LENGTH (type));
11529         /* Symbols of this form are reasonably rare, so we just
11530            piggyback on the existing location code rather than writing
11531            a new implementation of symbol_computed_ops.  */
11532         *baton = obstack_alloc (&objfile->objfile_obstack,
11533                                 sizeof (struct dwarf2_locexpr_baton));
11534         (*baton)->per_cu = cu->per_cu;
11535         gdb_assert ((*baton)->per_cu);
11536
11537         (*baton)->size = 2 + cu_header->addr_size;
11538         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11539         (*baton)->data = data;
11540
11541         data[0] = DW_OP_addr;
11542         store_unsigned_integer (&data[1], cu_header->addr_size,
11543                                 byte_order, DW_ADDR (attr));
11544         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11545       }
11546       break;
11547     case DW_FORM_string:
11548     case DW_FORM_strp:
11549       /* DW_STRING is already allocated on the objfile obstack, point
11550          directly to it.  */
11551       *bytes = (gdb_byte *) DW_STRING (attr);
11552       break;
11553     case DW_FORM_block1:
11554     case DW_FORM_block2:
11555     case DW_FORM_block4:
11556     case DW_FORM_block:
11557     case DW_FORM_exprloc:
11558       blk = DW_BLOCK (attr);
11559       if (TYPE_LENGTH (type) != blk->size)
11560         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11561                                                       TYPE_LENGTH (type));
11562       *bytes = blk->data;
11563       break;
11564
11565       /* The DW_AT_const_value attributes are supposed to carry the
11566          symbol's value "represented as it would be on the target
11567          architecture."  By the time we get here, it's already been
11568          converted to host endianness, so we just need to sign- or
11569          zero-extend it as appropriate.  */
11570     case DW_FORM_data1:
11571       *bytes = dwarf2_const_value_data (attr, type, name,
11572                                         obstack, cu, value, 8);
11573       break;
11574     case DW_FORM_data2:
11575       *bytes = dwarf2_const_value_data (attr, type, name,
11576                                         obstack, cu, value, 16);
11577       break;
11578     case DW_FORM_data4:
11579       *bytes = dwarf2_const_value_data (attr, type, name,
11580                                         obstack, cu, value, 32);
11581       break;
11582     case DW_FORM_data8:
11583       *bytes = dwarf2_const_value_data (attr, type, name,
11584                                         obstack, cu, value, 64);
11585       break;
11586
11587     case DW_FORM_sdata:
11588       *value = DW_SND (attr);
11589       break;
11590
11591     case DW_FORM_udata:
11592       *value = DW_UNSND (attr);
11593       break;
11594
11595     default:
11596       complaint (&symfile_complaints,
11597                  _("unsupported const value attribute form: '%s'"),
11598                  dwarf_form_name (attr->form));
11599       *value = 0;
11600       break;
11601     }
11602 }
11603
11604
11605 /* Copy constant value from an attribute to a symbol.  */
11606
11607 static void
11608 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11609                     struct dwarf2_cu *cu)
11610 {
11611   struct objfile *objfile = cu->objfile;
11612   struct comp_unit_head *cu_header = &cu->header;
11613   long value;
11614   gdb_byte *bytes;
11615   struct dwarf2_locexpr_baton *baton;
11616
11617   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11618                            SYMBOL_PRINT_NAME (sym),
11619                            &objfile->objfile_obstack, cu,
11620                            &value, &bytes, &baton);
11621
11622   if (baton != NULL)
11623     {
11624       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11625       SYMBOL_LOCATION_BATON (sym) = baton;
11626       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11627     }
11628   else if (bytes != NULL)
11629      {
11630       SYMBOL_VALUE_BYTES (sym) = bytes;
11631       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11632     }
11633   else
11634     {
11635       SYMBOL_VALUE (sym) = value;
11636       SYMBOL_CLASS (sym) = LOC_CONST;
11637     }
11638 }
11639
11640 /* Return the type of the die in question using its DW_AT_type attribute.  */
11641
11642 static struct type *
11643 die_type (struct die_info *die, struct dwarf2_cu *cu)
11644 {
11645   struct attribute *type_attr;
11646
11647   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11648   if (!type_attr)
11649     {
11650       /* A missing DW_AT_type represents a void type.  */
11651       return objfile_type (cu->objfile)->builtin_void;
11652     }
11653
11654   return lookup_die_type (die, type_attr, cu);
11655 }
11656
11657 /* True iff CU's producer generates GNAT Ada auxiliary information
11658    that allows to find parallel types through that information instead
11659    of having to do expensive parallel lookups by type name.  */
11660
11661 static int
11662 need_gnat_info (struct dwarf2_cu *cu)
11663 {
11664   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11665      of GNAT produces this auxiliary information, without any indication
11666      that it is produced.  Part of enhancing the FSF version of GNAT
11667      to produce that information will be to put in place an indicator
11668      that we can use in order to determine whether the descriptive type
11669      info is available or not.  One suggestion that has been made is
11670      to use a new attribute, attached to the CU die.  For now, assume
11671      that the descriptive type info is not available.  */
11672   return 0;
11673 }
11674
11675 /* Return the auxiliary type of the die in question using its
11676    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11677    attribute is not present.  */
11678
11679 static struct type *
11680 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11681 {
11682   struct attribute *type_attr;
11683
11684   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11685   if (!type_attr)
11686     return NULL;
11687
11688   return lookup_die_type (die, type_attr, cu);
11689 }
11690
11691 /* If DIE has a descriptive_type attribute, then set the TYPE's
11692    descriptive type accordingly.  */
11693
11694 static void
11695 set_descriptive_type (struct type *type, struct die_info *die,
11696                       struct dwarf2_cu *cu)
11697 {
11698   struct type *descriptive_type = die_descriptive_type (die, cu);
11699
11700   if (descriptive_type)
11701     {
11702       ALLOCATE_GNAT_AUX_TYPE (type);
11703       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11704     }
11705 }
11706
11707 /* Return the containing type of the die in question using its
11708    DW_AT_containing_type attribute.  */
11709
11710 static struct type *
11711 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11712 {
11713   struct attribute *type_attr;
11714
11715   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11716   if (!type_attr)
11717     error (_("Dwarf Error: Problem turning containing type into gdb type "
11718              "[in module %s]"), cu->objfile->name);
11719
11720   return lookup_die_type (die, type_attr, cu);
11721 }
11722
11723 /* Look up the type of DIE in CU using its type attribute ATTR.
11724    If there is no type substitute an error marker.  */
11725
11726 static struct type *
11727 lookup_die_type (struct die_info *die, struct attribute *attr,
11728                  struct dwarf2_cu *cu)
11729 {
11730   struct type *this_type;
11731
11732   /* First see if we have it cached.  */
11733
11734   if (is_ref_attr (attr))
11735     {
11736       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11737
11738       this_type = get_die_type_at_offset (offset, cu->per_cu);
11739     }
11740   else if (attr->form == DW_FORM_ref_sig8)
11741     {
11742       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11743       struct dwarf2_cu *sig_cu;
11744       unsigned int offset;
11745
11746       /* sig_type will be NULL if the signatured type is missing from
11747          the debug info.  */
11748       if (sig_type == NULL)
11749         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11750                  "at 0x%x [in module %s]"),
11751                die->offset, cu->objfile->name);
11752
11753       gdb_assert (sig_type->per_cu.from_debug_types);
11754       offset = sig_type->offset + sig_type->type_offset;
11755       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11756     }
11757   else
11758     {
11759       dump_die_for_error (die);
11760       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11761              dwarf_attr_name (attr->name), cu->objfile->name);
11762     }
11763
11764   /* If not cached we need to read it in.  */
11765
11766   if (this_type == NULL)
11767     {
11768       struct die_info *type_die;
11769       struct dwarf2_cu *type_cu = cu;
11770
11771       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11772       /* If the type is cached, we should have found it above.  */
11773       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11774       this_type = read_type_die_1 (type_die, type_cu);
11775     }
11776
11777   /* If we still don't have a type use an error marker.  */
11778
11779   if (this_type == NULL)
11780     {
11781       char *message, *saved;
11782
11783       /* read_type_die already issued a complaint.  */
11784       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11785                             cu->objfile->name,
11786                             cu->header.offset,
11787                             die->offset);
11788       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11789                              message, strlen (message));
11790       xfree (message);
11791
11792       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11793     }
11794
11795   return this_type;
11796 }
11797
11798 /* Return the type in DIE, CU.
11799    Returns NULL for invalid types.
11800
11801    This first does a lookup in the appropriate type_hash table,
11802    and only reads the die in if necessary.
11803
11804    NOTE: This can be called when reading in partial or full symbols.  */
11805
11806 static struct type *
11807 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11808 {
11809   struct type *this_type;
11810
11811   this_type = get_die_type (die, cu);
11812   if (this_type)
11813     return this_type;
11814
11815   return read_type_die_1 (die, cu);
11816 }
11817
11818 /* Read the type in DIE, CU.
11819    Returns NULL for invalid types.  */
11820
11821 static struct type *
11822 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11823 {
11824   struct type *this_type = NULL;
11825
11826   switch (die->tag)
11827     {
11828     case DW_TAG_class_type:
11829     case DW_TAG_interface_type:
11830     case DW_TAG_structure_type:
11831     case DW_TAG_union_type:
11832       this_type = read_structure_type (die, cu);
11833       break;
11834     case DW_TAG_enumeration_type:
11835       this_type = read_enumeration_type (die, cu);
11836       break;
11837     case DW_TAG_subprogram:
11838     case DW_TAG_subroutine_type:
11839     case DW_TAG_inlined_subroutine:
11840       this_type = read_subroutine_type (die, cu);
11841       break;
11842     case DW_TAG_array_type:
11843       this_type = read_array_type (die, cu);
11844       break;
11845     case DW_TAG_set_type:
11846       this_type = read_set_type (die, cu);
11847       break;
11848     case DW_TAG_pointer_type:
11849       this_type = read_tag_pointer_type (die, cu);
11850       break;
11851     case DW_TAG_ptr_to_member_type:
11852       this_type = read_tag_ptr_to_member_type (die, cu);
11853       break;
11854     case DW_TAG_reference_type:
11855       this_type = read_tag_reference_type (die, cu);
11856       break;
11857     case DW_TAG_const_type:
11858       this_type = read_tag_const_type (die, cu);
11859       break;
11860     case DW_TAG_volatile_type:
11861       this_type = read_tag_volatile_type (die, cu);
11862       break;
11863     case DW_TAG_string_type:
11864       this_type = read_tag_string_type (die, cu);
11865       break;
11866     case DW_TAG_typedef:
11867       this_type = read_typedef (die, cu);
11868       break;
11869     case DW_TAG_subrange_type:
11870       this_type = read_subrange_type (die, cu);
11871       break;
11872     case DW_TAG_base_type:
11873       this_type = read_base_type (die, cu);
11874       break;
11875     case DW_TAG_unspecified_type:
11876       this_type = read_unspecified_type (die, cu);
11877       break;
11878     case DW_TAG_namespace:
11879       this_type = read_namespace_type (die, cu);
11880       break;
11881     case DW_TAG_module:
11882       this_type = read_module_type (die, cu);
11883       break;
11884     default:
11885       complaint (&symfile_complaints,
11886                  _("unexpected tag in read_type_die: '%s'"),
11887                  dwarf_tag_name (die->tag));
11888       break;
11889     }
11890
11891   return this_type;
11892 }
11893
11894 /* See if we can figure out if the class lives in a namespace.  We do
11895    this by looking for a member function; its demangled name will
11896    contain namespace info, if there is any.
11897    Return the computed name or NULL.
11898    Space for the result is allocated on the objfile's obstack.
11899    This is the full-die version of guess_partial_die_structure_name.
11900    In this case we know DIE has no useful parent.  */
11901
11902 static char *
11903 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11904 {
11905   struct die_info *spec_die;
11906   struct dwarf2_cu *spec_cu;
11907   struct die_info *child;
11908
11909   spec_cu = cu;
11910   spec_die = die_specification (die, &spec_cu);
11911   if (spec_die != NULL)
11912     {
11913       die = spec_die;
11914       cu = spec_cu;
11915     }
11916
11917   for (child = die->child;
11918        child != NULL;
11919        child = child->sibling)
11920     {
11921       if (child->tag == DW_TAG_subprogram)
11922         {
11923           struct attribute *attr;
11924
11925           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11926           if (attr == NULL)
11927             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11928           if (attr != NULL)
11929             {
11930               char *actual_name
11931                 = language_class_name_from_physname (cu->language_defn,
11932                                                      DW_STRING (attr));
11933               char *name = NULL;
11934
11935               if (actual_name != NULL)
11936                 {
11937                   char *die_name = dwarf2_name (die, cu);
11938
11939                   if (die_name != NULL
11940                       && strcmp (die_name, actual_name) != 0)
11941                     {
11942                       /* Strip off the class name from the full name.
11943                          We want the prefix.  */
11944                       int die_name_len = strlen (die_name);
11945                       int actual_name_len = strlen (actual_name);
11946
11947                       /* Test for '::' as a sanity check.  */
11948                       if (actual_name_len > die_name_len + 2
11949                           && actual_name[actual_name_len
11950                                          - die_name_len - 1] == ':')
11951                         name =
11952                           obsavestring (actual_name,
11953                                         actual_name_len - die_name_len - 2,
11954                                         &cu->objfile->objfile_obstack);
11955                     }
11956                 }
11957               xfree (actual_name);
11958               return name;
11959             }
11960         }
11961     }
11962
11963   return NULL;
11964 }
11965
11966 /* Return the name of the namespace/class that DIE is defined within,
11967    or "" if we can't tell.  The caller should not xfree the result.
11968
11969    For example, if we're within the method foo() in the following
11970    code:
11971
11972    namespace N {
11973      class C {
11974        void foo () {
11975        }
11976      };
11977    }
11978
11979    then determine_prefix on foo's die will return "N::C".  */
11980
11981 static char *
11982 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11983 {
11984   struct die_info *parent, *spec_die;
11985   struct dwarf2_cu *spec_cu;
11986   struct type *parent_type;
11987
11988   if (cu->language != language_cplus && cu->language != language_java
11989       && cu->language != language_fortran)
11990     return "";
11991
11992   /* We have to be careful in the presence of DW_AT_specification.
11993      For example, with GCC 3.4, given the code
11994
11995      namespace N {
11996        void foo() {
11997          // Definition of N::foo.
11998        }
11999      }
12000
12001      then we'll have a tree of DIEs like this:
12002
12003      1: DW_TAG_compile_unit
12004        2: DW_TAG_namespace        // N
12005          3: DW_TAG_subprogram     // declaration of N::foo
12006        4: DW_TAG_subprogram       // definition of N::foo
12007             DW_AT_specification   // refers to die #3
12008
12009      Thus, when processing die #4, we have to pretend that we're in
12010      the context of its DW_AT_specification, namely the contex of die
12011      #3.  */
12012   spec_cu = cu;
12013   spec_die = die_specification (die, &spec_cu);
12014   if (spec_die == NULL)
12015     parent = die->parent;
12016   else
12017     {
12018       parent = spec_die->parent;
12019       cu = spec_cu;
12020     }
12021
12022   if (parent == NULL)
12023     return "";
12024   else if (parent->building_fullname)
12025     {
12026       const char *name;
12027       const char *parent_name;
12028
12029       /* It has been seen on RealView 2.2 built binaries,
12030          DW_TAG_template_type_param types actually _defined_ as
12031          children of the parent class:
12032
12033          enum E {};
12034          template class <class Enum> Class{};
12035          Class<enum E> class_e;
12036
12037          1: DW_TAG_class_type (Class)
12038            2: DW_TAG_enumeration_type (E)
12039              3: DW_TAG_enumerator (enum1:0)
12040              3: DW_TAG_enumerator (enum2:1)
12041              ...
12042            2: DW_TAG_template_type_param
12043               DW_AT_type  DW_FORM_ref_udata (E)
12044
12045          Besides being broken debug info, it can put GDB into an
12046          infinite loop.  Consider:
12047
12048          When we're building the full name for Class<E>, we'll start
12049          at Class, and go look over its template type parameters,
12050          finding E.  We'll then try to build the full name of E, and
12051          reach here.  We're now trying to build the full name of E,
12052          and look over the parent DIE for containing scope.  In the
12053          broken case, if we followed the parent DIE of E, we'd again
12054          find Class, and once again go look at its template type
12055          arguments, etc., etc.  Simply don't consider such parent die
12056          as source-level parent of this die (it can't be, the language
12057          doesn't allow it), and break the loop here.  */
12058       name = dwarf2_name (die, cu);
12059       parent_name = dwarf2_name (parent, cu);
12060       complaint (&symfile_complaints,
12061                  _("template param type '%s' defined within parent '%s'"),
12062                  name ? name : "<unknown>",
12063                  parent_name ? parent_name : "<unknown>");
12064       return "";
12065     }
12066   else
12067     switch (parent->tag)
12068       {
12069       case DW_TAG_namespace:
12070         parent_type = read_type_die (parent, cu);
12071         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12072            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12073            Work around this problem here.  */
12074         if (cu->language == language_cplus
12075             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12076           return "";
12077         /* We give a name to even anonymous namespaces.  */
12078         return TYPE_TAG_NAME (parent_type);
12079       case DW_TAG_class_type:
12080       case DW_TAG_interface_type:
12081       case DW_TAG_structure_type:
12082       case DW_TAG_union_type:
12083       case DW_TAG_module:
12084         parent_type = read_type_die (parent, cu);
12085         if (TYPE_TAG_NAME (parent_type) != NULL)
12086           return TYPE_TAG_NAME (parent_type);
12087         else
12088           /* An anonymous structure is only allowed non-static data
12089              members; no typedefs, no member functions, et cetera.
12090              So it does not need a prefix.  */
12091           return "";
12092       case DW_TAG_compile_unit:
12093         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12094         if (cu->language == language_cplus
12095             && dwarf2_per_objfile->types.asection != NULL
12096             && die->child != NULL
12097             && (die->tag == DW_TAG_class_type
12098                 || die->tag == DW_TAG_structure_type
12099                 || die->tag == DW_TAG_union_type))
12100           {
12101             char *name = guess_full_die_structure_name (die, cu);
12102             if (name != NULL)
12103               return name;
12104           }
12105         return "";
12106       default:
12107         return determine_prefix (parent, cu);
12108       }
12109 }
12110
12111 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12112    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12113    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12114    an obconcat, otherwise allocate storage for the result.  The CU argument is
12115    used to determine the language and hence, the appropriate separator.  */
12116
12117 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12118
12119 static char *
12120 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12121                  int physname, struct dwarf2_cu *cu)
12122 {
12123   const char *lead = "";
12124   const char *sep;
12125
12126   if (suffix == NULL || suffix[0] == '\0'
12127       || prefix == NULL || prefix[0] == '\0')
12128     sep = "";
12129   else if (cu->language == language_java)
12130     sep = ".";
12131   else if (cu->language == language_fortran && physname)
12132     {
12133       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12134          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12135
12136       lead = "__";
12137       sep = "_MOD_";
12138     }
12139   else
12140     sep = "::";
12141
12142   if (prefix == NULL)
12143     prefix = "";
12144   if (suffix == NULL)
12145     suffix = "";
12146
12147   if (obs == NULL)
12148     {
12149       char *retval
12150         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12151
12152       strcpy (retval, lead);
12153       strcat (retval, prefix);
12154       strcat (retval, sep);
12155       strcat (retval, suffix);
12156       return retval;
12157     }
12158   else
12159     {
12160       /* We have an obstack.  */
12161       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12162     }
12163 }
12164
12165 /* Return sibling of die, NULL if no sibling.  */
12166
12167 static struct die_info *
12168 sibling_die (struct die_info *die)
12169 {
12170   return die->sibling;
12171 }
12172
12173 /* Get name of a die, return NULL if not found.  */
12174
12175 static char *
12176 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12177                           struct obstack *obstack)
12178 {
12179   if (name && cu->language == language_cplus)
12180     {
12181       char *canon_name = cp_canonicalize_string (name);
12182
12183       if (canon_name != NULL)
12184         {
12185           if (strcmp (canon_name, name) != 0)
12186             name = obsavestring (canon_name, strlen (canon_name),
12187                                  obstack);
12188           xfree (canon_name);
12189         }
12190     }
12191
12192   return name;
12193 }
12194
12195 /* Get name of a die, return NULL if not found.  */
12196
12197 static char *
12198 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12199 {
12200   struct attribute *attr;
12201
12202   attr = dwarf2_attr (die, DW_AT_name, cu);
12203   if ((!attr || !DW_STRING (attr))
12204       && die->tag != DW_TAG_class_type
12205       && die->tag != DW_TAG_interface_type
12206       && die->tag != DW_TAG_structure_type
12207       && die->tag != DW_TAG_union_type)
12208     return NULL;
12209
12210   switch (die->tag)
12211     {
12212     case DW_TAG_compile_unit:
12213       /* Compilation units have a DW_AT_name that is a filename, not
12214          a source language identifier.  */
12215     case DW_TAG_enumeration_type:
12216     case DW_TAG_enumerator:
12217       /* These tags always have simple identifiers already; no need
12218          to canonicalize them.  */
12219       return DW_STRING (attr);
12220
12221     case DW_TAG_subprogram:
12222       /* Java constructors will all be named "<init>", so return
12223          the class name when we see this special case.  */
12224       if (cu->language == language_java
12225           && DW_STRING (attr) != NULL
12226           && strcmp (DW_STRING (attr), "<init>") == 0)
12227         {
12228           struct dwarf2_cu *spec_cu = cu;
12229           struct die_info *spec_die;
12230
12231           /* GCJ will output '<init>' for Java constructor names.
12232              For this special case, return the name of the parent class.  */
12233
12234           /* GCJ may output suprogram DIEs with AT_specification set.
12235              If so, use the name of the specified DIE.  */
12236           spec_die = die_specification (die, &spec_cu);
12237           if (spec_die != NULL)
12238             return dwarf2_name (spec_die, spec_cu);
12239
12240           do
12241             {
12242               die = die->parent;
12243               if (die->tag == DW_TAG_class_type)
12244                 return dwarf2_name (die, cu);
12245             }
12246           while (die->tag != DW_TAG_compile_unit);
12247         }
12248       break;
12249
12250     case DW_TAG_class_type:
12251     case DW_TAG_interface_type:
12252     case DW_TAG_structure_type:
12253     case DW_TAG_union_type:
12254       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12255          structures or unions.  These were of the form "._%d" in GCC 4.1,
12256          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12257          and GCC 4.4.  We work around this problem by ignoring these.  */
12258       if (attr && DW_STRING (attr)
12259           && (strncmp (DW_STRING (attr), "._", 2) == 0
12260               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12261         return NULL;
12262
12263       /* GCC might emit a nameless typedef that has a linkage name.  See
12264          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12265       if (!attr || DW_STRING (attr) == NULL)
12266         {
12267           char *demangled = NULL;
12268
12269           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12270           if (attr == NULL)
12271             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12272
12273           if (attr == NULL || DW_STRING (attr) == NULL)
12274             return NULL;
12275
12276           /* Avoid demangling DW_STRING (attr) the second time on a second
12277              call for the same DIE.  */
12278           if (!DW_STRING_IS_CANONICAL (attr))
12279             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12280
12281           if (demangled)
12282             {
12283               /* FIXME: we already did this for the partial symbol... */
12284               DW_STRING (attr)
12285                 = obsavestring (demangled, strlen (demangled),
12286                                 &cu->objfile->objfile_obstack);
12287               DW_STRING_IS_CANONICAL (attr) = 1;
12288               xfree (demangled);
12289             }
12290         }
12291       break;
12292
12293     default:
12294       break;
12295     }
12296
12297   if (!DW_STRING_IS_CANONICAL (attr))
12298     {
12299       DW_STRING (attr)
12300         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12301                                     &cu->objfile->objfile_obstack);
12302       DW_STRING_IS_CANONICAL (attr) = 1;
12303     }
12304   return DW_STRING (attr);
12305 }
12306
12307 /* Return the die that this die in an extension of, or NULL if there
12308    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12309    containing the return value on output.  */
12310
12311 static struct die_info *
12312 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12313 {
12314   struct attribute *attr;
12315
12316   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12317   if (attr == NULL)
12318     return NULL;
12319
12320   return follow_die_ref (die, attr, ext_cu);
12321 }
12322
12323 /* Convert a DIE tag into its string name.  */
12324
12325 static char *
12326 dwarf_tag_name (unsigned tag)
12327 {
12328   switch (tag)
12329     {
12330     case DW_TAG_padding:
12331       return "DW_TAG_padding";
12332     case DW_TAG_array_type:
12333       return "DW_TAG_array_type";
12334     case DW_TAG_class_type:
12335       return "DW_TAG_class_type";
12336     case DW_TAG_entry_point:
12337       return "DW_TAG_entry_point";
12338     case DW_TAG_enumeration_type:
12339       return "DW_TAG_enumeration_type";
12340     case DW_TAG_formal_parameter:
12341       return "DW_TAG_formal_parameter";
12342     case DW_TAG_imported_declaration:
12343       return "DW_TAG_imported_declaration";
12344     case DW_TAG_label:
12345       return "DW_TAG_label";
12346     case DW_TAG_lexical_block:
12347       return "DW_TAG_lexical_block";
12348     case DW_TAG_member:
12349       return "DW_TAG_member";
12350     case DW_TAG_pointer_type:
12351       return "DW_TAG_pointer_type";
12352     case DW_TAG_reference_type:
12353       return "DW_TAG_reference_type";
12354     case DW_TAG_compile_unit:
12355       return "DW_TAG_compile_unit";
12356     case DW_TAG_string_type:
12357       return "DW_TAG_string_type";
12358     case DW_TAG_structure_type:
12359       return "DW_TAG_structure_type";
12360     case DW_TAG_subroutine_type:
12361       return "DW_TAG_subroutine_type";
12362     case DW_TAG_typedef:
12363       return "DW_TAG_typedef";
12364     case DW_TAG_union_type:
12365       return "DW_TAG_union_type";
12366     case DW_TAG_unspecified_parameters:
12367       return "DW_TAG_unspecified_parameters";
12368     case DW_TAG_variant:
12369       return "DW_TAG_variant";
12370     case DW_TAG_common_block:
12371       return "DW_TAG_common_block";
12372     case DW_TAG_common_inclusion:
12373       return "DW_TAG_common_inclusion";
12374     case DW_TAG_inheritance:
12375       return "DW_TAG_inheritance";
12376     case DW_TAG_inlined_subroutine:
12377       return "DW_TAG_inlined_subroutine";
12378     case DW_TAG_module:
12379       return "DW_TAG_module";
12380     case DW_TAG_ptr_to_member_type:
12381       return "DW_TAG_ptr_to_member_type";
12382     case DW_TAG_set_type:
12383       return "DW_TAG_set_type";
12384     case DW_TAG_subrange_type:
12385       return "DW_TAG_subrange_type";
12386     case DW_TAG_with_stmt:
12387       return "DW_TAG_with_stmt";
12388     case DW_TAG_access_declaration:
12389       return "DW_TAG_access_declaration";
12390     case DW_TAG_base_type:
12391       return "DW_TAG_base_type";
12392     case DW_TAG_catch_block:
12393       return "DW_TAG_catch_block";
12394     case DW_TAG_const_type:
12395       return "DW_TAG_const_type";
12396     case DW_TAG_constant:
12397       return "DW_TAG_constant";
12398     case DW_TAG_enumerator:
12399       return "DW_TAG_enumerator";
12400     case DW_TAG_file_type:
12401       return "DW_TAG_file_type";
12402     case DW_TAG_friend:
12403       return "DW_TAG_friend";
12404     case DW_TAG_namelist:
12405       return "DW_TAG_namelist";
12406     case DW_TAG_namelist_item:
12407       return "DW_TAG_namelist_item";
12408     case DW_TAG_packed_type:
12409       return "DW_TAG_packed_type";
12410     case DW_TAG_subprogram:
12411       return "DW_TAG_subprogram";
12412     case DW_TAG_template_type_param:
12413       return "DW_TAG_template_type_param";
12414     case DW_TAG_template_value_param:
12415       return "DW_TAG_template_value_param";
12416     case DW_TAG_thrown_type:
12417       return "DW_TAG_thrown_type";
12418     case DW_TAG_try_block:
12419       return "DW_TAG_try_block";
12420     case DW_TAG_variant_part:
12421       return "DW_TAG_variant_part";
12422     case DW_TAG_variable:
12423       return "DW_TAG_variable";
12424     case DW_TAG_volatile_type:
12425       return "DW_TAG_volatile_type";
12426     case DW_TAG_dwarf_procedure:
12427       return "DW_TAG_dwarf_procedure";
12428     case DW_TAG_restrict_type:
12429       return "DW_TAG_restrict_type";
12430     case DW_TAG_interface_type:
12431       return "DW_TAG_interface_type";
12432     case DW_TAG_namespace:
12433       return "DW_TAG_namespace";
12434     case DW_TAG_imported_module:
12435       return "DW_TAG_imported_module";
12436     case DW_TAG_unspecified_type:
12437       return "DW_TAG_unspecified_type";
12438     case DW_TAG_partial_unit:
12439       return "DW_TAG_partial_unit";
12440     case DW_TAG_imported_unit:
12441       return "DW_TAG_imported_unit";
12442     case DW_TAG_condition:
12443       return "DW_TAG_condition";
12444     case DW_TAG_shared_type:
12445       return "DW_TAG_shared_type";
12446     case DW_TAG_type_unit:
12447       return "DW_TAG_type_unit";
12448     case DW_TAG_MIPS_loop:
12449       return "DW_TAG_MIPS_loop";
12450     case DW_TAG_HP_array_descriptor:
12451       return "DW_TAG_HP_array_descriptor";
12452     case DW_TAG_format_label:
12453       return "DW_TAG_format_label";
12454     case DW_TAG_function_template:
12455       return "DW_TAG_function_template";
12456     case DW_TAG_class_template:
12457       return "DW_TAG_class_template";
12458     case DW_TAG_GNU_BINCL:
12459       return "DW_TAG_GNU_BINCL";
12460     case DW_TAG_GNU_EINCL:
12461       return "DW_TAG_GNU_EINCL";
12462     case DW_TAG_upc_shared_type:
12463       return "DW_TAG_upc_shared_type";
12464     case DW_TAG_upc_strict_type:
12465       return "DW_TAG_upc_strict_type";
12466     case DW_TAG_upc_relaxed_type:
12467       return "DW_TAG_upc_relaxed_type";
12468     case DW_TAG_PGI_kanji_type:
12469       return "DW_TAG_PGI_kanji_type";
12470     case DW_TAG_PGI_interface_block:
12471       return "DW_TAG_PGI_interface_block";
12472     default:
12473       return "DW_TAG_<unknown>";
12474     }
12475 }
12476
12477 /* Convert a DWARF attribute code into its string name.  */
12478
12479 static char *
12480 dwarf_attr_name (unsigned attr)
12481 {
12482   switch (attr)
12483     {
12484     case DW_AT_sibling:
12485       return "DW_AT_sibling";
12486     case DW_AT_location:
12487       return "DW_AT_location";
12488     case DW_AT_name:
12489       return "DW_AT_name";
12490     case DW_AT_ordering:
12491       return "DW_AT_ordering";
12492     case DW_AT_subscr_data:
12493       return "DW_AT_subscr_data";
12494     case DW_AT_byte_size:
12495       return "DW_AT_byte_size";
12496     case DW_AT_bit_offset:
12497       return "DW_AT_bit_offset";
12498     case DW_AT_bit_size:
12499       return "DW_AT_bit_size";
12500     case DW_AT_element_list:
12501       return "DW_AT_element_list";
12502     case DW_AT_stmt_list:
12503       return "DW_AT_stmt_list";
12504     case DW_AT_low_pc:
12505       return "DW_AT_low_pc";
12506     case DW_AT_high_pc:
12507       return "DW_AT_high_pc";
12508     case DW_AT_language:
12509       return "DW_AT_language";
12510     case DW_AT_member:
12511       return "DW_AT_member";
12512     case DW_AT_discr:
12513       return "DW_AT_discr";
12514     case DW_AT_discr_value:
12515       return "DW_AT_discr_value";
12516     case DW_AT_visibility:
12517       return "DW_AT_visibility";
12518     case DW_AT_import:
12519       return "DW_AT_import";
12520     case DW_AT_string_length:
12521       return "DW_AT_string_length";
12522     case DW_AT_common_reference:
12523       return "DW_AT_common_reference";
12524     case DW_AT_comp_dir:
12525       return "DW_AT_comp_dir";
12526     case DW_AT_const_value:
12527       return "DW_AT_const_value";
12528     case DW_AT_containing_type:
12529       return "DW_AT_containing_type";
12530     case DW_AT_default_value:
12531       return "DW_AT_default_value";
12532     case DW_AT_inline:
12533       return "DW_AT_inline";
12534     case DW_AT_is_optional:
12535       return "DW_AT_is_optional";
12536     case DW_AT_lower_bound:
12537       return "DW_AT_lower_bound";
12538     case DW_AT_producer:
12539       return "DW_AT_producer";
12540     case DW_AT_prototyped:
12541       return "DW_AT_prototyped";
12542     case DW_AT_return_addr:
12543       return "DW_AT_return_addr";
12544     case DW_AT_start_scope:
12545       return "DW_AT_start_scope";
12546     case DW_AT_bit_stride:
12547       return "DW_AT_bit_stride";
12548     case DW_AT_upper_bound:
12549       return "DW_AT_upper_bound";
12550     case DW_AT_abstract_origin:
12551       return "DW_AT_abstract_origin";
12552     case DW_AT_accessibility:
12553       return "DW_AT_accessibility";
12554     case DW_AT_address_class:
12555       return "DW_AT_address_class";
12556     case DW_AT_artificial:
12557       return "DW_AT_artificial";
12558     case DW_AT_base_types:
12559       return "DW_AT_base_types";
12560     case DW_AT_calling_convention:
12561       return "DW_AT_calling_convention";
12562     case DW_AT_count:
12563       return "DW_AT_count";
12564     case DW_AT_data_member_location:
12565       return "DW_AT_data_member_location";
12566     case DW_AT_decl_column:
12567       return "DW_AT_decl_column";
12568     case DW_AT_decl_file:
12569       return "DW_AT_decl_file";
12570     case DW_AT_decl_line:
12571       return "DW_AT_decl_line";
12572     case DW_AT_declaration:
12573       return "DW_AT_declaration";
12574     case DW_AT_discr_list:
12575       return "DW_AT_discr_list";
12576     case DW_AT_encoding:
12577       return "DW_AT_encoding";
12578     case DW_AT_external:
12579       return "DW_AT_external";
12580     case DW_AT_frame_base:
12581       return "DW_AT_frame_base";
12582     case DW_AT_friend:
12583       return "DW_AT_friend";
12584     case DW_AT_identifier_case:
12585       return "DW_AT_identifier_case";
12586     case DW_AT_macro_info:
12587       return "DW_AT_macro_info";
12588     case DW_AT_namelist_items:
12589       return "DW_AT_namelist_items";
12590     case DW_AT_priority:
12591       return "DW_AT_priority";
12592     case DW_AT_segment:
12593       return "DW_AT_segment";
12594     case DW_AT_specification:
12595       return "DW_AT_specification";
12596     case DW_AT_static_link:
12597       return "DW_AT_static_link";
12598     case DW_AT_type:
12599       return "DW_AT_type";
12600     case DW_AT_use_location:
12601       return "DW_AT_use_location";
12602     case DW_AT_variable_parameter:
12603       return "DW_AT_variable_parameter";
12604     case DW_AT_virtuality:
12605       return "DW_AT_virtuality";
12606     case DW_AT_vtable_elem_location:
12607       return "DW_AT_vtable_elem_location";
12608     /* DWARF 3 values.  */
12609     case DW_AT_allocated:
12610       return "DW_AT_allocated";
12611     case DW_AT_associated:
12612       return "DW_AT_associated";
12613     case DW_AT_data_location:
12614       return "DW_AT_data_location";
12615     case DW_AT_byte_stride:
12616       return "DW_AT_byte_stride";
12617     case DW_AT_entry_pc:
12618       return "DW_AT_entry_pc";
12619     case DW_AT_use_UTF8:
12620       return "DW_AT_use_UTF8";
12621     case DW_AT_extension:
12622       return "DW_AT_extension";
12623     case DW_AT_ranges:
12624       return "DW_AT_ranges";
12625     case DW_AT_trampoline:
12626       return "DW_AT_trampoline";
12627     case DW_AT_call_column:
12628       return "DW_AT_call_column";
12629     case DW_AT_call_file:
12630       return "DW_AT_call_file";
12631     case DW_AT_call_line:
12632       return "DW_AT_call_line";
12633     case DW_AT_description:
12634       return "DW_AT_description";
12635     case DW_AT_binary_scale:
12636       return "DW_AT_binary_scale";
12637     case DW_AT_decimal_scale:
12638       return "DW_AT_decimal_scale";
12639     case DW_AT_small:
12640       return "DW_AT_small";
12641     case DW_AT_decimal_sign:
12642       return "DW_AT_decimal_sign";
12643     case DW_AT_digit_count:
12644       return "DW_AT_digit_count";
12645     case DW_AT_picture_string:
12646       return "DW_AT_picture_string";
12647     case DW_AT_mutable:
12648       return "DW_AT_mutable";
12649     case DW_AT_threads_scaled:
12650       return "DW_AT_threads_scaled";
12651     case DW_AT_explicit:
12652       return "DW_AT_explicit";
12653     case DW_AT_object_pointer:
12654       return "DW_AT_object_pointer";
12655     case DW_AT_endianity:
12656       return "DW_AT_endianity";
12657     case DW_AT_elemental:
12658       return "DW_AT_elemental";
12659     case DW_AT_pure:
12660       return "DW_AT_pure";
12661     case DW_AT_recursive:
12662       return "DW_AT_recursive";
12663     /* DWARF 4 values.  */
12664     case DW_AT_signature:
12665       return "DW_AT_signature";
12666     case DW_AT_linkage_name:
12667       return "DW_AT_linkage_name";
12668     /* SGI/MIPS extensions.  */
12669 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12670     case DW_AT_MIPS_fde:
12671       return "DW_AT_MIPS_fde";
12672 #endif
12673     case DW_AT_MIPS_loop_begin:
12674       return "DW_AT_MIPS_loop_begin";
12675     case DW_AT_MIPS_tail_loop_begin:
12676       return "DW_AT_MIPS_tail_loop_begin";
12677     case DW_AT_MIPS_epilog_begin:
12678       return "DW_AT_MIPS_epilog_begin";
12679     case DW_AT_MIPS_loop_unroll_factor:
12680       return "DW_AT_MIPS_loop_unroll_factor";
12681     case DW_AT_MIPS_software_pipeline_depth:
12682       return "DW_AT_MIPS_software_pipeline_depth";
12683     case DW_AT_MIPS_linkage_name:
12684       return "DW_AT_MIPS_linkage_name";
12685     case DW_AT_MIPS_stride:
12686       return "DW_AT_MIPS_stride";
12687     case DW_AT_MIPS_abstract_name:
12688       return "DW_AT_MIPS_abstract_name";
12689     case DW_AT_MIPS_clone_origin:
12690       return "DW_AT_MIPS_clone_origin";
12691     case DW_AT_MIPS_has_inlines:
12692       return "DW_AT_MIPS_has_inlines";
12693     /* HP extensions.  */
12694 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12695     case DW_AT_HP_block_index:
12696       return "DW_AT_HP_block_index";
12697 #endif
12698     case DW_AT_HP_unmodifiable:
12699       return "DW_AT_HP_unmodifiable";
12700     case DW_AT_HP_actuals_stmt_list:
12701       return "DW_AT_HP_actuals_stmt_list";
12702     case DW_AT_HP_proc_per_section:
12703       return "DW_AT_HP_proc_per_section";
12704     case DW_AT_HP_raw_data_ptr:
12705       return "DW_AT_HP_raw_data_ptr";
12706     case DW_AT_HP_pass_by_reference:
12707       return "DW_AT_HP_pass_by_reference";
12708     case DW_AT_HP_opt_level:
12709       return "DW_AT_HP_opt_level";
12710     case DW_AT_HP_prof_version_id:
12711       return "DW_AT_HP_prof_version_id";
12712     case DW_AT_HP_opt_flags:
12713       return "DW_AT_HP_opt_flags";
12714     case DW_AT_HP_cold_region_low_pc:
12715       return "DW_AT_HP_cold_region_low_pc";
12716     case DW_AT_HP_cold_region_high_pc:
12717       return "DW_AT_HP_cold_region_high_pc";
12718     case DW_AT_HP_all_variables_modifiable:
12719       return "DW_AT_HP_all_variables_modifiable";
12720     case DW_AT_HP_linkage_name:
12721       return "DW_AT_HP_linkage_name";
12722     case DW_AT_HP_prof_flags:
12723       return "DW_AT_HP_prof_flags";
12724     /* GNU extensions.  */
12725     case DW_AT_sf_names:
12726       return "DW_AT_sf_names";
12727     case DW_AT_src_info:
12728       return "DW_AT_src_info";
12729     case DW_AT_mac_info:
12730       return "DW_AT_mac_info";
12731     case DW_AT_src_coords:
12732       return "DW_AT_src_coords";
12733     case DW_AT_body_begin:
12734       return "DW_AT_body_begin";
12735     case DW_AT_body_end:
12736       return "DW_AT_body_end";
12737     case DW_AT_GNU_vector:
12738       return "DW_AT_GNU_vector";
12739     case DW_AT_GNU_odr_signature:
12740       return "DW_AT_GNU_odr_signature";
12741     /* VMS extensions.  */
12742     case DW_AT_VMS_rtnbeg_pd_address:
12743       return "DW_AT_VMS_rtnbeg_pd_address";
12744     /* UPC extension.  */
12745     case DW_AT_upc_threads_scaled:
12746       return "DW_AT_upc_threads_scaled";
12747     /* PGI (STMicroelectronics) extensions.  */
12748     case DW_AT_PGI_lbase:
12749       return "DW_AT_PGI_lbase";
12750     case DW_AT_PGI_soffset:
12751       return "DW_AT_PGI_soffset";
12752     case DW_AT_PGI_lstride:
12753       return "DW_AT_PGI_lstride";
12754     default:
12755       return "DW_AT_<unknown>";
12756     }
12757 }
12758
12759 /* Convert a DWARF value form code into its string name.  */
12760
12761 static char *
12762 dwarf_form_name (unsigned form)
12763 {
12764   switch (form)
12765     {
12766     case DW_FORM_addr:
12767       return "DW_FORM_addr";
12768     case DW_FORM_block2:
12769       return "DW_FORM_block2";
12770     case DW_FORM_block4:
12771       return "DW_FORM_block4";
12772     case DW_FORM_data2:
12773       return "DW_FORM_data2";
12774     case DW_FORM_data4:
12775       return "DW_FORM_data4";
12776     case DW_FORM_data8:
12777       return "DW_FORM_data8";
12778     case DW_FORM_string:
12779       return "DW_FORM_string";
12780     case DW_FORM_block:
12781       return "DW_FORM_block";
12782     case DW_FORM_block1:
12783       return "DW_FORM_block1";
12784     case DW_FORM_data1:
12785       return "DW_FORM_data1";
12786     case DW_FORM_flag:
12787       return "DW_FORM_flag";
12788     case DW_FORM_sdata:
12789       return "DW_FORM_sdata";
12790     case DW_FORM_strp:
12791       return "DW_FORM_strp";
12792     case DW_FORM_udata:
12793       return "DW_FORM_udata";
12794     case DW_FORM_ref_addr:
12795       return "DW_FORM_ref_addr";
12796     case DW_FORM_ref1:
12797       return "DW_FORM_ref1";
12798     case DW_FORM_ref2:
12799       return "DW_FORM_ref2";
12800     case DW_FORM_ref4:
12801       return "DW_FORM_ref4";
12802     case DW_FORM_ref8:
12803       return "DW_FORM_ref8";
12804     case DW_FORM_ref_udata:
12805       return "DW_FORM_ref_udata";
12806     case DW_FORM_indirect:
12807       return "DW_FORM_indirect";
12808     case DW_FORM_sec_offset:
12809       return "DW_FORM_sec_offset";
12810     case DW_FORM_exprloc:
12811       return "DW_FORM_exprloc";
12812     case DW_FORM_flag_present:
12813       return "DW_FORM_flag_present";
12814     case DW_FORM_ref_sig8:
12815       return "DW_FORM_ref_sig8";
12816     default:
12817       return "DW_FORM_<unknown>";
12818     }
12819 }
12820
12821 /* Convert a DWARF stack opcode into its string name.  */
12822
12823 const char *
12824 dwarf_stack_op_name (unsigned op)
12825 {
12826   switch (op)
12827     {
12828     case DW_OP_addr:
12829       return "DW_OP_addr";
12830     case DW_OP_deref:
12831       return "DW_OP_deref";
12832     case DW_OP_const1u:
12833       return "DW_OP_const1u";
12834     case DW_OP_const1s:
12835       return "DW_OP_const1s";
12836     case DW_OP_const2u:
12837       return "DW_OP_const2u";
12838     case DW_OP_const2s:
12839       return "DW_OP_const2s";
12840     case DW_OP_const4u:
12841       return "DW_OP_const4u";
12842     case DW_OP_const4s:
12843       return "DW_OP_const4s";
12844     case DW_OP_const8u:
12845       return "DW_OP_const8u";
12846     case DW_OP_const8s:
12847       return "DW_OP_const8s";
12848     case DW_OP_constu:
12849       return "DW_OP_constu";
12850     case DW_OP_consts:
12851       return "DW_OP_consts";
12852     case DW_OP_dup:
12853       return "DW_OP_dup";
12854     case DW_OP_drop:
12855       return "DW_OP_drop";
12856     case DW_OP_over:
12857       return "DW_OP_over";
12858     case DW_OP_pick:
12859       return "DW_OP_pick";
12860     case DW_OP_swap:
12861       return "DW_OP_swap";
12862     case DW_OP_rot:
12863       return "DW_OP_rot";
12864     case DW_OP_xderef:
12865       return "DW_OP_xderef";
12866     case DW_OP_abs:
12867       return "DW_OP_abs";
12868     case DW_OP_and:
12869       return "DW_OP_and";
12870     case DW_OP_div:
12871       return "DW_OP_div";
12872     case DW_OP_minus:
12873       return "DW_OP_minus";
12874     case DW_OP_mod:
12875       return "DW_OP_mod";
12876     case DW_OP_mul:
12877       return "DW_OP_mul";
12878     case DW_OP_neg:
12879       return "DW_OP_neg";
12880     case DW_OP_not:
12881       return "DW_OP_not";
12882     case DW_OP_or:
12883       return "DW_OP_or";
12884     case DW_OP_plus:
12885       return "DW_OP_plus";
12886     case DW_OP_plus_uconst:
12887       return "DW_OP_plus_uconst";
12888     case DW_OP_shl:
12889       return "DW_OP_shl";
12890     case DW_OP_shr:
12891       return "DW_OP_shr";
12892     case DW_OP_shra:
12893       return "DW_OP_shra";
12894     case DW_OP_xor:
12895       return "DW_OP_xor";
12896     case DW_OP_bra:
12897       return "DW_OP_bra";
12898     case DW_OP_eq:
12899       return "DW_OP_eq";
12900     case DW_OP_ge:
12901       return "DW_OP_ge";
12902     case DW_OP_gt:
12903       return "DW_OP_gt";
12904     case DW_OP_le:
12905       return "DW_OP_le";
12906     case DW_OP_lt:
12907       return "DW_OP_lt";
12908     case DW_OP_ne:
12909       return "DW_OP_ne";
12910     case DW_OP_skip:
12911       return "DW_OP_skip";
12912     case DW_OP_lit0:
12913       return "DW_OP_lit0";
12914     case DW_OP_lit1:
12915       return "DW_OP_lit1";
12916     case DW_OP_lit2:
12917       return "DW_OP_lit2";
12918     case DW_OP_lit3:
12919       return "DW_OP_lit3";
12920     case DW_OP_lit4:
12921       return "DW_OP_lit4";
12922     case DW_OP_lit5:
12923       return "DW_OP_lit5";
12924     case DW_OP_lit6:
12925       return "DW_OP_lit6";
12926     case DW_OP_lit7:
12927       return "DW_OP_lit7";
12928     case DW_OP_lit8:
12929       return "DW_OP_lit8";
12930     case DW_OP_lit9:
12931       return "DW_OP_lit9";
12932     case DW_OP_lit10:
12933       return "DW_OP_lit10";
12934     case DW_OP_lit11:
12935       return "DW_OP_lit11";
12936     case DW_OP_lit12:
12937       return "DW_OP_lit12";
12938     case DW_OP_lit13:
12939       return "DW_OP_lit13";
12940     case DW_OP_lit14:
12941       return "DW_OP_lit14";
12942     case DW_OP_lit15:
12943       return "DW_OP_lit15";
12944     case DW_OP_lit16:
12945       return "DW_OP_lit16";
12946     case DW_OP_lit17:
12947       return "DW_OP_lit17";
12948     case DW_OP_lit18:
12949       return "DW_OP_lit18";
12950     case DW_OP_lit19:
12951       return "DW_OP_lit19";
12952     case DW_OP_lit20:
12953       return "DW_OP_lit20";
12954     case DW_OP_lit21:
12955       return "DW_OP_lit21";
12956     case DW_OP_lit22:
12957       return "DW_OP_lit22";
12958     case DW_OP_lit23:
12959       return "DW_OP_lit23";
12960     case DW_OP_lit24:
12961       return "DW_OP_lit24";
12962     case DW_OP_lit25:
12963       return "DW_OP_lit25";
12964     case DW_OP_lit26:
12965       return "DW_OP_lit26";
12966     case DW_OP_lit27:
12967       return "DW_OP_lit27";
12968     case DW_OP_lit28:
12969       return "DW_OP_lit28";
12970     case DW_OP_lit29:
12971       return "DW_OP_lit29";
12972     case DW_OP_lit30:
12973       return "DW_OP_lit30";
12974     case DW_OP_lit31:
12975       return "DW_OP_lit31";
12976     case DW_OP_reg0:
12977       return "DW_OP_reg0";
12978     case DW_OP_reg1:
12979       return "DW_OP_reg1";
12980     case DW_OP_reg2:
12981       return "DW_OP_reg2";
12982     case DW_OP_reg3:
12983       return "DW_OP_reg3";
12984     case DW_OP_reg4:
12985       return "DW_OP_reg4";
12986     case DW_OP_reg5:
12987       return "DW_OP_reg5";
12988     case DW_OP_reg6:
12989       return "DW_OP_reg6";
12990     case DW_OP_reg7:
12991       return "DW_OP_reg7";
12992     case DW_OP_reg8:
12993       return "DW_OP_reg8";
12994     case DW_OP_reg9:
12995       return "DW_OP_reg9";
12996     case DW_OP_reg10:
12997       return "DW_OP_reg10";
12998     case DW_OP_reg11:
12999       return "DW_OP_reg11";
13000     case DW_OP_reg12:
13001       return "DW_OP_reg12";
13002     case DW_OP_reg13:
13003       return "DW_OP_reg13";
13004     case DW_OP_reg14:
13005       return "DW_OP_reg14";
13006     case DW_OP_reg15:
13007       return "DW_OP_reg15";
13008     case DW_OP_reg16:
13009       return "DW_OP_reg16";
13010     case DW_OP_reg17:
13011       return "DW_OP_reg17";
13012     case DW_OP_reg18:
13013       return "DW_OP_reg18";
13014     case DW_OP_reg19:
13015       return "DW_OP_reg19";
13016     case DW_OP_reg20:
13017       return "DW_OP_reg20";
13018     case DW_OP_reg21:
13019       return "DW_OP_reg21";
13020     case DW_OP_reg22:
13021       return "DW_OP_reg22";
13022     case DW_OP_reg23:
13023       return "DW_OP_reg23";
13024     case DW_OP_reg24:
13025       return "DW_OP_reg24";
13026     case DW_OP_reg25:
13027       return "DW_OP_reg25";
13028     case DW_OP_reg26:
13029       return "DW_OP_reg26";
13030     case DW_OP_reg27:
13031       return "DW_OP_reg27";
13032     case DW_OP_reg28:
13033       return "DW_OP_reg28";
13034     case DW_OP_reg29:
13035       return "DW_OP_reg29";
13036     case DW_OP_reg30:
13037       return "DW_OP_reg30";
13038     case DW_OP_reg31:
13039       return "DW_OP_reg31";
13040     case DW_OP_breg0:
13041       return "DW_OP_breg0";
13042     case DW_OP_breg1:
13043       return "DW_OP_breg1";
13044     case DW_OP_breg2:
13045       return "DW_OP_breg2";
13046     case DW_OP_breg3:
13047       return "DW_OP_breg3";
13048     case DW_OP_breg4:
13049       return "DW_OP_breg4";
13050     case DW_OP_breg5:
13051       return "DW_OP_breg5";
13052     case DW_OP_breg6:
13053       return "DW_OP_breg6";
13054     case DW_OP_breg7:
13055       return "DW_OP_breg7";
13056     case DW_OP_breg8:
13057       return "DW_OP_breg8";
13058     case DW_OP_breg9:
13059       return "DW_OP_breg9";
13060     case DW_OP_breg10:
13061       return "DW_OP_breg10";
13062     case DW_OP_breg11:
13063       return "DW_OP_breg11";
13064     case DW_OP_breg12:
13065       return "DW_OP_breg12";
13066     case DW_OP_breg13:
13067       return "DW_OP_breg13";
13068     case DW_OP_breg14:
13069       return "DW_OP_breg14";
13070     case DW_OP_breg15:
13071       return "DW_OP_breg15";
13072     case DW_OP_breg16:
13073       return "DW_OP_breg16";
13074     case DW_OP_breg17:
13075       return "DW_OP_breg17";
13076     case DW_OP_breg18:
13077       return "DW_OP_breg18";
13078     case DW_OP_breg19:
13079       return "DW_OP_breg19";
13080     case DW_OP_breg20:
13081       return "DW_OP_breg20";
13082     case DW_OP_breg21:
13083       return "DW_OP_breg21";
13084     case DW_OP_breg22:
13085       return "DW_OP_breg22";
13086     case DW_OP_breg23:
13087       return "DW_OP_breg23";
13088     case DW_OP_breg24:
13089       return "DW_OP_breg24";
13090     case DW_OP_breg25:
13091       return "DW_OP_breg25";
13092     case DW_OP_breg26:
13093       return "DW_OP_breg26";
13094     case DW_OP_breg27:
13095       return "DW_OP_breg27";
13096     case DW_OP_breg28:
13097       return "DW_OP_breg28";
13098     case DW_OP_breg29:
13099       return "DW_OP_breg29";
13100     case DW_OP_breg30:
13101       return "DW_OP_breg30";
13102     case DW_OP_breg31:
13103       return "DW_OP_breg31";
13104     case DW_OP_regx:
13105       return "DW_OP_regx";
13106     case DW_OP_fbreg:
13107       return "DW_OP_fbreg";
13108     case DW_OP_bregx:
13109       return "DW_OP_bregx";
13110     case DW_OP_piece:
13111       return "DW_OP_piece";
13112     case DW_OP_deref_size:
13113       return "DW_OP_deref_size";
13114     case DW_OP_xderef_size:
13115       return "DW_OP_xderef_size";
13116     case DW_OP_nop:
13117       return "DW_OP_nop";
13118     /* DWARF 3 extensions.  */
13119     case DW_OP_push_object_address:
13120       return "DW_OP_push_object_address";
13121     case DW_OP_call2:
13122       return "DW_OP_call2";
13123     case DW_OP_call4:
13124       return "DW_OP_call4";
13125     case DW_OP_call_ref:
13126       return "DW_OP_call_ref";
13127     case DW_OP_form_tls_address:
13128       return "DW_OP_form_tls_address";
13129     case DW_OP_call_frame_cfa:
13130       return "DW_OP_call_frame_cfa";
13131     case DW_OP_bit_piece:
13132       return "DW_OP_bit_piece";
13133     /* DWARF 4 extensions.  */
13134     case DW_OP_implicit_value:
13135       return "DW_OP_implicit_value";
13136     case DW_OP_stack_value:
13137       return "DW_OP_stack_value";
13138     /* GNU extensions.  */
13139     case DW_OP_GNU_push_tls_address:
13140       return "DW_OP_GNU_push_tls_address";
13141     case DW_OP_GNU_uninit:
13142       return "DW_OP_GNU_uninit";
13143     case DW_OP_GNU_implicit_pointer:
13144       return "DW_OP_GNU_implicit_pointer";
13145     default:
13146       return NULL;
13147     }
13148 }
13149
13150 static char *
13151 dwarf_bool_name (unsigned mybool)
13152 {
13153   if (mybool)
13154     return "TRUE";
13155   else
13156     return "FALSE";
13157 }
13158
13159 /* Convert a DWARF type code into its string name.  */
13160
13161 static char *
13162 dwarf_type_encoding_name (unsigned enc)
13163 {
13164   switch (enc)
13165     {
13166     case DW_ATE_void:
13167       return "DW_ATE_void";
13168     case DW_ATE_address:
13169       return "DW_ATE_address";
13170     case DW_ATE_boolean:
13171       return "DW_ATE_boolean";
13172     case DW_ATE_complex_float:
13173       return "DW_ATE_complex_float";
13174     case DW_ATE_float:
13175       return "DW_ATE_float";
13176     case DW_ATE_signed:
13177       return "DW_ATE_signed";
13178     case DW_ATE_signed_char:
13179       return "DW_ATE_signed_char";
13180     case DW_ATE_unsigned:
13181       return "DW_ATE_unsigned";
13182     case DW_ATE_unsigned_char:
13183       return "DW_ATE_unsigned_char";
13184     /* DWARF 3.  */
13185     case DW_ATE_imaginary_float:
13186       return "DW_ATE_imaginary_float";
13187     case DW_ATE_packed_decimal:
13188       return "DW_ATE_packed_decimal";
13189     case DW_ATE_numeric_string:
13190       return "DW_ATE_numeric_string";
13191     case DW_ATE_edited:
13192       return "DW_ATE_edited";
13193     case DW_ATE_signed_fixed:
13194       return "DW_ATE_signed_fixed";
13195     case DW_ATE_unsigned_fixed:
13196       return "DW_ATE_unsigned_fixed";
13197     case DW_ATE_decimal_float:
13198       return "DW_ATE_decimal_float";
13199     /* DWARF 4.  */
13200     case DW_ATE_UTF:
13201       return "DW_ATE_UTF";
13202     /* HP extensions.  */
13203     case DW_ATE_HP_float80:
13204       return "DW_ATE_HP_float80";
13205     case DW_ATE_HP_complex_float80:
13206       return "DW_ATE_HP_complex_float80";
13207     case DW_ATE_HP_float128:
13208       return "DW_ATE_HP_float128";
13209     case DW_ATE_HP_complex_float128:
13210       return "DW_ATE_HP_complex_float128";
13211     case DW_ATE_HP_floathpintel:
13212       return "DW_ATE_HP_floathpintel";
13213     case DW_ATE_HP_imaginary_float80:
13214       return "DW_ATE_HP_imaginary_float80";
13215     case DW_ATE_HP_imaginary_float128:
13216       return "DW_ATE_HP_imaginary_float128";
13217     default:
13218       return "DW_ATE_<unknown>";
13219     }
13220 }
13221
13222 /* Convert a DWARF call frame info operation to its string name.  */
13223
13224 #if 0
13225 static char *
13226 dwarf_cfi_name (unsigned cfi_opc)
13227 {
13228   switch (cfi_opc)
13229     {
13230     case DW_CFA_advance_loc:
13231       return "DW_CFA_advance_loc";
13232     case DW_CFA_offset:
13233       return "DW_CFA_offset";
13234     case DW_CFA_restore:
13235       return "DW_CFA_restore";
13236     case DW_CFA_nop:
13237       return "DW_CFA_nop";
13238     case DW_CFA_set_loc:
13239       return "DW_CFA_set_loc";
13240     case DW_CFA_advance_loc1:
13241       return "DW_CFA_advance_loc1";
13242     case DW_CFA_advance_loc2:
13243       return "DW_CFA_advance_loc2";
13244     case DW_CFA_advance_loc4:
13245       return "DW_CFA_advance_loc4";
13246     case DW_CFA_offset_extended:
13247       return "DW_CFA_offset_extended";
13248     case DW_CFA_restore_extended:
13249       return "DW_CFA_restore_extended";
13250     case DW_CFA_undefined:
13251       return "DW_CFA_undefined";
13252     case DW_CFA_same_value:
13253       return "DW_CFA_same_value";
13254     case DW_CFA_register:
13255       return "DW_CFA_register";
13256     case DW_CFA_remember_state:
13257       return "DW_CFA_remember_state";
13258     case DW_CFA_restore_state:
13259       return "DW_CFA_restore_state";
13260     case DW_CFA_def_cfa:
13261       return "DW_CFA_def_cfa";
13262     case DW_CFA_def_cfa_register:
13263       return "DW_CFA_def_cfa_register";
13264     case DW_CFA_def_cfa_offset:
13265       return "DW_CFA_def_cfa_offset";
13266     /* DWARF 3.  */
13267     case DW_CFA_def_cfa_expression:
13268       return "DW_CFA_def_cfa_expression";
13269     case DW_CFA_expression:
13270       return "DW_CFA_expression";
13271     case DW_CFA_offset_extended_sf:
13272       return "DW_CFA_offset_extended_sf";
13273     case DW_CFA_def_cfa_sf:
13274       return "DW_CFA_def_cfa_sf";
13275     case DW_CFA_def_cfa_offset_sf:
13276       return "DW_CFA_def_cfa_offset_sf";
13277     case DW_CFA_val_offset:
13278       return "DW_CFA_val_offset";
13279     case DW_CFA_val_offset_sf:
13280       return "DW_CFA_val_offset_sf";
13281     case DW_CFA_val_expression:
13282       return "DW_CFA_val_expression";
13283     /* SGI/MIPS specific.  */
13284     case DW_CFA_MIPS_advance_loc8:
13285       return "DW_CFA_MIPS_advance_loc8";
13286     /* GNU extensions.  */
13287     case DW_CFA_GNU_window_save:
13288       return "DW_CFA_GNU_window_save";
13289     case DW_CFA_GNU_args_size:
13290       return "DW_CFA_GNU_args_size";
13291     case DW_CFA_GNU_negative_offset_extended:
13292       return "DW_CFA_GNU_negative_offset_extended";
13293     default:
13294       return "DW_CFA_<unknown>";
13295     }
13296 }
13297 #endif
13298
13299 static void
13300 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13301 {
13302   unsigned int i;
13303
13304   print_spaces (indent, f);
13305   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13306            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13307
13308   if (die->parent != NULL)
13309     {
13310       print_spaces (indent, f);
13311       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13312                           die->parent->offset);
13313     }
13314
13315   print_spaces (indent, f);
13316   fprintf_unfiltered (f, "  has children: %s\n",
13317            dwarf_bool_name (die->child != NULL));
13318
13319   print_spaces (indent, f);
13320   fprintf_unfiltered (f, "  attributes:\n");
13321
13322   for (i = 0; i < die->num_attrs; ++i)
13323     {
13324       print_spaces (indent, f);
13325       fprintf_unfiltered (f, "    %s (%s) ",
13326                dwarf_attr_name (die->attrs[i].name),
13327                dwarf_form_name (die->attrs[i].form));
13328
13329       switch (die->attrs[i].form)
13330         {
13331         case DW_FORM_ref_addr:
13332         case DW_FORM_addr:
13333           fprintf_unfiltered (f, "address: ");
13334           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13335           break;
13336         case DW_FORM_block2:
13337         case DW_FORM_block4:
13338         case DW_FORM_block:
13339         case DW_FORM_block1:
13340           fprintf_unfiltered (f, "block: size %d",
13341                               DW_BLOCK (&die->attrs[i])->size);
13342           break;
13343         case DW_FORM_exprloc:
13344           fprintf_unfiltered (f, "expression: size %u",
13345                               DW_BLOCK (&die->attrs[i])->size);
13346           break;
13347         case DW_FORM_ref1:
13348         case DW_FORM_ref2:
13349         case DW_FORM_ref4:
13350           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13351                               (long) (DW_ADDR (&die->attrs[i])));
13352           break;
13353         case DW_FORM_data1:
13354         case DW_FORM_data2:
13355         case DW_FORM_data4:
13356         case DW_FORM_data8:
13357         case DW_FORM_udata:
13358         case DW_FORM_sdata:
13359           fprintf_unfiltered (f, "constant: %s",
13360                               pulongest (DW_UNSND (&die->attrs[i])));
13361           break;
13362         case DW_FORM_sec_offset:
13363           fprintf_unfiltered (f, "section offset: %s",
13364                               pulongest (DW_UNSND (&die->attrs[i])));
13365           break;
13366         case DW_FORM_ref_sig8:
13367           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13368             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13369                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13370           else
13371             fprintf_unfiltered (f, "signatured type, offset: unknown");
13372           break;
13373         case DW_FORM_string:
13374         case DW_FORM_strp:
13375           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13376                    DW_STRING (&die->attrs[i])
13377                    ? DW_STRING (&die->attrs[i]) : "",
13378                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13379           break;
13380         case DW_FORM_flag:
13381           if (DW_UNSND (&die->attrs[i]))
13382             fprintf_unfiltered (f, "flag: TRUE");
13383           else
13384             fprintf_unfiltered (f, "flag: FALSE");
13385           break;
13386         case DW_FORM_flag_present:
13387           fprintf_unfiltered (f, "flag: TRUE");
13388           break;
13389         case DW_FORM_indirect:
13390           /* The reader will have reduced the indirect form to
13391              the "base form" so this form should not occur.  */
13392           fprintf_unfiltered (f, 
13393                               "unexpected attribute form: DW_FORM_indirect");
13394           break;
13395         default:
13396           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13397                    die->attrs[i].form);
13398           break;
13399         }
13400       fprintf_unfiltered (f, "\n");
13401     }
13402 }
13403
13404 static void
13405 dump_die_for_error (struct die_info *die)
13406 {
13407   dump_die_shallow (gdb_stderr, 0, die);
13408 }
13409
13410 static void
13411 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13412 {
13413   int indent = level * 4;
13414
13415   gdb_assert (die != NULL);
13416
13417   if (level >= max_level)
13418     return;
13419
13420   dump_die_shallow (f, indent, die);
13421
13422   if (die->child != NULL)
13423     {
13424       print_spaces (indent, f);
13425       fprintf_unfiltered (f, "  Children:");
13426       if (level + 1 < max_level)
13427         {
13428           fprintf_unfiltered (f, "\n");
13429           dump_die_1 (f, level + 1, max_level, die->child);
13430         }
13431       else
13432         {
13433           fprintf_unfiltered (f,
13434                               " [not printed, max nesting level reached]\n");
13435         }
13436     }
13437
13438   if (die->sibling != NULL && level > 0)
13439     {
13440       dump_die_1 (f, level, max_level, die->sibling);
13441     }
13442 }
13443
13444 /* This is called from the pdie macro in gdbinit.in.
13445    It's not static so gcc will keep a copy callable from gdb.  */
13446
13447 void
13448 dump_die (struct die_info *die, int max_level)
13449 {
13450   dump_die_1 (gdb_stdlog, 0, max_level, die);
13451 }
13452
13453 static void
13454 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13455 {
13456   void **slot;
13457
13458   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13459
13460   *slot = die;
13461 }
13462
13463 static int
13464 is_ref_attr (struct attribute *attr)
13465 {
13466   switch (attr->form)
13467     {
13468     case DW_FORM_ref_addr:
13469     case DW_FORM_ref1:
13470     case DW_FORM_ref2:
13471     case DW_FORM_ref4:
13472     case DW_FORM_ref8:
13473     case DW_FORM_ref_udata:
13474       return 1;
13475     default:
13476       return 0;
13477     }
13478 }
13479
13480 static unsigned int
13481 dwarf2_get_ref_die_offset (struct attribute *attr)
13482 {
13483   if (is_ref_attr (attr))
13484     return DW_ADDR (attr);
13485
13486   complaint (&symfile_complaints,
13487              _("unsupported die ref attribute form: '%s'"),
13488              dwarf_form_name (attr->form));
13489   return 0;
13490 }
13491
13492 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13493  * the value held by the attribute is not constant.  */
13494
13495 static LONGEST
13496 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13497 {
13498   if (attr->form == DW_FORM_sdata)
13499     return DW_SND (attr);
13500   else if (attr->form == DW_FORM_udata
13501            || attr->form == DW_FORM_data1
13502            || attr->form == DW_FORM_data2
13503            || attr->form == DW_FORM_data4
13504            || attr->form == DW_FORM_data8)
13505     return DW_UNSND (attr);
13506   else
13507     {
13508       complaint (&symfile_complaints,
13509                  _("Attribute value is not a constant (%s)"),
13510                  dwarf_form_name (attr->form));
13511       return default_value;
13512     }
13513 }
13514
13515 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13516    unit and add it to our queue.
13517    The result is non-zero if PER_CU was queued, otherwise the result is zero
13518    meaning either PER_CU is already queued or it is already loaded.  */
13519
13520 static int
13521 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13522                        struct dwarf2_per_cu_data *per_cu)
13523 {
13524   /* We may arrive here during partial symbol reading, if we need full
13525      DIEs to process an unusual case (e.g. template arguments).  Do
13526      not queue PER_CU, just tell our caller to load its DIEs.  */
13527   if (dwarf2_per_objfile->reading_partial_symbols)
13528     {
13529       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13530         return 1;
13531       return 0;
13532     }
13533
13534   /* Mark the dependence relation so that we don't flush PER_CU
13535      too early.  */
13536   dwarf2_add_dependence (this_cu, per_cu);
13537
13538   /* If it's already on the queue, we have nothing to do.  */
13539   if (per_cu->queued)
13540     return 0;
13541
13542   /* If the compilation unit is already loaded, just mark it as
13543      used.  */
13544   if (per_cu->cu != NULL)
13545     {
13546       per_cu->cu->last_used = 0;
13547       return 0;
13548     }
13549
13550   /* Add it to the queue.  */
13551   queue_comp_unit (per_cu, this_cu->objfile);
13552
13553   return 1;
13554 }
13555
13556 /* Follow reference or signature attribute ATTR of SRC_DIE.
13557    On entry *REF_CU is the CU of SRC_DIE.
13558    On exit *REF_CU is the CU of the result.  */
13559
13560 static struct die_info *
13561 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13562                        struct dwarf2_cu **ref_cu)
13563 {
13564   struct die_info *die;
13565
13566   if (is_ref_attr (attr))
13567     die = follow_die_ref (src_die, attr, ref_cu);
13568   else if (attr->form == DW_FORM_ref_sig8)
13569     die = follow_die_sig (src_die, attr, ref_cu);
13570   else
13571     {
13572       dump_die_for_error (src_die);
13573       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13574              (*ref_cu)->objfile->name);
13575     }
13576
13577   return die;
13578 }
13579
13580 /* Follow reference OFFSET.
13581    On entry *REF_CU is the CU of the source die referencing OFFSET.
13582    On exit *REF_CU is the CU of the result.
13583    Returns NULL if OFFSET is invalid.  */
13584
13585 static struct die_info *
13586 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13587 {
13588   struct die_info temp_die;
13589   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13590
13591   gdb_assert (cu->per_cu != NULL);
13592
13593   target_cu = cu;
13594
13595   if (cu->per_cu->from_debug_types)
13596     {
13597       /* .debug_types CUs cannot reference anything outside their CU.
13598          If they need to, they have to reference a signatured type via
13599          DW_FORM_ref_sig8.  */
13600       if (! offset_in_cu_p (&cu->header, offset))
13601         return NULL;
13602     }
13603   else if (! offset_in_cu_p (&cu->header, offset))
13604     {
13605       struct dwarf2_per_cu_data *per_cu;
13606
13607       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13608
13609       /* If necessary, add it to the queue and load its DIEs.  */
13610       if (maybe_queue_comp_unit (cu, per_cu))
13611         load_full_comp_unit (per_cu, cu->objfile);
13612
13613       target_cu = per_cu->cu;
13614     }
13615   else if (cu->dies == NULL)
13616     {
13617       /* We're loading full DIEs during partial symbol reading.  */
13618       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13619       load_full_comp_unit (cu->per_cu, cu->objfile);
13620     }
13621
13622   *ref_cu = target_cu;
13623   temp_die.offset = offset;
13624   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13625 }
13626
13627 /* Follow reference attribute ATTR of SRC_DIE.
13628    On entry *REF_CU is the CU of SRC_DIE.
13629    On exit *REF_CU is the CU of the result.  */
13630
13631 static struct die_info *
13632 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13633                 struct dwarf2_cu **ref_cu)
13634 {
13635   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13636   struct dwarf2_cu *cu = *ref_cu;
13637   struct die_info *die;
13638
13639   die = follow_die_offset (offset, ref_cu);
13640   if (!die)
13641     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13642            "at 0x%x [in module %s]"),
13643            offset, src_die->offset, cu->objfile->name);
13644
13645   return die;
13646 }
13647
13648 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13649    value is intended for DW_OP_call*.  */
13650
13651 struct dwarf2_locexpr_baton
13652 dwarf2_fetch_die_location_block (unsigned int offset,
13653                                  struct dwarf2_per_cu_data *per_cu,
13654                                  CORE_ADDR (*get_frame_pc) (void *baton),
13655                                  void *baton)
13656 {
13657   struct dwarf2_cu *cu = per_cu->cu;
13658   struct die_info *die;
13659   struct attribute *attr;
13660   struct dwarf2_locexpr_baton retval;
13661
13662   dw2_setup (per_cu->objfile);
13663
13664   die = follow_die_offset (offset, &cu);
13665   if (!die)
13666     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13667            offset, per_cu->cu->objfile->name);
13668
13669   attr = dwarf2_attr (die, DW_AT_location, cu);
13670   if (!attr)
13671     {
13672       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13673
13674       retval.data = NULL;
13675       retval.size = 0;
13676     }
13677   else if (attr_form_is_section_offset (attr))
13678     {
13679       struct dwarf2_loclist_baton loclist_baton;
13680       CORE_ADDR pc = (*get_frame_pc) (baton);
13681       size_t size;
13682
13683       fill_in_loclist_baton (cu, &loclist_baton, attr);
13684
13685       retval.data = dwarf2_find_location_expression (&loclist_baton,
13686                                                      &size, pc);
13687       retval.size = size;
13688     }
13689   else
13690     {
13691       if (!attr_form_is_block (attr))
13692         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13693                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13694                offset, per_cu->cu->objfile->name);
13695
13696       retval.data = DW_BLOCK (attr)->data;
13697       retval.size = DW_BLOCK (attr)->size;
13698     }
13699   retval.per_cu = cu->per_cu;
13700   return retval;
13701 }
13702
13703 /* Follow the signature attribute ATTR in SRC_DIE.
13704    On entry *REF_CU is the CU of SRC_DIE.
13705    On exit *REF_CU is the CU of the result.  */
13706
13707 static struct die_info *
13708 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13709                 struct dwarf2_cu **ref_cu)
13710 {
13711   struct objfile *objfile = (*ref_cu)->objfile;
13712   struct die_info temp_die;
13713   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13714   struct dwarf2_cu *sig_cu;
13715   struct die_info *die;
13716
13717   /* sig_type will be NULL if the signatured type is missing from
13718      the debug info.  */
13719   if (sig_type == NULL)
13720     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13721              "at 0x%x [in module %s]"),
13722            src_die->offset, objfile->name);
13723
13724   /* If necessary, add it to the queue and load its DIEs.  */
13725
13726   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13727     read_signatured_type (objfile, sig_type);
13728
13729   gdb_assert (sig_type->per_cu.cu != NULL);
13730
13731   sig_cu = sig_type->per_cu.cu;
13732   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13733   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13734   if (die)
13735     {
13736       *ref_cu = sig_cu;
13737       return die;
13738     }
13739
13740   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13741          "from DIE at 0x%x [in module %s]"),
13742          sig_type->type_offset, src_die->offset, objfile->name);
13743 }
13744
13745 /* Given an offset of a signatured type, return its signatured_type.  */
13746
13747 static struct signatured_type *
13748 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13749 {
13750   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13751   unsigned int length, initial_length_size;
13752   unsigned int sig_offset;
13753   struct signatured_type find_entry, *type_sig;
13754
13755   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13756   sig_offset = (initial_length_size
13757                 + 2 /*version*/
13758                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13759                 + 1 /*address_size*/);
13760   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13761   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13762
13763   /* This is only used to lookup previously recorded types.
13764      If we didn't find it, it's our bug.  */
13765   gdb_assert (type_sig != NULL);
13766   gdb_assert (offset == type_sig->offset);
13767
13768   return type_sig;
13769 }
13770
13771 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13772
13773 static void
13774 read_signatured_type_at_offset (struct objfile *objfile,
13775                                 unsigned int offset)
13776 {
13777   struct signatured_type *type_sig;
13778
13779   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13780
13781   /* We have the section offset, but we need the signature to do the
13782      hash table lookup.  */
13783   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13784
13785   gdb_assert (type_sig->per_cu.cu == NULL);
13786
13787   read_signatured_type (objfile, type_sig);
13788
13789   gdb_assert (type_sig->per_cu.cu != NULL);
13790 }
13791
13792 /* Read in a signatured type and build its CU and DIEs.  */
13793
13794 static void
13795 read_signatured_type (struct objfile *objfile,
13796                       struct signatured_type *type_sig)
13797 {
13798   gdb_byte *types_ptr;
13799   struct die_reader_specs reader_specs;
13800   struct dwarf2_cu *cu;
13801   ULONGEST signature;
13802   struct cleanup *back_to, *free_cu_cleanup;
13803
13804   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13805   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13806
13807   gdb_assert (type_sig->per_cu.cu == NULL);
13808
13809   cu = xmalloc (sizeof (*cu));
13810   init_one_comp_unit (cu, objfile);
13811
13812   type_sig->per_cu.cu = cu;
13813   cu->per_cu = &type_sig->per_cu;
13814
13815   /* If an error occurs while loading, release our storage.  */
13816   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13817
13818   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13819                                         types_ptr, objfile->obfd);
13820   gdb_assert (signature == type_sig->signature);
13821
13822   cu->die_hash
13823     = htab_create_alloc_ex (cu->header.length / 12,
13824                             die_hash,
13825                             die_eq,
13826                             NULL,
13827                             &cu->comp_unit_obstack,
13828                             hashtab_obstack_allocate,
13829                             dummy_obstack_deallocate);
13830
13831   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13832   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13833
13834   init_cu_die_reader (&reader_specs, cu);
13835
13836   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13837                                     NULL /*parent*/);
13838
13839   /* We try not to read any attributes in this function, because not
13840      all objfiles needed for references have been loaded yet, and symbol
13841      table processing isn't initialized.  But we have to set the CU language,
13842      or we won't be able to build types correctly.  */
13843   prepare_one_comp_unit (cu, cu->dies);
13844
13845   do_cleanups (back_to);
13846
13847   /* We've successfully allocated this compilation unit.  Let our caller
13848      clean it up when finished with it.  */
13849   discard_cleanups (free_cu_cleanup);
13850
13851   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13852   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13853 }
13854
13855 /* Decode simple location descriptions.
13856    Given a pointer to a dwarf block that defines a location, compute
13857    the location and return the value.
13858
13859    NOTE drow/2003-11-18: This function is called in two situations
13860    now: for the address of static or global variables (partial symbols
13861    only) and for offsets into structures which are expected to be
13862    (more or less) constant.  The partial symbol case should go away,
13863    and only the constant case should remain.  That will let this
13864    function complain more accurately.  A few special modes are allowed
13865    without complaint for global variables (for instance, global
13866    register values and thread-local values).
13867
13868    A location description containing no operations indicates that the
13869    object is optimized out.  The return value is 0 for that case.
13870    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13871    callers will only want a very basic result and this can become a
13872    complaint.
13873
13874    Note that stack[0] is unused except as a default error return.  */
13875
13876 static CORE_ADDR
13877 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13878 {
13879   struct objfile *objfile = cu->objfile;
13880   int i;
13881   int size = blk->size;
13882   gdb_byte *data = blk->data;
13883   CORE_ADDR stack[64];
13884   int stacki;
13885   unsigned int bytes_read, unsnd;
13886   gdb_byte op;
13887
13888   i = 0;
13889   stacki = 0;
13890   stack[stacki] = 0;
13891   stack[++stacki] = 0;
13892
13893   while (i < size)
13894     {
13895       op = data[i++];
13896       switch (op)
13897         {
13898         case DW_OP_lit0:
13899         case DW_OP_lit1:
13900         case DW_OP_lit2:
13901         case DW_OP_lit3:
13902         case DW_OP_lit4:
13903         case DW_OP_lit5:
13904         case DW_OP_lit6:
13905         case DW_OP_lit7:
13906         case DW_OP_lit8:
13907         case DW_OP_lit9:
13908         case DW_OP_lit10:
13909         case DW_OP_lit11:
13910         case DW_OP_lit12:
13911         case DW_OP_lit13:
13912         case DW_OP_lit14:
13913         case DW_OP_lit15:
13914         case DW_OP_lit16:
13915         case DW_OP_lit17:
13916         case DW_OP_lit18:
13917         case DW_OP_lit19:
13918         case DW_OP_lit20:
13919         case DW_OP_lit21:
13920         case DW_OP_lit22:
13921         case DW_OP_lit23:
13922         case DW_OP_lit24:
13923         case DW_OP_lit25:
13924         case DW_OP_lit26:
13925         case DW_OP_lit27:
13926         case DW_OP_lit28:
13927         case DW_OP_lit29:
13928         case DW_OP_lit30:
13929         case DW_OP_lit31:
13930           stack[++stacki] = op - DW_OP_lit0;
13931           break;
13932
13933         case DW_OP_reg0:
13934         case DW_OP_reg1:
13935         case DW_OP_reg2:
13936         case DW_OP_reg3:
13937         case DW_OP_reg4:
13938         case DW_OP_reg5:
13939         case DW_OP_reg6:
13940         case DW_OP_reg7:
13941         case DW_OP_reg8:
13942         case DW_OP_reg9:
13943         case DW_OP_reg10:
13944         case DW_OP_reg11:
13945         case DW_OP_reg12:
13946         case DW_OP_reg13:
13947         case DW_OP_reg14:
13948         case DW_OP_reg15:
13949         case DW_OP_reg16:
13950         case DW_OP_reg17:
13951         case DW_OP_reg18:
13952         case DW_OP_reg19:
13953         case DW_OP_reg20:
13954         case DW_OP_reg21:
13955         case DW_OP_reg22:
13956         case DW_OP_reg23:
13957         case DW_OP_reg24:
13958         case DW_OP_reg25:
13959         case DW_OP_reg26:
13960         case DW_OP_reg27:
13961         case DW_OP_reg28:
13962         case DW_OP_reg29:
13963         case DW_OP_reg30:
13964         case DW_OP_reg31:
13965           stack[++stacki] = op - DW_OP_reg0;
13966           if (i < size)
13967             dwarf2_complex_location_expr_complaint ();
13968           break;
13969
13970         case DW_OP_regx:
13971           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13972           i += bytes_read;
13973           stack[++stacki] = unsnd;
13974           if (i < size)
13975             dwarf2_complex_location_expr_complaint ();
13976           break;
13977
13978         case DW_OP_addr:
13979           stack[++stacki] = read_address (objfile->obfd, &data[i],
13980                                           cu, &bytes_read);
13981           i += bytes_read;
13982           break;
13983
13984         case DW_OP_const1u:
13985           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13986           i += 1;
13987           break;
13988
13989         case DW_OP_const1s:
13990           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13991           i += 1;
13992           break;
13993
13994         case DW_OP_const2u:
13995           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13996           i += 2;
13997           break;
13998
13999         case DW_OP_const2s:
14000           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14001           i += 2;
14002           break;
14003
14004         case DW_OP_const4u:
14005           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14006           i += 4;
14007           break;
14008
14009         case DW_OP_const4s:
14010           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14011           i += 4;
14012           break;
14013
14014         case DW_OP_constu:
14015           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14016                                                   &bytes_read);
14017           i += bytes_read;
14018           break;
14019
14020         case DW_OP_consts:
14021           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14022           i += bytes_read;
14023           break;
14024
14025         case DW_OP_dup:
14026           stack[stacki + 1] = stack[stacki];
14027           stacki++;
14028           break;
14029
14030         case DW_OP_plus:
14031           stack[stacki - 1] += stack[stacki];
14032           stacki--;
14033           break;
14034
14035         case DW_OP_plus_uconst:
14036           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14037                                                  &bytes_read);
14038           i += bytes_read;
14039           break;
14040
14041         case DW_OP_minus:
14042           stack[stacki - 1] -= stack[stacki];
14043           stacki--;
14044           break;
14045
14046         case DW_OP_deref:
14047           /* If we're not the last op, then we definitely can't encode
14048              this using GDB's address_class enum.  This is valid for partial
14049              global symbols, although the variable's address will be bogus
14050              in the psymtab.  */
14051           if (i < size)
14052             dwarf2_complex_location_expr_complaint ();
14053           break;
14054
14055         case DW_OP_GNU_push_tls_address:
14056           /* The top of the stack has the offset from the beginning
14057              of the thread control block at which the variable is located.  */
14058           /* Nothing should follow this operator, so the top of stack would
14059              be returned.  */
14060           /* This is valid for partial global symbols, but the variable's
14061              address will be bogus in the psymtab.  */
14062           if (i < size)
14063             dwarf2_complex_location_expr_complaint ();
14064           break;
14065
14066         case DW_OP_GNU_uninit:
14067           break;
14068
14069         default:
14070           {
14071             const char *name = dwarf_stack_op_name (op);
14072
14073             if (name)
14074               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14075                          name);
14076             else
14077               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14078                          op);
14079           }
14080
14081           return (stack[stacki]);
14082         }
14083
14084       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14085          outside of the allocated space.  Also enforce minimum>0.  */
14086       if (stacki >= ARRAY_SIZE (stack) - 1)
14087         {
14088           complaint (&symfile_complaints,
14089                      _("location description stack overflow"));
14090           return 0;
14091         }
14092
14093       if (stacki <= 0)
14094         {
14095           complaint (&symfile_complaints,
14096                      _("location description stack underflow"));
14097           return 0;
14098         }
14099     }
14100   return (stack[stacki]);
14101 }
14102
14103 /* memory allocation interface */
14104
14105 static struct dwarf_block *
14106 dwarf_alloc_block (struct dwarf2_cu *cu)
14107 {
14108   struct dwarf_block *blk;
14109
14110   blk = (struct dwarf_block *)
14111     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14112   return (blk);
14113 }
14114
14115 static struct abbrev_info *
14116 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14117 {
14118   struct abbrev_info *abbrev;
14119
14120   abbrev = (struct abbrev_info *)
14121     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14122   memset (abbrev, 0, sizeof (struct abbrev_info));
14123   return (abbrev);
14124 }
14125
14126 static struct die_info *
14127 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14128 {
14129   struct die_info *die;
14130   size_t size = sizeof (struct die_info);
14131
14132   if (num_attrs > 1)
14133     size += (num_attrs - 1) * sizeof (struct attribute);
14134
14135   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14136   memset (die, 0, sizeof (struct die_info));
14137   return (die);
14138 }
14139
14140 \f
14141 /* Macro support.  */
14142
14143 /* Return the full name of file number I in *LH's file name table.
14144    Use COMP_DIR as the name of the current directory of the
14145    compilation.  The result is allocated using xmalloc; the caller is
14146    responsible for freeing it.  */
14147 static char *
14148 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14149 {
14150   /* Is the file number a valid index into the line header's file name
14151      table?  Remember that file numbers start with one, not zero.  */
14152   if (1 <= file && file <= lh->num_file_names)
14153     {
14154       struct file_entry *fe = &lh->file_names[file - 1];
14155
14156       if (IS_ABSOLUTE_PATH (fe->name))
14157         return xstrdup (fe->name);
14158       else
14159         {
14160           const char *dir;
14161           int dir_len;
14162           char *full_name;
14163
14164           if (fe->dir_index)
14165             dir = lh->include_dirs[fe->dir_index - 1];
14166           else
14167             dir = comp_dir;
14168
14169           if (dir)
14170             {
14171               dir_len = strlen (dir);
14172               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14173               strcpy (full_name, dir);
14174               full_name[dir_len] = '/';
14175               strcpy (full_name + dir_len + 1, fe->name);
14176               return full_name;
14177             }
14178           else
14179             return xstrdup (fe->name);
14180         }
14181     }
14182   else
14183     {
14184       /* The compiler produced a bogus file number.  We can at least
14185          record the macro definitions made in the file, even if we
14186          won't be able to find the file by name.  */
14187       char fake_name[80];
14188
14189       sprintf (fake_name, "<bad macro file number %d>", file);
14190
14191       complaint (&symfile_complaints,
14192                  _("bad file number in macro information (%d)"),
14193                  file);
14194
14195       return xstrdup (fake_name);
14196     }
14197 }
14198
14199
14200 static struct macro_source_file *
14201 macro_start_file (int file, int line,
14202                   struct macro_source_file *current_file,
14203                   const char *comp_dir,
14204                   struct line_header *lh, struct objfile *objfile)
14205 {
14206   /* The full name of this source file.  */
14207   char *full_name = file_full_name (file, lh, comp_dir);
14208
14209   /* We don't create a macro table for this compilation unit
14210      at all until we actually get a filename.  */
14211   if (! pending_macros)
14212     pending_macros = new_macro_table (&objfile->objfile_obstack,
14213                                       objfile->macro_cache);
14214
14215   if (! current_file)
14216     /* If we have no current file, then this must be the start_file
14217        directive for the compilation unit's main source file.  */
14218     current_file = macro_set_main (pending_macros, full_name);
14219   else
14220     current_file = macro_include (current_file, line, full_name);
14221
14222   xfree (full_name);
14223
14224   return current_file;
14225 }
14226
14227
14228 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14229    followed by a null byte.  */
14230 static char *
14231 copy_string (const char *buf, int len)
14232 {
14233   char *s = xmalloc (len + 1);
14234
14235   memcpy (s, buf, len);
14236   s[len] = '\0';
14237   return s;
14238 }
14239
14240
14241 static const char *
14242 consume_improper_spaces (const char *p, const char *body)
14243 {
14244   if (*p == ' ')
14245     {
14246       complaint (&symfile_complaints,
14247                  _("macro definition contains spaces "
14248                    "in formal argument list:\n`%s'"),
14249                  body);
14250
14251       while (*p == ' ')
14252         p++;
14253     }
14254
14255   return p;
14256 }
14257
14258
14259 static void
14260 parse_macro_definition (struct macro_source_file *file, int line,
14261                         const char *body)
14262 {
14263   const char *p;
14264
14265   /* The body string takes one of two forms.  For object-like macro
14266      definitions, it should be:
14267
14268         <macro name> " " <definition>
14269
14270      For function-like macro definitions, it should be:
14271
14272         <macro name> "() " <definition>
14273      or
14274         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14275
14276      Spaces may appear only where explicitly indicated, and in the
14277      <definition>.
14278
14279      The Dwarf 2 spec says that an object-like macro's name is always
14280      followed by a space, but versions of GCC around March 2002 omit
14281      the space when the macro's definition is the empty string.
14282
14283      The Dwarf 2 spec says that there should be no spaces between the
14284      formal arguments in a function-like macro's formal argument list,
14285      but versions of GCC around March 2002 include spaces after the
14286      commas.  */
14287
14288
14289   /* Find the extent of the macro name.  The macro name is terminated
14290      by either a space or null character (for an object-like macro) or
14291      an opening paren (for a function-like macro).  */
14292   for (p = body; *p; p++)
14293     if (*p == ' ' || *p == '(')
14294       break;
14295
14296   if (*p == ' ' || *p == '\0')
14297     {
14298       /* It's an object-like macro.  */
14299       int name_len = p - body;
14300       char *name = copy_string (body, name_len);
14301       const char *replacement;
14302
14303       if (*p == ' ')
14304         replacement = body + name_len + 1;
14305       else
14306         {
14307           dwarf2_macro_malformed_definition_complaint (body);
14308           replacement = body + name_len;
14309         }
14310
14311       macro_define_object (file, line, name, replacement);
14312
14313       xfree (name);
14314     }
14315   else if (*p == '(')
14316     {
14317       /* It's a function-like macro.  */
14318       char *name = copy_string (body, p - body);
14319       int argc = 0;
14320       int argv_size = 1;
14321       char **argv = xmalloc (argv_size * sizeof (*argv));
14322
14323       p++;
14324
14325       p = consume_improper_spaces (p, body);
14326
14327       /* Parse the formal argument list.  */
14328       while (*p && *p != ')')
14329         {
14330           /* Find the extent of the current argument name.  */
14331           const char *arg_start = p;
14332
14333           while (*p && *p != ',' && *p != ')' && *p != ' ')
14334             p++;
14335
14336           if (! *p || p == arg_start)
14337             dwarf2_macro_malformed_definition_complaint (body);
14338           else
14339             {
14340               /* Make sure argv has room for the new argument.  */
14341               if (argc >= argv_size)
14342                 {
14343                   argv_size *= 2;
14344                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14345                 }
14346
14347               argv[argc++] = copy_string (arg_start, p - arg_start);
14348             }
14349
14350           p = consume_improper_spaces (p, body);
14351
14352           /* Consume the comma, if present.  */
14353           if (*p == ',')
14354             {
14355               p++;
14356
14357               p = consume_improper_spaces (p, body);
14358             }
14359         }
14360
14361       if (*p == ')')
14362         {
14363           p++;
14364
14365           if (*p == ' ')
14366             /* Perfectly formed definition, no complaints.  */
14367             macro_define_function (file, line, name,
14368                                    argc, (const char **) argv,
14369                                    p + 1);
14370           else if (*p == '\0')
14371             {
14372               /* Complain, but do define it.  */
14373               dwarf2_macro_malformed_definition_complaint (body);
14374               macro_define_function (file, line, name,
14375                                      argc, (const char **) argv,
14376                                      p);
14377             }
14378           else
14379             /* Just complain.  */
14380             dwarf2_macro_malformed_definition_complaint (body);
14381         }
14382       else
14383         /* Just complain.  */
14384         dwarf2_macro_malformed_definition_complaint (body);
14385
14386       xfree (name);
14387       {
14388         int i;
14389
14390         for (i = 0; i < argc; i++)
14391           xfree (argv[i]);
14392       }
14393       xfree (argv);
14394     }
14395   else
14396     dwarf2_macro_malformed_definition_complaint (body);
14397 }
14398
14399
14400 static void
14401 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14402                      char *comp_dir, bfd *abfd,
14403                      struct dwarf2_cu *cu)
14404 {
14405   gdb_byte *mac_ptr, *mac_end;
14406   struct macro_source_file *current_file = 0;
14407   enum dwarf_macinfo_record_type macinfo_type;
14408   int at_commandline;
14409
14410   dwarf2_read_section (dwarf2_per_objfile->objfile,
14411                        &dwarf2_per_objfile->macinfo);
14412   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14413     {
14414       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14415       return;
14416     }
14417
14418   /* First pass: Find the name of the base filename.
14419      This filename is needed in order to process all macros whose definition
14420      (or undefinition) comes from the command line.  These macros are defined
14421      before the first DW_MACINFO_start_file entry, and yet still need to be
14422      associated to the base file.
14423
14424      To determine the base file name, we scan the macro definitions until we
14425      reach the first DW_MACINFO_start_file entry.  We then initialize
14426      CURRENT_FILE accordingly so that any macro definition found before the
14427      first DW_MACINFO_start_file can still be associated to the base file.  */
14428
14429   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14430   mac_end = dwarf2_per_objfile->macinfo.buffer
14431     + dwarf2_per_objfile->macinfo.size;
14432
14433   do
14434     {
14435       /* Do we at least have room for a macinfo type byte?  */
14436       if (mac_ptr >= mac_end)
14437         {
14438           /* Complaint is printed during the second pass as GDB will probably
14439              stop the first pass earlier upon finding
14440              DW_MACINFO_start_file.  */
14441           break;
14442         }
14443
14444       macinfo_type = read_1_byte (abfd, mac_ptr);
14445       mac_ptr++;
14446
14447       switch (macinfo_type)
14448         {
14449           /* A zero macinfo type indicates the end of the macro
14450              information.  */
14451         case 0:
14452           break;
14453
14454         case DW_MACINFO_define:
14455         case DW_MACINFO_undef:
14456           /* Only skip the data by MAC_PTR.  */
14457           {
14458             unsigned int bytes_read;
14459
14460             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14461             mac_ptr += bytes_read;
14462             read_direct_string (abfd, mac_ptr, &bytes_read);
14463             mac_ptr += bytes_read;
14464           }
14465           break;
14466
14467         case DW_MACINFO_start_file:
14468           {
14469             unsigned int bytes_read;
14470             int line, file;
14471
14472             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14473             mac_ptr += bytes_read;
14474             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14475             mac_ptr += bytes_read;
14476
14477             current_file = macro_start_file (file, line, current_file,
14478                                              comp_dir, lh, cu->objfile);
14479           }
14480           break;
14481
14482         case DW_MACINFO_end_file:
14483           /* No data to skip by MAC_PTR.  */
14484           break;
14485
14486         case DW_MACINFO_vendor_ext:
14487           /* Only skip the data by MAC_PTR.  */
14488           {
14489             unsigned int bytes_read;
14490
14491             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14492             mac_ptr += bytes_read;
14493             read_direct_string (abfd, mac_ptr, &bytes_read);
14494             mac_ptr += bytes_read;
14495           }
14496           break;
14497
14498         default:
14499           break;
14500         }
14501     } while (macinfo_type != 0 && current_file == NULL);
14502
14503   /* Second pass: Process all entries.
14504
14505      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14506      command-line macro definitions/undefinitions.  This flag is unset when we
14507      reach the first DW_MACINFO_start_file entry.  */
14508
14509   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14510
14511   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14512      GDB is still reading the definitions from command line.  First
14513      DW_MACINFO_start_file will need to be ignored as it was already executed
14514      to create CURRENT_FILE for the main source holding also the command line
14515      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14516      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14517
14518   at_commandline = 1;
14519
14520   do
14521     {
14522       /* Do we at least have room for a macinfo type byte?  */
14523       if (mac_ptr >= mac_end)
14524         {
14525           dwarf2_macros_too_long_complaint ();
14526           break;
14527         }
14528
14529       macinfo_type = read_1_byte (abfd, mac_ptr);
14530       mac_ptr++;
14531
14532       switch (macinfo_type)
14533         {
14534           /* A zero macinfo type indicates the end of the macro
14535              information.  */
14536         case 0:
14537           break;
14538
14539         case DW_MACINFO_define:
14540         case DW_MACINFO_undef:
14541           {
14542             unsigned int bytes_read;
14543             int line;
14544             char *body;
14545
14546             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14547             mac_ptr += bytes_read;
14548             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14549             mac_ptr += bytes_read;
14550
14551             if (! current_file)
14552               {
14553                 /* DWARF violation as no main source is present.  */
14554                 complaint (&symfile_complaints,
14555                            _("debug info with no main source gives macro %s "
14556                              "on line %d: %s"),
14557                            macinfo_type == DW_MACINFO_define ?
14558                              _("definition") :
14559                                macinfo_type == DW_MACINFO_undef ?
14560                                  _("undefinition") :
14561                                  _("something-or-other"), line, body);
14562                 break;
14563               }
14564             if ((line == 0 && !at_commandline)
14565                 || (line != 0 && at_commandline))
14566               complaint (&symfile_complaints,
14567                          _("debug info gives %s macro %s with %s line %d: %s"),
14568                          at_commandline ? _("command-line") : _("in-file"),
14569                          macinfo_type == DW_MACINFO_define ?
14570                            _("definition") :
14571                              macinfo_type == DW_MACINFO_undef ?
14572                                _("undefinition") :
14573                                _("something-or-other"),
14574                          line == 0 ? _("zero") : _("non-zero"), line, body);
14575
14576             if (macinfo_type == DW_MACINFO_define)
14577               parse_macro_definition (current_file, line, body);
14578             else if (macinfo_type == DW_MACINFO_undef)
14579               macro_undef (current_file, line, body);
14580           }
14581           break;
14582
14583         case DW_MACINFO_start_file:
14584           {
14585             unsigned int bytes_read;
14586             int line, file;
14587
14588             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14589             mac_ptr += bytes_read;
14590             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14591             mac_ptr += bytes_read;
14592
14593             if ((line == 0 && !at_commandline)
14594                 || (line != 0 && at_commandline))
14595               complaint (&symfile_complaints,
14596                          _("debug info gives source %d included "
14597                            "from %s at %s line %d"),
14598                          file, at_commandline ? _("command-line") : _("file"),
14599                          line == 0 ? _("zero") : _("non-zero"), line);
14600
14601             if (at_commandline)
14602               {
14603                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14604                 at_commandline = 0;
14605               }
14606             else
14607               current_file = macro_start_file (file, line,
14608                                                current_file, comp_dir,
14609                                                lh, cu->objfile);
14610           }
14611           break;
14612
14613         case DW_MACINFO_end_file:
14614           if (! current_file)
14615             complaint (&symfile_complaints,
14616                        _("macro debug info has an unmatched "
14617                          "`close_file' directive"));
14618           else
14619             {
14620               current_file = current_file->included_by;
14621               if (! current_file)
14622                 {
14623                   enum dwarf_macinfo_record_type next_type;
14624
14625                   /* GCC circa March 2002 doesn't produce the zero
14626                      type byte marking the end of the compilation
14627                      unit.  Complain if it's not there, but exit no
14628                      matter what.  */
14629
14630                   /* Do we at least have room for a macinfo type byte?  */
14631                   if (mac_ptr >= mac_end)
14632                     {
14633                       dwarf2_macros_too_long_complaint ();
14634                       return;
14635                     }
14636
14637                   /* We don't increment mac_ptr here, so this is just
14638                      a look-ahead.  */
14639                   next_type = read_1_byte (abfd, mac_ptr);
14640                   if (next_type != 0)
14641                     complaint (&symfile_complaints,
14642                                _("no terminating 0-type entry for "
14643                                  "macros in `.debug_macinfo' section"));
14644
14645                   return;
14646                 }
14647             }
14648           break;
14649
14650         case DW_MACINFO_vendor_ext:
14651           {
14652             unsigned int bytes_read;
14653             int constant;
14654
14655             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14656             mac_ptr += bytes_read;
14657             read_direct_string (abfd, mac_ptr, &bytes_read);
14658             mac_ptr += bytes_read;
14659
14660             /* We don't recognize any vendor extensions.  */
14661           }
14662           break;
14663         }
14664     } while (macinfo_type != 0);
14665 }
14666
14667 /* Check if the attribute's form is a DW_FORM_block*
14668    if so return true else false.  */
14669 static int
14670 attr_form_is_block (struct attribute *attr)
14671 {
14672   return (attr == NULL ? 0 :
14673       attr->form == DW_FORM_block1
14674       || attr->form == DW_FORM_block2
14675       || attr->form == DW_FORM_block4
14676       || attr->form == DW_FORM_block
14677       || attr->form == DW_FORM_exprloc);
14678 }
14679
14680 /* Return non-zero if ATTR's value is a section offset --- classes
14681    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14682    You may use DW_UNSND (attr) to retrieve such offsets.
14683
14684    Section 7.5.4, "Attribute Encodings", explains that no attribute
14685    may have a value that belongs to more than one of these classes; it
14686    would be ambiguous if we did, because we use the same forms for all
14687    of them.  */
14688 static int
14689 attr_form_is_section_offset (struct attribute *attr)
14690 {
14691   return (attr->form == DW_FORM_data4
14692           || attr->form == DW_FORM_data8
14693           || attr->form == DW_FORM_sec_offset);
14694 }
14695
14696
14697 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14698    zero otherwise.  When this function returns true, you can apply
14699    dwarf2_get_attr_constant_value to it.
14700
14701    However, note that for some attributes you must check
14702    attr_form_is_section_offset before using this test.  DW_FORM_data4
14703    and DW_FORM_data8 are members of both the constant class, and of
14704    the classes that contain offsets into other debug sections
14705    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14706    that, if an attribute's can be either a constant or one of the
14707    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14708    taken as section offsets, not constants.  */
14709 static int
14710 attr_form_is_constant (struct attribute *attr)
14711 {
14712   switch (attr->form)
14713     {
14714     case DW_FORM_sdata:
14715     case DW_FORM_udata:
14716     case DW_FORM_data1:
14717     case DW_FORM_data2:
14718     case DW_FORM_data4:
14719     case DW_FORM_data8:
14720       return 1;
14721     default:
14722       return 0;
14723     }
14724 }
14725
14726 /* A helper function that fills in a dwarf2_loclist_baton.  */
14727
14728 static void
14729 fill_in_loclist_baton (struct dwarf2_cu *cu,
14730                        struct dwarf2_loclist_baton *baton,
14731                        struct attribute *attr)
14732 {
14733   dwarf2_read_section (dwarf2_per_objfile->objfile,
14734                        &dwarf2_per_objfile->loc);
14735
14736   baton->per_cu = cu->per_cu;
14737   gdb_assert (baton->per_cu);
14738   /* We don't know how long the location list is, but make sure we
14739      don't run off the edge of the section.  */
14740   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14741   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14742   baton->base_address = cu->base_address;
14743 }
14744
14745 static void
14746 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14747                              struct dwarf2_cu *cu)
14748 {
14749   if (attr_form_is_section_offset (attr)
14750       /* ".debug_loc" may not exist at all, or the offset may be outside
14751          the section.  If so, fall through to the complaint in the
14752          other branch.  */
14753       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14754                                                 &dwarf2_per_objfile->loc))
14755     {
14756       struct dwarf2_loclist_baton *baton;
14757
14758       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14759                              sizeof (struct dwarf2_loclist_baton));
14760
14761       fill_in_loclist_baton (cu, baton, attr);
14762
14763       if (cu->base_known == 0)
14764         complaint (&symfile_complaints,
14765                    _("Location list used without "
14766                      "specifying the CU base address."));
14767
14768       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14769       SYMBOL_LOCATION_BATON (sym) = baton;
14770     }
14771   else
14772     {
14773       struct dwarf2_locexpr_baton *baton;
14774
14775       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14776                              sizeof (struct dwarf2_locexpr_baton));
14777       baton->per_cu = cu->per_cu;
14778       gdb_assert (baton->per_cu);
14779
14780       if (attr_form_is_block (attr))
14781         {
14782           /* Note that we're just copying the block's data pointer
14783              here, not the actual data.  We're still pointing into the
14784              info_buffer for SYM's objfile; right now we never release
14785              that buffer, but when we do clean up properly this may
14786              need to change.  */
14787           baton->size = DW_BLOCK (attr)->size;
14788           baton->data = DW_BLOCK (attr)->data;
14789         }
14790       else
14791         {
14792           dwarf2_invalid_attrib_class_complaint ("location description",
14793                                                  SYMBOL_NATURAL_NAME (sym));
14794           baton->size = 0;
14795           baton->data = NULL;
14796         }
14797
14798       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14799       SYMBOL_LOCATION_BATON (sym) = baton;
14800     }
14801 }
14802
14803 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14804    came from a separate debuginfo file, then the master objfile is
14805    returned.  */
14806
14807 struct objfile *
14808 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14809 {
14810   struct objfile *objfile = per_cu->objfile;
14811
14812   /* Return the master objfile, so that we can report and look up the
14813      correct file containing this variable.  */
14814   if (objfile->separate_debug_objfile_backlink)
14815     objfile = objfile->separate_debug_objfile_backlink;
14816
14817   return objfile;
14818 }
14819
14820 /* Return the address size given in the compilation unit header for CU.  */
14821
14822 CORE_ADDR
14823 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14824 {
14825   if (per_cu->cu)
14826     return per_cu->cu->header.addr_size;
14827   else
14828     {
14829       /* If the CU is not currently read in, we re-read its header.  */
14830       struct objfile *objfile = per_cu->objfile;
14831       struct dwarf2_per_objfile *per_objfile
14832         = objfile_data (objfile, dwarf2_objfile_data_key);
14833       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14834       struct comp_unit_head cu_header;
14835
14836       memset (&cu_header, 0, sizeof cu_header);
14837       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14838       return cu_header.addr_size;
14839     }
14840 }
14841
14842 /* Return the offset size given in the compilation unit header for CU.  */
14843
14844 int
14845 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14846 {
14847   if (per_cu->cu)
14848     return per_cu->cu->header.offset_size;
14849   else
14850     {
14851       /* If the CU is not currently read in, we re-read its header.  */
14852       struct objfile *objfile = per_cu->objfile;
14853       struct dwarf2_per_objfile *per_objfile
14854         = objfile_data (objfile, dwarf2_objfile_data_key);
14855       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14856       struct comp_unit_head cu_header;
14857
14858       memset (&cu_header, 0, sizeof cu_header);
14859       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14860       return cu_header.offset_size;
14861     }
14862 }
14863
14864 /* Return the text offset of the CU.  The returned offset comes from
14865    this CU's objfile.  If this objfile came from a separate debuginfo
14866    file, then the offset may be different from the corresponding
14867    offset in the parent objfile.  */
14868
14869 CORE_ADDR
14870 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14871 {
14872   struct objfile *objfile = per_cu->objfile;
14873
14874   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14875 }
14876
14877 /* Locate the .debug_info compilation unit from CU's objfile which contains
14878    the DIE at OFFSET.  Raises an error on failure.  */
14879
14880 static struct dwarf2_per_cu_data *
14881 dwarf2_find_containing_comp_unit (unsigned int offset,
14882                                   struct objfile *objfile)
14883 {
14884   struct dwarf2_per_cu_data *this_cu;
14885   int low, high;
14886
14887   low = 0;
14888   high = dwarf2_per_objfile->n_comp_units - 1;
14889   while (high > low)
14890     {
14891       int mid = low + (high - low) / 2;
14892
14893       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14894         high = mid;
14895       else
14896         low = mid + 1;
14897     }
14898   gdb_assert (low == high);
14899   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14900     {
14901       if (low == 0)
14902         error (_("Dwarf Error: could not find partial DIE containing "
14903                "offset 0x%lx [in module %s]"),
14904                (long) offset, bfd_get_filename (objfile->obfd));
14905
14906       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14907       return dwarf2_per_objfile->all_comp_units[low-1];
14908     }
14909   else
14910     {
14911       this_cu = dwarf2_per_objfile->all_comp_units[low];
14912       if (low == dwarf2_per_objfile->n_comp_units - 1
14913           && offset >= this_cu->offset + this_cu->length)
14914         error (_("invalid dwarf2 offset %u"), offset);
14915       gdb_assert (offset < this_cu->offset + this_cu->length);
14916       return this_cu;
14917     }
14918 }
14919
14920 /* Locate the compilation unit from OBJFILE which is located at exactly
14921    OFFSET.  Raises an error on failure.  */
14922
14923 static struct dwarf2_per_cu_data *
14924 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14925 {
14926   struct dwarf2_per_cu_data *this_cu;
14927
14928   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14929   if (this_cu->offset != offset)
14930     error (_("no compilation unit with offset %u."), offset);
14931   return this_cu;
14932 }
14933
14934 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14935
14936 static void
14937 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14938 {
14939   memset (cu, 0, sizeof (*cu));
14940   cu->objfile = objfile;
14941   obstack_init (&cu->comp_unit_obstack);
14942 }
14943
14944 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14945
14946 static void
14947 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14948 {
14949   struct attribute *attr;
14950
14951   /* Set the language we're debugging.  */
14952   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14953   if (attr)
14954     set_cu_language (DW_UNSND (attr), cu);
14955   else
14956     {
14957       cu->language = language_minimal;
14958       cu->language_defn = language_def (cu->language);
14959     }
14960 }
14961
14962 /* Release one cached compilation unit, CU.  We unlink it from the tree
14963    of compilation units, but we don't remove it from the read_in_chain;
14964    the caller is responsible for that.
14965    NOTE: DATA is a void * because this function is also used as a
14966    cleanup routine.  */
14967
14968 static void
14969 free_one_comp_unit (void *data)
14970 {
14971   struct dwarf2_cu *cu = data;
14972
14973   if (cu->per_cu != NULL)
14974     cu->per_cu->cu = NULL;
14975   cu->per_cu = NULL;
14976
14977   obstack_free (&cu->comp_unit_obstack, NULL);
14978
14979   xfree (cu);
14980 }
14981
14982 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14983    when we're finished with it.  We can't free the pointer itself, but be
14984    sure to unlink it from the cache.  Also release any associated storage
14985    and perform cache maintenance.
14986
14987    Only used during partial symbol parsing.  */
14988
14989 static void
14990 free_stack_comp_unit (void *data)
14991 {
14992   struct dwarf2_cu *cu = data;
14993
14994   obstack_free (&cu->comp_unit_obstack, NULL);
14995   cu->partial_dies = NULL;
14996
14997   if (cu->per_cu != NULL)
14998     {
14999       /* This compilation unit is on the stack in our caller, so we
15000          should not xfree it.  Just unlink it.  */
15001       cu->per_cu->cu = NULL;
15002       cu->per_cu = NULL;
15003
15004       /* If we had a per-cu pointer, then we may have other compilation
15005          units loaded, so age them now.  */
15006       age_cached_comp_units ();
15007     }
15008 }
15009
15010 /* Free all cached compilation units.  */
15011
15012 static void
15013 free_cached_comp_units (void *data)
15014 {
15015   struct dwarf2_per_cu_data *per_cu, **last_chain;
15016
15017   per_cu = dwarf2_per_objfile->read_in_chain;
15018   last_chain = &dwarf2_per_objfile->read_in_chain;
15019   while (per_cu != NULL)
15020     {
15021       struct dwarf2_per_cu_data *next_cu;
15022
15023       next_cu = per_cu->cu->read_in_chain;
15024
15025       free_one_comp_unit (per_cu->cu);
15026       *last_chain = next_cu;
15027
15028       per_cu = next_cu;
15029     }
15030 }
15031
15032 /* Increase the age counter on each cached compilation unit, and free
15033    any that are too old.  */
15034
15035 static void
15036 age_cached_comp_units (void)
15037 {
15038   struct dwarf2_per_cu_data *per_cu, **last_chain;
15039
15040   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15041   per_cu = dwarf2_per_objfile->read_in_chain;
15042   while (per_cu != NULL)
15043     {
15044       per_cu->cu->last_used ++;
15045       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15046         dwarf2_mark (per_cu->cu);
15047       per_cu = per_cu->cu->read_in_chain;
15048     }
15049
15050   per_cu = dwarf2_per_objfile->read_in_chain;
15051   last_chain = &dwarf2_per_objfile->read_in_chain;
15052   while (per_cu != NULL)
15053     {
15054       struct dwarf2_per_cu_data *next_cu;
15055
15056       next_cu = per_cu->cu->read_in_chain;
15057
15058       if (!per_cu->cu->mark)
15059         {
15060           free_one_comp_unit (per_cu->cu);
15061           *last_chain = next_cu;
15062         }
15063       else
15064         last_chain = &per_cu->cu->read_in_chain;
15065
15066       per_cu = next_cu;
15067     }
15068 }
15069
15070 /* Remove a single compilation unit from the cache.  */
15071
15072 static void
15073 free_one_cached_comp_unit (void *target_cu)
15074 {
15075   struct dwarf2_per_cu_data *per_cu, **last_chain;
15076
15077   per_cu = dwarf2_per_objfile->read_in_chain;
15078   last_chain = &dwarf2_per_objfile->read_in_chain;
15079   while (per_cu != NULL)
15080     {
15081       struct dwarf2_per_cu_data *next_cu;
15082
15083       next_cu = per_cu->cu->read_in_chain;
15084
15085       if (per_cu->cu == target_cu)
15086         {
15087           free_one_comp_unit (per_cu->cu);
15088           *last_chain = next_cu;
15089           break;
15090         }
15091       else
15092         last_chain = &per_cu->cu->read_in_chain;
15093
15094       per_cu = next_cu;
15095     }
15096 }
15097
15098 /* Release all extra memory associated with OBJFILE.  */
15099
15100 void
15101 dwarf2_free_objfile (struct objfile *objfile)
15102 {
15103   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15104
15105   if (dwarf2_per_objfile == NULL)
15106     return;
15107
15108   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
15109   free_cached_comp_units (NULL);
15110
15111   if (dwarf2_per_objfile->quick_file_names_table)
15112     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15113
15114   /* Everything else should be on the objfile obstack.  */
15115 }
15116
15117 /* A pair of DIE offset and GDB type pointer.  We store these
15118    in a hash table separate from the DIEs, and preserve them
15119    when the DIEs are flushed out of cache.  */
15120
15121 struct dwarf2_offset_and_type
15122 {
15123   unsigned int offset;
15124   struct type *type;
15125 };
15126
15127 /* Hash function for a dwarf2_offset_and_type.  */
15128
15129 static hashval_t
15130 offset_and_type_hash (const void *item)
15131 {
15132   const struct dwarf2_offset_and_type *ofs = item;
15133
15134   return ofs->offset;
15135 }
15136
15137 /* Equality function for a dwarf2_offset_and_type.  */
15138
15139 static int
15140 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15141 {
15142   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15143   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15144
15145   return ofs_lhs->offset == ofs_rhs->offset;
15146 }
15147
15148 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15149    table if necessary.  For convenience, return TYPE.
15150
15151    The DIEs reading must have careful ordering to:
15152     * Not cause infite loops trying to read in DIEs as a prerequisite for
15153       reading current DIE.
15154     * Not trying to dereference contents of still incompletely read in types
15155       while reading in other DIEs.
15156     * Enable referencing still incompletely read in types just by a pointer to
15157       the type without accessing its fields.
15158
15159    Therefore caller should follow these rules:
15160      * Try to fetch any prerequisite types we may need to build this DIE type
15161        before building the type and calling set_die_type.
15162      * After building type call set_die_type for current DIE as soon as
15163        possible before fetching more types to complete the current type.
15164      * Make the type as complete as possible before fetching more types.  */
15165
15166 static struct type *
15167 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15168 {
15169   struct dwarf2_offset_and_type **slot, ofs;
15170   struct objfile *objfile = cu->objfile;
15171   htab_t *type_hash_ptr;
15172
15173   /* For Ada types, make sure that the gnat-specific data is always
15174      initialized (if not already set).  There are a few types where
15175      we should not be doing so, because the type-specific area is
15176      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15177      where the type-specific area is used to store the floatformat).
15178      But this is not a problem, because the gnat-specific information
15179      is actually not needed for these types.  */
15180   if (need_gnat_info (cu)
15181       && TYPE_CODE (type) != TYPE_CODE_FUNC
15182       && TYPE_CODE (type) != TYPE_CODE_FLT
15183       && !HAVE_GNAT_AUX_INFO (type))
15184     INIT_GNAT_SPECIFIC (type);
15185
15186   if (cu->per_cu->from_debug_types)
15187     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15188   else
15189     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15190
15191   if (*type_hash_ptr == NULL)
15192     {
15193       *type_hash_ptr
15194         = htab_create_alloc_ex (127,
15195                                 offset_and_type_hash,
15196                                 offset_and_type_eq,
15197                                 NULL,
15198                                 &objfile->objfile_obstack,
15199                                 hashtab_obstack_allocate,
15200                                 dummy_obstack_deallocate);
15201     }
15202
15203   ofs.offset = die->offset;
15204   ofs.type = type;
15205   slot = (struct dwarf2_offset_and_type **)
15206     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15207   if (*slot)
15208     complaint (&symfile_complaints,
15209                _("A problem internal to GDB: DIE 0x%x has type already set"),
15210                die->offset);
15211   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15212   **slot = ofs;
15213   return type;
15214 }
15215
15216 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15217    table, or return NULL if the die does not have a saved type.  */
15218
15219 static struct type *
15220 get_die_type_at_offset (unsigned int offset,
15221                         struct dwarf2_per_cu_data *per_cu)
15222 {
15223   struct dwarf2_offset_and_type *slot, ofs;
15224   htab_t type_hash;
15225
15226   if (per_cu->from_debug_types)
15227     type_hash = dwarf2_per_objfile->debug_types_type_hash;
15228   else
15229     type_hash = dwarf2_per_objfile->debug_info_type_hash;
15230   if (type_hash == NULL)
15231     return NULL;
15232
15233   ofs.offset = offset;
15234   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15235   if (slot)
15236     return slot->type;
15237   else
15238     return NULL;
15239 }
15240
15241 /* Look up the type for DIE in the appropriate type_hash table,
15242    or return NULL if DIE does not have a saved type.  */
15243
15244 static struct type *
15245 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15246 {
15247   return get_die_type_at_offset (die->offset, cu->per_cu);
15248 }
15249
15250 /* Add a dependence relationship from CU to REF_PER_CU.  */
15251
15252 static void
15253 dwarf2_add_dependence (struct dwarf2_cu *cu,
15254                        struct dwarf2_per_cu_data *ref_per_cu)
15255 {
15256   void **slot;
15257
15258   if (cu->dependencies == NULL)
15259     cu->dependencies
15260       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15261                               NULL, &cu->comp_unit_obstack,
15262                               hashtab_obstack_allocate,
15263                               dummy_obstack_deallocate);
15264
15265   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15266   if (*slot == NULL)
15267     *slot = ref_per_cu;
15268 }
15269
15270 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15271    Set the mark field in every compilation unit in the
15272    cache that we must keep because we are keeping CU.  */
15273
15274 static int
15275 dwarf2_mark_helper (void **slot, void *data)
15276 {
15277   struct dwarf2_per_cu_data *per_cu;
15278
15279   per_cu = (struct dwarf2_per_cu_data *) *slot;
15280
15281   /* cu->dependencies references may not yet have been ever read if QUIT aborts
15282      reading of the chain.  As such dependencies remain valid it is not much
15283      useful to track and undo them during QUIT cleanups.  */
15284   if (per_cu->cu == NULL)
15285     return 1;
15286
15287   if (per_cu->cu->mark)
15288     return 1;
15289   per_cu->cu->mark = 1;
15290
15291   if (per_cu->cu->dependencies != NULL)
15292     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15293
15294   return 1;
15295 }
15296
15297 /* Set the mark field in CU and in every other compilation unit in the
15298    cache that we must keep because we are keeping CU.  */
15299
15300 static void
15301 dwarf2_mark (struct dwarf2_cu *cu)
15302 {
15303   if (cu->mark)
15304     return;
15305   cu->mark = 1;
15306   if (cu->dependencies != NULL)
15307     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15308 }
15309
15310 static void
15311 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15312 {
15313   while (per_cu)
15314     {
15315       per_cu->cu->mark = 0;
15316       per_cu = per_cu->cu->read_in_chain;
15317     }
15318 }
15319
15320 /* Trivial hash function for partial_die_info: the hash value of a DIE
15321    is its offset in .debug_info for this objfile.  */
15322
15323 static hashval_t
15324 partial_die_hash (const void *item)
15325 {
15326   const struct partial_die_info *part_die = item;
15327
15328   return part_die->offset;
15329 }
15330
15331 /* Trivial comparison function for partial_die_info structures: two DIEs
15332    are equal if they have the same offset.  */
15333
15334 static int
15335 partial_die_eq (const void *item_lhs, const void *item_rhs)
15336 {
15337   const struct partial_die_info *part_die_lhs = item_lhs;
15338   const struct partial_die_info *part_die_rhs = item_rhs;
15339
15340   return part_die_lhs->offset == part_die_rhs->offset;
15341 }
15342
15343 static struct cmd_list_element *set_dwarf2_cmdlist;
15344 static struct cmd_list_element *show_dwarf2_cmdlist;
15345
15346 static void
15347 set_dwarf2_cmd (char *args, int from_tty)
15348 {
15349   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15350 }
15351
15352 static void
15353 show_dwarf2_cmd (char *args, int from_tty)
15354 {
15355   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15356 }
15357
15358 /* If section described by INFO was mmapped, munmap it now.  */
15359
15360 static void
15361 munmap_section_buffer (struct dwarf2_section_info *info)
15362 {
15363   if (info->was_mmapped)
15364     {
15365 #ifdef HAVE_MMAP
15366       intptr_t begin = (intptr_t) info->buffer;
15367       intptr_t map_begin = begin & ~(pagesize - 1);
15368       size_t map_length = info->size + begin - map_begin;
15369
15370       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15371 #else
15372       /* Without HAVE_MMAP, we should never be here to begin with.  */
15373       gdb_assert_not_reached ("no mmap support");
15374 #endif
15375     }
15376 }
15377
15378 /* munmap debug sections for OBJFILE, if necessary.  */
15379
15380 static void
15381 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15382 {
15383   struct dwarf2_per_objfile *data = d;
15384
15385   /* This is sorted according to the order they're defined in to make it easier
15386      to keep in sync.  */
15387   munmap_section_buffer (&data->info);
15388   munmap_section_buffer (&data->abbrev);
15389   munmap_section_buffer (&data->line);
15390   munmap_section_buffer (&data->loc);
15391   munmap_section_buffer (&data->macinfo);
15392   munmap_section_buffer (&data->str);
15393   munmap_section_buffer (&data->ranges);
15394   munmap_section_buffer (&data->types);
15395   munmap_section_buffer (&data->frame);
15396   munmap_section_buffer (&data->eh_frame);
15397   munmap_section_buffer (&data->gdb_index);
15398 }
15399
15400 \f
15401 /* The "save gdb-index" command.  */
15402
15403 /* The contents of the hash table we create when building the string
15404    table.  */
15405 struct strtab_entry
15406 {
15407   offset_type offset;
15408   const char *str;
15409 };
15410
15411 /* Hash function for a strtab_entry.  */
15412
15413 static hashval_t
15414 hash_strtab_entry (const void *e)
15415 {
15416   const struct strtab_entry *entry = e;
15417   return mapped_index_string_hash (entry->str);
15418 }
15419
15420 /* Equality function for a strtab_entry.  */
15421
15422 static int
15423 eq_strtab_entry (const void *a, const void *b)
15424 {
15425   const struct strtab_entry *ea = a;
15426   const struct strtab_entry *eb = b;
15427   return !strcmp (ea->str, eb->str);
15428 }
15429
15430 /* Create a strtab_entry hash table.  */
15431
15432 static htab_t
15433 create_strtab (void)
15434 {
15435   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15436                             xfree, xcalloc, xfree);
15437 }
15438
15439 /* Add a string to the constant pool.  Return the string's offset in
15440    host order.  */
15441
15442 static offset_type
15443 add_string (htab_t table, struct obstack *cpool, const char *str)
15444 {
15445   void **slot;
15446   struct strtab_entry entry;
15447   struct strtab_entry *result;
15448
15449   entry.str = str;
15450   slot = htab_find_slot (table, &entry, INSERT);
15451   if (*slot)
15452     result = *slot;
15453   else
15454     {
15455       result = XNEW (struct strtab_entry);
15456       result->offset = obstack_object_size (cpool);
15457       result->str = str;
15458       obstack_grow_str0 (cpool, str);
15459       *slot = result;
15460     }
15461   return result->offset;
15462 }
15463
15464 /* An entry in the symbol table.  */
15465 struct symtab_index_entry
15466 {
15467   /* The name of the symbol.  */
15468   const char *name;
15469   /* The offset of the name in the constant pool.  */
15470   offset_type index_offset;
15471   /* A sorted vector of the indices of all the CUs that hold an object
15472      of this name.  */
15473   VEC (offset_type) *cu_indices;
15474 };
15475
15476 /* The symbol table.  This is a power-of-2-sized hash table.  */
15477 struct mapped_symtab
15478 {
15479   offset_type n_elements;
15480   offset_type size;
15481   struct symtab_index_entry **data;
15482 };
15483
15484 /* Hash function for a symtab_index_entry.  */
15485
15486 static hashval_t
15487 hash_symtab_entry (const void *e)
15488 {
15489   const struct symtab_index_entry *entry = e;
15490   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15491                          sizeof (offset_type) * VEC_length (offset_type,
15492                                                             entry->cu_indices),
15493                          0);
15494 }
15495
15496 /* Equality function for a symtab_index_entry.  */
15497
15498 static int
15499 eq_symtab_entry (const void *a, const void *b)
15500 {
15501   const struct symtab_index_entry *ea = a;
15502   const struct symtab_index_entry *eb = b;
15503   int len = VEC_length (offset_type, ea->cu_indices);
15504   if (len != VEC_length (offset_type, eb->cu_indices))
15505     return 0;
15506   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15507                   VEC_address (offset_type, eb->cu_indices),
15508                   sizeof (offset_type) * len);
15509 }
15510
15511 /* Destroy a symtab_index_entry.  */
15512
15513 static void
15514 delete_symtab_entry (void *p)
15515 {
15516   struct symtab_index_entry *entry = p;
15517   VEC_free (offset_type, entry->cu_indices);
15518   xfree (entry);
15519 }
15520
15521 /* Create a hash table holding symtab_index_entry objects.  */
15522
15523 static htab_t
15524 create_symbol_hash_table (void)
15525 {
15526   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15527                             delete_symtab_entry, xcalloc, xfree);
15528 }
15529
15530 /* Create a new mapped symtab object.  */
15531
15532 static struct mapped_symtab *
15533 create_mapped_symtab (void)
15534 {
15535   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15536   symtab->n_elements = 0;
15537   symtab->size = 1024;
15538   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15539   return symtab;
15540 }
15541
15542 /* Destroy a mapped_symtab.  */
15543
15544 static void
15545 cleanup_mapped_symtab (void *p)
15546 {
15547   struct mapped_symtab *symtab = p;
15548   /* The contents of the array are freed when the other hash table is
15549      destroyed.  */
15550   xfree (symtab->data);
15551   xfree (symtab);
15552 }
15553
15554 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15555    the slot.  */
15556
15557 static struct symtab_index_entry **
15558 find_slot (struct mapped_symtab *symtab, const char *name)
15559 {
15560   offset_type index, step, hash = mapped_index_string_hash (name);
15561
15562   index = hash & (symtab->size - 1);
15563   step = ((hash * 17) & (symtab->size - 1)) | 1;
15564
15565   for (;;)
15566     {
15567       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15568         return &symtab->data[index];
15569       index = (index + step) & (symtab->size - 1);
15570     }
15571 }
15572
15573 /* Expand SYMTAB's hash table.  */
15574
15575 static void
15576 hash_expand (struct mapped_symtab *symtab)
15577 {
15578   offset_type old_size = symtab->size;
15579   offset_type i;
15580   struct symtab_index_entry **old_entries = symtab->data;
15581
15582   symtab->size *= 2;
15583   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15584
15585   for (i = 0; i < old_size; ++i)
15586     {
15587       if (old_entries[i])
15588         {
15589           struct symtab_index_entry **slot = find_slot (symtab,
15590                                                         old_entries[i]->name);
15591           *slot = old_entries[i];
15592         }
15593     }
15594
15595   xfree (old_entries);
15596 }
15597
15598 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15599    is the index of the CU in which the symbol appears.  */
15600
15601 static void
15602 add_index_entry (struct mapped_symtab *symtab, const char *name,
15603                  offset_type cu_index)
15604 {
15605   struct symtab_index_entry **slot;
15606
15607   ++symtab->n_elements;
15608   if (4 * symtab->n_elements / 3 >= symtab->size)
15609     hash_expand (symtab);
15610
15611   slot = find_slot (symtab, name);
15612   if (!*slot)
15613     {
15614       *slot = XNEW (struct symtab_index_entry);
15615       (*slot)->name = name;
15616       (*slot)->cu_indices = NULL;
15617     }
15618   /* Don't push an index twice.  Due to how we add entries we only
15619      have to check the last one.  */ 
15620   if (VEC_empty (offset_type, (*slot)->cu_indices)
15621       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15622     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15623 }
15624
15625 /* Add a vector of indices to the constant pool.  */
15626
15627 static offset_type
15628 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15629                       struct symtab_index_entry *entry)
15630 {
15631   void **slot;
15632
15633   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15634   if (!*slot)
15635     {
15636       offset_type len = VEC_length (offset_type, entry->cu_indices);
15637       offset_type val = MAYBE_SWAP (len);
15638       offset_type iter;
15639       int i;
15640
15641       *slot = entry;
15642       entry->index_offset = obstack_object_size (cpool);
15643
15644       obstack_grow (cpool, &val, sizeof (val));
15645       for (i = 0;
15646            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15647            ++i)
15648         {
15649           val = MAYBE_SWAP (iter);
15650           obstack_grow (cpool, &val, sizeof (val));
15651         }
15652     }
15653   else
15654     {
15655       struct symtab_index_entry *old_entry = *slot;
15656       entry->index_offset = old_entry->index_offset;
15657       entry = old_entry;
15658     }
15659   return entry->index_offset;
15660 }
15661
15662 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15663    constant pool entries going into the obstack CPOOL.  */
15664
15665 static void
15666 write_hash_table (struct mapped_symtab *symtab,
15667                   struct obstack *output, struct obstack *cpool)
15668 {
15669   offset_type i;
15670   htab_t symbol_hash_table;
15671   htab_t str_table;
15672
15673   symbol_hash_table = create_symbol_hash_table ();
15674   str_table = create_strtab ();
15675
15676   /* We add all the index vectors to the constant pool first, to
15677      ensure alignment is ok.  */
15678   for (i = 0; i < symtab->size; ++i)
15679     {
15680       if (symtab->data[i])
15681         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15682     }
15683
15684   /* Now write out the hash table.  */
15685   for (i = 0; i < symtab->size; ++i)
15686     {
15687       offset_type str_off, vec_off;
15688
15689       if (symtab->data[i])
15690         {
15691           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15692           vec_off = symtab->data[i]->index_offset;
15693         }
15694       else
15695         {
15696           /* While 0 is a valid constant pool index, it is not valid
15697              to have 0 for both offsets.  */
15698           str_off = 0;
15699           vec_off = 0;
15700         }
15701
15702       str_off = MAYBE_SWAP (str_off);
15703       vec_off = MAYBE_SWAP (vec_off);
15704
15705       obstack_grow (output, &str_off, sizeof (str_off));
15706       obstack_grow (output, &vec_off, sizeof (vec_off));
15707     }
15708
15709   htab_delete (str_table);
15710   htab_delete (symbol_hash_table);
15711 }
15712
15713 /* Struct to map psymtab to CU index in the index file.  */
15714 struct psymtab_cu_index_map
15715 {
15716   struct partial_symtab *psymtab;
15717   unsigned int cu_index;
15718 };
15719
15720 static hashval_t
15721 hash_psymtab_cu_index (const void *item)
15722 {
15723   const struct psymtab_cu_index_map *map = item;
15724
15725   return htab_hash_pointer (map->psymtab);
15726 }
15727
15728 static int
15729 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15730 {
15731   const struct psymtab_cu_index_map *lhs = item_lhs;
15732   const struct psymtab_cu_index_map *rhs = item_rhs;
15733
15734   return lhs->psymtab == rhs->psymtab;
15735 }
15736
15737 /* Helper struct for building the address table.  */
15738 struct addrmap_index_data
15739 {
15740   struct objfile *objfile;
15741   struct obstack *addr_obstack;
15742   htab_t cu_index_htab;
15743
15744   /* Non-zero if the previous_* fields are valid.
15745      We can't write an entry until we see the next entry (since it is only then
15746      that we know the end of the entry).  */
15747   int previous_valid;
15748   /* Index of the CU in the table of all CUs in the index file.  */
15749   unsigned int previous_cu_index;
15750   /* Start address of the CU.  */
15751   CORE_ADDR previous_cu_start;
15752 };
15753
15754 /* Write an address entry to OBSTACK.  */
15755
15756 static void
15757 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15758                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15759 {
15760   offset_type cu_index_to_write;
15761   char addr[8];
15762   CORE_ADDR baseaddr;
15763
15764   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15765
15766   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15767   obstack_grow (obstack, addr, 8);
15768   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15769   obstack_grow (obstack, addr, 8);
15770   cu_index_to_write = MAYBE_SWAP (cu_index);
15771   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15772 }
15773
15774 /* Worker function for traversing an addrmap to build the address table.  */
15775
15776 static int
15777 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15778 {
15779   struct addrmap_index_data *data = datap;
15780   struct partial_symtab *pst = obj;
15781   offset_type cu_index;
15782   void **slot;
15783
15784   if (data->previous_valid)
15785     add_address_entry (data->objfile, data->addr_obstack,
15786                        data->previous_cu_start, start_addr,
15787                        data->previous_cu_index);
15788
15789   data->previous_cu_start = start_addr;
15790   if (pst != NULL)
15791     {
15792       struct psymtab_cu_index_map find_map, *map;
15793       find_map.psymtab = pst;
15794       map = htab_find (data->cu_index_htab, &find_map);
15795       gdb_assert (map != NULL);
15796       data->previous_cu_index = map->cu_index;
15797       data->previous_valid = 1;
15798     }
15799   else
15800       data->previous_valid = 0;
15801
15802   return 0;
15803 }
15804
15805 /* Write OBJFILE's address map to OBSTACK.
15806    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15807    in the index file.  */
15808
15809 static void
15810 write_address_map (struct objfile *objfile, struct obstack *obstack,
15811                    htab_t cu_index_htab)
15812 {
15813   struct addrmap_index_data addrmap_index_data;
15814
15815   /* When writing the address table, we have to cope with the fact that
15816      the addrmap iterator only provides the start of a region; we have to
15817      wait until the next invocation to get the start of the next region.  */
15818
15819   addrmap_index_data.objfile = objfile;
15820   addrmap_index_data.addr_obstack = obstack;
15821   addrmap_index_data.cu_index_htab = cu_index_htab;
15822   addrmap_index_data.previous_valid = 0;
15823
15824   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15825                    &addrmap_index_data);
15826
15827   /* It's highly unlikely the last entry (end address = 0xff...ff)
15828      is valid, but we should still handle it.
15829      The end address is recorded as the start of the next region, but that
15830      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15831      anyway.  */
15832   if (addrmap_index_data.previous_valid)
15833     add_address_entry (objfile, obstack,
15834                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15835                        addrmap_index_data.previous_cu_index);
15836 }
15837
15838 /* Add a list of partial symbols to SYMTAB.  */
15839
15840 static void
15841 write_psymbols (struct mapped_symtab *symtab,
15842                 htab_t psyms_seen,
15843                 struct partial_symbol **psymp,
15844                 int count,
15845                 offset_type cu_index,
15846                 int is_static)
15847 {
15848   for (; count-- > 0; ++psymp)
15849     {
15850       void **slot, *lookup;
15851
15852       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15853         error (_("Ada is not currently supported by the index"));
15854
15855       /* We only want to add a given psymbol once.  However, we also
15856          want to account for whether it is global or static.  So, we
15857          may add it twice, using slightly different values.  */
15858       if (is_static)
15859         {
15860           uintptr_t val = 1 | (uintptr_t) *psymp;
15861
15862           lookup = (void *) val;
15863         }
15864       else
15865         lookup = *psymp;
15866
15867       /* Only add a given psymbol once.  */
15868       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15869       if (!*slot)
15870         {
15871           *slot = lookup;
15872           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15873         }
15874     }
15875 }
15876
15877 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15878    exception if there is an error.  */
15879
15880 static void
15881 write_obstack (FILE *file, struct obstack *obstack)
15882 {
15883   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15884               file)
15885       != obstack_object_size (obstack))
15886     error (_("couldn't data write to file"));
15887 }
15888
15889 /* Unlink a file if the argument is not NULL.  */
15890
15891 static void
15892 unlink_if_set (void *p)
15893 {
15894   char **filename = p;
15895   if (*filename)
15896     unlink (*filename);
15897 }
15898
15899 /* A helper struct used when iterating over debug_types.  */
15900 struct signatured_type_index_data
15901 {
15902   struct objfile *objfile;
15903   struct mapped_symtab *symtab;
15904   struct obstack *types_list;
15905   htab_t psyms_seen;
15906   int cu_index;
15907 };
15908
15909 /* A helper function that writes a single signatured_type to an
15910    obstack.  */
15911
15912 static int
15913 write_one_signatured_type (void **slot, void *d)
15914 {
15915   struct signatured_type_index_data *info = d;
15916   struct signatured_type *entry = (struct signatured_type *) *slot;
15917   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15918   struct partial_symtab *psymtab = per_cu->v.psymtab;
15919   gdb_byte val[8];
15920
15921   write_psymbols (info->symtab,
15922                   info->psyms_seen,
15923                   info->objfile->global_psymbols.list
15924                   + psymtab->globals_offset,
15925                   psymtab->n_global_syms, info->cu_index,
15926                   0);
15927   write_psymbols (info->symtab,
15928                   info->psyms_seen,
15929                   info->objfile->static_psymbols.list
15930                   + psymtab->statics_offset,
15931                   psymtab->n_static_syms, info->cu_index,
15932                   1);
15933
15934   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15935   obstack_grow (info->types_list, val, 8);
15936   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15937   obstack_grow (info->types_list, val, 8);
15938   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15939   obstack_grow (info->types_list, val, 8);
15940
15941   ++info->cu_index;
15942
15943   return 1;
15944 }
15945
15946 /* A cleanup function for an htab_t.  */
15947
15948 static void
15949 cleanup_htab (void *arg)
15950 {
15951   htab_delete (arg);
15952 }
15953
15954 /* Create an index file for OBJFILE in the directory DIR.  */
15955
15956 static void
15957 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15958 {
15959   struct cleanup *cleanup;
15960   char *filename, *cleanup_filename;
15961   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15962   struct obstack cu_list, types_cu_list;
15963   int i;
15964   FILE *out_file;
15965   struct mapped_symtab *symtab;
15966   offset_type val, size_of_contents, total_len;
15967   struct stat st;
15968   char buf[8];
15969   htab_t psyms_seen;
15970   htab_t cu_index_htab;
15971   struct psymtab_cu_index_map *psymtab_cu_index_map;
15972
15973   if (!objfile->psymtabs)
15974     return;
15975   if (dwarf2_per_objfile->using_index)
15976     error (_("Cannot use an index to create the index"));
15977
15978   if (stat (objfile->name, &st) < 0)
15979     perror_with_name (objfile->name);
15980
15981   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15982                      INDEX_SUFFIX, (char *) NULL);
15983   cleanup = make_cleanup (xfree, filename);
15984
15985   out_file = fopen (filename, "wb");
15986   if (!out_file)
15987     error (_("Can't open `%s' for writing"), filename);
15988
15989   cleanup_filename = filename;
15990   make_cleanup (unlink_if_set, &cleanup_filename);
15991
15992   symtab = create_mapped_symtab ();
15993   make_cleanup (cleanup_mapped_symtab, symtab);
15994
15995   obstack_init (&addr_obstack);
15996   make_cleanup_obstack_free (&addr_obstack);
15997
15998   obstack_init (&cu_list);
15999   make_cleanup_obstack_free (&cu_list);
16000
16001   obstack_init (&types_cu_list);
16002   make_cleanup_obstack_free (&types_cu_list);
16003
16004   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16005                                   NULL, xcalloc, xfree);
16006   make_cleanup (cleanup_htab, psyms_seen);
16007
16008   /* While we're scanning CU's create a table that maps a psymtab pointer
16009      (which is what addrmap records) to its index (which is what is recorded
16010      in the index file).  This will later be needed to write the address
16011      table.  */
16012   cu_index_htab = htab_create_alloc (100,
16013                                      hash_psymtab_cu_index,
16014                                      eq_psymtab_cu_index,
16015                                      NULL, xcalloc, xfree);
16016   make_cleanup (cleanup_htab, cu_index_htab);
16017   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16018     xmalloc (sizeof (struct psymtab_cu_index_map)
16019              * dwarf2_per_objfile->n_comp_units);
16020   make_cleanup (xfree, psymtab_cu_index_map);
16021
16022   /* The CU list is already sorted, so we don't need to do additional
16023      work here.  Also, the debug_types entries do not appear in
16024      all_comp_units, but only in their own hash table.  */
16025   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16026     {
16027       struct dwarf2_per_cu_data *per_cu
16028         = dwarf2_per_objfile->all_comp_units[i];
16029       struct partial_symtab *psymtab = per_cu->v.psymtab;
16030       gdb_byte val[8];
16031       struct psymtab_cu_index_map *map;
16032       void **slot;
16033
16034       write_psymbols (symtab,
16035                       psyms_seen,
16036                       objfile->global_psymbols.list + psymtab->globals_offset,
16037                       psymtab->n_global_syms, i,
16038                       0);
16039       write_psymbols (symtab,
16040                       psyms_seen,
16041                       objfile->static_psymbols.list + psymtab->statics_offset,
16042                       psymtab->n_static_syms, i,
16043                       1);
16044
16045       map = &psymtab_cu_index_map[i];
16046       map->psymtab = psymtab;
16047       map->cu_index = i;
16048       slot = htab_find_slot (cu_index_htab, map, INSERT);
16049       gdb_assert (slot != NULL);
16050       gdb_assert (*slot == NULL);
16051       *slot = map;
16052
16053       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16054       obstack_grow (&cu_list, val, 8);
16055       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16056       obstack_grow (&cu_list, val, 8);
16057     }
16058
16059   /* Dump the address map.  */
16060   write_address_map (objfile, &addr_obstack, cu_index_htab);
16061
16062   /* Write out the .debug_type entries, if any.  */
16063   if (dwarf2_per_objfile->signatured_types)
16064     {
16065       struct signatured_type_index_data sig_data;
16066
16067       sig_data.objfile = objfile;
16068       sig_data.symtab = symtab;
16069       sig_data.types_list = &types_cu_list;
16070       sig_data.psyms_seen = psyms_seen;
16071       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16072       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16073                               write_one_signatured_type, &sig_data);
16074     }
16075
16076   obstack_init (&constant_pool);
16077   make_cleanup_obstack_free (&constant_pool);
16078   obstack_init (&symtab_obstack);
16079   make_cleanup_obstack_free (&symtab_obstack);
16080   write_hash_table (symtab, &symtab_obstack, &constant_pool);
16081
16082   obstack_init (&contents);
16083   make_cleanup_obstack_free (&contents);
16084   size_of_contents = 6 * sizeof (offset_type);
16085   total_len = size_of_contents;
16086
16087   /* The version number.  */
16088   val = MAYBE_SWAP (4);
16089   obstack_grow (&contents, &val, sizeof (val));
16090
16091   /* The offset of the CU list from the start of the file.  */
16092   val = MAYBE_SWAP (total_len);
16093   obstack_grow (&contents, &val, sizeof (val));
16094   total_len += obstack_object_size (&cu_list);
16095
16096   /* The offset of the types CU list from the start of the file.  */
16097   val = MAYBE_SWAP (total_len);
16098   obstack_grow (&contents, &val, sizeof (val));
16099   total_len += obstack_object_size (&types_cu_list);
16100
16101   /* The offset of the address table from the start of the file.  */
16102   val = MAYBE_SWAP (total_len);
16103   obstack_grow (&contents, &val, sizeof (val));
16104   total_len += obstack_object_size (&addr_obstack);
16105
16106   /* The offset of the symbol table from the start of the file.  */
16107   val = MAYBE_SWAP (total_len);
16108   obstack_grow (&contents, &val, sizeof (val));
16109   total_len += obstack_object_size (&symtab_obstack);
16110
16111   /* The offset of the constant pool from the start of the file.  */
16112   val = MAYBE_SWAP (total_len);
16113   obstack_grow (&contents, &val, sizeof (val));
16114   total_len += obstack_object_size (&constant_pool);
16115
16116   gdb_assert (obstack_object_size (&contents) == size_of_contents);
16117
16118   write_obstack (out_file, &contents);
16119   write_obstack (out_file, &cu_list);
16120   write_obstack (out_file, &types_cu_list);
16121   write_obstack (out_file, &addr_obstack);
16122   write_obstack (out_file, &symtab_obstack);
16123   write_obstack (out_file, &constant_pool);
16124
16125   fclose (out_file);
16126
16127   /* We want to keep the file, so we set cleanup_filename to NULL
16128      here.  See unlink_if_set.  */
16129   cleanup_filename = NULL;
16130
16131   do_cleanups (cleanup);
16132 }
16133
16134 /* The mapped index file format is designed to be directly mmap()able
16135    on any architecture.  In most cases, a datum is represented using a
16136    little-endian 32-bit integer value, called an offset_type.  Big
16137    endian machines must byte-swap the values before using them.
16138    Exceptions to this rule are noted.  The data is laid out such that
16139    alignment is always respected.
16140
16141    A mapped index consists of several sections.
16142
16143    1. The file header.  This is a sequence of values, of offset_type
16144    unless otherwise noted:
16145
16146    [0] The version number, currently 4.  Versions 1, 2 and 3 are
16147    obsolete.
16148    [1] The offset, from the start of the file, of the CU list.
16149    [2] The offset, from the start of the file, of the types CU list.
16150    Note that this section can be empty, in which case this offset will
16151    be equal to the next offset.
16152    [3] The offset, from the start of the file, of the address section.
16153    [4] The offset, from the start of the file, of the symbol table.
16154    [5] The offset, from the start of the file, of the constant pool.
16155
16156    2. The CU list.  This is a sequence of pairs of 64-bit
16157    little-endian values, sorted by the CU offset.  The first element
16158    in each pair is the offset of a CU in the .debug_info section.  The
16159    second element in each pair is the length of that CU.  References
16160    to a CU elsewhere in the map are done using a CU index, which is
16161    just the 0-based index into this table.  Note that if there are
16162    type CUs, then conceptually CUs and type CUs form a single list for
16163    the purposes of CU indices.
16164
16165    3. The types CU list.  This is a sequence of triplets of 64-bit
16166    little-endian values.  In a triplet, the first value is the CU
16167    offset, the second value is the type offset in the CU, and the
16168    third value is the type signature.  The types CU list is not
16169    sorted.
16170
16171    4. The address section.  The address section consists of a sequence
16172    of address entries.  Each address entry has three elements.
16173    [0] The low address.  This is a 64-bit little-endian value.
16174    [1] The high address.  This is a 64-bit little-endian value.
16175        Like DW_AT_high_pc, the value is one byte beyond the end.
16176    [2] The CU index.  This is an offset_type value.
16177
16178    5. The symbol table.  This is a hash table.  The size of the hash
16179    table is always a power of 2.  The initial hash and the step are
16180    currently defined by the `find_slot' function.
16181
16182    Each slot in the hash table consists of a pair of offset_type
16183    values.  The first value is the offset of the symbol's name in the
16184    constant pool.  The second value is the offset of the CU vector in
16185    the constant pool.
16186
16187    If both values are 0, then this slot in the hash table is empty.
16188    This is ok because while 0 is a valid constant pool index, it
16189    cannot be a valid index for both a string and a CU vector.
16190
16191    A string in the constant pool is stored as a \0-terminated string,
16192    as you'd expect.
16193
16194    A CU vector in the constant pool is a sequence of offset_type
16195    values.  The first value is the number of CU indices in the vector.
16196    Each subsequent value is the index of a CU in the CU list.  This
16197    element in the hash table is used to indicate which CUs define the
16198    symbol.
16199
16200    6. The constant pool.  This is simply a bunch of bytes.  It is
16201    organized so that alignment is correct: CU vectors are stored
16202    first, followed by strings.  */
16203
16204 static void
16205 save_gdb_index_command (char *arg, int from_tty)
16206 {
16207   struct objfile *objfile;
16208
16209   if (!arg || !*arg)
16210     error (_("usage: save gdb-index DIRECTORY"));
16211
16212   ALL_OBJFILES (objfile)
16213   {
16214     struct stat st;
16215
16216     /* If the objfile does not correspond to an actual file, skip it.  */
16217     if (stat (objfile->name, &st) < 0)
16218       continue;
16219
16220     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16221     if (dwarf2_per_objfile)
16222       {
16223         volatile struct gdb_exception except;
16224
16225         TRY_CATCH (except, RETURN_MASK_ERROR)
16226           {
16227             write_psymtabs_to_index (objfile, arg);
16228           }
16229         if (except.reason < 0)
16230           exception_fprintf (gdb_stderr, except,
16231                              _("Error while writing index for `%s': "),
16232                              objfile->name);
16233       }
16234   }
16235 }
16236
16237 \f
16238
16239 int dwarf2_always_disassemble;
16240
16241 static void
16242 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16243                                 struct cmd_list_element *c, const char *value)
16244 {
16245   fprintf_filtered (file,
16246                     _("Whether to always disassemble "
16247                       "DWARF expressions is %s.\n"),
16248                     value);
16249 }
16250
16251 static void
16252 show_check_physname (struct ui_file *file, int from_tty,
16253                      struct cmd_list_element *c, const char *value)
16254 {
16255   fprintf_filtered (file,
16256                     _("Whether to check \"physname\" is %s.\n"),
16257                     value);
16258 }
16259
16260 void _initialize_dwarf2_read (void);
16261
16262 void
16263 _initialize_dwarf2_read (void)
16264 {
16265   struct cmd_list_element *c;
16266
16267   dwarf2_objfile_data_key
16268     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16269
16270   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16271 Set DWARF 2 specific variables.\n\
16272 Configure DWARF 2 variables such as the cache size"),
16273                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16274                   0/*allow-unknown*/, &maintenance_set_cmdlist);
16275
16276   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16277 Show DWARF 2 specific variables\n\
16278 Show DWARF 2 variables such as the cache size"),
16279                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16280                   0/*allow-unknown*/, &maintenance_show_cmdlist);
16281
16282   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16283                             &dwarf2_max_cache_age, _("\
16284 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16285 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16286 A higher limit means that cached compilation units will be stored\n\
16287 in memory longer, and more total memory will be used.  Zero disables\n\
16288 caching, which can slow down startup."),
16289                             NULL,
16290                             show_dwarf2_max_cache_age,
16291                             &set_dwarf2_cmdlist,
16292                             &show_dwarf2_cmdlist);
16293
16294   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16295                            &dwarf2_always_disassemble, _("\
16296 Set whether `info address' always disassembles DWARF expressions."), _("\
16297 Show whether `info address' always disassembles DWARF expressions."), _("\
16298 When enabled, DWARF expressions are always printed in an assembly-like\n\
16299 syntax.  When disabled, expressions will be printed in a more\n\
16300 conversational style, when possible."),
16301                            NULL,
16302                            show_dwarf2_always_disassemble,
16303                            &set_dwarf2_cmdlist,
16304                            &show_dwarf2_cmdlist);
16305
16306   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16307 Set debugging of the dwarf2 DIE reader."), _("\
16308 Show debugging of the dwarf2 DIE reader."), _("\
16309 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16310 The value is the maximum depth to print."),
16311                             NULL,
16312                             NULL,
16313                             &setdebuglist, &showdebuglist);
16314
16315   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16316 Set cross-checking of \"physname\" code against demangler."), _("\
16317 Show cross-checking of \"physname\" code against demangler."), _("\
16318 When enabled, GDB's internal \"physname\" code is checked against\n\
16319 the demangler."),
16320                            NULL, show_check_physname,
16321                            &setdebuglist, &showdebuglist);
16322
16323   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16324                _("\
16325 Save a gdb-index file.\n\
16326 Usage: save gdb-index DIRECTORY"),
16327                &save_cmdlist);
16328   set_cmd_completer (c, filename_completer);
16329 }