gdb vendor branch: Bring in additional source files
[dragonfly.git] / contrib / gdb-7 / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6    Inc.  with support from Florida State University (under contract
7    with the Ada Joint Program Office), and Silicon Graphics, Inc.
8    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10    support.
11
12    This file is part of GDB.
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 3 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "symtab.h"
30 #include "gdbtypes.h"
31 #include "objfiles.h"
32 #include "dwarf2.h"
33 #include "buildsym.h"
34 #include "demangle.h"
35 #include "gdb-demangle.h"
36 #include "expression.h"
37 #include "filenames.h"  /* for DOSish file names */
38 #include "macrotab.h"
39 #include "language.h"
40 #include "complaints.h"
41 #include "bcache.h"
42 #include "dwarf2expr.h"
43 #include "dwarf2loc.h"
44 #include "cp-support.h"
45 #include "hashtab.h"
46 #include "command.h"
47 #include "gdbcmd.h"
48 #include "block.h"
49 #include "addrmap.h"
50 #include "typeprint.h"
51 #include "jv-lang.h"
52 #include "psympriv.h"
53 #include "exceptions.h"
54 #include "gdb_stat.h"
55 #include "completer.h"
56 #include "vec.h"
57 #include "c-lang.h"
58 #include "valprint.h"
59 #include <ctype.h>
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80    Because of alignment constraints, this structure has padding and cannot
81    be mapped directly onto the beginning of the .debug_info section.  */
82 typedef struct comp_unit_header
83   {
84     unsigned int length;        /* length of the .debug_info
85                                    contribution */
86     unsigned short version;     /* version number -- 2 for DWARF
87                                    version 2 */
88     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89     unsigned char addr_size;    /* byte size of an address -- 4 */
90   }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96    Because of alignment constraints, this structure has padding and cannot
97    be mapped directly onto the beginning of the .debug_info section.  */
98 typedef struct statement_prologue
99   {
100     unsigned int total_length;  /* byte length of the statement
101                                    information */
102     unsigned short version;     /* version number -- 2 for DWARF
103                                    version 2 */
104     unsigned int prologue_length;       /* # bytes between prologue &
105                                            stmt program */
106     unsigned char minimum_instruction_length;   /* byte size of
107                                                    smallest instr */
108     unsigned char default_is_stmt;      /* initial value of is_stmt
109                                            register */
110     char line_base;
111     unsigned char line_range;
112     unsigned char opcode_base;  /* number assigned to first special
113                                    opcode */
114     unsigned char *standard_opcode_lengths;
115   }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in.  */
119 static int dwarf2_die_debug = 0;
120
121 /* When non-zero, cross-check physname against demangler.  */
122 static int check_physname = 0;
123
124 static int pagesize;
125
126 /* When set, the file that we're processing is known to have debugging
127    info for C++ namespaces.  GCC 3.3.x did not produce this information,
128    but later versions do.  */
129
130 static int processing_has_namespace_info;
131
132 static const struct objfile_data *dwarf2_objfile_data_key;
133
134 struct dwarf2_section_info
135 {
136   asection *asection;
137   gdb_byte *buffer;
138   bfd_size_type size;
139   /* Not NULL if the section was actually mmapped.  */
140   void *map_addr;
141   /* Page aligned size of mmapped area.  */
142   bfd_size_type map_len;
143   /* True if we have tried to read this section.  */
144   int readin;
145 };
146
147 typedef struct dwarf2_section_info dwarf2_section_info_def;
148 DEF_VEC_O (dwarf2_section_info_def);
149
150 /* All offsets in the index are of this type.  It must be
151    architecture-independent.  */
152 typedef uint32_t offset_type;
153
154 DEF_VEC_I (offset_type);
155
156 /* A description of the mapped index.  The file format is described in
157    a comment by the code that writes the index.  */
158 struct mapped_index
159 {
160   /* Index data format version.  */
161   int version;
162
163   /* The total length of the buffer.  */
164   off_t total_size;
165
166   /* A pointer to the address table data.  */
167   const gdb_byte *address_table;
168
169   /* Size of the address table data in bytes.  */
170   offset_type address_table_size;
171
172   /* The symbol table, implemented as a hash table.  */
173   const offset_type *symbol_table;
174
175   /* Size in slots, each slot is 2 offset_types.  */
176   offset_type symbol_table_slots;
177
178   /* A pointer to the constant pool.  */
179   const char *constant_pool;
180 };
181
182 struct dwarf2_per_objfile
183 {
184   struct dwarf2_section_info info;
185   struct dwarf2_section_info abbrev;
186   struct dwarf2_section_info line;
187   struct dwarf2_section_info loc;
188   struct dwarf2_section_info macinfo;
189   struct dwarf2_section_info macro;
190   struct dwarf2_section_info str;
191   struct dwarf2_section_info ranges;
192   struct dwarf2_section_info frame;
193   struct dwarf2_section_info eh_frame;
194   struct dwarf2_section_info gdb_index;
195
196   VEC (dwarf2_section_info_def) *types;
197
198   /* Back link.  */
199   struct objfile *objfile;
200
201   /* A list of all the compilation units.  This is used to locate
202      the target compilation unit of a particular reference.  */
203   struct dwarf2_per_cu_data **all_comp_units;
204
205   /* The number of compilation units in ALL_COMP_UNITS.  */
206   int n_comp_units;
207
208   /* The number of .debug_types-related CUs.  */
209   int n_type_comp_units;
210
211   /* The .debug_types-related CUs.  */
212   struct dwarf2_per_cu_data **type_comp_units;
213
214   /* A chain of compilation units that are currently read in, so that
215      they can be freed later.  */
216   struct dwarf2_per_cu_data *read_in_chain;
217
218   /* A table mapping .debug_types signatures to its signatured_type entry.
219      This is NULL if the .debug_types section hasn't been read in yet.  */
220   htab_t signatured_types;
221
222   /* A flag indicating wether this objfile has a section loaded at a
223      VMA of 0.  */
224   int has_section_at_zero;
225
226   /* True if we are using the mapped index,
227      or we are faking it for OBJF_READNOW's sake.  */
228   unsigned char using_index;
229
230   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
231   struct mapped_index *index_table;
232
233   /* When using index_table, this keeps track of all quick_file_names entries.
234      TUs can share line table entries with CUs or other TUs, and there can be
235      a lot more TUs than unique line tables, so we maintain a separate table
236      of all line table entries to support the sharing.  */
237   htab_t quick_file_names_table;
238
239   /* Set during partial symbol reading, to prevent queueing of full
240      symbols.  */
241   int reading_partial_symbols;
242
243   /* Table mapping type .debug_info DIE offsets to types.
244      This is NULL if not allocated yet.
245      It (currently) makes sense to allocate debug_types_type_hash lazily.
246      To keep things simple we allocate both lazily.  */
247   htab_t debug_info_type_hash;
248
249   /* Table mapping type .debug_types DIE offsets to types.
250      This is NULL if not allocated yet.  */
251   htab_t debug_types_type_hash;
252 };
253
254 static struct dwarf2_per_objfile *dwarf2_per_objfile;
255
256 /* Default names of the debugging sections.  */
257
258 /* Note that if the debugging section has been compressed, it might
259    have a name like .zdebug_info.  */
260
261 static const struct dwarf2_debug_sections dwarf2_elf_names = {
262   { ".debug_info", ".zdebug_info" },
263   { ".debug_abbrev", ".zdebug_abbrev" },
264   { ".debug_line", ".zdebug_line" },
265   { ".debug_loc", ".zdebug_loc" },
266   { ".debug_macinfo", ".zdebug_macinfo" },
267   { ".debug_macro", ".zdebug_macro" },
268   { ".debug_str", ".zdebug_str" },
269   { ".debug_ranges", ".zdebug_ranges" },
270   { ".debug_types", ".zdebug_types" },
271   { ".debug_frame", ".zdebug_frame" },
272   { ".eh_frame", NULL },
273   { ".gdb_index", ".zgdb_index" },
274   23
275 };
276
277 /* local data types */
278
279 /* We hold several abbreviation tables in memory at the same time.  */
280 #ifndef ABBREV_HASH_SIZE
281 #define ABBREV_HASH_SIZE 121
282 #endif
283
284 /* The data in a compilation unit header, after target2host
285    translation, looks like this.  */
286 struct comp_unit_head
287 {
288   unsigned int length;
289   short version;
290   unsigned char addr_size;
291   unsigned char signed_addr_p;
292   unsigned int abbrev_offset;
293
294   /* Size of file offsets; either 4 or 8.  */
295   unsigned int offset_size;
296
297   /* Size of the length field; either 4 or 12.  */
298   unsigned int initial_length_size;
299
300   /* Offset to the first byte of this compilation unit header in the
301      .debug_info section, for resolving relative reference dies.  */
302   unsigned int offset;
303
304   /* Offset to first die in this cu from the start of the cu.
305      This will be the first byte following the compilation unit header.  */
306   unsigned int first_die_offset;
307 };
308
309 /* Type used for delaying computation of method physnames.
310    See comments for compute_delayed_physnames.  */
311 struct delayed_method_info
312 {
313   /* The type to which the method is attached, i.e., its parent class.  */
314   struct type *type;
315
316   /* The index of the method in the type's function fieldlists.  */
317   int fnfield_index;
318
319   /* The index of the method in the fieldlist.  */
320   int index;
321
322   /* The name of the DIE.  */
323   const char *name;
324
325   /*  The DIE associated with this method.  */
326   struct die_info *die;
327 };
328
329 typedef struct delayed_method_info delayed_method_info;
330 DEF_VEC_O (delayed_method_info);
331
332 /* Internal state when decoding a particular compilation unit.  */
333 struct dwarf2_cu
334 {
335   /* The objfile containing this compilation unit.  */
336   struct objfile *objfile;
337
338   /* The header of the compilation unit.  */
339   struct comp_unit_head header;
340
341   /* Base address of this compilation unit.  */
342   CORE_ADDR base_address;
343
344   /* Non-zero if base_address has been set.  */
345   int base_known;
346
347   struct function_range *first_fn, *last_fn, *cached_fn;
348
349   /* The language we are debugging.  */
350   enum language language;
351   const struct language_defn *language_defn;
352
353   const char *producer;
354
355   /* The generic symbol table building routines have separate lists for
356      file scope symbols and all all other scopes (local scopes).  So
357      we need to select the right one to pass to add_symbol_to_list().
358      We do it by keeping a pointer to the correct list in list_in_scope.
359
360      FIXME: The original dwarf code just treated the file scope as the
361      first local scope, and all other local scopes as nested local
362      scopes, and worked fine.  Check to see if we really need to
363      distinguish these in buildsym.c.  */
364   struct pending **list_in_scope;
365
366   /* DWARF abbreviation table associated with this compilation unit.  */
367   struct abbrev_info **dwarf2_abbrevs;
368
369   /* Storage for the abbrev table.  */
370   struct obstack abbrev_obstack;
371
372   /* Hash table holding all the loaded partial DIEs.  */
373   htab_t partial_dies;
374
375   /* Storage for things with the same lifetime as this read-in compilation
376      unit, including partial DIEs.  */
377   struct obstack comp_unit_obstack;
378
379   /* When multiple dwarf2_cu structures are living in memory, this field
380      chains them all together, so that they can be released efficiently.
381      We will probably also want a generation counter so that most-recently-used
382      compilation units are cached...  */
383   struct dwarf2_per_cu_data *read_in_chain;
384
385   /* Backchain to our per_cu entry if the tree has been built.  */
386   struct dwarf2_per_cu_data *per_cu;
387
388   /* How many compilation units ago was this CU last referenced?  */
389   int last_used;
390
391   /* A hash table of die offsets for following references.  */
392   htab_t die_hash;
393
394   /* Full DIEs if read in.  */
395   struct die_info *dies;
396
397   /* A set of pointers to dwarf2_per_cu_data objects for compilation
398      units referenced by this one.  Only set during full symbol processing;
399      partial symbol tables do not have dependencies.  */
400   htab_t dependencies;
401
402   /* Header data from the line table, during full symbol processing.  */
403   struct line_header *line_header;
404
405   /* A list of methods which need to have physnames computed
406      after all type information has been read.  */
407   VEC (delayed_method_info) *method_list;
408
409   /* To be copied to symtab->call_site_htab.  */
410   htab_t call_site_htab;
411
412   /* Mark used when releasing cached dies.  */
413   unsigned int mark : 1;
414
415   /* This flag will be set if this compilation unit might include
416      inter-compilation-unit references.  */
417   unsigned int has_form_ref_addr : 1;
418
419   /* This flag will be set if this compilation unit includes any
420      DW_TAG_namespace DIEs.  If we know that there are explicit
421      DIEs for namespaces, we don't need to try to infer them
422      from mangled names.  */
423   unsigned int has_namespace_info : 1;
424
425   /* This CU references .debug_loc.  See the symtab->locations_valid field.
426      This test is imperfect as there may exist optimized debug code not using
427      any location list and still facing inlining issues if handled as
428      unoptimized code.  For a future better test see GCC PR other/32998.  */
429
430   unsigned int has_loclist : 1;
431 };
432
433 /* Persistent data held for a compilation unit, even when not
434    processing it.  We put a pointer to this structure in the
435    read_symtab_private field of the psymtab.  If we encounter
436    inter-compilation-unit references, we also maintain a sorted
437    list of all compilation units.  */
438
439 struct dwarf2_per_cu_data
440 {
441   /* The start offset and length of this compilation unit.  2**29-1
442      bytes should suffice to store the length of any compilation unit
443      - if it doesn't, GDB will fall over anyway.
444      NOTE: Unlike comp_unit_head.length, this length includes
445      initial_length_size.  */
446   unsigned int offset;
447   unsigned int length : 29;
448
449   /* Flag indicating this compilation unit will be read in before
450      any of the current compilation units are processed.  */
451   unsigned int queued : 1;
452
453   /* This flag will be set if we need to load absolutely all DIEs
454      for this compilation unit, instead of just the ones we think
455      are interesting.  It gets set if we look for a DIE in the
456      hash table and don't find it.  */
457   unsigned int load_all_dies : 1;
458
459   /* Non-null if this CU is from .debug_types; in which case it points
460      to the section.  Otherwise it's from .debug_info.  */
461   struct dwarf2_section_info *debug_types_section;
462
463   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
464      of the CU cache it gets reset to NULL again.  */
465   struct dwarf2_cu *cu;
466
467   /* The corresponding objfile.  */
468   struct objfile *objfile;
469
470   /* When using partial symbol tables, the 'psymtab' field is active.
471      Otherwise the 'quick' field is active.  */
472   union
473   {
474     /* The partial symbol table associated with this compilation unit,
475        or NULL for partial units (which do not have an associated
476        symtab).  */
477     struct partial_symtab *psymtab;
478
479     /* Data needed by the "quick" functions.  */
480     struct dwarf2_per_cu_quick_data *quick;
481   } v;
482 };
483
484 /* Entry in the signatured_types hash table.  */
485
486 struct signatured_type
487 {
488   ULONGEST signature;
489
490   /* Offset in .debug_types of the type defined by this TU.  */
491   unsigned int type_offset;
492
493   /* The CU(/TU) of this type.  */
494   struct dwarf2_per_cu_data per_cu;
495 };
496
497 /* Struct used to pass misc. parameters to read_die_and_children, et
498    al.  which are used for both .debug_info and .debug_types dies.
499    All parameters here are unchanging for the life of the call.  This
500    struct exists to abstract away the constant parameters of die
501    reading.  */
502
503 struct die_reader_specs
504 {
505   /* The bfd of this objfile.  */
506   bfd* abfd;
507
508   /* The CU of the DIE we are parsing.  */
509   struct dwarf2_cu *cu;
510
511   /* Pointer to start of section buffer.
512      This is either the start of .debug_info or .debug_types.  */
513   const gdb_byte *buffer;
514 };
515
516 /* The line number information for a compilation unit (found in the
517    .debug_line section) begins with a "statement program header",
518    which contains the following information.  */
519 struct line_header
520 {
521   unsigned int total_length;
522   unsigned short version;
523   unsigned int header_length;
524   unsigned char minimum_instruction_length;
525   unsigned char maximum_ops_per_instruction;
526   unsigned char default_is_stmt;
527   int line_base;
528   unsigned char line_range;
529   unsigned char opcode_base;
530
531   /* standard_opcode_lengths[i] is the number of operands for the
532      standard opcode whose value is i.  This means that
533      standard_opcode_lengths[0] is unused, and the last meaningful
534      element is standard_opcode_lengths[opcode_base - 1].  */
535   unsigned char *standard_opcode_lengths;
536
537   /* The include_directories table.  NOTE!  These strings are not
538      allocated with xmalloc; instead, they are pointers into
539      debug_line_buffer.  If you try to free them, `free' will get
540      indigestion.  */
541   unsigned int num_include_dirs, include_dirs_size;
542   char **include_dirs;
543
544   /* The file_names table.  NOTE!  These strings are not allocated
545      with xmalloc; instead, they are pointers into debug_line_buffer.
546      Don't try to free them directly.  */
547   unsigned int num_file_names, file_names_size;
548   struct file_entry
549   {
550     char *name;
551     unsigned int dir_index;
552     unsigned int mod_time;
553     unsigned int length;
554     int included_p; /* Non-zero if referenced by the Line Number Program.  */
555     struct symtab *symtab; /* The associated symbol table, if any.  */
556   } *file_names;
557
558   /* The start and end of the statement program following this
559      header.  These point into dwarf2_per_objfile->line_buffer.  */
560   gdb_byte *statement_program_start, *statement_program_end;
561 };
562
563 /* When we construct a partial symbol table entry we only
564    need this much information.  */
565 struct partial_die_info
566   {
567     /* Offset of this DIE.  */
568     unsigned int offset;
569
570     /* DWARF-2 tag for this DIE.  */
571     ENUM_BITFIELD(dwarf_tag) tag : 16;
572
573     /* Assorted flags describing the data found in this DIE.  */
574     unsigned int has_children : 1;
575     unsigned int is_external : 1;
576     unsigned int is_declaration : 1;
577     unsigned int has_type : 1;
578     unsigned int has_specification : 1;
579     unsigned int has_pc_info : 1;
580
581     /* Flag set if the SCOPE field of this structure has been
582        computed.  */
583     unsigned int scope_set : 1;
584
585     /* Flag set if the DIE has a byte_size attribute.  */
586     unsigned int has_byte_size : 1;
587
588     /* Flag set if any of the DIE's children are template arguments.  */
589     unsigned int has_template_arguments : 1;
590
591     /* Flag set if fixup_partial_die has been called on this die.  */
592     unsigned int fixup_called : 1;
593
594     /* The name of this DIE.  Normally the value of DW_AT_name, but
595        sometimes a default name for unnamed DIEs.  */
596     char *name;
597
598     /* The linkage name, if present.  */
599     const char *linkage_name;
600
601     /* The scope to prepend to our children.  This is generally
602        allocated on the comp_unit_obstack, so will disappear
603        when this compilation unit leaves the cache.  */
604     char *scope;
605
606     /* The location description associated with this DIE, if any.  */
607     struct dwarf_block *locdesc;
608
609     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
610     CORE_ADDR lowpc;
611     CORE_ADDR highpc;
612
613     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
614        DW_AT_sibling, if any.  */
615     /* NOTE: This member isn't strictly necessary, read_partial_die could
616        return DW_AT_sibling values to its caller load_partial_dies.  */
617     gdb_byte *sibling;
618
619     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
620        DW_AT_specification (or DW_AT_abstract_origin or
621        DW_AT_extension).  */
622     unsigned int spec_offset;
623
624     /* Pointers to this DIE's parent, first child, and next sibling,
625        if any.  */
626     struct partial_die_info *die_parent, *die_child, *die_sibling;
627   };
628
629 /* This data structure holds the information of an abbrev.  */
630 struct abbrev_info
631   {
632     unsigned int number;        /* number identifying abbrev */
633     enum dwarf_tag tag;         /* dwarf tag */
634     unsigned short has_children;                /* boolean */
635     unsigned short num_attrs;   /* number of attributes */
636     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
637     struct abbrev_info *next;   /* next in chain */
638   };
639
640 struct attr_abbrev
641   {
642     ENUM_BITFIELD(dwarf_attribute) name : 16;
643     ENUM_BITFIELD(dwarf_form) form : 16;
644   };
645
646 /* Attributes have a name and a value.  */
647 struct attribute
648   {
649     ENUM_BITFIELD(dwarf_attribute) name : 16;
650     ENUM_BITFIELD(dwarf_form) form : 15;
651
652     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
653        field should be in u.str (existing only for DW_STRING) but it is kept
654        here for better struct attribute alignment.  */
655     unsigned int string_is_canonical : 1;
656
657     union
658       {
659         char *str;
660         struct dwarf_block *blk;
661         ULONGEST unsnd;
662         LONGEST snd;
663         CORE_ADDR addr;
664         struct signatured_type *signatured_type;
665       }
666     u;
667   };
668
669 /* This data structure holds a complete die structure.  */
670 struct die_info
671   {
672     /* DWARF-2 tag for this DIE.  */
673     ENUM_BITFIELD(dwarf_tag) tag : 16;
674
675     /* Number of attributes */
676     unsigned char num_attrs;
677
678     /* True if we're presently building the full type name for the
679        type derived from this DIE.  */
680     unsigned char building_fullname : 1;
681
682     /* Abbrev number */
683     unsigned int abbrev;
684
685     /* Offset in .debug_info or .debug_types section.  */
686     unsigned int offset;
687
688     /* The dies in a compilation unit form an n-ary tree.  PARENT
689        points to this die's parent; CHILD points to the first child of
690        this node; and all the children of a given node are chained
691        together via their SIBLING fields.  */
692     struct die_info *child;     /* Its first child, if any.  */
693     struct die_info *sibling;   /* Its next sibling, if any.  */
694     struct die_info *parent;    /* Its parent, if any.  */
695
696     /* An array of attributes, with NUM_ATTRS elements.  There may be
697        zero, but it's not common and zero-sized arrays are not
698        sufficiently portable C.  */
699     struct attribute attrs[1];
700   };
701
702 struct function_range
703 {
704   const char *name;
705   CORE_ADDR lowpc, highpc;
706   int seen_line;
707   struct function_range *next;
708 };
709
710 /* Get at parts of an attribute structure.  */
711
712 #define DW_STRING(attr)    ((attr)->u.str)
713 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
714 #define DW_UNSND(attr)     ((attr)->u.unsnd)
715 #define DW_BLOCK(attr)     ((attr)->u.blk)
716 #define DW_SND(attr)       ((attr)->u.snd)
717 #define DW_ADDR(attr)      ((attr)->u.addr)
718 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
719
720 /* Blocks are a bunch of untyped bytes.  */
721 struct dwarf_block
722   {
723     unsigned int size;
724
725     /* Valid only if SIZE is not zero.  */
726     gdb_byte *data;
727   };
728
729 #ifndef ATTR_ALLOC_CHUNK
730 #define ATTR_ALLOC_CHUNK 4
731 #endif
732
733 /* Allocate fields for structs, unions and enums in this size.  */
734 #ifndef DW_FIELD_ALLOC_CHUNK
735 #define DW_FIELD_ALLOC_CHUNK 4
736 #endif
737
738 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
739    but this would require a corresponding change in unpack_field_as_long
740    and friends.  */
741 static int bits_per_byte = 8;
742
743 /* The routines that read and process dies for a C struct or C++ class
744    pass lists of data member fields and lists of member function fields
745    in an instance of a field_info structure, as defined below.  */
746 struct field_info
747   {
748     /* List of data member and baseclasses fields.  */
749     struct nextfield
750       {
751         struct nextfield *next;
752         int accessibility;
753         int virtuality;
754         struct field field;
755       }
756      *fields, *baseclasses;
757
758     /* Number of fields (including baseclasses).  */
759     int nfields;
760
761     /* Number of baseclasses.  */
762     int nbaseclasses;
763
764     /* Set if the accesibility of one of the fields is not public.  */
765     int non_public_fields;
766
767     /* Member function fields array, entries are allocated in the order they
768        are encountered in the object file.  */
769     struct nextfnfield
770       {
771         struct nextfnfield *next;
772         struct fn_field fnfield;
773       }
774      *fnfields;
775
776     /* Member function fieldlist array, contains name of possibly overloaded
777        member function, number of overloaded member functions and a pointer
778        to the head of the member function field chain.  */
779     struct fnfieldlist
780       {
781         char *name;
782         int length;
783         struct nextfnfield *head;
784       }
785      *fnfieldlists;
786
787     /* Number of entries in the fnfieldlists array.  */
788     int nfnfields;
789
790     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
791        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
792     struct typedef_field_list
793       {
794         struct typedef_field field;
795         struct typedef_field_list *next;
796       }
797     *typedef_field_list;
798     unsigned typedef_field_list_count;
799   };
800
801 /* One item on the queue of compilation units to read in full symbols
802    for.  */
803 struct dwarf2_queue_item
804 {
805   struct dwarf2_per_cu_data *per_cu;
806   struct dwarf2_queue_item *next;
807 };
808
809 /* The current queue.  */
810 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
811
812 /* Loaded secondary compilation units are kept in memory until they
813    have not been referenced for the processing of this many
814    compilation units.  Set this to zero to disable caching.  Cache
815    sizes of up to at least twenty will improve startup time for
816    typical inter-CU-reference binaries, at an obvious memory cost.  */
817 static int dwarf2_max_cache_age = 5;
818 static void
819 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
820                            struct cmd_list_element *c, const char *value)
821 {
822   fprintf_filtered (file, _("The upper bound on the age of cached "
823                             "dwarf2 compilation units is %s.\n"),
824                     value);
825 }
826
827
828 /* Various complaints about symbol reading that don't abort the process.  */
829
830 static void
831 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
832 {
833   complaint (&symfile_complaints,
834              _("statement list doesn't fit in .debug_line section"));
835 }
836
837 static void
838 dwarf2_debug_line_missing_file_complaint (void)
839 {
840   complaint (&symfile_complaints,
841              _(".debug_line section has line data without a file"));
842 }
843
844 static void
845 dwarf2_debug_line_missing_end_sequence_complaint (void)
846 {
847   complaint (&symfile_complaints,
848              _(".debug_line section has line "
849                "program sequence without an end"));
850 }
851
852 static void
853 dwarf2_complex_location_expr_complaint (void)
854 {
855   complaint (&symfile_complaints, _("location expression too complex"));
856 }
857
858 static void
859 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
860                                               int arg3)
861 {
862   complaint (&symfile_complaints,
863              _("const value length mismatch for '%s', got %d, expected %d"),
864              arg1, arg2, arg3);
865 }
866
867 static void
868 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
869 {
870   complaint (&symfile_complaints,
871              _("macro info runs off end of `%s' section"),
872              section->asection->name);
873 }
874
875 static void
876 dwarf2_macro_malformed_definition_complaint (const char *arg1)
877 {
878   complaint (&symfile_complaints,
879              _("macro debug info contains a "
880                "malformed macro definition:\n`%s'"),
881              arg1);
882 }
883
884 static void
885 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
886 {
887   complaint (&symfile_complaints,
888              _("invalid attribute class or form for '%s' in '%s'"),
889              arg1, arg2);
890 }
891
892 /* local function prototypes */
893
894 static void dwarf2_locate_sections (bfd *, asection *, void *);
895
896 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
897                                            struct objfile *);
898
899 static void dwarf2_find_base_address (struct die_info *die,
900                                       struct dwarf2_cu *cu);
901
902 static void dwarf2_build_psymtabs_hard (struct objfile *);
903
904 static void scan_partial_symbols (struct partial_die_info *,
905                                   CORE_ADDR *, CORE_ADDR *,
906                                   int, struct dwarf2_cu *);
907
908 static void add_partial_symbol (struct partial_die_info *,
909                                 struct dwarf2_cu *);
910
911 static void add_partial_namespace (struct partial_die_info *pdi,
912                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
913                                    int need_pc, struct dwarf2_cu *cu);
914
915 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
916                                 CORE_ADDR *highpc, int need_pc,
917                                 struct dwarf2_cu *cu);
918
919 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
920                                      struct dwarf2_cu *cu);
921
922 static void add_partial_subprogram (struct partial_die_info *pdi,
923                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
924                                     int need_pc, struct dwarf2_cu *cu);
925
926 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
927                                      gdb_byte *buffer, gdb_byte *info_ptr,
928                                      bfd *abfd, struct dwarf2_cu *cu);
929
930 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
931
932 static void psymtab_to_symtab_1 (struct partial_symtab *);
933
934 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
935
936 static void dwarf2_free_abbrev_table (void *);
937
938 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
939
940 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
941                                             struct dwarf2_cu *);
942
943 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
944                                                  struct dwarf2_cu *);
945
946 static struct partial_die_info *load_partial_dies (bfd *,
947                                                    gdb_byte *, gdb_byte *,
948                                                    int, struct dwarf2_cu *);
949
950 static gdb_byte *read_partial_die (struct partial_die_info *,
951                                    struct abbrev_info *abbrev,
952                                    unsigned int, bfd *,
953                                    gdb_byte *, gdb_byte *,
954                                    struct dwarf2_cu *);
955
956 static struct partial_die_info *find_partial_die (unsigned int,
957                                                   struct dwarf2_cu *);
958
959 static void fixup_partial_die (struct partial_die_info *,
960                                struct dwarf2_cu *);
961
962 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
963                                  bfd *, gdb_byte *, struct dwarf2_cu *);
964
965 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
966                                        bfd *, gdb_byte *, struct dwarf2_cu *);
967
968 static unsigned int read_1_byte (bfd *, gdb_byte *);
969
970 static int read_1_signed_byte (bfd *, gdb_byte *);
971
972 static unsigned int read_2_bytes (bfd *, gdb_byte *);
973
974 static unsigned int read_4_bytes (bfd *, gdb_byte *);
975
976 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
977
978 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
979                                unsigned int *);
980
981 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
982
983 static LONGEST read_checked_initial_length_and_offset
984   (bfd *, gdb_byte *, const struct comp_unit_head *,
985    unsigned int *, unsigned int *);
986
987 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
988                             unsigned int *);
989
990 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
991
992 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
993
994 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
995
996 static char *read_indirect_string (bfd *, gdb_byte *,
997                                    const struct comp_unit_head *,
998                                    unsigned int *);
999
1000 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1001
1002 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1003
1004 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1005
1006 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1007
1008 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1009                                       struct dwarf2_cu *);
1010
1011 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1012                                                 unsigned int,
1013                                                 struct dwarf2_cu *);
1014
1015 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1016                                struct dwarf2_cu *cu);
1017
1018 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1019
1020 static struct die_info *die_specification (struct die_info *die,
1021                                            struct dwarf2_cu **);
1022
1023 static void free_line_header (struct line_header *lh);
1024
1025 static void add_file_name (struct line_header *, char *, unsigned int,
1026                            unsigned int, unsigned int);
1027
1028 static struct line_header *(dwarf_decode_line_header
1029                             (unsigned int offset,
1030                              bfd *abfd, struct dwarf2_cu *cu));
1031
1032 static void dwarf_decode_lines (struct line_header *, const char *,
1033                                 struct dwarf2_cu *, struct partial_symtab *,
1034                                 int);
1035
1036 static void dwarf2_start_subfile (char *, const char *, const char *);
1037
1038 static struct symbol *new_symbol (struct die_info *, struct type *,
1039                                   struct dwarf2_cu *);
1040
1041 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1042                                        struct dwarf2_cu *, struct symbol *);
1043
1044 static void dwarf2_const_value (struct attribute *, struct symbol *,
1045                                 struct dwarf2_cu *);
1046
1047 static void dwarf2_const_value_attr (struct attribute *attr,
1048                                      struct type *type,
1049                                      const char *name,
1050                                      struct obstack *obstack,
1051                                      struct dwarf2_cu *cu, long *value,
1052                                      gdb_byte **bytes,
1053                                      struct dwarf2_locexpr_baton **baton);
1054
1055 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1056
1057 static int need_gnat_info (struct dwarf2_cu *);
1058
1059 static struct type *die_descriptive_type (struct die_info *,
1060                                           struct dwarf2_cu *);
1061
1062 static void set_descriptive_type (struct type *, struct die_info *,
1063                                   struct dwarf2_cu *);
1064
1065 static struct type *die_containing_type (struct die_info *,
1066                                          struct dwarf2_cu *);
1067
1068 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1069                                      struct dwarf2_cu *);
1070
1071 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1072
1073 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1074
1075 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1076
1077 static char *typename_concat (struct obstack *obs, const char *prefix,
1078                               const char *suffix, int physname,
1079                               struct dwarf2_cu *cu);
1080
1081 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1082
1083 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1084
1085 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1086
1087 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1090
1091 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1092                                struct dwarf2_cu *, struct partial_symtab *);
1093
1094 static int dwarf2_get_pc_bounds (struct die_info *,
1095                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1096                                  struct partial_symtab *);
1097
1098 static void get_scope_pc_bounds (struct die_info *,
1099                                  CORE_ADDR *, CORE_ADDR *,
1100                                  struct dwarf2_cu *);
1101
1102 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1103                                         CORE_ADDR, struct dwarf2_cu *);
1104
1105 static void dwarf2_add_field (struct field_info *, struct die_info *,
1106                               struct dwarf2_cu *);
1107
1108 static void dwarf2_attach_fields_to_type (struct field_info *,
1109                                           struct type *, struct dwarf2_cu *);
1110
1111 static void dwarf2_add_member_fn (struct field_info *,
1112                                   struct die_info *, struct type *,
1113                                   struct dwarf2_cu *);
1114
1115 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1116                                              struct type *,
1117                                              struct dwarf2_cu *);
1118
1119 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1120
1121 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1122
1123 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1124
1125 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1126
1127 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1128
1129 static struct type *read_module_type (struct die_info *die,
1130                                       struct dwarf2_cu *cu);
1131
1132 static const char *namespace_name (struct die_info *die,
1133                                    int *is_anonymous, struct dwarf2_cu *);
1134
1135 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1136
1137 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1138
1139 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1140                                                        struct dwarf2_cu *);
1141
1142 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1143
1144 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1145                                                  gdb_byte *info_ptr,
1146                                                  gdb_byte **new_info_ptr,
1147                                                  struct die_info *parent);
1148
1149 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1150                                                gdb_byte *info_ptr,
1151                                                gdb_byte **new_info_ptr,
1152                                                struct die_info *parent);
1153
1154 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1155                                                gdb_byte *info_ptr,
1156                                                gdb_byte **new_info_ptr,
1157                                                struct die_info *parent);
1158
1159 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1160                                 struct die_info **, gdb_byte *,
1161                                 int *);
1162
1163 static void process_die (struct die_info *, struct dwarf2_cu *);
1164
1165 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1166                                        struct obstack *);
1167
1168 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1169
1170 static const char *dwarf2_full_name (char *name,
1171                                      struct die_info *die,
1172                                      struct dwarf2_cu *cu);
1173
1174 static struct die_info *dwarf2_extension (struct die_info *die,
1175                                           struct dwarf2_cu **);
1176
1177 static char *dwarf_tag_name (unsigned int);
1178
1179 static char *dwarf_attr_name (unsigned int);
1180
1181 static char *dwarf_form_name (unsigned int);
1182
1183 static char *dwarf_bool_name (unsigned int);
1184
1185 static char *dwarf_type_encoding_name (unsigned int);
1186
1187 #if 0
1188 static char *dwarf_cfi_name (unsigned int);
1189 #endif
1190
1191 static struct die_info *sibling_die (struct die_info *);
1192
1193 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1194
1195 static void dump_die_for_error (struct die_info *);
1196
1197 static void dump_die_1 (struct ui_file *, int level, int max_level,
1198                         struct die_info *);
1199
1200 /*static*/ void dump_die (struct die_info *, int max_level);
1201
1202 static void store_in_ref_table (struct die_info *,
1203                                 struct dwarf2_cu *);
1204
1205 static int is_ref_attr (struct attribute *);
1206
1207 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1208
1209 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1210
1211 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1212                                                struct attribute *,
1213                                                struct dwarf2_cu **);
1214
1215 static struct die_info *follow_die_ref (struct die_info *,
1216                                         struct attribute *,
1217                                         struct dwarf2_cu **);
1218
1219 static struct die_info *follow_die_sig (struct die_info *,
1220                                         struct attribute *,
1221                                         struct dwarf2_cu **);
1222
1223 static struct signatured_type *lookup_signatured_type_at_offset
1224     (struct objfile *objfile,
1225      struct dwarf2_section_info *section,
1226      unsigned int offset);
1227
1228 static void read_signatured_type_at_offset (struct objfile *objfile,
1229                                             struct dwarf2_section_info *sect,
1230                                             unsigned int offset);
1231
1232 static void read_signatured_type (struct objfile *,
1233                                   struct signatured_type *type_sig);
1234
1235 /* memory allocation interface */
1236
1237 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1238
1239 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1240
1241 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1242
1243 static void initialize_cu_func_list (struct dwarf2_cu *);
1244
1245 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1246                                  struct dwarf2_cu *);
1247
1248 static void dwarf_decode_macros (struct line_header *, unsigned int,
1249                                  char *, bfd *, struct dwarf2_cu *,
1250                                  struct dwarf2_section_info *,
1251                                  int);
1252
1253 static int attr_form_is_block (struct attribute *);
1254
1255 static int attr_form_is_section_offset (struct attribute *);
1256
1257 static int attr_form_is_constant (struct attribute *);
1258
1259 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1260                                    struct dwarf2_loclist_baton *baton,
1261                                    struct attribute *attr);
1262
1263 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1264                                          struct symbol *sym,
1265                                          struct dwarf2_cu *cu);
1266
1267 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1268                                struct abbrev_info *abbrev,
1269                                struct dwarf2_cu *cu);
1270
1271 static void free_stack_comp_unit (void *);
1272
1273 static hashval_t partial_die_hash (const void *item);
1274
1275 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1276
1277 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1278   (unsigned int offset, struct objfile *objfile);
1279
1280 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1281   (unsigned int offset, struct objfile *objfile);
1282
1283 static void init_one_comp_unit (struct dwarf2_cu *cu,
1284                                 struct objfile *objfile);
1285
1286 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1287                                    struct die_info *comp_unit_die);
1288
1289 static void free_one_comp_unit (void *);
1290
1291 static void free_cached_comp_units (void *);
1292
1293 static void age_cached_comp_units (void);
1294
1295 static void free_one_cached_comp_unit (void *);
1296
1297 static struct type *set_die_type (struct die_info *, struct type *,
1298                                   struct dwarf2_cu *);
1299
1300 static void create_all_comp_units (struct objfile *);
1301
1302 static int create_debug_types_hash_table (struct objfile *objfile);
1303
1304 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1305                                  struct objfile *);
1306
1307 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1308
1309 static void dwarf2_add_dependence (struct dwarf2_cu *,
1310                                    struct dwarf2_per_cu_data *);
1311
1312 static void dwarf2_mark (struct dwarf2_cu *);
1313
1314 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1315
1316 static struct type *get_die_type_at_offset (unsigned int,
1317                                             struct dwarf2_per_cu_data *per_cu);
1318
1319 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1320
1321 static void dwarf2_release_queue (void *dummy);
1322
1323 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1324                              struct objfile *objfile);
1325
1326 static void process_queue (struct objfile *objfile);
1327
1328 static void find_file_and_directory (struct die_info *die,
1329                                      struct dwarf2_cu *cu,
1330                                      char **name, char **comp_dir);
1331
1332 static char *file_full_name (int file, struct line_header *lh,
1333                              const char *comp_dir);
1334
1335 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1336                                               gdb_byte *info_ptr,
1337                                               gdb_byte *buffer,
1338                                               unsigned int buffer_size,
1339                                               bfd *abfd,
1340                                               int is_debug_types_section);
1341
1342 static void init_cu_die_reader (struct die_reader_specs *reader,
1343                                 struct dwarf2_cu *cu);
1344
1345 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1346
1347 #if WORDS_BIGENDIAN
1348
1349 /* Convert VALUE between big- and little-endian.  */
1350 static offset_type
1351 byte_swap (offset_type value)
1352 {
1353   offset_type result;
1354
1355   result = (value & 0xff) << 24;
1356   result |= (value & 0xff00) << 8;
1357   result |= (value & 0xff0000) >> 8;
1358   result |= (value & 0xff000000) >> 24;
1359   return result;
1360 }
1361
1362 #define MAYBE_SWAP(V)  byte_swap (V)
1363
1364 #else
1365 #define MAYBE_SWAP(V) (V)
1366 #endif /* WORDS_BIGENDIAN */
1367
1368 /* The suffix for an index file.  */
1369 #define INDEX_SUFFIX ".gdb-index"
1370
1371 static const char *dwarf2_physname (char *name, struct die_info *die,
1372                                     struct dwarf2_cu *cu);
1373
1374 /* Try to locate the sections we need for DWARF 2 debugging
1375    information and return true if we have enough to do something.
1376    NAMES points to the dwarf2 section names, or is NULL if the standard
1377    ELF names are used.  */
1378
1379 int
1380 dwarf2_has_info (struct objfile *objfile,
1381                  const struct dwarf2_debug_sections *names)
1382 {
1383   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1384   if (!dwarf2_per_objfile)
1385     {
1386       /* Initialize per-objfile state.  */
1387       struct dwarf2_per_objfile *data
1388         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1389
1390       memset (data, 0, sizeof (*data));
1391       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1392       dwarf2_per_objfile = data;
1393
1394       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1395                              (void *) names);
1396       dwarf2_per_objfile->objfile = objfile;
1397     }
1398   return (dwarf2_per_objfile->info.asection != NULL
1399           && dwarf2_per_objfile->abbrev.asection != NULL);
1400 }
1401
1402 /* When loading sections, we look either for uncompressed section or for
1403    compressed section names.  */
1404
1405 static int
1406 section_is_p (const char *section_name,
1407               const struct dwarf2_section_names *names)
1408 {
1409   if (names->normal != NULL
1410       && strcmp (section_name, names->normal) == 0)
1411     return 1;
1412   if (names->compressed != NULL
1413       && strcmp (section_name, names->compressed) == 0)
1414     return 1;
1415   return 0;
1416 }
1417
1418 /* This function is mapped across the sections and remembers the
1419    offset and size of each of the debugging sections we are interested
1420    in.  */
1421
1422 static void
1423 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1424 {
1425   const struct dwarf2_debug_sections *names;
1426
1427   if (vnames == NULL)
1428     names = &dwarf2_elf_names;
1429   else
1430     names = (const struct dwarf2_debug_sections *) vnames;
1431
1432   if (section_is_p (sectp->name, &names->info))
1433     {
1434       dwarf2_per_objfile->info.asection = sectp;
1435       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1436     }
1437   else if (section_is_p (sectp->name, &names->abbrev))
1438     {
1439       dwarf2_per_objfile->abbrev.asection = sectp;
1440       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1441     }
1442   else if (section_is_p (sectp->name, &names->line))
1443     {
1444       dwarf2_per_objfile->line.asection = sectp;
1445       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1446     }
1447   else if (section_is_p (sectp->name, &names->loc))
1448     {
1449       dwarf2_per_objfile->loc.asection = sectp;
1450       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1451     }
1452   else if (section_is_p (sectp->name, &names->macinfo))
1453     {
1454       dwarf2_per_objfile->macinfo.asection = sectp;
1455       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1456     }
1457   else if (section_is_p (sectp->name, &names->macro))
1458     {
1459       dwarf2_per_objfile->macro.asection = sectp;
1460       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1461     }
1462   else if (section_is_p (sectp->name, &names->str))
1463     {
1464       dwarf2_per_objfile->str.asection = sectp;
1465       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1466     }
1467   else if (section_is_p (sectp->name, &names->frame))
1468     {
1469       dwarf2_per_objfile->frame.asection = sectp;
1470       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1471     }
1472   else if (section_is_p (sectp->name, &names->eh_frame))
1473     {
1474       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1475
1476       if (aflag & SEC_HAS_CONTENTS)
1477         {
1478           dwarf2_per_objfile->eh_frame.asection = sectp;
1479           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1480         }
1481     }
1482   else if (section_is_p (sectp->name, &names->ranges))
1483     {
1484       dwarf2_per_objfile->ranges.asection = sectp;
1485       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1486     }
1487   else if (section_is_p (sectp->name, &names->types))
1488     {
1489       struct dwarf2_section_info type_section;
1490
1491       memset (&type_section, 0, sizeof (type_section));
1492       type_section.asection = sectp;
1493       type_section.size = bfd_get_section_size (sectp);
1494
1495       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1496                      &type_section);
1497     }
1498   else if (section_is_p (sectp->name, &names->gdb_index))
1499     {
1500       dwarf2_per_objfile->gdb_index.asection = sectp;
1501       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1502     }
1503
1504   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1505       && bfd_section_vma (abfd, sectp) == 0)
1506     dwarf2_per_objfile->has_section_at_zero = 1;
1507 }
1508
1509 /* Decompress a section that was compressed using zlib.  Store the
1510    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1511
1512 static void
1513 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1514                          gdb_byte **outbuf, bfd_size_type *outsize)
1515 {
1516   bfd *abfd = objfile->obfd;
1517 #ifndef HAVE_ZLIB_H
1518   error (_("Support for zlib-compressed DWARF data (from '%s') "
1519            "is disabled in this copy of GDB"),
1520          bfd_get_filename (abfd));
1521 #else
1522   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1523   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1524   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1525   bfd_size_type uncompressed_size;
1526   gdb_byte *uncompressed_buffer;
1527   z_stream strm;
1528   int rc;
1529   int header_size = 12;
1530
1531   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1532       || bfd_bread (compressed_buffer,
1533                     compressed_size, abfd) != compressed_size)
1534     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1535            bfd_get_filename (abfd));
1536
1537   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1538      by the uncompressed section size, 8 bytes in big-endian order.  */
1539   if (compressed_size < header_size
1540       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1541     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1542            bfd_get_filename (abfd));
1543   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1544   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1545   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1546   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1547   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1548   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1549   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1550   uncompressed_size += compressed_buffer[11];
1551
1552   /* It is possible the section consists of several compressed
1553      buffers concatenated together, so we uncompress in a loop.  */
1554   strm.zalloc = NULL;
1555   strm.zfree = NULL;
1556   strm.opaque = NULL;
1557   strm.avail_in = compressed_size - header_size;
1558   strm.next_in = (Bytef*) compressed_buffer + header_size;
1559   strm.avail_out = uncompressed_size;
1560   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1561                                        uncompressed_size);
1562   rc = inflateInit (&strm);
1563   while (strm.avail_in > 0)
1564     {
1565       if (rc != Z_OK)
1566         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1567                bfd_get_filename (abfd), rc);
1568       strm.next_out = ((Bytef*) uncompressed_buffer
1569                        + (uncompressed_size - strm.avail_out));
1570       rc = inflate (&strm, Z_FINISH);
1571       if (rc != Z_STREAM_END)
1572         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1573                bfd_get_filename (abfd), rc);
1574       rc = inflateReset (&strm);
1575     }
1576   rc = inflateEnd (&strm);
1577   if (rc != Z_OK
1578       || strm.avail_out != 0)
1579     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1580            bfd_get_filename (abfd), rc);
1581
1582   do_cleanups (cleanup);
1583   *outbuf = uncompressed_buffer;
1584   *outsize = uncompressed_size;
1585 #endif
1586 }
1587
1588 /* A helper function that decides whether a section is empty.  */
1589
1590 static int
1591 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1592 {
1593   return info->asection == NULL || info->size == 0;
1594 }
1595
1596 /* Read the contents of the section SECTP from object file specified by
1597    OBJFILE, store info about the section into INFO.
1598    If the section is compressed, uncompress it before returning.  */
1599
1600 static void
1601 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1602 {
1603   bfd *abfd = objfile->obfd;
1604   asection *sectp = info->asection;
1605   gdb_byte *buf, *retbuf;
1606   unsigned char header[4];
1607
1608   if (info->readin)
1609     return;
1610   info->buffer = NULL;
1611   info->map_addr = NULL;
1612   info->readin = 1;
1613
1614   if (dwarf2_section_empty_p (info))
1615     return;
1616
1617   /* Check if the file has a 4-byte header indicating compression.  */
1618   if (info->size > sizeof (header)
1619       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1620       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1621     {
1622       /* Upon decompression, update the buffer and its size.  */
1623       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1624         {
1625           zlib_decompress_section (objfile, sectp, &info->buffer,
1626                                    &info->size);
1627           return;
1628         }
1629     }
1630
1631 #ifdef HAVE_MMAP
1632   if (pagesize == 0)
1633     pagesize = getpagesize ();
1634
1635   /* Only try to mmap sections which are large enough: we don't want to
1636      waste space due to fragmentation.  Also, only try mmap for sections
1637      without relocations.  */
1638
1639   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1640     {
1641       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1642                          MAP_PRIVATE, sectp->filepos,
1643                          &info->map_addr, &info->map_len);
1644
1645       if ((caddr_t)info->buffer != MAP_FAILED)
1646         {
1647 #if HAVE_POSIX_MADVISE
1648           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1649 #endif
1650           return;
1651         }
1652     }
1653 #endif
1654
1655   /* If we get here, we are a normal, not-compressed section.  */
1656   info->buffer = buf
1657     = obstack_alloc (&objfile->objfile_obstack, info->size);
1658
1659   /* When debugging .o files, we may need to apply relocations; see
1660      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1661      We never compress sections in .o files, so we only need to
1662      try this when the section is not compressed.  */
1663   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1664   if (retbuf != NULL)
1665     {
1666       info->buffer = retbuf;
1667       return;
1668     }
1669
1670   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1671       || bfd_bread (buf, info->size, abfd) != info->size)
1672     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1673            bfd_get_filename (abfd));
1674 }
1675
1676 /* A helper function that returns the size of a section in a safe way.
1677    If you are positive that the section has been read before using the
1678    size, then it is safe to refer to the dwarf2_section_info object's
1679    "size" field directly.  In other cases, you must call this
1680    function, because for compressed sections the size field is not set
1681    correctly until the section has been read.  */
1682
1683 static bfd_size_type
1684 dwarf2_section_size (struct objfile *objfile,
1685                      struct dwarf2_section_info *info)
1686 {
1687   if (!info->readin)
1688     dwarf2_read_section (objfile, info);
1689   return info->size;
1690 }
1691
1692 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1693    SECTION_NAME.  */
1694
1695 void
1696 dwarf2_get_section_info (struct objfile *objfile,
1697                          enum dwarf2_section_enum sect,
1698                          asection **sectp, gdb_byte **bufp,
1699                          bfd_size_type *sizep)
1700 {
1701   struct dwarf2_per_objfile *data
1702     = objfile_data (objfile, dwarf2_objfile_data_key);
1703   struct dwarf2_section_info *info;
1704
1705   /* We may see an objfile without any DWARF, in which case we just
1706      return nothing.  */
1707   if (data == NULL)
1708     {
1709       *sectp = NULL;
1710       *bufp = NULL;
1711       *sizep = 0;
1712       return;
1713     }
1714   switch (sect)
1715     {
1716     case DWARF2_DEBUG_FRAME:
1717       info = &data->frame;
1718       break;
1719     case DWARF2_EH_FRAME:
1720       info = &data->eh_frame;
1721       break;
1722     default:
1723       gdb_assert_not_reached ("unexpected section");
1724     }
1725
1726   dwarf2_read_section (objfile, info);
1727
1728   *sectp = info->asection;
1729   *bufp = info->buffer;
1730   *sizep = info->size;
1731 }
1732
1733 \f
1734 /* DWARF quick_symbols_functions support.  */
1735
1736 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1737    unique line tables, so we maintain a separate table of all .debug_line
1738    derived entries to support the sharing.
1739    All the quick functions need is the list of file names.  We discard the
1740    line_header when we're done and don't need to record it here.  */
1741 struct quick_file_names
1742 {
1743   /* The offset in .debug_line of the line table.  We hash on this.  */
1744   unsigned int offset;
1745
1746   /* The number of entries in file_names, real_names.  */
1747   unsigned int num_file_names;
1748
1749   /* The file names from the line table, after being run through
1750      file_full_name.  */
1751   const char **file_names;
1752
1753   /* The file names from the line table after being run through
1754      gdb_realpath.  These are computed lazily.  */
1755   const char **real_names;
1756 };
1757
1758 /* When using the index (and thus not using psymtabs), each CU has an
1759    object of this type.  This is used to hold information needed by
1760    the various "quick" methods.  */
1761 struct dwarf2_per_cu_quick_data
1762 {
1763   /* The file table.  This can be NULL if there was no file table
1764      or it's currently not read in.
1765      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1766   struct quick_file_names *file_names;
1767
1768   /* The corresponding symbol table.  This is NULL if symbols for this
1769      CU have not yet been read.  */
1770   struct symtab *symtab;
1771
1772   /* A temporary mark bit used when iterating over all CUs in
1773      expand_symtabs_matching.  */
1774   unsigned int mark : 1;
1775
1776   /* True if we've tried to read the file table and found there isn't one.
1777      There will be no point in trying to read it again next time.  */
1778   unsigned int no_file_data : 1;
1779 };
1780
1781 /* Hash function for a quick_file_names.  */
1782
1783 static hashval_t
1784 hash_file_name_entry (const void *e)
1785 {
1786   const struct quick_file_names *file_data = e;
1787
1788   return file_data->offset;
1789 }
1790
1791 /* Equality function for a quick_file_names.  */
1792
1793 static int
1794 eq_file_name_entry (const void *a, const void *b)
1795 {
1796   const struct quick_file_names *ea = a;
1797   const struct quick_file_names *eb = b;
1798
1799   return ea->offset == eb->offset;
1800 }
1801
1802 /* Delete function for a quick_file_names.  */
1803
1804 static void
1805 delete_file_name_entry (void *e)
1806 {
1807   struct quick_file_names *file_data = e;
1808   int i;
1809
1810   for (i = 0; i < file_data->num_file_names; ++i)
1811     {
1812       xfree ((void*) file_data->file_names[i]);
1813       if (file_data->real_names)
1814         xfree ((void*) file_data->real_names[i]);
1815     }
1816
1817   /* The space for the struct itself lives on objfile_obstack,
1818      so we don't free it here.  */
1819 }
1820
1821 /* Create a quick_file_names hash table.  */
1822
1823 static htab_t
1824 create_quick_file_names_table (unsigned int nr_initial_entries)
1825 {
1826   return htab_create_alloc (nr_initial_entries,
1827                             hash_file_name_entry, eq_file_name_entry,
1828                             delete_file_name_entry, xcalloc, xfree);
1829 }
1830
1831 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1832    have to be created afterwards.  You should call age_cached_comp_units after
1833    processing PER_CU->CU.  dw2_setup must have been already called.  */
1834
1835 static void
1836 load_cu (struct dwarf2_per_cu_data *per_cu)
1837 {
1838   if (per_cu->debug_types_section)
1839     read_signatured_type_at_offset (per_cu->objfile,
1840                                     per_cu->debug_types_section,
1841                                     per_cu->offset);
1842   else
1843     load_full_comp_unit (per_cu, per_cu->objfile);
1844
1845   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1846
1847   gdb_assert (per_cu->cu != NULL);
1848 }
1849
1850 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1851    this CU came.  */
1852
1853 static void
1854 dw2_do_instantiate_symtab (struct objfile *objfile,
1855                            struct dwarf2_per_cu_data *per_cu)
1856 {
1857   struct cleanup *back_to;
1858
1859   back_to = make_cleanup (dwarf2_release_queue, NULL);
1860
1861   queue_comp_unit (per_cu, objfile);
1862
1863   load_cu (per_cu);
1864
1865   process_queue (objfile);
1866
1867   /* Age the cache, releasing compilation units that have not
1868      been used recently.  */
1869   age_cached_comp_units ();
1870
1871   do_cleanups (back_to);
1872 }
1873
1874 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1875    the objfile from which this CU came.  Returns the resulting symbol
1876    table.  */
1877
1878 static struct symtab *
1879 dw2_instantiate_symtab (struct objfile *objfile,
1880                         struct dwarf2_per_cu_data *per_cu)
1881 {
1882   if (!per_cu->v.quick->symtab)
1883     {
1884       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1885       increment_reading_symtab ();
1886       dw2_do_instantiate_symtab (objfile, per_cu);
1887       do_cleanups (back_to);
1888     }
1889   return per_cu->v.quick->symtab;
1890 }
1891
1892 /* Return the CU given its index.  */
1893
1894 static struct dwarf2_per_cu_data *
1895 dw2_get_cu (int index)
1896 {
1897   if (index >= dwarf2_per_objfile->n_comp_units)
1898     {
1899       index -= dwarf2_per_objfile->n_comp_units;
1900       return dwarf2_per_objfile->type_comp_units[index];
1901     }
1902   return dwarf2_per_objfile->all_comp_units[index];
1903 }
1904
1905 /* A helper function that knows how to read a 64-bit value in a way
1906    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1907    otherwise.  */
1908
1909 static int
1910 extract_cu_value (const char *bytes, ULONGEST *result)
1911 {
1912   if (sizeof (ULONGEST) < 8)
1913     {
1914       int i;
1915
1916       /* Ignore the upper 4 bytes if they are all zero.  */
1917       for (i = 0; i < 4; ++i)
1918         if (bytes[i + 4] != 0)
1919           return 0;
1920
1921       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1922     }
1923   else
1924     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1925   return 1;
1926 }
1927
1928 /* Read the CU list from the mapped index, and use it to create all
1929    the CU objects for this objfile.  Return 0 if something went wrong,
1930    1 if everything went ok.  */
1931
1932 static int
1933 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1934                        offset_type cu_list_elements)
1935 {
1936   offset_type i;
1937
1938   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1939   dwarf2_per_objfile->all_comp_units
1940     = obstack_alloc (&objfile->objfile_obstack,
1941                      dwarf2_per_objfile->n_comp_units
1942                      * sizeof (struct dwarf2_per_cu_data *));
1943
1944   for (i = 0; i < cu_list_elements; i += 2)
1945     {
1946       struct dwarf2_per_cu_data *the_cu;
1947       ULONGEST offset, length;
1948
1949       if (!extract_cu_value (cu_list, &offset)
1950           || !extract_cu_value (cu_list + 8, &length))
1951         return 0;
1952       cu_list += 2 * 8;
1953
1954       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1955                                struct dwarf2_per_cu_data);
1956       the_cu->offset = offset;
1957       the_cu->length = length;
1958       the_cu->objfile = objfile;
1959       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1960                                         struct dwarf2_per_cu_quick_data);
1961       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1962     }
1963
1964   return 1;
1965 }
1966
1967 /* Create the signatured type hash table from the index.  */
1968
1969 static int
1970 create_signatured_type_table_from_index (struct objfile *objfile,
1971                                          struct dwarf2_section_info *section,
1972                                          const gdb_byte *bytes,
1973                                          offset_type elements)
1974 {
1975   offset_type i;
1976   htab_t sig_types_hash;
1977
1978   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1979   dwarf2_per_objfile->type_comp_units
1980     = obstack_alloc (&objfile->objfile_obstack,
1981                      dwarf2_per_objfile->n_type_comp_units
1982                      * sizeof (struct dwarf2_per_cu_data *));
1983
1984   sig_types_hash = allocate_signatured_type_table (objfile);
1985
1986   for (i = 0; i < elements; i += 3)
1987     {
1988       struct signatured_type *type_sig;
1989       ULONGEST offset, type_offset, signature;
1990       void **slot;
1991
1992       if (!extract_cu_value (bytes, &offset)
1993           || !extract_cu_value (bytes + 8, &type_offset))
1994         return 0;
1995       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1996       bytes += 3 * 8;
1997
1998       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1999                                  struct signatured_type);
2000       type_sig->signature = signature;
2001       type_sig->type_offset = type_offset;
2002       type_sig->per_cu.debug_types_section = section;
2003       type_sig->per_cu.offset = offset;
2004       type_sig->per_cu.objfile = objfile;
2005       type_sig->per_cu.v.quick
2006         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2007                           struct dwarf2_per_cu_quick_data);
2008
2009       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
2010       *slot = type_sig;
2011
2012       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2013     }
2014
2015   dwarf2_per_objfile->signatured_types = sig_types_hash;
2016
2017   return 1;
2018 }
2019
2020 /* Read the address map data from the mapped index, and use it to
2021    populate the objfile's psymtabs_addrmap.  */
2022
2023 static void
2024 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2025 {
2026   const gdb_byte *iter, *end;
2027   struct obstack temp_obstack;
2028   struct addrmap *mutable_map;
2029   struct cleanup *cleanup;
2030   CORE_ADDR baseaddr;
2031
2032   obstack_init (&temp_obstack);
2033   cleanup = make_cleanup_obstack_free (&temp_obstack);
2034   mutable_map = addrmap_create_mutable (&temp_obstack);
2035
2036   iter = index->address_table;
2037   end = iter + index->address_table_size;
2038
2039   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2040
2041   while (iter < end)
2042     {
2043       ULONGEST hi, lo, cu_index;
2044       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2045       iter += 8;
2046       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2047       iter += 8;
2048       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2049       iter += 4;
2050       
2051       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2052                          dw2_get_cu (cu_index));
2053     }
2054
2055   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2056                                                     &objfile->objfile_obstack);
2057   do_cleanups (cleanup);
2058 }
2059
2060 /* The hash function for strings in the mapped index.  This is the same as
2061    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2062    implementation.  This is necessary because the hash function is tied to the
2063    format of the mapped index file.  The hash values do not have to match with
2064    SYMBOL_HASH_NEXT.
2065    
2066    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2067
2068 static hashval_t
2069 mapped_index_string_hash (int index_version, const void *p)
2070 {
2071   const unsigned char *str = (const unsigned char *) p;
2072   hashval_t r = 0;
2073   unsigned char c;
2074
2075   while ((c = *str++) != 0)
2076     {
2077       if (index_version >= 5)
2078         c = tolower (c);
2079       r = r * 67 + c - 113;
2080     }
2081
2082   return r;
2083 }
2084
2085 /* Find a slot in the mapped index INDEX for the object named NAME.
2086    If NAME is found, set *VEC_OUT to point to the CU vector in the
2087    constant pool and return 1.  If NAME cannot be found, return 0.  */
2088
2089 static int
2090 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2091                           offset_type **vec_out)
2092 {
2093   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2094   offset_type hash;
2095   offset_type slot, step;
2096   int (*cmp) (const char *, const char *);
2097
2098   if (current_language->la_language == language_cplus
2099       || current_language->la_language == language_java
2100       || current_language->la_language == language_fortran)
2101     {
2102       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2103          not contain any.  */
2104       const char *paren = strchr (name, '(');
2105
2106       if (paren)
2107         {
2108           char *dup;
2109
2110           dup = xmalloc (paren - name + 1);
2111           memcpy (dup, name, paren - name);
2112           dup[paren - name] = 0;
2113
2114           make_cleanup (xfree, dup);
2115           name = dup;
2116         }
2117     }
2118
2119   /* Index version 4 did not support case insensitive searches.  But the
2120      indexes for case insensitive languages are built in lowercase, therefore
2121      simulate our NAME being searched is also lowercased.  */
2122   hash = mapped_index_string_hash ((index->version == 4
2123                                     && case_sensitivity == case_sensitive_off
2124                                     ? 5 : index->version),
2125                                    name);
2126
2127   slot = hash & (index->symbol_table_slots - 1);
2128   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2129   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2130
2131   for (;;)
2132     {
2133       /* Convert a slot number to an offset into the table.  */
2134       offset_type i = 2 * slot;
2135       const char *str;
2136       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2137         {
2138           do_cleanups (back_to);
2139           return 0;
2140         }
2141
2142       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2143       if (!cmp (name, str))
2144         {
2145           *vec_out = (offset_type *) (index->constant_pool
2146                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2147           do_cleanups (back_to);
2148           return 1;
2149         }
2150
2151       slot = (slot + step) & (index->symbol_table_slots - 1);
2152     }
2153 }
2154
2155 /* Read the index file.  If everything went ok, initialize the "quick"
2156    elements of all the CUs and return 1.  Otherwise, return 0.  */
2157
2158 static int
2159 dwarf2_read_index (struct objfile *objfile)
2160 {
2161   char *addr;
2162   struct mapped_index *map;
2163   offset_type *metadata;
2164   const gdb_byte *cu_list;
2165   const gdb_byte *types_list = NULL;
2166   offset_type version, cu_list_elements;
2167   offset_type types_list_elements = 0;
2168   int i;
2169
2170   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2171     return 0;
2172
2173   /* Older elfutils strip versions could keep the section in the main
2174      executable while splitting it for the separate debug info file.  */
2175   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2176        & SEC_HAS_CONTENTS) == 0)
2177     return 0;
2178
2179   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2180
2181   addr = dwarf2_per_objfile->gdb_index.buffer;
2182   /* Version check.  */
2183   version = MAYBE_SWAP (*(offset_type *) addr);
2184   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2185      causes the index to behave very poorly for certain requests.  Version 3
2186      contained incomplete addrmap.  So, it seems better to just ignore such
2187      indices.  Index version 4 uses a different hash function than index
2188      version 5 and later.  */
2189   if (version < 4)
2190     return 0;
2191   /* Indexes with higher version than the one supported by GDB may be no
2192      longer backward compatible.  */
2193   if (version > 5)
2194     return 0;
2195
2196   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2197   map->version = version;
2198   map->total_size = dwarf2_per_objfile->gdb_index.size;
2199
2200   metadata = (offset_type *) (addr + sizeof (offset_type));
2201
2202   i = 0;
2203   cu_list = addr + MAYBE_SWAP (metadata[i]);
2204   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2205                       / 8);
2206   ++i;
2207
2208   types_list = addr + MAYBE_SWAP (metadata[i]);
2209   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2210                           - MAYBE_SWAP (metadata[i]))
2211                          / 8);
2212   ++i;
2213
2214   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2215   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2216                              - MAYBE_SWAP (metadata[i]));
2217   ++i;
2218
2219   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2220   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2221                               - MAYBE_SWAP (metadata[i]))
2222                              / (2 * sizeof (offset_type)));
2223   ++i;
2224
2225   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2226
2227   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2228     return 0;
2229
2230   if (types_list_elements)
2231     {
2232       struct dwarf2_section_info *section;
2233
2234       /* We can only handle a single .debug_types when we have an
2235          index.  */
2236       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2237         return 0;
2238
2239       section = VEC_index (dwarf2_section_info_def,
2240                            dwarf2_per_objfile->types, 0);
2241
2242       if (!create_signatured_type_table_from_index (objfile, section,
2243                                                     types_list,
2244                                                     types_list_elements))
2245         return 0;
2246     }
2247
2248   create_addrmap_from_index (objfile, map);
2249
2250   dwarf2_per_objfile->index_table = map;
2251   dwarf2_per_objfile->using_index = 1;
2252   dwarf2_per_objfile->quick_file_names_table =
2253     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2254
2255   return 1;
2256 }
2257
2258 /* A helper for the "quick" functions which sets the global
2259    dwarf2_per_objfile according to OBJFILE.  */
2260
2261 static void
2262 dw2_setup (struct objfile *objfile)
2263 {
2264   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2265   gdb_assert (dwarf2_per_objfile);
2266 }
2267
2268 /* A helper for the "quick" functions which attempts to read the line
2269    table for THIS_CU.  */
2270
2271 static struct quick_file_names *
2272 dw2_get_file_names (struct objfile *objfile,
2273                     struct dwarf2_per_cu_data *this_cu)
2274 {
2275   bfd *abfd = objfile->obfd;
2276   struct line_header *lh;
2277   struct attribute *attr;
2278   struct cleanup *cleanups;
2279   struct die_info *comp_unit_die;
2280   struct dwarf2_section_info* sec;
2281   gdb_byte *info_ptr, *buffer;
2282   int has_children, i;
2283   struct dwarf2_cu cu;
2284   unsigned int bytes_read, buffer_size;
2285   struct die_reader_specs reader_specs;
2286   char *name, *comp_dir;
2287   void **slot;
2288   struct quick_file_names *qfn;
2289   unsigned int line_offset;
2290
2291   if (this_cu->v.quick->file_names != NULL)
2292     return this_cu->v.quick->file_names;
2293   /* If we know there is no line data, no point in looking again.  */
2294   if (this_cu->v.quick->no_file_data)
2295     return NULL;
2296
2297   init_one_comp_unit (&cu, objfile);
2298   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2299
2300   if (this_cu->debug_types_section)
2301     sec = this_cu->debug_types_section;
2302   else
2303     sec = &dwarf2_per_objfile->info;
2304   dwarf2_read_section (objfile, sec);
2305   buffer_size = sec->size;
2306   buffer = sec->buffer;
2307   info_ptr = buffer + this_cu->offset;
2308
2309   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2310                                           buffer, buffer_size,
2311                                           abfd,
2312                                           this_cu->debug_types_section != NULL);
2313
2314   /* Skip dummy compilation units.  */
2315   if (info_ptr >= buffer + buffer_size
2316       || peek_abbrev_code (abfd, info_ptr) == 0)
2317     {
2318       do_cleanups (cleanups);
2319       return NULL;
2320     }
2321
2322   this_cu->cu = &cu;
2323   cu.per_cu = this_cu;
2324
2325   dwarf2_read_abbrevs (abfd, &cu);
2326   make_cleanup (dwarf2_free_abbrev_table, &cu);
2327
2328   init_cu_die_reader (&reader_specs, &cu);
2329   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2330                  &has_children);
2331
2332   lh = NULL;
2333   slot = NULL;
2334   line_offset = 0;
2335   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2336   if (attr)
2337     {
2338       struct quick_file_names find_entry;
2339
2340       line_offset = DW_UNSND (attr);
2341
2342       /* We may have already read in this line header (TU line header sharing).
2343          If we have we're done.  */
2344       find_entry.offset = line_offset;
2345       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2346                              &find_entry, INSERT);
2347       if (*slot != NULL)
2348         {
2349           do_cleanups (cleanups);
2350           this_cu->v.quick->file_names = *slot;
2351           return *slot;
2352         }
2353
2354       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2355     }
2356   if (lh == NULL)
2357     {
2358       do_cleanups (cleanups);
2359       this_cu->v.quick->no_file_data = 1;
2360       return NULL;
2361     }
2362
2363   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2364   qfn->offset = line_offset;
2365   gdb_assert (slot != NULL);
2366   *slot = qfn;
2367
2368   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2369
2370   qfn->num_file_names = lh->num_file_names;
2371   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2372                                    lh->num_file_names * sizeof (char *));
2373   for (i = 0; i < lh->num_file_names; ++i)
2374     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2375   qfn->real_names = NULL;
2376
2377   free_line_header (lh);
2378   do_cleanups (cleanups);
2379
2380   this_cu->v.quick->file_names = qfn;
2381   return qfn;
2382 }
2383
2384 /* A helper for the "quick" functions which computes and caches the
2385    real path for a given file name from the line table.  */
2386
2387 static const char *
2388 dw2_get_real_path (struct objfile *objfile,
2389                    struct quick_file_names *qfn, int index)
2390 {
2391   if (qfn->real_names == NULL)
2392     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2393                                       qfn->num_file_names, sizeof (char *));
2394
2395   if (qfn->real_names[index] == NULL)
2396     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2397
2398   return qfn->real_names[index];
2399 }
2400
2401 static struct symtab *
2402 dw2_find_last_source_symtab (struct objfile *objfile)
2403 {
2404   int index;
2405
2406   dw2_setup (objfile);
2407   index = dwarf2_per_objfile->n_comp_units - 1;
2408   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2409 }
2410
2411 /* Traversal function for dw2_forget_cached_source_info.  */
2412
2413 static int
2414 dw2_free_cached_file_names (void **slot, void *info)
2415 {
2416   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2417
2418   if (file_data->real_names)
2419     {
2420       int i;
2421
2422       for (i = 0; i < file_data->num_file_names; ++i)
2423         {
2424           xfree ((void*) file_data->real_names[i]);
2425           file_data->real_names[i] = NULL;
2426         }
2427     }
2428
2429   return 1;
2430 }
2431
2432 static void
2433 dw2_forget_cached_source_info (struct objfile *objfile)
2434 {
2435   dw2_setup (objfile);
2436
2437   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2438                           dw2_free_cached_file_names, NULL);
2439 }
2440
2441 /* Helper function for dw2_map_symtabs_matching_filename that expands
2442    the symtabs and calls the iterator.  */
2443
2444 static int
2445 dw2_map_expand_apply (struct objfile *objfile,
2446                       struct dwarf2_per_cu_data *per_cu,
2447                       const char *name,
2448                       const char *full_path, const char *real_path,
2449                       int (*callback) (struct symtab *, void *),
2450                       void *data)
2451 {
2452   struct symtab *last_made = objfile->symtabs;
2453
2454   /* Don't visit already-expanded CUs.  */
2455   if (per_cu->v.quick->symtab)
2456     return 0;
2457
2458   /* This may expand more than one symtab, and we want to iterate over
2459      all of them.  */
2460   dw2_instantiate_symtab (objfile, per_cu);
2461
2462   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2463                                     objfile->symtabs, last_made);
2464 }
2465
2466 /* Implementation of the map_symtabs_matching_filename method.  */
2467
2468 static int
2469 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2470                                    const char *full_path, const char *real_path,
2471                                    int (*callback) (struct symtab *, void *),
2472                                    void *data)
2473 {
2474   int i;
2475   const char *name_basename = lbasename (name);
2476   int check_basename = name_basename == name;
2477   struct dwarf2_per_cu_data *base_cu = NULL;
2478
2479   dw2_setup (objfile);
2480
2481   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2482                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2483     {
2484       int j;
2485       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2486       struct quick_file_names *file_data;
2487
2488       /* We only need to look at symtabs not already expanded.  */
2489       if (per_cu->v.quick->symtab)
2490         continue;
2491
2492       file_data = dw2_get_file_names (objfile, per_cu);
2493       if (file_data == NULL)
2494         continue;
2495
2496       for (j = 0; j < file_data->num_file_names; ++j)
2497         {
2498           const char *this_name = file_data->file_names[j];
2499
2500           if (FILENAME_CMP (name, this_name) == 0)
2501             {
2502               if (dw2_map_expand_apply (objfile, per_cu,
2503                                         name, full_path, real_path,
2504                                         callback, data))
2505                 return 1;
2506             }
2507
2508           if (check_basename && ! base_cu
2509               && FILENAME_CMP (lbasename (this_name), name) == 0)
2510             base_cu = per_cu;
2511
2512           /* Before we invoke realpath, which can get expensive when many
2513              files are involved, do a quick comparison of the basenames.  */
2514           if (! basenames_may_differ
2515               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2516             continue;
2517
2518           if (full_path != NULL)
2519             {
2520               const char *this_real_name = dw2_get_real_path (objfile,
2521                                                               file_data, j);
2522
2523               if (this_real_name != NULL
2524                   && FILENAME_CMP (full_path, this_real_name) == 0)
2525                 {
2526                   if (dw2_map_expand_apply (objfile, per_cu,
2527                                             name, full_path, real_path,
2528                                             callback, data))
2529                     return 1;
2530                 }
2531             }
2532
2533           if (real_path != NULL)
2534             {
2535               const char *this_real_name = dw2_get_real_path (objfile,
2536                                                               file_data, j);
2537
2538               if (this_real_name != NULL
2539                   && FILENAME_CMP (real_path, this_real_name) == 0)
2540                 {
2541                   if (dw2_map_expand_apply (objfile, per_cu,
2542                                             name, full_path, real_path,
2543                                             callback, data))
2544                     return 1;
2545                 }
2546             }
2547         }
2548     }
2549
2550   if (base_cu)
2551     {
2552       if (dw2_map_expand_apply (objfile, base_cu,
2553                                 name, full_path, real_path,
2554                                 callback, data))
2555         return 1;
2556     }
2557
2558   return 0;
2559 }
2560
2561 static struct symtab *
2562 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2563                    const char *name, domain_enum domain)
2564 {
2565   /* We do all the work in the pre_expand_symtabs_matching hook
2566      instead.  */
2567   return NULL;
2568 }
2569
2570 /* A helper function that expands all symtabs that hold an object
2571    named NAME.  */
2572
2573 static void
2574 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2575 {
2576   dw2_setup (objfile);
2577
2578   /* index_table is NULL if OBJF_READNOW.  */
2579   if (dwarf2_per_objfile->index_table)
2580     {
2581       offset_type *vec;
2582
2583       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2584                                     name, &vec))
2585         {
2586           offset_type i, len = MAYBE_SWAP (*vec);
2587           for (i = 0; i < len; ++i)
2588             {
2589               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2590               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2591
2592               dw2_instantiate_symtab (objfile, per_cu);
2593             }
2594         }
2595     }
2596 }
2597
2598 static void
2599 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2600                                  enum block_enum block_kind, const char *name,
2601                                  domain_enum domain)
2602 {
2603   dw2_do_expand_symtabs_matching (objfile, name);
2604 }
2605
2606 static void
2607 dw2_print_stats (struct objfile *objfile)
2608 {
2609   int i, count;
2610
2611   dw2_setup (objfile);
2612   count = 0;
2613   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2614                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2615     {
2616       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2617
2618       if (!per_cu->v.quick->symtab)
2619         ++count;
2620     }
2621   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2622 }
2623
2624 static void
2625 dw2_dump (struct objfile *objfile)
2626 {
2627   /* Nothing worth printing.  */
2628 }
2629
2630 static void
2631 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2632               struct section_offsets *delta)
2633 {
2634   /* There's nothing to relocate here.  */
2635 }
2636
2637 static void
2638 dw2_expand_symtabs_for_function (struct objfile *objfile,
2639                                  const char *func_name)
2640 {
2641   dw2_do_expand_symtabs_matching (objfile, func_name);
2642 }
2643
2644 static void
2645 dw2_expand_all_symtabs (struct objfile *objfile)
2646 {
2647   int i;
2648
2649   dw2_setup (objfile);
2650
2651   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2652                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2653     {
2654       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2655
2656       dw2_instantiate_symtab (objfile, per_cu);
2657     }
2658 }
2659
2660 static void
2661 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2662                                   const char *filename)
2663 {
2664   int i;
2665
2666   dw2_setup (objfile);
2667
2668   /* We don't need to consider type units here.
2669      This is only called for examining code, e.g. expand_line_sal.
2670      There can be an order of magnitude (or more) more type units
2671      than comp units, and we avoid them if we can.  */
2672
2673   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2674     {
2675       int j;
2676       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2677       struct quick_file_names *file_data;
2678
2679       /* We only need to look at symtabs not already expanded.  */
2680       if (per_cu->v.quick->symtab)
2681         continue;
2682
2683       file_data = dw2_get_file_names (objfile, per_cu);
2684       if (file_data == NULL)
2685         continue;
2686
2687       for (j = 0; j < file_data->num_file_names; ++j)
2688         {
2689           const char *this_name = file_data->file_names[j];
2690           if (FILENAME_CMP (this_name, filename) == 0)
2691             {
2692               dw2_instantiate_symtab (objfile, per_cu);
2693               break;
2694             }
2695         }
2696     }
2697 }
2698
2699 static const char *
2700 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2701 {
2702   struct dwarf2_per_cu_data *per_cu;
2703   offset_type *vec;
2704   struct quick_file_names *file_data;
2705
2706   dw2_setup (objfile);
2707
2708   /* index_table is NULL if OBJF_READNOW.  */
2709   if (!dwarf2_per_objfile->index_table)
2710     {
2711       struct symtab *s;
2712
2713       ALL_OBJFILE_SYMTABS (objfile, s)
2714         if (s->primary)
2715           {
2716             struct blockvector *bv = BLOCKVECTOR (s);
2717             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2718             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2719
2720             if (sym)
2721               return sym->symtab->filename;
2722           }
2723       return NULL;
2724     }
2725
2726   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2727                                  name, &vec))
2728     return NULL;
2729
2730   /* Note that this just looks at the very first one named NAME -- but
2731      actually we are looking for a function.  find_main_filename
2732      should be rewritten so that it doesn't require a custom hook.  It
2733      could just use the ordinary symbol tables.  */
2734   /* vec[0] is the length, which must always be >0.  */
2735   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2736
2737   file_data = dw2_get_file_names (objfile, per_cu);
2738   if (file_data == NULL)
2739     return NULL;
2740
2741   return file_data->file_names[file_data->num_file_names - 1];
2742 }
2743
2744 static void
2745 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2746                           struct objfile *objfile, int global,
2747                           int (*callback) (struct block *,
2748                                            struct symbol *, void *),
2749                           void *data, symbol_compare_ftype *match,
2750                           symbol_compare_ftype *ordered_compare)
2751 {
2752   /* Currently unimplemented; used for Ada.  The function can be called if the
2753      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2754      does not look for non-Ada symbols this function should just return.  */
2755 }
2756
2757 static void
2758 dw2_expand_symtabs_matching
2759   (struct objfile *objfile,
2760    int (*file_matcher) (const char *, void *),
2761    int (*name_matcher) (const struct language_defn *, const char *, void *),
2762    enum search_domain kind,
2763    void *data)
2764 {
2765   int i;
2766   offset_type iter;
2767   struct mapped_index *index;
2768
2769   dw2_setup (objfile);
2770
2771   /* index_table is NULL if OBJF_READNOW.  */
2772   if (!dwarf2_per_objfile->index_table)
2773     return;
2774   index = dwarf2_per_objfile->index_table;
2775
2776   if (file_matcher != NULL)
2777     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2778                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2779       {
2780         int j;
2781         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2782         struct quick_file_names *file_data;
2783
2784         per_cu->v.quick->mark = 0;
2785
2786         /* We only need to look at symtabs not already expanded.  */
2787         if (per_cu->v.quick->symtab)
2788           continue;
2789
2790         file_data = dw2_get_file_names (objfile, per_cu);
2791         if (file_data == NULL)
2792           continue;
2793
2794         for (j = 0; j < file_data->num_file_names; ++j)
2795           {
2796             if (file_matcher (file_data->file_names[j], data))
2797               {
2798                 per_cu->v.quick->mark = 1;
2799                 break;
2800               }
2801           }
2802       }
2803
2804   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2805     {
2806       offset_type idx = 2 * iter;
2807       const char *name;
2808       offset_type *vec, vec_len, vec_idx;
2809
2810       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2811         continue;
2812
2813       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2814
2815       if (! (*name_matcher) (current_language, name, data))
2816         continue;
2817
2818       /* The name was matched, now expand corresponding CUs that were
2819          marked.  */
2820       vec = (offset_type *) (index->constant_pool
2821                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2822       vec_len = MAYBE_SWAP (vec[0]);
2823       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2824         {
2825           struct dwarf2_per_cu_data *per_cu;
2826
2827           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2828           if (file_matcher == NULL || per_cu->v.quick->mark)
2829             dw2_instantiate_symtab (objfile, per_cu);
2830         }
2831     }
2832 }
2833
2834 static struct symtab *
2835 dw2_find_pc_sect_symtab (struct objfile *objfile,
2836                          struct minimal_symbol *msymbol,
2837                          CORE_ADDR pc,
2838                          struct obj_section *section,
2839                          int warn_if_readin)
2840 {
2841   struct dwarf2_per_cu_data *data;
2842
2843   dw2_setup (objfile);
2844
2845   if (!objfile->psymtabs_addrmap)
2846     return NULL;
2847
2848   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2849   if (!data)
2850     return NULL;
2851
2852   if (warn_if_readin && data->v.quick->symtab)
2853     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2854              paddress (get_objfile_arch (objfile), pc));
2855
2856   return dw2_instantiate_symtab (objfile, data);
2857 }
2858
2859 static void
2860 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2861                           void *data, int need_fullname)
2862 {
2863   int i;
2864
2865   dw2_setup (objfile);
2866
2867   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2868                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2869     {
2870       int j;
2871       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2872       struct quick_file_names *file_data;
2873
2874       /* We only need to look at symtabs not already expanded.  */
2875       if (per_cu->v.quick->symtab)
2876         continue;
2877
2878       file_data = dw2_get_file_names (objfile, per_cu);
2879       if (file_data == NULL)
2880         continue;
2881
2882       for (j = 0; j < file_data->num_file_names; ++j)
2883         {
2884           const char *this_real_name;
2885
2886           if (need_fullname)
2887             this_real_name = dw2_get_real_path (objfile, file_data, j);
2888           else
2889             this_real_name = NULL;
2890           (*fun) (file_data->file_names[j], this_real_name, data);
2891         }
2892     }
2893 }
2894
2895 static int
2896 dw2_has_symbols (struct objfile *objfile)
2897 {
2898   return 1;
2899 }
2900
2901 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2902 {
2903   dw2_has_symbols,
2904   dw2_find_last_source_symtab,
2905   dw2_forget_cached_source_info,
2906   dw2_map_symtabs_matching_filename,
2907   dw2_lookup_symbol,
2908   dw2_pre_expand_symtabs_matching,
2909   dw2_print_stats,
2910   dw2_dump,
2911   dw2_relocate,
2912   dw2_expand_symtabs_for_function,
2913   dw2_expand_all_symtabs,
2914   dw2_expand_symtabs_with_filename,
2915   dw2_find_symbol_file,
2916   dw2_map_matching_symbols,
2917   dw2_expand_symtabs_matching,
2918   dw2_find_pc_sect_symtab,
2919   dw2_map_symbol_filenames
2920 };
2921
2922 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2923    file will use psymtabs, or 1 if using the GNU index.  */
2924
2925 int
2926 dwarf2_initialize_objfile (struct objfile *objfile)
2927 {
2928   /* If we're about to read full symbols, don't bother with the
2929      indices.  In this case we also don't care if some other debug
2930      format is making psymtabs, because they are all about to be
2931      expanded anyway.  */
2932   if ((objfile->flags & OBJF_READNOW))
2933     {
2934       int i;
2935
2936       dwarf2_per_objfile->using_index = 1;
2937       create_all_comp_units (objfile);
2938       create_debug_types_hash_table (objfile);
2939       dwarf2_per_objfile->quick_file_names_table =
2940         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2941
2942       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2943                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2944         {
2945           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2946
2947           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2948                                             struct dwarf2_per_cu_quick_data);
2949         }
2950
2951       /* Return 1 so that gdb sees the "quick" functions.  However,
2952          these functions will be no-ops because we will have expanded
2953          all symtabs.  */
2954       return 1;
2955     }
2956
2957   if (dwarf2_read_index (objfile))
2958     return 1;
2959
2960   return 0;
2961 }
2962
2963 \f
2964
2965 /* Build a partial symbol table.  */
2966
2967 void
2968 dwarf2_build_psymtabs (struct objfile *objfile)
2969 {
2970   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2971     {
2972       init_psymbol_list (objfile, 1024);
2973     }
2974
2975   dwarf2_build_psymtabs_hard (objfile);
2976 }
2977
2978 /* Return TRUE if OFFSET is within CU_HEADER.  */
2979
2980 static inline int
2981 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2982 {
2983   unsigned int bottom = cu_header->offset;
2984   unsigned int top = (cu_header->offset
2985                       + cu_header->length
2986                       + cu_header->initial_length_size);
2987
2988   return (offset >= bottom && offset < top);
2989 }
2990
2991 /* Read in the comp unit header information from the debug_info at info_ptr.
2992    NOTE: This leaves members offset, first_die_offset to be filled in
2993    by the caller.  */
2994
2995 static gdb_byte *
2996 read_comp_unit_head (struct comp_unit_head *cu_header,
2997                      gdb_byte *info_ptr, bfd *abfd)
2998 {
2999   int signed_addr;
3000   unsigned int bytes_read;
3001
3002   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3003   cu_header->initial_length_size = bytes_read;
3004   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3005   info_ptr += bytes_read;
3006   cu_header->version = read_2_bytes (abfd, info_ptr);
3007   info_ptr += 2;
3008   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
3009                                           &bytes_read);
3010   info_ptr += bytes_read;
3011   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3012   info_ptr += 1;
3013   signed_addr = bfd_get_sign_extend_vma (abfd);
3014   if (signed_addr < 0)
3015     internal_error (__FILE__, __LINE__,
3016                     _("read_comp_unit_head: dwarf from non elf file"));
3017   cu_header->signed_addr_p = signed_addr;
3018
3019   return info_ptr;
3020 }
3021
3022 /* Read in a CU header and perform some basic error checking.  */
3023
3024 static gdb_byte *
3025 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
3026                              gdb_byte *buffer, unsigned int buffer_size,
3027                              bfd *abfd, int is_debug_types_section)
3028 {
3029   gdb_byte *beg_of_comp_unit = info_ptr;
3030
3031   header->offset = beg_of_comp_unit - buffer;
3032
3033   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3034
3035   /* If we're reading a type unit, skip over the signature and
3036      type_offset fields.  */
3037   if (is_debug_types_section)
3038     info_ptr += 8 /*signature*/ + header->offset_size;
3039
3040   header->first_die_offset = info_ptr - beg_of_comp_unit;
3041
3042   if (header->version != 2 && header->version != 3 && header->version != 4)
3043     error (_("Dwarf Error: wrong version in compilation unit header "
3044            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3045            bfd_get_filename (abfd));
3046
3047   if (header->abbrev_offset
3048       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3049                               &dwarf2_per_objfile->abbrev))
3050     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3051            "(offset 0x%lx + 6) [in module %s]"),
3052            (long) header->abbrev_offset,
3053            (long) (beg_of_comp_unit - buffer),
3054            bfd_get_filename (abfd));
3055
3056   if (beg_of_comp_unit + header->length + header->initial_length_size
3057       > buffer + buffer_size)
3058     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3059            "(offset 0x%lx + 0) [in module %s]"),
3060            (long) header->length,
3061            (long) (beg_of_comp_unit - buffer),
3062            bfd_get_filename (abfd));
3063
3064   return info_ptr;
3065 }
3066
3067 /* Read in the types comp unit header information from .debug_types entry at
3068    types_ptr.  The result is a pointer to one past the end of the header.  */
3069
3070 static gdb_byte *
3071 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3072                           struct dwarf2_section_info *section,
3073                           ULONGEST *signature,
3074                           gdb_byte *types_ptr, bfd *abfd)
3075 {
3076   gdb_byte *initial_types_ptr = types_ptr;
3077
3078   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3079   cu_header->offset = types_ptr - section->buffer;
3080
3081   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3082
3083   *signature = read_8_bytes (abfd, types_ptr);
3084   types_ptr += 8;
3085   types_ptr += cu_header->offset_size;
3086   cu_header->first_die_offset = types_ptr - initial_types_ptr;
3087
3088   return types_ptr;
3089 }
3090
3091 /* Allocate a new partial symtab for file named NAME and mark this new
3092    partial symtab as being an include of PST.  */
3093
3094 static void
3095 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3096                                struct objfile *objfile)
3097 {
3098   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3099
3100   subpst->section_offsets = pst->section_offsets;
3101   subpst->textlow = 0;
3102   subpst->texthigh = 0;
3103
3104   subpst->dependencies = (struct partial_symtab **)
3105     obstack_alloc (&objfile->objfile_obstack,
3106                    sizeof (struct partial_symtab *));
3107   subpst->dependencies[0] = pst;
3108   subpst->number_of_dependencies = 1;
3109
3110   subpst->globals_offset = 0;
3111   subpst->n_global_syms = 0;
3112   subpst->statics_offset = 0;
3113   subpst->n_static_syms = 0;
3114   subpst->symtab = NULL;
3115   subpst->read_symtab = pst->read_symtab;
3116   subpst->readin = 0;
3117
3118   /* No private part is necessary for include psymtabs.  This property
3119      can be used to differentiate between such include psymtabs and
3120      the regular ones.  */
3121   subpst->read_symtab_private = NULL;
3122 }
3123
3124 /* Read the Line Number Program data and extract the list of files
3125    included by the source file represented by PST.  Build an include
3126    partial symtab for each of these included files.  */
3127
3128 static void
3129 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3130                                struct die_info *die,
3131                                struct partial_symtab *pst)
3132 {
3133   struct objfile *objfile = cu->objfile;
3134   bfd *abfd = objfile->obfd;
3135   struct line_header *lh = NULL;
3136   struct attribute *attr;
3137
3138   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3139   if (attr)
3140     {
3141       unsigned int line_offset = DW_UNSND (attr);
3142
3143       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3144     }
3145   if (lh == NULL)
3146     return;  /* No linetable, so no includes.  */
3147
3148   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3149   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3150
3151   free_line_header (lh);
3152 }
3153
3154 static hashval_t
3155 hash_type_signature (const void *item)
3156 {
3157   const struct signatured_type *type_sig = item;
3158
3159   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3160   return type_sig->signature;
3161 }
3162
3163 static int
3164 eq_type_signature (const void *item_lhs, const void *item_rhs)
3165 {
3166   const struct signatured_type *lhs = item_lhs;
3167   const struct signatured_type *rhs = item_rhs;
3168
3169   return lhs->signature == rhs->signature;
3170 }
3171
3172 /* Allocate a hash table for signatured types.  */
3173
3174 static htab_t
3175 allocate_signatured_type_table (struct objfile *objfile)
3176 {
3177   return htab_create_alloc_ex (41,
3178                                hash_type_signature,
3179                                eq_type_signature,
3180                                NULL,
3181                                &objfile->objfile_obstack,
3182                                hashtab_obstack_allocate,
3183                                dummy_obstack_deallocate);
3184 }
3185
3186 /* A helper function to add a signatured type CU to a list.  */
3187
3188 static int
3189 add_signatured_type_cu_to_list (void **slot, void *datum)
3190 {
3191   struct signatured_type *sigt = *slot;
3192   struct dwarf2_per_cu_data ***datap = datum;
3193
3194   **datap = &sigt->per_cu;
3195   ++*datap;
3196
3197   return 1;
3198 }
3199
3200 /* Create the hash table of all entries in the .debug_types section.
3201    The result is zero if there is an error (e.g. missing .debug_types section),
3202    otherwise non-zero.  */
3203
3204 static int
3205 create_debug_types_hash_table (struct objfile *objfile)
3206 {
3207   htab_t types_htab = NULL;
3208   struct dwarf2_per_cu_data **iter;
3209   int ix;
3210   struct dwarf2_section_info *section;
3211
3212   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3213     {
3214       dwarf2_per_objfile->signatured_types = NULL;
3215       return 0;
3216     }
3217
3218   for (ix = 0;
3219        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3220                     ix, section);
3221        ++ix)
3222     {
3223       gdb_byte *info_ptr, *end_ptr;
3224
3225       dwarf2_read_section (objfile, section);
3226       info_ptr = section->buffer;
3227
3228       if (info_ptr == NULL)
3229         continue;
3230
3231       if (types_htab == NULL)
3232         types_htab = allocate_signatured_type_table (objfile);
3233
3234       if (dwarf2_die_debug)
3235         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3236
3237       end_ptr = info_ptr + section->size;
3238       while (info_ptr < end_ptr)
3239         {
3240           unsigned int offset;
3241           unsigned int offset_size;
3242           unsigned int type_offset;
3243           unsigned int length, initial_length_size;
3244           unsigned short version;
3245           ULONGEST signature;
3246           struct signatured_type *type_sig;
3247           void **slot;
3248           gdb_byte *ptr = info_ptr;
3249
3250           offset = ptr - section->buffer;
3251
3252           /* We need to read the type's signature in order to build the hash
3253              table, but we don't need to read anything else just yet.  */
3254
3255           /* Sanity check to ensure entire cu is present.  */
3256           length = read_initial_length (objfile->obfd, ptr,
3257                                         &initial_length_size);
3258           if (ptr + length + initial_length_size > end_ptr)
3259             {
3260               complaint (&symfile_complaints,
3261                          _("debug type entry runs off end "
3262                            "of `.debug_types' section, ignored"));
3263               break;
3264             }
3265
3266           offset_size = initial_length_size == 4 ? 4 : 8;
3267           ptr += initial_length_size;
3268           version = bfd_get_16 (objfile->obfd, ptr);
3269           ptr += 2;
3270           ptr += offset_size; /* abbrev offset */
3271           ptr += 1; /* address size */
3272           signature = bfd_get_64 (objfile->obfd, ptr);
3273           ptr += 8;
3274           type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3275           ptr += offset_size;
3276
3277           /* Skip dummy type units.  */
3278           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3279             {
3280               info_ptr = info_ptr + initial_length_size + length;
3281               continue;
3282             }
3283
3284           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3285           memset (type_sig, 0, sizeof (*type_sig));
3286           type_sig->signature = signature;
3287           type_sig->type_offset = type_offset;
3288           type_sig->per_cu.objfile = objfile;
3289           type_sig->per_cu.debug_types_section = section;
3290           type_sig->per_cu.offset = offset;
3291
3292           slot = htab_find_slot (types_htab, type_sig, INSERT);
3293           gdb_assert (slot != NULL);
3294           if (*slot != NULL)
3295             {
3296               const struct signatured_type *dup_sig = *slot;
3297
3298               complaint (&symfile_complaints,
3299                          _("debug type entry at offset 0x%x is duplicate to the "
3300                            "entry at offset 0x%x, signature 0x%s"),
3301                          offset, dup_sig->per_cu.offset,
3302                          phex (signature, sizeof (signature)));
3303               gdb_assert (signature == dup_sig->signature);
3304             }
3305           *slot = type_sig;
3306
3307           if (dwarf2_die_debug)
3308             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3309                                 offset, phex (signature, sizeof (signature)));
3310
3311           info_ptr = info_ptr + initial_length_size + length;
3312         }
3313     }
3314
3315   dwarf2_per_objfile->signatured_types = types_htab;
3316
3317   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3318   dwarf2_per_objfile->type_comp_units
3319     = obstack_alloc (&objfile->objfile_obstack,
3320                      dwarf2_per_objfile->n_type_comp_units
3321                      * sizeof (struct dwarf2_per_cu_data *));
3322   iter = &dwarf2_per_objfile->type_comp_units[0];
3323   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3324   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3325               == dwarf2_per_objfile->n_type_comp_units);
3326
3327   return 1;
3328 }
3329
3330 /* Lookup a signature based type.
3331    Returns NULL if SIG is not present in the table.  */
3332
3333 static struct signatured_type *
3334 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3335 {
3336   struct signatured_type find_entry, *entry;
3337
3338   if (dwarf2_per_objfile->signatured_types == NULL)
3339     {
3340       complaint (&symfile_complaints,
3341                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3342       return 0;
3343     }
3344
3345   find_entry.signature = sig;
3346   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3347   return entry;
3348 }
3349
3350 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3351
3352 static void
3353 init_cu_die_reader (struct die_reader_specs *reader,
3354                     struct dwarf2_cu *cu)
3355 {
3356   reader->abfd = cu->objfile->obfd;
3357   reader->cu = cu;
3358   if (cu->per_cu->debug_types_section)
3359     {
3360       gdb_assert (cu->per_cu->debug_types_section->readin);
3361       reader->buffer = cu->per_cu->debug_types_section->buffer;
3362     }
3363   else
3364     {
3365       gdb_assert (dwarf2_per_objfile->info.readin);
3366       reader->buffer = dwarf2_per_objfile->info.buffer;
3367     }
3368 }
3369
3370 /* Find the base address of the compilation unit for range lists and
3371    location lists.  It will normally be specified by DW_AT_low_pc.
3372    In DWARF-3 draft 4, the base address could be overridden by
3373    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3374    compilation units with discontinuous ranges.  */
3375
3376 static void
3377 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3378 {
3379   struct attribute *attr;
3380
3381   cu->base_known = 0;
3382   cu->base_address = 0;
3383
3384   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3385   if (attr)
3386     {
3387       cu->base_address = DW_ADDR (attr);
3388       cu->base_known = 1;
3389     }
3390   else
3391     {
3392       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3393       if (attr)
3394         {
3395           cu->base_address = DW_ADDR (attr);
3396           cu->base_known = 1;
3397         }
3398     }
3399 }
3400
3401 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3402    to combine the common parts.
3403    Process a compilation unit for a psymtab.
3404    BUFFER is a pointer to the beginning of the dwarf section buffer,
3405    either .debug_info or debug_types.
3406    INFO_PTR is a pointer to the start of the CU.
3407    Returns a pointer to the next CU.  */
3408
3409 static gdb_byte *
3410 process_psymtab_comp_unit (struct objfile *objfile,
3411                            struct dwarf2_per_cu_data *this_cu,
3412                            gdb_byte *buffer, gdb_byte *info_ptr,
3413                            unsigned int buffer_size)
3414 {
3415   bfd *abfd = objfile->obfd;
3416   gdb_byte *beg_of_comp_unit = info_ptr;
3417   struct die_info *comp_unit_die;
3418   struct partial_symtab *pst;
3419   CORE_ADDR baseaddr;
3420   struct cleanup *back_to_inner;
3421   struct dwarf2_cu cu;
3422   int has_children, has_pc_info;
3423   struct attribute *attr;
3424   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3425   struct die_reader_specs reader_specs;
3426   const char *filename;
3427
3428   init_one_comp_unit (&cu, objfile);
3429   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3430
3431   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3432                                           buffer, buffer_size,
3433                                           abfd,
3434                                           this_cu->debug_types_section != NULL);
3435
3436   /* Skip dummy compilation units.  */
3437   if (info_ptr >= buffer + buffer_size
3438       || peek_abbrev_code (abfd, info_ptr) == 0)
3439     {
3440       info_ptr = (beg_of_comp_unit + cu.header.length
3441                   + cu.header.initial_length_size);
3442       do_cleanups (back_to_inner);
3443       return info_ptr;
3444     }
3445
3446   cu.list_in_scope = &file_symbols;
3447
3448   /* If this compilation unit was already read in, free the
3449      cached copy in order to read it in again.  This is
3450      necessary because we skipped some symbols when we first
3451      read in the compilation unit (see load_partial_dies).
3452      This problem could be avoided, but the benefit is
3453      unclear.  */
3454   if (this_cu->cu != NULL)
3455     free_one_cached_comp_unit (this_cu->cu);
3456
3457   /* Note that this is a pointer to our stack frame, being
3458      added to a global data structure.  It will be cleaned up
3459      in free_stack_comp_unit when we finish with this
3460      compilation unit.  */
3461   this_cu->cu = &cu;
3462   cu.per_cu = this_cu;
3463
3464   /* Read the abbrevs for this compilation unit into a table.  */
3465   dwarf2_read_abbrevs (abfd, &cu);
3466   make_cleanup (dwarf2_free_abbrev_table, &cu);
3467
3468   /* Read the compilation unit die.  */
3469   init_cu_die_reader (&reader_specs, &cu);
3470   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3471                             &has_children);
3472
3473   if (this_cu->debug_types_section)
3474     {
3475       /* LENGTH has not been set yet for type units.  */
3476       gdb_assert (this_cu->offset == cu.header.offset);
3477       this_cu->length = cu.header.length + cu.header.initial_length_size;
3478     }
3479   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3480     {
3481       info_ptr = (beg_of_comp_unit + cu.header.length
3482                   + cu.header.initial_length_size);
3483       do_cleanups (back_to_inner);
3484       return info_ptr;
3485     }
3486
3487   prepare_one_comp_unit (&cu, comp_unit_die);
3488
3489   /* Allocate a new partial symbol table structure.  */
3490   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3491   if (attr == NULL || !DW_STRING (attr))
3492     filename = "";
3493   else
3494     filename = DW_STRING (attr);
3495   pst = start_psymtab_common (objfile, objfile->section_offsets,
3496                               filename,
3497                               /* TEXTLOW and TEXTHIGH are set below.  */
3498                               0,
3499                               objfile->global_psymbols.next,
3500                               objfile->static_psymbols.next);
3501   pst->psymtabs_addrmap_supported = 1;
3502
3503   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3504   if (attr != NULL)
3505     pst->dirname = DW_STRING (attr);
3506
3507   pst->read_symtab_private = this_cu;
3508
3509   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3510
3511   /* Store the function that reads in the rest of the symbol table.  */
3512   pst->read_symtab = dwarf2_psymtab_to_symtab;
3513
3514   this_cu->v.psymtab = pst;
3515
3516   dwarf2_find_base_address (comp_unit_die, &cu);
3517
3518   /* Possibly set the default values of LOWPC and HIGHPC from
3519      `DW_AT_ranges'.  */
3520   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3521                                       &best_highpc, &cu, pst);
3522   if (has_pc_info == 1 && best_lowpc < best_highpc)
3523     /* Store the contiguous range if it is not empty; it can be empty for
3524        CUs with no code.  */
3525     addrmap_set_empty (objfile->psymtabs_addrmap,
3526                        best_lowpc + baseaddr,
3527                        best_highpc + baseaddr - 1, pst);
3528
3529   /* Check if comp unit has_children.
3530      If so, read the rest of the partial symbols from this comp unit.
3531      If not, there's no more debug_info for this comp unit.  */
3532   if (has_children)
3533     {
3534       struct partial_die_info *first_die;
3535       CORE_ADDR lowpc, highpc;
3536
3537       lowpc = ((CORE_ADDR) -1);
3538       highpc = ((CORE_ADDR) 0);
3539
3540       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3541
3542       scan_partial_symbols (first_die, &lowpc, &highpc,
3543                             ! has_pc_info, &cu);
3544
3545       /* If we didn't find a lowpc, set it to highpc to avoid
3546          complaints from `maint check'.  */
3547       if (lowpc == ((CORE_ADDR) -1))
3548         lowpc = highpc;
3549
3550       /* If the compilation unit didn't have an explicit address range,
3551          then use the information extracted from its child dies.  */
3552       if (! has_pc_info)
3553         {
3554           best_lowpc = lowpc;
3555           best_highpc = highpc;
3556         }
3557     }
3558   pst->textlow = best_lowpc + baseaddr;
3559   pst->texthigh = best_highpc + baseaddr;
3560
3561   pst->n_global_syms = objfile->global_psymbols.next -
3562     (objfile->global_psymbols.list + pst->globals_offset);
3563   pst->n_static_syms = objfile->static_psymbols.next -
3564     (objfile->static_psymbols.list + pst->statics_offset);
3565   sort_pst_symbols (pst);
3566
3567   info_ptr = (beg_of_comp_unit + cu.header.length
3568               + cu.header.initial_length_size);
3569
3570   if (this_cu->debug_types_section)
3571     {
3572       /* It's not clear we want to do anything with stmt lists here.
3573          Waiting to see what gcc ultimately does.  */
3574     }
3575   else
3576     {
3577       /* Get the list of files included in the current compilation unit,
3578          and build a psymtab for each of them.  */
3579       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3580     }
3581
3582   do_cleanups (back_to_inner);
3583
3584   return info_ptr;
3585 }
3586
3587 /* Traversal function for htab_traverse_noresize.
3588    Process one .debug_types comp-unit.  */
3589
3590 static int
3591 process_type_comp_unit (void **slot, void *info)
3592 {
3593   struct signatured_type *entry = (struct signatured_type *) *slot;
3594   struct objfile *objfile = (struct objfile *) info;
3595   struct dwarf2_per_cu_data *this_cu;
3596
3597   this_cu = &entry->per_cu;
3598
3599   gdb_assert (this_cu->debug_types_section->readin);
3600   process_psymtab_comp_unit (objfile, this_cu,
3601                              this_cu->debug_types_section->buffer,
3602                              (this_cu->debug_types_section->buffer
3603                               + this_cu->offset),
3604                              this_cu->debug_types_section->size);
3605
3606   return 1;
3607 }
3608
3609 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3610    Build partial symbol tables for the .debug_types comp-units.  */
3611
3612 static void
3613 build_type_psymtabs (struct objfile *objfile)
3614 {
3615   if (! create_debug_types_hash_table (objfile))
3616     return;
3617
3618   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3619                           process_type_comp_unit, objfile);
3620 }
3621
3622 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3623
3624 static void
3625 psymtabs_addrmap_cleanup (void *o)
3626 {
3627   struct objfile *objfile = o;
3628
3629   objfile->psymtabs_addrmap = NULL;
3630 }
3631
3632 /* Build the partial symbol table by doing a quick pass through the
3633    .debug_info and .debug_abbrev sections.  */
3634
3635 static void
3636 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3637 {
3638   gdb_byte *info_ptr;
3639   struct cleanup *back_to, *addrmap_cleanup;
3640   struct obstack temp_obstack;
3641
3642   dwarf2_per_objfile->reading_partial_symbols = 1;
3643
3644   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3645   info_ptr = dwarf2_per_objfile->info.buffer;
3646
3647   /* Any cached compilation units will be linked by the per-objfile
3648      read_in_chain.  Make sure to free them when we're done.  */
3649   back_to = make_cleanup (free_cached_comp_units, NULL);
3650
3651   build_type_psymtabs (objfile);
3652
3653   create_all_comp_units (objfile);
3654
3655   /* Create a temporary address map on a temporary obstack.  We later
3656      copy this to the final obstack.  */
3657   obstack_init (&temp_obstack);
3658   make_cleanup_obstack_free (&temp_obstack);
3659   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3660   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3661
3662   /* Since the objects we're extracting from .debug_info vary in
3663      length, only the individual functions to extract them (like
3664      read_comp_unit_head and load_partial_die) can really know whether
3665      the buffer is large enough to hold another complete object.
3666
3667      At the moment, they don't actually check that.  If .debug_info
3668      holds just one extra byte after the last compilation unit's dies,
3669      then read_comp_unit_head will happily read off the end of the
3670      buffer.  read_partial_die is similarly casual.  Those functions
3671      should be fixed.
3672
3673      For this loop condition, simply checking whether there's any data
3674      left at all should be sufficient.  */
3675
3676   while (info_ptr < (dwarf2_per_objfile->info.buffer
3677                      + dwarf2_per_objfile->info.size))
3678     {
3679       struct dwarf2_per_cu_data *this_cu;
3680
3681       this_cu = dwarf2_find_comp_unit (info_ptr
3682                                        - dwarf2_per_objfile->info.buffer,
3683                                        objfile);
3684
3685       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3686                                             dwarf2_per_objfile->info.buffer,
3687                                             info_ptr,
3688                                             dwarf2_per_objfile->info.size);
3689     }
3690
3691   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3692                                                     &objfile->objfile_obstack);
3693   discard_cleanups (addrmap_cleanup);
3694
3695   do_cleanups (back_to);
3696 }
3697
3698 /* Load the partial DIEs for a secondary CU into memory.  */
3699
3700 static void
3701 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3702                         struct objfile *objfile)
3703 {
3704   bfd *abfd = objfile->obfd;
3705   gdb_byte *info_ptr;
3706   struct die_info *comp_unit_die;
3707   struct dwarf2_cu *cu;
3708   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3709   int has_children;
3710   struct die_reader_specs reader_specs;
3711   int read_cu = 0;
3712
3713   gdb_assert (! this_cu->debug_types_section);
3714
3715   gdb_assert (dwarf2_per_objfile->info.readin);
3716   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3717
3718   if (this_cu->cu == NULL)
3719     {
3720       cu = xmalloc (sizeof (*cu));
3721       init_one_comp_unit (cu, objfile);
3722
3723       read_cu = 1;
3724
3725       /* If an error occurs while loading, release our storage.  */
3726       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3727
3728       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3729                                               dwarf2_per_objfile->info.buffer,
3730                                               dwarf2_per_objfile->info.size,
3731                                               abfd, 0);
3732
3733       /* Skip dummy compilation units.  */
3734       if (info_ptr >= (dwarf2_per_objfile->info.buffer
3735                        + dwarf2_per_objfile->info.size)
3736           || peek_abbrev_code (abfd, info_ptr) == 0)
3737         {
3738           do_cleanups (free_cu_cleanup);
3739           return;
3740         }
3741
3742       /* Link this compilation unit into the compilation unit tree.  */
3743       this_cu->cu = cu;
3744       cu->per_cu = this_cu;
3745
3746       /* Link this CU into read_in_chain.  */
3747       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3748       dwarf2_per_objfile->read_in_chain = this_cu;
3749     }
3750   else
3751     {
3752       cu = this_cu->cu;
3753       info_ptr += cu->header.first_die_offset;
3754     }
3755
3756   /* Read the abbrevs for this compilation unit into a table.  */
3757   gdb_assert (cu->dwarf2_abbrevs == NULL);
3758   dwarf2_read_abbrevs (abfd, cu);
3759   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3760
3761   /* Read the compilation unit die.  */
3762   init_cu_die_reader (&reader_specs, cu);
3763   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3764                             &has_children);
3765
3766   prepare_one_comp_unit (cu, comp_unit_die);
3767
3768   /* Check if comp unit has_children.
3769      If so, read the rest of the partial symbols from this comp unit.
3770      If not, there's no more debug_info for this comp unit.  */
3771   if (has_children)
3772     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3773
3774   do_cleanups (free_abbrevs_cleanup);
3775
3776   if (read_cu)
3777     {
3778       /* We've successfully allocated this compilation unit.  Let our
3779          caller clean it up when finished with it.  */
3780       discard_cleanups (free_cu_cleanup);
3781     }
3782 }
3783
3784 /* Create a list of all compilation units in OBJFILE.  We do this only
3785    if an inter-comp-unit reference is found; presumably if there is one,
3786    there will be many, and one will occur early in the .debug_info section.
3787    So there's no point in building this list incrementally.  */
3788
3789 static void
3790 create_all_comp_units (struct objfile *objfile)
3791 {
3792   int n_allocated;
3793   int n_comp_units;
3794   struct dwarf2_per_cu_data **all_comp_units;
3795   gdb_byte *info_ptr;
3796
3797   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3798   info_ptr = dwarf2_per_objfile->info.buffer;
3799
3800   n_comp_units = 0;
3801   n_allocated = 10;
3802   all_comp_units = xmalloc (n_allocated
3803                             * sizeof (struct dwarf2_per_cu_data *));
3804
3805   while (info_ptr < dwarf2_per_objfile->info.buffer
3806          + dwarf2_per_objfile->info.size)
3807     {
3808       unsigned int length, initial_length_size;
3809       struct dwarf2_per_cu_data *this_cu;
3810       unsigned int offset;
3811
3812       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3813
3814       /* Read just enough information to find out where the next
3815          compilation unit is.  */
3816       length = read_initial_length (objfile->obfd, info_ptr,
3817                                     &initial_length_size);
3818
3819       /* Save the compilation unit for later lookup.  */
3820       this_cu = obstack_alloc (&objfile->objfile_obstack,
3821                                sizeof (struct dwarf2_per_cu_data));
3822       memset (this_cu, 0, sizeof (*this_cu));
3823       this_cu->offset = offset;
3824       this_cu->length = length + initial_length_size;
3825       this_cu->objfile = objfile;
3826
3827       if (n_comp_units == n_allocated)
3828         {
3829           n_allocated *= 2;
3830           all_comp_units = xrealloc (all_comp_units,
3831                                      n_allocated
3832                                      * sizeof (struct dwarf2_per_cu_data *));
3833         }
3834       all_comp_units[n_comp_units++] = this_cu;
3835
3836       info_ptr = info_ptr + this_cu->length;
3837     }
3838
3839   dwarf2_per_objfile->all_comp_units
3840     = obstack_alloc (&objfile->objfile_obstack,
3841                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3842   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3843           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3844   xfree (all_comp_units);
3845   dwarf2_per_objfile->n_comp_units = n_comp_units;
3846 }
3847
3848 /* Process all loaded DIEs for compilation unit CU, starting at
3849    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3850    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3851    DW_AT_ranges).  If NEED_PC is set, then this function will set
3852    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3853    and record the covered ranges in the addrmap.  */
3854
3855 static void
3856 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3857                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3858 {
3859   struct partial_die_info *pdi;
3860
3861   /* Now, march along the PDI's, descending into ones which have
3862      interesting children but skipping the children of the other ones,
3863      until we reach the end of the compilation unit.  */
3864
3865   pdi = first_die;
3866
3867   while (pdi != NULL)
3868     {
3869       fixup_partial_die (pdi, cu);
3870
3871       /* Anonymous namespaces or modules have no name but have interesting
3872          children, so we need to look at them.  Ditto for anonymous
3873          enums.  */
3874
3875       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3876           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3877         {
3878           switch (pdi->tag)
3879             {
3880             case DW_TAG_subprogram:
3881               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3882               break;
3883             case DW_TAG_constant:
3884             case DW_TAG_variable:
3885             case DW_TAG_typedef:
3886             case DW_TAG_union_type:
3887               if (!pdi->is_declaration)
3888                 {
3889                   add_partial_symbol (pdi, cu);
3890                 }
3891               break;
3892             case DW_TAG_class_type:
3893             case DW_TAG_interface_type:
3894             case DW_TAG_structure_type:
3895               if (!pdi->is_declaration)
3896                 {
3897                   add_partial_symbol (pdi, cu);
3898                 }
3899               break;
3900             case DW_TAG_enumeration_type:
3901               if (!pdi->is_declaration)
3902                 add_partial_enumeration (pdi, cu);
3903               break;
3904             case DW_TAG_base_type:
3905             case DW_TAG_subrange_type:
3906               /* File scope base type definitions are added to the partial
3907                  symbol table.  */
3908               add_partial_symbol (pdi, cu);
3909               break;
3910             case DW_TAG_namespace:
3911               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3912               break;
3913             case DW_TAG_module:
3914               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3915               break;
3916             default:
3917               break;
3918             }
3919         }
3920
3921       /* If the die has a sibling, skip to the sibling.  */
3922
3923       pdi = pdi->die_sibling;
3924     }
3925 }
3926
3927 /* Functions used to compute the fully scoped name of a partial DIE.
3928
3929    Normally, this is simple.  For C++, the parent DIE's fully scoped
3930    name is concatenated with "::" and the partial DIE's name.  For
3931    Java, the same thing occurs except that "." is used instead of "::".
3932    Enumerators are an exception; they use the scope of their parent
3933    enumeration type, i.e. the name of the enumeration type is not
3934    prepended to the enumerator.
3935
3936    There are two complexities.  One is DW_AT_specification; in this
3937    case "parent" means the parent of the target of the specification,
3938    instead of the direct parent of the DIE.  The other is compilers
3939    which do not emit DW_TAG_namespace; in this case we try to guess
3940    the fully qualified name of structure types from their members'
3941    linkage names.  This must be done using the DIE's children rather
3942    than the children of any DW_AT_specification target.  We only need
3943    to do this for structures at the top level, i.e. if the target of
3944    any DW_AT_specification (if any; otherwise the DIE itself) does not
3945    have a parent.  */
3946
3947 /* Compute the scope prefix associated with PDI's parent, in
3948    compilation unit CU.  The result will be allocated on CU's
3949    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3950    field.  NULL is returned if no prefix is necessary.  */
3951 static char *
3952 partial_die_parent_scope (struct partial_die_info *pdi,
3953                           struct dwarf2_cu *cu)
3954 {
3955   char *grandparent_scope;
3956   struct partial_die_info *parent, *real_pdi;
3957
3958   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3959      then this means the parent of the specification DIE.  */
3960
3961   real_pdi = pdi;
3962   while (real_pdi->has_specification)
3963     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3964
3965   parent = real_pdi->die_parent;
3966   if (parent == NULL)
3967     return NULL;
3968
3969   if (parent->scope_set)
3970     return parent->scope;
3971
3972   fixup_partial_die (parent, cu);
3973
3974   grandparent_scope = partial_die_parent_scope (parent, cu);
3975
3976   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3977      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3978      Work around this problem here.  */
3979   if (cu->language == language_cplus
3980       && parent->tag == DW_TAG_namespace
3981       && strcmp (parent->name, "::") == 0
3982       && grandparent_scope == NULL)
3983     {
3984       parent->scope = NULL;
3985       parent->scope_set = 1;
3986       return NULL;
3987     }
3988
3989   if (pdi->tag == DW_TAG_enumerator)
3990     /* Enumerators should not get the name of the enumeration as a prefix.  */
3991     parent->scope = grandparent_scope;
3992   else if (parent->tag == DW_TAG_namespace
3993       || parent->tag == DW_TAG_module
3994       || parent->tag == DW_TAG_structure_type
3995       || parent->tag == DW_TAG_class_type
3996       || parent->tag == DW_TAG_interface_type
3997       || parent->tag == DW_TAG_union_type
3998       || parent->tag == DW_TAG_enumeration_type)
3999     {
4000       if (grandparent_scope == NULL)
4001         parent->scope = parent->name;
4002       else
4003         parent->scope = typename_concat (&cu->comp_unit_obstack,
4004                                          grandparent_scope,
4005                                          parent->name, 0, cu);
4006     }
4007   else
4008     {
4009       /* FIXME drow/2004-04-01: What should we be doing with
4010          function-local names?  For partial symbols, we should probably be
4011          ignoring them.  */
4012       complaint (&symfile_complaints,
4013                  _("unhandled containing DIE tag %d for DIE at %d"),
4014                  parent->tag, pdi->offset);
4015       parent->scope = grandparent_scope;
4016     }
4017
4018   parent->scope_set = 1;
4019   return parent->scope;
4020 }
4021
4022 /* Return the fully scoped name associated with PDI, from compilation unit
4023    CU.  The result will be allocated with malloc.  */
4024 static char *
4025 partial_die_full_name (struct partial_die_info *pdi,
4026                        struct dwarf2_cu *cu)
4027 {
4028   char *parent_scope;
4029
4030   /* If this is a template instantiation, we can not work out the
4031      template arguments from partial DIEs.  So, unfortunately, we have
4032      to go through the full DIEs.  At least any work we do building
4033      types here will be reused if full symbols are loaded later.  */
4034   if (pdi->has_template_arguments)
4035     {
4036       fixup_partial_die (pdi, cu);
4037
4038       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4039         {
4040           struct die_info *die;
4041           struct attribute attr;
4042           struct dwarf2_cu *ref_cu = cu;
4043
4044           attr.name = 0;
4045           attr.form = DW_FORM_ref_addr;
4046           attr.u.addr = pdi->offset;
4047           die = follow_die_ref (NULL, &attr, &ref_cu);
4048
4049           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4050         }
4051     }
4052
4053   parent_scope = partial_die_parent_scope (pdi, cu);
4054   if (parent_scope == NULL)
4055     return NULL;
4056   else
4057     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4058 }
4059
4060 static void
4061 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4062 {
4063   struct objfile *objfile = cu->objfile;
4064   CORE_ADDR addr = 0;
4065   char *actual_name = NULL;
4066   const struct partial_symbol *psym = NULL;
4067   CORE_ADDR baseaddr;
4068   int built_actual_name = 0;
4069
4070   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4071
4072   actual_name = partial_die_full_name (pdi, cu);
4073   if (actual_name)
4074     built_actual_name = 1;
4075
4076   if (actual_name == NULL)
4077     actual_name = pdi->name;
4078
4079   switch (pdi->tag)
4080     {
4081     case DW_TAG_subprogram:
4082       if (pdi->is_external || cu->language == language_ada)
4083         {
4084           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4085              of the global scope.  But in Ada, we want to be able to access
4086              nested procedures globally.  So all Ada subprograms are stored
4087              in the global scope.  */
4088           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4089              mst_text, objfile); */
4090           add_psymbol_to_list (actual_name, strlen (actual_name),
4091                                built_actual_name,
4092                                VAR_DOMAIN, LOC_BLOCK,
4093                                &objfile->global_psymbols,
4094                                0, pdi->lowpc + baseaddr,
4095                                cu->language, objfile);
4096         }
4097       else
4098         {
4099           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4100              mst_file_text, objfile); */
4101           add_psymbol_to_list (actual_name, strlen (actual_name),
4102                                built_actual_name,
4103                                VAR_DOMAIN, LOC_BLOCK,
4104                                &objfile->static_psymbols,
4105                                0, pdi->lowpc + baseaddr,
4106                                cu->language, objfile);
4107         }
4108       break;
4109     case DW_TAG_constant:
4110       {
4111         struct psymbol_allocation_list *list;
4112
4113         if (pdi->is_external)
4114           list = &objfile->global_psymbols;
4115         else
4116           list = &objfile->static_psymbols;
4117         add_psymbol_to_list (actual_name, strlen (actual_name),
4118                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4119                              list, 0, 0, cu->language, objfile);
4120       }
4121       break;
4122     case DW_TAG_variable:
4123       if (pdi->locdesc)
4124         addr = decode_locdesc (pdi->locdesc, cu);
4125
4126       if (pdi->locdesc
4127           && addr == 0
4128           && !dwarf2_per_objfile->has_section_at_zero)
4129         {
4130           /* A global or static variable may also have been stripped
4131              out by the linker if unused, in which case its address
4132              will be nullified; do not add such variables into partial
4133              symbol table then.  */
4134         }
4135       else if (pdi->is_external)
4136         {
4137           /* Global Variable.
4138              Don't enter into the minimal symbol tables as there is
4139              a minimal symbol table entry from the ELF symbols already.
4140              Enter into partial symbol table if it has a location
4141              descriptor or a type.
4142              If the location descriptor is missing, new_symbol will create
4143              a LOC_UNRESOLVED symbol, the address of the variable will then
4144              be determined from the minimal symbol table whenever the variable
4145              is referenced.
4146              The address for the partial symbol table entry is not
4147              used by GDB, but it comes in handy for debugging partial symbol
4148              table building.  */
4149
4150           if (pdi->locdesc || pdi->has_type)
4151             add_psymbol_to_list (actual_name, strlen (actual_name),
4152                                  built_actual_name,
4153                                  VAR_DOMAIN, LOC_STATIC,
4154                                  &objfile->global_psymbols,
4155                                  0, addr + baseaddr,
4156                                  cu->language, objfile);
4157         }
4158       else
4159         {
4160           /* Static Variable.  Skip symbols without location descriptors.  */
4161           if (pdi->locdesc == NULL)
4162             {
4163               if (built_actual_name)
4164                 xfree (actual_name);
4165               return;
4166             }
4167           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4168              mst_file_data, objfile); */
4169           add_psymbol_to_list (actual_name, strlen (actual_name),
4170                                built_actual_name,
4171                                VAR_DOMAIN, LOC_STATIC,
4172                                &objfile->static_psymbols,
4173                                0, addr + baseaddr,
4174                                cu->language, objfile);
4175         }
4176       break;
4177     case DW_TAG_typedef:
4178     case DW_TAG_base_type:
4179     case DW_TAG_subrange_type:
4180       add_psymbol_to_list (actual_name, strlen (actual_name),
4181                            built_actual_name,
4182                            VAR_DOMAIN, LOC_TYPEDEF,
4183                            &objfile->static_psymbols,
4184                            0, (CORE_ADDR) 0, cu->language, objfile);
4185       break;
4186     case DW_TAG_namespace:
4187       add_psymbol_to_list (actual_name, strlen (actual_name),
4188                            built_actual_name,
4189                            VAR_DOMAIN, LOC_TYPEDEF,
4190                            &objfile->global_psymbols,
4191                            0, (CORE_ADDR) 0, cu->language, objfile);
4192       break;
4193     case DW_TAG_class_type:
4194     case DW_TAG_interface_type:
4195     case DW_TAG_structure_type:
4196     case DW_TAG_union_type:
4197     case DW_TAG_enumeration_type:
4198       /* Skip external references.  The DWARF standard says in the section
4199          about "Structure, Union, and Class Type Entries": "An incomplete
4200          structure, union or class type is represented by a structure,
4201          union or class entry that does not have a byte size attribute
4202          and that has a DW_AT_declaration attribute."  */
4203       if (!pdi->has_byte_size && pdi->is_declaration)
4204         {
4205           if (built_actual_name)
4206             xfree (actual_name);
4207           return;
4208         }
4209
4210       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4211          static vs. global.  */
4212       add_psymbol_to_list (actual_name, strlen (actual_name),
4213                            built_actual_name,
4214                            STRUCT_DOMAIN, LOC_TYPEDEF,
4215                            (cu->language == language_cplus
4216                             || cu->language == language_java)
4217                            ? &objfile->global_psymbols
4218                            : &objfile->static_psymbols,
4219                            0, (CORE_ADDR) 0, cu->language, objfile);
4220
4221       break;
4222     case DW_TAG_enumerator:
4223       add_psymbol_to_list (actual_name, strlen (actual_name),
4224                            built_actual_name,
4225                            VAR_DOMAIN, LOC_CONST,
4226                            (cu->language == language_cplus
4227                             || cu->language == language_java)
4228                            ? &objfile->global_psymbols
4229                            : &objfile->static_psymbols,
4230                            0, (CORE_ADDR) 0, cu->language, objfile);
4231       break;
4232     default:
4233       break;
4234     }
4235
4236   if (built_actual_name)
4237     xfree (actual_name);
4238 }
4239
4240 /* Read a partial die corresponding to a namespace; also, add a symbol
4241    corresponding to that namespace to the symbol table.  NAMESPACE is
4242    the name of the enclosing namespace.  */
4243
4244 static void
4245 add_partial_namespace (struct partial_die_info *pdi,
4246                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4247                        int need_pc, struct dwarf2_cu *cu)
4248 {
4249   /* Add a symbol for the namespace.  */
4250
4251   add_partial_symbol (pdi, cu);
4252
4253   /* Now scan partial symbols in that namespace.  */
4254
4255   if (pdi->has_children)
4256     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4257 }
4258
4259 /* Read a partial die corresponding to a Fortran module.  */
4260
4261 static void
4262 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4263                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4264 {
4265   /* Now scan partial symbols in that module.  */
4266
4267   if (pdi->has_children)
4268     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4269 }
4270
4271 /* Read a partial die corresponding to a subprogram and create a partial
4272    symbol for that subprogram.  When the CU language allows it, this
4273    routine also defines a partial symbol for each nested subprogram
4274    that this subprogram contains.
4275
4276    DIE my also be a lexical block, in which case we simply search
4277    recursively for suprograms defined inside that lexical block.
4278    Again, this is only performed when the CU language allows this
4279    type of definitions.  */
4280
4281 static void
4282 add_partial_subprogram (struct partial_die_info *pdi,
4283                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4284                         int need_pc, struct dwarf2_cu *cu)
4285 {
4286   if (pdi->tag == DW_TAG_subprogram)
4287     {
4288       if (pdi->has_pc_info)
4289         {
4290           if (pdi->lowpc < *lowpc)
4291             *lowpc = pdi->lowpc;
4292           if (pdi->highpc > *highpc)
4293             *highpc = pdi->highpc;
4294           if (need_pc)
4295             {
4296               CORE_ADDR baseaddr;
4297               struct objfile *objfile = cu->objfile;
4298
4299               baseaddr = ANOFFSET (objfile->section_offsets,
4300                                    SECT_OFF_TEXT (objfile));
4301               addrmap_set_empty (objfile->psymtabs_addrmap,
4302                                  pdi->lowpc + baseaddr,
4303                                  pdi->highpc - 1 + baseaddr,
4304                                  cu->per_cu->v.psymtab);
4305             }
4306           if (!pdi->is_declaration)
4307             /* Ignore subprogram DIEs that do not have a name, they are
4308                illegal.  Do not emit a complaint at this point, we will
4309                do so when we convert this psymtab into a symtab.  */
4310             if (pdi->name)
4311               add_partial_symbol (pdi, cu);
4312         }
4313     }
4314
4315   if (! pdi->has_children)
4316     return;
4317
4318   if (cu->language == language_ada)
4319     {
4320       pdi = pdi->die_child;
4321       while (pdi != NULL)
4322         {
4323           fixup_partial_die (pdi, cu);
4324           if (pdi->tag == DW_TAG_subprogram
4325               || pdi->tag == DW_TAG_lexical_block)
4326             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4327           pdi = pdi->die_sibling;
4328         }
4329     }
4330 }
4331
4332 /* Read a partial die corresponding to an enumeration type.  */
4333
4334 static void
4335 add_partial_enumeration (struct partial_die_info *enum_pdi,
4336                          struct dwarf2_cu *cu)
4337 {
4338   struct partial_die_info *pdi;
4339
4340   if (enum_pdi->name != NULL)
4341     add_partial_symbol (enum_pdi, cu);
4342
4343   pdi = enum_pdi->die_child;
4344   while (pdi)
4345     {
4346       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4347         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4348       else
4349         add_partial_symbol (pdi, cu);
4350       pdi = pdi->die_sibling;
4351     }
4352 }
4353
4354 /* Return the initial uleb128 in the die at INFO_PTR.  */
4355
4356 static unsigned int
4357 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4358 {
4359   unsigned int bytes_read;
4360
4361   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4362 }
4363
4364 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4365    Return the corresponding abbrev, or NULL if the number is zero (indicating
4366    an empty DIE).  In either case *BYTES_READ will be set to the length of
4367    the initial number.  */
4368
4369 static struct abbrev_info *
4370 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4371                  struct dwarf2_cu *cu)
4372 {
4373   bfd *abfd = cu->objfile->obfd;
4374   unsigned int abbrev_number;
4375   struct abbrev_info *abbrev;
4376
4377   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4378
4379   if (abbrev_number == 0)
4380     return NULL;
4381
4382   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4383   if (!abbrev)
4384     {
4385       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4386              abbrev_number, bfd_get_filename (abfd));
4387     }
4388
4389   return abbrev;
4390 }
4391
4392 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4393    Returns a pointer to the end of a series of DIEs, terminated by an empty
4394    DIE.  Any children of the skipped DIEs will also be skipped.  */
4395
4396 static gdb_byte *
4397 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4398 {
4399   struct abbrev_info *abbrev;
4400   unsigned int bytes_read;
4401
4402   while (1)
4403     {
4404       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4405       if (abbrev == NULL)
4406         return info_ptr + bytes_read;
4407       else
4408         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4409     }
4410 }
4411
4412 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4413    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4414    abbrev corresponding to that skipped uleb128 should be passed in
4415    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4416    children.  */
4417
4418 static gdb_byte *
4419 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4420               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4421 {
4422   unsigned int bytes_read;
4423   struct attribute attr;
4424   bfd *abfd = cu->objfile->obfd;
4425   unsigned int form, i;
4426
4427   for (i = 0; i < abbrev->num_attrs; i++)
4428     {
4429       /* The only abbrev we care about is DW_AT_sibling.  */
4430       if (abbrev->attrs[i].name == DW_AT_sibling)
4431         {
4432           read_attribute (&attr, &abbrev->attrs[i],
4433                           abfd, info_ptr, cu);
4434           if (attr.form == DW_FORM_ref_addr)
4435             complaint (&symfile_complaints,
4436                        _("ignoring absolute DW_AT_sibling"));
4437           else
4438             return buffer + dwarf2_get_ref_die_offset (&attr);
4439         }
4440
4441       /* If it isn't DW_AT_sibling, skip this attribute.  */
4442       form = abbrev->attrs[i].form;
4443     skip_attribute:
4444       switch (form)
4445         {
4446         case DW_FORM_ref_addr:
4447           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4448              and later it is offset sized.  */
4449           if (cu->header.version == 2)
4450             info_ptr += cu->header.addr_size;
4451           else
4452             info_ptr += cu->header.offset_size;
4453           break;
4454         case DW_FORM_addr:
4455           info_ptr += cu->header.addr_size;
4456           break;
4457         case DW_FORM_data1:
4458         case DW_FORM_ref1:
4459         case DW_FORM_flag:
4460           info_ptr += 1;
4461           break;
4462         case DW_FORM_flag_present:
4463           break;
4464         case DW_FORM_data2:
4465         case DW_FORM_ref2:
4466           info_ptr += 2;
4467           break;
4468         case DW_FORM_data4:
4469         case DW_FORM_ref4:
4470           info_ptr += 4;
4471           break;
4472         case DW_FORM_data8:
4473         case DW_FORM_ref8:
4474         case DW_FORM_ref_sig8:
4475           info_ptr += 8;
4476           break;
4477         case DW_FORM_string:
4478           read_direct_string (abfd, info_ptr, &bytes_read);
4479           info_ptr += bytes_read;
4480           break;
4481         case DW_FORM_sec_offset:
4482         case DW_FORM_strp:
4483           info_ptr += cu->header.offset_size;
4484           break;
4485         case DW_FORM_exprloc:
4486         case DW_FORM_block:
4487           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4488           info_ptr += bytes_read;
4489           break;
4490         case DW_FORM_block1:
4491           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4492           break;
4493         case DW_FORM_block2:
4494           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4495           break;
4496         case DW_FORM_block4:
4497           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4498           break;
4499         case DW_FORM_sdata:
4500         case DW_FORM_udata:
4501         case DW_FORM_ref_udata:
4502           info_ptr = skip_leb128 (abfd, info_ptr);
4503           break;
4504         case DW_FORM_indirect:
4505           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4506           info_ptr += bytes_read;
4507           /* We need to continue parsing from here, so just go back to
4508              the top.  */
4509           goto skip_attribute;
4510
4511         default:
4512           error (_("Dwarf Error: Cannot handle %s "
4513                    "in DWARF reader [in module %s]"),
4514                  dwarf_form_name (form),
4515                  bfd_get_filename (abfd));
4516         }
4517     }
4518
4519   if (abbrev->has_children)
4520     return skip_children (buffer, info_ptr, cu);
4521   else
4522     return info_ptr;
4523 }
4524
4525 /* Locate ORIG_PDI's sibling.
4526    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4527    in BUFFER.  */
4528
4529 static gdb_byte *
4530 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4531                     gdb_byte *buffer, gdb_byte *info_ptr,
4532                     bfd *abfd, struct dwarf2_cu *cu)
4533 {
4534   /* Do we know the sibling already?  */
4535
4536   if (orig_pdi->sibling)
4537     return orig_pdi->sibling;
4538
4539   /* Are there any children to deal with?  */
4540
4541   if (!orig_pdi->has_children)
4542     return info_ptr;
4543
4544   /* Skip the children the long way.  */
4545
4546   return skip_children (buffer, info_ptr, cu);
4547 }
4548
4549 /* Expand this partial symbol table into a full symbol table.  */
4550
4551 static void
4552 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4553 {
4554   if (pst != NULL)
4555     {
4556       if (pst->readin)
4557         {
4558           warning (_("bug: psymtab for %s is already read in."),
4559                    pst->filename);
4560         }
4561       else
4562         {
4563           if (info_verbose)
4564             {
4565               printf_filtered (_("Reading in symbols for %s..."),
4566                                pst->filename);
4567               gdb_flush (gdb_stdout);
4568             }
4569
4570           /* Restore our global data.  */
4571           dwarf2_per_objfile = objfile_data (pst->objfile,
4572                                              dwarf2_objfile_data_key);
4573
4574           /* If this psymtab is constructed from a debug-only objfile, the
4575              has_section_at_zero flag will not necessarily be correct.  We
4576              can get the correct value for this flag by looking at the data
4577              associated with the (presumably stripped) associated objfile.  */
4578           if (pst->objfile->separate_debug_objfile_backlink)
4579             {
4580               struct dwarf2_per_objfile *dpo_backlink
4581                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4582                                 dwarf2_objfile_data_key);
4583
4584               dwarf2_per_objfile->has_section_at_zero
4585                 = dpo_backlink->has_section_at_zero;
4586             }
4587
4588           dwarf2_per_objfile->reading_partial_symbols = 0;
4589
4590           psymtab_to_symtab_1 (pst);
4591
4592           /* Finish up the debug error message.  */
4593           if (info_verbose)
4594             printf_filtered (_("done.\n"));
4595         }
4596     }
4597 }
4598
4599 /* Add PER_CU to the queue.  */
4600
4601 static void
4602 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4603 {
4604   struct dwarf2_queue_item *item;
4605
4606   per_cu->queued = 1;
4607   item = xmalloc (sizeof (*item));
4608   item->per_cu = per_cu;
4609   item->next = NULL;
4610
4611   if (dwarf2_queue == NULL)
4612     dwarf2_queue = item;
4613   else
4614     dwarf2_queue_tail->next = item;
4615
4616   dwarf2_queue_tail = item;
4617 }
4618
4619 /* Process the queue.  */
4620
4621 static void
4622 process_queue (struct objfile *objfile)
4623 {
4624   struct dwarf2_queue_item *item, *next_item;
4625
4626   /* The queue starts out with one item, but following a DIE reference
4627      may load a new CU, adding it to the end of the queue.  */
4628   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4629     {
4630       if (dwarf2_per_objfile->using_index
4631           ? !item->per_cu->v.quick->symtab
4632           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4633         process_full_comp_unit (item->per_cu);
4634
4635       item->per_cu->queued = 0;
4636       next_item = item->next;
4637       xfree (item);
4638     }
4639
4640   dwarf2_queue_tail = NULL;
4641 }
4642
4643 /* Free all allocated queue entries.  This function only releases anything if
4644    an error was thrown; if the queue was processed then it would have been
4645    freed as we went along.  */
4646
4647 static void
4648 dwarf2_release_queue (void *dummy)
4649 {
4650   struct dwarf2_queue_item *item, *last;
4651
4652   item = dwarf2_queue;
4653   while (item)
4654     {
4655       /* Anything still marked queued is likely to be in an
4656          inconsistent state, so discard it.  */
4657       if (item->per_cu->queued)
4658         {
4659           if (item->per_cu->cu != NULL)
4660             free_one_cached_comp_unit (item->per_cu->cu);
4661           item->per_cu->queued = 0;
4662         }
4663
4664       last = item;
4665       item = item->next;
4666       xfree (last);
4667     }
4668
4669   dwarf2_queue = dwarf2_queue_tail = NULL;
4670 }
4671
4672 /* Read in full symbols for PST, and anything it depends on.  */
4673
4674 static void
4675 psymtab_to_symtab_1 (struct partial_symtab *pst)
4676 {
4677   struct dwarf2_per_cu_data *per_cu;
4678   struct cleanup *back_to;
4679   int i;
4680
4681   for (i = 0; i < pst->number_of_dependencies; i++)
4682     if (!pst->dependencies[i]->readin)
4683       {
4684         /* Inform about additional files that need to be read in.  */
4685         if (info_verbose)
4686           {
4687             /* FIXME: i18n: Need to make this a single string.  */
4688             fputs_filtered (" ", gdb_stdout);
4689             wrap_here ("");
4690             fputs_filtered ("and ", gdb_stdout);
4691             wrap_here ("");
4692             printf_filtered ("%s...", pst->dependencies[i]->filename);
4693             wrap_here ("");     /* Flush output.  */
4694             gdb_flush (gdb_stdout);
4695           }
4696         psymtab_to_symtab_1 (pst->dependencies[i]);
4697       }
4698
4699   per_cu = pst->read_symtab_private;
4700
4701   if (per_cu == NULL)
4702     {
4703       /* It's an include file, no symbols to read for it.
4704          Everything is in the parent symtab.  */
4705       pst->readin = 1;
4706       return;
4707     }
4708
4709   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4710 }
4711
4712 /* Load the DIEs associated with PER_CU into memory.  */
4713
4714 static void
4715 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4716                      struct objfile *objfile)
4717 {
4718   bfd *abfd = objfile->obfd;
4719   struct dwarf2_cu *cu;
4720   unsigned int offset;
4721   gdb_byte *info_ptr, *beg_of_comp_unit;
4722   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4723   struct attribute *attr;
4724   int read_cu = 0;
4725
4726   gdb_assert (! per_cu->debug_types_section);
4727
4728   /* Set local variables from the partial symbol table info.  */
4729   offset = per_cu->offset;
4730
4731   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4732   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4733   beg_of_comp_unit = info_ptr;
4734
4735   if (per_cu->cu == NULL)
4736     {
4737       cu = xmalloc (sizeof (*cu));
4738       init_one_comp_unit (cu, objfile);
4739
4740       read_cu = 1;
4741
4742       /* If an error occurs while loading, release our storage.  */
4743       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4744
4745       /* Read in the comp_unit header.  */
4746       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4747
4748       /* Skip dummy compilation units.  */
4749       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4750                        + dwarf2_per_objfile->info.size)
4751           || peek_abbrev_code (abfd, info_ptr) == 0)
4752         {
4753           do_cleanups (free_cu_cleanup);
4754           return;
4755         }
4756
4757       /* Complete the cu_header.  */
4758       cu->header.offset = offset;
4759       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4760
4761       /* Read the abbrevs for this compilation unit.  */
4762       dwarf2_read_abbrevs (abfd, cu);
4763       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4764
4765       /* Link this compilation unit into the compilation unit tree.  */
4766       per_cu->cu = cu;
4767       cu->per_cu = per_cu;
4768
4769       /* Link this CU into read_in_chain.  */
4770       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4771       dwarf2_per_objfile->read_in_chain = per_cu;
4772     }
4773   else
4774     {
4775       cu = per_cu->cu;
4776       info_ptr += cu->header.first_die_offset;
4777     }
4778
4779   cu->dies = read_comp_unit (info_ptr, cu);
4780
4781   /* We try not to read any attributes in this function, because not
4782      all objfiles needed for references have been loaded yet, and symbol
4783      table processing isn't initialized.  But we have to set the CU language,
4784      or we won't be able to build types correctly.  */
4785   prepare_one_comp_unit (cu, cu->dies);
4786
4787   /* Similarly, if we do not read the producer, we can not apply
4788      producer-specific interpretation.  */
4789   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4790   if (attr)
4791     cu->producer = DW_STRING (attr);
4792
4793   if (read_cu)
4794     {
4795       do_cleanups (free_abbrevs_cleanup);
4796
4797       /* We've successfully allocated this compilation unit.  Let our
4798          caller clean it up when finished with it.  */
4799       discard_cleanups (free_cu_cleanup);
4800     }
4801 }
4802
4803 /* Add a DIE to the delayed physname list.  */
4804
4805 static void
4806 add_to_method_list (struct type *type, int fnfield_index, int index,
4807                     const char *name, struct die_info *die,
4808                     struct dwarf2_cu *cu)
4809 {
4810   struct delayed_method_info mi;
4811   mi.type = type;
4812   mi.fnfield_index = fnfield_index;
4813   mi.index = index;
4814   mi.name = name;
4815   mi.die = die;
4816   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4817 }
4818
4819 /* A cleanup for freeing the delayed method list.  */
4820
4821 static void
4822 free_delayed_list (void *ptr)
4823 {
4824   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4825   if (cu->method_list != NULL)
4826     {
4827       VEC_free (delayed_method_info, cu->method_list);
4828       cu->method_list = NULL;
4829     }
4830 }
4831
4832 /* Compute the physnames of any methods on the CU's method list.
4833
4834    The computation of method physnames is delayed in order to avoid the
4835    (bad) condition that one of the method's formal parameters is of an as yet
4836    incomplete type.  */
4837
4838 static void
4839 compute_delayed_physnames (struct dwarf2_cu *cu)
4840 {
4841   int i;
4842   struct delayed_method_info *mi;
4843   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4844     {
4845       const char *physname;
4846       struct fn_fieldlist *fn_flp
4847         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4848       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4849       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4850     }
4851 }
4852
4853 /* Generate full symbol information for PST and CU, whose DIEs have
4854    already been loaded into memory.  */
4855
4856 static void
4857 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4858 {
4859   struct dwarf2_cu *cu = per_cu->cu;
4860   struct objfile *objfile = per_cu->objfile;
4861   CORE_ADDR lowpc, highpc;
4862   struct symtab *symtab;
4863   struct cleanup *back_to, *delayed_list_cleanup;
4864   CORE_ADDR baseaddr;
4865
4866   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4867
4868   buildsym_init ();
4869   back_to = make_cleanup (really_free_pendings, NULL);
4870   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4871
4872   cu->list_in_scope = &file_symbols;
4873
4874   /* Do line number decoding in read_file_scope () */
4875   process_die (cu->dies, cu);
4876
4877   /* Now that we have processed all the DIEs in the CU, all the types 
4878      should be complete, and it should now be safe to compute all of the
4879      physnames.  */
4880   compute_delayed_physnames (cu);
4881   do_cleanups (delayed_list_cleanup);
4882
4883   /* Some compilers don't define a DW_AT_high_pc attribute for the
4884      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4885      it, by scanning the DIE's below the compilation unit.  */
4886   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4887
4888   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4889
4890   if (symtab != NULL)
4891     {
4892       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4893
4894       /* Set symtab language to language from DW_AT_language.  If the
4895          compilation is from a C file generated by language preprocessors, do
4896          not set the language if it was already deduced by start_subfile.  */
4897       if (!(cu->language == language_c && symtab->language != language_c))
4898         symtab->language = cu->language;
4899
4900       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4901          produce DW_AT_location with location lists but it can be possibly
4902          invalid without -fvar-tracking.
4903
4904          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4905          needed, it would be wrong due to missing DW_AT_producer there.
4906
4907          Still one can confuse GDB by using non-standard GCC compilation
4908          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4909          */ 
4910       if (cu->has_loclist && gcc_4_minor >= 0)
4911         symtab->locations_valid = 1;
4912
4913       if (gcc_4_minor >= 5)
4914         symtab->epilogue_unwind_valid = 1;
4915
4916       symtab->call_site_htab = cu->call_site_htab;
4917     }
4918
4919   if (dwarf2_per_objfile->using_index)
4920     per_cu->v.quick->symtab = symtab;
4921   else
4922     {
4923       struct partial_symtab *pst = per_cu->v.psymtab;
4924       pst->symtab = symtab;
4925       pst->readin = 1;
4926     }
4927
4928   do_cleanups (back_to);
4929 }
4930
4931 /* Process a die and its children.  */
4932
4933 static void
4934 process_die (struct die_info *die, struct dwarf2_cu *cu)
4935 {
4936   switch (die->tag)
4937     {
4938     case DW_TAG_padding:
4939       break;
4940     case DW_TAG_compile_unit:
4941       read_file_scope (die, cu);
4942       break;
4943     case DW_TAG_type_unit:
4944       read_type_unit_scope (die, cu);
4945       break;
4946     case DW_TAG_subprogram:
4947     case DW_TAG_inlined_subroutine:
4948       read_func_scope (die, cu);
4949       break;
4950     case DW_TAG_lexical_block:
4951     case DW_TAG_try_block:
4952     case DW_TAG_catch_block:
4953       read_lexical_block_scope (die, cu);
4954       break;
4955     case DW_TAG_GNU_call_site:
4956       read_call_site_scope (die, cu);
4957       break;
4958     case DW_TAG_class_type:
4959     case DW_TAG_interface_type:
4960     case DW_TAG_structure_type:
4961     case DW_TAG_union_type:
4962       process_structure_scope (die, cu);
4963       break;
4964     case DW_TAG_enumeration_type:
4965       process_enumeration_scope (die, cu);
4966       break;
4967
4968     /* These dies have a type, but processing them does not create
4969        a symbol or recurse to process the children.  Therefore we can
4970        read them on-demand through read_type_die.  */
4971     case DW_TAG_subroutine_type:
4972     case DW_TAG_set_type:
4973     case DW_TAG_array_type:
4974     case DW_TAG_pointer_type:
4975     case DW_TAG_ptr_to_member_type:
4976     case DW_TAG_reference_type:
4977     case DW_TAG_string_type:
4978       break;
4979
4980     case DW_TAG_base_type:
4981     case DW_TAG_subrange_type:
4982     case DW_TAG_typedef:
4983       /* Add a typedef symbol for the type definition, if it has a
4984          DW_AT_name.  */
4985       new_symbol (die, read_type_die (die, cu), cu);
4986       break;
4987     case DW_TAG_common_block:
4988       read_common_block (die, cu);
4989       break;
4990     case DW_TAG_common_inclusion:
4991       break;
4992     case DW_TAG_namespace:
4993       processing_has_namespace_info = 1;
4994       read_namespace (die, cu);
4995       break;
4996     case DW_TAG_module:
4997       processing_has_namespace_info = 1;
4998       read_module (die, cu);
4999       break;
5000     case DW_TAG_imported_declaration:
5001     case DW_TAG_imported_module:
5002       processing_has_namespace_info = 1;
5003       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5004                                  || cu->language != language_fortran))
5005         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5006                    dwarf_tag_name (die->tag));
5007       read_import_statement (die, cu);
5008       break;
5009     default:
5010       new_symbol (die, NULL, cu);
5011       break;
5012     }
5013 }
5014
5015 /* A helper function for dwarf2_compute_name which determines whether DIE
5016    needs to have the name of the scope prepended to the name listed in the
5017    die.  */
5018
5019 static int
5020 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5021 {
5022   struct attribute *attr;
5023
5024   switch (die->tag)
5025     {
5026     case DW_TAG_namespace:
5027     case DW_TAG_typedef:
5028     case DW_TAG_class_type:
5029     case DW_TAG_interface_type:
5030     case DW_TAG_structure_type:
5031     case DW_TAG_union_type:
5032     case DW_TAG_enumeration_type:
5033     case DW_TAG_enumerator:
5034     case DW_TAG_subprogram:
5035     case DW_TAG_member:
5036       return 1;
5037
5038     case DW_TAG_variable:
5039     case DW_TAG_constant:
5040       /* We only need to prefix "globally" visible variables.  These include
5041          any variable marked with DW_AT_external or any variable that
5042          lives in a namespace.  [Variables in anonymous namespaces
5043          require prefixing, but they are not DW_AT_external.]  */
5044
5045       if (dwarf2_attr (die, DW_AT_specification, cu))
5046         {
5047           struct dwarf2_cu *spec_cu = cu;
5048
5049           return die_needs_namespace (die_specification (die, &spec_cu),
5050                                       spec_cu);
5051         }
5052
5053       attr = dwarf2_attr (die, DW_AT_external, cu);
5054       if (attr == NULL && die->parent->tag != DW_TAG_namespace
5055           && die->parent->tag != DW_TAG_module)
5056         return 0;
5057       /* A variable in a lexical block of some kind does not need a
5058          namespace, even though in C++ such variables may be external
5059          and have a mangled name.  */
5060       if (die->parent->tag ==  DW_TAG_lexical_block
5061           || die->parent->tag ==  DW_TAG_try_block
5062           || die->parent->tag ==  DW_TAG_catch_block
5063           || die->parent->tag == DW_TAG_subprogram)
5064         return 0;
5065       return 1;
5066
5067     default:
5068       return 0;
5069     }
5070 }
5071
5072 /* Retrieve the last character from a mem_file.  */
5073
5074 static void
5075 do_ui_file_peek_last (void *object, const char *buffer, long length)
5076 {
5077   char *last_char_p = (char *) object;
5078
5079   if (length > 0)
5080     *last_char_p = buffer[length - 1];
5081 }
5082
5083 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
5084    compute the physname for the object, which include a method's
5085    formal parameters (C++/Java) and return type (Java).
5086
5087    For Ada, return the DIE's linkage name rather than the fully qualified
5088    name.  PHYSNAME is ignored..
5089
5090    The result is allocated on the objfile_obstack and canonicalized.  */
5091
5092 static const char *
5093 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5094                      int physname)
5095 {
5096   if (name == NULL)
5097     name = dwarf2_name (die, cu);
5098
5099   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5100      compute it by typename_concat inside GDB.  */
5101   if (cu->language == language_ada
5102       || (cu->language == language_fortran && physname))
5103     {
5104       /* For Ada unit, we prefer the linkage name over the name, as
5105          the former contains the exported name, which the user expects
5106          to be able to reference.  Ideally, we want the user to be able
5107          to reference this entity using either natural or linkage name,
5108          but we haven't started looking at this enhancement yet.  */
5109       struct attribute *attr;
5110
5111       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5112       if (attr == NULL)
5113         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5114       if (attr && DW_STRING (attr))
5115         return DW_STRING (attr);
5116     }
5117
5118   /* These are the only languages we know how to qualify names in.  */
5119   if (name != NULL
5120       && (cu->language == language_cplus || cu->language == language_java
5121           || cu->language == language_fortran))
5122     {
5123       if (die_needs_namespace (die, cu))
5124         {
5125           long length;
5126           char *prefix;
5127           struct ui_file *buf;
5128
5129           prefix = determine_prefix (die, cu);
5130           buf = mem_fileopen ();
5131           if (*prefix != '\0')
5132             {
5133               char *prefixed_name = typename_concat (NULL, prefix, name,
5134                                                      physname, cu);
5135
5136               fputs_unfiltered (prefixed_name, buf);
5137               xfree (prefixed_name);
5138             }
5139           else
5140             fputs_unfiltered (name, buf);
5141
5142           /* Template parameters may be specified in the DIE's DW_AT_name, or
5143              as children with DW_TAG_template_type_param or
5144              DW_TAG_value_type_param.  If the latter, add them to the name
5145              here.  If the name already has template parameters, then
5146              skip this step; some versions of GCC emit both, and
5147              it is more efficient to use the pre-computed name.
5148
5149              Something to keep in mind about this process: it is very
5150              unlikely, or in some cases downright impossible, to produce
5151              something that will match the mangled name of a function.
5152              If the definition of the function has the same debug info,
5153              we should be able to match up with it anyway.  But fallbacks
5154              using the minimal symbol, for instance to find a method
5155              implemented in a stripped copy of libstdc++, will not work.
5156              If we do not have debug info for the definition, we will have to
5157              match them up some other way.
5158
5159              When we do name matching there is a related problem with function
5160              templates; two instantiated function templates are allowed to
5161              differ only by their return types, which we do not add here.  */
5162
5163           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5164             {
5165               struct attribute *attr;
5166               struct die_info *child;
5167               int first = 1;
5168
5169               die->building_fullname = 1;
5170
5171               for (child = die->child; child != NULL; child = child->sibling)
5172                 {
5173                   struct type *type;
5174                   long value;
5175                   gdb_byte *bytes;
5176                   struct dwarf2_locexpr_baton *baton;
5177                   struct value *v;
5178
5179                   if (child->tag != DW_TAG_template_type_param
5180                       && child->tag != DW_TAG_template_value_param)
5181                     continue;
5182
5183                   if (first)
5184                     {
5185                       fputs_unfiltered ("<", buf);
5186                       first = 0;
5187                     }
5188                   else
5189                     fputs_unfiltered (", ", buf);
5190
5191                   attr = dwarf2_attr (child, DW_AT_type, cu);
5192                   if (attr == NULL)
5193                     {
5194                       complaint (&symfile_complaints,
5195                                  _("template parameter missing DW_AT_type"));
5196                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5197                       continue;
5198                     }
5199                   type = die_type (child, cu);
5200
5201                   if (child->tag == DW_TAG_template_type_param)
5202                     {
5203                       c_print_type (type, "", buf, -1, 0);
5204                       continue;
5205                     }
5206
5207                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5208                   if (attr == NULL)
5209                     {
5210                       complaint (&symfile_complaints,
5211                                  _("template parameter missing "
5212                                    "DW_AT_const_value"));
5213                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5214                       continue;
5215                     }
5216
5217                   dwarf2_const_value_attr (attr, type, name,
5218                                            &cu->comp_unit_obstack, cu,
5219                                            &value, &bytes, &baton);
5220
5221                   if (TYPE_NOSIGN (type))
5222                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5223                        changed, this can use value_print instead.  */
5224                     c_printchar (value, type, buf);
5225                   else
5226                     {
5227                       struct value_print_options opts;
5228
5229                       if (baton != NULL)
5230                         v = dwarf2_evaluate_loc_desc (type, NULL,
5231                                                       baton->data,
5232                                                       baton->size,
5233                                                       baton->per_cu);
5234                       else if (bytes != NULL)
5235                         {
5236                           v = allocate_value (type);
5237                           memcpy (value_contents_writeable (v), bytes,
5238                                   TYPE_LENGTH (type));
5239                         }
5240                       else
5241                         v = value_from_longest (type, value);
5242
5243                       /* Specify decimal so that we do not depend on
5244                          the radix.  */
5245                       get_formatted_print_options (&opts, 'd');
5246                       opts.raw = 1;
5247                       value_print (v, buf, &opts);
5248                       release_value (v);
5249                       value_free (v);
5250                     }
5251                 }
5252
5253               die->building_fullname = 0;
5254
5255               if (!first)
5256                 {
5257                   /* Close the argument list, with a space if necessary
5258                      (nested templates).  */
5259                   char last_char = '\0';
5260                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5261                   if (last_char == '>')
5262                     fputs_unfiltered (" >", buf);
5263                   else
5264                     fputs_unfiltered (">", buf);
5265                 }
5266             }
5267
5268           /* For Java and C++ methods, append formal parameter type
5269              information, if PHYSNAME.  */
5270
5271           if (physname && die->tag == DW_TAG_subprogram
5272               && (cu->language == language_cplus
5273                   || cu->language == language_java))
5274             {
5275               struct type *type = read_type_die (die, cu);
5276
5277               c_type_print_args (type, buf, 1, cu->language);
5278
5279               if (cu->language == language_java)
5280                 {
5281                   /* For java, we must append the return type to method
5282                      names.  */
5283                   if (die->tag == DW_TAG_subprogram)
5284                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5285                                      0, 0);
5286                 }
5287               else if (cu->language == language_cplus)
5288                 {
5289                   /* Assume that an artificial first parameter is
5290                      "this", but do not crash if it is not.  RealView
5291                      marks unnamed (and thus unused) parameters as
5292                      artificial; there is no way to differentiate
5293                      the two cases.  */
5294                   if (TYPE_NFIELDS (type) > 0
5295                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5296                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5297                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5298                                                                         0))))
5299                     fputs_unfiltered (" const", buf);
5300                 }
5301             }
5302
5303           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5304                                        &length);
5305           ui_file_delete (buf);
5306
5307           if (cu->language == language_cplus)
5308             {
5309               char *cname
5310                 = dwarf2_canonicalize_name (name, cu,
5311                                             &cu->objfile->objfile_obstack);
5312
5313               if (cname != NULL)
5314                 name = cname;
5315             }
5316         }
5317     }
5318
5319   return name;
5320 }
5321
5322 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5323    If scope qualifiers are appropriate they will be added.  The result
5324    will be allocated on the objfile_obstack, or NULL if the DIE does
5325    not have a name.  NAME may either be from a previous call to
5326    dwarf2_name or NULL.
5327
5328    The output string will be canonicalized (if C++/Java).  */
5329
5330 static const char *
5331 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5332 {
5333   return dwarf2_compute_name (name, die, cu, 0);
5334 }
5335
5336 /* Construct a physname for the given DIE in CU.  NAME may either be
5337    from a previous call to dwarf2_name or NULL.  The result will be
5338    allocated on the objfile_objstack or NULL if the DIE does not have a
5339    name.
5340
5341    The output string will be canonicalized (if C++/Java).  */
5342
5343 static const char *
5344 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5345 {
5346   struct attribute *attr;
5347   const char *retval, *mangled = NULL, *canon = NULL;
5348   struct cleanup *back_to;
5349   int need_copy = 1;
5350
5351   /* In this case dwarf2_compute_name is just a shortcut not building anything
5352      on its own.  */
5353   if (!die_needs_namespace (die, cu))
5354     return dwarf2_compute_name (name, die, cu, 1);
5355
5356   back_to = make_cleanup (null_cleanup, NULL);
5357
5358   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5359   if (!attr)
5360     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5361
5362   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5363      has computed.  */
5364   if (attr && DW_STRING (attr))
5365     {
5366       char *demangled;
5367
5368       mangled = DW_STRING (attr);
5369
5370       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5371          type.  It is easier for GDB users to search for such functions as
5372          `name(params)' than `long name(params)'.  In such case the minimal
5373          symbol names do not match the full symbol names but for template
5374          functions there is never a need to look up their definition from their
5375          declaration so the only disadvantage remains the minimal symbol
5376          variant `long name(params)' does not have the proper inferior type.
5377          */
5378
5379       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5380                                             | (cu->language == language_java
5381                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5382                                                : DMGL_RET_DROP)));
5383       if (demangled)
5384         {
5385           make_cleanup (xfree, demangled);
5386           canon = demangled;
5387         }
5388       else
5389         {
5390           canon = mangled;
5391           need_copy = 0;
5392         }
5393     }
5394
5395   if (canon == NULL || check_physname)
5396     {
5397       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5398
5399       if (canon != NULL && strcmp (physname, canon) != 0)
5400         {
5401           /* It may not mean a bug in GDB.  The compiler could also
5402              compute DW_AT_linkage_name incorrectly.  But in such case
5403              GDB would need to be bug-to-bug compatible.  */
5404
5405           complaint (&symfile_complaints,
5406                      _("Computed physname <%s> does not match demangled <%s> "
5407                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5408                      physname, canon, mangled, die->offset, cu->objfile->name);
5409
5410           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5411              is available here - over computed PHYSNAME.  It is safer
5412              against both buggy GDB and buggy compilers.  */
5413
5414           retval = canon;
5415         }
5416       else
5417         {
5418           retval = physname;
5419           need_copy = 0;
5420         }
5421     }
5422   else
5423     retval = canon;
5424
5425   if (need_copy)
5426     retval = obsavestring (retval, strlen (retval),
5427                            &cu->objfile->objfile_obstack);
5428
5429   do_cleanups (back_to);
5430   return retval;
5431 }
5432
5433 /* Read the import statement specified by the given die and record it.  */
5434
5435 static void
5436 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5437 {
5438   struct attribute *import_attr;
5439   struct die_info *imported_die, *child_die;
5440   struct dwarf2_cu *imported_cu;
5441   const char *imported_name;
5442   const char *imported_name_prefix;
5443   const char *canonical_name;
5444   const char *import_alias;
5445   const char *imported_declaration = NULL;
5446   const char *import_prefix;
5447   VEC (const_char_ptr) *excludes = NULL;
5448   struct cleanup *cleanups;
5449
5450   char *temp;
5451
5452   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5453   if (import_attr == NULL)
5454     {
5455       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5456                  dwarf_tag_name (die->tag));
5457       return;
5458     }
5459
5460   imported_cu = cu;
5461   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5462   imported_name = dwarf2_name (imported_die, imported_cu);
5463   if (imported_name == NULL)
5464     {
5465       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5466
5467         The import in the following code:
5468         namespace A
5469           {
5470             typedef int B;
5471           }
5472
5473         int main ()
5474           {
5475             using A::B;
5476             B b;
5477             return b;
5478           }
5479
5480         ...
5481          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5482             <52>   DW_AT_decl_file   : 1
5483             <53>   DW_AT_decl_line   : 6
5484             <54>   DW_AT_import      : <0x75>
5485          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5486             <59>   DW_AT_name        : B
5487             <5b>   DW_AT_decl_file   : 1
5488             <5c>   DW_AT_decl_line   : 2
5489             <5d>   DW_AT_type        : <0x6e>
5490         ...
5491          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5492             <76>   DW_AT_byte_size   : 4
5493             <77>   DW_AT_encoding    : 5        (signed)
5494
5495         imports the wrong die ( 0x75 instead of 0x58 ).
5496         This case will be ignored until the gcc bug is fixed.  */
5497       return;
5498     }
5499
5500   /* Figure out the local name after import.  */
5501   import_alias = dwarf2_name (die, cu);
5502
5503   /* Figure out where the statement is being imported to.  */
5504   import_prefix = determine_prefix (die, cu);
5505
5506   /* Figure out what the scope of the imported die is and prepend it
5507      to the name of the imported die.  */
5508   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5509
5510   if (imported_die->tag != DW_TAG_namespace
5511       && imported_die->tag != DW_TAG_module)
5512     {
5513       imported_declaration = imported_name;
5514       canonical_name = imported_name_prefix;
5515     }
5516   else if (strlen (imported_name_prefix) > 0)
5517     {
5518       temp = alloca (strlen (imported_name_prefix)
5519                      + 2 + strlen (imported_name) + 1);
5520       strcpy (temp, imported_name_prefix);
5521       strcat (temp, "::");
5522       strcat (temp, imported_name);
5523       canonical_name = temp;
5524     }
5525   else
5526     canonical_name = imported_name;
5527
5528   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5529
5530   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5531     for (child_die = die->child; child_die && child_die->tag;
5532          child_die = sibling_die (child_die))
5533       {
5534         /* DWARF-4: A Fortran use statement with a “rename list” may be
5535            represented by an imported module entry with an import attribute
5536            referring to the module and owned entries corresponding to those
5537            entities that are renamed as part of being imported.  */
5538
5539         if (child_die->tag != DW_TAG_imported_declaration)
5540           {
5541             complaint (&symfile_complaints,
5542                        _("child DW_TAG_imported_declaration expected "
5543                          "- DIE at 0x%x [in module %s]"),
5544                        child_die->offset, cu->objfile->name);
5545             continue;
5546           }
5547
5548         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5549         if (import_attr == NULL)
5550           {
5551             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5552                        dwarf_tag_name (child_die->tag));
5553             continue;
5554           }
5555
5556         imported_cu = cu;
5557         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5558                                               &imported_cu);
5559         imported_name = dwarf2_name (imported_die, imported_cu);
5560         if (imported_name == NULL)
5561           {
5562             complaint (&symfile_complaints,
5563                        _("child DW_TAG_imported_declaration has unknown "
5564                          "imported name - DIE at 0x%x [in module %s]"),
5565                        child_die->offset, cu->objfile->name);
5566             continue;
5567           }
5568
5569         VEC_safe_push (const_char_ptr, excludes, imported_name);
5570
5571         process_die (child_die, cu);
5572       }
5573
5574   cp_add_using_directive (import_prefix,
5575                           canonical_name,
5576                           import_alias,
5577                           imported_declaration,
5578                           excludes,
5579                           &cu->objfile->objfile_obstack);
5580
5581   do_cleanups (cleanups);
5582 }
5583
5584 static void
5585 initialize_cu_func_list (struct dwarf2_cu *cu)
5586 {
5587   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5588 }
5589
5590 /* Cleanup function for read_file_scope.  */
5591
5592 static void
5593 free_cu_line_header (void *arg)
5594 {
5595   struct dwarf2_cu *cu = arg;
5596
5597   free_line_header (cu->line_header);
5598   cu->line_header = NULL;
5599 }
5600
5601 static void
5602 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5603                          char **name, char **comp_dir)
5604 {
5605   struct attribute *attr;
5606
5607   *name = NULL;
5608   *comp_dir = NULL;
5609
5610   /* Find the filename.  Do not use dwarf2_name here, since the filename
5611      is not a source language identifier.  */
5612   attr = dwarf2_attr (die, DW_AT_name, cu);
5613   if (attr)
5614     {
5615       *name = DW_STRING (attr);
5616     }
5617
5618   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5619   if (attr)
5620     *comp_dir = DW_STRING (attr);
5621   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5622     {
5623       *comp_dir = ldirname (*name);
5624       if (*comp_dir != NULL)
5625         make_cleanup (xfree, *comp_dir);
5626     }
5627   if (*comp_dir != NULL)
5628     {
5629       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5630          directory, get rid of it.  */
5631       char *cp = strchr (*comp_dir, ':');
5632
5633       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5634         *comp_dir = cp + 1;
5635     }
5636
5637   if (*name == NULL)
5638     *name = "<unknown>";
5639 }
5640
5641 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5642    DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5643    COMP_DIR is the compilation directory.
5644    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
5645
5646 static void
5647 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5648                         const char *comp_dir, int want_line_info)
5649 {
5650   struct attribute *attr;
5651   struct objfile *objfile = cu->objfile;
5652   bfd *abfd = objfile->obfd;
5653
5654   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5655   if (attr)
5656     {
5657       unsigned int line_offset = DW_UNSND (attr);
5658       struct line_header *line_header
5659         = dwarf_decode_line_header (line_offset, abfd, cu);
5660
5661       if (line_header)
5662         {
5663           cu->line_header = line_header;
5664           make_cleanup (free_cu_line_header, cu);
5665           dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5666         }
5667     }
5668 }
5669
5670 /* Process DW_TAG_compile_unit.  */
5671
5672 static void
5673 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5674 {
5675   struct objfile *objfile = cu->objfile;
5676   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5677   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5678   CORE_ADDR highpc = ((CORE_ADDR) 0);
5679   struct attribute *attr;
5680   char *name = NULL;
5681   char *comp_dir = NULL;
5682   struct die_info *child_die;
5683   bfd *abfd = objfile->obfd;
5684   CORE_ADDR baseaddr;
5685
5686   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5687
5688   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5689
5690   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5691      from finish_block.  */
5692   if (lowpc == ((CORE_ADDR) -1))
5693     lowpc = highpc;
5694   lowpc += baseaddr;
5695   highpc += baseaddr;
5696
5697   find_file_and_directory (die, cu, &name, &comp_dir);
5698
5699   attr = dwarf2_attr (die, DW_AT_language, cu);
5700   if (attr)
5701     {
5702       set_cu_language (DW_UNSND (attr), cu);
5703     }
5704
5705   attr = dwarf2_attr (die, DW_AT_producer, cu);
5706   if (attr)
5707     cu->producer = DW_STRING (attr);
5708
5709   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5710      standardised yet.  As a workaround for the language detection we fall
5711      back to the DW_AT_producer string.  */
5712   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5713     cu->language = language_opencl;
5714
5715   /* We assume that we're processing GCC output.  */
5716   processing_gcc_compilation = 2;
5717
5718   processing_has_namespace_info = 0;
5719
5720   start_symtab (name, comp_dir, lowpc);
5721   record_debugformat ("DWARF 2");
5722   record_producer (cu->producer);
5723
5724   initialize_cu_func_list (cu);
5725
5726   /* Decode line number information if present.  We do this before
5727      processing child DIEs, so that the line header table is available
5728      for DW_AT_decl_file.  */
5729   handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5730
5731   /* Process all dies in compilation unit.  */
5732   if (die->child != NULL)
5733     {
5734       child_die = die->child;
5735       while (child_die && child_die->tag)
5736         {
5737           process_die (child_die, cu);
5738           child_die = sibling_die (child_die);
5739         }
5740     }
5741
5742   /* Decode macro information, if present.  Dwarf 2 macro information
5743      refers to information in the line number info statement program
5744      header, so we can only read it if we've read the header
5745      successfully.  */
5746   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5747   if (attr && cu->line_header)
5748     {
5749       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5750         complaint (&symfile_complaints,
5751                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5752
5753       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5754                            comp_dir, abfd, cu,
5755                            &dwarf2_per_objfile->macro, 1);
5756     }
5757   else
5758     {
5759       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5760       if (attr && cu->line_header)
5761         {
5762           unsigned int macro_offset = DW_UNSND (attr);
5763
5764           dwarf_decode_macros (cu->line_header, macro_offset,
5765                                comp_dir, abfd, cu,
5766                                &dwarf2_per_objfile->macinfo, 0);
5767         }
5768     }
5769   do_cleanups (back_to);
5770 }
5771
5772 /* Process DW_TAG_type_unit.
5773    For TUs we want to skip the first top level sibling if it's not the
5774    actual type being defined by this TU.  In this case the first top
5775    level sibling is there to provide context only.  */
5776
5777 static void
5778 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5779 {
5780   struct objfile *objfile = cu->objfile;
5781   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5782   CORE_ADDR lowpc;
5783   struct attribute *attr;
5784   char *name = NULL;
5785   char *comp_dir = NULL;
5786   struct die_info *child_die;
5787   bfd *abfd = objfile->obfd;
5788
5789   /* start_symtab needs a low pc, but we don't really have one.
5790      Do what read_file_scope would do in the absence of such info.  */
5791   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5792
5793   /* Find the filename.  Do not use dwarf2_name here, since the filename
5794      is not a source language identifier.  */
5795   attr = dwarf2_attr (die, DW_AT_name, cu);
5796   if (attr)
5797     name = DW_STRING (attr);
5798
5799   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5800   if (attr)
5801     comp_dir = DW_STRING (attr);
5802   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5803     {
5804       comp_dir = ldirname (name);
5805       if (comp_dir != NULL)
5806         make_cleanup (xfree, comp_dir);
5807     }
5808
5809   if (name == NULL)
5810     name = "<unknown>";
5811
5812   attr = dwarf2_attr (die, DW_AT_language, cu);
5813   if (attr)
5814     set_cu_language (DW_UNSND (attr), cu);
5815
5816   /* This isn't technically needed today.  It is done for symmetry
5817      with read_file_scope.  */
5818   attr = dwarf2_attr (die, DW_AT_producer, cu);
5819   if (attr)
5820     cu->producer = DW_STRING (attr);
5821
5822   /* We assume that we're processing GCC output.  */
5823   processing_gcc_compilation = 2;
5824
5825   processing_has_namespace_info = 0;
5826
5827   start_symtab (name, comp_dir, lowpc);
5828   record_debugformat ("DWARF 2");
5829   record_producer (cu->producer);
5830
5831   /* Decode line number information if present.  We do this before
5832      processing child DIEs, so that the line header table is available
5833      for DW_AT_decl_file.
5834      We don't need the pc/line-number mapping for type units.  */
5835   handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5836
5837   /* Process the dies in the type unit.  */
5838   if (die->child == NULL)
5839     {
5840       dump_die_for_error (die);
5841       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5842              bfd_get_filename (abfd));
5843     }
5844
5845   child_die = die->child;
5846
5847   while (child_die && child_die->tag)
5848     {
5849       process_die (child_die, cu);
5850
5851       child_die = sibling_die (child_die);
5852     }
5853
5854   do_cleanups (back_to);
5855 }
5856
5857 static void
5858 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5859                      struct dwarf2_cu *cu)
5860 {
5861   struct function_range *thisfn;
5862
5863   thisfn = (struct function_range *)
5864     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5865   thisfn->name = name;
5866   thisfn->lowpc = lowpc;
5867   thisfn->highpc = highpc;
5868   thisfn->seen_line = 0;
5869   thisfn->next = NULL;
5870
5871   if (cu->last_fn == NULL)
5872       cu->first_fn = thisfn;
5873   else
5874       cu->last_fn->next = thisfn;
5875
5876   cu->last_fn = thisfn;
5877 }
5878
5879 /* qsort helper for inherit_abstract_dies.  */
5880
5881 static int
5882 unsigned_int_compar (const void *ap, const void *bp)
5883 {
5884   unsigned int a = *(unsigned int *) ap;
5885   unsigned int b = *(unsigned int *) bp;
5886
5887   return (a > b) - (b > a);
5888 }
5889
5890 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5891    Inherit only the children of the DW_AT_abstract_origin DIE not being
5892    already referenced by DW_AT_abstract_origin from the children of the
5893    current DIE.  */
5894
5895 static void
5896 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5897 {
5898   struct die_info *child_die;
5899   unsigned die_children_count;
5900   /* CU offsets which were referenced by children of the current DIE.  */
5901   unsigned *offsets;
5902   unsigned *offsets_end, *offsetp;
5903   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5904   struct die_info *origin_die;
5905   /* Iterator of the ORIGIN_DIE children.  */
5906   struct die_info *origin_child_die;
5907   struct cleanup *cleanups;
5908   struct attribute *attr;
5909   struct dwarf2_cu *origin_cu;
5910   struct pending **origin_previous_list_in_scope;
5911
5912   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5913   if (!attr)
5914     return;
5915
5916   /* Note that following die references may follow to a die in a
5917      different cu.  */
5918
5919   origin_cu = cu;
5920   origin_die = follow_die_ref (die, attr, &origin_cu);
5921
5922   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5923      symbols in.  */
5924   origin_previous_list_in_scope = origin_cu->list_in_scope;
5925   origin_cu->list_in_scope = cu->list_in_scope;
5926
5927   if (die->tag != origin_die->tag
5928       && !(die->tag == DW_TAG_inlined_subroutine
5929            && origin_die->tag == DW_TAG_subprogram))
5930     complaint (&symfile_complaints,
5931                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5932                die->offset, origin_die->offset);
5933
5934   child_die = die->child;
5935   die_children_count = 0;
5936   while (child_die && child_die->tag)
5937     {
5938       child_die = sibling_die (child_die);
5939       die_children_count++;
5940     }
5941   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5942   cleanups = make_cleanup (xfree, offsets);
5943
5944   offsets_end = offsets;
5945   child_die = die->child;
5946   while (child_die && child_die->tag)
5947     {
5948       /* For each CHILD_DIE, find the corresponding child of
5949          ORIGIN_DIE.  If there is more than one layer of
5950          DW_AT_abstract_origin, follow them all; there shouldn't be,
5951          but GCC versions at least through 4.4 generate this (GCC PR
5952          40573).  */
5953       struct die_info *child_origin_die = child_die;
5954       struct dwarf2_cu *child_origin_cu = cu;
5955
5956       while (1)
5957         {
5958           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5959                               child_origin_cu);
5960           if (attr == NULL)
5961             break;
5962           child_origin_die = follow_die_ref (child_origin_die, attr,
5963                                              &child_origin_cu);
5964         }
5965
5966       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5967          counterpart may exist.  */
5968       if (child_origin_die != child_die)
5969         {
5970           if (child_die->tag != child_origin_die->tag
5971               && !(child_die->tag == DW_TAG_inlined_subroutine
5972                    && child_origin_die->tag == DW_TAG_subprogram))
5973             complaint (&symfile_complaints,
5974                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5975                          "different tags"), child_die->offset,
5976                        child_origin_die->offset);
5977           if (child_origin_die->parent != origin_die)
5978             complaint (&symfile_complaints,
5979                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5980                          "different parents"), child_die->offset,
5981                        child_origin_die->offset);
5982           else
5983             *offsets_end++ = child_origin_die->offset;
5984         }
5985       child_die = sibling_die (child_die);
5986     }
5987   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5988          unsigned_int_compar);
5989   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5990     if (offsetp[-1] == *offsetp)
5991       complaint (&symfile_complaints,
5992                  _("Multiple children of DIE 0x%x refer "
5993                    "to DIE 0x%x as their abstract origin"),
5994                  die->offset, *offsetp);
5995
5996   offsetp = offsets;
5997   origin_child_die = origin_die->child;
5998   while (origin_child_die && origin_child_die->tag)
5999     {
6000       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
6001       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
6002         offsetp++;
6003       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
6004         {
6005           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
6006           process_die (origin_child_die, origin_cu);
6007         }
6008       origin_child_die = sibling_die (origin_child_die);
6009     }
6010   origin_cu->list_in_scope = origin_previous_list_in_scope;
6011
6012   do_cleanups (cleanups);
6013 }
6014
6015 static void
6016 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
6017 {
6018   struct objfile *objfile = cu->objfile;
6019   struct context_stack *new;
6020   CORE_ADDR lowpc;
6021   CORE_ADDR highpc;
6022   struct die_info *child_die;
6023   struct attribute *attr, *call_line, *call_file;
6024   char *name;
6025   CORE_ADDR baseaddr;
6026   struct block *block;
6027   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
6028   VEC (symbolp) *template_args = NULL;
6029   struct template_symbol *templ_func = NULL;
6030
6031   if (inlined_func)
6032     {
6033       /* If we do not have call site information, we can't show the
6034          caller of this inlined function.  That's too confusing, so
6035          only use the scope for local variables.  */
6036       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
6037       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
6038       if (call_line == NULL || call_file == NULL)
6039         {
6040           read_lexical_block_scope (die, cu);
6041           return;
6042         }
6043     }
6044
6045   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6046
6047   name = dwarf2_name (die, cu);
6048
6049   /* Ignore functions with missing or empty names.  These are actually
6050      illegal according to the DWARF standard.  */
6051   if (name == NULL)
6052     {
6053       complaint (&symfile_complaints,
6054                  _("missing name for subprogram DIE at %d"), die->offset);
6055       return;
6056     }
6057
6058   /* Ignore functions with missing or invalid low and high pc attributes.  */
6059   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6060     {
6061       attr = dwarf2_attr (die, DW_AT_external, cu);
6062       if (!attr || !DW_UNSND (attr))
6063         complaint (&symfile_complaints,
6064                    _("cannot get low and high bounds "
6065                      "for subprogram DIE at %d"),
6066                    die->offset);
6067       return;
6068     }
6069
6070   lowpc += baseaddr;
6071   highpc += baseaddr;
6072
6073   /* Record the function range for dwarf_decode_lines.  */
6074   add_to_cu_func_list (name, lowpc, highpc, cu);
6075
6076   /* If we have any template arguments, then we must allocate a
6077      different sort of symbol.  */
6078   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6079     {
6080       if (child_die->tag == DW_TAG_template_type_param
6081           || child_die->tag == DW_TAG_template_value_param)
6082         {
6083           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6084                                        struct template_symbol);
6085           templ_func->base.is_cplus_template_function = 1;
6086           break;
6087         }
6088     }
6089
6090   new = push_context (0, lowpc);
6091   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6092                                (struct symbol *) templ_func);
6093
6094   /* If there is a location expression for DW_AT_frame_base, record
6095      it.  */
6096   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6097   if (attr)
6098     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6099        expression is being recorded directly in the function's symbol
6100        and not in a separate frame-base object.  I guess this hack is
6101        to avoid adding some sort of frame-base adjunct/annex to the
6102        function's symbol :-(.  The problem with doing this is that it
6103        results in a function symbol with a location expression that
6104        has nothing to do with the location of the function, ouch!  The
6105        relationship should be: a function's symbol has-a frame base; a
6106        frame-base has-a location expression.  */
6107     dwarf2_symbol_mark_computed (attr, new->name, cu);
6108
6109   cu->list_in_scope = &local_symbols;
6110
6111   if (die->child != NULL)
6112     {
6113       child_die = die->child;
6114       while (child_die && child_die->tag)
6115         {
6116           if (child_die->tag == DW_TAG_template_type_param
6117               || child_die->tag == DW_TAG_template_value_param)
6118             {
6119               struct symbol *arg = new_symbol (child_die, NULL, cu);
6120
6121               if (arg != NULL)
6122                 VEC_safe_push (symbolp, template_args, arg);
6123             }
6124           else
6125             process_die (child_die, cu);
6126           child_die = sibling_die (child_die);
6127         }
6128     }
6129
6130   inherit_abstract_dies (die, cu);
6131
6132   /* If we have a DW_AT_specification, we might need to import using
6133      directives from the context of the specification DIE.  See the
6134      comment in determine_prefix.  */
6135   if (cu->language == language_cplus
6136       && dwarf2_attr (die, DW_AT_specification, cu))
6137     {
6138       struct dwarf2_cu *spec_cu = cu;
6139       struct die_info *spec_die = die_specification (die, &spec_cu);
6140
6141       while (spec_die)
6142         {
6143           child_die = spec_die->child;
6144           while (child_die && child_die->tag)
6145             {
6146               if (child_die->tag == DW_TAG_imported_module)
6147                 process_die (child_die, spec_cu);
6148               child_die = sibling_die (child_die);
6149             }
6150
6151           /* In some cases, GCC generates specification DIEs that
6152              themselves contain DW_AT_specification attributes.  */
6153           spec_die = die_specification (spec_die, &spec_cu);
6154         }
6155     }
6156
6157   new = pop_context ();
6158   /* Make a block for the local symbols within.  */
6159   block = finish_block (new->name, &local_symbols, new->old_blocks,
6160                         lowpc, highpc, objfile);
6161
6162   /* For C++, set the block's scope.  */
6163   if (cu->language == language_cplus || cu->language == language_fortran)
6164     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6165                         determine_prefix (die, cu),
6166                         processing_has_namespace_info);
6167
6168   /* If we have address ranges, record them.  */
6169   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6170
6171   /* Attach template arguments to function.  */
6172   if (! VEC_empty (symbolp, template_args))
6173     {
6174       gdb_assert (templ_func != NULL);
6175
6176       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6177       templ_func->template_arguments
6178         = obstack_alloc (&objfile->objfile_obstack,
6179                          (templ_func->n_template_arguments
6180                           * sizeof (struct symbol *)));
6181       memcpy (templ_func->template_arguments,
6182               VEC_address (symbolp, template_args),
6183               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6184       VEC_free (symbolp, template_args);
6185     }
6186
6187   /* In C++, we can have functions nested inside functions (e.g., when
6188      a function declares a class that has methods).  This means that
6189      when we finish processing a function scope, we may need to go
6190      back to building a containing block's symbol lists.  */
6191   local_symbols = new->locals;
6192   param_symbols = new->params;
6193   using_directives = new->using_directives;
6194
6195   /* If we've finished processing a top-level function, subsequent
6196      symbols go in the file symbol list.  */
6197   if (outermost_context_p ())
6198     cu->list_in_scope = &file_symbols;
6199 }
6200
6201 /* Process all the DIES contained within a lexical block scope.  Start
6202    a new scope, process the dies, and then close the scope.  */
6203
6204 static void
6205 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6206 {
6207   struct objfile *objfile = cu->objfile;
6208   struct context_stack *new;
6209   CORE_ADDR lowpc, highpc;
6210   struct die_info *child_die;
6211   CORE_ADDR baseaddr;
6212
6213   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6214
6215   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6216   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6217      as multiple lexical blocks?  Handling children in a sane way would
6218      be nasty.  Might be easier to properly extend generic blocks to
6219      describe ranges.  */
6220   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6221     return;
6222   lowpc += baseaddr;
6223   highpc += baseaddr;
6224
6225   push_context (0, lowpc);
6226   if (die->child != NULL)
6227     {
6228       child_die = die->child;
6229       while (child_die && child_die->tag)
6230         {
6231           process_die (child_die, cu);
6232           child_die = sibling_die (child_die);
6233         }
6234     }
6235   new = pop_context ();
6236
6237   if (local_symbols != NULL || using_directives != NULL)
6238     {
6239       struct block *block
6240         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6241                         highpc, objfile);
6242
6243       /* Note that recording ranges after traversing children, as we
6244          do here, means that recording a parent's ranges entails
6245          walking across all its children's ranges as they appear in
6246          the address map, which is quadratic behavior.
6247
6248          It would be nicer to record the parent's ranges before
6249          traversing its children, simply overriding whatever you find
6250          there.  But since we don't even decide whether to create a
6251          block until after we've traversed its children, that's hard
6252          to do.  */
6253       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6254     }
6255   local_symbols = new->locals;
6256   using_directives = new->using_directives;
6257 }
6258
6259 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6260
6261 static void
6262 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6263 {
6264   struct objfile *objfile = cu->objfile;
6265   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6266   CORE_ADDR pc, baseaddr;
6267   struct attribute *attr;
6268   struct call_site *call_site, call_site_local;
6269   void **slot;
6270   int nparams;
6271   struct die_info *child_die;
6272
6273   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6274
6275   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6276   if (!attr)
6277     {
6278       complaint (&symfile_complaints,
6279                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6280                    "DIE 0x%x [in module %s]"),
6281                  die->offset, cu->objfile->name);
6282       return;
6283     }
6284   pc = DW_ADDR (attr) + baseaddr;
6285
6286   if (cu->call_site_htab == NULL)
6287     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6288                                                NULL, &objfile->objfile_obstack,
6289                                                hashtab_obstack_allocate, NULL);
6290   call_site_local.pc = pc;
6291   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6292   if (*slot != NULL)
6293     {
6294       complaint (&symfile_complaints,
6295                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6296                    "DIE 0x%x [in module %s]"),
6297                  paddress (gdbarch, pc), die->offset, cu->objfile->name);
6298       return;
6299     }
6300
6301   /* Count parameters at the caller.  */
6302
6303   nparams = 0;
6304   for (child_die = die->child; child_die && child_die->tag;
6305        child_die = sibling_die (child_die))
6306     {
6307       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6308         {
6309           complaint (&symfile_complaints,
6310                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6311                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6312                      child_die->tag, child_die->offset, cu->objfile->name);
6313           continue;
6314         }
6315
6316       nparams++;
6317     }
6318
6319   call_site = obstack_alloc (&objfile->objfile_obstack,
6320                              (sizeof (*call_site)
6321                               + (sizeof (*call_site->parameter)
6322                                  * (nparams - 1))));
6323   *slot = call_site;
6324   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6325   call_site->pc = pc;
6326
6327   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6328     {
6329       struct die_info *func_die;
6330
6331       /* Skip also over DW_TAG_inlined_subroutine.  */
6332       for (func_die = die->parent;
6333            func_die && func_die->tag != DW_TAG_subprogram
6334            && func_die->tag != DW_TAG_subroutine_type;
6335            func_die = func_die->parent);
6336
6337       /* DW_AT_GNU_all_call_sites is a superset
6338          of DW_AT_GNU_all_tail_call_sites.  */
6339       if (func_die
6340           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6341           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6342         {
6343           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6344              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6345              both the initial caller containing the real return address PC and
6346              the final callee containing the current PC of a chain of tail
6347              calls do not need to have the tail call list complete.  But any
6348              function candidate for a virtual tail call frame searched via
6349              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6350              determined unambiguously.  */
6351         }
6352       else
6353         {
6354           struct type *func_type = NULL;
6355
6356           if (func_die)
6357             func_type = get_die_type (func_die, cu);
6358           if (func_type != NULL)
6359             {
6360               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6361
6362               /* Enlist this call site to the function.  */
6363               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6364               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6365             }
6366           else
6367             complaint (&symfile_complaints,
6368                        _("Cannot find function owning DW_TAG_GNU_call_site "
6369                          "DIE 0x%x [in module %s]"),
6370                        die->offset, cu->objfile->name);
6371         }
6372     }
6373
6374   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6375   if (attr == NULL)
6376     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6377   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6378   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6379     /* Keep NULL DWARF_BLOCK.  */;
6380   else if (attr_form_is_block (attr))
6381     {
6382       struct dwarf2_locexpr_baton *dlbaton;
6383
6384       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6385       dlbaton->data = DW_BLOCK (attr)->data;
6386       dlbaton->size = DW_BLOCK (attr)->size;
6387       dlbaton->per_cu = cu->per_cu;
6388
6389       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6390     }
6391   else if (is_ref_attr (attr))
6392     {
6393       struct objfile *objfile = cu->objfile;
6394       struct dwarf2_cu *target_cu = cu;
6395       struct die_info *target_die;
6396
6397       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6398       gdb_assert (target_cu->objfile == objfile);
6399       if (die_is_declaration (target_die, target_cu))
6400         {
6401           const char *target_physname;
6402
6403           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6404           if (target_physname == NULL)
6405             complaint (&symfile_complaints,
6406                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6407                          "physname, for referencing DIE 0x%x [in module %s]"),
6408                        die->offset, cu->objfile->name);
6409           else
6410             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6411         }
6412       else
6413         {
6414           CORE_ADDR lowpc;
6415
6416           /* DW_AT_entry_pc should be preferred.  */
6417           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6418             complaint (&symfile_complaints,
6419                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6420                          "low pc, for referencing DIE 0x%x [in module %s]"),
6421                        die->offset, cu->objfile->name);
6422           else
6423             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6424         }
6425     }
6426   else
6427     complaint (&symfile_complaints,
6428                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6429                  "block nor reference, for DIE 0x%x [in module %s]"),
6430                die->offset, cu->objfile->name);
6431
6432   call_site->per_cu = cu->per_cu;
6433
6434   for (child_die = die->child;
6435        child_die && child_die->tag;
6436        child_die = sibling_die (child_die))
6437     {
6438       struct dwarf2_locexpr_baton *dlbaton;
6439       struct call_site_parameter *parameter;
6440
6441       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6442         {
6443           /* Already printed the complaint above.  */
6444           continue;
6445         }
6446
6447       gdb_assert (call_site->parameter_count < nparams);
6448       parameter = &call_site->parameter[call_site->parameter_count];
6449
6450       /* DW_AT_location specifies the register number.  Value of the data
6451          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6452
6453       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6454       if (!attr || !attr_form_is_block (attr))
6455         {
6456           complaint (&symfile_complaints,
6457                      _("No DW_FORM_block* DW_AT_location for "
6458                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6459                      child_die->offset, cu->objfile->name);
6460           continue;
6461         }
6462       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6463                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6464       if (parameter->dwarf_reg == -1
6465           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6466                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6467                                         &parameter->fb_offset))
6468         {
6469           complaint (&symfile_complaints,
6470                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6471                        "for DW_FORM_block* DW_AT_location for "
6472                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6473                      child_die->offset, cu->objfile->name);
6474           continue;
6475         }
6476
6477       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6478       if (!attr_form_is_block (attr))
6479         {
6480           complaint (&symfile_complaints,
6481                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6482                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6483                      child_die->offset, cu->objfile->name);
6484           continue;
6485         }
6486       parameter->value = DW_BLOCK (attr)->data;
6487       parameter->value_size = DW_BLOCK (attr)->size;
6488
6489       /* Parameters are not pre-cleared by memset above.  */
6490       parameter->data_value = NULL;
6491       parameter->data_value_size = 0;
6492       call_site->parameter_count++;
6493
6494       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6495       if (attr)
6496         {
6497           if (!attr_form_is_block (attr))
6498             complaint (&symfile_complaints,
6499                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6500                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6501                        child_die->offset, cu->objfile->name);
6502           else
6503             {
6504               parameter->data_value = DW_BLOCK (attr)->data;
6505               parameter->data_value_size = DW_BLOCK (attr)->size;
6506             }
6507         }
6508     }
6509 }
6510
6511 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6512    Return 1 if the attributes are present and valid, otherwise, return 0.
6513    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6514
6515 static int
6516 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6517                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6518                     struct partial_symtab *ranges_pst)
6519 {
6520   struct objfile *objfile = cu->objfile;
6521   struct comp_unit_head *cu_header = &cu->header;
6522   bfd *obfd = objfile->obfd;
6523   unsigned int addr_size = cu_header->addr_size;
6524   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6525   /* Base address selection entry.  */
6526   CORE_ADDR base;
6527   int found_base;
6528   unsigned int dummy;
6529   gdb_byte *buffer;
6530   CORE_ADDR marker;
6531   int low_set;
6532   CORE_ADDR low = 0;
6533   CORE_ADDR high = 0;
6534   CORE_ADDR baseaddr;
6535
6536   found_base = cu->base_known;
6537   base = cu->base_address;
6538
6539   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6540   if (offset >= dwarf2_per_objfile->ranges.size)
6541     {
6542       complaint (&symfile_complaints,
6543                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6544                  offset);
6545       return 0;
6546     }
6547   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6548
6549   /* Read in the largest possible address.  */
6550   marker = read_address (obfd, buffer, cu, &dummy);
6551   if ((marker & mask) == mask)
6552     {
6553       /* If we found the largest possible address, then
6554          read the base address.  */
6555       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6556       buffer += 2 * addr_size;
6557       offset += 2 * addr_size;
6558       found_base = 1;
6559     }
6560
6561   low_set = 0;
6562
6563   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6564
6565   while (1)
6566     {
6567       CORE_ADDR range_beginning, range_end;
6568
6569       range_beginning = read_address (obfd, buffer, cu, &dummy);
6570       buffer += addr_size;
6571       range_end = read_address (obfd, buffer, cu, &dummy);
6572       buffer += addr_size;
6573       offset += 2 * addr_size;
6574
6575       /* An end of list marker is a pair of zero addresses.  */
6576       if (range_beginning == 0 && range_end == 0)
6577         /* Found the end of list entry.  */
6578         break;
6579
6580       /* Each base address selection entry is a pair of 2 values.
6581          The first is the largest possible address, the second is
6582          the base address.  Check for a base address here.  */
6583       if ((range_beginning & mask) == mask)
6584         {
6585           /* If we found the largest possible address, then
6586              read the base address.  */
6587           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6588           found_base = 1;
6589           continue;
6590         }
6591
6592       if (!found_base)
6593         {
6594           /* We have no valid base address for the ranges
6595              data.  */
6596           complaint (&symfile_complaints,
6597                      _("Invalid .debug_ranges data (no base address)"));
6598           return 0;
6599         }
6600
6601       if (range_beginning > range_end)
6602         {
6603           /* Inverted range entries are invalid.  */
6604           complaint (&symfile_complaints,
6605                      _("Invalid .debug_ranges data (inverted range)"));
6606           return 0;
6607         }
6608
6609       /* Empty range entries have no effect.  */
6610       if (range_beginning == range_end)
6611         continue;
6612
6613       range_beginning += base;
6614       range_end += base;
6615
6616       if (ranges_pst != NULL)
6617         addrmap_set_empty (objfile->psymtabs_addrmap,
6618                            range_beginning + baseaddr,
6619                            range_end - 1 + baseaddr,
6620                            ranges_pst);
6621
6622       /* FIXME: This is recording everything as a low-high
6623          segment of consecutive addresses.  We should have a
6624          data structure for discontiguous block ranges
6625          instead.  */
6626       if (! low_set)
6627         {
6628           low = range_beginning;
6629           high = range_end;
6630           low_set = 1;
6631         }
6632       else
6633         {
6634           if (range_beginning < low)
6635             low = range_beginning;
6636           if (range_end > high)
6637             high = range_end;
6638         }
6639     }
6640
6641   if (! low_set)
6642     /* If the first entry is an end-of-list marker, the range
6643        describes an empty scope, i.e. no instructions.  */
6644     return 0;
6645
6646   if (low_return)
6647     *low_return = low;
6648   if (high_return)
6649     *high_return = high;
6650   return 1;
6651 }
6652
6653 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6654    are present and valid, otherwise, return 0.  Return -1 if the range is
6655    discontinuous, i.e. derived from DW_AT_ranges information.  */
6656 static int
6657 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6658                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6659                       struct partial_symtab *pst)
6660 {
6661   struct attribute *attr;
6662   CORE_ADDR low = 0;
6663   CORE_ADDR high = 0;
6664   int ret = 0;
6665
6666   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6667   if (attr)
6668     {
6669       high = DW_ADDR (attr);
6670       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6671       if (attr)
6672         low = DW_ADDR (attr);
6673       else
6674         /* Found high w/o low attribute.  */
6675         return 0;
6676
6677       /* Found consecutive range of addresses.  */
6678       ret = 1;
6679     }
6680   else
6681     {
6682       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6683       if (attr != NULL)
6684         {
6685           /* Value of the DW_AT_ranges attribute is the offset in the
6686              .debug_ranges section.  */
6687           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6688             return 0;
6689           /* Found discontinuous range of addresses.  */
6690           ret = -1;
6691         }
6692     }
6693
6694   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6695   if (high <= low)
6696     return 0;
6697
6698   /* When using the GNU linker, .gnu.linkonce. sections are used to
6699      eliminate duplicate copies of functions and vtables and such.
6700      The linker will arbitrarily choose one and discard the others.
6701      The AT_*_pc values for such functions refer to local labels in
6702      these sections.  If the section from that file was discarded, the
6703      labels are not in the output, so the relocs get a value of 0.
6704      If this is a discarded function, mark the pc bounds as invalid,
6705      so that GDB will ignore it.  */
6706   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6707     return 0;
6708
6709   *lowpc = low;
6710   if (highpc)
6711     *highpc = high;
6712   return ret;
6713 }
6714
6715 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6716    its low and high PC addresses.  Do nothing if these addresses could not
6717    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6718    and HIGHPC to the high address if greater than HIGHPC.  */
6719
6720 static void
6721 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6722                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6723                                  struct dwarf2_cu *cu)
6724 {
6725   CORE_ADDR low, high;
6726   struct die_info *child = die->child;
6727
6728   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6729     {
6730       *lowpc = min (*lowpc, low);
6731       *highpc = max (*highpc, high);
6732     }
6733
6734   /* If the language does not allow nested subprograms (either inside
6735      subprograms or lexical blocks), we're done.  */
6736   if (cu->language != language_ada)
6737     return;
6738
6739   /* Check all the children of the given DIE.  If it contains nested
6740      subprograms, then check their pc bounds.  Likewise, we need to
6741      check lexical blocks as well, as they may also contain subprogram
6742      definitions.  */
6743   while (child && child->tag)
6744     {
6745       if (child->tag == DW_TAG_subprogram
6746           || child->tag == DW_TAG_lexical_block)
6747         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6748       child = sibling_die (child);
6749     }
6750 }
6751
6752 /* Get the low and high pc's represented by the scope DIE, and store
6753    them in *LOWPC and *HIGHPC.  If the correct values can't be
6754    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6755
6756 static void
6757 get_scope_pc_bounds (struct die_info *die,
6758                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6759                      struct dwarf2_cu *cu)
6760 {
6761   CORE_ADDR best_low = (CORE_ADDR) -1;
6762   CORE_ADDR best_high = (CORE_ADDR) 0;
6763   CORE_ADDR current_low, current_high;
6764
6765   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6766     {
6767       best_low = current_low;
6768       best_high = current_high;
6769     }
6770   else
6771     {
6772       struct die_info *child = die->child;
6773
6774       while (child && child->tag)
6775         {
6776           switch (child->tag) {
6777           case DW_TAG_subprogram:
6778             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6779             break;
6780           case DW_TAG_namespace:
6781           case DW_TAG_module:
6782             /* FIXME: carlton/2004-01-16: Should we do this for
6783                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6784                that current GCC's always emit the DIEs corresponding
6785                to definitions of methods of classes as children of a
6786                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6787                the DIEs giving the declarations, which could be
6788                anywhere).  But I don't see any reason why the
6789                standards says that they have to be there.  */
6790             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6791
6792             if (current_low != ((CORE_ADDR) -1))
6793               {
6794                 best_low = min (best_low, current_low);
6795                 best_high = max (best_high, current_high);
6796               }
6797             break;
6798           default:
6799             /* Ignore.  */
6800             break;
6801           }
6802
6803           child = sibling_die (child);
6804         }
6805     }
6806
6807   *lowpc = best_low;
6808   *highpc = best_high;
6809 }
6810
6811 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6812    in DIE.  */
6813 static void
6814 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6815                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6816 {
6817   struct attribute *attr;
6818
6819   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6820   if (attr)
6821     {
6822       CORE_ADDR high = DW_ADDR (attr);
6823
6824       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6825       if (attr)
6826         {
6827           CORE_ADDR low = DW_ADDR (attr);
6828
6829           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6830         }
6831     }
6832
6833   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6834   if (attr)
6835     {
6836       bfd *obfd = cu->objfile->obfd;
6837
6838       /* The value of the DW_AT_ranges attribute is the offset of the
6839          address range list in the .debug_ranges section.  */
6840       unsigned long offset = DW_UNSND (attr);
6841       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6842
6843       /* For some target architectures, but not others, the
6844          read_address function sign-extends the addresses it returns.
6845          To recognize base address selection entries, we need a
6846          mask.  */
6847       unsigned int addr_size = cu->header.addr_size;
6848       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6849
6850       /* The base address, to which the next pair is relative.  Note
6851          that this 'base' is a DWARF concept: most entries in a range
6852          list are relative, to reduce the number of relocs against the
6853          debugging information.  This is separate from this function's
6854          'baseaddr' argument, which GDB uses to relocate debugging
6855          information from a shared library based on the address at
6856          which the library was loaded.  */
6857       CORE_ADDR base = cu->base_address;
6858       int base_known = cu->base_known;
6859
6860       gdb_assert (dwarf2_per_objfile->ranges.readin);
6861       if (offset >= dwarf2_per_objfile->ranges.size)
6862         {
6863           complaint (&symfile_complaints,
6864                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6865                      offset);
6866           return;
6867         }
6868
6869       for (;;)
6870         {
6871           unsigned int bytes_read;
6872           CORE_ADDR start, end;
6873
6874           start = read_address (obfd, buffer, cu, &bytes_read);
6875           buffer += bytes_read;
6876           end = read_address (obfd, buffer, cu, &bytes_read);
6877           buffer += bytes_read;
6878
6879           /* Did we find the end of the range list?  */
6880           if (start == 0 && end == 0)
6881             break;
6882
6883           /* Did we find a base address selection entry?  */
6884           else if ((start & base_select_mask) == base_select_mask)
6885             {
6886               base = end;
6887               base_known = 1;
6888             }
6889
6890           /* We found an ordinary address range.  */
6891           else
6892             {
6893               if (!base_known)
6894                 {
6895                   complaint (&symfile_complaints,
6896                              _("Invalid .debug_ranges data "
6897                                "(no base address)"));
6898                   return;
6899                 }
6900
6901               if (start > end)
6902                 {
6903                   /* Inverted range entries are invalid.  */
6904                   complaint (&symfile_complaints,
6905                              _("Invalid .debug_ranges data "
6906                                "(inverted range)"));
6907                   return;
6908                 }
6909
6910               /* Empty range entries have no effect.  */
6911               if (start == end)
6912                 continue;
6913
6914               record_block_range (block,
6915                                   baseaddr + base + start,
6916                                   baseaddr + base + end - 1);
6917             }
6918         }
6919     }
6920 }
6921
6922 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6923    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6924    during 4.6.0 experimental.  */
6925
6926 static int
6927 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6928 {
6929   const char *cs;
6930   int major, minor, release;
6931
6932   if (cu->producer == NULL)
6933     {
6934       /* For unknown compilers expect their behavior is DWARF version
6935          compliant.
6936
6937          GCC started to support .debug_types sections by -gdwarf-4 since
6938          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6939          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6940          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6941          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6942
6943       return 0;
6944     }
6945
6946   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6947
6948   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6949     {
6950       /* For non-GCC compilers expect their behavior is DWARF version
6951          compliant.  */
6952
6953       return 0;
6954     }
6955   cs = &cu->producer[strlen ("GNU ")];
6956   while (*cs && !isdigit (*cs))
6957     cs++;
6958   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6959     {
6960       /* Not recognized as GCC.  */
6961
6962       return 0;
6963     }
6964
6965   return major < 4 || (major == 4 && minor < 6);
6966 }
6967
6968 /* Return the default accessibility type if it is not overriden by
6969    DW_AT_accessibility.  */
6970
6971 static enum dwarf_access_attribute
6972 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6973 {
6974   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6975     {
6976       /* The default DWARF 2 accessibility for members is public, the default
6977          accessibility for inheritance is private.  */
6978
6979       if (die->tag != DW_TAG_inheritance)
6980         return DW_ACCESS_public;
6981       else
6982         return DW_ACCESS_private;
6983     }
6984   else
6985     {
6986       /* DWARF 3+ defines the default accessibility a different way.  The same
6987          rules apply now for DW_TAG_inheritance as for the members and it only
6988          depends on the container kind.  */
6989
6990       if (die->parent->tag == DW_TAG_class_type)
6991         return DW_ACCESS_private;
6992       else
6993         return DW_ACCESS_public;
6994     }
6995 }
6996
6997 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6998    offset.  If the attribute was not found return 0, otherwise return
6999    1.  If it was found but could not properly be handled, set *OFFSET
7000    to 0.  */
7001
7002 static int
7003 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
7004                              LONGEST *offset)
7005 {
7006   struct attribute *attr;
7007
7008   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
7009   if (attr != NULL)
7010     {
7011       *offset = 0;
7012
7013       /* Note that we do not check for a section offset first here.
7014          This is because DW_AT_data_member_location is new in DWARF 4,
7015          so if we see it, we can assume that a constant form is really
7016          a constant and not a section offset.  */
7017       if (attr_form_is_constant (attr))
7018         *offset = dwarf2_get_attr_constant_value (attr, 0);
7019       else if (attr_form_is_section_offset (attr))
7020         dwarf2_complex_location_expr_complaint ();
7021       else if (attr_form_is_block (attr))
7022         *offset = decode_locdesc (DW_BLOCK (attr), cu);
7023       else
7024         dwarf2_complex_location_expr_complaint ();
7025
7026       return 1;
7027     }
7028
7029   return 0;
7030 }
7031
7032 /* Add an aggregate field to the field list.  */
7033
7034 static void
7035 dwarf2_add_field (struct field_info *fip, struct die_info *die,
7036                   struct dwarf2_cu *cu)
7037 {
7038   struct objfile *objfile = cu->objfile;
7039   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7040   struct nextfield *new_field;
7041   struct attribute *attr;
7042   struct field *fp;
7043   char *fieldname = "";
7044
7045   /* Allocate a new field list entry and link it in.  */
7046   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
7047   make_cleanup (xfree, new_field);
7048   memset (new_field, 0, sizeof (struct nextfield));
7049
7050   if (die->tag == DW_TAG_inheritance)
7051     {
7052       new_field->next = fip->baseclasses;
7053       fip->baseclasses = new_field;
7054     }
7055   else
7056     {
7057       new_field->next = fip->fields;
7058       fip->fields = new_field;
7059     }
7060   fip->nfields++;
7061
7062   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7063   if (attr)
7064     new_field->accessibility = DW_UNSND (attr);
7065   else
7066     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7067   if (new_field->accessibility != DW_ACCESS_public)
7068     fip->non_public_fields = 1;
7069
7070   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7071   if (attr)
7072     new_field->virtuality = DW_UNSND (attr);
7073   else
7074     new_field->virtuality = DW_VIRTUALITY_none;
7075
7076   fp = &new_field->field;
7077
7078   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7079     {
7080       LONGEST offset;
7081
7082       /* Data member other than a C++ static data member.  */
7083
7084       /* Get type of field.  */
7085       fp->type = die_type (die, cu);
7086
7087       SET_FIELD_BITPOS (*fp, 0);
7088
7089       /* Get bit size of field (zero if none).  */
7090       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7091       if (attr)
7092         {
7093           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7094         }
7095       else
7096         {
7097           FIELD_BITSIZE (*fp) = 0;
7098         }
7099
7100       /* Get bit offset of field.  */
7101       if (handle_data_member_location (die, cu, &offset))
7102         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7103       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7104       if (attr)
7105         {
7106           if (gdbarch_bits_big_endian (gdbarch))
7107             {
7108               /* For big endian bits, the DW_AT_bit_offset gives the
7109                  additional bit offset from the MSB of the containing
7110                  anonymous object to the MSB of the field.  We don't
7111                  have to do anything special since we don't need to
7112                  know the size of the anonymous object.  */
7113               FIELD_BITPOS (*fp) += DW_UNSND (attr);
7114             }
7115           else
7116             {
7117               /* For little endian bits, compute the bit offset to the
7118                  MSB of the anonymous object, subtract off the number of
7119                  bits from the MSB of the field to the MSB of the
7120                  object, and then subtract off the number of bits of
7121                  the field itself.  The result is the bit offset of
7122                  the LSB of the field.  */
7123               int anonymous_size;
7124               int bit_offset = DW_UNSND (attr);
7125
7126               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7127               if (attr)
7128                 {
7129                   /* The size of the anonymous object containing
7130                      the bit field is explicit, so use the
7131                      indicated size (in bytes).  */
7132                   anonymous_size = DW_UNSND (attr);
7133                 }
7134               else
7135                 {
7136                   /* The size of the anonymous object containing
7137                      the bit field must be inferred from the type
7138                      attribute of the data member containing the
7139                      bit field.  */
7140                   anonymous_size = TYPE_LENGTH (fp->type);
7141                 }
7142               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7143                 - bit_offset - FIELD_BITSIZE (*fp);
7144             }
7145         }
7146
7147       /* Get name of field.  */
7148       fieldname = dwarf2_name (die, cu);
7149       if (fieldname == NULL)
7150         fieldname = "";
7151
7152       /* The name is already allocated along with this objfile, so we don't
7153          need to duplicate it for the type.  */
7154       fp->name = fieldname;
7155
7156       /* Change accessibility for artificial fields (e.g. virtual table
7157          pointer or virtual base class pointer) to private.  */
7158       if (dwarf2_attr (die, DW_AT_artificial, cu))
7159         {
7160           FIELD_ARTIFICIAL (*fp) = 1;
7161           new_field->accessibility = DW_ACCESS_private;
7162           fip->non_public_fields = 1;
7163         }
7164     }
7165   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7166     {
7167       /* C++ static member.  */
7168
7169       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7170          is a declaration, but all versions of G++ as of this writing
7171          (so through at least 3.2.1) incorrectly generate
7172          DW_TAG_variable tags.  */
7173
7174       const char *physname;
7175
7176       /* Get name of field.  */
7177       fieldname = dwarf2_name (die, cu);
7178       if (fieldname == NULL)
7179         return;
7180
7181       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7182       if (attr
7183           /* Only create a symbol if this is an external value.
7184              new_symbol checks this and puts the value in the global symbol
7185              table, which we want.  If it is not external, new_symbol
7186              will try to put the value in cu->list_in_scope which is wrong.  */
7187           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7188         {
7189           /* A static const member, not much different than an enum as far as
7190              we're concerned, except that we can support more types.  */
7191           new_symbol (die, NULL, cu);
7192         }
7193
7194       /* Get physical name.  */
7195       physname = dwarf2_physname (fieldname, die, cu);
7196
7197       /* The name is already allocated along with this objfile, so we don't
7198          need to duplicate it for the type.  */
7199       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7200       FIELD_TYPE (*fp) = die_type (die, cu);
7201       FIELD_NAME (*fp) = fieldname;
7202     }
7203   else if (die->tag == DW_TAG_inheritance)
7204     {
7205       LONGEST offset;
7206
7207       /* C++ base class field.  */
7208       if (handle_data_member_location (die, cu, &offset))
7209         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7210       FIELD_BITSIZE (*fp) = 0;
7211       FIELD_TYPE (*fp) = die_type (die, cu);
7212       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7213       fip->nbaseclasses++;
7214     }
7215 }
7216
7217 /* Add a typedef defined in the scope of the FIP's class.  */
7218
7219 static void
7220 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7221                     struct dwarf2_cu *cu)
7222 {
7223   struct objfile *objfile = cu->objfile;
7224   struct typedef_field_list *new_field;
7225   struct attribute *attr;
7226   struct typedef_field *fp;
7227   char *fieldname = "";
7228
7229   /* Allocate a new field list entry and link it in.  */
7230   new_field = xzalloc (sizeof (*new_field));
7231   make_cleanup (xfree, new_field);
7232
7233   gdb_assert (die->tag == DW_TAG_typedef);
7234
7235   fp = &new_field->field;
7236
7237   /* Get name of field.  */
7238   fp->name = dwarf2_name (die, cu);
7239   if (fp->name == NULL)
7240     return;
7241
7242   fp->type = read_type_die (die, cu);
7243
7244   new_field->next = fip->typedef_field_list;
7245   fip->typedef_field_list = new_field;
7246   fip->typedef_field_list_count++;
7247 }
7248
7249 /* Create the vector of fields, and attach it to the type.  */
7250
7251 static void
7252 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7253                               struct dwarf2_cu *cu)
7254 {
7255   int nfields = fip->nfields;
7256
7257   /* Record the field count, allocate space for the array of fields,
7258      and create blank accessibility bitfields if necessary.  */
7259   TYPE_NFIELDS (type) = nfields;
7260   TYPE_FIELDS (type) = (struct field *)
7261     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7262   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7263
7264   if (fip->non_public_fields && cu->language != language_ada)
7265     {
7266       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7267
7268       TYPE_FIELD_PRIVATE_BITS (type) =
7269         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7270       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7271
7272       TYPE_FIELD_PROTECTED_BITS (type) =
7273         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7274       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7275
7276       TYPE_FIELD_IGNORE_BITS (type) =
7277         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7278       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7279     }
7280
7281   /* If the type has baseclasses, allocate and clear a bit vector for
7282      TYPE_FIELD_VIRTUAL_BITS.  */
7283   if (fip->nbaseclasses && cu->language != language_ada)
7284     {
7285       int num_bytes = B_BYTES (fip->nbaseclasses);
7286       unsigned char *pointer;
7287
7288       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7289       pointer = TYPE_ALLOC (type, num_bytes);
7290       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7291       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7292       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7293     }
7294
7295   /* Copy the saved-up fields into the field vector.  Start from the head of
7296      the list, adding to the tail of the field array, so that they end up in
7297      the same order in the array in which they were added to the list.  */
7298   while (nfields-- > 0)
7299     {
7300       struct nextfield *fieldp;
7301
7302       if (fip->fields)
7303         {
7304           fieldp = fip->fields;
7305           fip->fields = fieldp->next;
7306         }
7307       else
7308         {
7309           fieldp = fip->baseclasses;
7310           fip->baseclasses = fieldp->next;
7311         }
7312
7313       TYPE_FIELD (type, nfields) = fieldp->field;
7314       switch (fieldp->accessibility)
7315         {
7316         case DW_ACCESS_private:
7317           if (cu->language != language_ada)
7318             SET_TYPE_FIELD_PRIVATE (type, nfields);
7319           break;
7320
7321         case DW_ACCESS_protected:
7322           if (cu->language != language_ada)
7323             SET_TYPE_FIELD_PROTECTED (type, nfields);
7324           break;
7325
7326         case DW_ACCESS_public:
7327           break;
7328
7329         default:
7330           /* Unknown accessibility.  Complain and treat it as public.  */
7331           {
7332             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7333                        fieldp->accessibility);
7334           }
7335           break;
7336         }
7337       if (nfields < fip->nbaseclasses)
7338         {
7339           switch (fieldp->virtuality)
7340             {
7341             case DW_VIRTUALITY_virtual:
7342             case DW_VIRTUALITY_pure_virtual:
7343               if (cu->language == language_ada)
7344                 error (_("unexpected virtuality in component of Ada type"));
7345               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7346               break;
7347             }
7348         }
7349     }
7350 }
7351
7352 /* Add a member function to the proper fieldlist.  */
7353
7354 static void
7355 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7356                       struct type *type, struct dwarf2_cu *cu)
7357 {
7358   struct objfile *objfile = cu->objfile;
7359   struct attribute *attr;
7360   struct fnfieldlist *flp;
7361   int i;
7362   struct fn_field *fnp;
7363   char *fieldname;
7364   struct nextfnfield *new_fnfield;
7365   struct type *this_type;
7366   enum dwarf_access_attribute accessibility;
7367
7368   if (cu->language == language_ada)
7369     error (_("unexpected member function in Ada type"));
7370
7371   /* Get name of member function.  */
7372   fieldname = dwarf2_name (die, cu);
7373   if (fieldname == NULL)
7374     return;
7375
7376   /* Look up member function name in fieldlist.  */
7377   for (i = 0; i < fip->nfnfields; i++)
7378     {
7379       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7380         break;
7381     }
7382
7383   /* Create new list element if necessary.  */
7384   if (i < fip->nfnfields)
7385     flp = &fip->fnfieldlists[i];
7386   else
7387     {
7388       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7389         {
7390           fip->fnfieldlists = (struct fnfieldlist *)
7391             xrealloc (fip->fnfieldlists,
7392                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7393                       * sizeof (struct fnfieldlist));
7394           if (fip->nfnfields == 0)
7395             make_cleanup (free_current_contents, &fip->fnfieldlists);
7396         }
7397       flp = &fip->fnfieldlists[fip->nfnfields];
7398       flp->name = fieldname;
7399       flp->length = 0;
7400       flp->head = NULL;
7401       i = fip->nfnfields++;
7402     }
7403
7404   /* Create a new member function field and chain it to the field list
7405      entry.  */
7406   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7407   make_cleanup (xfree, new_fnfield);
7408   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7409   new_fnfield->next = flp->head;
7410   flp->head = new_fnfield;
7411   flp->length++;
7412
7413   /* Fill in the member function field info.  */
7414   fnp = &new_fnfield->fnfield;
7415
7416   /* Delay processing of the physname until later.  */
7417   if (cu->language == language_cplus || cu->language == language_java)
7418     {
7419       add_to_method_list (type, i, flp->length - 1, fieldname,
7420                           die, cu);
7421     }
7422   else
7423     {
7424       const char *physname = dwarf2_physname (fieldname, die, cu);
7425       fnp->physname = physname ? physname : "";
7426     }
7427
7428   fnp->type = alloc_type (objfile);
7429   this_type = read_type_die (die, cu);
7430   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7431     {
7432       int nparams = TYPE_NFIELDS (this_type);
7433
7434       /* TYPE is the domain of this method, and THIS_TYPE is the type
7435            of the method itself (TYPE_CODE_METHOD).  */
7436       smash_to_method_type (fnp->type, type,
7437                             TYPE_TARGET_TYPE (this_type),
7438                             TYPE_FIELDS (this_type),
7439                             TYPE_NFIELDS (this_type),
7440                             TYPE_VARARGS (this_type));
7441
7442       /* Handle static member functions.
7443          Dwarf2 has no clean way to discern C++ static and non-static
7444          member functions.  G++ helps GDB by marking the first
7445          parameter for non-static member functions (which is the this
7446          pointer) as artificial.  We obtain this information from
7447          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7448       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7449         fnp->voffset = VOFFSET_STATIC;
7450     }
7451   else
7452     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7453                dwarf2_full_name (fieldname, die, cu));
7454
7455   /* Get fcontext from DW_AT_containing_type if present.  */
7456   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7457     fnp->fcontext = die_containing_type (die, cu);
7458
7459   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7460      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7461
7462   /* Get accessibility.  */
7463   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7464   if (attr)
7465     accessibility = DW_UNSND (attr);
7466   else
7467     accessibility = dwarf2_default_access_attribute (die, cu);
7468   switch (accessibility)
7469     {
7470     case DW_ACCESS_private:
7471       fnp->is_private = 1;
7472       break;
7473     case DW_ACCESS_protected:
7474       fnp->is_protected = 1;
7475       break;
7476     }
7477
7478   /* Check for artificial methods.  */
7479   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7480   if (attr && DW_UNSND (attr) != 0)
7481     fnp->is_artificial = 1;
7482
7483   /* Get index in virtual function table if it is a virtual member
7484      function.  For older versions of GCC, this is an offset in the
7485      appropriate virtual table, as specified by DW_AT_containing_type.
7486      For everyone else, it is an expression to be evaluated relative
7487      to the object address.  */
7488
7489   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7490   if (attr)
7491     {
7492       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7493         {
7494           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7495             {
7496               /* Old-style GCC.  */
7497               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7498             }
7499           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7500                    || (DW_BLOCK (attr)->size > 1
7501                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7502                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7503             {
7504               struct dwarf_block blk;
7505               int offset;
7506
7507               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7508                         ? 1 : 2);
7509               blk.size = DW_BLOCK (attr)->size - offset;
7510               blk.data = DW_BLOCK (attr)->data + offset;
7511               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7512               if ((fnp->voffset % cu->header.addr_size) != 0)
7513                 dwarf2_complex_location_expr_complaint ();
7514               else
7515                 fnp->voffset /= cu->header.addr_size;
7516               fnp->voffset += 2;
7517             }
7518           else
7519             dwarf2_complex_location_expr_complaint ();
7520
7521           if (!fnp->fcontext)
7522             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7523         }
7524       else if (attr_form_is_section_offset (attr))
7525         {
7526           dwarf2_complex_location_expr_complaint ();
7527         }
7528       else
7529         {
7530           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7531                                                  fieldname);
7532         }
7533     }
7534   else
7535     {
7536       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7537       if (attr && DW_UNSND (attr))
7538         {
7539           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7540           complaint (&symfile_complaints,
7541                      _("Member function \"%s\" (offset %d) is virtual "
7542                        "but the vtable offset is not specified"),
7543                      fieldname, die->offset);
7544           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7545           TYPE_CPLUS_DYNAMIC (type) = 1;
7546         }
7547     }
7548 }
7549
7550 /* Create the vector of member function fields, and attach it to the type.  */
7551
7552 static void
7553 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7554                                  struct dwarf2_cu *cu)
7555 {
7556   struct fnfieldlist *flp;
7557   int total_length = 0;
7558   int i;
7559
7560   if (cu->language == language_ada)
7561     error (_("unexpected member functions in Ada type"));
7562
7563   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7564   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7565     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7566
7567   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7568     {
7569       struct nextfnfield *nfp = flp->head;
7570       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7571       int k;
7572
7573       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7574       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7575       fn_flp->fn_fields = (struct fn_field *)
7576         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7577       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7578         fn_flp->fn_fields[k] = nfp->fnfield;
7579
7580       total_length += flp->length;
7581     }
7582
7583   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7584   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7585 }
7586
7587 /* Returns non-zero if NAME is the name of a vtable member in CU's
7588    language, zero otherwise.  */
7589 static int
7590 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7591 {
7592   static const char vptr[] = "_vptr";
7593   static const char vtable[] = "vtable";
7594
7595   /* Look for the C++ and Java forms of the vtable.  */
7596   if ((cu->language == language_java
7597        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7598        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7599        && is_cplus_marker (name[sizeof (vptr) - 1])))
7600     return 1;
7601
7602   return 0;
7603 }
7604
7605 /* GCC outputs unnamed structures that are really pointers to member
7606    functions, with the ABI-specified layout.  If TYPE describes
7607    such a structure, smash it into a member function type.
7608
7609    GCC shouldn't do this; it should just output pointer to member DIEs.
7610    This is GCC PR debug/28767.  */
7611
7612 static void
7613 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7614 {
7615   struct type *pfn_type, *domain_type, *new_type;
7616
7617   /* Check for a structure with no name and two children.  */
7618   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7619     return;
7620
7621   /* Check for __pfn and __delta members.  */
7622   if (TYPE_FIELD_NAME (type, 0) == NULL
7623       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7624       || TYPE_FIELD_NAME (type, 1) == NULL
7625       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7626     return;
7627
7628   /* Find the type of the method.  */
7629   pfn_type = TYPE_FIELD_TYPE (type, 0);
7630   if (pfn_type == NULL
7631       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7632       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7633     return;
7634
7635   /* Look for the "this" argument.  */
7636   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7637   if (TYPE_NFIELDS (pfn_type) == 0
7638       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7639       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7640     return;
7641
7642   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7643   new_type = alloc_type (objfile);
7644   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7645                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7646                         TYPE_VARARGS (pfn_type));
7647   smash_to_methodptr_type (type, new_type);
7648 }
7649
7650 /* Called when we find the DIE that starts a structure or union scope
7651    (definition) to create a type for the structure or union.  Fill in
7652    the type's name and general properties; the members will not be
7653    processed until process_structure_type.
7654
7655    NOTE: we need to call these functions regardless of whether or not the
7656    DIE has a DW_AT_name attribute, since it might be an anonymous
7657    structure or union.  This gets the type entered into our set of
7658    user defined types.
7659
7660    However, if the structure is incomplete (an opaque struct/union)
7661    then suppress creating a symbol table entry for it since gdb only
7662    wants to find the one with the complete definition.  Note that if
7663    it is complete, we just call new_symbol, which does it's own
7664    checking about whether the struct/union is anonymous or not (and
7665    suppresses creating a symbol table entry itself).  */
7666
7667 static struct type *
7668 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7669 {
7670   struct objfile *objfile = cu->objfile;
7671   struct type *type;
7672   struct attribute *attr;
7673   char *name;
7674
7675   /* If the definition of this type lives in .debug_types, read that type.
7676      Don't follow DW_AT_specification though, that will take us back up
7677      the chain and we want to go down.  */
7678   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7679   if (attr)
7680     {
7681       struct dwarf2_cu *type_cu = cu;
7682       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7683
7684       /* We could just recurse on read_structure_type, but we need to call
7685          get_die_type to ensure only one type for this DIE is created.
7686          This is important, for example, because for c++ classes we need
7687          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7688       type = read_type_die (type_die, type_cu);
7689
7690       /* TYPE_CU may not be the same as CU.
7691          Ensure TYPE is recorded in CU's type_hash table.  */
7692       return set_die_type (die, type, cu);
7693     }
7694
7695   type = alloc_type (objfile);
7696   INIT_CPLUS_SPECIFIC (type);
7697
7698   name = dwarf2_name (die, cu);
7699   if (name != NULL)
7700     {
7701       if (cu->language == language_cplus
7702           || cu->language == language_java)
7703         {
7704           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7705
7706           /* dwarf2_full_name might have already finished building the DIE's
7707              type.  If so, there is no need to continue.  */
7708           if (get_die_type (die, cu) != NULL)
7709             return get_die_type (die, cu);
7710
7711           TYPE_TAG_NAME (type) = full_name;
7712           if (die->tag == DW_TAG_structure_type
7713               || die->tag == DW_TAG_class_type)
7714             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7715         }
7716       else
7717         {
7718           /* The name is already allocated along with this objfile, so
7719              we don't need to duplicate it for the type.  */
7720           TYPE_TAG_NAME (type) = (char *) name;
7721           if (die->tag == DW_TAG_class_type)
7722             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7723         }
7724     }
7725
7726   if (die->tag == DW_TAG_structure_type)
7727     {
7728       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7729     }
7730   else if (die->tag == DW_TAG_union_type)
7731     {
7732       TYPE_CODE (type) = TYPE_CODE_UNION;
7733     }
7734   else
7735     {
7736       TYPE_CODE (type) = TYPE_CODE_CLASS;
7737     }
7738
7739   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7740     TYPE_DECLARED_CLASS (type) = 1;
7741
7742   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7743   if (attr)
7744     {
7745       TYPE_LENGTH (type) = DW_UNSND (attr);
7746     }
7747   else
7748     {
7749       TYPE_LENGTH (type) = 0;
7750     }
7751
7752   TYPE_STUB_SUPPORTED (type) = 1;
7753   if (die_is_declaration (die, cu))
7754     TYPE_STUB (type) = 1;
7755   else if (attr == NULL && die->child == NULL
7756            && producer_is_realview (cu->producer))
7757     /* RealView does not output the required DW_AT_declaration
7758        on incomplete types.  */
7759     TYPE_STUB (type) = 1;
7760
7761   /* We need to add the type field to the die immediately so we don't
7762      infinitely recurse when dealing with pointers to the structure
7763      type within the structure itself.  */
7764   set_die_type (die, type, cu);
7765
7766   /* set_die_type should be already done.  */
7767   set_descriptive_type (type, die, cu);
7768
7769   return type;
7770 }
7771
7772 /* Finish creating a structure or union type, including filling in
7773    its members and creating a symbol for it.  */
7774
7775 static void
7776 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7777 {
7778   struct objfile *objfile = cu->objfile;
7779   struct die_info *child_die = die->child;
7780   struct type *type;
7781
7782   type = get_die_type (die, cu);
7783   if (type == NULL)
7784     type = read_structure_type (die, cu);
7785
7786   if (die->child != NULL && ! die_is_declaration (die, cu))
7787     {
7788       struct field_info fi;
7789       struct die_info *child_die;
7790       VEC (symbolp) *template_args = NULL;
7791       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7792
7793       memset (&fi, 0, sizeof (struct field_info));
7794
7795       child_die = die->child;
7796
7797       while (child_die && child_die->tag)
7798         {
7799           if (child_die->tag == DW_TAG_member
7800               || child_die->tag == DW_TAG_variable)
7801             {
7802               /* NOTE: carlton/2002-11-05: A C++ static data member
7803                  should be a DW_TAG_member that is a declaration, but
7804                  all versions of G++ as of this writing (so through at
7805                  least 3.2.1) incorrectly generate DW_TAG_variable
7806                  tags for them instead.  */
7807               dwarf2_add_field (&fi, child_die, cu);
7808             }
7809           else if (child_die->tag == DW_TAG_subprogram)
7810             {
7811               /* C++ member function.  */
7812               dwarf2_add_member_fn (&fi, child_die, type, cu);
7813             }
7814           else if (child_die->tag == DW_TAG_inheritance)
7815             {
7816               /* C++ base class field.  */
7817               dwarf2_add_field (&fi, child_die, cu);
7818             }
7819           else if (child_die->tag == DW_TAG_typedef)
7820             dwarf2_add_typedef (&fi, child_die, cu);
7821           else if (child_die->tag == DW_TAG_template_type_param
7822                    || child_die->tag == DW_TAG_template_value_param)
7823             {
7824               struct symbol *arg = new_symbol (child_die, NULL, cu);
7825
7826               if (arg != NULL)
7827                 VEC_safe_push (symbolp, template_args, arg);
7828             }
7829
7830           child_die = sibling_die (child_die);
7831         }
7832
7833       /* Attach template arguments to type.  */
7834       if (! VEC_empty (symbolp, template_args))
7835         {
7836           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7837           TYPE_N_TEMPLATE_ARGUMENTS (type)
7838             = VEC_length (symbolp, template_args);
7839           TYPE_TEMPLATE_ARGUMENTS (type)
7840             = obstack_alloc (&objfile->objfile_obstack,
7841                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7842                               * sizeof (struct symbol *)));
7843           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7844                   VEC_address (symbolp, template_args),
7845                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7846                    * sizeof (struct symbol *)));
7847           VEC_free (symbolp, template_args);
7848         }
7849
7850       /* Attach fields and member functions to the type.  */
7851       if (fi.nfields)
7852         dwarf2_attach_fields_to_type (&fi, type, cu);
7853       if (fi.nfnfields)
7854         {
7855           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7856
7857           /* Get the type which refers to the base class (possibly this
7858              class itself) which contains the vtable pointer for the current
7859              class from the DW_AT_containing_type attribute.  This use of
7860              DW_AT_containing_type is a GNU extension.  */
7861
7862           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7863             {
7864               struct type *t = die_containing_type (die, cu);
7865
7866               TYPE_VPTR_BASETYPE (type) = t;
7867               if (type == t)
7868                 {
7869                   int i;
7870
7871                   /* Our own class provides vtbl ptr.  */
7872                   for (i = TYPE_NFIELDS (t) - 1;
7873                        i >= TYPE_N_BASECLASSES (t);
7874                        --i)
7875                     {
7876                       char *fieldname = TYPE_FIELD_NAME (t, i);
7877
7878                       if (is_vtable_name (fieldname, cu))
7879                         {
7880                           TYPE_VPTR_FIELDNO (type) = i;
7881                           break;
7882                         }
7883                     }
7884
7885                   /* Complain if virtual function table field not found.  */
7886                   if (i < TYPE_N_BASECLASSES (t))
7887                     complaint (&symfile_complaints,
7888                                _("virtual function table pointer "
7889                                  "not found when defining class '%s'"),
7890                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7891                                "");
7892                 }
7893               else
7894                 {
7895                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7896                 }
7897             }
7898           else if (cu->producer
7899                    && strncmp (cu->producer,
7900                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7901             {
7902               /* The IBM XLC compiler does not provide direct indication
7903                  of the containing type, but the vtable pointer is
7904                  always named __vfp.  */
7905
7906               int i;
7907
7908               for (i = TYPE_NFIELDS (type) - 1;
7909                    i >= TYPE_N_BASECLASSES (type);
7910                    --i)
7911                 {
7912                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7913                     {
7914                       TYPE_VPTR_FIELDNO (type) = i;
7915                       TYPE_VPTR_BASETYPE (type) = type;
7916                       break;
7917                     }
7918                 }
7919             }
7920         }
7921
7922       /* Copy fi.typedef_field_list linked list elements content into the
7923          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7924       if (fi.typedef_field_list)
7925         {
7926           int i = fi.typedef_field_list_count;
7927
7928           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7929           TYPE_TYPEDEF_FIELD_ARRAY (type)
7930             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7931           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7932
7933           /* Reverse the list order to keep the debug info elements order.  */
7934           while (--i >= 0)
7935             {
7936               struct typedef_field *dest, *src;
7937
7938               dest = &TYPE_TYPEDEF_FIELD (type, i);
7939               src = &fi.typedef_field_list->field;
7940               fi.typedef_field_list = fi.typedef_field_list->next;
7941               *dest = *src;
7942             }
7943         }
7944
7945       do_cleanups (back_to);
7946
7947       if (HAVE_CPLUS_STRUCT (type))
7948         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7949     }
7950
7951   quirk_gcc_member_function_pointer (type, cu->objfile);
7952
7953   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7954      snapshots) has been known to create a die giving a declaration
7955      for a class that has, as a child, a die giving a definition for a
7956      nested class.  So we have to process our children even if the
7957      current die is a declaration.  Normally, of course, a declaration
7958      won't have any children at all.  */
7959
7960   while (child_die != NULL && child_die->tag)
7961     {
7962       if (child_die->tag == DW_TAG_member
7963           || child_die->tag == DW_TAG_variable
7964           || child_die->tag == DW_TAG_inheritance
7965           || child_die->tag == DW_TAG_template_value_param
7966           || child_die->tag == DW_TAG_template_type_param)
7967         {
7968           /* Do nothing.  */
7969         }
7970       else
7971         process_die (child_die, cu);
7972
7973       child_die = sibling_die (child_die);
7974     }
7975
7976   /* Do not consider external references.  According to the DWARF standard,
7977      these DIEs are identified by the fact that they have no byte_size
7978      attribute, and a declaration attribute.  */
7979   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7980       || !die_is_declaration (die, cu))
7981     new_symbol (die, type, cu);
7982 }
7983
7984 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7985    complete the type's fields yet, or create any symbols.  */
7986
7987 static struct type *
7988 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7989 {
7990   struct objfile *objfile = cu->objfile;
7991   struct type *type;
7992   struct attribute *attr;
7993   const char *name;
7994
7995   /* If the definition of this type lives in .debug_types, read that type.
7996      Don't follow DW_AT_specification though, that will take us back up
7997      the chain and we want to go down.  */
7998   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7999   if (attr)
8000     {
8001       struct dwarf2_cu *type_cu = cu;
8002       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
8003
8004       type = read_type_die (type_die, type_cu);
8005
8006       /* TYPE_CU may not be the same as CU.
8007          Ensure TYPE is recorded in CU's type_hash table.  */
8008       return set_die_type (die, type, cu);
8009     }
8010
8011   type = alloc_type (objfile);
8012
8013   TYPE_CODE (type) = TYPE_CODE_ENUM;
8014   name = dwarf2_full_name (NULL, die, cu);
8015   if (name != NULL)
8016     TYPE_TAG_NAME (type) = (char *) name;
8017
8018   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8019   if (attr)
8020     {
8021       TYPE_LENGTH (type) = DW_UNSND (attr);
8022     }
8023   else
8024     {
8025       TYPE_LENGTH (type) = 0;
8026     }
8027
8028   /* The enumeration DIE can be incomplete.  In Ada, any type can be
8029      declared as private in the package spec, and then defined only
8030      inside the package body.  Such types are known as Taft Amendment
8031      Types.  When another package uses such a type, an incomplete DIE
8032      may be generated by the compiler.  */
8033   if (die_is_declaration (die, cu))
8034     TYPE_STUB (type) = 1;
8035
8036   return set_die_type (die, type, cu);
8037 }
8038
8039 /* Given a pointer to a die which begins an enumeration, process all
8040    the dies that define the members of the enumeration, and create the
8041    symbol for the enumeration type.
8042
8043    NOTE: We reverse the order of the element list.  */
8044
8045 static void
8046 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8047 {
8048   struct type *this_type;
8049
8050   this_type = get_die_type (die, cu);
8051   if (this_type == NULL)
8052     this_type = read_enumeration_type (die, cu);
8053
8054   if (die->child != NULL)
8055     {
8056       struct die_info *child_die;
8057       struct symbol *sym;
8058       struct field *fields = NULL;
8059       int num_fields = 0;
8060       int unsigned_enum = 1;
8061       char *name;
8062
8063       child_die = die->child;
8064       while (child_die && child_die->tag)
8065         {
8066           if (child_die->tag != DW_TAG_enumerator)
8067             {
8068               process_die (child_die, cu);
8069             }
8070           else
8071             {
8072               name = dwarf2_name (child_die, cu);
8073               if (name)
8074                 {
8075                   sym = new_symbol (child_die, this_type, cu);
8076                   if (SYMBOL_VALUE (sym) < 0)
8077                     unsigned_enum = 0;
8078
8079                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8080                     {
8081                       fields = (struct field *)
8082                         xrealloc (fields,
8083                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
8084                                   * sizeof (struct field));
8085                     }
8086
8087                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8088                   FIELD_TYPE (fields[num_fields]) = NULL;
8089                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8090                   FIELD_BITSIZE (fields[num_fields]) = 0;
8091
8092                   num_fields++;
8093                 }
8094             }
8095
8096           child_die = sibling_die (child_die);
8097         }
8098
8099       if (num_fields)
8100         {
8101           TYPE_NFIELDS (this_type) = num_fields;
8102           TYPE_FIELDS (this_type) = (struct field *)
8103             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8104           memcpy (TYPE_FIELDS (this_type), fields,
8105                   sizeof (struct field) * num_fields);
8106           xfree (fields);
8107         }
8108       if (unsigned_enum)
8109         TYPE_UNSIGNED (this_type) = 1;
8110     }
8111
8112   /* If we are reading an enum from a .debug_types unit, and the enum
8113      is a declaration, and the enum is not the signatured type in the
8114      unit, then we do not want to add a symbol for it.  Adding a
8115      symbol would in some cases obscure the true definition of the
8116      enum, giving users an incomplete type when the definition is
8117      actually available.  Note that we do not want to do this for all
8118      enums which are just declarations, because C++0x allows forward
8119      enum declarations.  */
8120   if (cu->per_cu->debug_types_section
8121       && die_is_declaration (die, cu))
8122     {
8123       struct signatured_type *type_sig;
8124
8125       type_sig
8126         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8127                                             cu->per_cu->debug_types_section,
8128                                             cu->per_cu->offset);
8129       if (type_sig->type_offset != die->offset)
8130         return;
8131     }
8132
8133   new_symbol (die, this_type, cu);
8134 }
8135
8136 /* Extract all information from a DW_TAG_array_type DIE and put it in
8137    the DIE's type field.  For now, this only handles one dimensional
8138    arrays.  */
8139
8140 static struct type *
8141 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8142 {
8143   struct objfile *objfile = cu->objfile;
8144   struct die_info *child_die;
8145   struct type *type;
8146   struct type *element_type, *range_type, *index_type;
8147   struct type **range_types = NULL;
8148   struct attribute *attr;
8149   int ndim = 0;
8150   struct cleanup *back_to;
8151   char *name;
8152
8153   element_type = die_type (die, cu);
8154
8155   /* The die_type call above may have already set the type for this DIE.  */
8156   type = get_die_type (die, cu);
8157   if (type)
8158     return type;
8159
8160   /* Irix 6.2 native cc creates array types without children for
8161      arrays with unspecified length.  */
8162   if (die->child == NULL)
8163     {
8164       index_type = objfile_type (objfile)->builtin_int;
8165       range_type = create_range_type (NULL, index_type, 0, -1);
8166       type = create_array_type (NULL, element_type, range_type);
8167       return set_die_type (die, type, cu);
8168     }
8169
8170   back_to = make_cleanup (null_cleanup, NULL);
8171   child_die = die->child;
8172   while (child_die && child_die->tag)
8173     {
8174       if (child_die->tag == DW_TAG_subrange_type)
8175         {
8176           struct type *child_type = read_type_die (child_die, cu);
8177
8178           if (child_type != NULL)
8179             {
8180               /* The range type was succesfully read.  Save it for the
8181                  array type creation.  */
8182               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8183                 {
8184                   range_types = (struct type **)
8185                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8186                               * sizeof (struct type *));
8187                   if (ndim == 0)
8188                     make_cleanup (free_current_contents, &range_types);
8189                 }
8190               range_types[ndim++] = child_type;
8191             }
8192         }
8193       child_die = sibling_die (child_die);
8194     }
8195
8196   /* Dwarf2 dimensions are output from left to right, create the
8197      necessary array types in backwards order.  */
8198
8199   type = element_type;
8200
8201   if (read_array_order (die, cu) == DW_ORD_col_major)
8202     {
8203       int i = 0;
8204
8205       while (i < ndim)
8206         type = create_array_type (NULL, type, range_types[i++]);
8207     }
8208   else
8209     {
8210       while (ndim-- > 0)
8211         type = create_array_type (NULL, type, range_types[ndim]);
8212     }
8213
8214   /* Understand Dwarf2 support for vector types (like they occur on
8215      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8216      array type.  This is not part of the Dwarf2/3 standard yet, but a
8217      custom vendor extension.  The main difference between a regular
8218      array and the vector variant is that vectors are passed by value
8219      to functions.  */
8220   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8221   if (attr)
8222     make_vector_type (type);
8223
8224   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8225      implementation may choose to implement triple vectors using this
8226      attribute.  */
8227   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8228   if (attr)
8229     {
8230       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8231         TYPE_LENGTH (type) = DW_UNSND (attr);
8232       else
8233         complaint (&symfile_complaints,
8234                    _("DW_AT_byte_size for array type smaller "
8235                      "than the total size of elements"));
8236     }
8237
8238   name = dwarf2_name (die, cu);
8239   if (name)
8240     TYPE_NAME (type) = name;
8241
8242   /* Install the type in the die.  */
8243   set_die_type (die, type, cu);
8244
8245   /* set_die_type should be already done.  */
8246   set_descriptive_type (type, die, cu);
8247
8248   do_cleanups (back_to);
8249
8250   return type;
8251 }
8252
8253 static enum dwarf_array_dim_ordering
8254 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8255 {
8256   struct attribute *attr;
8257
8258   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8259
8260   if (attr) return DW_SND (attr);
8261
8262   /* GNU F77 is a special case, as at 08/2004 array type info is the
8263      opposite order to the dwarf2 specification, but data is still
8264      laid out as per normal fortran.
8265
8266      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8267      version checking.  */
8268
8269   if (cu->language == language_fortran
8270       && cu->producer && strstr (cu->producer, "GNU F77"))
8271     {
8272       return DW_ORD_row_major;
8273     }
8274
8275   switch (cu->language_defn->la_array_ordering)
8276     {
8277     case array_column_major:
8278       return DW_ORD_col_major;
8279     case array_row_major:
8280     default:
8281       return DW_ORD_row_major;
8282     };
8283 }
8284
8285 /* Extract all information from a DW_TAG_set_type DIE and put it in
8286    the DIE's type field.  */
8287
8288 static struct type *
8289 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8290 {
8291   struct type *domain_type, *set_type;
8292   struct attribute *attr;
8293
8294   domain_type = die_type (die, cu);
8295
8296   /* The die_type call above may have already set the type for this DIE.  */
8297   set_type = get_die_type (die, cu);
8298   if (set_type)
8299     return set_type;
8300
8301   set_type = create_set_type (NULL, domain_type);
8302
8303   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8304   if (attr)
8305     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8306
8307   return set_die_type (die, set_type, cu);
8308 }
8309
8310 /* First cut: install each common block member as a global variable.  */
8311
8312 static void
8313 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8314 {
8315   struct die_info *child_die;
8316   struct attribute *attr;
8317   struct symbol *sym;
8318   CORE_ADDR base = (CORE_ADDR) 0;
8319
8320   attr = dwarf2_attr (die, DW_AT_location, cu);
8321   if (attr)
8322     {
8323       /* Support the .debug_loc offsets.  */
8324       if (attr_form_is_block (attr))
8325         {
8326           base = decode_locdesc (DW_BLOCK (attr), cu);
8327         }
8328       else if (attr_form_is_section_offset (attr))
8329         {
8330           dwarf2_complex_location_expr_complaint ();
8331         }
8332       else
8333         {
8334           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8335                                                  "common block member");
8336         }
8337     }
8338   if (die->child != NULL)
8339     {
8340       child_die = die->child;
8341       while (child_die && child_die->tag)
8342         {
8343           LONGEST offset;
8344
8345           sym = new_symbol (child_die, NULL, cu);
8346           if (sym != NULL
8347               && handle_data_member_location (child_die, cu, &offset))
8348             {
8349               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8350               add_symbol_to_list (sym, &global_symbols);
8351             }
8352           child_die = sibling_die (child_die);
8353         }
8354     }
8355 }
8356
8357 /* Create a type for a C++ namespace.  */
8358
8359 static struct type *
8360 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8361 {
8362   struct objfile *objfile = cu->objfile;
8363   const char *previous_prefix, *name;
8364   int is_anonymous;
8365   struct type *type;
8366
8367   /* For extensions, reuse the type of the original namespace.  */
8368   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8369     {
8370       struct die_info *ext_die;
8371       struct dwarf2_cu *ext_cu = cu;
8372
8373       ext_die = dwarf2_extension (die, &ext_cu);
8374       type = read_type_die (ext_die, ext_cu);
8375
8376       /* EXT_CU may not be the same as CU.
8377          Ensure TYPE is recorded in CU's type_hash table.  */
8378       return set_die_type (die, type, cu);
8379     }
8380
8381   name = namespace_name (die, &is_anonymous, cu);
8382
8383   /* Now build the name of the current namespace.  */
8384
8385   previous_prefix = determine_prefix (die, cu);
8386   if (previous_prefix[0] != '\0')
8387     name = typename_concat (&objfile->objfile_obstack,
8388                             previous_prefix, name, 0, cu);
8389
8390   /* Create the type.  */
8391   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8392                     objfile);
8393   TYPE_NAME (type) = (char *) name;
8394   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8395
8396   return set_die_type (die, type, cu);
8397 }
8398
8399 /* Read a C++ namespace.  */
8400
8401 static void
8402 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8403 {
8404   struct objfile *objfile = cu->objfile;
8405   int is_anonymous;
8406
8407   /* Add a symbol associated to this if we haven't seen the namespace
8408      before.  Also, add a using directive if it's an anonymous
8409      namespace.  */
8410
8411   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8412     {
8413       struct type *type;
8414
8415       type = read_type_die (die, cu);
8416       new_symbol (die, type, cu);
8417
8418       namespace_name (die, &is_anonymous, cu);
8419       if (is_anonymous)
8420         {
8421           const char *previous_prefix = determine_prefix (die, cu);
8422
8423           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8424                                   NULL, NULL, &objfile->objfile_obstack);
8425         }
8426     }
8427
8428   if (die->child != NULL)
8429     {
8430       struct die_info *child_die = die->child;
8431
8432       while (child_die && child_die->tag)
8433         {
8434           process_die (child_die, cu);
8435           child_die = sibling_die (child_die);
8436         }
8437     }
8438 }
8439
8440 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8441    imported module.  Still we need that type as local Fortran "use ... only"
8442    declaration imports depend on the created type in determine_prefix.  */
8443
8444 static struct type *
8445 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8446 {
8447   struct objfile *objfile = cu->objfile;
8448   char *module_name;
8449   struct type *type;
8450
8451   module_name = dwarf2_name (die, cu);
8452   if (!module_name)
8453     complaint (&symfile_complaints,
8454                _("DW_TAG_module has no name, offset 0x%x"),
8455                die->offset);
8456   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8457
8458   /* determine_prefix uses TYPE_TAG_NAME.  */
8459   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8460
8461   return set_die_type (die, type, cu);
8462 }
8463
8464 /* Read a Fortran module.  */
8465
8466 static void
8467 read_module (struct die_info *die, struct dwarf2_cu *cu)
8468 {
8469   struct die_info *child_die = die->child;
8470
8471   while (child_die && child_die->tag)
8472     {
8473       process_die (child_die, cu);
8474       child_die = sibling_die (child_die);
8475     }
8476 }
8477
8478 /* Return the name of the namespace represented by DIE.  Set
8479    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8480    namespace.  */
8481
8482 static const char *
8483 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8484 {
8485   struct die_info *current_die;
8486   const char *name = NULL;
8487
8488   /* Loop through the extensions until we find a name.  */
8489
8490   for (current_die = die;
8491        current_die != NULL;
8492        current_die = dwarf2_extension (die, &cu))
8493     {
8494       name = dwarf2_name (current_die, cu);
8495       if (name != NULL)
8496         break;
8497     }
8498
8499   /* Is it an anonymous namespace?  */
8500
8501   *is_anonymous = (name == NULL);
8502   if (*is_anonymous)
8503     name = CP_ANONYMOUS_NAMESPACE_STR;
8504
8505   return name;
8506 }
8507
8508 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8509    the user defined type vector.  */
8510
8511 static struct type *
8512 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8513 {
8514   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8515   struct comp_unit_head *cu_header = &cu->header;
8516   struct type *type;
8517   struct attribute *attr_byte_size;
8518   struct attribute *attr_address_class;
8519   int byte_size, addr_class;
8520   struct type *target_type;
8521
8522   target_type = die_type (die, cu);
8523
8524   /* The die_type call above may have already set the type for this DIE.  */
8525   type = get_die_type (die, cu);
8526   if (type)
8527     return type;
8528
8529   type = lookup_pointer_type (target_type);
8530
8531   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8532   if (attr_byte_size)
8533     byte_size = DW_UNSND (attr_byte_size);
8534   else
8535     byte_size = cu_header->addr_size;
8536
8537   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8538   if (attr_address_class)
8539     addr_class = DW_UNSND (attr_address_class);
8540   else
8541     addr_class = DW_ADDR_none;
8542
8543   /* If the pointer size or address class is different than the
8544      default, create a type variant marked as such and set the
8545      length accordingly.  */
8546   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8547     {
8548       if (gdbarch_address_class_type_flags_p (gdbarch))
8549         {
8550           int type_flags;
8551
8552           type_flags = gdbarch_address_class_type_flags
8553                          (gdbarch, byte_size, addr_class);
8554           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8555                       == 0);
8556           type = make_type_with_address_space (type, type_flags);
8557         }
8558       else if (TYPE_LENGTH (type) != byte_size)
8559         {
8560           complaint (&symfile_complaints,
8561                      _("invalid pointer size %d"), byte_size);
8562         }
8563       else
8564         {
8565           /* Should we also complain about unhandled address classes?  */
8566         }
8567     }
8568
8569   TYPE_LENGTH (type) = byte_size;
8570   return set_die_type (die, type, cu);
8571 }
8572
8573 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8574    the user defined type vector.  */
8575
8576 static struct type *
8577 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8578 {
8579   struct type *type;
8580   struct type *to_type;
8581   struct type *domain;
8582
8583   to_type = die_type (die, cu);
8584   domain = die_containing_type (die, cu);
8585
8586   /* The calls above may have already set the type for this DIE.  */
8587   type = get_die_type (die, cu);
8588   if (type)
8589     return type;
8590
8591   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8592     type = lookup_methodptr_type (to_type);
8593   else
8594     type = lookup_memberptr_type (to_type, domain);
8595
8596   return set_die_type (die, type, cu);
8597 }
8598
8599 /* Extract all information from a DW_TAG_reference_type DIE and add to
8600    the user defined type vector.  */
8601
8602 static struct type *
8603 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8604 {
8605   struct comp_unit_head *cu_header = &cu->header;
8606   struct type *type, *target_type;
8607   struct attribute *attr;
8608
8609   target_type = die_type (die, cu);
8610
8611   /* The die_type call above may have already set the type for this DIE.  */
8612   type = get_die_type (die, cu);
8613   if (type)
8614     return type;
8615
8616   type = lookup_reference_type (target_type);
8617   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8618   if (attr)
8619     {
8620       TYPE_LENGTH (type) = DW_UNSND (attr);
8621     }
8622   else
8623     {
8624       TYPE_LENGTH (type) = cu_header->addr_size;
8625     }
8626   return set_die_type (die, type, cu);
8627 }
8628
8629 static struct type *
8630 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8631 {
8632   struct type *base_type, *cv_type;
8633
8634   base_type = die_type (die, cu);
8635
8636   /* The die_type call above may have already set the type for this DIE.  */
8637   cv_type = get_die_type (die, cu);
8638   if (cv_type)
8639     return cv_type;
8640
8641   /* In case the const qualifier is applied to an array type, the element type
8642      is so qualified, not the array type (section 6.7.3 of C99).  */
8643   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8644     {
8645       struct type *el_type, *inner_array;
8646
8647       base_type = copy_type (base_type);
8648       inner_array = base_type;
8649
8650       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8651         {
8652           TYPE_TARGET_TYPE (inner_array) =
8653             copy_type (TYPE_TARGET_TYPE (inner_array));
8654           inner_array = TYPE_TARGET_TYPE (inner_array);
8655         }
8656
8657       el_type = TYPE_TARGET_TYPE (inner_array);
8658       TYPE_TARGET_TYPE (inner_array) =
8659         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8660
8661       return set_die_type (die, base_type, cu);
8662     }
8663
8664   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8665   return set_die_type (die, cv_type, cu);
8666 }
8667
8668 static struct type *
8669 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8670 {
8671   struct type *base_type, *cv_type;
8672
8673   base_type = die_type (die, cu);
8674
8675   /* The die_type call above may have already set the type for this DIE.  */
8676   cv_type = get_die_type (die, cu);
8677   if (cv_type)
8678     return cv_type;
8679
8680   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8681   return set_die_type (die, cv_type, cu);
8682 }
8683
8684 /* Extract all information from a DW_TAG_string_type DIE and add to
8685    the user defined type vector.  It isn't really a user defined type,
8686    but it behaves like one, with other DIE's using an AT_user_def_type
8687    attribute to reference it.  */
8688
8689 static struct type *
8690 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8691 {
8692   struct objfile *objfile = cu->objfile;
8693   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8694   struct type *type, *range_type, *index_type, *char_type;
8695   struct attribute *attr;
8696   unsigned int length;
8697
8698   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8699   if (attr)
8700     {
8701       length = DW_UNSND (attr);
8702     }
8703   else
8704     {
8705       /* Check for the DW_AT_byte_size attribute.  */
8706       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8707       if (attr)
8708         {
8709           length = DW_UNSND (attr);
8710         }
8711       else
8712         {
8713           length = 1;
8714         }
8715     }
8716
8717   index_type = objfile_type (objfile)->builtin_int;
8718   range_type = create_range_type (NULL, index_type, 1, length);
8719   char_type = language_string_char_type (cu->language_defn, gdbarch);
8720   type = create_string_type (NULL, char_type, range_type);
8721
8722   return set_die_type (die, type, cu);
8723 }
8724
8725 /* Handle DIES due to C code like:
8726
8727    struct foo
8728    {
8729    int (*funcp)(int a, long l);
8730    int b;
8731    };
8732
8733    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8734
8735 static struct type *
8736 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8737 {
8738   struct type *type;            /* Type that this function returns.  */
8739   struct type *ftype;           /* Function that returns above type.  */
8740   struct attribute *attr;
8741
8742   type = die_type (die, cu);
8743
8744   /* The die_type call above may have already set the type for this DIE.  */
8745   ftype = get_die_type (die, cu);
8746   if (ftype)
8747     return ftype;
8748
8749   ftype = lookup_function_type (type);
8750
8751   /* All functions in C++, Pascal and Java have prototypes.  */
8752   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8753   if ((attr && (DW_UNSND (attr) != 0))
8754       || cu->language == language_cplus
8755       || cu->language == language_java
8756       || cu->language == language_pascal)
8757     TYPE_PROTOTYPED (ftype) = 1;
8758   else if (producer_is_realview (cu->producer))
8759     /* RealView does not emit DW_AT_prototyped.  We can not
8760        distinguish prototyped and unprototyped functions; default to
8761        prototyped, since that is more common in modern code (and
8762        RealView warns about unprototyped functions).  */
8763     TYPE_PROTOTYPED (ftype) = 1;
8764
8765   /* Store the calling convention in the type if it's available in
8766      the subroutine die.  Otherwise set the calling convention to
8767      the default value DW_CC_normal.  */
8768   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8769   if (attr)
8770     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8771   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8772     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8773   else
8774     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8775
8776   /* We need to add the subroutine type to the die immediately so
8777      we don't infinitely recurse when dealing with parameters
8778      declared as the same subroutine type.  */
8779   set_die_type (die, ftype, cu);
8780
8781   if (die->child != NULL)
8782     {
8783       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8784       struct die_info *child_die;
8785       int nparams, iparams;
8786
8787       /* Count the number of parameters.
8788          FIXME: GDB currently ignores vararg functions, but knows about
8789          vararg member functions.  */
8790       nparams = 0;
8791       child_die = die->child;
8792       while (child_die && child_die->tag)
8793         {
8794           if (child_die->tag == DW_TAG_formal_parameter)
8795             nparams++;
8796           else if (child_die->tag == DW_TAG_unspecified_parameters)
8797             TYPE_VARARGS (ftype) = 1;
8798           child_die = sibling_die (child_die);
8799         }
8800
8801       /* Allocate storage for parameters and fill them in.  */
8802       TYPE_NFIELDS (ftype) = nparams;
8803       TYPE_FIELDS (ftype) = (struct field *)
8804         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8805
8806       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8807          even if we error out during the parameters reading below.  */
8808       for (iparams = 0; iparams < nparams; iparams++)
8809         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8810
8811       iparams = 0;
8812       child_die = die->child;
8813       while (child_die && child_die->tag)
8814         {
8815           if (child_die->tag == DW_TAG_formal_parameter)
8816             {
8817               struct type *arg_type;
8818
8819               /* DWARF version 2 has no clean way to discern C++
8820                  static and non-static member functions.  G++ helps
8821                  GDB by marking the first parameter for non-static
8822                  member functions (which is the this pointer) as
8823                  artificial.  We pass this information to
8824                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8825
8826                  DWARF version 3 added DW_AT_object_pointer, which GCC
8827                  4.5 does not yet generate.  */
8828               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8829               if (attr)
8830                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8831               else
8832                 {
8833                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8834
8835                   /* GCC/43521: In java, the formal parameter
8836                      "this" is sometimes not marked with DW_AT_artificial.  */
8837                   if (cu->language == language_java)
8838                     {
8839                       const char *name = dwarf2_name (child_die, cu);
8840
8841                       if (name && !strcmp (name, "this"))
8842                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8843                     }
8844                 }
8845               arg_type = die_type (child_die, cu);
8846
8847               /* RealView does not mark THIS as const, which the testsuite
8848                  expects.  GCC marks THIS as const in method definitions,
8849                  but not in the class specifications (GCC PR 43053).  */
8850               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8851                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8852                 {
8853                   int is_this = 0;
8854                   struct dwarf2_cu *arg_cu = cu;
8855                   const char *name = dwarf2_name (child_die, cu);
8856
8857                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8858                   if (attr)
8859                     {
8860                       /* If the compiler emits this, use it.  */
8861                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8862                         is_this = 1;
8863                     }
8864                   else if (name && strcmp (name, "this") == 0)
8865                     /* Function definitions will have the argument names.  */
8866                     is_this = 1;
8867                   else if (name == NULL && iparams == 0)
8868                     /* Declarations may not have the names, so like
8869                        elsewhere in GDB, assume an artificial first
8870                        argument is "this".  */
8871                     is_this = 1;
8872
8873                   if (is_this)
8874                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8875                                              arg_type, 0);
8876                 }
8877
8878               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8879               iparams++;
8880             }
8881           child_die = sibling_die (child_die);
8882         }
8883     }
8884
8885   return ftype;
8886 }
8887
8888 static struct type *
8889 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8890 {
8891   struct objfile *objfile = cu->objfile;
8892   const char *name = NULL;
8893   struct type *this_type;
8894
8895   name = dwarf2_full_name (NULL, die, cu);
8896   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8897                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8898   TYPE_NAME (this_type) = (char *) name;
8899   set_die_type (die, this_type, cu);
8900   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8901   return this_type;
8902 }
8903
8904 /* Find a representation of a given base type and install
8905    it in the TYPE field of the die.  */
8906
8907 static struct type *
8908 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8909 {
8910   struct objfile *objfile = cu->objfile;
8911   struct type *type;
8912   struct attribute *attr;
8913   int encoding = 0, size = 0;
8914   char *name;
8915   enum type_code code = TYPE_CODE_INT;
8916   int type_flags = 0;
8917   struct type *target_type = NULL;
8918
8919   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8920   if (attr)
8921     {
8922       encoding = DW_UNSND (attr);
8923     }
8924   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8925   if (attr)
8926     {
8927       size = DW_UNSND (attr);
8928     }
8929   name = dwarf2_name (die, cu);
8930   if (!name)
8931     {
8932       complaint (&symfile_complaints,
8933                  _("DW_AT_name missing from DW_TAG_base_type"));
8934     }
8935
8936   switch (encoding)
8937     {
8938       case DW_ATE_address:
8939         /* Turn DW_ATE_address into a void * pointer.  */
8940         code = TYPE_CODE_PTR;
8941         type_flags |= TYPE_FLAG_UNSIGNED;
8942         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8943         break;
8944       case DW_ATE_boolean:
8945         code = TYPE_CODE_BOOL;
8946         type_flags |= TYPE_FLAG_UNSIGNED;
8947         break;
8948       case DW_ATE_complex_float:
8949         code = TYPE_CODE_COMPLEX;
8950         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8951         break;
8952       case DW_ATE_decimal_float:
8953         code = TYPE_CODE_DECFLOAT;
8954         break;
8955       case DW_ATE_float:
8956         code = TYPE_CODE_FLT;
8957         break;
8958       case DW_ATE_signed:
8959         break;
8960       case DW_ATE_unsigned:
8961         type_flags |= TYPE_FLAG_UNSIGNED;
8962         if (cu->language == language_fortran
8963             && name
8964             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8965           code = TYPE_CODE_CHAR;
8966         break;
8967       case DW_ATE_signed_char:
8968         if (cu->language == language_ada || cu->language == language_m2
8969             || cu->language == language_pascal
8970             || cu->language == language_fortran)
8971           code = TYPE_CODE_CHAR;
8972         break;
8973       case DW_ATE_unsigned_char:
8974         if (cu->language == language_ada || cu->language == language_m2
8975             || cu->language == language_pascal
8976             || cu->language == language_fortran)
8977           code = TYPE_CODE_CHAR;
8978         type_flags |= TYPE_FLAG_UNSIGNED;
8979         break;
8980       case DW_ATE_UTF:
8981         /* We just treat this as an integer and then recognize the
8982            type by name elsewhere.  */
8983         break;
8984
8985       default:
8986         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8987                    dwarf_type_encoding_name (encoding));
8988         break;
8989     }
8990
8991   type = init_type (code, size, type_flags, NULL, objfile);
8992   TYPE_NAME (type) = name;
8993   TYPE_TARGET_TYPE (type) = target_type;
8994
8995   if (name && strcmp (name, "char") == 0)
8996     TYPE_NOSIGN (type) = 1;
8997
8998   return set_die_type (die, type, cu);
8999 }
9000
9001 /* Read the given DW_AT_subrange DIE.  */
9002
9003 static struct type *
9004 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
9005 {
9006   struct type *base_type;
9007   struct type *range_type;
9008   struct attribute *attr;
9009   LONGEST low = 0;
9010   LONGEST high = -1;
9011   char *name;
9012   LONGEST negative_mask;
9013
9014   base_type = die_type (die, cu);
9015   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
9016   check_typedef (base_type);
9017
9018   /* The die_type call above may have already set the type for this DIE.  */
9019   range_type = get_die_type (die, cu);
9020   if (range_type)
9021     return range_type;
9022
9023   if (cu->language == language_fortran)
9024     {
9025       /* FORTRAN implies a lower bound of 1, if not given.  */
9026       low = 1;
9027     }
9028
9029   /* FIXME: For variable sized arrays either of these could be
9030      a variable rather than a constant value.  We'll allow it,
9031      but we don't know how to handle it.  */
9032   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
9033   if (attr)
9034     low = dwarf2_get_attr_constant_value (attr, 0);
9035
9036   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
9037   if (attr)
9038     {
9039       if (attr_form_is_block (attr) || is_ref_attr (attr))
9040         {
9041           /* GCC encodes arrays with unspecified or dynamic length
9042              with a DW_FORM_block1 attribute or a reference attribute.
9043              FIXME: GDB does not yet know how to handle dynamic
9044              arrays properly, treat them as arrays with unspecified
9045              length for now.
9046
9047              FIXME: jimb/2003-09-22: GDB does not really know
9048              how to handle arrays of unspecified length
9049              either; we just represent them as zero-length
9050              arrays.  Choose an appropriate upper bound given
9051              the lower bound we've computed above.  */
9052           high = low - 1;
9053         }
9054       else
9055         high = dwarf2_get_attr_constant_value (attr, 1);
9056     }
9057   else
9058     {
9059       attr = dwarf2_attr (die, DW_AT_count, cu);
9060       if (attr)
9061         {
9062           int count = dwarf2_get_attr_constant_value (attr, 1);
9063           high = low + count - 1;
9064         }
9065       else
9066         {
9067           /* Unspecified array length.  */
9068           high = low - 1;
9069         }
9070     }
9071
9072   /* Dwarf-2 specifications explicitly allows to create subrange types
9073      without specifying a base type.
9074      In that case, the base type must be set to the type of
9075      the lower bound, upper bound or count, in that order, if any of these
9076      three attributes references an object that has a type.
9077      If no base type is found, the Dwarf-2 specifications say that
9078      a signed integer type of size equal to the size of an address should
9079      be used.
9080      For the following C code: `extern char gdb_int [];'
9081      GCC produces an empty range DIE.
9082      FIXME: muller/2010-05-28: Possible references to object for low bound,
9083      high bound or count are not yet handled by this code.  */
9084   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9085     {
9086       struct objfile *objfile = cu->objfile;
9087       struct gdbarch *gdbarch = get_objfile_arch (objfile);
9088       int addr_size = gdbarch_addr_bit (gdbarch) /8;
9089       struct type *int_type = objfile_type (objfile)->builtin_int;
9090
9091       /* Test "int", "long int", and "long long int" objfile types,
9092          and select the first one having a size above or equal to the
9093          architecture address size.  */
9094       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9095         base_type = int_type;
9096       else
9097         {
9098           int_type = objfile_type (objfile)->builtin_long;
9099           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9100             base_type = int_type;
9101           else
9102             {
9103               int_type = objfile_type (objfile)->builtin_long_long;
9104               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9105                 base_type = int_type;
9106             }
9107         }
9108     }
9109
9110   negative_mask =
9111     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9112   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9113     low |= negative_mask;
9114   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9115     high |= negative_mask;
9116
9117   range_type = create_range_type (NULL, base_type, low, high);
9118
9119   /* Mark arrays with dynamic length at least as an array of unspecified
9120      length.  GDB could check the boundary but before it gets implemented at
9121      least allow accessing the array elements.  */
9122   if (attr && attr_form_is_block (attr))
9123     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9124
9125   /* Ada expects an empty array on no boundary attributes.  */
9126   if (attr == NULL && cu->language != language_ada)
9127     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9128
9129   name = dwarf2_name (die, cu);
9130   if (name)
9131     TYPE_NAME (range_type) = name;
9132
9133   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9134   if (attr)
9135     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9136
9137   set_die_type (die, range_type, cu);
9138
9139   /* set_die_type should be already done.  */
9140   set_descriptive_type (range_type, die, cu);
9141
9142   return range_type;
9143 }
9144
9145 static struct type *
9146 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9147 {
9148   struct type *type;
9149
9150   /* For now, we only support the C meaning of an unspecified type: void.  */
9151
9152   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9153   TYPE_NAME (type) = dwarf2_name (die, cu);
9154
9155   return set_die_type (die, type, cu);
9156 }
9157
9158 /* Trivial hash function for die_info: the hash value of a DIE
9159    is its offset in .debug_info for this objfile.  */
9160
9161 static hashval_t
9162 die_hash (const void *item)
9163 {
9164   const struct die_info *die = item;
9165
9166   return die->offset;
9167 }
9168
9169 /* Trivial comparison function for die_info structures: two DIEs
9170    are equal if they have the same offset.  */
9171
9172 static int
9173 die_eq (const void *item_lhs, const void *item_rhs)
9174 {
9175   const struct die_info *die_lhs = item_lhs;
9176   const struct die_info *die_rhs = item_rhs;
9177
9178   return die_lhs->offset == die_rhs->offset;
9179 }
9180
9181 /* Read a whole compilation unit into a linked list of dies.  */
9182
9183 static struct die_info *
9184 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9185 {
9186   struct die_reader_specs reader_specs;
9187   int read_abbrevs = 0;
9188   struct cleanup *back_to = NULL;
9189   struct die_info *die;
9190
9191   if (cu->dwarf2_abbrevs == NULL)
9192     {
9193       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9194       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9195       read_abbrevs = 1;
9196     }
9197
9198   gdb_assert (cu->die_hash == NULL);
9199   cu->die_hash
9200     = htab_create_alloc_ex (cu->header.length / 12,
9201                             die_hash,
9202                             die_eq,
9203                             NULL,
9204                             &cu->comp_unit_obstack,
9205                             hashtab_obstack_allocate,
9206                             dummy_obstack_deallocate);
9207
9208   init_cu_die_reader (&reader_specs, cu);
9209
9210   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9211
9212   if (read_abbrevs)
9213     do_cleanups (back_to);
9214
9215   return die;
9216 }
9217
9218 /* Main entry point for reading a DIE and all children.
9219    Read the DIE and dump it if requested.  */
9220
9221 static struct die_info *
9222 read_die_and_children (const struct die_reader_specs *reader,
9223                        gdb_byte *info_ptr,
9224                        gdb_byte **new_info_ptr,
9225                        struct die_info *parent)
9226 {
9227   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9228                                                      new_info_ptr, parent);
9229
9230   if (dwarf2_die_debug)
9231     {
9232       fprintf_unfiltered (gdb_stdlog,
9233                           "\nRead die from %s of %s:\n",
9234                           (reader->cu->per_cu->debug_types_section
9235                            ? ".debug_types"
9236                            : ".debug_info"),
9237                           reader->abfd->filename);
9238       dump_die (result, dwarf2_die_debug);
9239     }
9240
9241   return result;
9242 }
9243
9244 /* Read a single die and all its descendents.  Set the die's sibling
9245    field to NULL; set other fields in the die correctly, and set all
9246    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9247    location of the info_ptr after reading all of those dies.  PARENT
9248    is the parent of the die in question.  */
9249
9250 static struct die_info *
9251 read_die_and_children_1 (const struct die_reader_specs *reader,
9252                          gdb_byte *info_ptr,
9253                          gdb_byte **new_info_ptr,
9254                          struct die_info *parent)
9255 {
9256   struct die_info *die;
9257   gdb_byte *cur_ptr;
9258   int has_children;
9259
9260   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9261   if (die == NULL)
9262     {
9263       *new_info_ptr = cur_ptr;
9264       return NULL;
9265     }
9266   store_in_ref_table (die, reader->cu);
9267
9268   if (has_children)
9269     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9270   else
9271     {
9272       die->child = NULL;
9273       *new_info_ptr = cur_ptr;
9274     }
9275
9276   die->sibling = NULL;
9277   die->parent = parent;
9278   return die;
9279 }
9280
9281 /* Read a die, all of its descendents, and all of its siblings; set
9282    all of the fields of all of the dies correctly.  Arguments are as
9283    in read_die_and_children.  */
9284
9285 static struct die_info *
9286 read_die_and_siblings (const struct die_reader_specs *reader,
9287                        gdb_byte *info_ptr,
9288                        gdb_byte **new_info_ptr,
9289                        struct die_info *parent)
9290 {
9291   struct die_info *first_die, *last_sibling;
9292   gdb_byte *cur_ptr;
9293
9294   cur_ptr = info_ptr;
9295   first_die = last_sibling = NULL;
9296
9297   while (1)
9298     {
9299       struct die_info *die
9300         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9301
9302       if (die == NULL)
9303         {
9304           *new_info_ptr = cur_ptr;
9305           return first_die;
9306         }
9307
9308       if (!first_die)
9309         first_die = die;
9310       else
9311         last_sibling->sibling = die;
9312
9313       last_sibling = die;
9314     }
9315 }
9316
9317 /* Read the die from the .debug_info section buffer.  Set DIEP to
9318    point to a newly allocated die with its information, except for its
9319    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9320    whether the die has children or not.  */
9321
9322 static gdb_byte *
9323 read_full_die (const struct die_reader_specs *reader,
9324                struct die_info **diep, gdb_byte *info_ptr,
9325                int *has_children)
9326 {
9327   unsigned int abbrev_number, bytes_read, i, offset;
9328   struct abbrev_info *abbrev;
9329   struct die_info *die;
9330   struct dwarf2_cu *cu = reader->cu;
9331   bfd *abfd = reader->abfd;
9332
9333   offset = info_ptr - reader->buffer;
9334   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9335   info_ptr += bytes_read;
9336   if (!abbrev_number)
9337     {
9338       *diep = NULL;
9339       *has_children = 0;
9340       return info_ptr;
9341     }
9342
9343   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9344   if (!abbrev)
9345     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9346            abbrev_number,
9347            bfd_get_filename (abfd));
9348
9349   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9350   die->offset = offset;
9351   die->tag = abbrev->tag;
9352   die->abbrev = abbrev_number;
9353
9354   die->num_attrs = abbrev->num_attrs;
9355
9356   for (i = 0; i < abbrev->num_attrs; ++i)
9357     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9358                                abfd, info_ptr, cu);
9359
9360   *diep = die;
9361   *has_children = abbrev->has_children;
9362   return info_ptr;
9363 }
9364
9365 /* In DWARF version 2, the description of the debugging information is
9366    stored in a separate .debug_abbrev section.  Before we read any
9367    dies from a section we read in all abbreviations and install them
9368    in a hash table.  This function also sets flags in CU describing
9369    the data found in the abbrev table.  */
9370
9371 static void
9372 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9373 {
9374   struct comp_unit_head *cu_header = &cu->header;
9375   gdb_byte *abbrev_ptr;
9376   struct abbrev_info *cur_abbrev;
9377   unsigned int abbrev_number, bytes_read, abbrev_name;
9378   unsigned int abbrev_form, hash_number;
9379   struct attr_abbrev *cur_attrs;
9380   unsigned int allocated_attrs;
9381
9382   /* Initialize dwarf2 abbrevs.  */
9383   obstack_init (&cu->abbrev_obstack);
9384   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9385                                       (ABBREV_HASH_SIZE
9386                                        * sizeof (struct abbrev_info *)));
9387   memset (cu->dwarf2_abbrevs, 0,
9388           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9389
9390   dwarf2_read_section (dwarf2_per_objfile->objfile,
9391                        &dwarf2_per_objfile->abbrev);
9392   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9393   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9394   abbrev_ptr += bytes_read;
9395
9396   allocated_attrs = ATTR_ALLOC_CHUNK;
9397   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9398
9399   /* Loop until we reach an abbrev number of 0.  */
9400   while (abbrev_number)
9401     {
9402       cur_abbrev = dwarf_alloc_abbrev (cu);
9403
9404       /* read in abbrev header */
9405       cur_abbrev->number = abbrev_number;
9406       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9407       abbrev_ptr += bytes_read;
9408       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9409       abbrev_ptr += 1;
9410
9411       if (cur_abbrev->tag == DW_TAG_namespace)
9412         cu->has_namespace_info = 1;
9413
9414       /* now read in declarations */
9415       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9416       abbrev_ptr += bytes_read;
9417       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9418       abbrev_ptr += bytes_read;
9419       while (abbrev_name)
9420         {
9421           if (cur_abbrev->num_attrs == allocated_attrs)
9422             {
9423               allocated_attrs += ATTR_ALLOC_CHUNK;
9424               cur_attrs
9425                 = xrealloc (cur_attrs, (allocated_attrs
9426                                         * sizeof (struct attr_abbrev)));
9427             }
9428
9429           /* Record whether this compilation unit might have
9430              inter-compilation-unit references.  If we don't know what form
9431              this attribute will have, then it might potentially be a
9432              DW_FORM_ref_addr, so we conservatively expect inter-CU
9433              references.  */
9434
9435           if (abbrev_form == DW_FORM_ref_addr
9436               || abbrev_form == DW_FORM_indirect)
9437             cu->has_form_ref_addr = 1;
9438
9439           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9440           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9441           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9442           abbrev_ptr += bytes_read;
9443           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9444           abbrev_ptr += bytes_read;
9445         }
9446
9447       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9448                                          (cur_abbrev->num_attrs
9449                                           * sizeof (struct attr_abbrev)));
9450       memcpy (cur_abbrev->attrs, cur_attrs,
9451               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9452
9453       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9454       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9455       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9456
9457       /* Get next abbreviation.
9458          Under Irix6 the abbreviations for a compilation unit are not
9459          always properly terminated with an abbrev number of 0.
9460          Exit loop if we encounter an abbreviation which we have
9461          already read (which means we are about to read the abbreviations
9462          for the next compile unit) or if the end of the abbreviation
9463          table is reached.  */
9464       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9465           >= dwarf2_per_objfile->abbrev.size)
9466         break;
9467       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9468       abbrev_ptr += bytes_read;
9469       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9470         break;
9471     }
9472
9473   xfree (cur_attrs);
9474 }
9475
9476 /* Release the memory used by the abbrev table for a compilation unit.  */
9477
9478 static void
9479 dwarf2_free_abbrev_table (void *ptr_to_cu)
9480 {
9481   struct dwarf2_cu *cu = ptr_to_cu;
9482
9483   obstack_free (&cu->abbrev_obstack, NULL);
9484   cu->dwarf2_abbrevs = NULL;
9485 }
9486
9487 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9488
9489 static struct abbrev_info *
9490 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9491 {
9492   unsigned int hash_number;
9493   struct abbrev_info *abbrev;
9494
9495   hash_number = number % ABBREV_HASH_SIZE;
9496   abbrev = cu->dwarf2_abbrevs[hash_number];
9497
9498   while (abbrev)
9499     {
9500       if (abbrev->number == number)
9501         return abbrev;
9502       else
9503         abbrev = abbrev->next;
9504     }
9505   return NULL;
9506 }
9507
9508 /* Returns nonzero if TAG represents a type that we might generate a partial
9509    symbol for.  */
9510
9511 static int
9512 is_type_tag_for_partial (int tag)
9513 {
9514   switch (tag)
9515     {
9516 #if 0
9517     /* Some types that would be reasonable to generate partial symbols for,
9518        that we don't at present.  */
9519     case DW_TAG_array_type:
9520     case DW_TAG_file_type:
9521     case DW_TAG_ptr_to_member_type:
9522     case DW_TAG_set_type:
9523     case DW_TAG_string_type:
9524     case DW_TAG_subroutine_type:
9525 #endif
9526     case DW_TAG_base_type:
9527     case DW_TAG_class_type:
9528     case DW_TAG_interface_type:
9529     case DW_TAG_enumeration_type:
9530     case DW_TAG_structure_type:
9531     case DW_TAG_subrange_type:
9532     case DW_TAG_typedef:
9533     case DW_TAG_union_type:
9534       return 1;
9535     default:
9536       return 0;
9537     }
9538 }
9539
9540 /* Load all DIEs that are interesting for partial symbols into memory.  */
9541
9542 static struct partial_die_info *
9543 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9544                    int building_psymtab, struct dwarf2_cu *cu)
9545 {
9546   struct partial_die_info *part_die;
9547   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9548   struct abbrev_info *abbrev;
9549   unsigned int bytes_read;
9550   unsigned int load_all = 0;
9551
9552   int nesting_level = 1;
9553
9554   parent_die = NULL;
9555   last_die = NULL;
9556
9557   if (cu->per_cu && cu->per_cu->load_all_dies)
9558     load_all = 1;
9559
9560   cu->partial_dies
9561     = htab_create_alloc_ex (cu->header.length / 12,
9562                             partial_die_hash,
9563                             partial_die_eq,
9564                             NULL,
9565                             &cu->comp_unit_obstack,
9566                             hashtab_obstack_allocate,
9567                             dummy_obstack_deallocate);
9568
9569   part_die = obstack_alloc (&cu->comp_unit_obstack,
9570                             sizeof (struct partial_die_info));
9571
9572   while (1)
9573     {
9574       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9575
9576       /* A NULL abbrev means the end of a series of children.  */
9577       if (abbrev == NULL)
9578         {
9579           if (--nesting_level == 0)
9580             {
9581               /* PART_DIE was probably the last thing allocated on the
9582                  comp_unit_obstack, so we could call obstack_free
9583                  here.  We don't do that because the waste is small,
9584                  and will be cleaned up when we're done with this
9585                  compilation unit.  This way, we're also more robust
9586                  against other users of the comp_unit_obstack.  */
9587               return first_die;
9588             }
9589           info_ptr += bytes_read;
9590           last_die = parent_die;
9591           parent_die = parent_die->die_parent;
9592           continue;
9593         }
9594
9595       /* Check for template arguments.  We never save these; if
9596          they're seen, we just mark the parent, and go on our way.  */
9597       if (parent_die != NULL
9598           && cu->language == language_cplus
9599           && (abbrev->tag == DW_TAG_template_type_param
9600               || abbrev->tag == DW_TAG_template_value_param))
9601         {
9602           parent_die->has_template_arguments = 1;
9603
9604           if (!load_all)
9605             {
9606               /* We don't need a partial DIE for the template argument.  */
9607               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9608                                        cu);
9609               continue;
9610             }
9611         }
9612
9613       /* We only recurse into subprograms looking for template arguments.
9614          Skip their other children.  */
9615       if (!load_all
9616           && cu->language == language_cplus
9617           && parent_die != NULL
9618           && parent_die->tag == DW_TAG_subprogram)
9619         {
9620           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9621           continue;
9622         }
9623
9624       /* Check whether this DIE is interesting enough to save.  Normally
9625          we would not be interested in members here, but there may be
9626          later variables referencing them via DW_AT_specification (for
9627          static members).  */
9628       if (!load_all
9629           && !is_type_tag_for_partial (abbrev->tag)
9630           && abbrev->tag != DW_TAG_constant
9631           && abbrev->tag != DW_TAG_enumerator
9632           && abbrev->tag != DW_TAG_subprogram
9633           && abbrev->tag != DW_TAG_lexical_block
9634           && abbrev->tag != DW_TAG_variable
9635           && abbrev->tag != DW_TAG_namespace
9636           && abbrev->tag != DW_TAG_module
9637           && abbrev->tag != DW_TAG_member)
9638         {
9639           /* Otherwise we skip to the next sibling, if any.  */
9640           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9641           continue;
9642         }
9643
9644       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9645                                    buffer, info_ptr, cu);
9646
9647       /* This two-pass algorithm for processing partial symbols has a
9648          high cost in cache pressure.  Thus, handle some simple cases
9649          here which cover the majority of C partial symbols.  DIEs
9650          which neither have specification tags in them, nor could have
9651          specification tags elsewhere pointing at them, can simply be
9652          processed and discarded.
9653
9654          This segment is also optional; scan_partial_symbols and
9655          add_partial_symbol will handle these DIEs if we chain
9656          them in normally.  When compilers which do not emit large
9657          quantities of duplicate debug information are more common,
9658          this code can probably be removed.  */
9659
9660       /* Any complete simple types at the top level (pretty much all
9661          of them, for a language without namespaces), can be processed
9662          directly.  */
9663       if (parent_die == NULL
9664           && part_die->has_specification == 0
9665           && part_die->is_declaration == 0
9666           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9667               || part_die->tag == DW_TAG_base_type
9668               || part_die->tag == DW_TAG_subrange_type))
9669         {
9670           if (building_psymtab && part_die->name != NULL)
9671             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9672                                  VAR_DOMAIN, LOC_TYPEDEF,
9673                                  &cu->objfile->static_psymbols,
9674                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9675           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9676           continue;
9677         }
9678
9679       /* The exception for DW_TAG_typedef with has_children above is
9680          a workaround of GCC PR debug/47510.  In the case of this complaint
9681          type_name_no_tag_or_error will error on such types later.
9682
9683          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9684          it could not find the child DIEs referenced later, this is checked
9685          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9686
9687       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9688         complaint (&symfile_complaints,
9689                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9690                      "- DIE at 0x%x [in module %s]"),
9691                    part_die->offset, cu->objfile->name);
9692
9693       /* If we're at the second level, and we're an enumerator, and
9694          our parent has no specification (meaning possibly lives in a
9695          namespace elsewhere), then we can add the partial symbol now
9696          instead of queueing it.  */
9697       if (part_die->tag == DW_TAG_enumerator
9698           && parent_die != NULL
9699           && parent_die->die_parent == NULL
9700           && parent_die->tag == DW_TAG_enumeration_type
9701           && parent_die->has_specification == 0)
9702         {
9703           if (part_die->name == NULL)
9704             complaint (&symfile_complaints,
9705                        _("malformed enumerator DIE ignored"));
9706           else if (building_psymtab)
9707             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9708                                  VAR_DOMAIN, LOC_CONST,
9709                                  (cu->language == language_cplus
9710                                   || cu->language == language_java)
9711                                  ? &cu->objfile->global_psymbols
9712                                  : &cu->objfile->static_psymbols,
9713                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9714
9715           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9716           continue;
9717         }
9718
9719       /* We'll save this DIE so link it in.  */
9720       part_die->die_parent = parent_die;
9721       part_die->die_sibling = NULL;
9722       part_die->die_child = NULL;
9723
9724       if (last_die && last_die == parent_die)
9725         last_die->die_child = part_die;
9726       else if (last_die)
9727         last_die->die_sibling = part_die;
9728
9729       last_die = part_die;
9730
9731       if (first_die == NULL)
9732         first_die = part_die;
9733
9734       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9735          find interesting need to be in the hash table, because we
9736          also have the parent/sibling/child chains; only those that we
9737          might refer to by offset later during partial symbol reading.
9738
9739          For now this means things that might have be the target of a
9740          DW_AT_specification, DW_AT_abstract_origin, or
9741          DW_AT_extension.  DW_AT_extension will refer only to
9742          namespaces; DW_AT_abstract_origin refers to functions (and
9743          many things under the function DIE, but we do not recurse
9744          into function DIEs during partial symbol reading) and
9745          possibly variables as well; DW_AT_specification refers to
9746          declarations.  Declarations ought to have the DW_AT_declaration
9747          flag.  It happens that GCC forgets to put it in sometimes, but
9748          only for functions, not for types.
9749
9750          Adding more things than necessary to the hash table is harmless
9751          except for the performance cost.  Adding too few will result in
9752          wasted time in find_partial_die, when we reread the compilation
9753          unit with load_all_dies set.  */
9754
9755       if (load_all
9756           || abbrev->tag == DW_TAG_constant
9757           || abbrev->tag == DW_TAG_subprogram
9758           || abbrev->tag == DW_TAG_variable
9759           || abbrev->tag == DW_TAG_namespace
9760           || part_die->is_declaration)
9761         {
9762           void **slot;
9763
9764           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9765                                            part_die->offset, INSERT);
9766           *slot = part_die;
9767         }
9768
9769       part_die = obstack_alloc (&cu->comp_unit_obstack,
9770                                 sizeof (struct partial_die_info));
9771
9772       /* For some DIEs we want to follow their children (if any).  For C
9773          we have no reason to follow the children of structures; for other
9774          languages we have to, so that we can get at method physnames
9775          to infer fully qualified class names, for DW_AT_specification,
9776          and for C++ template arguments.  For C++, we also look one level
9777          inside functions to find template arguments (if the name of the
9778          function does not already contain the template arguments).
9779
9780          For Ada, we need to scan the children of subprograms and lexical
9781          blocks as well because Ada allows the definition of nested
9782          entities that could be interesting for the debugger, such as
9783          nested subprograms for instance.  */
9784       if (last_die->has_children
9785           && (load_all
9786               || last_die->tag == DW_TAG_namespace
9787               || last_die->tag == DW_TAG_module
9788               || last_die->tag == DW_TAG_enumeration_type
9789               || (cu->language == language_cplus
9790                   && last_die->tag == DW_TAG_subprogram
9791                   && (last_die->name == NULL
9792                       || strchr (last_die->name, '<') == NULL))
9793               || (cu->language != language_c
9794                   && (last_die->tag == DW_TAG_class_type
9795                       || last_die->tag == DW_TAG_interface_type
9796                       || last_die->tag == DW_TAG_structure_type
9797                       || last_die->tag == DW_TAG_union_type))
9798               || (cu->language == language_ada
9799                   && (last_die->tag == DW_TAG_subprogram
9800                       || last_die->tag == DW_TAG_lexical_block))))
9801         {
9802           nesting_level++;
9803           parent_die = last_die;
9804           continue;
9805         }
9806
9807       /* Otherwise we skip to the next sibling, if any.  */
9808       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9809
9810       /* Back to the top, do it again.  */
9811     }
9812 }
9813
9814 /* Read a minimal amount of information into the minimal die structure.  */
9815
9816 static gdb_byte *
9817 read_partial_die (struct partial_die_info *part_die,
9818                   struct abbrev_info *abbrev,
9819                   unsigned int abbrev_len, bfd *abfd,
9820                   gdb_byte *buffer, gdb_byte *info_ptr,
9821                   struct dwarf2_cu *cu)
9822 {
9823   unsigned int i;
9824   struct attribute attr;
9825   int has_low_pc_attr = 0;
9826   int has_high_pc_attr = 0;
9827
9828   memset (part_die, 0, sizeof (struct partial_die_info));
9829
9830   part_die->offset = info_ptr - buffer;
9831
9832   info_ptr += abbrev_len;
9833
9834   if (abbrev == NULL)
9835     return info_ptr;
9836
9837   part_die->tag = abbrev->tag;
9838   part_die->has_children = abbrev->has_children;
9839
9840   for (i = 0; i < abbrev->num_attrs; ++i)
9841     {
9842       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9843
9844       /* Store the data if it is of an attribute we want to keep in a
9845          partial symbol table.  */
9846       switch (attr.name)
9847         {
9848         case DW_AT_name:
9849           switch (part_die->tag)
9850             {
9851             case DW_TAG_compile_unit:
9852             case DW_TAG_type_unit:
9853               /* Compilation units have a DW_AT_name that is a filename, not
9854                  a source language identifier.  */
9855             case DW_TAG_enumeration_type:
9856             case DW_TAG_enumerator:
9857               /* These tags always have simple identifiers already; no need
9858                  to canonicalize them.  */
9859               part_die->name = DW_STRING (&attr);
9860               break;
9861             default:
9862               part_die->name
9863                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9864                                             &cu->objfile->objfile_obstack);
9865               break;
9866             }
9867           break;
9868         case DW_AT_linkage_name:
9869         case DW_AT_MIPS_linkage_name:
9870           /* Note that both forms of linkage name might appear.  We
9871              assume they will be the same, and we only store the last
9872              one we see.  */
9873           if (cu->language == language_ada)
9874             part_die->name = DW_STRING (&attr);
9875           part_die->linkage_name = DW_STRING (&attr);
9876           break;
9877         case DW_AT_low_pc:
9878           has_low_pc_attr = 1;
9879           part_die->lowpc = DW_ADDR (&attr);
9880           break;
9881         case DW_AT_high_pc:
9882           has_high_pc_attr = 1;
9883           part_die->highpc = DW_ADDR (&attr);
9884           break;
9885         case DW_AT_location:
9886           /* Support the .debug_loc offsets.  */
9887           if (attr_form_is_block (&attr))
9888             {
9889                part_die->locdesc = DW_BLOCK (&attr);
9890             }
9891           else if (attr_form_is_section_offset (&attr))
9892             {
9893               dwarf2_complex_location_expr_complaint ();
9894             }
9895           else
9896             {
9897               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9898                                                      "partial symbol information");
9899             }
9900           break;
9901         case DW_AT_external:
9902           part_die->is_external = DW_UNSND (&attr);
9903           break;
9904         case DW_AT_declaration:
9905           part_die->is_declaration = DW_UNSND (&attr);
9906           break;
9907         case DW_AT_type:
9908           part_die->has_type = 1;
9909           break;
9910         case DW_AT_abstract_origin:
9911         case DW_AT_specification:
9912         case DW_AT_extension:
9913           part_die->has_specification = 1;
9914           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9915           break;
9916         case DW_AT_sibling:
9917           /* Ignore absolute siblings, they might point outside of
9918              the current compile unit.  */
9919           if (attr.form == DW_FORM_ref_addr)
9920             complaint (&symfile_complaints,
9921                        _("ignoring absolute DW_AT_sibling"));
9922           else
9923             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9924           break;
9925         case DW_AT_byte_size:
9926           part_die->has_byte_size = 1;
9927           break;
9928         case DW_AT_calling_convention:
9929           /* DWARF doesn't provide a way to identify a program's source-level
9930              entry point.  DW_AT_calling_convention attributes are only meant
9931              to describe functions' calling conventions.
9932
9933              However, because it's a necessary piece of information in
9934              Fortran, and because DW_CC_program is the only piece of debugging
9935              information whose definition refers to a 'main program' at all,
9936              several compilers have begun marking Fortran main programs with
9937              DW_CC_program --- even when those functions use the standard
9938              calling conventions.
9939
9940              So until DWARF specifies a way to provide this information and
9941              compilers pick up the new representation, we'll support this
9942              practice.  */
9943           if (DW_UNSND (&attr) == DW_CC_program
9944               && cu->language == language_fortran)
9945             {
9946               set_main_name (part_die->name);
9947
9948               /* As this DIE has a static linkage the name would be difficult
9949                  to look up later.  */
9950               language_of_main = language_fortran;
9951             }
9952           break;
9953         default:
9954           break;
9955         }
9956     }
9957
9958   if (has_low_pc_attr && has_high_pc_attr)
9959     {
9960       /* When using the GNU linker, .gnu.linkonce. sections are used to
9961          eliminate duplicate copies of functions and vtables and such.
9962          The linker will arbitrarily choose one and discard the others.
9963          The AT_*_pc values for such functions refer to local labels in
9964          these sections.  If the section from that file was discarded, the
9965          labels are not in the output, so the relocs get a value of 0.
9966          If this is a discarded function, mark the pc bounds as invalid,
9967          so that GDB will ignore it.  */
9968       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9969         {
9970           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9971
9972           complaint (&symfile_complaints,
9973                      _("DW_AT_low_pc %s is zero "
9974                        "for DIE at 0x%x [in module %s]"),
9975                      paddress (gdbarch, part_die->lowpc),
9976                      part_die->offset, cu->objfile->name);
9977         }
9978       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9979       else if (part_die->lowpc >= part_die->highpc)
9980         {
9981           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9982
9983           complaint (&symfile_complaints,
9984                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9985                        "for DIE at 0x%x [in module %s]"),
9986                      paddress (gdbarch, part_die->lowpc),
9987                      paddress (gdbarch, part_die->highpc),
9988                      part_die->offset, cu->objfile->name);
9989         }
9990       else
9991         part_die->has_pc_info = 1;
9992     }
9993
9994   return info_ptr;
9995 }
9996
9997 /* Find a cached partial DIE at OFFSET in CU.  */
9998
9999 static struct partial_die_info *
10000 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
10001 {
10002   struct partial_die_info *lookup_die = NULL;
10003   struct partial_die_info part_die;
10004
10005   part_die.offset = offset;
10006   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
10007
10008   return lookup_die;
10009 }
10010
10011 /* Find a partial DIE at OFFSET, which may or may not be in CU,
10012    except in the case of .debug_types DIEs which do not reference
10013    outside their CU (they do however referencing other types via
10014    DW_FORM_ref_sig8).  */
10015
10016 static struct partial_die_info *
10017 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
10018 {
10019   struct dwarf2_per_cu_data *per_cu = NULL;
10020   struct partial_die_info *pd = NULL;
10021
10022   if (cu->per_cu->debug_types_section)
10023     {
10024       pd = find_partial_die_in_comp_unit (offset, cu);
10025       if (pd != NULL)
10026         return pd;
10027       goto not_found;
10028     }
10029
10030   if (offset_in_cu_p (&cu->header, offset))
10031     {
10032       pd = find_partial_die_in_comp_unit (offset, cu);
10033       if (pd != NULL)
10034         return pd;
10035     }
10036
10037   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
10038
10039   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10040     load_partial_comp_unit (per_cu, cu->objfile);
10041
10042   per_cu->cu->last_used = 0;
10043   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10044
10045   if (pd == NULL && per_cu->load_all_dies == 0)
10046     {
10047       struct cleanup *back_to;
10048       struct partial_die_info comp_unit_die;
10049       struct abbrev_info *abbrev;
10050       unsigned int bytes_read;
10051       char *info_ptr;
10052
10053       per_cu->load_all_dies = 1;
10054
10055       /* Re-read the DIEs.  */
10056       back_to = make_cleanup (null_cleanup, 0);
10057       if (per_cu->cu->dwarf2_abbrevs == NULL)
10058         {
10059           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
10060           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10061         }
10062       info_ptr = (dwarf2_per_objfile->info.buffer
10063                   + per_cu->cu->header.offset
10064                   + per_cu->cu->header.first_die_offset);
10065       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10066       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10067                                    per_cu->cu->objfile->obfd,
10068                                    dwarf2_per_objfile->info.buffer, info_ptr,
10069                                    per_cu->cu);
10070       if (comp_unit_die.has_children)
10071         load_partial_dies (per_cu->cu->objfile->obfd,
10072                            dwarf2_per_objfile->info.buffer, info_ptr,
10073                            0, per_cu->cu);
10074       do_cleanups (back_to);
10075
10076       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10077     }
10078
10079  not_found:
10080
10081   if (pd == NULL)
10082     internal_error (__FILE__, __LINE__,
10083                     _("could not find partial DIE 0x%x "
10084                       "in cache [from module %s]\n"),
10085                     offset, bfd_get_filename (cu->objfile->obfd));
10086   return pd;
10087 }
10088
10089 /* See if we can figure out if the class lives in a namespace.  We do
10090    this by looking for a member function; its demangled name will
10091    contain namespace info, if there is any.  */
10092
10093 static void
10094 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10095                                   struct dwarf2_cu *cu)
10096 {
10097   /* NOTE: carlton/2003-10-07: Getting the info this way changes
10098      what template types look like, because the demangler
10099      frequently doesn't give the same name as the debug info.  We
10100      could fix this by only using the demangled name to get the
10101      prefix (but see comment in read_structure_type).  */
10102
10103   struct partial_die_info *real_pdi;
10104   struct partial_die_info *child_pdi;
10105
10106   /* If this DIE (this DIE's specification, if any) has a parent, then
10107      we should not do this.  We'll prepend the parent's fully qualified
10108      name when we create the partial symbol.  */
10109
10110   real_pdi = struct_pdi;
10111   while (real_pdi->has_specification)
10112     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10113
10114   if (real_pdi->die_parent != NULL)
10115     return;
10116
10117   for (child_pdi = struct_pdi->die_child;
10118        child_pdi != NULL;
10119        child_pdi = child_pdi->die_sibling)
10120     {
10121       if (child_pdi->tag == DW_TAG_subprogram
10122           && child_pdi->linkage_name != NULL)
10123         {
10124           char *actual_class_name
10125             = language_class_name_from_physname (cu->language_defn,
10126                                                  child_pdi->linkage_name);
10127           if (actual_class_name != NULL)
10128             {
10129               struct_pdi->name
10130                 = obsavestring (actual_class_name,
10131                                 strlen (actual_class_name),
10132                                 &cu->objfile->objfile_obstack);
10133               xfree (actual_class_name);
10134             }
10135           break;
10136         }
10137     }
10138 }
10139
10140 /* Adjust PART_DIE before generating a symbol for it.  This function
10141    may set the is_external flag or change the DIE's name.  */
10142
10143 static void
10144 fixup_partial_die (struct partial_die_info *part_die,
10145                    struct dwarf2_cu *cu)
10146 {
10147   /* Once we've fixed up a die, there's no point in doing so again.
10148      This also avoids a memory leak if we were to call
10149      guess_partial_die_structure_name multiple times.  */
10150   if (part_die->fixup_called)
10151     return;
10152
10153   /* If we found a reference attribute and the DIE has no name, try
10154      to find a name in the referred to DIE.  */
10155
10156   if (part_die->name == NULL && part_die->has_specification)
10157     {
10158       struct partial_die_info *spec_die;
10159
10160       spec_die = find_partial_die (part_die->spec_offset, cu);
10161
10162       fixup_partial_die (spec_die, cu);
10163
10164       if (spec_die->name)
10165         {
10166           part_die->name = spec_die->name;
10167
10168           /* Copy DW_AT_external attribute if it is set.  */
10169           if (spec_die->is_external)
10170             part_die->is_external = spec_die->is_external;
10171         }
10172     }
10173
10174   /* Set default names for some unnamed DIEs.  */
10175
10176   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10177     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10178
10179   /* If there is no parent die to provide a namespace, and there are
10180      children, see if we can determine the namespace from their linkage
10181      name.
10182      NOTE: We need to do this even if cu->has_namespace_info != 0.
10183      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
10184   if (cu->language == language_cplus
10185       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10186       && part_die->die_parent == NULL
10187       && part_die->has_children
10188       && (part_die->tag == DW_TAG_class_type
10189           || part_die->tag == DW_TAG_structure_type
10190           || part_die->tag == DW_TAG_union_type))
10191     guess_partial_die_structure_name (part_die, cu);
10192
10193   /* GCC might emit a nameless struct or union that has a linkage
10194      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10195   if (part_die->name == NULL
10196       && (part_die->tag == DW_TAG_class_type
10197           || part_die->tag == DW_TAG_interface_type
10198           || part_die->tag == DW_TAG_structure_type
10199           || part_die->tag == DW_TAG_union_type)
10200       && part_die->linkage_name != NULL)
10201     {
10202       char *demangled;
10203
10204       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10205       if (demangled)
10206         {
10207           const char *base;
10208
10209           /* Strip any leading namespaces/classes, keep only the base name.
10210              DW_AT_name for named DIEs does not contain the prefixes.  */
10211           base = strrchr (demangled, ':');
10212           if (base && base > demangled && base[-1] == ':')
10213             base++;
10214           else
10215             base = demangled;
10216
10217           part_die->name = obsavestring (base, strlen (base),
10218                                          &cu->objfile->objfile_obstack);
10219           xfree (demangled);
10220         }
10221     }
10222
10223   part_die->fixup_called = 1;
10224 }
10225
10226 /* Read an attribute value described by an attribute form.  */
10227
10228 static gdb_byte *
10229 read_attribute_value (struct attribute *attr, unsigned form,
10230                       bfd *abfd, gdb_byte *info_ptr,
10231                       struct dwarf2_cu *cu)
10232 {
10233   struct comp_unit_head *cu_header = &cu->header;
10234   unsigned int bytes_read;
10235   struct dwarf_block *blk;
10236
10237   attr->form = form;
10238   switch (form)
10239     {
10240     case DW_FORM_ref_addr:
10241       if (cu->header.version == 2)
10242         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10243       else
10244         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10245                                       &cu->header, &bytes_read);
10246       info_ptr += bytes_read;
10247       break;
10248     case DW_FORM_addr:
10249       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10250       info_ptr += bytes_read;
10251       break;
10252     case DW_FORM_block2:
10253       blk = dwarf_alloc_block (cu);
10254       blk->size = read_2_bytes (abfd, info_ptr);
10255       info_ptr += 2;
10256       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10257       info_ptr += blk->size;
10258       DW_BLOCK (attr) = blk;
10259       break;
10260     case DW_FORM_block4:
10261       blk = dwarf_alloc_block (cu);
10262       blk->size = read_4_bytes (abfd, info_ptr);
10263       info_ptr += 4;
10264       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10265       info_ptr += blk->size;
10266       DW_BLOCK (attr) = blk;
10267       break;
10268     case DW_FORM_data2:
10269       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10270       info_ptr += 2;
10271       break;
10272     case DW_FORM_data4:
10273       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10274       info_ptr += 4;
10275       break;
10276     case DW_FORM_data8:
10277       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10278       info_ptr += 8;
10279       break;
10280     case DW_FORM_sec_offset:
10281       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10282       info_ptr += bytes_read;
10283       break;
10284     case DW_FORM_string:
10285       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10286       DW_STRING_IS_CANONICAL (attr) = 0;
10287       info_ptr += bytes_read;
10288       break;
10289     case DW_FORM_strp:
10290       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10291                                                &bytes_read);
10292       DW_STRING_IS_CANONICAL (attr) = 0;
10293       info_ptr += bytes_read;
10294       break;
10295     case DW_FORM_exprloc:
10296     case DW_FORM_block:
10297       blk = dwarf_alloc_block (cu);
10298       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10299       info_ptr += bytes_read;
10300       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10301       info_ptr += blk->size;
10302       DW_BLOCK (attr) = blk;
10303       break;
10304     case DW_FORM_block1:
10305       blk = dwarf_alloc_block (cu);
10306       blk->size = read_1_byte (abfd, info_ptr);
10307       info_ptr += 1;
10308       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10309       info_ptr += blk->size;
10310       DW_BLOCK (attr) = blk;
10311       break;
10312     case DW_FORM_data1:
10313       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10314       info_ptr += 1;
10315       break;
10316     case DW_FORM_flag:
10317       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10318       info_ptr += 1;
10319       break;
10320     case DW_FORM_flag_present:
10321       DW_UNSND (attr) = 1;
10322       break;
10323     case DW_FORM_sdata:
10324       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10325       info_ptr += bytes_read;
10326       break;
10327     case DW_FORM_udata:
10328       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10329       info_ptr += bytes_read;
10330       break;
10331     case DW_FORM_ref1:
10332       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10333       info_ptr += 1;
10334       break;
10335     case DW_FORM_ref2:
10336       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10337       info_ptr += 2;
10338       break;
10339     case DW_FORM_ref4:
10340       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10341       info_ptr += 4;
10342       break;
10343     case DW_FORM_ref8:
10344       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10345       info_ptr += 8;
10346       break;
10347     case DW_FORM_ref_sig8:
10348       /* Convert the signature to something we can record in DW_UNSND
10349          for later lookup.
10350          NOTE: This is NULL if the type wasn't found.  */
10351       DW_SIGNATURED_TYPE (attr) =
10352         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10353       info_ptr += 8;
10354       break;
10355     case DW_FORM_ref_udata:
10356       DW_ADDR (attr) = (cu->header.offset
10357                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10358       info_ptr += bytes_read;
10359       break;
10360     case DW_FORM_indirect:
10361       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10362       info_ptr += bytes_read;
10363       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10364       break;
10365     default:
10366       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10367              dwarf_form_name (form),
10368              bfd_get_filename (abfd));
10369     }
10370
10371   /* We have seen instances where the compiler tried to emit a byte
10372      size attribute of -1 which ended up being encoded as an unsigned
10373      0xffffffff.  Although 0xffffffff is technically a valid size value,
10374      an object of this size seems pretty unlikely so we can relatively
10375      safely treat these cases as if the size attribute was invalid and
10376      treat them as zero by default.  */
10377   if (attr->name == DW_AT_byte_size
10378       && form == DW_FORM_data4
10379       && DW_UNSND (attr) >= 0xffffffff)
10380     {
10381       complaint
10382         (&symfile_complaints,
10383          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10384          hex_string (DW_UNSND (attr)));
10385       DW_UNSND (attr) = 0;
10386     }
10387
10388   return info_ptr;
10389 }
10390
10391 /* Read an attribute described by an abbreviated attribute.  */
10392
10393 static gdb_byte *
10394 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10395                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10396 {
10397   attr->name = abbrev->name;
10398   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10399 }
10400
10401 /* Read dwarf information from a buffer.  */
10402
10403 static unsigned int
10404 read_1_byte (bfd *abfd, gdb_byte *buf)
10405 {
10406   return bfd_get_8 (abfd, buf);
10407 }
10408
10409 static int
10410 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10411 {
10412   return bfd_get_signed_8 (abfd, buf);
10413 }
10414
10415 static unsigned int
10416 read_2_bytes (bfd *abfd, gdb_byte *buf)
10417 {
10418   return bfd_get_16 (abfd, buf);
10419 }
10420
10421 static int
10422 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10423 {
10424   return bfd_get_signed_16 (abfd, buf);
10425 }
10426
10427 static unsigned int
10428 read_4_bytes (bfd *abfd, gdb_byte *buf)
10429 {
10430   return bfd_get_32 (abfd, buf);
10431 }
10432
10433 static int
10434 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10435 {
10436   return bfd_get_signed_32 (abfd, buf);
10437 }
10438
10439 static ULONGEST
10440 read_8_bytes (bfd *abfd, gdb_byte *buf)
10441 {
10442   return bfd_get_64 (abfd, buf);
10443 }
10444
10445 static CORE_ADDR
10446 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10447               unsigned int *bytes_read)
10448 {
10449   struct comp_unit_head *cu_header = &cu->header;
10450   CORE_ADDR retval = 0;
10451
10452   if (cu_header->signed_addr_p)
10453     {
10454       switch (cu_header->addr_size)
10455         {
10456         case 2:
10457           retval = bfd_get_signed_16 (abfd, buf);
10458           break;
10459         case 4:
10460           retval = bfd_get_signed_32 (abfd, buf);
10461           break;
10462         case 8:
10463           retval = bfd_get_signed_64 (abfd, buf);
10464           break;
10465         default:
10466           internal_error (__FILE__, __LINE__,
10467                           _("read_address: bad switch, signed [in module %s]"),
10468                           bfd_get_filename (abfd));
10469         }
10470     }
10471   else
10472     {
10473       switch (cu_header->addr_size)
10474         {
10475         case 2:
10476           retval = bfd_get_16 (abfd, buf);
10477           break;
10478         case 4:
10479           retval = bfd_get_32 (abfd, buf);
10480           break;
10481         case 8:
10482           retval = bfd_get_64 (abfd, buf);
10483           break;
10484         default:
10485           internal_error (__FILE__, __LINE__,
10486                           _("read_address: bad switch, "
10487                             "unsigned [in module %s]"),
10488                           bfd_get_filename (abfd));
10489         }
10490     }
10491
10492   *bytes_read = cu_header->addr_size;
10493   return retval;
10494 }
10495
10496 /* Read the initial length from a section.  The (draft) DWARF 3
10497    specification allows the initial length to take up either 4 bytes
10498    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10499    bytes describe the length and all offsets will be 8 bytes in length
10500    instead of 4.
10501
10502    An older, non-standard 64-bit format is also handled by this
10503    function.  The older format in question stores the initial length
10504    as an 8-byte quantity without an escape value.  Lengths greater
10505    than 2^32 aren't very common which means that the initial 4 bytes
10506    is almost always zero.  Since a length value of zero doesn't make
10507    sense for the 32-bit format, this initial zero can be considered to
10508    be an escape value which indicates the presence of the older 64-bit
10509    format.  As written, the code can't detect (old format) lengths
10510    greater than 4GB.  If it becomes necessary to handle lengths
10511    somewhat larger than 4GB, we could allow other small values (such
10512    as the non-sensical values of 1, 2, and 3) to also be used as
10513    escape values indicating the presence of the old format.
10514
10515    The value returned via bytes_read should be used to increment the
10516    relevant pointer after calling read_initial_length().
10517
10518    [ Note:  read_initial_length() and read_offset() are based on the
10519      document entitled "DWARF Debugging Information Format", revision
10520      3, draft 8, dated November 19, 2001.  This document was obtained
10521      from:
10522
10523         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10524
10525      This document is only a draft and is subject to change.  (So beware.)
10526
10527      Details regarding the older, non-standard 64-bit format were
10528      determined empirically by examining 64-bit ELF files produced by
10529      the SGI toolchain on an IRIX 6.5 machine.
10530
10531      - Kevin, July 16, 2002
10532    ] */
10533
10534 static LONGEST
10535 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10536 {
10537   LONGEST length = bfd_get_32 (abfd, buf);
10538
10539   if (length == 0xffffffff)
10540     {
10541       length = bfd_get_64 (abfd, buf + 4);
10542       *bytes_read = 12;
10543     }
10544   else if (length == 0)
10545     {
10546       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10547       length = bfd_get_64 (abfd, buf);
10548       *bytes_read = 8;
10549     }
10550   else
10551     {
10552       *bytes_read = 4;
10553     }
10554
10555   return length;
10556 }
10557
10558 /* Cover function for read_initial_length.
10559    Returns the length of the object at BUF, and stores the size of the
10560    initial length in *BYTES_READ and stores the size that offsets will be in
10561    *OFFSET_SIZE.
10562    If the initial length size is not equivalent to that specified in
10563    CU_HEADER then issue a complaint.
10564    This is useful when reading non-comp-unit headers.  */
10565
10566 static LONGEST
10567 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10568                                         const struct comp_unit_head *cu_header,
10569                                         unsigned int *bytes_read,
10570                                         unsigned int *offset_size)
10571 {
10572   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10573
10574   gdb_assert (cu_header->initial_length_size == 4
10575               || cu_header->initial_length_size == 8
10576               || cu_header->initial_length_size == 12);
10577
10578   if (cu_header->initial_length_size != *bytes_read)
10579     complaint (&symfile_complaints,
10580                _("intermixed 32-bit and 64-bit DWARF sections"));
10581
10582   *offset_size = (*bytes_read == 4) ? 4 : 8;
10583   return length;
10584 }
10585
10586 /* Read an offset from the data stream.  The size of the offset is
10587    given by cu_header->offset_size.  */
10588
10589 static LONGEST
10590 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10591              unsigned int *bytes_read)
10592 {
10593   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10594
10595   *bytes_read = cu_header->offset_size;
10596   return offset;
10597 }
10598
10599 /* Read an offset from the data stream.  */
10600
10601 static LONGEST
10602 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10603 {
10604   LONGEST retval = 0;
10605
10606   switch (offset_size)
10607     {
10608     case 4:
10609       retval = bfd_get_32 (abfd, buf);
10610       break;
10611     case 8:
10612       retval = bfd_get_64 (abfd, buf);
10613       break;
10614     default:
10615       internal_error (__FILE__, __LINE__,
10616                       _("read_offset_1: bad switch [in module %s]"),
10617                       bfd_get_filename (abfd));
10618     }
10619
10620   return retval;
10621 }
10622
10623 static gdb_byte *
10624 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10625 {
10626   /* If the size of a host char is 8 bits, we can return a pointer
10627      to the buffer, otherwise we have to copy the data to a buffer
10628      allocated on the temporary obstack.  */
10629   gdb_assert (HOST_CHAR_BIT == 8);
10630   return buf;
10631 }
10632
10633 static char *
10634 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10635 {
10636   /* If the size of a host char is 8 bits, we can return a pointer
10637      to the string, otherwise we have to copy the string to a buffer
10638      allocated on the temporary obstack.  */
10639   gdb_assert (HOST_CHAR_BIT == 8);
10640   if (*buf == '\0')
10641     {
10642       *bytes_read_ptr = 1;
10643       return NULL;
10644     }
10645   *bytes_read_ptr = strlen ((char *) buf) + 1;
10646   return (char *) buf;
10647 }
10648
10649 static char *
10650 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10651 {
10652   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10653   if (dwarf2_per_objfile->str.buffer == NULL)
10654     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10655            bfd_get_filename (abfd));
10656   if (str_offset >= dwarf2_per_objfile->str.size)
10657     error (_("DW_FORM_strp pointing outside of "
10658              ".debug_str section [in module %s]"),
10659            bfd_get_filename (abfd));
10660   gdb_assert (HOST_CHAR_BIT == 8);
10661   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10662     return NULL;
10663   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10664 }
10665
10666 static char *
10667 read_indirect_string (bfd *abfd, gdb_byte *buf,
10668                       const struct comp_unit_head *cu_header,
10669                       unsigned int *bytes_read_ptr)
10670 {
10671   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10672
10673   return read_indirect_string_at_offset (abfd, str_offset);
10674 }
10675
10676 static unsigned long
10677 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10678 {
10679   unsigned long result;
10680   unsigned int num_read;
10681   int i, shift;
10682   unsigned char byte;
10683
10684   result = 0;
10685   shift = 0;
10686   num_read = 0;
10687   i = 0;
10688   while (1)
10689     {
10690       byte = bfd_get_8 (abfd, buf);
10691       buf++;
10692       num_read++;
10693       result |= ((unsigned long)(byte & 127) << shift);
10694       if ((byte & 128) == 0)
10695         {
10696           break;
10697         }
10698       shift += 7;
10699     }
10700   *bytes_read_ptr = num_read;
10701   return result;
10702 }
10703
10704 static long
10705 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10706 {
10707   long result;
10708   int i, shift, num_read;
10709   unsigned char byte;
10710
10711   result = 0;
10712   shift = 0;
10713   num_read = 0;
10714   i = 0;
10715   while (1)
10716     {
10717       byte = bfd_get_8 (abfd, buf);
10718       buf++;
10719       num_read++;
10720       result |= ((long)(byte & 127) << shift);
10721       shift += 7;
10722       if ((byte & 128) == 0)
10723         {
10724           break;
10725         }
10726     }
10727   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10728     result |= -(((long)1) << shift);
10729   *bytes_read_ptr = num_read;
10730   return result;
10731 }
10732
10733 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10734
10735 static gdb_byte *
10736 skip_leb128 (bfd *abfd, gdb_byte *buf)
10737 {
10738   int byte;
10739
10740   while (1)
10741     {
10742       byte = bfd_get_8 (abfd, buf);
10743       buf++;
10744       if ((byte & 128) == 0)
10745         return buf;
10746     }
10747 }
10748
10749 static void
10750 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10751 {
10752   switch (lang)
10753     {
10754     case DW_LANG_C89:
10755     case DW_LANG_C99:
10756     case DW_LANG_C:
10757       cu->language = language_c;
10758       break;
10759     case DW_LANG_C_plus_plus:
10760       cu->language = language_cplus;
10761       break;
10762     case DW_LANG_D:
10763       cu->language = language_d;
10764       break;
10765     case DW_LANG_Fortran77:
10766     case DW_LANG_Fortran90:
10767     case DW_LANG_Fortran95:
10768       cu->language = language_fortran;
10769       break;
10770     case DW_LANG_Mips_Assembler:
10771       cu->language = language_asm;
10772       break;
10773     case DW_LANG_Java:
10774       cu->language = language_java;
10775       break;
10776     case DW_LANG_Ada83:
10777     case DW_LANG_Ada95:
10778       cu->language = language_ada;
10779       break;
10780     case DW_LANG_Modula2:
10781       cu->language = language_m2;
10782       break;
10783     case DW_LANG_Pascal83:
10784       cu->language = language_pascal;
10785       break;
10786     case DW_LANG_ObjC:
10787       cu->language = language_objc;
10788       break;
10789     case DW_LANG_Cobol74:
10790     case DW_LANG_Cobol85:
10791     default:
10792       cu->language = language_minimal;
10793       break;
10794     }
10795   cu->language_defn = language_def (cu->language);
10796 }
10797
10798 /* Return the named attribute or NULL if not there.  */
10799
10800 static struct attribute *
10801 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10802 {
10803   unsigned int i;
10804   struct attribute *spec = NULL;
10805
10806   for (i = 0; i < die->num_attrs; ++i)
10807     {
10808       if (die->attrs[i].name == name)
10809         return &die->attrs[i];
10810       if (die->attrs[i].name == DW_AT_specification
10811           || die->attrs[i].name == DW_AT_abstract_origin)
10812         spec = &die->attrs[i];
10813     }
10814
10815   if (spec)
10816     {
10817       die = follow_die_ref (die, spec, &cu);
10818       return dwarf2_attr (die, name, cu);
10819     }
10820
10821   return NULL;
10822 }
10823
10824 /* Return the named attribute or NULL if not there,
10825    but do not follow DW_AT_specification, etc.
10826    This is for use in contexts where we're reading .debug_types dies.
10827    Following DW_AT_specification, DW_AT_abstract_origin will take us
10828    back up the chain, and we want to go down.  */
10829
10830 static struct attribute *
10831 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10832                        struct dwarf2_cu *cu)
10833 {
10834   unsigned int i;
10835
10836   for (i = 0; i < die->num_attrs; ++i)
10837     if (die->attrs[i].name == name)
10838       return &die->attrs[i];
10839
10840   return NULL;
10841 }
10842
10843 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10844    and holds a non-zero value.  This function should only be used for
10845    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10846
10847 static int
10848 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10849 {
10850   struct attribute *attr = dwarf2_attr (die, name, cu);
10851
10852   return (attr && DW_UNSND (attr));
10853 }
10854
10855 static int
10856 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10857 {
10858   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10859      which value is non-zero.  However, we have to be careful with
10860      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10861      (via dwarf2_flag_true_p) follows this attribute.  So we may
10862      end up accidently finding a declaration attribute that belongs
10863      to a different DIE referenced by the specification attribute,
10864      even though the given DIE does not have a declaration attribute.  */
10865   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10866           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10867 }
10868
10869 /* Return the die giving the specification for DIE, if there is
10870    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10871    containing the return value on output.  If there is no
10872    specification, but there is an abstract origin, that is
10873    returned.  */
10874
10875 static struct die_info *
10876 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10877 {
10878   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10879                                              *spec_cu);
10880
10881   if (spec_attr == NULL)
10882     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10883
10884   if (spec_attr == NULL)
10885     return NULL;
10886   else
10887     return follow_die_ref (die, spec_attr, spec_cu);
10888 }
10889
10890 /* Free the line_header structure *LH, and any arrays and strings it
10891    refers to.
10892    NOTE: This is also used as a "cleanup" function.  */
10893
10894 static void
10895 free_line_header (struct line_header *lh)
10896 {
10897   if (lh->standard_opcode_lengths)
10898     xfree (lh->standard_opcode_lengths);
10899
10900   /* Remember that all the lh->file_names[i].name pointers are
10901      pointers into debug_line_buffer, and don't need to be freed.  */
10902   if (lh->file_names)
10903     xfree (lh->file_names);
10904
10905   /* Similarly for the include directory names.  */
10906   if (lh->include_dirs)
10907     xfree (lh->include_dirs);
10908
10909   xfree (lh);
10910 }
10911
10912 /* Add an entry to LH's include directory table.  */
10913
10914 static void
10915 add_include_dir (struct line_header *lh, char *include_dir)
10916 {
10917   /* Grow the array if necessary.  */
10918   if (lh->include_dirs_size == 0)
10919     {
10920       lh->include_dirs_size = 1; /* for testing */
10921       lh->include_dirs = xmalloc (lh->include_dirs_size
10922                                   * sizeof (*lh->include_dirs));
10923     }
10924   else if (lh->num_include_dirs >= lh->include_dirs_size)
10925     {
10926       lh->include_dirs_size *= 2;
10927       lh->include_dirs = xrealloc (lh->include_dirs,
10928                                    (lh->include_dirs_size
10929                                     * sizeof (*lh->include_dirs)));
10930     }
10931
10932   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10933 }
10934
10935 /* Add an entry to LH's file name table.  */
10936
10937 static void
10938 add_file_name (struct line_header *lh,
10939                char *name,
10940                unsigned int dir_index,
10941                unsigned int mod_time,
10942                unsigned int length)
10943 {
10944   struct file_entry *fe;
10945
10946   /* Grow the array if necessary.  */
10947   if (lh->file_names_size == 0)
10948     {
10949       lh->file_names_size = 1; /* for testing */
10950       lh->file_names = xmalloc (lh->file_names_size
10951                                 * sizeof (*lh->file_names));
10952     }
10953   else if (lh->num_file_names >= lh->file_names_size)
10954     {
10955       lh->file_names_size *= 2;
10956       lh->file_names = xrealloc (lh->file_names,
10957                                  (lh->file_names_size
10958                                   * sizeof (*lh->file_names)));
10959     }
10960
10961   fe = &lh->file_names[lh->num_file_names++];
10962   fe->name = name;
10963   fe->dir_index = dir_index;
10964   fe->mod_time = mod_time;
10965   fe->length = length;
10966   fe->included_p = 0;
10967   fe->symtab = NULL;
10968 }
10969
10970 /* Read the statement program header starting at OFFSET in
10971    .debug_line, according to the endianness of ABFD.  Return a pointer
10972    to a struct line_header, allocated using xmalloc.
10973
10974    NOTE: the strings in the include directory and file name tables of
10975    the returned object point into debug_line_buffer, and must not be
10976    freed.  */
10977
10978 static struct line_header *
10979 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10980                           struct dwarf2_cu *cu)
10981 {
10982   struct cleanup *back_to;
10983   struct line_header *lh;
10984   gdb_byte *line_ptr;
10985   unsigned int bytes_read, offset_size;
10986   int i;
10987   char *cur_dir, *cur_file;
10988
10989   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10990   if (dwarf2_per_objfile->line.buffer == NULL)
10991     {
10992       complaint (&symfile_complaints, _("missing .debug_line section"));
10993       return 0;
10994     }
10995
10996   /* Make sure that at least there's room for the total_length field.
10997      That could be 12 bytes long, but we're just going to fudge that.  */
10998   if (offset + 4 >= dwarf2_per_objfile->line.size)
10999     {
11000       dwarf2_statement_list_fits_in_line_number_section_complaint ();
11001       return 0;
11002     }
11003
11004   lh = xmalloc (sizeof (*lh));
11005   memset (lh, 0, sizeof (*lh));
11006   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11007                           (void *) lh);
11008
11009   line_ptr = dwarf2_per_objfile->line.buffer + offset;
11010
11011   /* Read in the header.  */
11012   lh->total_length =
11013     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11014                                             &bytes_read, &offset_size);
11015   line_ptr += bytes_read;
11016   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11017                                      + dwarf2_per_objfile->line.size))
11018     {
11019       dwarf2_statement_list_fits_in_line_number_section_complaint ();
11020       return 0;
11021     }
11022   lh->statement_program_end = line_ptr + lh->total_length;
11023   lh->version = read_2_bytes (abfd, line_ptr);
11024   line_ptr += 2;
11025   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11026   line_ptr += offset_size;
11027   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11028   line_ptr += 1;
11029   if (lh->version >= 4)
11030     {
11031       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11032       line_ptr += 1;
11033     }
11034   else
11035     lh->maximum_ops_per_instruction = 1;
11036
11037   if (lh->maximum_ops_per_instruction == 0)
11038     {
11039       lh->maximum_ops_per_instruction = 1;
11040       complaint (&symfile_complaints,
11041                  _("invalid maximum_ops_per_instruction "
11042                    "in `.debug_line' section"));
11043     }
11044
11045   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11046   line_ptr += 1;
11047   lh->line_base = read_1_signed_byte (abfd, line_ptr);
11048   line_ptr += 1;
11049   lh->line_range = read_1_byte (abfd, line_ptr);
11050   line_ptr += 1;
11051   lh->opcode_base = read_1_byte (abfd, line_ptr);
11052   line_ptr += 1;
11053   lh->standard_opcode_lengths
11054     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11055
11056   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
11057   for (i = 1; i < lh->opcode_base; ++i)
11058     {
11059       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11060       line_ptr += 1;
11061     }
11062
11063   /* Read directory table.  */
11064   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11065     {
11066       line_ptr += bytes_read;
11067       add_include_dir (lh, cur_dir);
11068     }
11069   line_ptr += bytes_read;
11070
11071   /* Read file name table.  */
11072   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11073     {
11074       unsigned int dir_index, mod_time, length;
11075
11076       line_ptr += bytes_read;
11077       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11078       line_ptr += bytes_read;
11079       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11080       line_ptr += bytes_read;
11081       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11082       line_ptr += bytes_read;
11083
11084       add_file_name (lh, cur_file, dir_index, mod_time, length);
11085     }
11086   line_ptr += bytes_read;
11087   lh->statement_program_start = line_ptr;
11088
11089   if (line_ptr > (dwarf2_per_objfile->line.buffer
11090                   + dwarf2_per_objfile->line.size))
11091     complaint (&symfile_complaints,
11092                _("line number info header doesn't "
11093                  "fit in `.debug_line' section"));
11094
11095   discard_cleanups (back_to);
11096   return lh;
11097 }
11098
11099 /* This function exists to work around a bug in certain compilers
11100    (particularly GCC 2.95), in which the first line number marker of a
11101    function does not show up until after the prologue, right before
11102    the second line number marker.  This function shifts ADDRESS down
11103    to the beginning of the function if necessary, and is called on
11104    addresses passed to record_line.  */
11105
11106 static CORE_ADDR
11107 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
11108 {
11109   struct function_range *fn;
11110
11111   /* Find the function_range containing address.  */
11112   if (!cu->first_fn)
11113     return address;
11114
11115   if (!cu->cached_fn)
11116     cu->cached_fn = cu->first_fn;
11117
11118   fn = cu->cached_fn;
11119   while (fn)
11120     if (fn->lowpc <= address && fn->highpc > address)
11121       goto found;
11122     else
11123       fn = fn->next;
11124
11125   fn = cu->first_fn;
11126   while (fn && fn != cu->cached_fn)
11127     if (fn->lowpc <= address && fn->highpc > address)
11128       goto found;
11129     else
11130       fn = fn->next;
11131
11132   return address;
11133
11134  found:
11135   if (fn->seen_line)
11136     return address;
11137   if (address != fn->lowpc)
11138     complaint (&symfile_complaints,
11139                _("misplaced first line number at 0x%lx for '%s'"),
11140                (unsigned long) address, fn->name);
11141   fn->seen_line = 1;
11142   return fn->lowpc;
11143 }
11144
11145 /* Subroutine of dwarf_decode_lines to simplify it.
11146    Return the file name of the psymtab for included file FILE_INDEX
11147    in line header LH of PST.
11148    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11149    If space for the result is malloc'd, it will be freed by a cleanup.
11150    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
11151
11152 static char *
11153 psymtab_include_file_name (const struct line_header *lh, int file_index,
11154                            const struct partial_symtab *pst,
11155                            const char *comp_dir)
11156 {
11157   const struct file_entry fe = lh->file_names [file_index];
11158   char *include_name = fe.name;
11159   char *include_name_to_compare = include_name;
11160   char *dir_name = NULL;
11161   const char *pst_filename;
11162   char *copied_name = NULL;
11163   int file_is_pst;
11164
11165   if (fe.dir_index)
11166     dir_name = lh->include_dirs[fe.dir_index - 1];
11167
11168   if (!IS_ABSOLUTE_PATH (include_name)
11169       && (dir_name != NULL || comp_dir != NULL))
11170     {
11171       /* Avoid creating a duplicate psymtab for PST.
11172          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11173          Before we do the comparison, however, we need to account
11174          for DIR_NAME and COMP_DIR.
11175          First prepend dir_name (if non-NULL).  If we still don't
11176          have an absolute path prepend comp_dir (if non-NULL).
11177          However, the directory we record in the include-file's
11178          psymtab does not contain COMP_DIR (to match the
11179          corresponding symtab(s)).
11180
11181          Example:
11182
11183          bash$ cd /tmp
11184          bash$ gcc -g ./hello.c
11185          include_name = "hello.c"
11186          dir_name = "."
11187          DW_AT_comp_dir = comp_dir = "/tmp"
11188          DW_AT_name = "./hello.c"  */
11189
11190       if (dir_name != NULL)
11191         {
11192           include_name = concat (dir_name, SLASH_STRING,
11193                                  include_name, (char *)NULL);
11194           include_name_to_compare = include_name;
11195           make_cleanup (xfree, include_name);
11196         }
11197       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11198         {
11199           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11200                                             include_name, (char *)NULL);
11201         }
11202     }
11203
11204   pst_filename = pst->filename;
11205   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11206     {
11207       copied_name = concat (pst->dirname, SLASH_STRING,
11208                             pst_filename, (char *)NULL);
11209       pst_filename = copied_name;
11210     }
11211
11212   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11213
11214   if (include_name_to_compare != include_name)
11215     xfree (include_name_to_compare);
11216   if (copied_name != NULL)
11217     xfree (copied_name);
11218
11219   if (file_is_pst)
11220     return NULL;
11221   return include_name;
11222 }
11223
11224 /* Ignore this record_line request.  */
11225
11226 static void
11227 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11228 {
11229   return;
11230 }
11231
11232 /* Subroutine of dwarf_decode_lines to simplify it.
11233    Process the line number information in LH.  */
11234
11235 static void
11236 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11237                       struct dwarf2_cu *cu, struct partial_symtab *pst)
11238 {
11239   gdb_byte *line_ptr, *extended_end;
11240   gdb_byte *line_end;
11241   unsigned int bytes_read, extended_len;
11242   unsigned char op_code, extended_op, adj_opcode;
11243   CORE_ADDR baseaddr;
11244   struct objfile *objfile = cu->objfile;
11245   bfd *abfd = objfile->obfd;
11246   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11247   const int decode_for_pst_p = (pst != NULL);
11248   struct subfile *last_subfile = NULL;
11249   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11250     = record_line;
11251
11252   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11253
11254   line_ptr = lh->statement_program_start;
11255   line_end = lh->statement_program_end;
11256
11257   /* Read the statement sequences until there's nothing left.  */
11258   while (line_ptr < line_end)
11259     {
11260       /* state machine registers  */
11261       CORE_ADDR address = 0;
11262       unsigned int file = 1;
11263       unsigned int line = 1;
11264       unsigned int column = 0;
11265       int is_stmt = lh->default_is_stmt;
11266       int basic_block = 0;
11267       int end_sequence = 0;
11268       CORE_ADDR addr;
11269       unsigned char op_index = 0;
11270
11271       if (!decode_for_pst_p && lh->num_file_names >= file)
11272         {
11273           /* Start a subfile for the current file of the state machine.  */
11274           /* lh->include_dirs and lh->file_names are 0-based, but the
11275              directory and file name numbers in the statement program
11276              are 1-based.  */
11277           struct file_entry *fe = &lh->file_names[file - 1];
11278           char *dir = NULL;
11279
11280           if (fe->dir_index)
11281             dir = lh->include_dirs[fe->dir_index - 1];
11282
11283           dwarf2_start_subfile (fe->name, dir, comp_dir);
11284         }
11285
11286       /* Decode the table.  */
11287       while (!end_sequence)
11288         {
11289           op_code = read_1_byte (abfd, line_ptr);
11290           line_ptr += 1;
11291           if (line_ptr > line_end)
11292             {
11293               dwarf2_debug_line_missing_end_sequence_complaint ();
11294               break;
11295             }
11296
11297           if (op_code >= lh->opcode_base)
11298             {
11299               /* Special operand.  */
11300               adj_opcode = op_code - lh->opcode_base;
11301               address += (((op_index + (adj_opcode / lh->line_range))
11302                            / lh->maximum_ops_per_instruction)
11303                           * lh->minimum_instruction_length);
11304               op_index = ((op_index + (adj_opcode / lh->line_range))
11305                           % lh->maximum_ops_per_instruction);
11306               line += lh->line_base + (adj_opcode % lh->line_range);
11307               if (lh->num_file_names < file || file == 0)
11308                 dwarf2_debug_line_missing_file_complaint ();
11309               /* For now we ignore lines not starting on an
11310                  instruction boundary.  */
11311               else if (op_index == 0)
11312                 {
11313                   lh->file_names[file - 1].included_p = 1;
11314                   if (!decode_for_pst_p && is_stmt)
11315                     {
11316                       if (last_subfile != current_subfile)
11317                         {
11318                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11319                           if (last_subfile)
11320                             (*p_record_line) (last_subfile, 0, addr);
11321                           last_subfile = current_subfile;
11322                         }
11323                       /* Append row to matrix using current values.  */
11324                       addr = check_cu_functions (address, cu);
11325                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
11326                       (*p_record_line) (current_subfile, line, addr);
11327                     }
11328                 }
11329               basic_block = 0;
11330             }
11331           else switch (op_code)
11332             {
11333             case DW_LNS_extended_op:
11334               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11335                                                    &bytes_read);
11336               line_ptr += bytes_read;
11337               extended_end = line_ptr + extended_len;
11338               extended_op = read_1_byte (abfd, line_ptr);
11339               line_ptr += 1;
11340               switch (extended_op)
11341                 {
11342                 case DW_LNE_end_sequence:
11343                   p_record_line = record_line;
11344                   end_sequence = 1;
11345                   break;
11346                 case DW_LNE_set_address:
11347                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11348
11349                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11350                     {
11351                       /* This line table is for a function which has been
11352                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11353
11354                       long line_offset
11355                         = line_ptr - dwarf2_per_objfile->line.buffer;
11356
11357                       complaint (&symfile_complaints,
11358                                  _(".debug_line address at offset 0x%lx is 0 "
11359                                    "[in module %s]"),
11360                                  line_offset, cu->objfile->name);
11361                       p_record_line = noop_record_line;
11362                     }
11363
11364                   op_index = 0;
11365                   line_ptr += bytes_read;
11366                   address += baseaddr;
11367                   break;
11368                 case DW_LNE_define_file:
11369                   {
11370                     char *cur_file;
11371                     unsigned int dir_index, mod_time, length;
11372
11373                     cur_file = read_direct_string (abfd, line_ptr,
11374                                                    &bytes_read);
11375                     line_ptr += bytes_read;
11376                     dir_index =
11377                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11378                     line_ptr += bytes_read;
11379                     mod_time =
11380                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11381                     line_ptr += bytes_read;
11382                     length =
11383                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11384                     line_ptr += bytes_read;
11385                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11386                   }
11387                   break;
11388                 case DW_LNE_set_discriminator:
11389                   /* The discriminator is not interesting to the debugger;
11390                      just ignore it.  */
11391                   line_ptr = extended_end;
11392                   break;
11393                 default:
11394                   complaint (&symfile_complaints,
11395                              _("mangled .debug_line section"));
11396                   return;
11397                 }
11398               /* Make sure that we parsed the extended op correctly.  If e.g.
11399                  we expected a different address size than the producer used,
11400                  we may have read the wrong number of bytes.  */
11401               if (line_ptr != extended_end)
11402                 {
11403                   complaint (&symfile_complaints,
11404                              _("mangled .debug_line section"));
11405                   return;
11406                 }
11407               break;
11408             case DW_LNS_copy:
11409               if (lh->num_file_names < file || file == 0)
11410                 dwarf2_debug_line_missing_file_complaint ();
11411               else
11412                 {
11413                   lh->file_names[file - 1].included_p = 1;
11414                   if (!decode_for_pst_p && is_stmt)
11415                     {
11416                       if (last_subfile != current_subfile)
11417                         {
11418                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11419                           if (last_subfile)
11420                             (*p_record_line) (last_subfile, 0, addr);
11421                           last_subfile = current_subfile;
11422                         }
11423                       addr = check_cu_functions (address, cu);
11424                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
11425                       (*p_record_line) (current_subfile, line, addr);
11426                     }
11427                 }
11428               basic_block = 0;
11429               break;
11430             case DW_LNS_advance_pc:
11431               {
11432                 CORE_ADDR adjust
11433                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11434
11435                 address += (((op_index + adjust)
11436                              / lh->maximum_ops_per_instruction)
11437                             * lh->minimum_instruction_length);
11438                 op_index = ((op_index + adjust)
11439                             % lh->maximum_ops_per_instruction);
11440                 line_ptr += bytes_read;
11441               }
11442               break;
11443             case DW_LNS_advance_line:
11444               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11445               line_ptr += bytes_read;
11446               break;
11447             case DW_LNS_set_file:
11448               {
11449                 /* The arrays lh->include_dirs and lh->file_names are
11450                    0-based, but the directory and file name numbers in
11451                    the statement program are 1-based.  */
11452                 struct file_entry *fe;
11453                 char *dir = NULL;
11454
11455                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11456                 line_ptr += bytes_read;
11457                 if (lh->num_file_names < file || file == 0)
11458                   dwarf2_debug_line_missing_file_complaint ();
11459                 else
11460                   {
11461                     fe = &lh->file_names[file - 1];
11462                     if (fe->dir_index)
11463                       dir = lh->include_dirs[fe->dir_index - 1];
11464                     if (!decode_for_pst_p)
11465                       {
11466                         last_subfile = current_subfile;
11467                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11468                       }
11469                   }
11470               }
11471               break;
11472             case DW_LNS_set_column:
11473               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11474               line_ptr += bytes_read;
11475               break;
11476             case DW_LNS_negate_stmt:
11477               is_stmt = (!is_stmt);
11478               break;
11479             case DW_LNS_set_basic_block:
11480               basic_block = 1;
11481               break;
11482             /* Add to the address register of the state machine the
11483                address increment value corresponding to special opcode
11484                255.  I.e., this value is scaled by the minimum
11485                instruction length since special opcode 255 would have
11486                scaled the increment.  */
11487             case DW_LNS_const_add_pc:
11488               {
11489                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11490
11491                 address += (((op_index + adjust)
11492                              / lh->maximum_ops_per_instruction)
11493                             * lh->minimum_instruction_length);
11494                 op_index = ((op_index + adjust)
11495                             % lh->maximum_ops_per_instruction);
11496               }
11497               break;
11498             case DW_LNS_fixed_advance_pc:
11499               address += read_2_bytes (abfd, line_ptr);
11500               op_index = 0;
11501               line_ptr += 2;
11502               break;
11503             default:
11504               {
11505                 /* Unknown standard opcode, ignore it.  */
11506                 int i;
11507
11508                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11509                   {
11510                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11511                     line_ptr += bytes_read;
11512                   }
11513               }
11514             }
11515         }
11516       if (lh->num_file_names < file || file == 0)
11517         dwarf2_debug_line_missing_file_complaint ();
11518       else
11519         {
11520           lh->file_names[file - 1].included_p = 1;
11521           if (!decode_for_pst_p)
11522             {
11523               addr = gdbarch_addr_bits_remove (gdbarch, address);
11524               (*p_record_line) (current_subfile, 0, addr);
11525             }
11526         }
11527     }
11528 }
11529
11530 /* Decode the Line Number Program (LNP) for the given line_header
11531    structure and CU.  The actual information extracted and the type
11532    of structures created from the LNP depends on the value of PST.
11533
11534    1. If PST is NULL, then this procedure uses the data from the program
11535       to create all necessary symbol tables, and their linetables.
11536
11537    2. If PST is not NULL, this procedure reads the program to determine
11538       the list of files included by the unit represented by PST, and
11539       builds all the associated partial symbol tables.
11540
11541    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11542    It is used for relative paths in the line table.
11543    NOTE: When processing partial symtabs (pst != NULL),
11544    comp_dir == pst->dirname.
11545
11546    NOTE: It is important that psymtabs have the same file name (via strcmp)
11547    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11548    symtab we don't use it in the name of the psymtabs we create.
11549    E.g. expand_line_sal requires this when finding psymtabs to expand.
11550    A good testcase for this is mb-inline.exp.  */
11551
11552 static void
11553 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11554                     struct dwarf2_cu *cu, struct partial_symtab *pst,
11555                     int want_line_info)
11556 {
11557   struct objfile *objfile = cu->objfile;
11558   const int decode_for_pst_p = (pst != NULL);
11559   struct subfile *first_subfile = current_subfile;
11560
11561   if (want_line_info)
11562     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11563
11564   if (decode_for_pst_p)
11565     {
11566       int file_index;
11567
11568       /* Now that we're done scanning the Line Header Program, we can
11569          create the psymtab of each included file.  */
11570       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11571         if (lh->file_names[file_index].included_p == 1)
11572           {
11573             char *include_name =
11574               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11575             if (include_name != NULL)
11576               dwarf2_create_include_psymtab (include_name, pst, objfile);
11577           }
11578     }
11579   else
11580     {
11581       /* Make sure a symtab is created for every file, even files
11582          which contain only variables (i.e. no code with associated
11583          line numbers).  */
11584       int i;
11585
11586       for (i = 0; i < lh->num_file_names; i++)
11587         {
11588           char *dir = NULL;
11589           struct file_entry *fe;
11590
11591           fe = &lh->file_names[i];
11592           if (fe->dir_index)
11593             dir = lh->include_dirs[fe->dir_index - 1];
11594           dwarf2_start_subfile (fe->name, dir, comp_dir);
11595
11596           /* Skip the main file; we don't need it, and it must be
11597              allocated last, so that it will show up before the
11598              non-primary symtabs in the objfile's symtab list.  */
11599           if (current_subfile == first_subfile)
11600             continue;
11601
11602           if (current_subfile->symtab == NULL)
11603             current_subfile->symtab = allocate_symtab (current_subfile->name,
11604                                                        cu->objfile);
11605           fe->symtab = current_subfile->symtab;
11606         }
11607     }
11608 }
11609
11610 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11611    DIRNAME the name of the source directory which contains FILENAME
11612    or NULL if not known.  COMP_DIR is the compilation directory for the
11613    linetable's compilation unit or NULL if not known.
11614    This routine tries to keep line numbers from identical absolute and
11615    relative file names in a common subfile.
11616
11617    Using the `list' example from the GDB testsuite, which resides in
11618    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11619    of /srcdir/list0.c yields the following debugging information for list0.c:
11620
11621    DW_AT_name:          /srcdir/list0.c
11622    DW_AT_comp_dir:              /compdir
11623    files.files[0].name: list0.h
11624    files.files[0].dir:  /srcdir
11625    files.files[1].name: list0.c
11626    files.files[1].dir:  /srcdir
11627
11628    The line number information for list0.c has to end up in a single
11629    subfile, so that `break /srcdir/list0.c:1' works as expected.
11630    start_subfile will ensure that this happens provided that we pass the
11631    concatenation of files.files[1].dir and files.files[1].name as the
11632    subfile's name.  */
11633
11634 static void
11635 dwarf2_start_subfile (char *filename, const char *dirname,
11636                       const char *comp_dir)
11637 {
11638   char *fullname;
11639
11640   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11641      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11642      second argument to start_subfile.  To be consistent, we do the
11643      same here.  In order not to lose the line information directory,
11644      we concatenate it to the filename when it makes sense.
11645      Note that the Dwarf3 standard says (speaking of filenames in line
11646      information): ``The directory index is ignored for file names
11647      that represent full path names''.  Thus ignoring dirname in the
11648      `else' branch below isn't an issue.  */
11649
11650   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11651     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11652   else
11653     fullname = filename;
11654
11655   start_subfile (fullname, comp_dir);
11656
11657   if (fullname != filename)
11658     xfree (fullname);
11659 }
11660
11661 static void
11662 var_decode_location (struct attribute *attr, struct symbol *sym,
11663                      struct dwarf2_cu *cu)
11664 {
11665   struct objfile *objfile = cu->objfile;
11666   struct comp_unit_head *cu_header = &cu->header;
11667
11668   /* NOTE drow/2003-01-30: There used to be a comment and some special
11669      code here to turn a symbol with DW_AT_external and a
11670      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11671      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11672      with some versions of binutils) where shared libraries could have
11673      relocations against symbols in their debug information - the
11674      minimal symbol would have the right address, but the debug info
11675      would not.  It's no longer necessary, because we will explicitly
11676      apply relocations when we read in the debug information now.  */
11677
11678   /* A DW_AT_location attribute with no contents indicates that a
11679      variable has been optimized away.  */
11680   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11681     {
11682       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11683       return;
11684     }
11685
11686   /* Handle one degenerate form of location expression specially, to
11687      preserve GDB's previous behavior when section offsets are
11688      specified.  If this is just a DW_OP_addr then mark this symbol
11689      as LOC_STATIC.  */
11690
11691   if (attr_form_is_block (attr)
11692       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11693       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11694     {
11695       unsigned int dummy;
11696
11697       SYMBOL_VALUE_ADDRESS (sym) =
11698         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11699       SYMBOL_CLASS (sym) = LOC_STATIC;
11700       fixup_symbol_section (sym, objfile);
11701       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11702                                               SYMBOL_SECTION (sym));
11703       return;
11704     }
11705
11706   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11707      expression evaluator, and use LOC_COMPUTED only when necessary
11708      (i.e. when the value of a register or memory location is
11709      referenced, or a thread-local block, etc.).  Then again, it might
11710      not be worthwhile.  I'm assuming that it isn't unless performance
11711      or memory numbers show me otherwise.  */
11712
11713   dwarf2_symbol_mark_computed (attr, sym, cu);
11714   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11715
11716   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11717     cu->has_loclist = 1;
11718 }
11719
11720 /* Given a pointer to a DWARF information entry, figure out if we need
11721    to make a symbol table entry for it, and if so, create a new entry
11722    and return a pointer to it.
11723    If TYPE is NULL, determine symbol type from the die, otherwise
11724    used the passed type.
11725    If SPACE is not NULL, use it to hold the new symbol.  If it is
11726    NULL, allocate a new symbol on the objfile's obstack.  */
11727
11728 static struct symbol *
11729 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11730                  struct symbol *space)
11731 {
11732   struct objfile *objfile = cu->objfile;
11733   struct symbol *sym = NULL;
11734   char *name;
11735   struct attribute *attr = NULL;
11736   struct attribute *attr2 = NULL;
11737   CORE_ADDR baseaddr;
11738   struct pending **list_to_add = NULL;
11739
11740   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11741
11742   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11743
11744   name = dwarf2_name (die, cu);
11745   if (name)
11746     {
11747       const char *linkagename;
11748       int suppress_add = 0;
11749
11750       if (space)
11751         sym = space;
11752       else
11753         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11754       OBJSTAT (objfile, n_syms++);
11755
11756       /* Cache this symbol's name and the name's demangled form (if any).  */
11757       SYMBOL_SET_LANGUAGE (sym, cu->language);
11758       linkagename = dwarf2_physname (name, die, cu);
11759       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11760
11761       /* Fortran does not have mangling standard and the mangling does differ
11762          between gfortran, iFort etc.  */
11763       if (cu->language == language_fortran
11764           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11765         symbol_set_demangled_name (&(sym->ginfo),
11766                                    (char *) dwarf2_full_name (name, die, cu),
11767                                    NULL);
11768
11769       /* Default assumptions.
11770          Use the passed type or decode it from the die.  */
11771       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11772       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11773       if (type != NULL)
11774         SYMBOL_TYPE (sym) = type;
11775       else
11776         SYMBOL_TYPE (sym) = die_type (die, cu);
11777       attr = dwarf2_attr (die,
11778                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11779                           cu);
11780       if (attr)
11781         {
11782           SYMBOL_LINE (sym) = DW_UNSND (attr);
11783         }
11784
11785       attr = dwarf2_attr (die,
11786                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11787                           cu);
11788       if (attr)
11789         {
11790           int file_index = DW_UNSND (attr);
11791
11792           if (cu->line_header == NULL
11793               || file_index > cu->line_header->num_file_names)
11794             complaint (&symfile_complaints,
11795                        _("file index out of range"));
11796           else if (file_index > 0)
11797             {
11798               struct file_entry *fe;
11799
11800               fe = &cu->line_header->file_names[file_index - 1];
11801               SYMBOL_SYMTAB (sym) = fe->symtab;
11802             }
11803         }
11804
11805       switch (die->tag)
11806         {
11807         case DW_TAG_label:
11808           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11809           if (attr)
11810             {
11811               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11812             }
11813           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11814           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11815           SYMBOL_CLASS (sym) = LOC_LABEL;
11816           add_symbol_to_list (sym, cu->list_in_scope);
11817           break;
11818         case DW_TAG_subprogram:
11819           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11820              finish_block.  */
11821           SYMBOL_CLASS (sym) = LOC_BLOCK;
11822           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11823           if ((attr2 && (DW_UNSND (attr2) != 0))
11824               || cu->language == language_ada)
11825             {
11826               /* Subprograms marked external are stored as a global symbol.
11827                  Ada subprograms, whether marked external or not, are always
11828                  stored as a global symbol, because we want to be able to
11829                  access them globally.  For instance, we want to be able
11830                  to break on a nested subprogram without having to
11831                  specify the context.  */
11832               list_to_add = &global_symbols;
11833             }
11834           else
11835             {
11836               list_to_add = cu->list_in_scope;
11837             }
11838           break;
11839         case DW_TAG_inlined_subroutine:
11840           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11841              finish_block.  */
11842           SYMBOL_CLASS (sym) = LOC_BLOCK;
11843           SYMBOL_INLINED (sym) = 1;
11844           /* Do not add the symbol to any lists.  It will be found via
11845              BLOCK_FUNCTION from the blockvector.  */
11846           break;
11847         case DW_TAG_template_value_param:
11848           suppress_add = 1;
11849           /* Fall through.  */
11850         case DW_TAG_constant:
11851         case DW_TAG_variable:
11852         case DW_TAG_member:
11853           /* Compilation with minimal debug info may result in
11854              variables with missing type entries.  Change the
11855              misleading `void' type to something sensible.  */
11856           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11857             SYMBOL_TYPE (sym)
11858               = objfile_type (objfile)->nodebug_data_symbol;
11859
11860           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11861           /* In the case of DW_TAG_member, we should only be called for
11862              static const members.  */
11863           if (die->tag == DW_TAG_member)
11864             {
11865               /* dwarf2_add_field uses die_is_declaration,
11866                  so we do the same.  */
11867               gdb_assert (die_is_declaration (die, cu));
11868               gdb_assert (attr);
11869             }
11870           if (attr)
11871             {
11872               dwarf2_const_value (attr, sym, cu);
11873               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11874               if (!suppress_add)
11875                 {
11876                   if (attr2 && (DW_UNSND (attr2) != 0))
11877                     list_to_add = &global_symbols;
11878                   else
11879                     list_to_add = cu->list_in_scope;
11880                 }
11881               break;
11882             }
11883           attr = dwarf2_attr (die, DW_AT_location, cu);
11884           if (attr)
11885             {
11886               var_decode_location (attr, sym, cu);
11887               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11888               if (SYMBOL_CLASS (sym) == LOC_STATIC
11889                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11890                   && !dwarf2_per_objfile->has_section_at_zero)
11891                 {
11892                   /* When a static variable is eliminated by the linker,
11893                      the corresponding debug information is not stripped
11894                      out, but the variable address is set to null;
11895                      do not add such variables into symbol table.  */
11896                 }
11897               else if (attr2 && (DW_UNSND (attr2) != 0))
11898                 {
11899                   /* Workaround gfortran PR debug/40040 - it uses
11900                      DW_AT_location for variables in -fPIC libraries which may
11901                      get overriden by other libraries/executable and get
11902                      a different address.  Resolve it by the minimal symbol
11903                      which may come from inferior's executable using copy
11904                      relocation.  Make this workaround only for gfortran as for
11905                      other compilers GDB cannot guess the minimal symbol
11906                      Fortran mangling kind.  */
11907                   if (cu->language == language_fortran && die->parent
11908                       && die->parent->tag == DW_TAG_module
11909                       && cu->producer
11910                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11911                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11912
11913                   /* A variable with DW_AT_external is never static,
11914                      but it may be block-scoped.  */
11915                   list_to_add = (cu->list_in_scope == &file_symbols
11916                                  ? &global_symbols : cu->list_in_scope);
11917                 }
11918               else
11919                 list_to_add = cu->list_in_scope;
11920             }
11921           else
11922             {
11923               /* We do not know the address of this symbol.
11924                  If it is an external symbol and we have type information
11925                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11926                  The address of the variable will then be determined from
11927                  the minimal symbol table whenever the variable is
11928                  referenced.  */
11929               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11930               if (attr2 && (DW_UNSND (attr2) != 0)
11931                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11932                 {
11933                   /* A variable with DW_AT_external is never static, but it
11934                      may be block-scoped.  */
11935                   list_to_add = (cu->list_in_scope == &file_symbols
11936                                  ? &global_symbols : cu->list_in_scope);
11937
11938                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11939                 }
11940               else if (!die_is_declaration (die, cu))
11941                 {
11942                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11943                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11944                   if (!suppress_add)
11945                     list_to_add = cu->list_in_scope;
11946                 }
11947             }
11948           break;
11949         case DW_TAG_formal_parameter:
11950           /* If we are inside a function, mark this as an argument.  If
11951              not, we might be looking at an argument to an inlined function
11952              when we do not have enough information to show inlined frames;
11953              pretend it's a local variable in that case so that the user can
11954              still see it.  */
11955           if (context_stack_depth > 0
11956               && context_stack[context_stack_depth - 1].name != NULL)
11957             SYMBOL_IS_ARGUMENT (sym) = 1;
11958           attr = dwarf2_attr (die, DW_AT_location, cu);
11959           if (attr)
11960             {
11961               var_decode_location (attr, sym, cu);
11962             }
11963           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11964           if (attr)
11965             {
11966               dwarf2_const_value (attr, sym, cu);
11967             }
11968
11969           list_to_add = cu->list_in_scope;
11970           break;
11971         case DW_TAG_unspecified_parameters:
11972           /* From varargs functions; gdb doesn't seem to have any
11973              interest in this information, so just ignore it for now.
11974              (FIXME?) */
11975           break;
11976         case DW_TAG_template_type_param:
11977           suppress_add = 1;
11978           /* Fall through.  */
11979         case DW_TAG_class_type:
11980         case DW_TAG_interface_type:
11981         case DW_TAG_structure_type:
11982         case DW_TAG_union_type:
11983         case DW_TAG_set_type:
11984         case DW_TAG_enumeration_type:
11985           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11986           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11987
11988           {
11989             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11990                really ever be static objects: otherwise, if you try
11991                to, say, break of a class's method and you're in a file
11992                which doesn't mention that class, it won't work unless
11993                the check for all static symbols in lookup_symbol_aux
11994                saves you.  See the OtherFileClass tests in
11995                gdb.c++/namespace.exp.  */
11996
11997             if (!suppress_add)
11998               {
11999                 list_to_add = (cu->list_in_scope == &file_symbols
12000                                && (cu->language == language_cplus
12001                                    || cu->language == language_java)
12002                                ? &global_symbols : cu->list_in_scope);
12003
12004                 /* The semantics of C++ state that "struct foo {
12005                    ... }" also defines a typedef for "foo".  A Java
12006                    class declaration also defines a typedef for the
12007                    class.  */
12008                 if (cu->language == language_cplus
12009                     || cu->language == language_java
12010                     || cu->language == language_ada)
12011                   {
12012                     /* The symbol's name is already allocated along
12013                        with this objfile, so we don't need to
12014                        duplicate it for the type.  */
12015                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
12016                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
12017                   }
12018               }
12019           }
12020           break;
12021         case DW_TAG_typedef:
12022           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12023           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
12024           list_to_add = cu->list_in_scope;
12025           break;
12026         case DW_TAG_base_type:
12027         case DW_TAG_subrange_type:
12028           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12029           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
12030           list_to_add = cu->list_in_scope;
12031           break;
12032         case DW_TAG_enumerator:
12033           attr = dwarf2_attr (die, DW_AT_const_value, cu);
12034           if (attr)
12035             {
12036               dwarf2_const_value (attr, sym, cu);
12037             }
12038           {
12039             /* NOTE: carlton/2003-11-10: See comment above in the
12040                DW_TAG_class_type, etc. block.  */
12041
12042             list_to_add = (cu->list_in_scope == &file_symbols
12043                            && (cu->language == language_cplus
12044                                || cu->language == language_java)
12045                            ? &global_symbols : cu->list_in_scope);
12046           }
12047           break;
12048         case DW_TAG_namespace:
12049           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12050           list_to_add = &global_symbols;
12051           break;
12052         default:
12053           /* Not a tag we recognize.  Hopefully we aren't processing
12054              trash data, but since we must specifically ignore things
12055              we don't recognize, there is nothing else we should do at
12056              this point.  */
12057           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
12058                      dwarf_tag_name (die->tag));
12059           break;
12060         }
12061
12062       if (suppress_add)
12063         {
12064           sym->hash_next = objfile->template_symbols;
12065           objfile->template_symbols = sym;
12066           list_to_add = NULL;
12067         }
12068
12069       if (list_to_add != NULL)
12070         add_symbol_to_list (sym, list_to_add);
12071
12072       /* For the benefit of old versions of GCC, check for anonymous
12073          namespaces based on the demangled name.  */
12074       if (!processing_has_namespace_info
12075           && cu->language == language_cplus)
12076         cp_scan_for_anonymous_namespaces (sym, objfile);
12077     }
12078   return (sym);
12079 }
12080
12081 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
12082
12083 static struct symbol *
12084 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12085 {
12086   return new_symbol_full (die, type, cu, NULL);
12087 }
12088
12089 /* Given an attr with a DW_FORM_dataN value in host byte order,
12090    zero-extend it as appropriate for the symbol's type.  The DWARF
12091    standard (v4) is not entirely clear about the meaning of using
12092    DW_FORM_dataN for a constant with a signed type, where the type is
12093    wider than the data.  The conclusion of a discussion on the DWARF
12094    list was that this is unspecified.  We choose to always zero-extend
12095    because that is the interpretation long in use by GCC.  */
12096
12097 static gdb_byte *
12098 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12099                          const char *name, struct obstack *obstack,
12100                          struct dwarf2_cu *cu, long *value, int bits)
12101 {
12102   struct objfile *objfile = cu->objfile;
12103   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12104                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12105   LONGEST l = DW_UNSND (attr);
12106
12107   if (bits < sizeof (*value) * 8)
12108     {
12109       l &= ((LONGEST) 1 << bits) - 1;
12110       *value = l;
12111     }
12112   else if (bits == sizeof (*value) * 8)
12113     *value = l;
12114   else
12115     {
12116       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12117       store_unsigned_integer (bytes, bits / 8, byte_order, l);
12118       return bytes;
12119     }
12120
12121   return NULL;
12122 }
12123
12124 /* Read a constant value from an attribute.  Either set *VALUE, or if
12125    the value does not fit in *VALUE, set *BYTES - either already
12126    allocated on the objfile obstack, or newly allocated on OBSTACK,
12127    or, set *BATON, if we translated the constant to a location
12128    expression.  */
12129
12130 static void
12131 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12132                          const char *name, struct obstack *obstack,
12133                          struct dwarf2_cu *cu,
12134                          long *value, gdb_byte **bytes,
12135                          struct dwarf2_locexpr_baton **baton)
12136 {
12137   struct objfile *objfile = cu->objfile;
12138   struct comp_unit_head *cu_header = &cu->header;
12139   struct dwarf_block *blk;
12140   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12141                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12142
12143   *value = 0;
12144   *bytes = NULL;
12145   *baton = NULL;
12146
12147   switch (attr->form)
12148     {
12149     case DW_FORM_addr:
12150       {
12151         gdb_byte *data;
12152
12153         if (TYPE_LENGTH (type) != cu_header->addr_size)
12154           dwarf2_const_value_length_mismatch_complaint (name,
12155                                                         cu_header->addr_size,
12156                                                         TYPE_LENGTH (type));
12157         /* Symbols of this form are reasonably rare, so we just
12158            piggyback on the existing location code rather than writing
12159            a new implementation of symbol_computed_ops.  */
12160         *baton = obstack_alloc (&objfile->objfile_obstack,
12161                                 sizeof (struct dwarf2_locexpr_baton));
12162         (*baton)->per_cu = cu->per_cu;
12163         gdb_assert ((*baton)->per_cu);
12164
12165         (*baton)->size = 2 + cu_header->addr_size;
12166         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12167         (*baton)->data = data;
12168
12169         data[0] = DW_OP_addr;
12170         store_unsigned_integer (&data[1], cu_header->addr_size,
12171                                 byte_order, DW_ADDR (attr));
12172         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12173       }
12174       break;
12175     case DW_FORM_string:
12176     case DW_FORM_strp:
12177       /* DW_STRING is already allocated on the objfile obstack, point
12178          directly to it.  */
12179       *bytes = (gdb_byte *) DW_STRING (attr);
12180       break;
12181     case DW_FORM_block1:
12182     case DW_FORM_block2:
12183     case DW_FORM_block4:
12184     case DW_FORM_block:
12185     case DW_FORM_exprloc:
12186       blk = DW_BLOCK (attr);
12187       if (TYPE_LENGTH (type) != blk->size)
12188         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12189                                                       TYPE_LENGTH (type));
12190       *bytes = blk->data;
12191       break;
12192
12193       /* The DW_AT_const_value attributes are supposed to carry the
12194          symbol's value "represented as it would be on the target
12195          architecture."  By the time we get here, it's already been
12196          converted to host endianness, so we just need to sign- or
12197          zero-extend it as appropriate.  */
12198     case DW_FORM_data1:
12199       *bytes = dwarf2_const_value_data (attr, type, name,
12200                                         obstack, cu, value, 8);
12201       break;
12202     case DW_FORM_data2:
12203       *bytes = dwarf2_const_value_data (attr, type, name,
12204                                         obstack, cu, value, 16);
12205       break;
12206     case DW_FORM_data4:
12207       *bytes = dwarf2_const_value_data (attr, type, name,
12208                                         obstack, cu, value, 32);
12209       break;
12210     case DW_FORM_data8:
12211       *bytes = dwarf2_const_value_data (attr, type, name,
12212                                         obstack, cu, value, 64);
12213       break;
12214
12215     case DW_FORM_sdata:
12216       *value = DW_SND (attr);
12217       break;
12218
12219     case DW_FORM_udata:
12220       *value = DW_UNSND (attr);
12221       break;
12222
12223     default:
12224       complaint (&symfile_complaints,
12225                  _("unsupported const value attribute form: '%s'"),
12226                  dwarf_form_name (attr->form));
12227       *value = 0;
12228       break;
12229     }
12230 }
12231
12232
12233 /* Copy constant value from an attribute to a symbol.  */
12234
12235 static void
12236 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12237                     struct dwarf2_cu *cu)
12238 {
12239   struct objfile *objfile = cu->objfile;
12240   struct comp_unit_head *cu_header = &cu->header;
12241   long value;
12242   gdb_byte *bytes;
12243   struct dwarf2_locexpr_baton *baton;
12244
12245   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12246                            SYMBOL_PRINT_NAME (sym),
12247                            &objfile->objfile_obstack, cu,
12248                            &value, &bytes, &baton);
12249
12250   if (baton != NULL)
12251     {
12252       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12253       SYMBOL_LOCATION_BATON (sym) = baton;
12254       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12255     }
12256   else if (bytes != NULL)
12257      {
12258       SYMBOL_VALUE_BYTES (sym) = bytes;
12259       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12260     }
12261   else
12262     {
12263       SYMBOL_VALUE (sym) = value;
12264       SYMBOL_CLASS (sym) = LOC_CONST;
12265     }
12266 }
12267
12268 /* Return the type of the die in question using its DW_AT_type attribute.  */
12269
12270 static struct type *
12271 die_type (struct die_info *die, struct dwarf2_cu *cu)
12272 {
12273   struct attribute *type_attr;
12274
12275   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12276   if (!type_attr)
12277     {
12278       /* A missing DW_AT_type represents a void type.  */
12279       return objfile_type (cu->objfile)->builtin_void;
12280     }
12281
12282   return lookup_die_type (die, type_attr, cu);
12283 }
12284
12285 /* True iff CU's producer generates GNAT Ada auxiliary information
12286    that allows to find parallel types through that information instead
12287    of having to do expensive parallel lookups by type name.  */
12288
12289 static int
12290 need_gnat_info (struct dwarf2_cu *cu)
12291 {
12292   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12293      of GNAT produces this auxiliary information, without any indication
12294      that it is produced.  Part of enhancing the FSF version of GNAT
12295      to produce that information will be to put in place an indicator
12296      that we can use in order to determine whether the descriptive type
12297      info is available or not.  One suggestion that has been made is
12298      to use a new attribute, attached to the CU die.  For now, assume
12299      that the descriptive type info is not available.  */
12300   return 0;
12301 }
12302
12303 /* Return the auxiliary type of the die in question using its
12304    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12305    attribute is not present.  */
12306
12307 static struct type *
12308 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12309 {
12310   struct attribute *type_attr;
12311
12312   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12313   if (!type_attr)
12314     return NULL;
12315
12316   return lookup_die_type (die, type_attr, cu);
12317 }
12318
12319 /* If DIE has a descriptive_type attribute, then set the TYPE's
12320    descriptive type accordingly.  */
12321
12322 static void
12323 set_descriptive_type (struct type *type, struct die_info *die,
12324                       struct dwarf2_cu *cu)
12325 {
12326   struct type *descriptive_type = die_descriptive_type (die, cu);
12327
12328   if (descriptive_type)
12329     {
12330       ALLOCATE_GNAT_AUX_TYPE (type);
12331       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12332     }
12333 }
12334
12335 /* Return the containing type of the die in question using its
12336    DW_AT_containing_type attribute.  */
12337
12338 static struct type *
12339 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12340 {
12341   struct attribute *type_attr;
12342
12343   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12344   if (!type_attr)
12345     error (_("Dwarf Error: Problem turning containing type into gdb type "
12346              "[in module %s]"), cu->objfile->name);
12347
12348   return lookup_die_type (die, type_attr, cu);
12349 }
12350
12351 /* Look up the type of DIE in CU using its type attribute ATTR.
12352    If there is no type substitute an error marker.  */
12353
12354 static struct type *
12355 lookup_die_type (struct die_info *die, struct attribute *attr,
12356                  struct dwarf2_cu *cu)
12357 {
12358   struct type *this_type;
12359
12360   /* First see if we have it cached.  */
12361
12362   if (is_ref_attr (attr))
12363     {
12364       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12365
12366       this_type = get_die_type_at_offset (offset, cu->per_cu);
12367     }
12368   else if (attr->form == DW_FORM_ref_sig8)
12369     {
12370       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12371       struct dwarf2_cu *sig_cu;
12372       unsigned int offset;
12373
12374       /* sig_type will be NULL if the signatured type is missing from
12375          the debug info.  */
12376       if (sig_type == NULL)
12377         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12378                  "at 0x%x [in module %s]"),
12379                die->offset, cu->objfile->name);
12380
12381       gdb_assert (sig_type->per_cu.debug_types_section);
12382       offset = sig_type->per_cu.offset + sig_type->type_offset;
12383       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12384     }
12385   else
12386     {
12387       dump_die_for_error (die);
12388       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12389              dwarf_attr_name (attr->name), cu->objfile->name);
12390     }
12391
12392   /* If not cached we need to read it in.  */
12393
12394   if (this_type == NULL)
12395     {
12396       struct die_info *type_die;
12397       struct dwarf2_cu *type_cu = cu;
12398
12399       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12400       /* If the type is cached, we should have found it above.  */
12401       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12402       this_type = read_type_die_1 (type_die, type_cu);
12403     }
12404
12405   /* If we still don't have a type use an error marker.  */
12406
12407   if (this_type == NULL)
12408     {
12409       char *message, *saved;
12410
12411       /* read_type_die already issued a complaint.  */
12412       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12413                             cu->objfile->name,
12414                             cu->header.offset,
12415                             die->offset);
12416       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12417                              message, strlen (message));
12418       xfree (message);
12419
12420       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12421     }
12422
12423   return this_type;
12424 }
12425
12426 /* Return the type in DIE, CU.
12427    Returns NULL for invalid types.
12428
12429    This first does a lookup in the appropriate type_hash table,
12430    and only reads the die in if necessary.
12431
12432    NOTE: This can be called when reading in partial or full symbols.  */
12433
12434 static struct type *
12435 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12436 {
12437   struct type *this_type;
12438
12439   this_type = get_die_type (die, cu);
12440   if (this_type)
12441     return this_type;
12442
12443   return read_type_die_1 (die, cu);
12444 }
12445
12446 /* Read the type in DIE, CU.
12447    Returns NULL for invalid types.  */
12448
12449 static struct type *
12450 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12451 {
12452   struct type *this_type = NULL;
12453
12454   switch (die->tag)
12455     {
12456     case DW_TAG_class_type:
12457     case DW_TAG_interface_type:
12458     case DW_TAG_structure_type:
12459     case DW_TAG_union_type:
12460       this_type = read_structure_type (die, cu);
12461       break;
12462     case DW_TAG_enumeration_type:
12463       this_type = read_enumeration_type (die, cu);
12464       break;
12465     case DW_TAG_subprogram:
12466     case DW_TAG_subroutine_type:
12467     case DW_TAG_inlined_subroutine:
12468       this_type = read_subroutine_type (die, cu);
12469       break;
12470     case DW_TAG_array_type:
12471       this_type = read_array_type (die, cu);
12472       break;
12473     case DW_TAG_set_type:
12474       this_type = read_set_type (die, cu);
12475       break;
12476     case DW_TAG_pointer_type:
12477       this_type = read_tag_pointer_type (die, cu);
12478       break;
12479     case DW_TAG_ptr_to_member_type:
12480       this_type = read_tag_ptr_to_member_type (die, cu);
12481       break;
12482     case DW_TAG_reference_type:
12483       this_type = read_tag_reference_type (die, cu);
12484       break;
12485     case DW_TAG_const_type:
12486       this_type = read_tag_const_type (die, cu);
12487       break;
12488     case DW_TAG_volatile_type:
12489       this_type = read_tag_volatile_type (die, cu);
12490       break;
12491     case DW_TAG_string_type:
12492       this_type = read_tag_string_type (die, cu);
12493       break;
12494     case DW_TAG_typedef:
12495       this_type = read_typedef (die, cu);
12496       break;
12497     case DW_TAG_subrange_type:
12498       this_type = read_subrange_type (die, cu);
12499       break;
12500     case DW_TAG_base_type:
12501       this_type = read_base_type (die, cu);
12502       break;
12503     case DW_TAG_unspecified_type:
12504       this_type = read_unspecified_type (die, cu);
12505       break;
12506     case DW_TAG_namespace:
12507       this_type = read_namespace_type (die, cu);
12508       break;
12509     case DW_TAG_module:
12510       this_type = read_module_type (die, cu);
12511       break;
12512     default:
12513       complaint (&symfile_complaints,
12514                  _("unexpected tag in read_type_die: '%s'"),
12515                  dwarf_tag_name (die->tag));
12516       break;
12517     }
12518
12519   return this_type;
12520 }
12521
12522 /* See if we can figure out if the class lives in a namespace.  We do
12523    this by looking for a member function; its demangled name will
12524    contain namespace info, if there is any.
12525    Return the computed name or NULL.
12526    Space for the result is allocated on the objfile's obstack.
12527    This is the full-die version of guess_partial_die_structure_name.
12528    In this case we know DIE has no useful parent.  */
12529
12530 static char *
12531 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12532 {
12533   struct die_info *spec_die;
12534   struct dwarf2_cu *spec_cu;
12535   struct die_info *child;
12536
12537   spec_cu = cu;
12538   spec_die = die_specification (die, &spec_cu);
12539   if (spec_die != NULL)
12540     {
12541       die = spec_die;
12542       cu = spec_cu;
12543     }
12544
12545   for (child = die->child;
12546        child != NULL;
12547        child = child->sibling)
12548     {
12549       if (child->tag == DW_TAG_subprogram)
12550         {
12551           struct attribute *attr;
12552
12553           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12554           if (attr == NULL)
12555             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12556           if (attr != NULL)
12557             {
12558               char *actual_name
12559                 = language_class_name_from_physname (cu->language_defn,
12560                                                      DW_STRING (attr));
12561               char *name = NULL;
12562
12563               if (actual_name != NULL)
12564                 {
12565                   char *die_name = dwarf2_name (die, cu);
12566
12567                   if (die_name != NULL
12568                       && strcmp (die_name, actual_name) != 0)
12569                     {
12570                       /* Strip off the class name from the full name.
12571                          We want the prefix.  */
12572                       int die_name_len = strlen (die_name);
12573                       int actual_name_len = strlen (actual_name);
12574
12575                       /* Test for '::' as a sanity check.  */
12576                       if (actual_name_len > die_name_len + 2
12577                           && actual_name[actual_name_len
12578                                          - die_name_len - 1] == ':')
12579                         name =
12580                           obsavestring (actual_name,
12581                                         actual_name_len - die_name_len - 2,
12582                                         &cu->objfile->objfile_obstack);
12583                     }
12584                 }
12585               xfree (actual_name);
12586               return name;
12587             }
12588         }
12589     }
12590
12591   return NULL;
12592 }
12593
12594 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12595    prefix part in such case.  See
12596    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12597
12598 static char *
12599 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12600 {
12601   struct attribute *attr;
12602   char *base;
12603
12604   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12605       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12606     return NULL;
12607
12608   attr = dwarf2_attr (die, DW_AT_name, cu);
12609   if (attr != NULL && DW_STRING (attr) != NULL)
12610     return NULL;
12611
12612   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12613   if (attr == NULL)
12614     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12615   if (attr == NULL || DW_STRING (attr) == NULL)
12616     return NULL;
12617
12618   /* dwarf2_name had to be already called.  */
12619   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12620
12621   /* Strip the base name, keep any leading namespaces/classes.  */
12622   base = strrchr (DW_STRING (attr), ':');
12623   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12624     return "";
12625
12626   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12627                        &cu->objfile->objfile_obstack);
12628 }
12629
12630 /* Return the name of the namespace/class that DIE is defined within,
12631    or "" if we can't tell.  The caller should not xfree the result.
12632
12633    For example, if we're within the method foo() in the following
12634    code:
12635
12636    namespace N {
12637      class C {
12638        void foo () {
12639        }
12640      };
12641    }
12642
12643    then determine_prefix on foo's die will return "N::C".  */
12644
12645 static char *
12646 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12647 {
12648   struct die_info *parent, *spec_die;
12649   struct dwarf2_cu *spec_cu;
12650   struct type *parent_type;
12651   char *retval;
12652
12653   if (cu->language != language_cplus && cu->language != language_java
12654       && cu->language != language_fortran)
12655     return "";
12656
12657   retval = anonymous_struct_prefix (die, cu);
12658   if (retval)
12659     return retval;
12660
12661   /* We have to be careful in the presence of DW_AT_specification.
12662      For example, with GCC 3.4, given the code
12663
12664      namespace N {
12665        void foo() {
12666          // Definition of N::foo.
12667        }
12668      }
12669
12670      then we'll have a tree of DIEs like this:
12671
12672      1: DW_TAG_compile_unit
12673        2: DW_TAG_namespace        // N
12674          3: DW_TAG_subprogram     // declaration of N::foo
12675        4: DW_TAG_subprogram       // definition of N::foo
12676             DW_AT_specification   // refers to die #3
12677
12678      Thus, when processing die #4, we have to pretend that we're in
12679      the context of its DW_AT_specification, namely the contex of die
12680      #3.  */
12681   spec_cu = cu;
12682   spec_die = die_specification (die, &spec_cu);
12683   if (spec_die == NULL)
12684     parent = die->parent;
12685   else
12686     {
12687       parent = spec_die->parent;
12688       cu = spec_cu;
12689     }
12690
12691   if (parent == NULL)
12692     return "";
12693   else if (parent->building_fullname)
12694     {
12695       const char *name;
12696       const char *parent_name;
12697
12698       /* It has been seen on RealView 2.2 built binaries,
12699          DW_TAG_template_type_param types actually _defined_ as
12700          children of the parent class:
12701
12702          enum E {};
12703          template class <class Enum> Class{};
12704          Class<enum E> class_e;
12705
12706          1: DW_TAG_class_type (Class)
12707            2: DW_TAG_enumeration_type (E)
12708              3: DW_TAG_enumerator (enum1:0)
12709              3: DW_TAG_enumerator (enum2:1)
12710              ...
12711            2: DW_TAG_template_type_param
12712               DW_AT_type  DW_FORM_ref_udata (E)
12713
12714          Besides being broken debug info, it can put GDB into an
12715          infinite loop.  Consider:
12716
12717          When we're building the full name for Class<E>, we'll start
12718          at Class, and go look over its template type parameters,
12719          finding E.  We'll then try to build the full name of E, and
12720          reach here.  We're now trying to build the full name of E,
12721          and look over the parent DIE for containing scope.  In the
12722          broken case, if we followed the parent DIE of E, we'd again
12723          find Class, and once again go look at its template type
12724          arguments, etc., etc.  Simply don't consider such parent die
12725          as source-level parent of this die (it can't be, the language
12726          doesn't allow it), and break the loop here.  */
12727       name = dwarf2_name (die, cu);
12728       parent_name = dwarf2_name (parent, cu);
12729       complaint (&symfile_complaints,
12730                  _("template param type '%s' defined within parent '%s'"),
12731                  name ? name : "<unknown>",
12732                  parent_name ? parent_name : "<unknown>");
12733       return "";
12734     }
12735   else
12736     switch (parent->tag)
12737       {
12738       case DW_TAG_namespace:
12739         parent_type = read_type_die (parent, cu);
12740         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12741            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12742            Work around this problem here.  */
12743         if (cu->language == language_cplus
12744             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12745           return "";
12746         /* We give a name to even anonymous namespaces.  */
12747         return TYPE_TAG_NAME (parent_type);
12748       case DW_TAG_class_type:
12749       case DW_TAG_interface_type:
12750       case DW_TAG_structure_type:
12751       case DW_TAG_union_type:
12752       case DW_TAG_module:
12753         parent_type = read_type_die (parent, cu);
12754         if (TYPE_TAG_NAME (parent_type) != NULL)
12755           return TYPE_TAG_NAME (parent_type);
12756         else
12757           /* An anonymous structure is only allowed non-static data
12758              members; no typedefs, no member functions, et cetera.
12759              So it does not need a prefix.  */
12760           return "";
12761       case DW_TAG_compile_unit:
12762         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12763         if (cu->language == language_cplus
12764             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12765             && die->child != NULL
12766             && (die->tag == DW_TAG_class_type
12767                 || die->tag == DW_TAG_structure_type
12768                 || die->tag == DW_TAG_union_type))
12769           {
12770             char *name = guess_full_die_structure_name (die, cu);
12771             if (name != NULL)
12772               return name;
12773           }
12774         return "";
12775       default:
12776         return determine_prefix (parent, cu);
12777       }
12778 }
12779
12780 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12781    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12782    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12783    an obconcat, otherwise allocate storage for the result.  The CU argument is
12784    used to determine the language and hence, the appropriate separator.  */
12785
12786 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12787
12788 static char *
12789 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12790                  int physname, struct dwarf2_cu *cu)
12791 {
12792   const char *lead = "";
12793   const char *sep;
12794
12795   if (suffix == NULL || suffix[0] == '\0'
12796       || prefix == NULL || prefix[0] == '\0')
12797     sep = "";
12798   else if (cu->language == language_java)
12799     sep = ".";
12800   else if (cu->language == language_fortran && physname)
12801     {
12802       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12803          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12804
12805       lead = "__";
12806       sep = "_MOD_";
12807     }
12808   else
12809     sep = "::";
12810
12811   if (prefix == NULL)
12812     prefix = "";
12813   if (suffix == NULL)
12814     suffix = "";
12815
12816   if (obs == NULL)
12817     {
12818       char *retval
12819         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12820
12821       strcpy (retval, lead);
12822       strcat (retval, prefix);
12823       strcat (retval, sep);
12824       strcat (retval, suffix);
12825       return retval;
12826     }
12827   else
12828     {
12829       /* We have an obstack.  */
12830       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12831     }
12832 }
12833
12834 /* Return sibling of die, NULL if no sibling.  */
12835
12836 static struct die_info *
12837 sibling_die (struct die_info *die)
12838 {
12839   return die->sibling;
12840 }
12841
12842 /* Get name of a die, return NULL if not found.  */
12843
12844 static char *
12845 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12846                           struct obstack *obstack)
12847 {
12848   if (name && cu->language == language_cplus)
12849     {
12850       char *canon_name = cp_canonicalize_string (name);
12851
12852       if (canon_name != NULL)
12853         {
12854           if (strcmp (canon_name, name) != 0)
12855             name = obsavestring (canon_name, strlen (canon_name),
12856                                  obstack);
12857           xfree (canon_name);
12858         }
12859     }
12860
12861   return name;
12862 }
12863
12864 /* Get name of a die, return NULL if not found.  */
12865
12866 static char *
12867 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12868 {
12869   struct attribute *attr;
12870
12871   attr = dwarf2_attr (die, DW_AT_name, cu);
12872   if ((!attr || !DW_STRING (attr))
12873       && die->tag != DW_TAG_class_type
12874       && die->tag != DW_TAG_interface_type
12875       && die->tag != DW_TAG_structure_type
12876       && die->tag != DW_TAG_union_type)
12877     return NULL;
12878
12879   switch (die->tag)
12880     {
12881     case DW_TAG_compile_unit:
12882       /* Compilation units have a DW_AT_name that is a filename, not
12883          a source language identifier.  */
12884     case DW_TAG_enumeration_type:
12885     case DW_TAG_enumerator:
12886       /* These tags always have simple identifiers already; no need
12887          to canonicalize them.  */
12888       return DW_STRING (attr);
12889
12890     case DW_TAG_subprogram:
12891       /* Java constructors will all be named "<init>", so return
12892          the class name when we see this special case.  */
12893       if (cu->language == language_java
12894           && DW_STRING (attr) != NULL
12895           && strcmp (DW_STRING (attr), "<init>") == 0)
12896         {
12897           struct dwarf2_cu *spec_cu = cu;
12898           struct die_info *spec_die;
12899
12900           /* GCJ will output '<init>' for Java constructor names.
12901              For this special case, return the name of the parent class.  */
12902
12903           /* GCJ may output suprogram DIEs with AT_specification set.
12904              If so, use the name of the specified DIE.  */
12905           spec_die = die_specification (die, &spec_cu);
12906           if (spec_die != NULL)
12907             return dwarf2_name (spec_die, spec_cu);
12908
12909           do
12910             {
12911               die = die->parent;
12912               if (die->tag == DW_TAG_class_type)
12913                 return dwarf2_name (die, cu);
12914             }
12915           while (die->tag != DW_TAG_compile_unit);
12916         }
12917       break;
12918
12919     case DW_TAG_class_type:
12920     case DW_TAG_interface_type:
12921     case DW_TAG_structure_type:
12922     case DW_TAG_union_type:
12923       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12924          structures or unions.  These were of the form "._%d" in GCC 4.1,
12925          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12926          and GCC 4.4.  We work around this problem by ignoring these.  */
12927       if (attr && DW_STRING (attr)
12928           && (strncmp (DW_STRING (attr), "._", 2) == 0
12929               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12930         return NULL;
12931
12932       /* GCC might emit a nameless typedef that has a linkage name.  See
12933          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12934       if (!attr || DW_STRING (attr) == NULL)
12935         {
12936           char *demangled = NULL;
12937
12938           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12939           if (attr == NULL)
12940             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12941
12942           if (attr == NULL || DW_STRING (attr) == NULL)
12943             return NULL;
12944
12945           /* Avoid demangling DW_STRING (attr) the second time on a second
12946              call for the same DIE.  */
12947           if (!DW_STRING_IS_CANONICAL (attr))
12948             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12949
12950           if (demangled)
12951             {
12952               char *base;
12953
12954               /* FIXME: we already did this for the partial symbol... */
12955               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12956                                                &cu->objfile->objfile_obstack);
12957               DW_STRING_IS_CANONICAL (attr) = 1;
12958               xfree (demangled);
12959
12960               /* Strip any leading namespaces/classes, keep only the base name.
12961                  DW_AT_name for named DIEs does not contain the prefixes.  */
12962               base = strrchr (DW_STRING (attr), ':');
12963               if (base && base > DW_STRING (attr) && base[-1] == ':')
12964                 return &base[1];
12965               else
12966                 return DW_STRING (attr);
12967             }
12968         }
12969       break;
12970
12971     default:
12972       break;
12973     }
12974
12975   if (!DW_STRING_IS_CANONICAL (attr))
12976     {
12977       DW_STRING (attr)
12978         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12979                                     &cu->objfile->objfile_obstack);
12980       DW_STRING_IS_CANONICAL (attr) = 1;
12981     }
12982   return DW_STRING (attr);
12983 }
12984
12985 /* Return the die that this die in an extension of, or NULL if there
12986    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12987    containing the return value on output.  */
12988
12989 static struct die_info *
12990 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12991 {
12992   struct attribute *attr;
12993
12994   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12995   if (attr == NULL)
12996     return NULL;
12997
12998   return follow_die_ref (die, attr, ext_cu);
12999 }
13000
13001 /* Convert a DIE tag into its string name.  */
13002
13003 static char *
13004 dwarf_tag_name (unsigned tag)
13005 {
13006   switch (tag)
13007     {
13008     case DW_TAG_padding:
13009       return "DW_TAG_padding";
13010     case DW_TAG_array_type:
13011       return "DW_TAG_array_type";
13012     case DW_TAG_class_type:
13013       return "DW_TAG_class_type";
13014     case DW_TAG_entry_point:
13015       return "DW_TAG_entry_point";
13016     case DW_TAG_enumeration_type:
13017       return "DW_TAG_enumeration_type";
13018     case DW_TAG_formal_parameter:
13019       return "DW_TAG_formal_parameter";
13020     case DW_TAG_imported_declaration:
13021       return "DW_TAG_imported_declaration";
13022     case DW_TAG_label:
13023       return "DW_TAG_label";
13024     case DW_TAG_lexical_block:
13025       return "DW_TAG_lexical_block";
13026     case DW_TAG_member:
13027       return "DW_TAG_member";
13028     case DW_TAG_pointer_type:
13029       return "DW_TAG_pointer_type";
13030     case DW_TAG_reference_type:
13031       return "DW_TAG_reference_type";
13032     case DW_TAG_compile_unit:
13033       return "DW_TAG_compile_unit";
13034     case DW_TAG_string_type:
13035       return "DW_TAG_string_type";
13036     case DW_TAG_structure_type:
13037       return "DW_TAG_structure_type";
13038     case DW_TAG_subroutine_type:
13039       return "DW_TAG_subroutine_type";
13040     case DW_TAG_typedef:
13041       return "DW_TAG_typedef";
13042     case DW_TAG_union_type:
13043       return "DW_TAG_union_type";
13044     case DW_TAG_unspecified_parameters:
13045       return "DW_TAG_unspecified_parameters";
13046     case DW_TAG_variant:
13047       return "DW_TAG_variant";
13048     case DW_TAG_common_block:
13049       return "DW_TAG_common_block";
13050     case DW_TAG_common_inclusion:
13051       return "DW_TAG_common_inclusion";
13052     case DW_TAG_inheritance:
13053       return "DW_TAG_inheritance";
13054     case DW_TAG_inlined_subroutine:
13055       return "DW_TAG_inlined_subroutine";
13056     case DW_TAG_module:
13057       return "DW_TAG_module";
13058     case DW_TAG_ptr_to_member_type:
13059       return "DW_TAG_ptr_to_member_type";
13060     case DW_TAG_set_type:
13061       return "DW_TAG_set_type";
13062     case DW_TAG_subrange_type:
13063       return "DW_TAG_subrange_type";
13064     case DW_TAG_with_stmt:
13065       return "DW_TAG_with_stmt";
13066     case DW_TAG_access_declaration:
13067       return "DW_TAG_access_declaration";
13068     case DW_TAG_base_type:
13069       return "DW_TAG_base_type";
13070     case DW_TAG_catch_block:
13071       return "DW_TAG_catch_block";
13072     case DW_TAG_const_type:
13073       return "DW_TAG_const_type";
13074     case DW_TAG_constant:
13075       return "DW_TAG_constant";
13076     case DW_TAG_enumerator:
13077       return "DW_TAG_enumerator";
13078     case DW_TAG_file_type:
13079       return "DW_TAG_file_type";
13080     case DW_TAG_friend:
13081       return "DW_TAG_friend";
13082     case DW_TAG_namelist:
13083       return "DW_TAG_namelist";
13084     case DW_TAG_namelist_item:
13085       return "DW_TAG_namelist_item";
13086     case DW_TAG_packed_type:
13087       return "DW_TAG_packed_type";
13088     case DW_TAG_subprogram:
13089       return "DW_TAG_subprogram";
13090     case DW_TAG_template_type_param:
13091       return "DW_TAG_template_type_param";
13092     case DW_TAG_template_value_param:
13093       return "DW_TAG_template_value_param";
13094     case DW_TAG_thrown_type:
13095       return "DW_TAG_thrown_type";
13096     case DW_TAG_try_block:
13097       return "DW_TAG_try_block";
13098     case DW_TAG_variant_part:
13099       return "DW_TAG_variant_part";
13100     case DW_TAG_variable:
13101       return "DW_TAG_variable";
13102     case DW_TAG_volatile_type:
13103       return "DW_TAG_volatile_type";
13104     case DW_TAG_dwarf_procedure:
13105       return "DW_TAG_dwarf_procedure";
13106     case DW_TAG_restrict_type:
13107       return "DW_TAG_restrict_type";
13108     case DW_TAG_interface_type:
13109       return "DW_TAG_interface_type";
13110     case DW_TAG_namespace:
13111       return "DW_TAG_namespace";
13112     case DW_TAG_imported_module:
13113       return "DW_TAG_imported_module";
13114     case DW_TAG_unspecified_type:
13115       return "DW_TAG_unspecified_type";
13116     case DW_TAG_partial_unit:
13117       return "DW_TAG_partial_unit";
13118     case DW_TAG_imported_unit:
13119       return "DW_TAG_imported_unit";
13120     case DW_TAG_condition:
13121       return "DW_TAG_condition";
13122     case DW_TAG_shared_type:
13123       return "DW_TAG_shared_type";
13124     case DW_TAG_type_unit:
13125       return "DW_TAG_type_unit";
13126     case DW_TAG_MIPS_loop:
13127       return "DW_TAG_MIPS_loop";
13128     case DW_TAG_HP_array_descriptor:
13129       return "DW_TAG_HP_array_descriptor";
13130     case DW_TAG_format_label:
13131       return "DW_TAG_format_label";
13132     case DW_TAG_function_template:
13133       return "DW_TAG_function_template";
13134     case DW_TAG_class_template:
13135       return "DW_TAG_class_template";
13136     case DW_TAG_GNU_BINCL:
13137       return "DW_TAG_GNU_BINCL";
13138     case DW_TAG_GNU_EINCL:
13139       return "DW_TAG_GNU_EINCL";
13140     case DW_TAG_upc_shared_type:
13141       return "DW_TAG_upc_shared_type";
13142     case DW_TAG_upc_strict_type:
13143       return "DW_TAG_upc_strict_type";
13144     case DW_TAG_upc_relaxed_type:
13145       return "DW_TAG_upc_relaxed_type";
13146     case DW_TAG_PGI_kanji_type:
13147       return "DW_TAG_PGI_kanji_type";
13148     case DW_TAG_PGI_interface_block:
13149       return "DW_TAG_PGI_interface_block";
13150     case DW_TAG_GNU_call_site:
13151       return "DW_TAG_GNU_call_site";
13152     default:
13153       return "DW_TAG_<unknown>";
13154     }
13155 }
13156
13157 /* Convert a DWARF attribute code into its string name.  */
13158
13159 static char *
13160 dwarf_attr_name (unsigned attr)
13161 {
13162   switch (attr)
13163     {
13164     case DW_AT_sibling:
13165       return "DW_AT_sibling";
13166     case DW_AT_location:
13167       return "DW_AT_location";
13168     case DW_AT_name:
13169       return "DW_AT_name";
13170     case DW_AT_ordering:
13171       return "DW_AT_ordering";
13172     case DW_AT_subscr_data:
13173       return "DW_AT_subscr_data";
13174     case DW_AT_byte_size:
13175       return "DW_AT_byte_size";
13176     case DW_AT_bit_offset:
13177       return "DW_AT_bit_offset";
13178     case DW_AT_bit_size:
13179       return "DW_AT_bit_size";
13180     case DW_AT_element_list:
13181       return "DW_AT_element_list";
13182     case DW_AT_stmt_list:
13183       return "DW_AT_stmt_list";
13184     case DW_AT_low_pc:
13185       return "DW_AT_low_pc";
13186     case DW_AT_high_pc:
13187       return "DW_AT_high_pc";
13188     case DW_AT_language:
13189       return "DW_AT_language";
13190     case DW_AT_member:
13191       return "DW_AT_member";
13192     case DW_AT_discr:
13193       return "DW_AT_discr";
13194     case DW_AT_discr_value:
13195       return "DW_AT_discr_value";
13196     case DW_AT_visibility:
13197       return "DW_AT_visibility";
13198     case DW_AT_import:
13199       return "DW_AT_import";
13200     case DW_AT_string_length:
13201       return "DW_AT_string_length";
13202     case DW_AT_common_reference:
13203       return "DW_AT_common_reference";
13204     case DW_AT_comp_dir:
13205       return "DW_AT_comp_dir";
13206     case DW_AT_const_value:
13207       return "DW_AT_const_value";
13208     case DW_AT_containing_type:
13209       return "DW_AT_containing_type";
13210     case DW_AT_default_value:
13211       return "DW_AT_default_value";
13212     case DW_AT_inline:
13213       return "DW_AT_inline";
13214     case DW_AT_is_optional:
13215       return "DW_AT_is_optional";
13216     case DW_AT_lower_bound:
13217       return "DW_AT_lower_bound";
13218     case DW_AT_producer:
13219       return "DW_AT_producer";
13220     case DW_AT_prototyped:
13221       return "DW_AT_prototyped";
13222     case DW_AT_return_addr:
13223       return "DW_AT_return_addr";
13224     case DW_AT_start_scope:
13225       return "DW_AT_start_scope";
13226     case DW_AT_bit_stride:
13227       return "DW_AT_bit_stride";
13228     case DW_AT_upper_bound:
13229       return "DW_AT_upper_bound";
13230     case DW_AT_abstract_origin:
13231       return "DW_AT_abstract_origin";
13232     case DW_AT_accessibility:
13233       return "DW_AT_accessibility";
13234     case DW_AT_address_class:
13235       return "DW_AT_address_class";
13236     case DW_AT_artificial:
13237       return "DW_AT_artificial";
13238     case DW_AT_base_types:
13239       return "DW_AT_base_types";
13240     case DW_AT_calling_convention:
13241       return "DW_AT_calling_convention";
13242     case DW_AT_count:
13243       return "DW_AT_count";
13244     case DW_AT_data_member_location:
13245       return "DW_AT_data_member_location";
13246     case DW_AT_decl_column:
13247       return "DW_AT_decl_column";
13248     case DW_AT_decl_file:
13249       return "DW_AT_decl_file";
13250     case DW_AT_decl_line:
13251       return "DW_AT_decl_line";
13252     case DW_AT_declaration:
13253       return "DW_AT_declaration";
13254     case DW_AT_discr_list:
13255       return "DW_AT_discr_list";
13256     case DW_AT_encoding:
13257       return "DW_AT_encoding";
13258     case DW_AT_external:
13259       return "DW_AT_external";
13260     case DW_AT_frame_base:
13261       return "DW_AT_frame_base";
13262     case DW_AT_friend:
13263       return "DW_AT_friend";
13264     case DW_AT_identifier_case:
13265       return "DW_AT_identifier_case";
13266     case DW_AT_macro_info:
13267       return "DW_AT_macro_info";
13268     case DW_AT_namelist_items:
13269       return "DW_AT_namelist_items";
13270     case DW_AT_priority:
13271       return "DW_AT_priority";
13272     case DW_AT_segment:
13273       return "DW_AT_segment";
13274     case DW_AT_specification:
13275       return "DW_AT_specification";
13276     case DW_AT_static_link:
13277       return "DW_AT_static_link";
13278     case DW_AT_type:
13279       return "DW_AT_type";
13280     case DW_AT_use_location:
13281       return "DW_AT_use_location";
13282     case DW_AT_variable_parameter:
13283       return "DW_AT_variable_parameter";
13284     case DW_AT_virtuality:
13285       return "DW_AT_virtuality";
13286     case DW_AT_vtable_elem_location:
13287       return "DW_AT_vtable_elem_location";
13288     /* DWARF 3 values.  */
13289     case DW_AT_allocated:
13290       return "DW_AT_allocated";
13291     case DW_AT_associated:
13292       return "DW_AT_associated";
13293     case DW_AT_data_location:
13294       return "DW_AT_data_location";
13295     case DW_AT_byte_stride:
13296       return "DW_AT_byte_stride";
13297     case DW_AT_entry_pc:
13298       return "DW_AT_entry_pc";
13299     case DW_AT_use_UTF8:
13300       return "DW_AT_use_UTF8";
13301     case DW_AT_extension:
13302       return "DW_AT_extension";
13303     case DW_AT_ranges:
13304       return "DW_AT_ranges";
13305     case DW_AT_trampoline:
13306       return "DW_AT_trampoline";
13307     case DW_AT_call_column:
13308       return "DW_AT_call_column";
13309     case DW_AT_call_file:
13310       return "DW_AT_call_file";
13311     case DW_AT_call_line:
13312       return "DW_AT_call_line";
13313     case DW_AT_description:
13314       return "DW_AT_description";
13315     case DW_AT_binary_scale:
13316       return "DW_AT_binary_scale";
13317     case DW_AT_decimal_scale:
13318       return "DW_AT_decimal_scale";
13319     case DW_AT_small:
13320       return "DW_AT_small";
13321     case DW_AT_decimal_sign:
13322       return "DW_AT_decimal_sign";
13323     case DW_AT_digit_count:
13324       return "DW_AT_digit_count";
13325     case DW_AT_picture_string:
13326       return "DW_AT_picture_string";
13327     case DW_AT_mutable:
13328       return "DW_AT_mutable";
13329     case DW_AT_threads_scaled:
13330       return "DW_AT_threads_scaled";
13331     case DW_AT_explicit:
13332       return "DW_AT_explicit";
13333     case DW_AT_object_pointer:
13334       return "DW_AT_object_pointer";
13335     case DW_AT_endianity:
13336       return "DW_AT_endianity";
13337     case DW_AT_elemental:
13338       return "DW_AT_elemental";
13339     case DW_AT_pure:
13340       return "DW_AT_pure";
13341     case DW_AT_recursive:
13342       return "DW_AT_recursive";
13343     /* DWARF 4 values.  */
13344     case DW_AT_signature:
13345       return "DW_AT_signature";
13346     case DW_AT_linkage_name:
13347       return "DW_AT_linkage_name";
13348     /* SGI/MIPS extensions.  */
13349 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13350     case DW_AT_MIPS_fde:
13351       return "DW_AT_MIPS_fde";
13352 #endif
13353     case DW_AT_MIPS_loop_begin:
13354       return "DW_AT_MIPS_loop_begin";
13355     case DW_AT_MIPS_tail_loop_begin:
13356       return "DW_AT_MIPS_tail_loop_begin";
13357     case DW_AT_MIPS_epilog_begin:
13358       return "DW_AT_MIPS_epilog_begin";
13359     case DW_AT_MIPS_loop_unroll_factor:
13360       return "DW_AT_MIPS_loop_unroll_factor";
13361     case DW_AT_MIPS_software_pipeline_depth:
13362       return "DW_AT_MIPS_software_pipeline_depth";
13363     case DW_AT_MIPS_linkage_name:
13364       return "DW_AT_MIPS_linkage_name";
13365     case DW_AT_MIPS_stride:
13366       return "DW_AT_MIPS_stride";
13367     case DW_AT_MIPS_abstract_name:
13368       return "DW_AT_MIPS_abstract_name";
13369     case DW_AT_MIPS_clone_origin:
13370       return "DW_AT_MIPS_clone_origin";
13371     case DW_AT_MIPS_has_inlines:
13372       return "DW_AT_MIPS_has_inlines";
13373     /* HP extensions.  */
13374 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13375     case DW_AT_HP_block_index:
13376       return "DW_AT_HP_block_index";
13377 #endif
13378     case DW_AT_HP_unmodifiable:
13379       return "DW_AT_HP_unmodifiable";
13380     case DW_AT_HP_actuals_stmt_list:
13381       return "DW_AT_HP_actuals_stmt_list";
13382     case DW_AT_HP_proc_per_section:
13383       return "DW_AT_HP_proc_per_section";
13384     case DW_AT_HP_raw_data_ptr:
13385       return "DW_AT_HP_raw_data_ptr";
13386     case DW_AT_HP_pass_by_reference:
13387       return "DW_AT_HP_pass_by_reference";
13388     case DW_AT_HP_opt_level:
13389       return "DW_AT_HP_opt_level";
13390     case DW_AT_HP_prof_version_id:
13391       return "DW_AT_HP_prof_version_id";
13392     case DW_AT_HP_opt_flags:
13393       return "DW_AT_HP_opt_flags";
13394     case DW_AT_HP_cold_region_low_pc:
13395       return "DW_AT_HP_cold_region_low_pc";
13396     case DW_AT_HP_cold_region_high_pc:
13397       return "DW_AT_HP_cold_region_high_pc";
13398     case DW_AT_HP_all_variables_modifiable:
13399       return "DW_AT_HP_all_variables_modifiable";
13400     case DW_AT_HP_linkage_name:
13401       return "DW_AT_HP_linkage_name";
13402     case DW_AT_HP_prof_flags:
13403       return "DW_AT_HP_prof_flags";
13404     /* GNU extensions.  */
13405     case DW_AT_sf_names:
13406       return "DW_AT_sf_names";
13407     case DW_AT_src_info:
13408       return "DW_AT_src_info";
13409     case DW_AT_mac_info:
13410       return "DW_AT_mac_info";
13411     case DW_AT_src_coords:
13412       return "DW_AT_src_coords";
13413     case DW_AT_body_begin:
13414       return "DW_AT_body_begin";
13415     case DW_AT_body_end:
13416       return "DW_AT_body_end";
13417     case DW_AT_GNU_vector:
13418       return "DW_AT_GNU_vector";
13419     case DW_AT_GNU_odr_signature:
13420       return "DW_AT_GNU_odr_signature";
13421     /* VMS extensions.  */
13422     case DW_AT_VMS_rtnbeg_pd_address:
13423       return "DW_AT_VMS_rtnbeg_pd_address";
13424     /* UPC extension.  */
13425     case DW_AT_upc_threads_scaled:
13426       return "DW_AT_upc_threads_scaled";
13427     /* PGI (STMicroelectronics) extensions.  */
13428     case DW_AT_PGI_lbase:
13429       return "DW_AT_PGI_lbase";
13430     case DW_AT_PGI_soffset:
13431       return "DW_AT_PGI_soffset";
13432     case DW_AT_PGI_lstride:
13433       return "DW_AT_PGI_lstride";
13434     default:
13435       return "DW_AT_<unknown>";
13436     }
13437 }
13438
13439 /* Convert a DWARF value form code into its string name.  */
13440
13441 static char *
13442 dwarf_form_name (unsigned form)
13443 {
13444   switch (form)
13445     {
13446     case DW_FORM_addr:
13447       return "DW_FORM_addr";
13448     case DW_FORM_block2:
13449       return "DW_FORM_block2";
13450     case DW_FORM_block4:
13451       return "DW_FORM_block4";
13452     case DW_FORM_data2:
13453       return "DW_FORM_data2";
13454     case DW_FORM_data4:
13455       return "DW_FORM_data4";
13456     case DW_FORM_data8:
13457       return "DW_FORM_data8";
13458     case DW_FORM_string:
13459       return "DW_FORM_string";
13460     case DW_FORM_block:
13461       return "DW_FORM_block";
13462     case DW_FORM_block1:
13463       return "DW_FORM_block1";
13464     case DW_FORM_data1:
13465       return "DW_FORM_data1";
13466     case DW_FORM_flag:
13467       return "DW_FORM_flag";
13468     case DW_FORM_sdata:
13469       return "DW_FORM_sdata";
13470     case DW_FORM_strp:
13471       return "DW_FORM_strp";
13472     case DW_FORM_udata:
13473       return "DW_FORM_udata";
13474     case DW_FORM_ref_addr:
13475       return "DW_FORM_ref_addr";
13476     case DW_FORM_ref1:
13477       return "DW_FORM_ref1";
13478     case DW_FORM_ref2:
13479       return "DW_FORM_ref2";
13480     case DW_FORM_ref4:
13481       return "DW_FORM_ref4";
13482     case DW_FORM_ref8:
13483       return "DW_FORM_ref8";
13484     case DW_FORM_ref_udata:
13485       return "DW_FORM_ref_udata";
13486     case DW_FORM_indirect:
13487       return "DW_FORM_indirect";
13488     case DW_FORM_sec_offset:
13489       return "DW_FORM_sec_offset";
13490     case DW_FORM_exprloc:
13491       return "DW_FORM_exprloc";
13492     case DW_FORM_flag_present:
13493       return "DW_FORM_flag_present";
13494     case DW_FORM_ref_sig8:
13495       return "DW_FORM_ref_sig8";
13496     default:
13497       return "DW_FORM_<unknown>";
13498     }
13499 }
13500
13501 /* Convert a DWARF stack opcode into its string name.  */
13502
13503 const char *
13504 dwarf_stack_op_name (unsigned op)
13505 {
13506   switch (op)
13507     {
13508     case DW_OP_addr:
13509       return "DW_OP_addr";
13510     case DW_OP_deref:
13511       return "DW_OP_deref";
13512     case DW_OP_const1u:
13513       return "DW_OP_const1u";
13514     case DW_OP_const1s:
13515       return "DW_OP_const1s";
13516     case DW_OP_const2u:
13517       return "DW_OP_const2u";
13518     case DW_OP_const2s:
13519       return "DW_OP_const2s";
13520     case DW_OP_const4u:
13521       return "DW_OP_const4u";
13522     case DW_OP_const4s:
13523       return "DW_OP_const4s";
13524     case DW_OP_const8u:
13525       return "DW_OP_const8u";
13526     case DW_OP_const8s:
13527       return "DW_OP_const8s";
13528     case DW_OP_constu:
13529       return "DW_OP_constu";
13530     case DW_OP_consts:
13531       return "DW_OP_consts";
13532     case DW_OP_dup:
13533       return "DW_OP_dup";
13534     case DW_OP_drop:
13535       return "DW_OP_drop";
13536     case DW_OP_over:
13537       return "DW_OP_over";
13538     case DW_OP_pick:
13539       return "DW_OP_pick";
13540     case DW_OP_swap:
13541       return "DW_OP_swap";
13542     case DW_OP_rot:
13543       return "DW_OP_rot";
13544     case DW_OP_xderef:
13545       return "DW_OP_xderef";
13546     case DW_OP_abs:
13547       return "DW_OP_abs";
13548     case DW_OP_and:
13549       return "DW_OP_and";
13550     case DW_OP_div:
13551       return "DW_OP_div";
13552     case DW_OP_minus:
13553       return "DW_OP_minus";
13554     case DW_OP_mod:
13555       return "DW_OP_mod";
13556     case DW_OP_mul:
13557       return "DW_OP_mul";
13558     case DW_OP_neg:
13559       return "DW_OP_neg";
13560     case DW_OP_not:
13561       return "DW_OP_not";
13562     case DW_OP_or:
13563       return "DW_OP_or";
13564     case DW_OP_plus:
13565       return "DW_OP_plus";
13566     case DW_OP_plus_uconst:
13567       return "DW_OP_plus_uconst";
13568     case DW_OP_shl:
13569       return "DW_OP_shl";
13570     case DW_OP_shr:
13571       return "DW_OP_shr";
13572     case DW_OP_shra:
13573       return "DW_OP_shra";
13574     case DW_OP_xor:
13575       return "DW_OP_xor";
13576     case DW_OP_bra:
13577       return "DW_OP_bra";
13578     case DW_OP_eq:
13579       return "DW_OP_eq";
13580     case DW_OP_ge:
13581       return "DW_OP_ge";
13582     case DW_OP_gt:
13583       return "DW_OP_gt";
13584     case DW_OP_le:
13585       return "DW_OP_le";
13586     case DW_OP_lt:
13587       return "DW_OP_lt";
13588     case DW_OP_ne:
13589       return "DW_OP_ne";
13590     case DW_OP_skip:
13591       return "DW_OP_skip";
13592     case DW_OP_lit0:
13593       return "DW_OP_lit0";
13594     case DW_OP_lit1:
13595       return "DW_OP_lit1";
13596     case DW_OP_lit2:
13597       return "DW_OP_lit2";
13598     case DW_OP_lit3:
13599       return "DW_OP_lit3";
13600     case DW_OP_lit4:
13601       return "DW_OP_lit4";
13602     case DW_OP_lit5:
13603       return "DW_OP_lit5";
13604     case DW_OP_lit6:
13605       return "DW_OP_lit6";
13606     case DW_OP_lit7:
13607       return "DW_OP_lit7";
13608     case DW_OP_lit8:
13609       return "DW_OP_lit8";
13610     case DW_OP_lit9:
13611       return "DW_OP_lit9";
13612     case DW_OP_lit10:
13613       return "DW_OP_lit10";
13614     case DW_OP_lit11:
13615       return "DW_OP_lit11";
13616     case DW_OP_lit12:
13617       return "DW_OP_lit12";
13618     case DW_OP_lit13:
13619       return "DW_OP_lit13";
13620     case DW_OP_lit14:
13621       return "DW_OP_lit14";
13622     case DW_OP_lit15:
13623       return "DW_OP_lit15";
13624     case DW_OP_lit16:
13625       return "DW_OP_lit16";
13626     case DW_OP_lit17:
13627       return "DW_OP_lit17";
13628     case DW_OP_lit18:
13629       return "DW_OP_lit18";
13630     case DW_OP_lit19:
13631       return "DW_OP_lit19";
13632     case DW_OP_lit20:
13633       return "DW_OP_lit20";
13634     case DW_OP_lit21:
13635       return "DW_OP_lit21";
13636     case DW_OP_lit22:
13637       return "DW_OP_lit22";
13638     case DW_OP_lit23:
13639       return "DW_OP_lit23";
13640     case DW_OP_lit24:
13641       return "DW_OP_lit24";
13642     case DW_OP_lit25:
13643       return "DW_OP_lit25";
13644     case DW_OP_lit26:
13645       return "DW_OP_lit26";
13646     case DW_OP_lit27:
13647       return "DW_OP_lit27";
13648     case DW_OP_lit28:
13649       return "DW_OP_lit28";
13650     case DW_OP_lit29:
13651       return "DW_OP_lit29";
13652     case DW_OP_lit30:
13653       return "DW_OP_lit30";
13654     case DW_OP_lit31:
13655       return "DW_OP_lit31";
13656     case DW_OP_reg0:
13657       return "DW_OP_reg0";
13658     case DW_OP_reg1:
13659       return "DW_OP_reg1";
13660     case DW_OP_reg2:
13661       return "DW_OP_reg2";
13662     case DW_OP_reg3:
13663       return "DW_OP_reg3";
13664     case DW_OP_reg4:
13665       return "DW_OP_reg4";
13666     case DW_OP_reg5:
13667       return "DW_OP_reg5";
13668     case DW_OP_reg6:
13669       return "DW_OP_reg6";
13670     case DW_OP_reg7:
13671       return "DW_OP_reg7";
13672     case DW_OP_reg8:
13673       return "DW_OP_reg8";
13674     case DW_OP_reg9:
13675       return "DW_OP_reg9";
13676     case DW_OP_reg10:
13677       return "DW_OP_reg10";
13678     case DW_OP_reg11:
13679       return "DW_OP_reg11";
13680     case DW_OP_reg12:
13681       return "DW_OP_reg12";
13682     case DW_OP_reg13:
13683       return "DW_OP_reg13";
13684     case DW_OP_reg14:
13685       return "DW_OP_reg14";
13686     case DW_OP_reg15:
13687       return "DW_OP_reg15";
13688     case DW_OP_reg16:
13689       return "DW_OP_reg16";
13690     case DW_OP_reg17:
13691       return "DW_OP_reg17";
13692     case DW_OP_reg18:
13693       return "DW_OP_reg18";
13694     case DW_OP_reg19:
13695       return "DW_OP_reg19";
13696     case DW_OP_reg20:
13697       return "DW_OP_reg20";
13698     case DW_OP_reg21:
13699       return "DW_OP_reg21";
13700     case DW_OP_reg22:
13701       return "DW_OP_reg22";
13702     case DW_OP_reg23:
13703       return "DW_OP_reg23";
13704     case DW_OP_reg24:
13705       return "DW_OP_reg24";
13706     case DW_OP_reg25:
13707       return "DW_OP_reg25";
13708     case DW_OP_reg26:
13709       return "DW_OP_reg26";
13710     case DW_OP_reg27:
13711       return "DW_OP_reg27";
13712     case DW_OP_reg28:
13713       return "DW_OP_reg28";
13714     case DW_OP_reg29:
13715       return "DW_OP_reg29";
13716     case DW_OP_reg30:
13717       return "DW_OP_reg30";
13718     case DW_OP_reg31:
13719       return "DW_OP_reg31";
13720     case DW_OP_breg0:
13721       return "DW_OP_breg0";
13722     case DW_OP_breg1:
13723       return "DW_OP_breg1";
13724     case DW_OP_breg2:
13725       return "DW_OP_breg2";
13726     case DW_OP_breg3:
13727       return "DW_OP_breg3";
13728     case DW_OP_breg4:
13729       return "DW_OP_breg4";
13730     case DW_OP_breg5:
13731       return "DW_OP_breg5";
13732     case DW_OP_breg6:
13733       return "DW_OP_breg6";
13734     case DW_OP_breg7:
13735       return "DW_OP_breg7";
13736     case DW_OP_breg8:
13737       return "DW_OP_breg8";
13738     case DW_OP_breg9:
13739       return "DW_OP_breg9";
13740     case DW_OP_breg10:
13741       return "DW_OP_breg10";
13742     case DW_OP_breg11:
13743       return "DW_OP_breg11";
13744     case DW_OP_breg12:
13745       return "DW_OP_breg12";
13746     case DW_OP_breg13:
13747       return "DW_OP_breg13";
13748     case DW_OP_breg14:
13749       return "DW_OP_breg14";
13750     case DW_OP_breg15:
13751       return "DW_OP_breg15";
13752     case DW_OP_breg16:
13753       return "DW_OP_breg16";
13754     case DW_OP_breg17:
13755       return "DW_OP_breg17";
13756     case DW_OP_breg18:
13757       return "DW_OP_breg18";
13758     case DW_OP_breg19:
13759       return "DW_OP_breg19";
13760     case DW_OP_breg20:
13761       return "DW_OP_breg20";
13762     case DW_OP_breg21:
13763       return "DW_OP_breg21";
13764     case DW_OP_breg22:
13765       return "DW_OP_breg22";
13766     case DW_OP_breg23:
13767       return "DW_OP_breg23";
13768     case DW_OP_breg24:
13769       return "DW_OP_breg24";
13770     case DW_OP_breg25:
13771       return "DW_OP_breg25";
13772     case DW_OP_breg26:
13773       return "DW_OP_breg26";
13774     case DW_OP_breg27:
13775       return "DW_OP_breg27";
13776     case DW_OP_breg28:
13777       return "DW_OP_breg28";
13778     case DW_OP_breg29:
13779       return "DW_OP_breg29";
13780     case DW_OP_breg30:
13781       return "DW_OP_breg30";
13782     case DW_OP_breg31:
13783       return "DW_OP_breg31";
13784     case DW_OP_regx:
13785       return "DW_OP_regx";
13786     case DW_OP_fbreg:
13787       return "DW_OP_fbreg";
13788     case DW_OP_bregx:
13789       return "DW_OP_bregx";
13790     case DW_OP_piece:
13791       return "DW_OP_piece";
13792     case DW_OP_deref_size:
13793       return "DW_OP_deref_size";
13794     case DW_OP_xderef_size:
13795       return "DW_OP_xderef_size";
13796     case DW_OP_nop:
13797       return "DW_OP_nop";
13798     /* DWARF 3 extensions.  */
13799     case DW_OP_push_object_address:
13800       return "DW_OP_push_object_address";
13801     case DW_OP_call2:
13802       return "DW_OP_call2";
13803     case DW_OP_call4:
13804       return "DW_OP_call4";
13805     case DW_OP_call_ref:
13806       return "DW_OP_call_ref";
13807     case DW_OP_form_tls_address:
13808       return "DW_OP_form_tls_address";
13809     case DW_OP_call_frame_cfa:
13810       return "DW_OP_call_frame_cfa";
13811     case DW_OP_bit_piece:
13812       return "DW_OP_bit_piece";
13813     /* DWARF 4 extensions.  */
13814     case DW_OP_implicit_value:
13815       return "DW_OP_implicit_value";
13816     case DW_OP_stack_value:
13817       return "DW_OP_stack_value";
13818     /* GNU extensions.  */
13819     case DW_OP_GNU_push_tls_address:
13820       return "DW_OP_GNU_push_tls_address";
13821     case DW_OP_GNU_uninit:
13822       return "DW_OP_GNU_uninit";
13823     case DW_OP_GNU_implicit_pointer:
13824       return "DW_OP_GNU_implicit_pointer";
13825     case DW_OP_GNU_entry_value:
13826       return "DW_OP_GNU_entry_value";
13827     case DW_OP_GNU_const_type:
13828       return "DW_OP_GNU_const_type";
13829     case DW_OP_GNU_regval_type:
13830       return "DW_OP_GNU_regval_type";
13831     case DW_OP_GNU_deref_type:
13832       return "DW_OP_GNU_deref_type";
13833     case DW_OP_GNU_convert:
13834       return "DW_OP_GNU_convert";
13835     case DW_OP_GNU_reinterpret:
13836       return "DW_OP_GNU_reinterpret";
13837     default:
13838       return NULL;
13839     }
13840 }
13841
13842 static char *
13843 dwarf_bool_name (unsigned mybool)
13844 {
13845   if (mybool)
13846     return "TRUE";
13847   else
13848     return "FALSE";
13849 }
13850
13851 /* Convert a DWARF type code into its string name.  */
13852
13853 static char *
13854 dwarf_type_encoding_name (unsigned enc)
13855 {
13856   switch (enc)
13857     {
13858     case DW_ATE_void:
13859       return "DW_ATE_void";
13860     case DW_ATE_address:
13861       return "DW_ATE_address";
13862     case DW_ATE_boolean:
13863       return "DW_ATE_boolean";
13864     case DW_ATE_complex_float:
13865       return "DW_ATE_complex_float";
13866     case DW_ATE_float:
13867       return "DW_ATE_float";
13868     case DW_ATE_signed:
13869       return "DW_ATE_signed";
13870     case DW_ATE_signed_char:
13871       return "DW_ATE_signed_char";
13872     case DW_ATE_unsigned:
13873       return "DW_ATE_unsigned";
13874     case DW_ATE_unsigned_char:
13875       return "DW_ATE_unsigned_char";
13876     /* DWARF 3.  */
13877     case DW_ATE_imaginary_float:
13878       return "DW_ATE_imaginary_float";
13879     case DW_ATE_packed_decimal:
13880       return "DW_ATE_packed_decimal";
13881     case DW_ATE_numeric_string:
13882       return "DW_ATE_numeric_string";
13883     case DW_ATE_edited:
13884       return "DW_ATE_edited";
13885     case DW_ATE_signed_fixed:
13886       return "DW_ATE_signed_fixed";
13887     case DW_ATE_unsigned_fixed:
13888       return "DW_ATE_unsigned_fixed";
13889     case DW_ATE_decimal_float:
13890       return "DW_ATE_decimal_float";
13891     /* DWARF 4.  */
13892     case DW_ATE_UTF:
13893       return "DW_ATE_UTF";
13894     /* HP extensions.  */
13895     case DW_ATE_HP_float80:
13896       return "DW_ATE_HP_float80";
13897     case DW_ATE_HP_complex_float80:
13898       return "DW_ATE_HP_complex_float80";
13899     case DW_ATE_HP_float128:
13900       return "DW_ATE_HP_float128";
13901     case DW_ATE_HP_complex_float128:
13902       return "DW_ATE_HP_complex_float128";
13903     case DW_ATE_HP_floathpintel:
13904       return "DW_ATE_HP_floathpintel";
13905     case DW_ATE_HP_imaginary_float80:
13906       return "DW_ATE_HP_imaginary_float80";
13907     case DW_ATE_HP_imaginary_float128:
13908       return "DW_ATE_HP_imaginary_float128";
13909     default:
13910       return "DW_ATE_<unknown>";
13911     }
13912 }
13913
13914 /* Convert a DWARF call frame info operation to its string name.  */
13915
13916 #if 0
13917 static char *
13918 dwarf_cfi_name (unsigned cfi_opc)
13919 {
13920   switch (cfi_opc)
13921     {
13922     case DW_CFA_advance_loc:
13923       return "DW_CFA_advance_loc";
13924     case DW_CFA_offset:
13925       return "DW_CFA_offset";
13926     case DW_CFA_restore:
13927       return "DW_CFA_restore";
13928     case DW_CFA_nop:
13929       return "DW_CFA_nop";
13930     case DW_CFA_set_loc:
13931       return "DW_CFA_set_loc";
13932     case DW_CFA_advance_loc1:
13933       return "DW_CFA_advance_loc1";
13934     case DW_CFA_advance_loc2:
13935       return "DW_CFA_advance_loc2";
13936     case DW_CFA_advance_loc4:
13937       return "DW_CFA_advance_loc4";
13938     case DW_CFA_offset_extended:
13939       return "DW_CFA_offset_extended";
13940     case DW_CFA_restore_extended:
13941       return "DW_CFA_restore_extended";
13942     case DW_CFA_undefined:
13943       return "DW_CFA_undefined";
13944     case DW_CFA_same_value:
13945       return "DW_CFA_same_value";
13946     case DW_CFA_register:
13947       return "DW_CFA_register";
13948     case DW_CFA_remember_state:
13949       return "DW_CFA_remember_state";
13950     case DW_CFA_restore_state:
13951       return "DW_CFA_restore_state";
13952     case DW_CFA_def_cfa:
13953       return "DW_CFA_def_cfa";
13954     case DW_CFA_def_cfa_register:
13955       return "DW_CFA_def_cfa_register";
13956     case DW_CFA_def_cfa_offset:
13957       return "DW_CFA_def_cfa_offset";
13958     /* DWARF 3.  */
13959     case DW_CFA_def_cfa_expression:
13960       return "DW_CFA_def_cfa_expression";
13961     case DW_CFA_expression:
13962       return "DW_CFA_expression";
13963     case DW_CFA_offset_extended_sf:
13964       return "DW_CFA_offset_extended_sf";
13965     case DW_CFA_def_cfa_sf:
13966       return "DW_CFA_def_cfa_sf";
13967     case DW_CFA_def_cfa_offset_sf:
13968       return "DW_CFA_def_cfa_offset_sf";
13969     case DW_CFA_val_offset:
13970       return "DW_CFA_val_offset";
13971     case DW_CFA_val_offset_sf:
13972       return "DW_CFA_val_offset_sf";
13973     case DW_CFA_val_expression:
13974       return "DW_CFA_val_expression";
13975     /* SGI/MIPS specific.  */
13976     case DW_CFA_MIPS_advance_loc8:
13977       return "DW_CFA_MIPS_advance_loc8";
13978     /* GNU extensions.  */
13979     case DW_CFA_GNU_window_save:
13980       return "DW_CFA_GNU_window_save";
13981     case DW_CFA_GNU_args_size:
13982       return "DW_CFA_GNU_args_size";
13983     case DW_CFA_GNU_negative_offset_extended:
13984       return "DW_CFA_GNU_negative_offset_extended";
13985     default:
13986       return "DW_CFA_<unknown>";
13987     }
13988 }
13989 #endif
13990
13991 static void
13992 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13993 {
13994   unsigned int i;
13995
13996   print_spaces (indent, f);
13997   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13998            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13999
14000   if (die->parent != NULL)
14001     {
14002       print_spaces (indent, f);
14003       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
14004                           die->parent->offset);
14005     }
14006
14007   print_spaces (indent, f);
14008   fprintf_unfiltered (f, "  has children: %s\n",
14009            dwarf_bool_name (die->child != NULL));
14010
14011   print_spaces (indent, f);
14012   fprintf_unfiltered (f, "  attributes:\n");
14013
14014   for (i = 0; i < die->num_attrs; ++i)
14015     {
14016       print_spaces (indent, f);
14017       fprintf_unfiltered (f, "    %s (%s) ",
14018                dwarf_attr_name (die->attrs[i].name),
14019                dwarf_form_name (die->attrs[i].form));
14020
14021       switch (die->attrs[i].form)
14022         {
14023         case DW_FORM_ref_addr:
14024         case DW_FORM_addr:
14025           fprintf_unfiltered (f, "address: ");
14026           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14027           break;
14028         case DW_FORM_block2:
14029         case DW_FORM_block4:
14030         case DW_FORM_block:
14031         case DW_FORM_block1:
14032           fprintf_unfiltered (f, "block: size %d",
14033                               DW_BLOCK (&die->attrs[i])->size);
14034           break;
14035         case DW_FORM_exprloc:
14036           fprintf_unfiltered (f, "expression: size %u",
14037                               DW_BLOCK (&die->attrs[i])->size);
14038           break;
14039         case DW_FORM_ref1:
14040         case DW_FORM_ref2:
14041         case DW_FORM_ref4:
14042           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14043                               (long) (DW_ADDR (&die->attrs[i])));
14044           break;
14045         case DW_FORM_data1:
14046         case DW_FORM_data2:
14047         case DW_FORM_data4:
14048         case DW_FORM_data8:
14049         case DW_FORM_udata:
14050         case DW_FORM_sdata:
14051           fprintf_unfiltered (f, "constant: %s",
14052                               pulongest (DW_UNSND (&die->attrs[i])));
14053           break;
14054         case DW_FORM_sec_offset:
14055           fprintf_unfiltered (f, "section offset: %s",
14056                               pulongest (DW_UNSND (&die->attrs[i])));
14057           break;
14058         case DW_FORM_ref_sig8:
14059           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14060             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14061                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
14062           else
14063             fprintf_unfiltered (f, "signatured type, offset: unknown");
14064           break;
14065         case DW_FORM_string:
14066         case DW_FORM_strp:
14067           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14068                    DW_STRING (&die->attrs[i])
14069                    ? DW_STRING (&die->attrs[i]) : "",
14070                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14071           break;
14072         case DW_FORM_flag:
14073           if (DW_UNSND (&die->attrs[i]))
14074             fprintf_unfiltered (f, "flag: TRUE");
14075           else
14076             fprintf_unfiltered (f, "flag: FALSE");
14077           break;
14078         case DW_FORM_flag_present:
14079           fprintf_unfiltered (f, "flag: TRUE");
14080           break;
14081         case DW_FORM_indirect:
14082           /* The reader will have reduced the indirect form to
14083              the "base form" so this form should not occur.  */
14084           fprintf_unfiltered (f, 
14085                               "unexpected attribute form: DW_FORM_indirect");
14086           break;
14087         default:
14088           fprintf_unfiltered (f, "unsupported attribute form: %d.",
14089                    die->attrs[i].form);
14090           break;
14091         }
14092       fprintf_unfiltered (f, "\n");
14093     }
14094 }
14095
14096 static void
14097 dump_die_for_error (struct die_info *die)
14098 {
14099   dump_die_shallow (gdb_stderr, 0, die);
14100 }
14101
14102 static void
14103 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14104 {
14105   int indent = level * 4;
14106
14107   gdb_assert (die != NULL);
14108
14109   if (level >= max_level)
14110     return;
14111
14112   dump_die_shallow (f, indent, die);
14113
14114   if (die->child != NULL)
14115     {
14116       print_spaces (indent, f);
14117       fprintf_unfiltered (f, "  Children:");
14118       if (level + 1 < max_level)
14119         {
14120           fprintf_unfiltered (f, "\n");
14121           dump_die_1 (f, level + 1, max_level, die->child);
14122         }
14123       else
14124         {
14125           fprintf_unfiltered (f,
14126                               " [not printed, max nesting level reached]\n");
14127         }
14128     }
14129
14130   if (die->sibling != NULL && level > 0)
14131     {
14132       dump_die_1 (f, level, max_level, die->sibling);
14133     }
14134 }
14135
14136 /* This is called from the pdie macro in gdbinit.in.
14137    It's not static so gcc will keep a copy callable from gdb.  */
14138
14139 void
14140 dump_die (struct die_info *die, int max_level)
14141 {
14142   dump_die_1 (gdb_stdlog, 0, max_level, die);
14143 }
14144
14145 static void
14146 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14147 {
14148   void **slot;
14149
14150   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14151
14152   *slot = die;
14153 }
14154
14155 static int
14156 is_ref_attr (struct attribute *attr)
14157 {
14158   switch (attr->form)
14159     {
14160     case DW_FORM_ref_addr:
14161     case DW_FORM_ref1:
14162     case DW_FORM_ref2:
14163     case DW_FORM_ref4:
14164     case DW_FORM_ref8:
14165     case DW_FORM_ref_udata:
14166       return 1;
14167     default:
14168       return 0;
14169     }
14170 }
14171
14172 static unsigned int
14173 dwarf2_get_ref_die_offset (struct attribute *attr)
14174 {
14175   if (is_ref_attr (attr))
14176     return DW_ADDR (attr);
14177
14178   complaint (&symfile_complaints,
14179              _("unsupported die ref attribute form: '%s'"),
14180              dwarf_form_name (attr->form));
14181   return 0;
14182 }
14183
14184 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14185  * the value held by the attribute is not constant.  */
14186
14187 static LONGEST
14188 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14189 {
14190   if (attr->form == DW_FORM_sdata)
14191     return DW_SND (attr);
14192   else if (attr->form == DW_FORM_udata
14193            || attr->form == DW_FORM_data1
14194            || attr->form == DW_FORM_data2
14195            || attr->form == DW_FORM_data4
14196            || attr->form == DW_FORM_data8)
14197     return DW_UNSND (attr);
14198   else
14199     {
14200       complaint (&symfile_complaints,
14201                  _("Attribute value is not a constant (%s)"),
14202                  dwarf_form_name (attr->form));
14203       return default_value;
14204     }
14205 }
14206
14207 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14208    unit and add it to our queue.
14209    The result is non-zero if PER_CU was queued, otherwise the result is zero
14210    meaning either PER_CU is already queued or it is already loaded.  */
14211
14212 static int
14213 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14214                        struct dwarf2_per_cu_data *per_cu)
14215 {
14216   /* We may arrive here during partial symbol reading, if we need full
14217      DIEs to process an unusual case (e.g. template arguments).  Do
14218      not queue PER_CU, just tell our caller to load its DIEs.  */
14219   if (dwarf2_per_objfile->reading_partial_symbols)
14220     {
14221       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14222         return 1;
14223       return 0;
14224     }
14225
14226   /* Mark the dependence relation so that we don't flush PER_CU
14227      too early.  */
14228   dwarf2_add_dependence (this_cu, per_cu);
14229
14230   /* If it's already on the queue, we have nothing to do.  */
14231   if (per_cu->queued)
14232     return 0;
14233
14234   /* If the compilation unit is already loaded, just mark it as
14235      used.  */
14236   if (per_cu->cu != NULL)
14237     {
14238       per_cu->cu->last_used = 0;
14239       return 0;
14240     }
14241
14242   /* Add it to the queue.  */
14243   queue_comp_unit (per_cu, this_cu->objfile);
14244
14245   return 1;
14246 }
14247
14248 /* Follow reference or signature attribute ATTR of SRC_DIE.
14249    On entry *REF_CU is the CU of SRC_DIE.
14250    On exit *REF_CU is the CU of the result.  */
14251
14252 static struct die_info *
14253 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14254                        struct dwarf2_cu **ref_cu)
14255 {
14256   struct die_info *die;
14257
14258   if (is_ref_attr (attr))
14259     die = follow_die_ref (src_die, attr, ref_cu);
14260   else if (attr->form == DW_FORM_ref_sig8)
14261     die = follow_die_sig (src_die, attr, ref_cu);
14262   else
14263     {
14264       dump_die_for_error (src_die);
14265       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14266              (*ref_cu)->objfile->name);
14267     }
14268
14269   return die;
14270 }
14271
14272 /* Follow reference OFFSET.
14273    On entry *REF_CU is the CU of the source die referencing OFFSET.
14274    On exit *REF_CU is the CU of the result.
14275    Returns NULL if OFFSET is invalid.  */
14276
14277 static struct die_info *
14278 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14279 {
14280   struct die_info temp_die;
14281   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14282
14283   gdb_assert (cu->per_cu != NULL);
14284
14285   target_cu = cu;
14286
14287   if (cu->per_cu->debug_types_section)
14288     {
14289       /* .debug_types CUs cannot reference anything outside their CU.
14290          If they need to, they have to reference a signatured type via
14291          DW_FORM_ref_sig8.  */
14292       if (! offset_in_cu_p (&cu->header, offset))
14293         return NULL;
14294     }
14295   else if (! offset_in_cu_p (&cu->header, offset))
14296     {
14297       struct dwarf2_per_cu_data *per_cu;
14298
14299       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14300
14301       /* If necessary, add it to the queue and load its DIEs.  */
14302       if (maybe_queue_comp_unit (cu, per_cu))
14303         load_full_comp_unit (per_cu, cu->objfile);
14304
14305       target_cu = per_cu->cu;
14306     }
14307   else if (cu->dies == NULL)
14308     {
14309       /* We're loading full DIEs during partial symbol reading.  */
14310       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14311       load_full_comp_unit (cu->per_cu, cu->objfile);
14312     }
14313
14314   *ref_cu = target_cu;
14315   temp_die.offset = offset;
14316   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14317 }
14318
14319 /* Follow reference attribute ATTR of SRC_DIE.
14320    On entry *REF_CU is the CU of SRC_DIE.
14321    On exit *REF_CU is the CU of the result.  */
14322
14323 static struct die_info *
14324 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14325                 struct dwarf2_cu **ref_cu)
14326 {
14327   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14328   struct dwarf2_cu *cu = *ref_cu;
14329   struct die_info *die;
14330
14331   die = follow_die_offset (offset, ref_cu);
14332   if (!die)
14333     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14334            "at 0x%x [in module %s]"),
14335            offset, src_die->offset, cu->objfile->name);
14336
14337   return die;
14338 }
14339
14340 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14341    Returned value is intended for DW_OP_call*.  Returned
14342    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14343
14344 struct dwarf2_locexpr_baton
14345 dwarf2_fetch_die_location_block (unsigned int offset,
14346                                  struct dwarf2_per_cu_data *per_cu,
14347                                  CORE_ADDR (*get_frame_pc) (void *baton),
14348                                  void *baton)
14349 {
14350   struct dwarf2_cu *cu;
14351   struct die_info *die;
14352   struct attribute *attr;
14353   struct dwarf2_locexpr_baton retval;
14354
14355   dw2_setup (per_cu->objfile);
14356
14357   if (per_cu->cu == NULL)
14358     load_cu (per_cu);
14359   cu = per_cu->cu;
14360
14361   die = follow_die_offset (offset, &cu);
14362   if (!die)
14363     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14364            offset, per_cu->cu->objfile->name);
14365
14366   attr = dwarf2_attr (die, DW_AT_location, cu);
14367   if (!attr)
14368     {
14369       /* DWARF: "If there is no such attribute, then there is no effect.".
14370          DATA is ignored if SIZE is 0.  */
14371
14372       retval.data = NULL;
14373       retval.size = 0;
14374     }
14375   else if (attr_form_is_section_offset (attr))
14376     {
14377       struct dwarf2_loclist_baton loclist_baton;
14378       CORE_ADDR pc = (*get_frame_pc) (baton);
14379       size_t size;
14380
14381       fill_in_loclist_baton (cu, &loclist_baton, attr);
14382
14383       retval.data = dwarf2_find_location_expression (&loclist_baton,
14384                                                      &size, pc);
14385       retval.size = size;
14386     }
14387   else
14388     {
14389       if (!attr_form_is_block (attr))
14390         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14391                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14392                offset, per_cu->cu->objfile->name);
14393
14394       retval.data = DW_BLOCK (attr)->data;
14395       retval.size = DW_BLOCK (attr)->size;
14396     }
14397   retval.per_cu = cu->per_cu;
14398
14399   age_cached_comp_units ();
14400
14401   return retval;
14402 }
14403
14404 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14405    PER_CU.  */
14406
14407 struct type *
14408 dwarf2_get_die_type (unsigned int die_offset,
14409                      struct dwarf2_per_cu_data *per_cu)
14410 {
14411   dw2_setup (per_cu->objfile);
14412   return get_die_type_at_offset (die_offset, per_cu);
14413 }
14414
14415 /* Follow the signature attribute ATTR in SRC_DIE.
14416    On entry *REF_CU is the CU of SRC_DIE.
14417    On exit *REF_CU is the CU of the result.  */
14418
14419 static struct die_info *
14420 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14421                 struct dwarf2_cu **ref_cu)
14422 {
14423   struct objfile *objfile = (*ref_cu)->objfile;
14424   struct die_info temp_die;
14425   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14426   struct dwarf2_cu *sig_cu;
14427   struct die_info *die;
14428
14429   /* sig_type will be NULL if the signatured type is missing from
14430      the debug info.  */
14431   if (sig_type == NULL)
14432     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14433              "at 0x%x [in module %s]"),
14434            src_die->offset, objfile->name);
14435
14436   /* If necessary, add it to the queue and load its DIEs.  */
14437
14438   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14439     read_signatured_type (objfile, sig_type);
14440
14441   gdb_assert (sig_type->per_cu.cu != NULL);
14442
14443   sig_cu = sig_type->per_cu.cu;
14444   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14445   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14446   if (die)
14447     {
14448       *ref_cu = sig_cu;
14449       return die;
14450     }
14451
14452   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14453          "from DIE at 0x%x [in module %s]"),
14454          sig_type->type_offset, src_die->offset, objfile->name);
14455 }
14456
14457 /* Given an offset of a signatured type, return its signatured_type.  */
14458
14459 static struct signatured_type *
14460 lookup_signatured_type_at_offset (struct objfile *objfile,
14461                                   struct dwarf2_section_info *section,
14462                                   unsigned int offset)
14463 {
14464   gdb_byte *info_ptr = section->buffer + offset;
14465   unsigned int length, initial_length_size;
14466   unsigned int sig_offset;
14467   struct signatured_type find_entry, *type_sig;
14468
14469   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14470   sig_offset = (initial_length_size
14471                 + 2 /*version*/
14472                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14473                 + 1 /*address_size*/);
14474   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14475   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14476
14477   /* This is only used to lookup previously recorded types.
14478      If we didn't find it, it's our bug.  */
14479   gdb_assert (type_sig != NULL);
14480   gdb_assert (offset == type_sig->per_cu.offset);
14481
14482   return type_sig;
14483 }
14484
14485 /* Read in signatured type at OFFSET and build its CU and die(s).  */
14486
14487 static void
14488 read_signatured_type_at_offset (struct objfile *objfile,
14489                                 struct dwarf2_section_info *sect,
14490                                 unsigned int offset)
14491 {
14492   struct signatured_type *type_sig;
14493
14494   dwarf2_read_section (objfile, sect);
14495
14496   /* We have the section offset, but we need the signature to do the
14497      hash table lookup.  */
14498   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14499
14500   gdb_assert (type_sig->per_cu.cu == NULL);
14501
14502   read_signatured_type (objfile, type_sig);
14503
14504   gdb_assert (type_sig->per_cu.cu != NULL);
14505 }
14506
14507 /* Read in a signatured type and build its CU and DIEs.  */
14508
14509 static void
14510 read_signatured_type (struct objfile *objfile,
14511                       struct signatured_type *type_sig)
14512 {
14513   gdb_byte *types_ptr;
14514   struct die_reader_specs reader_specs;
14515   struct dwarf2_cu *cu;
14516   ULONGEST signature;
14517   struct cleanup *back_to, *free_cu_cleanup;
14518   struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14519
14520   dwarf2_read_section (objfile, section);
14521   types_ptr = section->buffer + type_sig->per_cu.offset;
14522
14523   gdb_assert (type_sig->per_cu.cu == NULL);
14524
14525   cu = xmalloc (sizeof (*cu));
14526   init_one_comp_unit (cu, objfile);
14527
14528   type_sig->per_cu.cu = cu;
14529   cu->per_cu = &type_sig->per_cu;
14530
14531   /* If an error occurs while loading, release our storage.  */
14532   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14533
14534   types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14535                                         types_ptr, objfile->obfd);
14536   gdb_assert (signature == type_sig->signature);
14537
14538   cu->die_hash
14539     = htab_create_alloc_ex (cu->header.length / 12,
14540                             die_hash,
14541                             die_eq,
14542                             NULL,
14543                             &cu->comp_unit_obstack,
14544                             hashtab_obstack_allocate,
14545                             dummy_obstack_deallocate);
14546
14547   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14548   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14549
14550   init_cu_die_reader (&reader_specs, cu);
14551
14552   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14553                                     NULL /*parent*/);
14554
14555   /* We try not to read any attributes in this function, because not
14556      all objfiles needed for references have been loaded yet, and symbol
14557      table processing isn't initialized.  But we have to set the CU language,
14558      or we won't be able to build types correctly.  */
14559   prepare_one_comp_unit (cu, cu->dies);
14560
14561   do_cleanups (back_to);
14562
14563   /* We've successfully allocated this compilation unit.  Let our caller
14564      clean it up when finished with it.  */
14565   discard_cleanups (free_cu_cleanup);
14566
14567   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14568   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14569 }
14570
14571 /* Decode simple location descriptions.
14572    Given a pointer to a dwarf block that defines a location, compute
14573    the location and return the value.
14574
14575    NOTE drow/2003-11-18: This function is called in two situations
14576    now: for the address of static or global variables (partial symbols
14577    only) and for offsets into structures which are expected to be
14578    (more or less) constant.  The partial symbol case should go away,
14579    and only the constant case should remain.  That will let this
14580    function complain more accurately.  A few special modes are allowed
14581    without complaint for global variables (for instance, global
14582    register values and thread-local values).
14583
14584    A location description containing no operations indicates that the
14585    object is optimized out.  The return value is 0 for that case.
14586    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14587    callers will only want a very basic result and this can become a
14588    complaint.
14589
14590    Note that stack[0] is unused except as a default error return.  */
14591
14592 static CORE_ADDR
14593 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14594 {
14595   struct objfile *objfile = cu->objfile;
14596   int i;
14597   int size = blk->size;
14598   gdb_byte *data = blk->data;
14599   CORE_ADDR stack[64];
14600   int stacki;
14601   unsigned int bytes_read, unsnd;
14602   gdb_byte op;
14603
14604   i = 0;
14605   stacki = 0;
14606   stack[stacki] = 0;
14607   stack[++stacki] = 0;
14608
14609   while (i < size)
14610     {
14611       op = data[i++];
14612       switch (op)
14613         {
14614         case DW_OP_lit0:
14615         case DW_OP_lit1:
14616         case DW_OP_lit2:
14617         case DW_OP_lit3:
14618         case DW_OP_lit4:
14619         case DW_OP_lit5:
14620         case DW_OP_lit6:
14621         case DW_OP_lit7:
14622         case DW_OP_lit8:
14623         case DW_OP_lit9:
14624         case DW_OP_lit10:
14625         case DW_OP_lit11:
14626         case DW_OP_lit12:
14627         case DW_OP_lit13:
14628         case DW_OP_lit14:
14629         case DW_OP_lit15:
14630         case DW_OP_lit16:
14631         case DW_OP_lit17:
14632         case DW_OP_lit18:
14633         case DW_OP_lit19:
14634         case DW_OP_lit20:
14635         case DW_OP_lit21:
14636         case DW_OP_lit22:
14637         case DW_OP_lit23:
14638         case DW_OP_lit24:
14639         case DW_OP_lit25:
14640         case DW_OP_lit26:
14641         case DW_OP_lit27:
14642         case DW_OP_lit28:
14643         case DW_OP_lit29:
14644         case DW_OP_lit30:
14645         case DW_OP_lit31:
14646           stack[++stacki] = op - DW_OP_lit0;
14647           break;
14648
14649         case DW_OP_reg0:
14650         case DW_OP_reg1:
14651         case DW_OP_reg2:
14652         case DW_OP_reg3:
14653         case DW_OP_reg4:
14654         case DW_OP_reg5:
14655         case DW_OP_reg6:
14656         case DW_OP_reg7:
14657         case DW_OP_reg8:
14658         case DW_OP_reg9:
14659         case DW_OP_reg10:
14660         case DW_OP_reg11:
14661         case DW_OP_reg12:
14662         case DW_OP_reg13:
14663         case DW_OP_reg14:
14664         case DW_OP_reg15:
14665         case DW_OP_reg16:
14666         case DW_OP_reg17:
14667         case DW_OP_reg18:
14668         case DW_OP_reg19:
14669         case DW_OP_reg20:
14670         case DW_OP_reg21:
14671         case DW_OP_reg22:
14672         case DW_OP_reg23:
14673         case DW_OP_reg24:
14674         case DW_OP_reg25:
14675         case DW_OP_reg26:
14676         case DW_OP_reg27:
14677         case DW_OP_reg28:
14678         case DW_OP_reg29:
14679         case DW_OP_reg30:
14680         case DW_OP_reg31:
14681           stack[++stacki] = op - DW_OP_reg0;
14682           if (i < size)
14683             dwarf2_complex_location_expr_complaint ();
14684           break;
14685
14686         case DW_OP_regx:
14687           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14688           i += bytes_read;
14689           stack[++stacki] = unsnd;
14690           if (i < size)
14691             dwarf2_complex_location_expr_complaint ();
14692           break;
14693
14694         case DW_OP_addr:
14695           stack[++stacki] = read_address (objfile->obfd, &data[i],
14696                                           cu, &bytes_read);
14697           i += bytes_read;
14698           break;
14699
14700         case DW_OP_const1u:
14701           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14702           i += 1;
14703           break;
14704
14705         case DW_OP_const1s:
14706           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14707           i += 1;
14708           break;
14709
14710         case DW_OP_const2u:
14711           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14712           i += 2;
14713           break;
14714
14715         case DW_OP_const2s:
14716           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14717           i += 2;
14718           break;
14719
14720         case DW_OP_const4u:
14721           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14722           i += 4;
14723           break;
14724
14725         case DW_OP_const4s:
14726           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14727           i += 4;
14728           break;
14729
14730         case DW_OP_const8u:
14731           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14732           i += 8;
14733           break;
14734
14735         case DW_OP_constu:
14736           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14737                                                   &bytes_read);
14738           i += bytes_read;
14739           break;
14740
14741         case DW_OP_consts:
14742           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14743           i += bytes_read;
14744           break;
14745
14746         case DW_OP_dup:
14747           stack[stacki + 1] = stack[stacki];
14748           stacki++;
14749           break;
14750
14751         case DW_OP_plus:
14752           stack[stacki - 1] += stack[stacki];
14753           stacki--;
14754           break;
14755
14756         case DW_OP_plus_uconst:
14757           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14758                                                  &bytes_read);
14759           i += bytes_read;
14760           break;
14761
14762         case DW_OP_minus:
14763           stack[stacki - 1] -= stack[stacki];
14764           stacki--;
14765           break;
14766
14767         case DW_OP_deref:
14768           /* If we're not the last op, then we definitely can't encode
14769              this using GDB's address_class enum.  This is valid for partial
14770              global symbols, although the variable's address will be bogus
14771              in the psymtab.  */
14772           if (i < size)
14773             dwarf2_complex_location_expr_complaint ();
14774           break;
14775
14776         case DW_OP_GNU_push_tls_address:
14777           /* The top of the stack has the offset from the beginning
14778              of the thread control block at which the variable is located.  */
14779           /* Nothing should follow this operator, so the top of stack would
14780              be returned.  */
14781           /* This is valid for partial global symbols, but the variable's
14782              address will be bogus in the psymtab.  Make it always at least
14783              non-zero to not look as a variable garbage collected by linker
14784              which have DW_OP_addr 0.  */
14785           if (i < size)
14786             dwarf2_complex_location_expr_complaint ();
14787           stack[stacki]++;
14788           break;
14789
14790         case DW_OP_GNU_uninit:
14791           break;
14792
14793         default:
14794           {
14795             const char *name = dwarf_stack_op_name (op);
14796
14797             if (name)
14798               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14799                          name);
14800             else
14801               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14802                          op);
14803           }
14804
14805           return (stack[stacki]);
14806         }
14807
14808       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14809          outside of the allocated space.  Also enforce minimum>0.  */
14810       if (stacki >= ARRAY_SIZE (stack) - 1)
14811         {
14812           complaint (&symfile_complaints,
14813                      _("location description stack overflow"));
14814           return 0;
14815         }
14816
14817       if (stacki <= 0)
14818         {
14819           complaint (&symfile_complaints,
14820                      _("location description stack underflow"));
14821           return 0;
14822         }
14823     }
14824   return (stack[stacki]);
14825 }
14826
14827 /* memory allocation interface */
14828
14829 static struct dwarf_block *
14830 dwarf_alloc_block (struct dwarf2_cu *cu)
14831 {
14832   struct dwarf_block *blk;
14833
14834   blk = (struct dwarf_block *)
14835     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14836   return (blk);
14837 }
14838
14839 static struct abbrev_info *
14840 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14841 {
14842   struct abbrev_info *abbrev;
14843
14844   abbrev = (struct abbrev_info *)
14845     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14846   memset (abbrev, 0, sizeof (struct abbrev_info));
14847   return (abbrev);
14848 }
14849
14850 static struct die_info *
14851 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14852 {
14853   struct die_info *die;
14854   size_t size = sizeof (struct die_info);
14855
14856   if (num_attrs > 1)
14857     size += (num_attrs - 1) * sizeof (struct attribute);
14858
14859   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14860   memset (die, 0, sizeof (struct die_info));
14861   return (die);
14862 }
14863
14864 \f
14865 /* Macro support.  */
14866
14867 /* Return the full name of file number I in *LH's file name table.
14868    Use COMP_DIR as the name of the current directory of the
14869    compilation.  The result is allocated using xmalloc; the caller is
14870    responsible for freeing it.  */
14871 static char *
14872 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14873 {
14874   /* Is the file number a valid index into the line header's file name
14875      table?  Remember that file numbers start with one, not zero.  */
14876   if (1 <= file && file <= lh->num_file_names)
14877     {
14878       struct file_entry *fe = &lh->file_names[file - 1];
14879
14880       if (IS_ABSOLUTE_PATH (fe->name))
14881         return xstrdup (fe->name);
14882       else
14883         {
14884           const char *dir;
14885           int dir_len;
14886           char *full_name;
14887
14888           if (fe->dir_index)
14889             dir = lh->include_dirs[fe->dir_index - 1];
14890           else
14891             dir = comp_dir;
14892
14893           if (dir)
14894             {
14895               dir_len = strlen (dir);
14896               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14897               strcpy (full_name, dir);
14898               full_name[dir_len] = '/';
14899               strcpy (full_name + dir_len + 1, fe->name);
14900               return full_name;
14901             }
14902           else
14903             return xstrdup (fe->name);
14904         }
14905     }
14906   else
14907     {
14908       /* The compiler produced a bogus file number.  We can at least
14909          record the macro definitions made in the file, even if we
14910          won't be able to find the file by name.  */
14911       char fake_name[80];
14912
14913       sprintf (fake_name, "<bad macro file number %d>", file);
14914
14915       complaint (&symfile_complaints,
14916                  _("bad file number in macro information (%d)"),
14917                  file);
14918
14919       return xstrdup (fake_name);
14920     }
14921 }
14922
14923
14924 static struct macro_source_file *
14925 macro_start_file (int file, int line,
14926                   struct macro_source_file *current_file,
14927                   const char *comp_dir,
14928                   struct line_header *lh, struct objfile *objfile)
14929 {
14930   /* The full name of this source file.  */
14931   char *full_name = file_full_name (file, lh, comp_dir);
14932
14933   /* We don't create a macro table for this compilation unit
14934      at all until we actually get a filename.  */
14935   if (! pending_macros)
14936     pending_macros = new_macro_table (&objfile->objfile_obstack,
14937                                       objfile->macro_cache);
14938
14939   if (! current_file)
14940     /* If we have no current file, then this must be the start_file
14941        directive for the compilation unit's main source file.  */
14942     current_file = macro_set_main (pending_macros, full_name);
14943   else
14944     current_file = macro_include (current_file, line, full_name);
14945
14946   xfree (full_name);
14947
14948   return current_file;
14949 }
14950
14951
14952 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14953    followed by a null byte.  */
14954 static char *
14955 copy_string (const char *buf, int len)
14956 {
14957   char *s = xmalloc (len + 1);
14958
14959   memcpy (s, buf, len);
14960   s[len] = '\0';
14961   return s;
14962 }
14963
14964
14965 static const char *
14966 consume_improper_spaces (const char *p, const char *body)
14967 {
14968   if (*p == ' ')
14969     {
14970       complaint (&symfile_complaints,
14971                  _("macro definition contains spaces "
14972                    "in formal argument list:\n`%s'"),
14973                  body);
14974
14975       while (*p == ' ')
14976         p++;
14977     }
14978
14979   return p;
14980 }
14981
14982
14983 static void
14984 parse_macro_definition (struct macro_source_file *file, int line,
14985                         const char *body)
14986 {
14987   const char *p;
14988
14989   /* The body string takes one of two forms.  For object-like macro
14990      definitions, it should be:
14991
14992         <macro name> " " <definition>
14993
14994      For function-like macro definitions, it should be:
14995
14996         <macro name> "() " <definition>
14997      or
14998         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14999
15000      Spaces may appear only where explicitly indicated, and in the
15001      <definition>.
15002
15003      The Dwarf 2 spec says that an object-like macro's name is always
15004      followed by a space, but versions of GCC around March 2002 omit
15005      the space when the macro's definition is the empty string.
15006
15007      The Dwarf 2 spec says that there should be no spaces between the
15008      formal arguments in a function-like macro's formal argument list,
15009      but versions of GCC around March 2002 include spaces after the
15010      commas.  */
15011
15012
15013   /* Find the extent of the macro name.  The macro name is terminated
15014      by either a space or null character (for an object-like macro) or
15015      an opening paren (for a function-like macro).  */
15016   for (p = body; *p; p++)
15017     if (*p == ' ' || *p == '(')
15018       break;
15019
15020   if (*p == ' ' || *p == '\0')
15021     {
15022       /* It's an object-like macro.  */
15023       int name_len = p - body;
15024       char *name = copy_string (body, name_len);
15025       const char *replacement;
15026
15027       if (*p == ' ')
15028         replacement = body + name_len + 1;
15029       else
15030         {
15031           dwarf2_macro_malformed_definition_complaint (body);
15032           replacement = body + name_len;
15033         }
15034
15035       macro_define_object (file, line, name, replacement);
15036
15037       xfree (name);
15038     }
15039   else if (*p == '(')
15040     {
15041       /* It's a function-like macro.  */
15042       char *name = copy_string (body, p - body);
15043       int argc = 0;
15044       int argv_size = 1;
15045       char **argv = xmalloc (argv_size * sizeof (*argv));
15046
15047       p++;
15048
15049       p = consume_improper_spaces (p, body);
15050
15051       /* Parse the formal argument list.  */
15052       while (*p && *p != ')')
15053         {
15054           /* Find the extent of the current argument name.  */
15055           const char *arg_start = p;
15056
15057           while (*p && *p != ',' && *p != ')' && *p != ' ')
15058             p++;
15059
15060           if (! *p || p == arg_start)
15061             dwarf2_macro_malformed_definition_complaint (body);
15062           else
15063             {
15064               /* Make sure argv has room for the new argument.  */
15065               if (argc >= argv_size)
15066                 {
15067                   argv_size *= 2;
15068                   argv = xrealloc (argv, argv_size * sizeof (*argv));
15069                 }
15070
15071               argv[argc++] = copy_string (arg_start, p - arg_start);
15072             }
15073
15074           p = consume_improper_spaces (p, body);
15075
15076           /* Consume the comma, if present.  */
15077           if (*p == ',')
15078             {
15079               p++;
15080
15081               p = consume_improper_spaces (p, body);
15082             }
15083         }
15084
15085       if (*p == ')')
15086         {
15087           p++;
15088
15089           if (*p == ' ')
15090             /* Perfectly formed definition, no complaints.  */
15091             macro_define_function (file, line, name,
15092                                    argc, (const char **) argv,
15093                                    p + 1);
15094           else if (*p == '\0')
15095             {
15096               /* Complain, but do define it.  */
15097               dwarf2_macro_malformed_definition_complaint (body);
15098               macro_define_function (file, line, name,
15099                                      argc, (const char **) argv,
15100                                      p);
15101             }
15102           else
15103             /* Just complain.  */
15104             dwarf2_macro_malformed_definition_complaint (body);
15105         }
15106       else
15107         /* Just complain.  */
15108         dwarf2_macro_malformed_definition_complaint (body);
15109
15110       xfree (name);
15111       {
15112         int i;
15113
15114         for (i = 0; i < argc; i++)
15115           xfree (argv[i]);
15116       }
15117       xfree (argv);
15118     }
15119   else
15120     dwarf2_macro_malformed_definition_complaint (body);
15121 }
15122
15123 /* Skip some bytes from BYTES according to the form given in FORM.
15124    Returns the new pointer.  */
15125
15126 static gdb_byte *
15127 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15128                  enum dwarf_form form,
15129                  unsigned int offset_size,
15130                  struct dwarf2_section_info *section)
15131 {
15132   unsigned int bytes_read;
15133
15134   switch (form)
15135     {
15136     case DW_FORM_data1:
15137     case DW_FORM_flag:
15138       ++bytes;
15139       break;
15140
15141     case DW_FORM_data2:
15142       bytes += 2;
15143       break;
15144
15145     case DW_FORM_data4:
15146       bytes += 4;
15147       break;
15148
15149     case DW_FORM_data8:
15150       bytes += 8;
15151       break;
15152
15153     case DW_FORM_string:
15154       read_direct_string (abfd, bytes, &bytes_read);
15155       bytes += bytes_read;
15156       break;
15157
15158     case DW_FORM_sec_offset:
15159     case DW_FORM_strp:
15160       bytes += offset_size;
15161       break;
15162
15163     case DW_FORM_block:
15164       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15165       bytes += bytes_read;
15166       break;
15167
15168     case DW_FORM_block1:
15169       bytes += 1 + read_1_byte (abfd, bytes);
15170       break;
15171     case DW_FORM_block2:
15172       bytes += 2 + read_2_bytes (abfd, bytes);
15173       break;
15174     case DW_FORM_block4:
15175       bytes += 4 + read_4_bytes (abfd, bytes);
15176       break;
15177
15178     case DW_FORM_sdata:
15179     case DW_FORM_udata:
15180       bytes = skip_leb128 (abfd, bytes);
15181       break;
15182
15183     default:
15184       {
15185       complain:
15186         complaint (&symfile_complaints,
15187                    _("invalid form 0x%x in `%s'"),
15188                    form,
15189                    section->asection->name);
15190         return NULL;
15191       }
15192     }
15193
15194   return bytes;
15195 }
15196
15197 /* A helper for dwarf_decode_macros that handles skipping an unknown
15198    opcode.  Returns an updated pointer to the macro data buffer; or,
15199    on error, issues a complaint and returns NULL.  */
15200
15201 static gdb_byte *
15202 skip_unknown_opcode (unsigned int opcode,
15203                      gdb_byte **opcode_definitions,
15204                      gdb_byte *mac_ptr,
15205                      bfd *abfd,
15206                      unsigned int offset_size,
15207                      struct dwarf2_section_info *section)
15208 {
15209   unsigned int bytes_read, i;
15210   unsigned long arg;
15211   gdb_byte *defn;
15212
15213   if (opcode_definitions[opcode] == NULL)
15214     {
15215       complaint (&symfile_complaints,
15216                  _("unrecognized DW_MACFINO opcode 0x%x"),
15217                  opcode);
15218       return NULL;
15219     }
15220
15221   defn = opcode_definitions[opcode];
15222   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15223   defn += bytes_read;
15224
15225   for (i = 0; i < arg; ++i)
15226     {
15227       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15228       if (mac_ptr == NULL)
15229         {
15230           /* skip_form_bytes already issued the complaint.  */
15231           return NULL;
15232         }
15233     }
15234
15235   return mac_ptr;
15236 }
15237
15238 /* A helper function which parses the header of a macro section.
15239    If the macro section is the extended (for now called "GNU") type,
15240    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15241    the header, or issues a complaint and returns NULL on error.  */
15242
15243 static gdb_byte *
15244 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15245                           bfd *abfd,
15246                           gdb_byte *mac_ptr,
15247                           unsigned int *offset_size,
15248                           int section_is_gnu)
15249 {
15250   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15251
15252   if (section_is_gnu)
15253     {
15254       unsigned int version, flags;
15255
15256       version = read_2_bytes (abfd, mac_ptr);
15257       if (version != 4)
15258         {
15259           complaint (&symfile_complaints,
15260                      _("unrecognized version `%d' in .debug_macro section"),
15261                      version);
15262           return NULL;
15263         }
15264       mac_ptr += 2;
15265
15266       flags = read_1_byte (abfd, mac_ptr);
15267       ++mac_ptr;
15268       *offset_size = (flags & 1) ? 8 : 4;
15269
15270       if ((flags & 2) != 0)
15271         /* We don't need the line table offset.  */
15272         mac_ptr += *offset_size;
15273
15274       /* Vendor opcode descriptions.  */
15275       if ((flags & 4) != 0)
15276         {
15277           unsigned int i, count;
15278
15279           count = read_1_byte (abfd, mac_ptr);
15280           ++mac_ptr;
15281           for (i = 0; i < count; ++i)
15282             {
15283               unsigned int opcode, bytes_read;
15284               unsigned long arg;
15285
15286               opcode = read_1_byte (abfd, mac_ptr);
15287               ++mac_ptr;
15288               opcode_definitions[opcode] = mac_ptr;
15289               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15290               mac_ptr += bytes_read;
15291               mac_ptr += arg;
15292             }
15293         }
15294     }
15295
15296   return mac_ptr;
15297 }
15298
15299 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15300    including DW_GNU_MACINFO_transparent_include.  */
15301
15302 static void
15303 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15304                           struct macro_source_file *current_file,
15305                           struct line_header *lh, char *comp_dir,
15306                           struct dwarf2_section_info *section,
15307                           int section_is_gnu,
15308                           unsigned int offset_size,
15309                           struct objfile *objfile)
15310 {
15311   enum dwarf_macro_record_type macinfo_type;
15312   int at_commandline;
15313   gdb_byte *opcode_definitions[256];
15314
15315   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15316                                       &offset_size, section_is_gnu);
15317   if (mac_ptr == NULL)
15318     {
15319       /* We already issued a complaint.  */
15320       return;
15321     }
15322
15323   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15324      GDB is still reading the definitions from command line.  First
15325      DW_MACINFO_start_file will need to be ignored as it was already executed
15326      to create CURRENT_FILE for the main source holding also the command line
15327      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15328      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15329
15330   at_commandline = 1;
15331
15332   do
15333     {
15334       /* Do we at least have room for a macinfo type byte?  */
15335       if (mac_ptr >= mac_end)
15336         {
15337           dwarf2_macros_too_long_complaint (section);
15338           break;
15339         }
15340
15341       macinfo_type = read_1_byte (abfd, mac_ptr);
15342       mac_ptr++;
15343
15344       /* Note that we rely on the fact that the corresponding GNU and
15345          DWARF constants are the same.  */
15346       switch (macinfo_type)
15347         {
15348           /* A zero macinfo type indicates the end of the macro
15349              information.  */
15350         case 0:
15351           break;
15352
15353         case DW_MACRO_GNU_define:
15354         case DW_MACRO_GNU_undef:
15355         case DW_MACRO_GNU_define_indirect:
15356         case DW_MACRO_GNU_undef_indirect:
15357           {
15358             unsigned int bytes_read;
15359             int line;
15360             char *body;
15361             int is_define;
15362
15363             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15364             mac_ptr += bytes_read;
15365
15366             if (macinfo_type == DW_MACRO_GNU_define
15367                 || macinfo_type == DW_MACRO_GNU_undef)
15368               {
15369                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15370                 mac_ptr += bytes_read;
15371               }
15372             else
15373               {
15374                 LONGEST str_offset;
15375
15376                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15377                 mac_ptr += offset_size;
15378
15379                 body = read_indirect_string_at_offset (abfd, str_offset);
15380               }
15381
15382             is_define = (macinfo_type == DW_MACRO_GNU_define
15383                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15384             if (! current_file)
15385               {
15386                 /* DWARF violation as no main source is present.  */
15387                 complaint (&symfile_complaints,
15388                            _("debug info with no main source gives macro %s "
15389                              "on line %d: %s"),
15390                            is_define ? _("definition") : _("undefinition"),
15391                            line, body);
15392                 break;
15393               }
15394             if ((line == 0 && !at_commandline)
15395                 || (line != 0 && at_commandline))
15396               complaint (&symfile_complaints,
15397                          _("debug info gives %s macro %s with %s line %d: %s"),
15398                          at_commandline ? _("command-line") : _("in-file"),
15399                          is_define ? _("definition") : _("undefinition"),
15400                          line == 0 ? _("zero") : _("non-zero"), line, body);
15401
15402             if (is_define)
15403               parse_macro_definition (current_file, line, body);
15404             else
15405               {
15406                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15407                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15408                 macro_undef (current_file, line, body);
15409               }
15410           }
15411           break;
15412
15413         case DW_MACRO_GNU_start_file:
15414           {
15415             unsigned int bytes_read;
15416             int line, file;
15417
15418             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15419             mac_ptr += bytes_read;
15420             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15421             mac_ptr += bytes_read;
15422
15423             if ((line == 0 && !at_commandline)
15424                 || (line != 0 && at_commandline))
15425               complaint (&symfile_complaints,
15426                          _("debug info gives source %d included "
15427                            "from %s at %s line %d"),
15428                          file, at_commandline ? _("command-line") : _("file"),
15429                          line == 0 ? _("zero") : _("non-zero"), line);
15430
15431             if (at_commandline)
15432               {
15433                 /* This DW_MACRO_GNU_start_file was executed in the
15434                    pass one.  */
15435                 at_commandline = 0;
15436               }
15437             else
15438               current_file = macro_start_file (file, line,
15439                                                current_file, comp_dir,
15440                                                lh, objfile);
15441           }
15442           break;
15443
15444         case DW_MACRO_GNU_end_file:
15445           if (! current_file)
15446             complaint (&symfile_complaints,
15447                        _("macro debug info has an unmatched "
15448                          "`close_file' directive"));
15449           else
15450             {
15451               current_file = current_file->included_by;
15452               if (! current_file)
15453                 {
15454                   enum dwarf_macro_record_type next_type;
15455
15456                   /* GCC circa March 2002 doesn't produce the zero
15457                      type byte marking the end of the compilation
15458                      unit.  Complain if it's not there, but exit no
15459                      matter what.  */
15460
15461                   /* Do we at least have room for a macinfo type byte?  */
15462                   if (mac_ptr >= mac_end)
15463                     {
15464                       dwarf2_macros_too_long_complaint (section);
15465                       return;
15466                     }
15467
15468                   /* We don't increment mac_ptr here, so this is just
15469                      a look-ahead.  */
15470                   next_type = read_1_byte (abfd, mac_ptr);
15471                   if (next_type != 0)
15472                     complaint (&symfile_complaints,
15473                                _("no terminating 0-type entry for "
15474                                  "macros in `.debug_macinfo' section"));
15475
15476                   return;
15477                 }
15478             }
15479           break;
15480
15481         case DW_MACRO_GNU_transparent_include:
15482           {
15483             LONGEST offset;
15484
15485             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15486             mac_ptr += offset_size;
15487
15488             dwarf_decode_macro_bytes (abfd,
15489                                       section->buffer + offset,
15490                                       mac_end, current_file,
15491                                       lh, comp_dir,
15492                                       section, section_is_gnu,
15493                                       offset_size, objfile);
15494           }
15495           break;
15496
15497         case DW_MACINFO_vendor_ext:
15498           if (!section_is_gnu)
15499             {
15500               unsigned int bytes_read;
15501               int constant;
15502
15503               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15504               mac_ptr += bytes_read;
15505               read_direct_string (abfd, mac_ptr, &bytes_read);
15506               mac_ptr += bytes_read;
15507
15508               /* We don't recognize any vendor extensions.  */
15509               break;
15510             }
15511           /* FALLTHROUGH */
15512
15513         default:
15514           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15515                                          mac_ptr, abfd, offset_size,
15516                                          section);
15517           if (mac_ptr == NULL)
15518             return;
15519           break;
15520         }
15521     } while (macinfo_type != 0);
15522 }
15523
15524 static void
15525 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15526                      char *comp_dir, bfd *abfd,
15527                      struct dwarf2_cu *cu,
15528                      struct dwarf2_section_info *section,
15529                      int section_is_gnu)
15530 {
15531   gdb_byte *mac_ptr, *mac_end;
15532   struct macro_source_file *current_file = 0;
15533   enum dwarf_macro_record_type macinfo_type;
15534   unsigned int offset_size = cu->header.offset_size;
15535   gdb_byte *opcode_definitions[256];
15536
15537   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15538   if (section->buffer == NULL)
15539     {
15540       complaint (&symfile_complaints, _("missing %s section"),
15541                  section->asection->name);
15542       return;
15543     }
15544
15545   /* First pass: Find the name of the base filename.
15546      This filename is needed in order to process all macros whose definition
15547      (or undefinition) comes from the command line.  These macros are defined
15548      before the first DW_MACINFO_start_file entry, and yet still need to be
15549      associated to the base file.
15550
15551      To determine the base file name, we scan the macro definitions until we
15552      reach the first DW_MACINFO_start_file entry.  We then initialize
15553      CURRENT_FILE accordingly so that any macro definition found before the
15554      first DW_MACINFO_start_file can still be associated to the base file.  */
15555
15556   mac_ptr = section->buffer + offset;
15557   mac_end = section->buffer + section->size;
15558
15559   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15560                                       &offset_size, section_is_gnu);
15561   if (mac_ptr == NULL)
15562     {
15563       /* We already issued a complaint.  */
15564       return;
15565     }
15566
15567   do
15568     {
15569       /* Do we at least have room for a macinfo type byte?  */
15570       if (mac_ptr >= mac_end)
15571         {
15572           /* Complaint is printed during the second pass as GDB will probably
15573              stop the first pass earlier upon finding
15574              DW_MACINFO_start_file.  */
15575           break;
15576         }
15577
15578       macinfo_type = read_1_byte (abfd, mac_ptr);
15579       mac_ptr++;
15580
15581       /* Note that we rely on the fact that the corresponding GNU and
15582          DWARF constants are the same.  */
15583       switch (macinfo_type)
15584         {
15585           /* A zero macinfo type indicates the end of the macro
15586              information.  */
15587         case 0:
15588           break;
15589
15590         case DW_MACRO_GNU_define:
15591         case DW_MACRO_GNU_undef:
15592           /* Only skip the data by MAC_PTR.  */
15593           {
15594             unsigned int bytes_read;
15595
15596             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15597             mac_ptr += bytes_read;
15598             read_direct_string (abfd, mac_ptr, &bytes_read);
15599             mac_ptr += bytes_read;
15600           }
15601           break;
15602
15603         case DW_MACRO_GNU_start_file:
15604           {
15605             unsigned int bytes_read;
15606             int line, file;
15607
15608             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15609             mac_ptr += bytes_read;
15610             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15611             mac_ptr += bytes_read;
15612
15613             current_file = macro_start_file (file, line, current_file,
15614                                              comp_dir, lh, cu->objfile);
15615           }
15616           break;
15617
15618         case DW_MACRO_GNU_end_file:
15619           /* No data to skip by MAC_PTR.  */
15620           break;
15621
15622         case DW_MACRO_GNU_define_indirect:
15623         case DW_MACRO_GNU_undef_indirect:
15624           {
15625             unsigned int bytes_read;
15626
15627             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15628             mac_ptr += bytes_read;
15629             mac_ptr += offset_size;
15630           }
15631           break;
15632
15633         case DW_MACRO_GNU_transparent_include:
15634           /* Note that, according to the spec, a transparent include
15635              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15636              skip this opcode.  */
15637           mac_ptr += offset_size;
15638           break;
15639
15640         case DW_MACINFO_vendor_ext:
15641           /* Only skip the data by MAC_PTR.  */
15642           if (!section_is_gnu)
15643             {
15644               unsigned int bytes_read;
15645
15646               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15647               mac_ptr += bytes_read;
15648               read_direct_string (abfd, mac_ptr, &bytes_read);
15649               mac_ptr += bytes_read;
15650             }
15651           /* FALLTHROUGH */
15652
15653         default:
15654           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15655                                          mac_ptr, abfd, offset_size,
15656                                          section);
15657           if (mac_ptr == NULL)
15658             return;
15659           break;
15660         }
15661     } while (macinfo_type != 0 && current_file == NULL);
15662
15663   /* Second pass: Process all entries.
15664
15665      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15666      command-line macro definitions/undefinitions.  This flag is unset when we
15667      reach the first DW_MACINFO_start_file entry.  */
15668
15669   dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15670                             current_file, lh, comp_dir, section, section_is_gnu,
15671                             offset_size, cu->objfile);
15672 }
15673
15674 /* Check if the attribute's form is a DW_FORM_block*
15675    if so return true else false.  */
15676 static int
15677 attr_form_is_block (struct attribute *attr)
15678 {
15679   return (attr == NULL ? 0 :
15680       attr->form == DW_FORM_block1
15681       || attr->form == DW_FORM_block2
15682       || attr->form == DW_FORM_block4
15683       || attr->form == DW_FORM_block
15684       || attr->form == DW_FORM_exprloc);
15685 }
15686
15687 /* Return non-zero if ATTR's value is a section offset --- classes
15688    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15689    You may use DW_UNSND (attr) to retrieve such offsets.
15690
15691    Section 7.5.4, "Attribute Encodings", explains that no attribute
15692    may have a value that belongs to more than one of these classes; it
15693    would be ambiguous if we did, because we use the same forms for all
15694    of them.  */
15695 static int
15696 attr_form_is_section_offset (struct attribute *attr)
15697 {
15698   return (attr->form == DW_FORM_data4
15699           || attr->form == DW_FORM_data8
15700           || attr->form == DW_FORM_sec_offset);
15701 }
15702
15703
15704 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15705    zero otherwise.  When this function returns true, you can apply
15706    dwarf2_get_attr_constant_value to it.
15707
15708    However, note that for some attributes you must check
15709    attr_form_is_section_offset before using this test.  DW_FORM_data4
15710    and DW_FORM_data8 are members of both the constant class, and of
15711    the classes that contain offsets into other debug sections
15712    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15713    that, if an attribute's can be either a constant or one of the
15714    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15715    taken as section offsets, not constants.  */
15716 static int
15717 attr_form_is_constant (struct attribute *attr)
15718 {
15719   switch (attr->form)
15720     {
15721     case DW_FORM_sdata:
15722     case DW_FORM_udata:
15723     case DW_FORM_data1:
15724     case DW_FORM_data2:
15725     case DW_FORM_data4:
15726     case DW_FORM_data8:
15727       return 1;
15728     default:
15729       return 0;
15730     }
15731 }
15732
15733 /* A helper function that fills in a dwarf2_loclist_baton.  */
15734
15735 static void
15736 fill_in_loclist_baton (struct dwarf2_cu *cu,
15737                        struct dwarf2_loclist_baton *baton,
15738                        struct attribute *attr)
15739 {
15740   dwarf2_read_section (dwarf2_per_objfile->objfile,
15741                        &dwarf2_per_objfile->loc);
15742
15743   baton->per_cu = cu->per_cu;
15744   gdb_assert (baton->per_cu);
15745   /* We don't know how long the location list is, but make sure we
15746      don't run off the edge of the section.  */
15747   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15748   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15749   baton->base_address = cu->base_address;
15750 }
15751
15752 static void
15753 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15754                              struct dwarf2_cu *cu)
15755 {
15756   if (attr_form_is_section_offset (attr)
15757       /* ".debug_loc" may not exist at all, or the offset may be outside
15758          the section.  If so, fall through to the complaint in the
15759          other branch.  */
15760       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15761                                                 &dwarf2_per_objfile->loc))
15762     {
15763       struct dwarf2_loclist_baton *baton;
15764
15765       baton = obstack_alloc (&cu->objfile->objfile_obstack,
15766                              sizeof (struct dwarf2_loclist_baton));
15767
15768       fill_in_loclist_baton (cu, baton, attr);
15769
15770       if (cu->base_known == 0)
15771         complaint (&symfile_complaints,
15772                    _("Location list used without "
15773                      "specifying the CU base address."));
15774
15775       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15776       SYMBOL_LOCATION_BATON (sym) = baton;
15777     }
15778   else
15779     {
15780       struct dwarf2_locexpr_baton *baton;
15781
15782       baton = obstack_alloc (&cu->objfile->objfile_obstack,
15783                              sizeof (struct dwarf2_locexpr_baton));
15784       baton->per_cu = cu->per_cu;
15785       gdb_assert (baton->per_cu);
15786
15787       if (attr_form_is_block (attr))
15788         {
15789           /* Note that we're just copying the block's data pointer
15790              here, not the actual data.  We're still pointing into the
15791              info_buffer for SYM's objfile; right now we never release
15792              that buffer, but when we do clean up properly this may
15793              need to change.  */
15794           baton->size = DW_BLOCK (attr)->size;
15795           baton->data = DW_BLOCK (attr)->data;
15796         }
15797       else
15798         {
15799           dwarf2_invalid_attrib_class_complaint ("location description",
15800                                                  SYMBOL_NATURAL_NAME (sym));
15801           baton->size = 0;
15802         }
15803
15804       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15805       SYMBOL_LOCATION_BATON (sym) = baton;
15806     }
15807 }
15808
15809 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15810    came from a separate debuginfo file, then the master objfile is
15811    returned.  */
15812
15813 struct objfile *
15814 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15815 {
15816   struct objfile *objfile = per_cu->objfile;
15817
15818   /* Return the master objfile, so that we can report and look up the
15819      correct file containing this variable.  */
15820   if (objfile->separate_debug_objfile_backlink)
15821     objfile = objfile->separate_debug_objfile_backlink;
15822
15823   return objfile;
15824 }
15825
15826 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15827    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15828    CU_HEADERP first.  */
15829
15830 static const struct comp_unit_head *
15831 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15832                        struct dwarf2_per_cu_data *per_cu)
15833 {
15834   struct objfile *objfile;
15835   struct dwarf2_per_objfile *per_objfile;
15836   gdb_byte *info_ptr;
15837
15838   if (per_cu->cu)
15839     return &per_cu->cu->header;
15840
15841   objfile = per_cu->objfile;
15842   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15843   info_ptr = per_objfile->info.buffer + per_cu->offset;
15844
15845   memset (cu_headerp, 0, sizeof (*cu_headerp));
15846   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15847
15848   return cu_headerp;
15849 }
15850
15851 /* Return the address size given in the compilation unit header for CU.  */
15852
15853 CORE_ADDR
15854 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15855 {
15856   struct comp_unit_head cu_header_local;
15857   const struct comp_unit_head *cu_headerp;
15858
15859   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15860
15861   return cu_headerp->addr_size;
15862 }
15863
15864 /* Return the offset size given in the compilation unit header for CU.  */
15865
15866 int
15867 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15868 {
15869   struct comp_unit_head cu_header_local;
15870   const struct comp_unit_head *cu_headerp;
15871
15872   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15873
15874   return cu_headerp->offset_size;
15875 }
15876
15877 /* See its dwarf2loc.h declaration.  */
15878
15879 int
15880 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15881 {
15882   struct comp_unit_head cu_header_local;
15883   const struct comp_unit_head *cu_headerp;
15884
15885   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15886
15887   if (cu_headerp->version == 2)
15888     return cu_headerp->addr_size;
15889   else
15890     return cu_headerp->offset_size;
15891 }
15892
15893 /* Return the text offset of the CU.  The returned offset comes from
15894    this CU's objfile.  If this objfile came from a separate debuginfo
15895    file, then the offset may be different from the corresponding
15896    offset in the parent objfile.  */
15897
15898 CORE_ADDR
15899 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15900 {
15901   struct objfile *objfile = per_cu->objfile;
15902
15903   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15904 }
15905
15906 /* Locate the .debug_info compilation unit from CU's objfile which contains
15907    the DIE at OFFSET.  Raises an error on failure.  */
15908
15909 static struct dwarf2_per_cu_data *
15910 dwarf2_find_containing_comp_unit (unsigned int offset,
15911                                   struct objfile *objfile)
15912 {
15913   struct dwarf2_per_cu_data *this_cu;
15914   int low, high;
15915
15916   low = 0;
15917   high = dwarf2_per_objfile->n_comp_units - 1;
15918   while (high > low)
15919     {
15920       int mid = low + (high - low) / 2;
15921
15922       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15923         high = mid;
15924       else
15925         low = mid + 1;
15926     }
15927   gdb_assert (low == high);
15928   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15929     {
15930       if (low == 0)
15931         error (_("Dwarf Error: could not find partial DIE containing "
15932                "offset 0x%lx [in module %s]"),
15933                (long) offset, bfd_get_filename (objfile->obfd));
15934
15935       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15936       return dwarf2_per_objfile->all_comp_units[low-1];
15937     }
15938   else
15939     {
15940       this_cu = dwarf2_per_objfile->all_comp_units[low];
15941       if (low == dwarf2_per_objfile->n_comp_units - 1
15942           && offset >= this_cu->offset + this_cu->length)
15943         error (_("invalid dwarf2 offset %u"), offset);
15944       gdb_assert (offset < this_cu->offset + this_cu->length);
15945       return this_cu;
15946     }
15947 }
15948
15949 /* Locate the compilation unit from OBJFILE which is located at exactly
15950    OFFSET.  Raises an error on failure.  */
15951
15952 static struct dwarf2_per_cu_data *
15953 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15954 {
15955   struct dwarf2_per_cu_data *this_cu;
15956
15957   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15958   if (this_cu->offset != offset)
15959     error (_("no compilation unit with offset %u."), offset);
15960   return this_cu;
15961 }
15962
15963 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
15964
15965 static void
15966 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15967 {
15968   memset (cu, 0, sizeof (*cu));
15969   cu->objfile = objfile;
15970   obstack_init (&cu->comp_unit_obstack);
15971 }
15972
15973 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15974
15975 static void
15976 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15977 {
15978   struct attribute *attr;
15979
15980   /* Set the language we're debugging.  */
15981   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15982   if (attr)
15983     set_cu_language (DW_UNSND (attr), cu);
15984   else
15985     {
15986       cu->language = language_minimal;
15987       cu->language_defn = language_def (cu->language);
15988     }
15989 }
15990
15991 /* Release one cached compilation unit, CU.  We unlink it from the tree
15992    of compilation units, but we don't remove it from the read_in_chain;
15993    the caller is responsible for that.
15994    NOTE: DATA is a void * because this function is also used as a
15995    cleanup routine.  */
15996
15997 static void
15998 free_one_comp_unit (void *data)
15999 {
16000   struct dwarf2_cu *cu = data;
16001
16002   if (cu->per_cu != NULL)
16003     cu->per_cu->cu = NULL;
16004   cu->per_cu = NULL;
16005
16006   obstack_free (&cu->comp_unit_obstack, NULL);
16007
16008   xfree (cu);
16009 }
16010
16011 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16012    when we're finished with it.  We can't free the pointer itself, but be
16013    sure to unlink it from the cache.  Also release any associated storage
16014    and perform cache maintenance.
16015
16016    Only used during partial symbol parsing.  */
16017
16018 static void
16019 free_stack_comp_unit (void *data)
16020 {
16021   struct dwarf2_cu *cu = data;
16022
16023   obstack_free (&cu->comp_unit_obstack, NULL);
16024   cu->partial_dies = NULL;
16025
16026   if (cu->per_cu != NULL)
16027     {
16028       /* This compilation unit is on the stack in our caller, so we
16029          should not xfree it.  Just unlink it.  */
16030       cu->per_cu->cu = NULL;
16031       cu->per_cu = NULL;
16032
16033       /* If we had a per-cu pointer, then we may have other compilation
16034          units loaded, so age them now.  */
16035       age_cached_comp_units ();
16036     }
16037 }
16038
16039 /* Free all cached compilation units.  */
16040
16041 static void
16042 free_cached_comp_units (void *data)
16043 {
16044   struct dwarf2_per_cu_data *per_cu, **last_chain;
16045
16046   per_cu = dwarf2_per_objfile->read_in_chain;
16047   last_chain = &dwarf2_per_objfile->read_in_chain;
16048   while (per_cu != NULL)
16049     {
16050       struct dwarf2_per_cu_data *next_cu;
16051
16052       next_cu = per_cu->cu->read_in_chain;
16053
16054       free_one_comp_unit (per_cu->cu);
16055       *last_chain = next_cu;
16056
16057       per_cu = next_cu;
16058     }
16059 }
16060
16061 /* Increase the age counter on each cached compilation unit, and free
16062    any that are too old.  */
16063
16064 static void
16065 age_cached_comp_units (void)
16066 {
16067   struct dwarf2_per_cu_data *per_cu, **last_chain;
16068
16069   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16070   per_cu = dwarf2_per_objfile->read_in_chain;
16071   while (per_cu != NULL)
16072     {
16073       per_cu->cu->last_used ++;
16074       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16075         dwarf2_mark (per_cu->cu);
16076       per_cu = per_cu->cu->read_in_chain;
16077     }
16078
16079   per_cu = dwarf2_per_objfile->read_in_chain;
16080   last_chain = &dwarf2_per_objfile->read_in_chain;
16081   while (per_cu != NULL)
16082     {
16083       struct dwarf2_per_cu_data *next_cu;
16084
16085       next_cu = per_cu->cu->read_in_chain;
16086
16087       if (!per_cu->cu->mark)
16088         {
16089           free_one_comp_unit (per_cu->cu);
16090           *last_chain = next_cu;
16091         }
16092       else
16093         last_chain = &per_cu->cu->read_in_chain;
16094
16095       per_cu = next_cu;
16096     }
16097 }
16098
16099 /* Remove a single compilation unit from the cache.  */
16100
16101 static void
16102 free_one_cached_comp_unit (void *target_cu)
16103 {
16104   struct dwarf2_per_cu_data *per_cu, **last_chain;
16105
16106   per_cu = dwarf2_per_objfile->read_in_chain;
16107   last_chain = &dwarf2_per_objfile->read_in_chain;
16108   while (per_cu != NULL)
16109     {
16110       struct dwarf2_per_cu_data *next_cu;
16111
16112       next_cu = per_cu->cu->read_in_chain;
16113
16114       if (per_cu->cu == target_cu)
16115         {
16116           free_one_comp_unit (per_cu->cu);
16117           *last_chain = next_cu;
16118           break;
16119         }
16120       else
16121         last_chain = &per_cu->cu->read_in_chain;
16122
16123       per_cu = next_cu;
16124     }
16125 }
16126
16127 /* Release all extra memory associated with OBJFILE.  */
16128
16129 void
16130 dwarf2_free_objfile (struct objfile *objfile)
16131 {
16132   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16133
16134   if (dwarf2_per_objfile == NULL)
16135     return;
16136
16137   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
16138   free_cached_comp_units (NULL);
16139
16140   if (dwarf2_per_objfile->quick_file_names_table)
16141     htab_delete (dwarf2_per_objfile->quick_file_names_table);
16142
16143   /* Everything else should be on the objfile obstack.  */
16144 }
16145
16146 /* A pair of DIE offset and GDB type pointer.  We store these
16147    in a hash table separate from the DIEs, and preserve them
16148    when the DIEs are flushed out of cache.  */
16149
16150 struct dwarf2_offset_and_type
16151 {
16152   unsigned int offset;
16153   struct type *type;
16154 };
16155
16156 /* Hash function for a dwarf2_offset_and_type.  */
16157
16158 static hashval_t
16159 offset_and_type_hash (const void *item)
16160 {
16161   const struct dwarf2_offset_and_type *ofs = item;
16162
16163   return ofs->offset;
16164 }
16165
16166 /* Equality function for a dwarf2_offset_and_type.  */
16167
16168 static int
16169 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16170 {
16171   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16172   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16173
16174   return ofs_lhs->offset == ofs_rhs->offset;
16175 }
16176
16177 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16178    table if necessary.  For convenience, return TYPE.
16179
16180    The DIEs reading must have careful ordering to:
16181     * Not cause infite loops trying to read in DIEs as a prerequisite for
16182       reading current DIE.
16183     * Not trying to dereference contents of still incompletely read in types
16184       while reading in other DIEs.
16185     * Enable referencing still incompletely read in types just by a pointer to
16186       the type without accessing its fields.
16187
16188    Therefore caller should follow these rules:
16189      * Try to fetch any prerequisite types we may need to build this DIE type
16190        before building the type and calling set_die_type.
16191      * After building type call set_die_type for current DIE as soon as
16192        possible before fetching more types to complete the current type.
16193      * Make the type as complete as possible before fetching more types.  */
16194
16195 static struct type *
16196 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16197 {
16198   struct dwarf2_offset_and_type **slot, ofs;
16199   struct objfile *objfile = cu->objfile;
16200   htab_t *type_hash_ptr;
16201
16202   /* For Ada types, make sure that the gnat-specific data is always
16203      initialized (if not already set).  There are a few types where
16204      we should not be doing so, because the type-specific area is
16205      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16206      where the type-specific area is used to store the floatformat).
16207      But this is not a problem, because the gnat-specific information
16208      is actually not needed for these types.  */
16209   if (need_gnat_info (cu)
16210       && TYPE_CODE (type) != TYPE_CODE_FUNC
16211       && TYPE_CODE (type) != TYPE_CODE_FLT
16212       && !HAVE_GNAT_AUX_INFO (type))
16213     INIT_GNAT_SPECIFIC (type);
16214
16215   if (cu->per_cu->debug_types_section)
16216     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16217   else
16218     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16219
16220   if (*type_hash_ptr == NULL)
16221     {
16222       *type_hash_ptr
16223         = htab_create_alloc_ex (127,
16224                                 offset_and_type_hash,
16225                                 offset_and_type_eq,
16226                                 NULL,
16227                                 &objfile->objfile_obstack,
16228                                 hashtab_obstack_allocate,
16229                                 dummy_obstack_deallocate);
16230     }
16231
16232   ofs.offset = die->offset;
16233   ofs.type = type;
16234   slot = (struct dwarf2_offset_and_type **)
16235     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16236   if (*slot)
16237     complaint (&symfile_complaints,
16238                _("A problem internal to GDB: DIE 0x%x has type already set"),
16239                die->offset);
16240   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16241   **slot = ofs;
16242   return type;
16243 }
16244
16245 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16246    table, or return NULL if the die does not have a saved type.  */
16247
16248 static struct type *
16249 get_die_type_at_offset (unsigned int offset,
16250                         struct dwarf2_per_cu_data *per_cu)
16251 {
16252   struct dwarf2_offset_and_type *slot, ofs;
16253   htab_t type_hash;
16254
16255   if (per_cu->debug_types_section)
16256     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16257   else
16258     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16259   if (type_hash == NULL)
16260     return NULL;
16261
16262   ofs.offset = offset;
16263   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16264   if (slot)
16265     return slot->type;
16266   else
16267     return NULL;
16268 }
16269
16270 /* Look up the type for DIE in the appropriate type_hash table,
16271    or return NULL if DIE does not have a saved type.  */
16272
16273 static struct type *
16274 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16275 {
16276   return get_die_type_at_offset (die->offset, cu->per_cu);
16277 }
16278
16279 /* Add a dependence relationship from CU to REF_PER_CU.  */
16280
16281 static void
16282 dwarf2_add_dependence (struct dwarf2_cu *cu,
16283                        struct dwarf2_per_cu_data *ref_per_cu)
16284 {
16285   void **slot;
16286
16287   if (cu->dependencies == NULL)
16288     cu->dependencies
16289       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16290                               NULL, &cu->comp_unit_obstack,
16291                               hashtab_obstack_allocate,
16292                               dummy_obstack_deallocate);
16293
16294   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16295   if (*slot == NULL)
16296     *slot = ref_per_cu;
16297 }
16298
16299 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16300    Set the mark field in every compilation unit in the
16301    cache that we must keep because we are keeping CU.  */
16302
16303 static int
16304 dwarf2_mark_helper (void **slot, void *data)
16305 {
16306   struct dwarf2_per_cu_data *per_cu;
16307
16308   per_cu = (struct dwarf2_per_cu_data *) *slot;
16309
16310   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16311      reading of the chain.  As such dependencies remain valid it is not much
16312      useful to track and undo them during QUIT cleanups.  */
16313   if (per_cu->cu == NULL)
16314     return 1;
16315
16316   if (per_cu->cu->mark)
16317     return 1;
16318   per_cu->cu->mark = 1;
16319
16320   if (per_cu->cu->dependencies != NULL)
16321     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16322
16323   return 1;
16324 }
16325
16326 /* Set the mark field in CU and in every other compilation unit in the
16327    cache that we must keep because we are keeping CU.  */
16328
16329 static void
16330 dwarf2_mark (struct dwarf2_cu *cu)
16331 {
16332   if (cu->mark)
16333     return;
16334   cu->mark = 1;
16335   if (cu->dependencies != NULL)
16336     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16337 }
16338
16339 static void
16340 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16341 {
16342   while (per_cu)
16343     {
16344       per_cu->cu->mark = 0;
16345       per_cu = per_cu->cu->read_in_chain;
16346     }
16347 }
16348
16349 /* Trivial hash function for partial_die_info: the hash value of a DIE
16350    is its offset in .debug_info for this objfile.  */
16351
16352 static hashval_t
16353 partial_die_hash (const void *item)
16354 {
16355   const struct partial_die_info *part_die = item;
16356
16357   return part_die->offset;
16358 }
16359
16360 /* Trivial comparison function for partial_die_info structures: two DIEs
16361    are equal if they have the same offset.  */
16362
16363 static int
16364 partial_die_eq (const void *item_lhs, const void *item_rhs)
16365 {
16366   const struct partial_die_info *part_die_lhs = item_lhs;
16367   const struct partial_die_info *part_die_rhs = item_rhs;
16368
16369   return part_die_lhs->offset == part_die_rhs->offset;
16370 }
16371
16372 static struct cmd_list_element *set_dwarf2_cmdlist;
16373 static struct cmd_list_element *show_dwarf2_cmdlist;
16374
16375 static void
16376 set_dwarf2_cmd (char *args, int from_tty)
16377 {
16378   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16379 }
16380
16381 static void
16382 show_dwarf2_cmd (char *args, int from_tty)
16383 {
16384   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16385 }
16386
16387 /* If section described by INFO was mmapped, munmap it now.  */
16388
16389 static void
16390 munmap_section_buffer (struct dwarf2_section_info *info)
16391 {
16392   if (info->map_addr != NULL)
16393     {
16394 #ifdef HAVE_MMAP
16395       int res;
16396
16397       res = munmap (info->map_addr, info->map_len);
16398       gdb_assert (res == 0);
16399 #else
16400       /* Without HAVE_MMAP, we should never be here to begin with.  */
16401       gdb_assert_not_reached ("no mmap support");
16402 #endif
16403     }
16404 }
16405
16406 /* munmap debug sections for OBJFILE, if necessary.  */
16407
16408 static void
16409 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16410 {
16411   struct dwarf2_per_objfile *data = d;
16412   int ix;
16413   struct dwarf2_section_info *section;
16414
16415   /* This is sorted according to the order they're defined in to make it easier
16416      to keep in sync.  */
16417   munmap_section_buffer (&data->info);
16418   munmap_section_buffer (&data->abbrev);
16419   munmap_section_buffer (&data->line);
16420   munmap_section_buffer (&data->loc);
16421   munmap_section_buffer (&data->macinfo);
16422   munmap_section_buffer (&data->macro);
16423   munmap_section_buffer (&data->str);
16424   munmap_section_buffer (&data->ranges);
16425   munmap_section_buffer (&data->frame);
16426   munmap_section_buffer (&data->eh_frame);
16427   munmap_section_buffer (&data->gdb_index);
16428
16429   for (ix = 0;
16430        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16431        ++ix)
16432     munmap_section_buffer (section);
16433
16434   VEC_free (dwarf2_section_info_def, data->types);
16435 }
16436
16437 \f
16438 /* The "save gdb-index" command.  */
16439
16440 /* The contents of the hash table we create when building the string
16441    table.  */
16442 struct strtab_entry
16443 {
16444   offset_type offset;
16445   const char *str;
16446 };
16447
16448 /* Hash function for a strtab_entry.
16449
16450    Function is used only during write_hash_table so no index format backward
16451    compatibility is needed.  */
16452
16453 static hashval_t
16454 hash_strtab_entry (const void *e)
16455 {
16456   const struct strtab_entry *entry = e;
16457   return mapped_index_string_hash (INT_MAX, entry->str);
16458 }
16459
16460 /* Equality function for a strtab_entry.  */
16461
16462 static int
16463 eq_strtab_entry (const void *a, const void *b)
16464 {
16465   const struct strtab_entry *ea = a;
16466   const struct strtab_entry *eb = b;
16467   return !strcmp (ea->str, eb->str);
16468 }
16469
16470 /* Create a strtab_entry hash table.  */
16471
16472 static htab_t
16473 create_strtab (void)
16474 {
16475   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16476                             xfree, xcalloc, xfree);
16477 }
16478
16479 /* Add a string to the constant pool.  Return the string's offset in
16480    host order.  */
16481
16482 static offset_type
16483 add_string (htab_t table, struct obstack *cpool, const char *str)
16484 {
16485   void **slot;
16486   struct strtab_entry entry;
16487   struct strtab_entry *result;
16488
16489   entry.str = str;
16490   slot = htab_find_slot (table, &entry, INSERT);
16491   if (*slot)
16492     result = *slot;
16493   else
16494     {
16495       result = XNEW (struct strtab_entry);
16496       result->offset = obstack_object_size (cpool);
16497       result->str = str;
16498       obstack_grow_str0 (cpool, str);
16499       *slot = result;
16500     }
16501   return result->offset;
16502 }
16503
16504 /* An entry in the symbol table.  */
16505 struct symtab_index_entry
16506 {
16507   /* The name of the symbol.  */
16508   const char *name;
16509   /* The offset of the name in the constant pool.  */
16510   offset_type index_offset;
16511   /* A sorted vector of the indices of all the CUs that hold an object
16512      of this name.  */
16513   VEC (offset_type) *cu_indices;
16514 };
16515
16516 /* The symbol table.  This is a power-of-2-sized hash table.  */
16517 struct mapped_symtab
16518 {
16519   offset_type n_elements;
16520   offset_type size;
16521   struct symtab_index_entry **data;
16522 };
16523
16524 /* Hash function for a symtab_index_entry.  */
16525
16526 static hashval_t
16527 hash_symtab_entry (const void *e)
16528 {
16529   const struct symtab_index_entry *entry = e;
16530   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16531                          sizeof (offset_type) * VEC_length (offset_type,
16532                                                             entry->cu_indices),
16533                          0);
16534 }
16535
16536 /* Equality function for a symtab_index_entry.  */
16537
16538 static int
16539 eq_symtab_entry (const void *a, const void *b)
16540 {
16541   const struct symtab_index_entry *ea = a;
16542   const struct symtab_index_entry *eb = b;
16543   int len = VEC_length (offset_type, ea->cu_indices);
16544   if (len != VEC_length (offset_type, eb->cu_indices))
16545     return 0;
16546   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16547                   VEC_address (offset_type, eb->cu_indices),
16548                   sizeof (offset_type) * len);
16549 }
16550
16551 /* Destroy a symtab_index_entry.  */
16552
16553 static void
16554 delete_symtab_entry (void *p)
16555 {
16556   struct symtab_index_entry *entry = p;
16557   VEC_free (offset_type, entry->cu_indices);
16558   xfree (entry);
16559 }
16560
16561 /* Create a hash table holding symtab_index_entry objects.  */
16562
16563 static htab_t
16564 create_symbol_hash_table (void)
16565 {
16566   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16567                             delete_symtab_entry, xcalloc, xfree);
16568 }
16569
16570 /* Create a new mapped symtab object.  */
16571
16572 static struct mapped_symtab *
16573 create_mapped_symtab (void)
16574 {
16575   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16576   symtab->n_elements = 0;
16577   symtab->size = 1024;
16578   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16579   return symtab;
16580 }
16581
16582 /* Destroy a mapped_symtab.  */
16583
16584 static void
16585 cleanup_mapped_symtab (void *p)
16586 {
16587   struct mapped_symtab *symtab = p;
16588   /* The contents of the array are freed when the other hash table is
16589      destroyed.  */
16590   xfree (symtab->data);
16591   xfree (symtab);
16592 }
16593
16594 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16595    the slot.
16596    
16597    Function is used only during write_hash_table so no index format backward
16598    compatibility is needed.  */
16599
16600 static struct symtab_index_entry **
16601 find_slot (struct mapped_symtab *symtab, const char *name)
16602 {
16603   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16604
16605   index = hash & (symtab->size - 1);
16606   step = ((hash * 17) & (symtab->size - 1)) | 1;
16607
16608   for (;;)
16609     {
16610       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16611         return &symtab->data[index];
16612       index = (index + step) & (symtab->size - 1);
16613     }
16614 }
16615
16616 /* Expand SYMTAB's hash table.  */
16617
16618 static void
16619 hash_expand (struct mapped_symtab *symtab)
16620 {
16621   offset_type old_size = symtab->size;
16622   offset_type i;
16623   struct symtab_index_entry **old_entries = symtab->data;
16624
16625   symtab->size *= 2;
16626   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16627
16628   for (i = 0; i < old_size; ++i)
16629     {
16630       if (old_entries[i])
16631         {
16632           struct symtab_index_entry **slot = find_slot (symtab,
16633                                                         old_entries[i]->name);
16634           *slot = old_entries[i];
16635         }
16636     }
16637
16638   xfree (old_entries);
16639 }
16640
16641 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16642    is the index of the CU in which the symbol appears.  */
16643
16644 static void
16645 add_index_entry (struct mapped_symtab *symtab, const char *name,
16646                  offset_type cu_index)
16647 {
16648   struct symtab_index_entry **slot;
16649
16650   ++symtab->n_elements;
16651   if (4 * symtab->n_elements / 3 >= symtab->size)
16652     hash_expand (symtab);
16653
16654   slot = find_slot (symtab, name);
16655   if (!*slot)
16656     {
16657       *slot = XNEW (struct symtab_index_entry);
16658       (*slot)->name = name;
16659       (*slot)->cu_indices = NULL;
16660     }
16661   /* Don't push an index twice.  Due to how we add entries we only
16662      have to check the last one.  */ 
16663   if (VEC_empty (offset_type, (*slot)->cu_indices)
16664       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16665     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16666 }
16667
16668 /* Add a vector of indices to the constant pool.  */
16669
16670 static offset_type
16671 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16672                       struct symtab_index_entry *entry)
16673 {
16674   void **slot;
16675
16676   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16677   if (!*slot)
16678     {
16679       offset_type len = VEC_length (offset_type, entry->cu_indices);
16680       offset_type val = MAYBE_SWAP (len);
16681       offset_type iter;
16682       int i;
16683
16684       *slot = entry;
16685       entry->index_offset = obstack_object_size (cpool);
16686
16687       obstack_grow (cpool, &val, sizeof (val));
16688       for (i = 0;
16689            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16690            ++i)
16691         {
16692           val = MAYBE_SWAP (iter);
16693           obstack_grow (cpool, &val, sizeof (val));
16694         }
16695     }
16696   else
16697     {
16698       struct symtab_index_entry *old_entry = *slot;
16699       entry->index_offset = old_entry->index_offset;
16700       entry = old_entry;
16701     }
16702   return entry->index_offset;
16703 }
16704
16705 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16706    constant pool entries going into the obstack CPOOL.  */
16707
16708 static void
16709 write_hash_table (struct mapped_symtab *symtab,
16710                   struct obstack *output, struct obstack *cpool)
16711 {
16712   offset_type i;
16713   htab_t symbol_hash_table;
16714   htab_t str_table;
16715
16716   symbol_hash_table = create_symbol_hash_table ();
16717   str_table = create_strtab ();
16718
16719   /* We add all the index vectors to the constant pool first, to
16720      ensure alignment is ok.  */
16721   for (i = 0; i < symtab->size; ++i)
16722     {
16723       if (symtab->data[i])
16724         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16725     }
16726
16727   /* Now write out the hash table.  */
16728   for (i = 0; i < symtab->size; ++i)
16729     {
16730       offset_type str_off, vec_off;
16731
16732       if (symtab->data[i])
16733         {
16734           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16735           vec_off = symtab->data[i]->index_offset;
16736         }
16737       else
16738         {
16739           /* While 0 is a valid constant pool index, it is not valid
16740              to have 0 for both offsets.  */
16741           str_off = 0;
16742           vec_off = 0;
16743         }
16744
16745       str_off = MAYBE_SWAP (str_off);
16746       vec_off = MAYBE_SWAP (vec_off);
16747
16748       obstack_grow (output, &str_off, sizeof (str_off));
16749       obstack_grow (output, &vec_off, sizeof (vec_off));
16750     }
16751
16752   htab_delete (str_table);
16753   htab_delete (symbol_hash_table);
16754 }
16755
16756 /* Struct to map psymtab to CU index in the index file.  */
16757 struct psymtab_cu_index_map
16758 {
16759   struct partial_symtab *psymtab;
16760   unsigned int cu_index;
16761 };
16762
16763 static hashval_t
16764 hash_psymtab_cu_index (const void *item)
16765 {
16766   const struct psymtab_cu_index_map *map = item;
16767
16768   return htab_hash_pointer (map->psymtab);
16769 }
16770
16771 static int
16772 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16773 {
16774   const struct psymtab_cu_index_map *lhs = item_lhs;
16775   const struct psymtab_cu_index_map *rhs = item_rhs;
16776
16777   return lhs->psymtab == rhs->psymtab;
16778 }
16779
16780 /* Helper struct for building the address table.  */
16781 struct addrmap_index_data
16782 {
16783   struct objfile *objfile;
16784   struct obstack *addr_obstack;
16785   htab_t cu_index_htab;
16786
16787   /* Non-zero if the previous_* fields are valid.
16788      We can't write an entry until we see the next entry (since it is only then
16789      that we know the end of the entry).  */
16790   int previous_valid;
16791   /* Index of the CU in the table of all CUs in the index file.  */
16792   unsigned int previous_cu_index;
16793   /* Start address of the CU.  */
16794   CORE_ADDR previous_cu_start;
16795 };
16796
16797 /* Write an address entry to OBSTACK.  */
16798
16799 static void
16800 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16801                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16802 {
16803   offset_type cu_index_to_write;
16804   char addr[8];
16805   CORE_ADDR baseaddr;
16806
16807   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16808
16809   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16810   obstack_grow (obstack, addr, 8);
16811   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16812   obstack_grow (obstack, addr, 8);
16813   cu_index_to_write = MAYBE_SWAP (cu_index);
16814   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16815 }
16816
16817 /* Worker function for traversing an addrmap to build the address table.  */
16818
16819 static int
16820 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16821 {
16822   struct addrmap_index_data *data = datap;
16823   struct partial_symtab *pst = obj;
16824   offset_type cu_index;
16825   void **slot;
16826
16827   if (data->previous_valid)
16828     add_address_entry (data->objfile, data->addr_obstack,
16829                        data->previous_cu_start, start_addr,
16830                        data->previous_cu_index);
16831
16832   data->previous_cu_start = start_addr;
16833   if (pst != NULL)
16834     {
16835       struct psymtab_cu_index_map find_map, *map;
16836       find_map.psymtab = pst;
16837       map = htab_find (data->cu_index_htab, &find_map);
16838       gdb_assert (map != NULL);
16839       data->previous_cu_index = map->cu_index;
16840       data->previous_valid = 1;
16841     }
16842   else
16843       data->previous_valid = 0;
16844
16845   return 0;
16846 }
16847
16848 /* Write OBJFILE's address map to OBSTACK.
16849    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16850    in the index file.  */
16851
16852 static void
16853 write_address_map (struct objfile *objfile, struct obstack *obstack,
16854                    htab_t cu_index_htab)
16855 {
16856   struct addrmap_index_data addrmap_index_data;
16857
16858   /* When writing the address table, we have to cope with the fact that
16859      the addrmap iterator only provides the start of a region; we have to
16860      wait until the next invocation to get the start of the next region.  */
16861
16862   addrmap_index_data.objfile = objfile;
16863   addrmap_index_data.addr_obstack = obstack;
16864   addrmap_index_data.cu_index_htab = cu_index_htab;
16865   addrmap_index_data.previous_valid = 0;
16866
16867   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16868                    &addrmap_index_data);
16869
16870   /* It's highly unlikely the last entry (end address = 0xff...ff)
16871      is valid, but we should still handle it.
16872      The end address is recorded as the start of the next region, but that
16873      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16874      anyway.  */
16875   if (addrmap_index_data.previous_valid)
16876     add_address_entry (objfile, obstack,
16877                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16878                        addrmap_index_data.previous_cu_index);
16879 }
16880
16881 /* Add a list of partial symbols to SYMTAB.  */
16882
16883 static void
16884 write_psymbols (struct mapped_symtab *symtab,
16885                 htab_t psyms_seen,
16886                 struct partial_symbol **psymp,
16887                 int count,
16888                 offset_type cu_index,
16889                 int is_static)
16890 {
16891   for (; count-- > 0; ++psymp)
16892     {
16893       void **slot, *lookup;
16894
16895       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16896         error (_("Ada is not currently supported by the index"));
16897
16898       /* We only want to add a given psymbol once.  However, we also
16899          want to account for whether it is global or static.  So, we
16900          may add it twice, using slightly different values.  */
16901       if (is_static)
16902         {
16903           uintptr_t val = 1 | (uintptr_t) *psymp;
16904
16905           lookup = (void *) val;
16906         }
16907       else
16908         lookup = *psymp;
16909
16910       /* Only add a given psymbol once.  */
16911       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16912       if (!*slot)
16913         {
16914           *slot = lookup;
16915           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16916         }
16917     }
16918 }
16919
16920 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16921    exception if there is an error.  */
16922
16923 static void
16924 write_obstack (FILE *file, struct obstack *obstack)
16925 {
16926   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16927               file)
16928       != obstack_object_size (obstack))
16929     error (_("couldn't data write to file"));
16930 }
16931
16932 /* Unlink a file if the argument is not NULL.  */
16933
16934 static void
16935 unlink_if_set (void *p)
16936 {
16937   char **filename = p;
16938   if (*filename)
16939     unlink (*filename);
16940 }
16941
16942 /* A helper struct used when iterating over debug_types.  */
16943 struct signatured_type_index_data
16944 {
16945   struct objfile *objfile;
16946   struct mapped_symtab *symtab;
16947   struct obstack *types_list;
16948   htab_t psyms_seen;
16949   int cu_index;
16950 };
16951
16952 /* A helper function that writes a single signatured_type to an
16953    obstack.  */
16954
16955 static int
16956 write_one_signatured_type (void **slot, void *d)
16957 {
16958   struct signatured_type_index_data *info = d;
16959   struct signatured_type *entry = (struct signatured_type *) *slot;
16960   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16961   struct partial_symtab *psymtab = per_cu->v.psymtab;
16962   gdb_byte val[8];
16963
16964   write_psymbols (info->symtab,
16965                   info->psyms_seen,
16966                   info->objfile->global_psymbols.list
16967                   + psymtab->globals_offset,
16968                   psymtab->n_global_syms, info->cu_index,
16969                   0);
16970   write_psymbols (info->symtab,
16971                   info->psyms_seen,
16972                   info->objfile->static_psymbols.list
16973                   + psymtab->statics_offset,
16974                   psymtab->n_static_syms, info->cu_index,
16975                   1);
16976
16977   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16978   obstack_grow (info->types_list, val, 8);
16979   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16980   obstack_grow (info->types_list, val, 8);
16981   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16982   obstack_grow (info->types_list, val, 8);
16983
16984   ++info->cu_index;
16985
16986   return 1;
16987 }
16988
16989 /* Create an index file for OBJFILE in the directory DIR.  */
16990
16991 static void
16992 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16993 {
16994   struct cleanup *cleanup;
16995   char *filename, *cleanup_filename;
16996   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16997   struct obstack cu_list, types_cu_list;
16998   int i;
16999   FILE *out_file;
17000   struct mapped_symtab *symtab;
17001   offset_type val, size_of_contents, total_len;
17002   struct stat st;
17003   char buf[8];
17004   htab_t psyms_seen;
17005   htab_t cu_index_htab;
17006   struct psymtab_cu_index_map *psymtab_cu_index_map;
17007
17008   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17009     return;
17010
17011   if (dwarf2_per_objfile->using_index)
17012     error (_("Cannot use an index to create the index"));
17013
17014   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17015     error (_("Cannot make an index when the file has multiple .debug_types sections"));
17016
17017   if (stat (objfile->name, &st) < 0)
17018     perror_with_name (objfile->name);
17019
17020   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17021                      INDEX_SUFFIX, (char *) NULL);
17022   cleanup = make_cleanup (xfree, filename);
17023
17024   out_file = fopen (filename, "wb");
17025   if (!out_file)
17026     error (_("Can't open `%s' for writing"), filename);
17027
17028   cleanup_filename = filename;
17029   make_cleanup (unlink_if_set, &cleanup_filename);
17030
17031   symtab = create_mapped_symtab ();
17032   make_cleanup (cleanup_mapped_symtab, symtab);
17033
17034   obstack_init (&addr_obstack);
17035   make_cleanup_obstack_free (&addr_obstack);
17036
17037   obstack_init (&cu_list);
17038   make_cleanup_obstack_free (&cu_list);
17039
17040   obstack_init (&types_cu_list);
17041   make_cleanup_obstack_free (&types_cu_list);
17042
17043   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17044                                   NULL, xcalloc, xfree);
17045   make_cleanup_htab_delete (psyms_seen);
17046
17047   /* While we're scanning CU's create a table that maps a psymtab pointer
17048      (which is what addrmap records) to its index (which is what is recorded
17049      in the index file).  This will later be needed to write the address
17050      table.  */
17051   cu_index_htab = htab_create_alloc (100,
17052                                      hash_psymtab_cu_index,
17053                                      eq_psymtab_cu_index,
17054                                      NULL, xcalloc, xfree);
17055   make_cleanup_htab_delete (cu_index_htab);
17056   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17057     xmalloc (sizeof (struct psymtab_cu_index_map)
17058              * dwarf2_per_objfile->n_comp_units);
17059   make_cleanup (xfree, psymtab_cu_index_map);
17060
17061   /* The CU list is already sorted, so we don't need to do additional
17062      work here.  Also, the debug_types entries do not appear in
17063      all_comp_units, but only in their own hash table.  */
17064   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17065     {
17066       struct dwarf2_per_cu_data *per_cu
17067         = dwarf2_per_objfile->all_comp_units[i];
17068       struct partial_symtab *psymtab = per_cu->v.psymtab;
17069       gdb_byte val[8];
17070       struct psymtab_cu_index_map *map;
17071       void **slot;
17072
17073       write_psymbols (symtab,
17074                       psyms_seen,
17075                       objfile->global_psymbols.list + psymtab->globals_offset,
17076                       psymtab->n_global_syms, i,
17077                       0);
17078       write_psymbols (symtab,
17079                       psyms_seen,
17080                       objfile->static_psymbols.list + psymtab->statics_offset,
17081                       psymtab->n_static_syms, i,
17082                       1);
17083
17084       map = &psymtab_cu_index_map[i];
17085       map->psymtab = psymtab;
17086       map->cu_index = i;
17087       slot = htab_find_slot (cu_index_htab, map, INSERT);
17088       gdb_assert (slot != NULL);
17089       gdb_assert (*slot == NULL);
17090       *slot = map;
17091
17092       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
17093       obstack_grow (&cu_list, val, 8);
17094       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17095       obstack_grow (&cu_list, val, 8);
17096     }
17097
17098   /* Dump the address map.  */
17099   write_address_map (objfile, &addr_obstack, cu_index_htab);
17100
17101   /* Write out the .debug_type entries, if any.  */
17102   if (dwarf2_per_objfile->signatured_types)
17103     {
17104       struct signatured_type_index_data sig_data;
17105
17106       sig_data.objfile = objfile;
17107       sig_data.symtab = symtab;
17108       sig_data.types_list = &types_cu_list;
17109       sig_data.psyms_seen = psyms_seen;
17110       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17111       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17112                               write_one_signatured_type, &sig_data);
17113     }
17114
17115   obstack_init (&constant_pool);
17116   make_cleanup_obstack_free (&constant_pool);
17117   obstack_init (&symtab_obstack);
17118   make_cleanup_obstack_free (&symtab_obstack);
17119   write_hash_table (symtab, &symtab_obstack, &constant_pool);
17120
17121   obstack_init (&contents);
17122   make_cleanup_obstack_free (&contents);
17123   size_of_contents = 6 * sizeof (offset_type);
17124   total_len = size_of_contents;
17125
17126   /* The version number.  */
17127   val = MAYBE_SWAP (5);
17128   obstack_grow (&contents, &val, sizeof (val));
17129
17130   /* The offset of the CU list from the start of the file.  */
17131   val = MAYBE_SWAP (total_len);
17132   obstack_grow (&contents, &val, sizeof (val));
17133   total_len += obstack_object_size (&cu_list);
17134
17135   /* The offset of the types CU list from the start of the file.  */
17136   val = MAYBE_SWAP (total_len);
17137   obstack_grow (&contents, &val, sizeof (val));
17138   total_len += obstack_object_size (&types_cu_list);
17139
17140   /* The offset of the address table from the start of the file.  */
17141   val = MAYBE_SWAP (total_len);
17142   obstack_grow (&contents, &val, sizeof (val));
17143   total_len += obstack_object_size (&addr_obstack);
17144
17145   /* The offset of the symbol table from the start of the file.  */
17146   val = MAYBE_SWAP (total_len);
17147   obstack_grow (&contents, &val, sizeof (val));
17148   total_len += obstack_object_size (&symtab_obstack);
17149
17150   /* The offset of the constant pool from the start of the file.  */
17151   val = MAYBE_SWAP (total_len);
17152   obstack_grow (&contents, &val, sizeof (val));
17153   total_len += obstack_object_size (&constant_pool);
17154
17155   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17156
17157   write_obstack (out_file, &contents);
17158   write_obstack (out_file, &cu_list);
17159   write_obstack (out_file, &types_cu_list);
17160   write_obstack (out_file, &addr_obstack);
17161   write_obstack (out_file, &symtab_obstack);
17162   write_obstack (out_file, &constant_pool);
17163
17164   fclose (out_file);
17165
17166   /* We want to keep the file, so we set cleanup_filename to NULL
17167      here.  See unlink_if_set.  */
17168   cleanup_filename = NULL;
17169
17170   do_cleanups (cleanup);
17171 }
17172
17173 /* Implementation of the `save gdb-index' command.
17174    
17175    Note that the file format used by this command is documented in the
17176    GDB manual.  Any changes here must be documented there.  */
17177
17178 static void
17179 save_gdb_index_command (char *arg, int from_tty)
17180 {
17181   struct objfile *objfile;
17182
17183   if (!arg || !*arg)
17184     error (_("usage: save gdb-index DIRECTORY"));
17185
17186   ALL_OBJFILES (objfile)
17187   {
17188     struct stat st;
17189
17190     /* If the objfile does not correspond to an actual file, skip it.  */
17191     if (stat (objfile->name, &st) < 0)
17192       continue;
17193
17194     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17195     if (dwarf2_per_objfile)
17196       {
17197         volatile struct gdb_exception except;
17198
17199         TRY_CATCH (except, RETURN_MASK_ERROR)
17200           {
17201             write_psymtabs_to_index (objfile, arg);
17202           }
17203         if (except.reason < 0)
17204           exception_fprintf (gdb_stderr, except,
17205                              _("Error while writing index for `%s': "),
17206                              objfile->name);
17207       }
17208   }
17209 }
17210
17211 \f
17212
17213 int dwarf2_always_disassemble;
17214
17215 static void
17216 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17217                                 struct cmd_list_element *c, const char *value)
17218 {
17219   fprintf_filtered (file,
17220                     _("Whether to always disassemble "
17221                       "DWARF expressions is %s.\n"),
17222                     value);
17223 }
17224
17225 static void
17226 show_check_physname (struct ui_file *file, int from_tty,
17227                      struct cmd_list_element *c, const char *value)
17228 {
17229   fprintf_filtered (file,
17230                     _("Whether to check \"physname\" is %s.\n"),
17231                     value);
17232 }
17233
17234 void _initialize_dwarf2_read (void);
17235
17236 void
17237 _initialize_dwarf2_read (void)
17238 {
17239   struct cmd_list_element *c;
17240
17241   dwarf2_objfile_data_key
17242     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17243
17244   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17245 Set DWARF 2 specific variables.\n\
17246 Configure DWARF 2 variables such as the cache size"),
17247                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17248                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17249
17250   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17251 Show DWARF 2 specific variables\n\
17252 Show DWARF 2 variables such as the cache size"),
17253                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17254                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17255
17256   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17257                             &dwarf2_max_cache_age, _("\
17258 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17259 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17260 A higher limit means that cached compilation units will be stored\n\
17261 in memory longer, and more total memory will be used.  Zero disables\n\
17262 caching, which can slow down startup."),
17263                             NULL,
17264                             show_dwarf2_max_cache_age,
17265                             &set_dwarf2_cmdlist,
17266                             &show_dwarf2_cmdlist);
17267
17268   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17269                            &dwarf2_always_disassemble, _("\
17270 Set whether `info address' always disassembles DWARF expressions."), _("\
17271 Show whether `info address' always disassembles DWARF expressions."), _("\
17272 When enabled, DWARF expressions are always printed in an assembly-like\n\
17273 syntax.  When disabled, expressions will be printed in a more\n\
17274 conversational style, when possible."),
17275                            NULL,
17276                            show_dwarf2_always_disassemble,
17277                            &set_dwarf2_cmdlist,
17278                            &show_dwarf2_cmdlist);
17279
17280   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17281 Set debugging of the dwarf2 DIE reader."), _("\
17282 Show debugging of the dwarf2 DIE reader."), _("\
17283 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17284 The value is the maximum depth to print."),
17285                             NULL,
17286                             NULL,
17287                             &setdebuglist, &showdebuglist);
17288
17289   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17290 Set cross-checking of \"physname\" code against demangler."), _("\
17291 Show cross-checking of \"physname\" code against demangler."), _("\
17292 When enabled, GDB's internal \"physname\" code is checked against\n\
17293 the demangler."),
17294                            NULL, show_check_physname,
17295                            &setdebuglist, &showdebuglist);
17296
17297   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17298                _("\
17299 Save a gdb-index file.\n\
17300 Usage: save gdb-index DIRECTORY"),
17301                &save_cmdlist);
17302   set_cmd_completer (c, filename_completer);
17303 }