* dwarf2read.c (init_one_comp_unit): Delete arg "objfile".
[external/binutils.git] / 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 /* Collection of data recorded per objfile.
183    This hangs off of dwarf2_objfile_data_key.  */
184
185 struct dwarf2_per_objfile
186 {
187   struct dwarf2_section_info info;
188   struct dwarf2_section_info abbrev;
189   struct dwarf2_section_info line;
190   struct dwarf2_section_info loc;
191   struct dwarf2_section_info macinfo;
192   struct dwarf2_section_info macro;
193   struct dwarf2_section_info str;
194   struct dwarf2_section_info ranges;
195   struct dwarf2_section_info frame;
196   struct dwarf2_section_info eh_frame;
197   struct dwarf2_section_info gdb_index;
198
199   VEC (dwarf2_section_info_def) *types;
200
201   /* Back link.  */
202   struct objfile *objfile;
203
204   /* A list of all the compilation units.  This is used to locate
205      the target compilation unit of a particular reference.  */
206   struct dwarf2_per_cu_data **all_comp_units;
207
208   /* The number of compilation units in ALL_COMP_UNITS.  */
209   int n_comp_units;
210
211   /* The number of .debug_types-related CUs.  */
212   int n_type_comp_units;
213
214   /* The .debug_types-related CUs.  */
215   struct dwarf2_per_cu_data **type_comp_units;
216
217   /* A chain of compilation units that are currently read in, so that
218      they can be freed later.  */
219   struct dwarf2_per_cu_data *read_in_chain;
220
221   /* A table mapping .debug_types signatures to its signatured_type entry.
222      This is NULL if the .debug_types section hasn't been read in yet.  */
223   htab_t signatured_types;
224
225   /* A flag indicating wether this objfile has a section loaded at a
226      VMA of 0.  */
227   int has_section_at_zero;
228
229   /* True if we are using the mapped index,
230      or we are faking it for OBJF_READNOW's sake.  */
231   unsigned char using_index;
232
233   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
234   struct mapped_index *index_table;
235
236   /* When using index_table, this keeps track of all quick_file_names entries.
237      TUs can share line table entries with CUs or other TUs, and there can be
238      a lot more TUs than unique line tables, so we maintain a separate table
239      of all line table entries to support the sharing.  */
240   htab_t quick_file_names_table;
241
242   /* Set during partial symbol reading, to prevent queueing of full
243      symbols.  */
244   int reading_partial_symbols;
245
246   /* Table mapping type .debug_info DIE offsets to types.
247      This is NULL if not allocated yet.
248      It (currently) makes sense to allocate debug_types_type_hash lazily.
249      To keep things simple we allocate both lazily.  */
250   htab_t debug_info_type_hash;
251
252   /* Table mapping type .debug_types DIE offsets to types.
253      This is NULL if not allocated yet.  */
254   htab_t debug_types_type_hash;
255 };
256
257 static struct dwarf2_per_objfile *dwarf2_per_objfile;
258
259 /* Default names of the debugging sections.  */
260
261 /* Note that if the debugging section has been compressed, it might
262    have a name like .zdebug_info.  */
263
264 static const struct dwarf2_debug_sections dwarf2_elf_names =
265 {
266   { ".debug_info", ".zdebug_info" },
267   { ".debug_abbrev", ".zdebug_abbrev" },
268   { ".debug_line", ".zdebug_line" },
269   { ".debug_loc", ".zdebug_loc" },
270   { ".debug_macinfo", ".zdebug_macinfo" },
271   { ".debug_macro", ".zdebug_macro" },
272   { ".debug_str", ".zdebug_str" },
273   { ".debug_ranges", ".zdebug_ranges" },
274   { ".debug_types", ".zdebug_types" },
275   { ".debug_frame", ".zdebug_frame" },
276   { ".eh_frame", NULL },
277   { ".gdb_index", ".zgdb_index" },
278   23
279 };
280
281 /* local data types */
282
283 /* We hold several abbreviation tables in memory at the same time.  */
284 #ifndef ABBREV_HASH_SIZE
285 #define ABBREV_HASH_SIZE 121
286 #endif
287
288 /* The data in a compilation unit header, after target2host
289    translation, looks like this.  */
290 struct comp_unit_head
291 {
292   unsigned int length;
293   short version;
294   unsigned char addr_size;
295   unsigned char signed_addr_p;
296   unsigned int abbrev_offset;
297
298   /* Size of file offsets; either 4 or 8.  */
299   unsigned int offset_size;
300
301   /* Size of the length field; either 4 or 12.  */
302   unsigned int initial_length_size;
303
304   /* Offset to the first byte of this compilation unit header in the
305      .debug_info section, for resolving relative reference dies.  */
306   unsigned int offset;
307
308   /* Offset to first die in this cu from the start of the cu.
309      This will be the first byte following the compilation unit header.  */
310   unsigned int first_die_offset;
311 };
312
313 /* Type used for delaying computation of method physnames.
314    See comments for compute_delayed_physnames.  */
315 struct delayed_method_info
316 {
317   /* The type to which the method is attached, i.e., its parent class.  */
318   struct type *type;
319
320   /* The index of the method in the type's function fieldlists.  */
321   int fnfield_index;
322
323   /* The index of the method in the fieldlist.  */
324   int index;
325
326   /* The name of the DIE.  */
327   const char *name;
328
329   /*  The DIE associated with this method.  */
330   struct die_info *die;
331 };
332
333 typedef struct delayed_method_info delayed_method_info;
334 DEF_VEC_O (delayed_method_info);
335
336 /* Internal state when decoding a particular compilation unit.  */
337 struct dwarf2_cu
338 {
339   /* The objfile containing this compilation unit.  */
340   struct objfile *objfile;
341
342   /* The header of the compilation unit.  */
343   struct comp_unit_head header;
344
345   /* Base address of this compilation unit.  */
346   CORE_ADDR base_address;
347
348   /* Non-zero if base_address has been set.  */
349   int base_known;
350
351   /* The language we are debugging.  */
352   enum language language;
353   const struct language_defn *language_defn;
354
355   const char *producer;
356
357   /* The generic symbol table building routines have separate lists for
358      file scope symbols and all all other scopes (local scopes).  So
359      we need to select the right one to pass to add_symbol_to_list().
360      We do it by keeping a pointer to the correct list in list_in_scope.
361
362      FIXME: The original dwarf code just treated the file scope as the
363      first local scope, and all other local scopes as nested local
364      scopes, and worked fine.  Check to see if we really need to
365      distinguish these in buildsym.c.  */
366   struct pending **list_in_scope;
367
368   /* DWARF abbreviation table associated with this compilation unit.  */
369   struct abbrev_info **dwarf2_abbrevs;
370
371   /* Storage for the abbrev table.  */
372   struct obstack abbrev_obstack;
373
374   /* Hash table holding all the loaded partial DIEs.  */
375   htab_t partial_dies;
376
377   /* Storage for things with the same lifetime as this read-in compilation
378      unit, including partial DIEs.  */
379   struct obstack comp_unit_obstack;
380
381   /* When multiple dwarf2_cu structures are living in memory, this field
382      chains them all together, so that they can be released efficiently.
383      We will probably also want a generation counter so that most-recently-used
384      compilation units are cached...  */
385   struct dwarf2_per_cu_data *read_in_chain;
386
387   /* Backchain to our per_cu entry if the tree has been built.  */
388   struct dwarf2_per_cu_data *per_cu;
389
390   /* How many compilation units ago was this CU last referenced?  */
391   int last_used;
392
393   /* A hash table of die offsets for following references.  */
394   htab_t die_hash;
395
396   /* Full DIEs if read in.  */
397   struct die_info *dies;
398
399   /* A set of pointers to dwarf2_per_cu_data objects for compilation
400      units referenced by this one.  Only set during full symbol processing;
401      partial symbol tables do not have dependencies.  */
402   htab_t dependencies;
403
404   /* Header data from the line table, during full symbol processing.  */
405   struct line_header *line_header;
406
407   /* A list of methods which need to have physnames computed
408      after all type information has been read.  */
409   VEC (delayed_method_info) *method_list;
410
411   /* To be copied to symtab->call_site_htab.  */
412   htab_t call_site_htab;
413
414   /* Mark used when releasing cached dies.  */
415   unsigned int mark : 1;
416
417   /* This flag will be set if this compilation unit might include
418      inter-compilation-unit references.  */
419   unsigned int has_form_ref_addr : 1;
420
421   /* This flag will be set if this compilation unit includes any
422      DW_TAG_namespace DIEs.  If we know that there are explicit
423      DIEs for namespaces, we don't need to try to infer them
424      from mangled names.  */
425   unsigned int has_namespace_info : 1;
426
427   /* This CU references .debug_loc.  See the symtab->locations_valid field.
428      This test is imperfect as there may exist optimized debug code not using
429      any location list and still facing inlining issues if handled as
430      unoptimized code.  For a future better test see GCC PR other/32998.  */
431   unsigned int has_loclist : 1;
432 };
433
434 /* Persistent data held for a compilation unit, even when not
435    processing it.  We put a pointer to this structure in the
436    read_symtab_private field of the psymtab.  If we encounter
437    inter-compilation-unit references, we also maintain a sorted
438    list of all compilation units.  */
439
440 struct dwarf2_per_cu_data
441 {
442   /* The start offset and length of this compilation unit.  2**29-1
443      bytes should suffice to store the length of any compilation unit
444      - if it doesn't, GDB will fall over anyway.
445      NOTE: Unlike comp_unit_head.length, this length includes
446      initial_length_size.  */
447   unsigned int offset;
448   unsigned int length : 29;
449
450   /* Flag indicating this compilation unit will be read in before
451      any of the current compilation units are processed.  */
452   unsigned int queued : 1;
453
454   /* This flag will be set if we need to load absolutely all DIEs
455      for this compilation unit, instead of just the ones we think
456      are interesting.  It gets set if we look for a DIE in the
457      hash table and don't find it.  */
458   unsigned int load_all_dies : 1;
459
460   /* Non-null if this CU is from .debug_types; in which case it points
461      to the section.  Otherwise it's from .debug_info.  */
462   struct dwarf2_section_info *debug_types_section;
463
464   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
465      of the CU cache it gets reset to NULL again.  */
466   struct dwarf2_cu *cu;
467
468   /* The corresponding objfile.
469      Normally we can get the objfile from dwarf2_per_objfile.
470      However we can enter this file with just a "per_cu" handle.  */
471   struct objfile *objfile;
472
473   /* When using partial symbol tables, the 'psymtab' field is active.
474      Otherwise the 'quick' field is active.  */
475   union
476   {
477     /* The partial symbol table associated with this compilation unit,
478        or NULL for partial units (which do not have an associated
479        symtab).  */
480     struct partial_symtab *psymtab;
481
482     /* Data needed by the "quick" functions.  */
483     struct dwarf2_per_cu_quick_data *quick;
484   } v;
485 };
486
487 /* Entry in the signatured_types hash table.  */
488
489 struct signatured_type
490 {
491   ULONGEST signature;
492
493   /* Offset in .debug_types of the type defined by this TU.  */
494   unsigned int type_offset;
495
496   /* The CU(/TU) of this type.  */
497   struct dwarf2_per_cu_data per_cu;
498 };
499
500 /* Struct used to pass misc. parameters to read_die_and_children, et
501    al.  which are used for both .debug_info and .debug_types dies.
502    All parameters here are unchanging for the life of the call.  This
503    struct exists to abstract away the constant parameters of die
504    reading.  */
505
506 struct die_reader_specs
507 {
508   /* The bfd of this objfile.  */
509   bfd* abfd;
510
511   /* The CU of the DIE we are parsing.  */
512   struct dwarf2_cu *cu;
513
514   /* Pointer to start of section buffer.
515      This is either the start of .debug_info or .debug_types.  */
516   const gdb_byte *buffer;
517 };
518
519 /* The line number information for a compilation unit (found in the
520    .debug_line section) begins with a "statement program header",
521    which contains the following information.  */
522 struct line_header
523 {
524   unsigned int total_length;
525   unsigned short version;
526   unsigned int header_length;
527   unsigned char minimum_instruction_length;
528   unsigned char maximum_ops_per_instruction;
529   unsigned char default_is_stmt;
530   int line_base;
531   unsigned char line_range;
532   unsigned char opcode_base;
533
534   /* standard_opcode_lengths[i] is the number of operands for the
535      standard opcode whose value is i.  This means that
536      standard_opcode_lengths[0] is unused, and the last meaningful
537      element is standard_opcode_lengths[opcode_base - 1].  */
538   unsigned char *standard_opcode_lengths;
539
540   /* The include_directories table.  NOTE!  These strings are not
541      allocated with xmalloc; instead, they are pointers into
542      debug_line_buffer.  If you try to free them, `free' will get
543      indigestion.  */
544   unsigned int num_include_dirs, include_dirs_size;
545   char **include_dirs;
546
547   /* The file_names table.  NOTE!  These strings are not allocated
548      with xmalloc; instead, they are pointers into debug_line_buffer.
549      Don't try to free them directly.  */
550   unsigned int num_file_names, file_names_size;
551   struct file_entry
552   {
553     char *name;
554     unsigned int dir_index;
555     unsigned int mod_time;
556     unsigned int length;
557     int included_p; /* Non-zero if referenced by the Line Number Program.  */
558     struct symtab *symtab; /* The associated symbol table, if any.  */
559   } *file_names;
560
561   /* The start and end of the statement program following this
562      header.  These point into dwarf2_per_objfile->line_buffer.  */
563   gdb_byte *statement_program_start, *statement_program_end;
564 };
565
566 /* When we construct a partial symbol table entry we only
567    need this much information.  */
568 struct partial_die_info
569   {
570     /* Offset of this DIE.  */
571     unsigned int offset;
572
573     /* DWARF-2 tag for this DIE.  */
574     ENUM_BITFIELD(dwarf_tag) tag : 16;
575
576     /* Assorted flags describing the data found in this DIE.  */
577     unsigned int has_children : 1;
578     unsigned int is_external : 1;
579     unsigned int is_declaration : 1;
580     unsigned int has_type : 1;
581     unsigned int has_specification : 1;
582     unsigned int has_pc_info : 1;
583
584     /* Flag set if the SCOPE field of this structure has been
585        computed.  */
586     unsigned int scope_set : 1;
587
588     /* Flag set if the DIE has a byte_size attribute.  */
589     unsigned int has_byte_size : 1;
590
591     /* Flag set if any of the DIE's children are template arguments.  */
592     unsigned int has_template_arguments : 1;
593
594     /* Flag set if fixup_partial_die has been called on this die.  */
595     unsigned int fixup_called : 1;
596
597     /* The name of this DIE.  Normally the value of DW_AT_name, but
598        sometimes a default name for unnamed DIEs.  */
599     char *name;
600
601     /* The linkage name, if present.  */
602     const char *linkage_name;
603
604     /* The scope to prepend to our children.  This is generally
605        allocated on the comp_unit_obstack, so will disappear
606        when this compilation unit leaves the cache.  */
607     char *scope;
608
609     /* The location description associated with this DIE, if any.  */
610     struct dwarf_block *locdesc;
611
612     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
613     CORE_ADDR lowpc;
614     CORE_ADDR highpc;
615
616     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
617        DW_AT_sibling, if any.  */
618     /* NOTE: This member isn't strictly necessary, read_partial_die could
619        return DW_AT_sibling values to its caller load_partial_dies.  */
620     gdb_byte *sibling;
621
622     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
623        DW_AT_specification (or DW_AT_abstract_origin or
624        DW_AT_extension).  */
625     unsigned int spec_offset;
626
627     /* Pointers to this DIE's parent, first child, and next sibling,
628        if any.  */
629     struct partial_die_info *die_parent, *die_child, *die_sibling;
630   };
631
632 /* This data structure holds the information of an abbrev.  */
633 struct abbrev_info
634   {
635     unsigned int number;        /* number identifying abbrev */
636     enum dwarf_tag tag;         /* dwarf tag */
637     unsigned short has_children;                /* boolean */
638     unsigned short num_attrs;   /* number of attributes */
639     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
640     struct abbrev_info *next;   /* next in chain */
641   };
642
643 struct attr_abbrev
644   {
645     ENUM_BITFIELD(dwarf_attribute) name : 16;
646     ENUM_BITFIELD(dwarf_form) form : 16;
647   };
648
649 /* Attributes have a name and a value.  */
650 struct attribute
651   {
652     ENUM_BITFIELD(dwarf_attribute) name : 16;
653     ENUM_BITFIELD(dwarf_form) form : 15;
654
655     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
656        field should be in u.str (existing only for DW_STRING) but it is kept
657        here for better struct attribute alignment.  */
658     unsigned int string_is_canonical : 1;
659
660     union
661       {
662         char *str;
663         struct dwarf_block *blk;
664         ULONGEST unsnd;
665         LONGEST snd;
666         CORE_ADDR addr;
667         struct signatured_type *signatured_type;
668       }
669     u;
670   };
671
672 /* This data structure holds a complete die structure.  */
673 struct die_info
674   {
675     /* DWARF-2 tag for this DIE.  */
676     ENUM_BITFIELD(dwarf_tag) tag : 16;
677
678     /* Number of attributes */
679     unsigned char num_attrs;
680
681     /* True if we're presently building the full type name for the
682        type derived from this DIE.  */
683     unsigned char building_fullname : 1;
684
685     /* Abbrev number */
686     unsigned int abbrev;
687
688     /* Offset in .debug_info or .debug_types section.  */
689     unsigned int offset;
690
691     /* The dies in a compilation unit form an n-ary tree.  PARENT
692        points to this die's parent; CHILD points to the first child of
693        this node; and all the children of a given node are chained
694        together via their SIBLING fields.  */
695     struct die_info *child;     /* Its first child, if any.  */
696     struct die_info *sibling;   /* Its next sibling, if any.  */
697     struct die_info *parent;    /* Its parent, if any.  */
698
699     /* An array of attributes, with NUM_ATTRS elements.  There may be
700        zero, but it's not common and zero-sized arrays are not
701        sufficiently portable C.  */
702     struct attribute attrs[1];
703   };
704
705 /* Get at parts of an attribute structure.  */
706
707 #define DW_STRING(attr)    ((attr)->u.str)
708 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
709 #define DW_UNSND(attr)     ((attr)->u.unsnd)
710 #define DW_BLOCK(attr)     ((attr)->u.blk)
711 #define DW_SND(attr)       ((attr)->u.snd)
712 #define DW_ADDR(attr)      ((attr)->u.addr)
713 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
714
715 /* Blocks are a bunch of untyped bytes.  */
716 struct dwarf_block
717   {
718     unsigned int size;
719
720     /* Valid only if SIZE is not zero.  */
721     gdb_byte *data;
722   };
723
724 #ifndef ATTR_ALLOC_CHUNK
725 #define ATTR_ALLOC_CHUNK 4
726 #endif
727
728 /* Allocate fields for structs, unions and enums in this size.  */
729 #ifndef DW_FIELD_ALLOC_CHUNK
730 #define DW_FIELD_ALLOC_CHUNK 4
731 #endif
732
733 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
734    but this would require a corresponding change in unpack_field_as_long
735    and friends.  */
736 static int bits_per_byte = 8;
737
738 /* The routines that read and process dies for a C struct or C++ class
739    pass lists of data member fields and lists of member function fields
740    in an instance of a field_info structure, as defined below.  */
741 struct field_info
742   {
743     /* List of data member and baseclasses fields.  */
744     struct nextfield
745       {
746         struct nextfield *next;
747         int accessibility;
748         int virtuality;
749         struct field field;
750       }
751      *fields, *baseclasses;
752
753     /* Number of fields (including baseclasses).  */
754     int nfields;
755
756     /* Number of baseclasses.  */
757     int nbaseclasses;
758
759     /* Set if the accesibility of one of the fields is not public.  */
760     int non_public_fields;
761
762     /* Member function fields array, entries are allocated in the order they
763        are encountered in the object file.  */
764     struct nextfnfield
765       {
766         struct nextfnfield *next;
767         struct fn_field fnfield;
768       }
769      *fnfields;
770
771     /* Member function fieldlist array, contains name of possibly overloaded
772        member function, number of overloaded member functions and a pointer
773        to the head of the member function field chain.  */
774     struct fnfieldlist
775       {
776         char *name;
777         int length;
778         struct nextfnfield *head;
779       }
780      *fnfieldlists;
781
782     /* Number of entries in the fnfieldlists array.  */
783     int nfnfields;
784
785     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
786        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
787     struct typedef_field_list
788       {
789         struct typedef_field field;
790         struct typedef_field_list *next;
791       }
792     *typedef_field_list;
793     unsigned typedef_field_list_count;
794   };
795
796 /* One item on the queue of compilation units to read in full symbols
797    for.  */
798 struct dwarf2_queue_item
799 {
800   struct dwarf2_per_cu_data *per_cu;
801   struct dwarf2_queue_item *next;
802 };
803
804 /* The current queue.  */
805 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
806
807 /* Loaded secondary compilation units are kept in memory until they
808    have not been referenced for the processing of this many
809    compilation units.  Set this to zero to disable caching.  Cache
810    sizes of up to at least twenty will improve startup time for
811    typical inter-CU-reference binaries, at an obvious memory cost.  */
812 static int dwarf2_max_cache_age = 5;
813 static void
814 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
815                            struct cmd_list_element *c, const char *value)
816 {
817   fprintf_filtered (file, _("The upper bound on the age of cached "
818                             "dwarf2 compilation units is %s.\n"),
819                     value);
820 }
821
822
823 /* Various complaints about symbol reading that don't abort the process.  */
824
825 static void
826 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
827 {
828   complaint (&symfile_complaints,
829              _("statement list doesn't fit in .debug_line section"));
830 }
831
832 static void
833 dwarf2_debug_line_missing_file_complaint (void)
834 {
835   complaint (&symfile_complaints,
836              _(".debug_line section has line data without a file"));
837 }
838
839 static void
840 dwarf2_debug_line_missing_end_sequence_complaint (void)
841 {
842   complaint (&symfile_complaints,
843              _(".debug_line section has line "
844                "program sequence without an end"));
845 }
846
847 static void
848 dwarf2_complex_location_expr_complaint (void)
849 {
850   complaint (&symfile_complaints, _("location expression too complex"));
851 }
852
853 static void
854 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
855                                               int arg3)
856 {
857   complaint (&symfile_complaints,
858              _("const value length mismatch for '%s', got %d, expected %d"),
859              arg1, arg2, arg3);
860 }
861
862 static void
863 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
864 {
865   complaint (&symfile_complaints,
866              _("macro info runs off end of `%s' section"),
867              section->asection->name);
868 }
869
870 static void
871 dwarf2_macro_malformed_definition_complaint (const char *arg1)
872 {
873   complaint (&symfile_complaints,
874              _("macro debug info contains a "
875                "malformed macro definition:\n`%s'"),
876              arg1);
877 }
878
879 static void
880 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
881 {
882   complaint (&symfile_complaints,
883              _("invalid attribute class or form for '%s' in '%s'"),
884              arg1, arg2);
885 }
886
887 /* local function prototypes */
888
889 static void dwarf2_locate_sections (bfd *, asection *, void *);
890
891 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
892                                            struct objfile *);
893
894 static void dwarf2_find_base_address (struct die_info *die,
895                                       struct dwarf2_cu *cu);
896
897 static void dwarf2_build_psymtabs_hard (struct objfile *);
898
899 static void scan_partial_symbols (struct partial_die_info *,
900                                   CORE_ADDR *, CORE_ADDR *,
901                                   int, struct dwarf2_cu *);
902
903 static void add_partial_symbol (struct partial_die_info *,
904                                 struct dwarf2_cu *);
905
906 static void add_partial_namespace (struct partial_die_info *pdi,
907                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
908                                    int need_pc, struct dwarf2_cu *cu);
909
910 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
911                                 CORE_ADDR *highpc, int need_pc,
912                                 struct dwarf2_cu *cu);
913
914 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
915                                      struct dwarf2_cu *cu);
916
917 static void add_partial_subprogram (struct partial_die_info *pdi,
918                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
919                                     int need_pc, struct dwarf2_cu *cu);
920
921 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
922                                      gdb_byte *buffer, gdb_byte *info_ptr,
923                                      bfd *abfd, struct dwarf2_cu *cu);
924
925 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
926
927 static void psymtab_to_symtab_1 (struct partial_symtab *);
928
929 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
930
931 static void dwarf2_free_abbrev_table (void *);
932
933 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
934
935 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
936                                             struct dwarf2_cu *);
937
938 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
939                                                  struct dwarf2_cu *);
940
941 static struct partial_die_info *load_partial_dies (bfd *,
942                                                    gdb_byte *, gdb_byte *,
943                                                    int, struct dwarf2_cu *);
944
945 static gdb_byte *read_partial_die (struct partial_die_info *,
946                                    struct abbrev_info *abbrev,
947                                    unsigned int, bfd *,
948                                    gdb_byte *, gdb_byte *,
949                                    struct dwarf2_cu *);
950
951 static struct partial_die_info *find_partial_die (unsigned int,
952                                                   struct dwarf2_cu *);
953
954 static void fixup_partial_die (struct partial_die_info *,
955                                struct dwarf2_cu *);
956
957 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
958                                  bfd *, gdb_byte *, struct dwarf2_cu *);
959
960 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
961                                        bfd *, gdb_byte *, struct dwarf2_cu *);
962
963 static unsigned int read_1_byte (bfd *, gdb_byte *);
964
965 static int read_1_signed_byte (bfd *, gdb_byte *);
966
967 static unsigned int read_2_bytes (bfd *, gdb_byte *);
968
969 static unsigned int read_4_bytes (bfd *, gdb_byte *);
970
971 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
972
973 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
974                                unsigned int *);
975
976 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
977
978 static LONGEST read_checked_initial_length_and_offset
979   (bfd *, gdb_byte *, const struct comp_unit_head *,
980    unsigned int *, unsigned int *);
981
982 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
983                             unsigned int *);
984
985 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
986
987 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
988
989 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
990
991 static char *read_indirect_string (bfd *, gdb_byte *,
992                                    const struct comp_unit_head *,
993                                    unsigned int *);
994
995 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
996
997 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
998
999 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1000
1001 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1002
1003 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1004                                       struct dwarf2_cu *);
1005
1006 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1007                                                 unsigned int,
1008                                                 struct dwarf2_cu *);
1009
1010 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1011                                struct dwarf2_cu *cu);
1012
1013 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1014
1015 static struct die_info *die_specification (struct die_info *die,
1016                                            struct dwarf2_cu **);
1017
1018 static void free_line_header (struct line_header *lh);
1019
1020 static void add_file_name (struct line_header *, char *, unsigned int,
1021                            unsigned int, unsigned int);
1022
1023 static struct line_header *(dwarf_decode_line_header
1024                             (unsigned int offset,
1025                              bfd *abfd, struct dwarf2_cu *cu));
1026
1027 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1028                                 struct dwarf2_cu *, struct partial_symtab *);
1029
1030 static void dwarf2_start_subfile (char *, const char *, const char *);
1031
1032 static struct symbol *new_symbol (struct die_info *, struct type *,
1033                                   struct dwarf2_cu *);
1034
1035 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1036                                        struct dwarf2_cu *, struct symbol *);
1037
1038 static void dwarf2_const_value (struct attribute *, struct symbol *,
1039                                 struct dwarf2_cu *);
1040
1041 static void dwarf2_const_value_attr (struct attribute *attr,
1042                                      struct type *type,
1043                                      const char *name,
1044                                      struct obstack *obstack,
1045                                      struct dwarf2_cu *cu, long *value,
1046                                      gdb_byte **bytes,
1047                                      struct dwarf2_locexpr_baton **baton);
1048
1049 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1050
1051 static int need_gnat_info (struct dwarf2_cu *);
1052
1053 static struct type *die_descriptive_type (struct die_info *,
1054                                           struct dwarf2_cu *);
1055
1056 static void set_descriptive_type (struct type *, struct die_info *,
1057                                   struct dwarf2_cu *);
1058
1059 static struct type *die_containing_type (struct die_info *,
1060                                          struct dwarf2_cu *);
1061
1062 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1063                                      struct dwarf2_cu *);
1064
1065 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1066
1067 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1068
1069 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1070
1071 static char *typename_concat (struct obstack *obs, const char *prefix,
1072                               const char *suffix, int physname,
1073                               struct dwarf2_cu *cu);
1074
1075 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1082
1083 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1084
1085 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1086                                struct dwarf2_cu *, struct partial_symtab *);
1087
1088 static int dwarf2_get_pc_bounds (struct die_info *,
1089                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1090                                  struct partial_symtab *);
1091
1092 static void get_scope_pc_bounds (struct die_info *,
1093                                  CORE_ADDR *, CORE_ADDR *,
1094                                  struct dwarf2_cu *);
1095
1096 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1097                                         CORE_ADDR, struct dwarf2_cu *);
1098
1099 static void dwarf2_add_field (struct field_info *, struct die_info *,
1100                               struct dwarf2_cu *);
1101
1102 static void dwarf2_attach_fields_to_type (struct field_info *,
1103                                           struct type *, struct dwarf2_cu *);
1104
1105 static void dwarf2_add_member_fn (struct field_info *,
1106                                   struct die_info *, struct type *,
1107                                   struct dwarf2_cu *);
1108
1109 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1110                                              struct type *,
1111                                              struct dwarf2_cu *);
1112
1113 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1114
1115 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1116
1117 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1118
1119 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1120
1121 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1122
1123 static struct type *read_module_type (struct die_info *die,
1124                                       struct dwarf2_cu *cu);
1125
1126 static const char *namespace_name (struct die_info *die,
1127                                    int *is_anonymous, struct dwarf2_cu *);
1128
1129 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1130
1131 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1132
1133 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1134                                                        struct dwarf2_cu *);
1135
1136 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1137
1138 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1139                                                  gdb_byte *info_ptr,
1140                                                  gdb_byte **new_info_ptr,
1141                                                  struct die_info *parent);
1142
1143 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1144                                                gdb_byte *info_ptr,
1145                                                gdb_byte **new_info_ptr,
1146                                                struct die_info *parent);
1147
1148 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1149                                                gdb_byte *info_ptr,
1150                                                gdb_byte **new_info_ptr,
1151                                                struct die_info *parent);
1152
1153 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1154                                 struct die_info **, gdb_byte *,
1155                                 int *);
1156
1157 static void process_die (struct die_info *, struct dwarf2_cu *);
1158
1159 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1160                                        struct obstack *);
1161
1162 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1163
1164 static const char *dwarf2_full_name (char *name,
1165                                      struct die_info *die,
1166                                      struct dwarf2_cu *cu);
1167
1168 static struct die_info *dwarf2_extension (struct die_info *die,
1169                                           struct dwarf2_cu **);
1170
1171 static char *dwarf_tag_name (unsigned int);
1172
1173 static char *dwarf_attr_name (unsigned int);
1174
1175 static char *dwarf_form_name (unsigned int);
1176
1177 static char *dwarf_bool_name (unsigned int);
1178
1179 static char *dwarf_type_encoding_name (unsigned int);
1180
1181 #if 0
1182 static char *dwarf_cfi_name (unsigned int);
1183 #endif
1184
1185 static struct die_info *sibling_die (struct die_info *);
1186
1187 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1188
1189 static void dump_die_for_error (struct die_info *);
1190
1191 static void dump_die_1 (struct ui_file *, int level, int max_level,
1192                         struct die_info *);
1193
1194 /*static*/ void dump_die (struct die_info *, int max_level);
1195
1196 static void store_in_ref_table (struct die_info *,
1197                                 struct dwarf2_cu *);
1198
1199 static int is_ref_attr (struct attribute *);
1200
1201 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1202
1203 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1204
1205 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1206                                                struct attribute *,
1207                                                struct dwarf2_cu **);
1208
1209 static struct die_info *follow_die_ref (struct die_info *,
1210                                         struct attribute *,
1211                                         struct dwarf2_cu **);
1212
1213 static struct die_info *follow_die_sig (struct die_info *,
1214                                         struct attribute *,
1215                                         struct dwarf2_cu **);
1216
1217 static struct signatured_type *lookup_signatured_type_at_offset
1218     (struct objfile *objfile,
1219      struct dwarf2_section_info *section,
1220      unsigned int offset);
1221
1222 static void read_signatured_type_at_offset (struct objfile *objfile,
1223                                             struct dwarf2_section_info *sect,
1224                                             unsigned int offset);
1225
1226 static void read_signatured_type (struct signatured_type *type_sig);
1227
1228 /* memory allocation interface */
1229
1230 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1231
1232 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1233
1234 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1235
1236 static void dwarf_decode_macros (struct line_header *, unsigned int,
1237                                  char *, bfd *, struct dwarf2_cu *,
1238                                  struct dwarf2_section_info *,
1239                                  int);
1240
1241 static int attr_form_is_block (struct attribute *);
1242
1243 static int attr_form_is_section_offset (struct attribute *);
1244
1245 static int attr_form_is_constant (struct attribute *);
1246
1247 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1248                                    struct dwarf2_loclist_baton *baton,
1249                                    struct attribute *attr);
1250
1251 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1252                                          struct symbol *sym,
1253                                          struct dwarf2_cu *cu);
1254
1255 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1256                                struct abbrev_info *abbrev,
1257                                struct dwarf2_cu *cu);
1258
1259 static void free_stack_comp_unit (void *);
1260
1261 static hashval_t partial_die_hash (const void *item);
1262
1263 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1264
1265 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1266   (unsigned int offset, struct objfile *objfile);
1267
1268 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1269   (unsigned int offset, struct objfile *objfile);
1270
1271 static void init_one_comp_unit (struct dwarf2_cu *cu,
1272                                 struct dwarf2_per_cu_data *per_cu);
1273
1274 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1275                                    struct die_info *comp_unit_die);
1276
1277 static void free_heap_comp_unit (void *);
1278
1279 static void free_cached_comp_units (void *);
1280
1281 static void age_cached_comp_units (void);
1282
1283 static void free_one_cached_comp_unit (void *);
1284
1285 static struct type *set_die_type (struct die_info *, struct type *,
1286                                   struct dwarf2_cu *);
1287
1288 static void create_all_comp_units (struct objfile *);
1289
1290 static int create_debug_types_hash_table (struct objfile *objfile);
1291
1292 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1293
1294 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1295
1296 static void dwarf2_add_dependence (struct dwarf2_cu *,
1297                                    struct dwarf2_per_cu_data *);
1298
1299 static void dwarf2_mark (struct dwarf2_cu *);
1300
1301 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1302
1303 static struct type *get_die_type_at_offset (unsigned int,
1304                                             struct dwarf2_per_cu_data *per_cu);
1305
1306 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1307
1308 static void dwarf2_release_queue (void *dummy);
1309
1310 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1311
1312 static void process_queue (void);
1313
1314 static void find_file_and_directory (struct die_info *die,
1315                                      struct dwarf2_cu *cu,
1316                                      char **name, char **comp_dir);
1317
1318 static char *file_full_name (int file, struct line_header *lh,
1319                              const char *comp_dir);
1320
1321 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1322                                               gdb_byte *info_ptr,
1323                                               gdb_byte *buffer,
1324                                               unsigned int buffer_size,
1325                                               bfd *abfd,
1326                                               int is_debug_types_section);
1327
1328 static void init_cu_die_reader (struct die_reader_specs *reader,
1329                                 struct dwarf2_cu *cu);
1330
1331 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1332
1333 #if WORDS_BIGENDIAN
1334
1335 /* Convert VALUE between big- and little-endian.  */
1336 static offset_type
1337 byte_swap (offset_type value)
1338 {
1339   offset_type result;
1340
1341   result = (value & 0xff) << 24;
1342   result |= (value & 0xff00) << 8;
1343   result |= (value & 0xff0000) >> 8;
1344   result |= (value & 0xff000000) >> 24;
1345   return result;
1346 }
1347
1348 #define MAYBE_SWAP(V)  byte_swap (V)
1349
1350 #else
1351 #define MAYBE_SWAP(V) (V)
1352 #endif /* WORDS_BIGENDIAN */
1353
1354 /* The suffix for an index file.  */
1355 #define INDEX_SUFFIX ".gdb-index"
1356
1357 static const char *dwarf2_physname (char *name, struct die_info *die,
1358                                     struct dwarf2_cu *cu);
1359
1360 /* Try to locate the sections we need for DWARF 2 debugging
1361    information and return true if we have enough to do something.
1362    NAMES points to the dwarf2 section names, or is NULL if the standard
1363    ELF names are used.  */
1364
1365 int
1366 dwarf2_has_info (struct objfile *objfile,
1367                  const struct dwarf2_debug_sections *names)
1368 {
1369   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1370   if (!dwarf2_per_objfile)
1371     {
1372       /* Initialize per-objfile state.  */
1373       struct dwarf2_per_objfile *data
1374         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1375
1376       memset (data, 0, sizeof (*data));
1377       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1378       dwarf2_per_objfile = data;
1379
1380       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1381                              (void *) names);
1382       dwarf2_per_objfile->objfile = objfile;
1383     }
1384   return (dwarf2_per_objfile->info.asection != NULL
1385           && dwarf2_per_objfile->abbrev.asection != NULL);
1386 }
1387
1388 /* When loading sections, we look either for uncompressed section or for
1389    compressed section names.  */
1390
1391 static int
1392 section_is_p (const char *section_name,
1393               const struct dwarf2_section_names *names)
1394 {
1395   if (names->normal != NULL
1396       && strcmp (section_name, names->normal) == 0)
1397     return 1;
1398   if (names->compressed != NULL
1399       && strcmp (section_name, names->compressed) == 0)
1400     return 1;
1401   return 0;
1402 }
1403
1404 /* This function is mapped across the sections and remembers the
1405    offset and size of each of the debugging sections we are interested
1406    in.  */
1407
1408 static void
1409 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1410 {
1411   const struct dwarf2_debug_sections *names;
1412
1413   if (vnames == NULL)
1414     names = &dwarf2_elf_names;
1415   else
1416     names = (const struct dwarf2_debug_sections *) vnames;
1417
1418   if (section_is_p (sectp->name, &names->info))
1419     {
1420       dwarf2_per_objfile->info.asection = sectp;
1421       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1422     }
1423   else if (section_is_p (sectp->name, &names->abbrev))
1424     {
1425       dwarf2_per_objfile->abbrev.asection = sectp;
1426       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1427     }
1428   else if (section_is_p (sectp->name, &names->line))
1429     {
1430       dwarf2_per_objfile->line.asection = sectp;
1431       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1432     }
1433   else if (section_is_p (sectp->name, &names->loc))
1434     {
1435       dwarf2_per_objfile->loc.asection = sectp;
1436       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1437     }
1438   else if (section_is_p (sectp->name, &names->macinfo))
1439     {
1440       dwarf2_per_objfile->macinfo.asection = sectp;
1441       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1442     }
1443   else if (section_is_p (sectp->name, &names->macro))
1444     {
1445       dwarf2_per_objfile->macro.asection = sectp;
1446       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1447     }
1448   else if (section_is_p (sectp->name, &names->str))
1449     {
1450       dwarf2_per_objfile->str.asection = sectp;
1451       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1452     }
1453   else if (section_is_p (sectp->name, &names->frame))
1454     {
1455       dwarf2_per_objfile->frame.asection = sectp;
1456       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1457     }
1458   else if (section_is_p (sectp->name, &names->eh_frame))
1459     {
1460       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1461
1462       if (aflag & SEC_HAS_CONTENTS)
1463         {
1464           dwarf2_per_objfile->eh_frame.asection = sectp;
1465           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1466         }
1467     }
1468   else if (section_is_p (sectp->name, &names->ranges))
1469     {
1470       dwarf2_per_objfile->ranges.asection = sectp;
1471       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1472     }
1473   else if (section_is_p (sectp->name, &names->types))
1474     {
1475       struct dwarf2_section_info type_section;
1476
1477       memset (&type_section, 0, sizeof (type_section));
1478       type_section.asection = sectp;
1479       type_section.size = bfd_get_section_size (sectp);
1480
1481       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1482                      &type_section);
1483     }
1484   else if (section_is_p (sectp->name, &names->gdb_index))
1485     {
1486       dwarf2_per_objfile->gdb_index.asection = sectp;
1487       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1488     }
1489
1490   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1491       && bfd_section_vma (abfd, sectp) == 0)
1492     dwarf2_per_objfile->has_section_at_zero = 1;
1493 }
1494
1495 /* Decompress a section that was compressed using zlib.  Store the
1496    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1497
1498 static void
1499 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1500                          gdb_byte **outbuf, bfd_size_type *outsize)
1501 {
1502   bfd *abfd = objfile->obfd;
1503 #ifndef HAVE_ZLIB_H
1504   error (_("Support for zlib-compressed DWARF data (from '%s') "
1505            "is disabled in this copy of GDB"),
1506          bfd_get_filename (abfd));
1507 #else
1508   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1509   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1510   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1511   bfd_size_type uncompressed_size;
1512   gdb_byte *uncompressed_buffer;
1513   z_stream strm;
1514   int rc;
1515   int header_size = 12;
1516
1517   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1518       || bfd_bread (compressed_buffer,
1519                     compressed_size, abfd) != compressed_size)
1520     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1521            bfd_get_filename (abfd));
1522
1523   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1524      by the uncompressed section size, 8 bytes in big-endian order.  */
1525   if (compressed_size < header_size
1526       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1527     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1528            bfd_get_filename (abfd));
1529   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1530   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1531   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1532   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1533   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1534   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1535   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1536   uncompressed_size += compressed_buffer[11];
1537
1538   /* It is possible the section consists of several compressed
1539      buffers concatenated together, so we uncompress in a loop.  */
1540   strm.zalloc = NULL;
1541   strm.zfree = NULL;
1542   strm.opaque = NULL;
1543   strm.avail_in = compressed_size - header_size;
1544   strm.next_in = (Bytef*) compressed_buffer + header_size;
1545   strm.avail_out = uncompressed_size;
1546   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1547                                        uncompressed_size);
1548   rc = inflateInit (&strm);
1549   while (strm.avail_in > 0)
1550     {
1551       if (rc != Z_OK)
1552         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1553                bfd_get_filename (abfd), rc);
1554       strm.next_out = ((Bytef*) uncompressed_buffer
1555                        + (uncompressed_size - strm.avail_out));
1556       rc = inflate (&strm, Z_FINISH);
1557       if (rc != Z_STREAM_END)
1558         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1559                bfd_get_filename (abfd), rc);
1560       rc = inflateReset (&strm);
1561     }
1562   rc = inflateEnd (&strm);
1563   if (rc != Z_OK
1564       || strm.avail_out != 0)
1565     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1566            bfd_get_filename (abfd), rc);
1567
1568   do_cleanups (cleanup);
1569   *outbuf = uncompressed_buffer;
1570   *outsize = uncompressed_size;
1571 #endif
1572 }
1573
1574 /* A helper function that decides whether a section is empty.  */
1575
1576 static int
1577 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1578 {
1579   return info->asection == NULL || info->size == 0;
1580 }
1581
1582 /* Read the contents of the section INFO from object file specified by
1583    OBJFILE, store info about the section into INFO.
1584    If the section is compressed, uncompress it before returning.  */
1585
1586 static void
1587 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1588 {
1589   bfd *abfd = objfile->obfd;
1590   asection *sectp = info->asection;
1591   gdb_byte *buf, *retbuf;
1592   unsigned char header[4];
1593
1594   if (info->readin)
1595     return;
1596   info->buffer = NULL;
1597   info->map_addr = NULL;
1598   info->readin = 1;
1599
1600   if (dwarf2_section_empty_p (info))
1601     return;
1602
1603   /* Check if the file has a 4-byte header indicating compression.  */
1604   if (info->size > sizeof (header)
1605       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1606       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1607     {
1608       /* Upon decompression, update the buffer and its size.  */
1609       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1610         {
1611           zlib_decompress_section (objfile, sectp, &info->buffer,
1612                                    &info->size);
1613           return;
1614         }
1615     }
1616
1617 #ifdef HAVE_MMAP
1618   if (pagesize == 0)
1619     pagesize = getpagesize ();
1620
1621   /* Only try to mmap sections which are large enough: we don't want to
1622      waste space due to fragmentation.  Also, only try mmap for sections
1623      without relocations.  */
1624
1625   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1626     {
1627       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1628                          MAP_PRIVATE, sectp->filepos,
1629                          &info->map_addr, &info->map_len);
1630
1631       if ((caddr_t)info->buffer != MAP_FAILED)
1632         {
1633 #if HAVE_POSIX_MADVISE
1634           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1635 #endif
1636           return;
1637         }
1638     }
1639 #endif
1640
1641   /* If we get here, we are a normal, not-compressed section.  */
1642   info->buffer = buf
1643     = obstack_alloc (&objfile->objfile_obstack, info->size);
1644
1645   /* When debugging .o files, we may need to apply relocations; see
1646      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1647      We never compress sections in .o files, so we only need to
1648      try this when the section is not compressed.  */
1649   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1650   if (retbuf != NULL)
1651     {
1652       info->buffer = retbuf;
1653       return;
1654     }
1655
1656   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1657       || bfd_bread (buf, info->size, abfd) != info->size)
1658     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1659            bfd_get_filename (abfd));
1660 }
1661
1662 /* A helper function that returns the size of a section in a safe way.
1663    If you are positive that the section has been read before using the
1664    size, then it is safe to refer to the dwarf2_section_info object's
1665    "size" field directly.  In other cases, you must call this
1666    function, because for compressed sections the size field is not set
1667    correctly until the section has been read.  */
1668
1669 static bfd_size_type
1670 dwarf2_section_size (struct objfile *objfile,
1671                      struct dwarf2_section_info *info)
1672 {
1673   if (!info->readin)
1674     dwarf2_read_section (objfile, info);
1675   return info->size;
1676 }
1677
1678 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1679    SECTION_NAME.  */
1680
1681 void
1682 dwarf2_get_section_info (struct objfile *objfile,
1683                          enum dwarf2_section_enum sect,
1684                          asection **sectp, gdb_byte **bufp,
1685                          bfd_size_type *sizep)
1686 {
1687   struct dwarf2_per_objfile *data
1688     = objfile_data (objfile, dwarf2_objfile_data_key);
1689   struct dwarf2_section_info *info;
1690
1691   /* We may see an objfile without any DWARF, in which case we just
1692      return nothing.  */
1693   if (data == NULL)
1694     {
1695       *sectp = NULL;
1696       *bufp = NULL;
1697       *sizep = 0;
1698       return;
1699     }
1700   switch (sect)
1701     {
1702     case DWARF2_DEBUG_FRAME:
1703       info = &data->frame;
1704       break;
1705     case DWARF2_EH_FRAME:
1706       info = &data->eh_frame;
1707       break;
1708     default:
1709       gdb_assert_not_reached ("unexpected section");
1710     }
1711
1712   dwarf2_read_section (objfile, info);
1713
1714   *sectp = info->asection;
1715   *bufp = info->buffer;
1716   *sizep = info->size;
1717 }
1718
1719 \f
1720 /* DWARF quick_symbols_functions support.  */
1721
1722 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1723    unique line tables, so we maintain a separate table of all .debug_line
1724    derived entries to support the sharing.
1725    All the quick functions need is the list of file names.  We discard the
1726    line_header when we're done and don't need to record it here.  */
1727 struct quick_file_names
1728 {
1729   /* The offset in .debug_line of the line table.  We hash on this.  */
1730   unsigned int offset;
1731
1732   /* The number of entries in file_names, real_names.  */
1733   unsigned int num_file_names;
1734
1735   /* The file names from the line table, after being run through
1736      file_full_name.  */
1737   const char **file_names;
1738
1739   /* The file names from the line table after being run through
1740      gdb_realpath.  These are computed lazily.  */
1741   const char **real_names;
1742 };
1743
1744 /* When using the index (and thus not using psymtabs), each CU has an
1745    object of this type.  This is used to hold information needed by
1746    the various "quick" methods.  */
1747 struct dwarf2_per_cu_quick_data
1748 {
1749   /* The file table.  This can be NULL if there was no file table
1750      or it's currently not read in.
1751      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1752   struct quick_file_names *file_names;
1753
1754   /* The corresponding symbol table.  This is NULL if symbols for this
1755      CU have not yet been read.  */
1756   struct symtab *symtab;
1757
1758   /* A temporary mark bit used when iterating over all CUs in
1759      expand_symtabs_matching.  */
1760   unsigned int mark : 1;
1761
1762   /* True if we've tried to read the file table and found there isn't one.
1763      There will be no point in trying to read it again next time.  */
1764   unsigned int no_file_data : 1;
1765 };
1766
1767 /* Hash function for a quick_file_names.  */
1768
1769 static hashval_t
1770 hash_file_name_entry (const void *e)
1771 {
1772   const struct quick_file_names *file_data = e;
1773
1774   return file_data->offset;
1775 }
1776
1777 /* Equality function for a quick_file_names.  */
1778
1779 static int
1780 eq_file_name_entry (const void *a, const void *b)
1781 {
1782   const struct quick_file_names *ea = a;
1783   const struct quick_file_names *eb = b;
1784
1785   return ea->offset == eb->offset;
1786 }
1787
1788 /* Delete function for a quick_file_names.  */
1789
1790 static void
1791 delete_file_name_entry (void *e)
1792 {
1793   struct quick_file_names *file_data = e;
1794   int i;
1795
1796   for (i = 0; i < file_data->num_file_names; ++i)
1797     {
1798       xfree ((void*) file_data->file_names[i]);
1799       if (file_data->real_names)
1800         xfree ((void*) file_data->real_names[i]);
1801     }
1802
1803   /* The space for the struct itself lives on objfile_obstack,
1804      so we don't free it here.  */
1805 }
1806
1807 /* Create a quick_file_names hash table.  */
1808
1809 static htab_t
1810 create_quick_file_names_table (unsigned int nr_initial_entries)
1811 {
1812   return htab_create_alloc (nr_initial_entries,
1813                             hash_file_name_entry, eq_file_name_entry,
1814                             delete_file_name_entry, xcalloc, xfree);
1815 }
1816
1817 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1818    have to be created afterwards.  You should call age_cached_comp_units after
1819    processing PER_CU->CU.  dw2_setup must have been already called.  */
1820
1821 static void
1822 load_cu (struct dwarf2_per_cu_data *per_cu)
1823 {
1824   if (per_cu->debug_types_section)
1825     read_signatured_type_at_offset (per_cu->objfile,
1826                                     per_cu->debug_types_section,
1827                                     per_cu->offset);
1828   else
1829     load_full_comp_unit (per_cu);
1830
1831   gdb_assert (per_cu->cu != NULL);
1832
1833   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1834 }
1835
1836 /* Read in the symbols for PER_CU.  */
1837
1838 static void
1839 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1840 {
1841   struct cleanup *back_to;
1842
1843   back_to = make_cleanup (dwarf2_release_queue, NULL);
1844
1845   queue_comp_unit (per_cu);
1846
1847   load_cu (per_cu);
1848
1849   process_queue ();
1850
1851   /* Age the cache, releasing compilation units that have not
1852      been used recently.  */
1853   age_cached_comp_units ();
1854
1855   do_cleanups (back_to);
1856 }
1857
1858 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1859    the objfile from which this CU came.  Returns the resulting symbol
1860    table.  */
1861
1862 static struct symtab *
1863 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1864 {
1865   if (!per_cu->v.quick->symtab)
1866     {
1867       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1868       increment_reading_symtab ();
1869       dw2_do_instantiate_symtab (per_cu);
1870       do_cleanups (back_to);
1871     }
1872   return per_cu->v.quick->symtab;
1873 }
1874
1875 /* Return the CU given its index.  */
1876
1877 static struct dwarf2_per_cu_data *
1878 dw2_get_cu (int index)
1879 {
1880   if (index >= dwarf2_per_objfile->n_comp_units)
1881     {
1882       index -= dwarf2_per_objfile->n_comp_units;
1883       return dwarf2_per_objfile->type_comp_units[index];
1884     }
1885   return dwarf2_per_objfile->all_comp_units[index];
1886 }
1887
1888 /* A helper function that knows how to read a 64-bit value in a way
1889    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1890    otherwise.  */
1891
1892 static int
1893 extract_cu_value (const char *bytes, ULONGEST *result)
1894 {
1895   if (sizeof (ULONGEST) < 8)
1896     {
1897       int i;
1898
1899       /* Ignore the upper 4 bytes if they are all zero.  */
1900       for (i = 0; i < 4; ++i)
1901         if (bytes[i + 4] != 0)
1902           return 0;
1903
1904       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1905     }
1906   else
1907     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1908   return 1;
1909 }
1910
1911 /* Read the CU list from the mapped index, and use it to create all
1912    the CU objects for this objfile.  Return 0 if something went wrong,
1913    1 if everything went ok.  */
1914
1915 static int
1916 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1917                        offset_type cu_list_elements)
1918 {
1919   offset_type i;
1920
1921   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1922   dwarf2_per_objfile->all_comp_units
1923     = obstack_alloc (&objfile->objfile_obstack,
1924                      dwarf2_per_objfile->n_comp_units
1925                      * sizeof (struct dwarf2_per_cu_data *));
1926
1927   for (i = 0; i < cu_list_elements; i += 2)
1928     {
1929       struct dwarf2_per_cu_data *the_cu;
1930       ULONGEST offset, length;
1931
1932       if (!extract_cu_value (cu_list, &offset)
1933           || !extract_cu_value (cu_list + 8, &length))
1934         return 0;
1935       cu_list += 2 * 8;
1936
1937       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1938                                struct dwarf2_per_cu_data);
1939       the_cu->offset = offset;
1940       the_cu->length = length;
1941       the_cu->objfile = objfile;
1942       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1943                                         struct dwarf2_per_cu_quick_data);
1944       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1945     }
1946
1947   return 1;
1948 }
1949
1950 /* Create the signatured type hash table from the index.  */
1951
1952 static int
1953 create_signatured_type_table_from_index (struct objfile *objfile,
1954                                          struct dwarf2_section_info *section,
1955                                          const gdb_byte *bytes,
1956                                          offset_type elements)
1957 {
1958   offset_type i;
1959   htab_t sig_types_hash;
1960
1961   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1962   dwarf2_per_objfile->type_comp_units
1963     = obstack_alloc (&objfile->objfile_obstack,
1964                      dwarf2_per_objfile->n_type_comp_units
1965                      * sizeof (struct dwarf2_per_cu_data *));
1966
1967   sig_types_hash = allocate_signatured_type_table (objfile);
1968
1969   for (i = 0; i < elements; i += 3)
1970     {
1971       struct signatured_type *type_sig;
1972       ULONGEST offset, type_offset, signature;
1973       void **slot;
1974
1975       if (!extract_cu_value (bytes, &offset)
1976           || !extract_cu_value (bytes + 8, &type_offset))
1977         return 0;
1978       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1979       bytes += 3 * 8;
1980
1981       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1982                                  struct signatured_type);
1983       type_sig->signature = signature;
1984       type_sig->type_offset = type_offset;
1985       type_sig->per_cu.debug_types_section = section;
1986       type_sig->per_cu.offset = offset;
1987       type_sig->per_cu.objfile = objfile;
1988       type_sig->per_cu.v.quick
1989         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1990                           struct dwarf2_per_cu_quick_data);
1991
1992       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1993       *slot = type_sig;
1994
1995       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1996     }
1997
1998   dwarf2_per_objfile->signatured_types = sig_types_hash;
1999
2000   return 1;
2001 }
2002
2003 /* Read the address map data from the mapped index, and use it to
2004    populate the objfile's psymtabs_addrmap.  */
2005
2006 static void
2007 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2008 {
2009   const gdb_byte *iter, *end;
2010   struct obstack temp_obstack;
2011   struct addrmap *mutable_map;
2012   struct cleanup *cleanup;
2013   CORE_ADDR baseaddr;
2014
2015   obstack_init (&temp_obstack);
2016   cleanup = make_cleanup_obstack_free (&temp_obstack);
2017   mutable_map = addrmap_create_mutable (&temp_obstack);
2018
2019   iter = index->address_table;
2020   end = iter + index->address_table_size;
2021
2022   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2023
2024   while (iter < end)
2025     {
2026       ULONGEST hi, lo, cu_index;
2027       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2028       iter += 8;
2029       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2030       iter += 8;
2031       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2032       iter += 4;
2033       
2034       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2035                          dw2_get_cu (cu_index));
2036     }
2037
2038   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2039                                                     &objfile->objfile_obstack);
2040   do_cleanups (cleanup);
2041 }
2042
2043 /* The hash function for strings in the mapped index.  This is the same as
2044    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2045    implementation.  This is necessary because the hash function is tied to the
2046    format of the mapped index file.  The hash values do not have to match with
2047    SYMBOL_HASH_NEXT.
2048    
2049    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2050
2051 static hashval_t
2052 mapped_index_string_hash (int index_version, const void *p)
2053 {
2054   const unsigned char *str = (const unsigned char *) p;
2055   hashval_t r = 0;
2056   unsigned char c;
2057
2058   while ((c = *str++) != 0)
2059     {
2060       if (index_version >= 5)
2061         c = tolower (c);
2062       r = r * 67 + c - 113;
2063     }
2064
2065   return r;
2066 }
2067
2068 /* Find a slot in the mapped index INDEX for the object named NAME.
2069    If NAME is found, set *VEC_OUT to point to the CU vector in the
2070    constant pool and return 1.  If NAME cannot be found, return 0.  */
2071
2072 static int
2073 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2074                           offset_type **vec_out)
2075 {
2076   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2077   offset_type hash;
2078   offset_type slot, step;
2079   int (*cmp) (const char *, const char *);
2080
2081   if (current_language->la_language == language_cplus
2082       || current_language->la_language == language_java
2083       || current_language->la_language == language_fortran)
2084     {
2085       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2086          not contain any.  */
2087       const char *paren = strchr (name, '(');
2088
2089       if (paren)
2090         {
2091           char *dup;
2092
2093           dup = xmalloc (paren - name + 1);
2094           memcpy (dup, name, paren - name);
2095           dup[paren - name] = 0;
2096
2097           make_cleanup (xfree, dup);
2098           name = dup;
2099         }
2100     }
2101
2102   /* Index version 4 did not support case insensitive searches.  But the
2103      indexes for case insensitive languages are built in lowercase, therefore
2104      simulate our NAME being searched is also lowercased.  */
2105   hash = mapped_index_string_hash ((index->version == 4
2106                                     && case_sensitivity == case_sensitive_off
2107                                     ? 5 : index->version),
2108                                    name);
2109
2110   slot = hash & (index->symbol_table_slots - 1);
2111   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2112   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2113
2114   for (;;)
2115     {
2116       /* Convert a slot number to an offset into the table.  */
2117       offset_type i = 2 * slot;
2118       const char *str;
2119       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2120         {
2121           do_cleanups (back_to);
2122           return 0;
2123         }
2124
2125       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2126       if (!cmp (name, str))
2127         {
2128           *vec_out = (offset_type *) (index->constant_pool
2129                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2130           do_cleanups (back_to);
2131           return 1;
2132         }
2133
2134       slot = (slot + step) & (index->symbol_table_slots - 1);
2135     }
2136 }
2137
2138 /* Read the index file.  If everything went ok, initialize the "quick"
2139    elements of all the CUs and return 1.  Otherwise, return 0.  */
2140
2141 static int
2142 dwarf2_read_index (struct objfile *objfile)
2143 {
2144   char *addr;
2145   struct mapped_index *map;
2146   offset_type *metadata;
2147   const gdb_byte *cu_list;
2148   const gdb_byte *types_list = NULL;
2149   offset_type version, cu_list_elements;
2150   offset_type types_list_elements = 0;
2151   int i;
2152
2153   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2154     return 0;
2155
2156   /* Older elfutils strip versions could keep the section in the main
2157      executable while splitting it for the separate debug info file.  */
2158   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2159        & SEC_HAS_CONTENTS) == 0)
2160     return 0;
2161
2162   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2163
2164   addr = dwarf2_per_objfile->gdb_index.buffer;
2165   /* Version check.  */
2166   version = MAYBE_SWAP (*(offset_type *) addr);
2167   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2168      causes the index to behave very poorly for certain requests.  Version 3
2169      contained incomplete addrmap.  So, it seems better to just ignore such
2170      indices.  Index version 4 uses a different hash function than index
2171      version 5 and later.  */
2172   if (version < 4)
2173     return 0;
2174   /* Indexes with higher version than the one supported by GDB may be no
2175      longer backward compatible.  */
2176   if (version > 5)
2177     return 0;
2178
2179   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2180   map->version = version;
2181   map->total_size = dwarf2_per_objfile->gdb_index.size;
2182
2183   metadata = (offset_type *) (addr + sizeof (offset_type));
2184
2185   i = 0;
2186   cu_list = addr + MAYBE_SWAP (metadata[i]);
2187   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2188                       / 8);
2189   ++i;
2190
2191   types_list = addr + MAYBE_SWAP (metadata[i]);
2192   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2193                           - MAYBE_SWAP (metadata[i]))
2194                          / 8);
2195   ++i;
2196
2197   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2198   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2199                              - MAYBE_SWAP (metadata[i]));
2200   ++i;
2201
2202   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2203   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2204                               - MAYBE_SWAP (metadata[i]))
2205                              / (2 * sizeof (offset_type)));
2206   ++i;
2207
2208   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2209
2210   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2211     return 0;
2212
2213   if (types_list_elements)
2214     {
2215       struct dwarf2_section_info *section;
2216
2217       /* We can only handle a single .debug_types when we have an
2218          index.  */
2219       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2220         return 0;
2221
2222       section = VEC_index (dwarf2_section_info_def,
2223                            dwarf2_per_objfile->types, 0);
2224
2225       if (!create_signatured_type_table_from_index (objfile, section,
2226                                                     types_list,
2227                                                     types_list_elements))
2228         return 0;
2229     }
2230
2231   create_addrmap_from_index (objfile, map);
2232
2233   dwarf2_per_objfile->index_table = map;
2234   dwarf2_per_objfile->using_index = 1;
2235   dwarf2_per_objfile->quick_file_names_table =
2236     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2237
2238   return 1;
2239 }
2240
2241 /* A helper for the "quick" functions which sets the global
2242    dwarf2_per_objfile according to OBJFILE.  */
2243
2244 static void
2245 dw2_setup (struct objfile *objfile)
2246 {
2247   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2248   gdb_assert (dwarf2_per_objfile);
2249 }
2250
2251 /* A helper for the "quick" functions which attempts to read the line
2252    table for THIS_CU.  */
2253
2254 static struct quick_file_names *
2255 dw2_get_file_names (struct objfile *objfile,
2256                     struct dwarf2_per_cu_data *this_cu)
2257 {
2258   bfd *abfd = objfile->obfd;
2259   struct line_header *lh;
2260   struct attribute *attr;
2261   struct cleanup *cleanups;
2262   struct die_info *comp_unit_die;
2263   struct dwarf2_section_info* sec;
2264   gdb_byte *info_ptr, *buffer;
2265   int has_children, i;
2266   struct dwarf2_cu cu;
2267   unsigned int bytes_read, buffer_size;
2268   struct die_reader_specs reader_specs;
2269   char *name, *comp_dir;
2270   void **slot;
2271   struct quick_file_names *qfn;
2272   unsigned int line_offset;
2273
2274   if (this_cu->v.quick->file_names != NULL)
2275     return this_cu->v.quick->file_names;
2276   /* If we know there is no line data, no point in looking again.  */
2277   if (this_cu->v.quick->no_file_data)
2278     return NULL;
2279
2280   init_one_comp_unit (&cu, this_cu);
2281   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2282
2283   if (this_cu->debug_types_section)
2284     sec = this_cu->debug_types_section;
2285   else
2286     sec = &dwarf2_per_objfile->info;
2287   dwarf2_read_section (objfile, sec);
2288   buffer_size = sec->size;
2289   buffer = sec->buffer;
2290   info_ptr = buffer + this_cu->offset;
2291
2292   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2293                                           buffer, buffer_size,
2294                                           abfd,
2295                                           this_cu->debug_types_section != NULL);
2296
2297   /* Skip dummy compilation units.  */
2298   if (info_ptr >= buffer + buffer_size
2299       || peek_abbrev_code (abfd, info_ptr) == 0)
2300     {
2301       do_cleanups (cleanups);
2302       return NULL;
2303     }
2304
2305   dwarf2_read_abbrevs (abfd, &cu);
2306   make_cleanup (dwarf2_free_abbrev_table, &cu);
2307
2308   init_cu_die_reader (&reader_specs, &cu);
2309   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2310                  &has_children);
2311
2312   lh = NULL;
2313   slot = NULL;
2314   line_offset = 0;
2315   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2316   if (attr)
2317     {
2318       struct quick_file_names find_entry;
2319
2320       line_offset = DW_UNSND (attr);
2321
2322       /* We may have already read in this line header (TU line header sharing).
2323          If we have we're done.  */
2324       find_entry.offset = line_offset;
2325       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2326                              &find_entry, INSERT);
2327       if (*slot != NULL)
2328         {
2329           do_cleanups (cleanups);
2330           this_cu->v.quick->file_names = *slot;
2331           return *slot;
2332         }
2333
2334       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2335     }
2336   if (lh == NULL)
2337     {
2338       do_cleanups (cleanups);
2339       this_cu->v.quick->no_file_data = 1;
2340       return NULL;
2341     }
2342
2343   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2344   qfn->offset = line_offset;
2345   gdb_assert (slot != NULL);
2346   *slot = qfn;
2347
2348   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2349
2350   qfn->num_file_names = lh->num_file_names;
2351   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2352                                    lh->num_file_names * sizeof (char *));
2353   for (i = 0; i < lh->num_file_names; ++i)
2354     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2355   qfn->real_names = NULL;
2356
2357   free_line_header (lh);
2358   do_cleanups (cleanups);
2359
2360   this_cu->v.quick->file_names = qfn;
2361   return qfn;
2362 }
2363
2364 /* A helper for the "quick" functions which computes and caches the
2365    real path for a given file name from the line table.  */
2366
2367 static const char *
2368 dw2_get_real_path (struct objfile *objfile,
2369                    struct quick_file_names *qfn, int index)
2370 {
2371   if (qfn->real_names == NULL)
2372     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2373                                       qfn->num_file_names, sizeof (char *));
2374
2375   if (qfn->real_names[index] == NULL)
2376     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2377
2378   return qfn->real_names[index];
2379 }
2380
2381 static struct symtab *
2382 dw2_find_last_source_symtab (struct objfile *objfile)
2383 {
2384   int index;
2385
2386   dw2_setup (objfile);
2387   index = dwarf2_per_objfile->n_comp_units - 1;
2388   return dw2_instantiate_symtab (dw2_get_cu (index));
2389 }
2390
2391 /* Traversal function for dw2_forget_cached_source_info.  */
2392
2393 static int
2394 dw2_free_cached_file_names (void **slot, void *info)
2395 {
2396   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2397
2398   if (file_data->real_names)
2399     {
2400       int i;
2401
2402       for (i = 0; i < file_data->num_file_names; ++i)
2403         {
2404           xfree ((void*) file_data->real_names[i]);
2405           file_data->real_names[i] = NULL;
2406         }
2407     }
2408
2409   return 1;
2410 }
2411
2412 static void
2413 dw2_forget_cached_source_info (struct objfile *objfile)
2414 {
2415   dw2_setup (objfile);
2416
2417   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2418                           dw2_free_cached_file_names, NULL);
2419 }
2420
2421 /* Helper function for dw2_map_symtabs_matching_filename that expands
2422    the symtabs and calls the iterator.  */
2423
2424 static int
2425 dw2_map_expand_apply (struct objfile *objfile,
2426                       struct dwarf2_per_cu_data *per_cu,
2427                       const char *name,
2428                       const char *full_path, const char *real_path,
2429                       int (*callback) (struct symtab *, void *),
2430                       void *data)
2431 {
2432   struct symtab *last_made = objfile->symtabs;
2433
2434   /* Don't visit already-expanded CUs.  */
2435   if (per_cu->v.quick->symtab)
2436     return 0;
2437
2438   /* This may expand more than one symtab, and we want to iterate over
2439      all of them.  */
2440   dw2_instantiate_symtab (per_cu);
2441
2442   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2443                                     objfile->symtabs, last_made);
2444 }
2445
2446 /* Implementation of the map_symtabs_matching_filename method.  */
2447
2448 static int
2449 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2450                                    const char *full_path, const char *real_path,
2451                                    int (*callback) (struct symtab *, void *),
2452                                    void *data)
2453 {
2454   int i;
2455   const char *name_basename = lbasename (name);
2456   int check_basename = name_basename == name;
2457   struct dwarf2_per_cu_data *base_cu = NULL;
2458
2459   dw2_setup (objfile);
2460
2461   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2462                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2463     {
2464       int j;
2465       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2466       struct quick_file_names *file_data;
2467
2468       /* We only need to look at symtabs not already expanded.  */
2469       if (per_cu->v.quick->symtab)
2470         continue;
2471
2472       file_data = dw2_get_file_names (objfile, per_cu);
2473       if (file_data == NULL)
2474         continue;
2475
2476       for (j = 0; j < file_data->num_file_names; ++j)
2477         {
2478           const char *this_name = file_data->file_names[j];
2479
2480           if (FILENAME_CMP (name, this_name) == 0)
2481             {
2482               if (dw2_map_expand_apply (objfile, per_cu,
2483                                         name, full_path, real_path,
2484                                         callback, data))
2485                 return 1;
2486             }
2487
2488           if (check_basename && ! base_cu
2489               && FILENAME_CMP (lbasename (this_name), name) == 0)
2490             base_cu = per_cu;
2491
2492           /* Before we invoke realpath, which can get expensive when many
2493              files are involved, do a quick comparison of the basenames.  */
2494           if (! basenames_may_differ
2495               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2496             continue;
2497
2498           if (full_path != NULL)
2499             {
2500               const char *this_real_name = dw2_get_real_path (objfile,
2501                                                               file_data, j);
2502
2503               if (this_real_name != NULL
2504                   && FILENAME_CMP (full_path, this_real_name) == 0)
2505                 {
2506                   if (dw2_map_expand_apply (objfile, per_cu,
2507                                             name, full_path, real_path,
2508                                             callback, data))
2509                     return 1;
2510                 }
2511             }
2512
2513           if (real_path != NULL)
2514             {
2515               const char *this_real_name = dw2_get_real_path (objfile,
2516                                                               file_data, j);
2517
2518               if (this_real_name != NULL
2519                   && FILENAME_CMP (real_path, this_real_name) == 0)
2520                 {
2521                   if (dw2_map_expand_apply (objfile, per_cu,
2522                                             name, full_path, real_path,
2523                                             callback, data))
2524                     return 1;
2525                 }
2526             }
2527         }
2528     }
2529
2530   if (base_cu)
2531     {
2532       if (dw2_map_expand_apply (objfile, base_cu,
2533                                 name, full_path, real_path,
2534                                 callback, data))
2535         return 1;
2536     }
2537
2538   return 0;
2539 }
2540
2541 static struct symtab *
2542 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2543                    const char *name, domain_enum domain)
2544 {
2545   /* We do all the work in the pre_expand_symtabs_matching hook
2546      instead.  */
2547   return NULL;
2548 }
2549
2550 /* A helper function that expands all symtabs that hold an object
2551    named NAME.  */
2552
2553 static void
2554 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2555 {
2556   dw2_setup (objfile);
2557
2558   /* index_table is NULL if OBJF_READNOW.  */
2559   if (dwarf2_per_objfile->index_table)
2560     {
2561       offset_type *vec;
2562
2563       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2564                                     name, &vec))
2565         {
2566           offset_type i, len = MAYBE_SWAP (*vec);
2567           for (i = 0; i < len; ++i)
2568             {
2569               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2570               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2571
2572               dw2_instantiate_symtab (per_cu);
2573             }
2574         }
2575     }
2576 }
2577
2578 static void
2579 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2580                                  enum block_enum block_kind, const char *name,
2581                                  domain_enum domain)
2582 {
2583   dw2_do_expand_symtabs_matching (objfile, name);
2584 }
2585
2586 static void
2587 dw2_print_stats (struct objfile *objfile)
2588 {
2589   int i, count;
2590
2591   dw2_setup (objfile);
2592   count = 0;
2593   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2594                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2595     {
2596       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2597
2598       if (!per_cu->v.quick->symtab)
2599         ++count;
2600     }
2601   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2602 }
2603
2604 static void
2605 dw2_dump (struct objfile *objfile)
2606 {
2607   /* Nothing worth printing.  */
2608 }
2609
2610 static void
2611 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2612               struct section_offsets *delta)
2613 {
2614   /* There's nothing to relocate here.  */
2615 }
2616
2617 static void
2618 dw2_expand_symtabs_for_function (struct objfile *objfile,
2619                                  const char *func_name)
2620 {
2621   dw2_do_expand_symtabs_matching (objfile, func_name);
2622 }
2623
2624 static void
2625 dw2_expand_all_symtabs (struct objfile *objfile)
2626 {
2627   int i;
2628
2629   dw2_setup (objfile);
2630
2631   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2632                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2633     {
2634       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2635
2636       dw2_instantiate_symtab (per_cu);
2637     }
2638 }
2639
2640 static void
2641 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2642                                   const char *filename)
2643 {
2644   int i;
2645
2646   dw2_setup (objfile);
2647
2648   /* We don't need to consider type units here.
2649      This is only called for examining code, e.g. expand_line_sal.
2650      There can be an order of magnitude (or more) more type units
2651      than comp units, and we avoid them if we can.  */
2652
2653   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2654     {
2655       int j;
2656       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2657       struct quick_file_names *file_data;
2658
2659       /* We only need to look at symtabs not already expanded.  */
2660       if (per_cu->v.quick->symtab)
2661         continue;
2662
2663       file_data = dw2_get_file_names (objfile, per_cu);
2664       if (file_data == NULL)
2665         continue;
2666
2667       for (j = 0; j < file_data->num_file_names; ++j)
2668         {
2669           const char *this_name = file_data->file_names[j];
2670           if (FILENAME_CMP (this_name, filename) == 0)
2671             {
2672               dw2_instantiate_symtab (per_cu);
2673               break;
2674             }
2675         }
2676     }
2677 }
2678
2679 static const char *
2680 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2681 {
2682   struct dwarf2_per_cu_data *per_cu;
2683   offset_type *vec;
2684   struct quick_file_names *file_data;
2685
2686   dw2_setup (objfile);
2687
2688   /* index_table is NULL if OBJF_READNOW.  */
2689   if (!dwarf2_per_objfile->index_table)
2690     {
2691       struct symtab *s;
2692
2693       ALL_OBJFILE_SYMTABS (objfile, s)
2694         if (s->primary)
2695           {
2696             struct blockvector *bv = BLOCKVECTOR (s);
2697             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2698             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2699
2700             if (sym)
2701               return sym->symtab->filename;
2702           }
2703       return NULL;
2704     }
2705
2706   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2707                                  name, &vec))
2708     return NULL;
2709
2710   /* Note that this just looks at the very first one named NAME -- but
2711      actually we are looking for a function.  find_main_filename
2712      should be rewritten so that it doesn't require a custom hook.  It
2713      could just use the ordinary symbol tables.  */
2714   /* vec[0] is the length, which must always be >0.  */
2715   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2716
2717   file_data = dw2_get_file_names (objfile, per_cu);
2718   if (file_data == NULL)
2719     return NULL;
2720
2721   return file_data->file_names[file_data->num_file_names - 1];
2722 }
2723
2724 static void
2725 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2726                           struct objfile *objfile, int global,
2727                           int (*callback) (struct block *,
2728                                            struct symbol *, void *),
2729                           void *data, symbol_compare_ftype *match,
2730                           symbol_compare_ftype *ordered_compare)
2731 {
2732   /* Currently unimplemented; used for Ada.  The function can be called if the
2733      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2734      does not look for non-Ada symbols this function should just return.  */
2735 }
2736
2737 static void
2738 dw2_expand_symtabs_matching
2739   (struct objfile *objfile,
2740    int (*file_matcher) (const char *, void *),
2741    int (*name_matcher) (const struct language_defn *, const char *, void *),
2742    enum search_domain kind,
2743    void *data)
2744 {
2745   int i;
2746   offset_type iter;
2747   struct mapped_index *index;
2748
2749   dw2_setup (objfile);
2750
2751   /* index_table is NULL if OBJF_READNOW.  */
2752   if (!dwarf2_per_objfile->index_table)
2753     return;
2754   index = dwarf2_per_objfile->index_table;
2755
2756   if (file_matcher != NULL)
2757     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2758                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2759       {
2760         int j;
2761         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2762         struct quick_file_names *file_data;
2763
2764         per_cu->v.quick->mark = 0;
2765
2766         /* We only need to look at symtabs not already expanded.  */
2767         if (per_cu->v.quick->symtab)
2768           continue;
2769
2770         file_data = dw2_get_file_names (objfile, per_cu);
2771         if (file_data == NULL)
2772           continue;
2773
2774         for (j = 0; j < file_data->num_file_names; ++j)
2775           {
2776             if (file_matcher (file_data->file_names[j], data))
2777               {
2778                 per_cu->v.quick->mark = 1;
2779                 break;
2780               }
2781           }
2782       }
2783
2784   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2785     {
2786       offset_type idx = 2 * iter;
2787       const char *name;
2788       offset_type *vec, vec_len, vec_idx;
2789
2790       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2791         continue;
2792
2793       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2794
2795       if (! (*name_matcher) (current_language, name, data))
2796         continue;
2797
2798       /* The name was matched, now expand corresponding CUs that were
2799          marked.  */
2800       vec = (offset_type *) (index->constant_pool
2801                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2802       vec_len = MAYBE_SWAP (vec[0]);
2803       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2804         {
2805           struct dwarf2_per_cu_data *per_cu;
2806
2807           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2808           if (file_matcher == NULL || per_cu->v.quick->mark)
2809             dw2_instantiate_symtab (per_cu);
2810         }
2811     }
2812 }
2813
2814 static struct symtab *
2815 dw2_find_pc_sect_symtab (struct objfile *objfile,
2816                          struct minimal_symbol *msymbol,
2817                          CORE_ADDR pc,
2818                          struct obj_section *section,
2819                          int warn_if_readin)
2820 {
2821   struct dwarf2_per_cu_data *data;
2822
2823   dw2_setup (objfile);
2824
2825   if (!objfile->psymtabs_addrmap)
2826     return NULL;
2827
2828   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2829   if (!data)
2830     return NULL;
2831
2832   if (warn_if_readin && data->v.quick->symtab)
2833     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2834              paddress (get_objfile_arch (objfile), pc));
2835
2836   return dw2_instantiate_symtab (data);
2837 }
2838
2839 static void
2840 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2841                           void *data, int need_fullname)
2842 {
2843   int i;
2844
2845   dw2_setup (objfile);
2846
2847   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2848                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2849     {
2850       int j;
2851       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2852       struct quick_file_names *file_data;
2853
2854       /* We only need to look at symtabs not already expanded.  */
2855       if (per_cu->v.quick->symtab)
2856         continue;
2857
2858       file_data = dw2_get_file_names (objfile, per_cu);
2859       if (file_data == NULL)
2860         continue;
2861
2862       for (j = 0; j < file_data->num_file_names; ++j)
2863         {
2864           const char *this_real_name;
2865
2866           if (need_fullname)
2867             this_real_name = dw2_get_real_path (objfile, file_data, j);
2868           else
2869             this_real_name = NULL;
2870           (*fun) (file_data->file_names[j], this_real_name, data);
2871         }
2872     }
2873 }
2874
2875 static int
2876 dw2_has_symbols (struct objfile *objfile)
2877 {
2878   return 1;
2879 }
2880
2881 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2882 {
2883   dw2_has_symbols,
2884   dw2_find_last_source_symtab,
2885   dw2_forget_cached_source_info,
2886   dw2_map_symtabs_matching_filename,
2887   dw2_lookup_symbol,
2888   dw2_pre_expand_symtabs_matching,
2889   dw2_print_stats,
2890   dw2_dump,
2891   dw2_relocate,
2892   dw2_expand_symtabs_for_function,
2893   dw2_expand_all_symtabs,
2894   dw2_expand_symtabs_with_filename,
2895   dw2_find_symbol_file,
2896   dw2_map_matching_symbols,
2897   dw2_expand_symtabs_matching,
2898   dw2_find_pc_sect_symtab,
2899   dw2_map_symbol_filenames
2900 };
2901
2902 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2903    file will use psymtabs, or 1 if using the GNU index.  */
2904
2905 int
2906 dwarf2_initialize_objfile (struct objfile *objfile)
2907 {
2908   /* If we're about to read full symbols, don't bother with the
2909      indices.  In this case we also don't care if some other debug
2910      format is making psymtabs, because they are all about to be
2911      expanded anyway.  */
2912   if ((objfile->flags & OBJF_READNOW))
2913     {
2914       int i;
2915
2916       dwarf2_per_objfile->using_index = 1;
2917       create_all_comp_units (objfile);
2918       create_debug_types_hash_table (objfile);
2919       dwarf2_per_objfile->quick_file_names_table =
2920         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2921
2922       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2923                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2924         {
2925           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2926
2927           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2928                                             struct dwarf2_per_cu_quick_data);
2929         }
2930
2931       /* Return 1 so that gdb sees the "quick" functions.  However,
2932          these functions will be no-ops because we will have expanded
2933          all symtabs.  */
2934       return 1;
2935     }
2936
2937   if (dwarf2_read_index (objfile))
2938     return 1;
2939
2940   return 0;
2941 }
2942
2943 \f
2944
2945 /* Build a partial symbol table.  */
2946
2947 void
2948 dwarf2_build_psymtabs (struct objfile *objfile)
2949 {
2950   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2951     {
2952       init_psymbol_list (objfile, 1024);
2953     }
2954
2955   dwarf2_build_psymtabs_hard (objfile);
2956 }
2957
2958 /* Return TRUE if OFFSET is within CU_HEADER.  */
2959
2960 static inline int
2961 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2962 {
2963   unsigned int bottom = cu_header->offset;
2964   unsigned int top = (cu_header->offset
2965                       + cu_header->length
2966                       + cu_header->initial_length_size);
2967
2968   return (offset >= bottom && offset < top);
2969 }
2970
2971 /* Read in the comp unit header information from the debug_info at info_ptr.
2972    NOTE: This leaves members offset, first_die_offset to be filled in
2973    by the caller.  */
2974
2975 static gdb_byte *
2976 read_comp_unit_head (struct comp_unit_head *cu_header,
2977                      gdb_byte *info_ptr, bfd *abfd)
2978 {
2979   int signed_addr;
2980   unsigned int bytes_read;
2981
2982   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2983   cu_header->initial_length_size = bytes_read;
2984   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2985   info_ptr += bytes_read;
2986   cu_header->version = read_2_bytes (abfd, info_ptr);
2987   info_ptr += 2;
2988   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2989                                           &bytes_read);
2990   info_ptr += bytes_read;
2991   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2992   info_ptr += 1;
2993   signed_addr = bfd_get_sign_extend_vma (abfd);
2994   if (signed_addr < 0)
2995     internal_error (__FILE__, __LINE__,
2996                     _("read_comp_unit_head: dwarf from non elf file"));
2997   cu_header->signed_addr_p = signed_addr;
2998
2999   return info_ptr;
3000 }
3001
3002 /* Read in a CU header and perform some basic error checking.  */
3003
3004 static gdb_byte *
3005 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
3006                              gdb_byte *buffer, unsigned int buffer_size,
3007                              bfd *abfd, int is_debug_types_section)
3008 {
3009   gdb_byte *beg_of_comp_unit = info_ptr;
3010
3011   header->offset = beg_of_comp_unit - buffer;
3012
3013   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3014
3015   /* If we're reading a type unit, skip over the signature and
3016      type_offset fields.  */
3017   if (is_debug_types_section)
3018     info_ptr += 8 /*signature*/ + header->offset_size;
3019
3020   header->first_die_offset = info_ptr - beg_of_comp_unit;
3021
3022   if (header->version != 2 && header->version != 3 && header->version != 4)
3023     error (_("Dwarf Error: wrong version in compilation unit header "
3024            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3025            bfd_get_filename (abfd));
3026
3027   if (header->abbrev_offset
3028       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3029                               &dwarf2_per_objfile->abbrev))
3030     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3031            "(offset 0x%lx + 6) [in module %s]"),
3032            (long) header->abbrev_offset,
3033            (long) (beg_of_comp_unit - buffer),
3034            bfd_get_filename (abfd));
3035
3036   if (beg_of_comp_unit + header->length + header->initial_length_size
3037       > buffer + buffer_size)
3038     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3039            "(offset 0x%lx + 0) [in module %s]"),
3040            (long) header->length,
3041            (long) (beg_of_comp_unit - buffer),
3042            bfd_get_filename (abfd));
3043
3044   return info_ptr;
3045 }
3046
3047 /* Read in the types comp unit header information from .debug_types entry at
3048    types_ptr.  The result is a pointer to one past the end of the header.  */
3049
3050 static gdb_byte *
3051 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3052                           struct dwarf2_section_info *section,
3053                           ULONGEST *signature,
3054                           gdb_byte *types_ptr, bfd *abfd)
3055 {
3056   gdb_byte *initial_types_ptr = types_ptr;
3057
3058   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3059   cu_header->offset = types_ptr - section->buffer;
3060
3061   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3062
3063   *signature = read_8_bytes (abfd, types_ptr);
3064   types_ptr += 8;
3065   types_ptr += cu_header->offset_size;
3066   cu_header->first_die_offset = types_ptr - initial_types_ptr;
3067
3068   return types_ptr;
3069 }
3070
3071 /* Allocate a new partial symtab for file named NAME and mark this new
3072    partial symtab as being an include of PST.  */
3073
3074 static void
3075 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3076                                struct objfile *objfile)
3077 {
3078   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3079
3080   subpst->section_offsets = pst->section_offsets;
3081   subpst->textlow = 0;
3082   subpst->texthigh = 0;
3083
3084   subpst->dependencies = (struct partial_symtab **)
3085     obstack_alloc (&objfile->objfile_obstack,
3086                    sizeof (struct partial_symtab *));
3087   subpst->dependencies[0] = pst;
3088   subpst->number_of_dependencies = 1;
3089
3090   subpst->globals_offset = 0;
3091   subpst->n_global_syms = 0;
3092   subpst->statics_offset = 0;
3093   subpst->n_static_syms = 0;
3094   subpst->symtab = NULL;
3095   subpst->read_symtab = pst->read_symtab;
3096   subpst->readin = 0;
3097
3098   /* No private part is necessary for include psymtabs.  This property
3099      can be used to differentiate between such include psymtabs and
3100      the regular ones.  */
3101   subpst->read_symtab_private = NULL;
3102 }
3103
3104 /* Read the Line Number Program data and extract the list of files
3105    included by the source file represented by PST.  Build an include
3106    partial symtab for each of these included files.  */
3107
3108 static void
3109 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3110                                struct die_info *die,
3111                                struct partial_symtab *pst)
3112 {
3113   struct objfile *objfile = cu->objfile;
3114   bfd *abfd = objfile->obfd;
3115   struct line_header *lh = NULL;
3116   struct attribute *attr;
3117
3118   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3119   if (attr)
3120     {
3121       unsigned int line_offset = DW_UNSND (attr);
3122
3123       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3124     }
3125   if (lh == NULL)
3126     return;  /* No linetable, so no includes.  */
3127
3128   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3129   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3130
3131   free_line_header (lh);
3132 }
3133
3134 static hashval_t
3135 hash_type_signature (const void *item)
3136 {
3137   const struct signatured_type *type_sig = item;
3138
3139   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3140   return type_sig->signature;
3141 }
3142
3143 static int
3144 eq_type_signature (const void *item_lhs, const void *item_rhs)
3145 {
3146   const struct signatured_type *lhs = item_lhs;
3147   const struct signatured_type *rhs = item_rhs;
3148
3149   return lhs->signature == rhs->signature;
3150 }
3151
3152 /* Allocate a hash table for signatured types.  */
3153
3154 static htab_t
3155 allocate_signatured_type_table (struct objfile *objfile)
3156 {
3157   return htab_create_alloc_ex (41,
3158                                hash_type_signature,
3159                                eq_type_signature,
3160                                NULL,
3161                                &objfile->objfile_obstack,
3162                                hashtab_obstack_allocate,
3163                                dummy_obstack_deallocate);
3164 }
3165
3166 /* A helper function to add a signatured type CU to a list.  */
3167
3168 static int
3169 add_signatured_type_cu_to_list (void **slot, void *datum)
3170 {
3171   struct signatured_type *sigt = *slot;
3172   struct dwarf2_per_cu_data ***datap = datum;
3173
3174   **datap = &sigt->per_cu;
3175   ++*datap;
3176
3177   return 1;
3178 }
3179
3180 /* Create the hash table of all entries in the .debug_types section.
3181    The result is zero if there is an error (e.g. missing .debug_types section),
3182    otherwise non-zero.  */
3183
3184 static int
3185 create_debug_types_hash_table (struct objfile *objfile)
3186 {
3187   htab_t types_htab = NULL;
3188   struct dwarf2_per_cu_data **iter;
3189   int ix;
3190   struct dwarf2_section_info *section;
3191
3192   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3193     {
3194       dwarf2_per_objfile->signatured_types = NULL;
3195       return 0;
3196     }
3197
3198   for (ix = 0;
3199        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3200                     ix, section);
3201        ++ix)
3202     {
3203       gdb_byte *info_ptr, *end_ptr;
3204
3205       dwarf2_read_section (objfile, section);
3206       info_ptr = section->buffer;
3207
3208       if (info_ptr == NULL)
3209         continue;
3210
3211       if (types_htab == NULL)
3212         types_htab = allocate_signatured_type_table (objfile);
3213
3214       if (dwarf2_die_debug)
3215         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3216
3217       end_ptr = info_ptr + section->size;
3218       while (info_ptr < end_ptr)
3219         {
3220           unsigned int offset;
3221           unsigned int offset_size;
3222           unsigned int type_offset;
3223           unsigned int length, initial_length_size;
3224           unsigned short version;
3225           ULONGEST signature;
3226           struct signatured_type *type_sig;
3227           void **slot;
3228           gdb_byte *ptr = info_ptr;
3229
3230           offset = ptr - section->buffer;
3231
3232           /* We need to read the type's signature in order to build the hash
3233              table, but we don't need to read anything else just yet.  */
3234
3235           /* Sanity check to ensure entire cu is present.  */
3236           length = read_initial_length (objfile->obfd, ptr,
3237                                         &initial_length_size);
3238           if (ptr + length + initial_length_size > end_ptr)
3239             {
3240               complaint (&symfile_complaints,
3241                          _("debug type entry runs off end "
3242                            "of `.debug_types' section, ignored"));
3243               break;
3244             }
3245
3246           offset_size = initial_length_size == 4 ? 4 : 8;
3247           ptr += initial_length_size;
3248           version = bfd_get_16 (objfile->obfd, ptr);
3249           ptr += 2;
3250           ptr += offset_size; /* abbrev offset */
3251           ptr += 1; /* address size */
3252           signature = bfd_get_64 (objfile->obfd, ptr);
3253           ptr += 8;
3254           type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3255           ptr += offset_size;
3256
3257           /* Skip dummy type units.  */
3258           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3259             {
3260               info_ptr = info_ptr + initial_length_size + length;
3261               continue;
3262             }
3263
3264           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3265           memset (type_sig, 0, sizeof (*type_sig));
3266           type_sig->signature = signature;
3267           type_sig->type_offset = type_offset;
3268           type_sig->per_cu.objfile = objfile;
3269           type_sig->per_cu.debug_types_section = section;
3270           type_sig->per_cu.offset = offset;
3271
3272           slot = htab_find_slot (types_htab, type_sig, INSERT);
3273           gdb_assert (slot != NULL);
3274           if (*slot != NULL)
3275             {
3276               const struct signatured_type *dup_sig = *slot;
3277
3278               complaint (&symfile_complaints,
3279                          _("debug type entry at offset 0x%x is duplicate to the "
3280                            "entry at offset 0x%x, signature 0x%s"),
3281                          offset, dup_sig->per_cu.offset,
3282                          phex (signature, sizeof (signature)));
3283               gdb_assert (signature == dup_sig->signature);
3284             }
3285           *slot = type_sig;
3286
3287           if (dwarf2_die_debug)
3288             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3289                                 offset, phex (signature, sizeof (signature)));
3290
3291           info_ptr = info_ptr + initial_length_size + length;
3292         }
3293     }
3294
3295   dwarf2_per_objfile->signatured_types = types_htab;
3296
3297   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3298   dwarf2_per_objfile->type_comp_units
3299     = obstack_alloc (&objfile->objfile_obstack,
3300                      dwarf2_per_objfile->n_type_comp_units
3301                      * sizeof (struct dwarf2_per_cu_data *));
3302   iter = &dwarf2_per_objfile->type_comp_units[0];
3303   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3304   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3305               == dwarf2_per_objfile->n_type_comp_units);
3306
3307   return 1;
3308 }
3309
3310 /* Lookup a signature based type.
3311    Returns NULL if SIG is not present in the table.  */
3312
3313 static struct signatured_type *
3314 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3315 {
3316   struct signatured_type find_entry, *entry;
3317
3318   if (dwarf2_per_objfile->signatured_types == NULL)
3319     {
3320       complaint (&symfile_complaints,
3321                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3322       return 0;
3323     }
3324
3325   find_entry.signature = sig;
3326   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3327   return entry;
3328 }
3329
3330 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3331
3332 static void
3333 init_cu_die_reader (struct die_reader_specs *reader,
3334                     struct dwarf2_cu *cu)
3335 {
3336   reader->abfd = cu->objfile->obfd;
3337   reader->cu = cu;
3338   if (cu->per_cu->debug_types_section)
3339     {
3340       gdb_assert (cu->per_cu->debug_types_section->readin);
3341       reader->buffer = cu->per_cu->debug_types_section->buffer;
3342     }
3343   else
3344     {
3345       gdb_assert (dwarf2_per_objfile->info.readin);
3346       reader->buffer = dwarf2_per_objfile->info.buffer;
3347     }
3348 }
3349
3350 /* Find the base address of the compilation unit for range lists and
3351    location lists.  It will normally be specified by DW_AT_low_pc.
3352    In DWARF-3 draft 4, the base address could be overridden by
3353    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3354    compilation units with discontinuous ranges.  */
3355
3356 static void
3357 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3358 {
3359   struct attribute *attr;
3360
3361   cu->base_known = 0;
3362   cu->base_address = 0;
3363
3364   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3365   if (attr)
3366     {
3367       cu->base_address = DW_ADDR (attr);
3368       cu->base_known = 1;
3369     }
3370   else
3371     {
3372       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3373       if (attr)
3374         {
3375           cu->base_address = DW_ADDR (attr);
3376           cu->base_known = 1;
3377         }
3378     }
3379 }
3380
3381 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3382    to combine the common parts.
3383    Process a compilation unit for a psymtab.
3384    BUFFER is a pointer to the beginning of the dwarf section buffer,
3385    either .debug_info or debug_types.
3386    INFO_PTR is a pointer to the start of the CU.
3387    Returns a pointer to the next CU.  */
3388
3389 static gdb_byte *
3390 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3391                            gdb_byte *buffer, gdb_byte *info_ptr,
3392                            unsigned int buffer_size)
3393 {
3394   struct objfile *objfile = this_cu->objfile;
3395   bfd *abfd = objfile->obfd;
3396   gdb_byte *beg_of_comp_unit = info_ptr;
3397   struct die_info *comp_unit_die;
3398   struct partial_symtab *pst;
3399   CORE_ADDR baseaddr;
3400   struct cleanup *back_to_inner;
3401   struct dwarf2_cu cu;
3402   int has_children, has_pc_info;
3403   struct attribute *attr;
3404   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3405   struct die_reader_specs reader_specs;
3406   const char *filename;
3407
3408   /* If this compilation unit was already read in, free the
3409      cached copy in order to read it in again.  This is
3410      necessary because we skipped some symbols when we first
3411      read in the compilation unit (see load_partial_dies).
3412      This problem could be avoided, but the benefit is
3413      unclear.  */
3414   if (this_cu->cu != NULL)
3415     free_one_cached_comp_unit (this_cu->cu);
3416
3417   /* Note that this is a pointer to our stack frame, being
3418      added to a global data structure.  It will be cleaned up
3419      in free_stack_comp_unit when we finish with this
3420      compilation unit.  */
3421   init_one_comp_unit (&cu, this_cu);
3422   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3423
3424   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3425                                           buffer, buffer_size,
3426                                           abfd,
3427                                           this_cu->debug_types_section != NULL);
3428
3429   /* Skip dummy compilation units.  */
3430   if (info_ptr >= buffer + buffer_size
3431       || peek_abbrev_code (abfd, info_ptr) == 0)
3432     {
3433       info_ptr = (beg_of_comp_unit + cu.header.length
3434                   + cu.header.initial_length_size);
3435       do_cleanups (back_to_inner);
3436       return info_ptr;
3437     }
3438
3439   cu.list_in_scope = &file_symbols;
3440
3441   /* Read the abbrevs for this compilation unit into a table.  */
3442   dwarf2_read_abbrevs (abfd, &cu);
3443   make_cleanup (dwarf2_free_abbrev_table, &cu);
3444
3445   /* Read the compilation unit die.  */
3446   init_cu_die_reader (&reader_specs, &cu);
3447   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3448                             &has_children);
3449
3450   if (this_cu->debug_types_section)
3451     {
3452       /* LENGTH has not been set yet for type units.  */
3453       gdb_assert (this_cu->offset == cu.header.offset);
3454       this_cu->length = cu.header.length + cu.header.initial_length_size;
3455     }
3456   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3457     {
3458       info_ptr = (beg_of_comp_unit + cu.header.length
3459                   + cu.header.initial_length_size);
3460       do_cleanups (back_to_inner);
3461       return info_ptr;
3462     }
3463
3464   prepare_one_comp_unit (&cu, comp_unit_die);
3465
3466   /* Allocate a new partial symbol table structure.  */
3467   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3468   if (attr == NULL || !DW_STRING (attr))
3469     filename = "";
3470   else
3471     filename = DW_STRING (attr);
3472   pst = start_psymtab_common (objfile, objfile->section_offsets,
3473                               filename,
3474                               /* TEXTLOW and TEXTHIGH are set below.  */
3475                               0,
3476                               objfile->global_psymbols.next,
3477                               objfile->static_psymbols.next);
3478   pst->psymtabs_addrmap_supported = 1;
3479
3480   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3481   if (attr != NULL)
3482     pst->dirname = DW_STRING (attr);
3483
3484   pst->read_symtab_private = this_cu;
3485
3486   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3487
3488   /* Store the function that reads in the rest of the symbol table.  */
3489   pst->read_symtab = dwarf2_psymtab_to_symtab;
3490
3491   this_cu->v.psymtab = pst;
3492
3493   dwarf2_find_base_address (comp_unit_die, &cu);
3494
3495   /* Possibly set the default values of LOWPC and HIGHPC from
3496      `DW_AT_ranges'.  */
3497   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3498                                       &best_highpc, &cu, pst);
3499   if (has_pc_info == 1 && best_lowpc < best_highpc)
3500     /* Store the contiguous range if it is not empty; it can be empty for
3501        CUs with no code.  */
3502     addrmap_set_empty (objfile->psymtabs_addrmap,
3503                        best_lowpc + baseaddr,
3504                        best_highpc + baseaddr - 1, pst);
3505
3506   /* Check if comp unit has_children.
3507      If so, read the rest of the partial symbols from this comp unit.
3508      If not, there's no more debug_info for this comp unit.  */
3509   if (has_children)
3510     {
3511       struct partial_die_info *first_die;
3512       CORE_ADDR lowpc, highpc;
3513
3514       lowpc = ((CORE_ADDR) -1);
3515       highpc = ((CORE_ADDR) 0);
3516
3517       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3518
3519       scan_partial_symbols (first_die, &lowpc, &highpc,
3520                             ! has_pc_info, &cu);
3521
3522       /* If we didn't find a lowpc, set it to highpc to avoid
3523          complaints from `maint check'.  */
3524       if (lowpc == ((CORE_ADDR) -1))
3525         lowpc = highpc;
3526
3527       /* If the compilation unit didn't have an explicit address range,
3528          then use the information extracted from its child dies.  */
3529       if (! has_pc_info)
3530         {
3531           best_lowpc = lowpc;
3532           best_highpc = highpc;
3533         }
3534     }
3535   pst->textlow = best_lowpc + baseaddr;
3536   pst->texthigh = best_highpc + baseaddr;
3537
3538   pst->n_global_syms = objfile->global_psymbols.next -
3539     (objfile->global_psymbols.list + pst->globals_offset);
3540   pst->n_static_syms = objfile->static_psymbols.next -
3541     (objfile->static_psymbols.list + pst->statics_offset);
3542   sort_pst_symbols (pst);
3543
3544   info_ptr = (beg_of_comp_unit + cu.header.length
3545               + cu.header.initial_length_size);
3546
3547   if (this_cu->debug_types_section)
3548     {
3549       /* It's not clear we want to do anything with stmt lists here.
3550          Waiting to see what gcc ultimately does.  */
3551     }
3552   else
3553     {
3554       /* Get the list of files included in the current compilation unit,
3555          and build a psymtab for each of them.  */
3556       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3557     }
3558
3559   do_cleanups (back_to_inner);
3560
3561   return info_ptr;
3562 }
3563
3564 /* Traversal function for htab_traverse_noresize.
3565    Process one .debug_types comp-unit.  */
3566
3567 static int
3568 process_type_comp_unit (void **slot, void *info)
3569 {
3570   struct signatured_type *entry = (struct signatured_type *) *slot;
3571   struct dwarf2_per_cu_data *this_cu;
3572
3573   gdb_assert (info == NULL);
3574   this_cu = &entry->per_cu;
3575
3576   gdb_assert (this_cu->debug_types_section->readin);
3577   process_psymtab_comp_unit (this_cu,
3578                              this_cu->debug_types_section->buffer,
3579                              (this_cu->debug_types_section->buffer
3580                               + this_cu->offset),
3581                              this_cu->debug_types_section->size);
3582
3583   return 1;
3584 }
3585
3586 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3587    Build partial symbol tables for the .debug_types comp-units.  */
3588
3589 static void
3590 build_type_psymtabs (struct objfile *objfile)
3591 {
3592   if (! create_debug_types_hash_table (objfile))
3593     return;
3594
3595   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3596                           process_type_comp_unit, NULL);
3597 }
3598
3599 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3600
3601 static void
3602 psymtabs_addrmap_cleanup (void *o)
3603 {
3604   struct objfile *objfile = o;
3605
3606   objfile->psymtabs_addrmap = NULL;
3607 }
3608
3609 /* Build the partial symbol table by doing a quick pass through the
3610    .debug_info and .debug_abbrev sections.  */
3611
3612 static void
3613 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3614 {
3615   gdb_byte *info_ptr;
3616   struct cleanup *back_to, *addrmap_cleanup;
3617   struct obstack temp_obstack;
3618
3619   dwarf2_per_objfile->reading_partial_symbols = 1;
3620
3621   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3622   info_ptr = dwarf2_per_objfile->info.buffer;
3623
3624   /* Any cached compilation units will be linked by the per-objfile
3625      read_in_chain.  Make sure to free them when we're done.  */
3626   back_to = make_cleanup (free_cached_comp_units, NULL);
3627
3628   build_type_psymtabs (objfile);
3629
3630   create_all_comp_units (objfile);
3631
3632   /* Create a temporary address map on a temporary obstack.  We later
3633      copy this to the final obstack.  */
3634   obstack_init (&temp_obstack);
3635   make_cleanup_obstack_free (&temp_obstack);
3636   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3637   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3638
3639   /* Since the objects we're extracting from .debug_info vary in
3640      length, only the individual functions to extract them (like
3641      read_comp_unit_head and load_partial_die) can really know whether
3642      the buffer is large enough to hold another complete object.
3643
3644      At the moment, they don't actually check that.  If .debug_info
3645      holds just one extra byte after the last compilation unit's dies,
3646      then read_comp_unit_head will happily read off the end of the
3647      buffer.  read_partial_die is similarly casual.  Those functions
3648      should be fixed.
3649
3650      For this loop condition, simply checking whether there's any data
3651      left at all should be sufficient.  */
3652
3653   while (info_ptr < (dwarf2_per_objfile->info.buffer
3654                      + dwarf2_per_objfile->info.size))
3655     {
3656       struct dwarf2_per_cu_data *this_cu;
3657
3658       this_cu = dwarf2_find_comp_unit (info_ptr
3659                                        - dwarf2_per_objfile->info.buffer,
3660                                        objfile);
3661
3662       info_ptr = process_psymtab_comp_unit (this_cu,
3663                                             dwarf2_per_objfile->info.buffer,
3664                                             info_ptr,
3665                                             dwarf2_per_objfile->info.size);
3666     }
3667
3668   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3669                                                     &objfile->objfile_obstack);
3670   discard_cleanups (addrmap_cleanup);
3671
3672   do_cleanups (back_to);
3673 }
3674
3675 /* Load the partial DIEs for a secondary CU into memory.  */
3676
3677 static void
3678 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3679 {
3680   struct objfile *objfile = this_cu->objfile;
3681   bfd *abfd = objfile->obfd;
3682   gdb_byte *info_ptr;
3683   struct die_info *comp_unit_die;
3684   struct dwarf2_cu *cu;
3685   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3686   int has_children;
3687   struct die_reader_specs reader_specs;
3688   int read_cu = 0;
3689
3690   gdb_assert (! this_cu->debug_types_section);
3691
3692   gdb_assert (dwarf2_per_objfile->info.readin);
3693   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3694
3695   if (this_cu->cu == NULL)
3696     {
3697       cu = xmalloc (sizeof (*cu));
3698       init_one_comp_unit (cu, this_cu);
3699
3700       read_cu = 1;
3701
3702       /* If an error occurs while loading, release our storage.  */
3703       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3704
3705       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3706                                               dwarf2_per_objfile->info.buffer,
3707                                               dwarf2_per_objfile->info.size,
3708                                               abfd, 0);
3709
3710       /* Skip dummy compilation units.  */
3711       if (info_ptr >= (dwarf2_per_objfile->info.buffer
3712                        + dwarf2_per_objfile->info.size)
3713           || peek_abbrev_code (abfd, info_ptr) == 0)
3714         {
3715           do_cleanups (free_cu_cleanup);
3716           return;
3717         }
3718
3719       /* Link this CU into read_in_chain.  */
3720       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3721       dwarf2_per_objfile->read_in_chain = this_cu;
3722     }
3723   else
3724     {
3725       cu = this_cu->cu;
3726       info_ptr += cu->header.first_die_offset;
3727     }
3728
3729   /* Read the abbrevs for this compilation unit into a table.  */
3730   gdb_assert (cu->dwarf2_abbrevs == NULL);
3731   dwarf2_read_abbrevs (abfd, cu);
3732   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3733
3734   /* Read the compilation unit die.  */
3735   init_cu_die_reader (&reader_specs, cu);
3736   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3737                             &has_children);
3738
3739   prepare_one_comp_unit (cu, comp_unit_die);
3740
3741   /* Check if comp unit has_children.
3742      If so, read the rest of the partial symbols from this comp unit.
3743      If not, there's no more debug_info for this comp unit.  */
3744   if (has_children)
3745     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3746
3747   do_cleanups (free_abbrevs_cleanup);
3748
3749   if (read_cu)
3750     {
3751       /* We've successfully allocated this compilation unit.  Let our
3752          caller clean it up when finished with it.  */
3753       discard_cleanups (free_cu_cleanup);
3754     }
3755 }
3756
3757 /* Create a list of all compilation units in OBJFILE.
3758    This is only done for -readnow and building partial symtabs.  */
3759
3760 static void
3761 create_all_comp_units (struct objfile *objfile)
3762 {
3763   int n_allocated;
3764   int n_comp_units;
3765   struct dwarf2_per_cu_data **all_comp_units;
3766   gdb_byte *info_ptr;
3767
3768   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3769   info_ptr = dwarf2_per_objfile->info.buffer;
3770
3771   n_comp_units = 0;
3772   n_allocated = 10;
3773   all_comp_units = xmalloc (n_allocated
3774                             * sizeof (struct dwarf2_per_cu_data *));
3775
3776   while (info_ptr < dwarf2_per_objfile->info.buffer
3777          + dwarf2_per_objfile->info.size)
3778     {
3779       unsigned int length, initial_length_size;
3780       struct dwarf2_per_cu_data *this_cu;
3781       unsigned int offset;
3782
3783       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3784
3785       /* Read just enough information to find out where the next
3786          compilation unit is.  */
3787       length = read_initial_length (objfile->obfd, info_ptr,
3788                                     &initial_length_size);
3789
3790       /* Save the compilation unit for later lookup.  */
3791       this_cu = obstack_alloc (&objfile->objfile_obstack,
3792                                sizeof (struct dwarf2_per_cu_data));
3793       memset (this_cu, 0, sizeof (*this_cu));
3794       this_cu->offset = offset;
3795       this_cu->length = length + initial_length_size;
3796       this_cu->objfile = objfile;
3797
3798       if (n_comp_units == n_allocated)
3799         {
3800           n_allocated *= 2;
3801           all_comp_units = xrealloc (all_comp_units,
3802                                      n_allocated
3803                                      * sizeof (struct dwarf2_per_cu_data *));
3804         }
3805       all_comp_units[n_comp_units++] = this_cu;
3806
3807       info_ptr = info_ptr + this_cu->length;
3808     }
3809
3810   dwarf2_per_objfile->all_comp_units
3811     = obstack_alloc (&objfile->objfile_obstack,
3812                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3813   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3814           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3815   xfree (all_comp_units);
3816   dwarf2_per_objfile->n_comp_units = n_comp_units;
3817 }
3818
3819 /* Process all loaded DIEs for compilation unit CU, starting at
3820    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3821    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3822    DW_AT_ranges).  If NEED_PC is set, then this function will set
3823    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3824    and record the covered ranges in the addrmap.  */
3825
3826 static void
3827 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3828                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3829 {
3830   struct partial_die_info *pdi;
3831
3832   /* Now, march along the PDI's, descending into ones which have
3833      interesting children but skipping the children of the other ones,
3834      until we reach the end of the compilation unit.  */
3835
3836   pdi = first_die;
3837
3838   while (pdi != NULL)
3839     {
3840       fixup_partial_die (pdi, cu);
3841
3842       /* Anonymous namespaces or modules have no name but have interesting
3843          children, so we need to look at them.  Ditto for anonymous
3844          enums.  */
3845
3846       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3847           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3848         {
3849           switch (pdi->tag)
3850             {
3851             case DW_TAG_subprogram:
3852               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3853               break;
3854             case DW_TAG_constant:
3855             case DW_TAG_variable:
3856             case DW_TAG_typedef:
3857             case DW_TAG_union_type:
3858               if (!pdi->is_declaration)
3859                 {
3860                   add_partial_symbol (pdi, cu);
3861                 }
3862               break;
3863             case DW_TAG_class_type:
3864             case DW_TAG_interface_type:
3865             case DW_TAG_structure_type:
3866               if (!pdi->is_declaration)
3867                 {
3868                   add_partial_symbol (pdi, cu);
3869                 }
3870               break;
3871             case DW_TAG_enumeration_type:
3872               if (!pdi->is_declaration)
3873                 add_partial_enumeration (pdi, cu);
3874               break;
3875             case DW_TAG_base_type:
3876             case DW_TAG_subrange_type:
3877               /* File scope base type definitions are added to the partial
3878                  symbol table.  */
3879               add_partial_symbol (pdi, cu);
3880               break;
3881             case DW_TAG_namespace:
3882               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3883               break;
3884             case DW_TAG_module:
3885               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3886               break;
3887             default:
3888               break;
3889             }
3890         }
3891
3892       /* If the die has a sibling, skip to the sibling.  */
3893
3894       pdi = pdi->die_sibling;
3895     }
3896 }
3897
3898 /* Functions used to compute the fully scoped name of a partial DIE.
3899
3900    Normally, this is simple.  For C++, the parent DIE's fully scoped
3901    name is concatenated with "::" and the partial DIE's name.  For
3902    Java, the same thing occurs except that "." is used instead of "::".
3903    Enumerators are an exception; they use the scope of their parent
3904    enumeration type, i.e. the name of the enumeration type is not
3905    prepended to the enumerator.
3906
3907    There are two complexities.  One is DW_AT_specification; in this
3908    case "parent" means the parent of the target of the specification,
3909    instead of the direct parent of the DIE.  The other is compilers
3910    which do not emit DW_TAG_namespace; in this case we try to guess
3911    the fully qualified name of structure types from their members'
3912    linkage names.  This must be done using the DIE's children rather
3913    than the children of any DW_AT_specification target.  We only need
3914    to do this for structures at the top level, i.e. if the target of
3915    any DW_AT_specification (if any; otherwise the DIE itself) does not
3916    have a parent.  */
3917
3918 /* Compute the scope prefix associated with PDI's parent, in
3919    compilation unit CU.  The result will be allocated on CU's
3920    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3921    field.  NULL is returned if no prefix is necessary.  */
3922 static char *
3923 partial_die_parent_scope (struct partial_die_info *pdi,
3924                           struct dwarf2_cu *cu)
3925 {
3926   char *grandparent_scope;
3927   struct partial_die_info *parent, *real_pdi;
3928
3929   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3930      then this means the parent of the specification DIE.  */
3931
3932   real_pdi = pdi;
3933   while (real_pdi->has_specification)
3934     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3935
3936   parent = real_pdi->die_parent;
3937   if (parent == NULL)
3938     return NULL;
3939
3940   if (parent->scope_set)
3941     return parent->scope;
3942
3943   fixup_partial_die (parent, cu);
3944
3945   grandparent_scope = partial_die_parent_scope (parent, cu);
3946
3947   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3948      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3949      Work around this problem here.  */
3950   if (cu->language == language_cplus
3951       && parent->tag == DW_TAG_namespace
3952       && strcmp (parent->name, "::") == 0
3953       && grandparent_scope == NULL)
3954     {
3955       parent->scope = NULL;
3956       parent->scope_set = 1;
3957       return NULL;
3958     }
3959
3960   if (pdi->tag == DW_TAG_enumerator)
3961     /* Enumerators should not get the name of the enumeration as a prefix.  */
3962     parent->scope = grandparent_scope;
3963   else if (parent->tag == DW_TAG_namespace
3964       || parent->tag == DW_TAG_module
3965       || parent->tag == DW_TAG_structure_type
3966       || parent->tag == DW_TAG_class_type
3967       || parent->tag == DW_TAG_interface_type
3968       || parent->tag == DW_TAG_union_type
3969       || parent->tag == DW_TAG_enumeration_type)
3970     {
3971       if (grandparent_scope == NULL)
3972         parent->scope = parent->name;
3973       else
3974         parent->scope = typename_concat (&cu->comp_unit_obstack,
3975                                          grandparent_scope,
3976                                          parent->name, 0, cu);
3977     }
3978   else
3979     {
3980       /* FIXME drow/2004-04-01: What should we be doing with
3981          function-local names?  For partial symbols, we should probably be
3982          ignoring them.  */
3983       complaint (&symfile_complaints,
3984                  _("unhandled containing DIE tag %d for DIE at %d"),
3985                  parent->tag, pdi->offset);
3986       parent->scope = grandparent_scope;
3987     }
3988
3989   parent->scope_set = 1;
3990   return parent->scope;
3991 }
3992
3993 /* Return the fully scoped name associated with PDI, from compilation unit
3994    CU.  The result will be allocated with malloc.  */
3995 static char *
3996 partial_die_full_name (struct partial_die_info *pdi,
3997                        struct dwarf2_cu *cu)
3998 {
3999   char *parent_scope;
4000
4001   /* If this is a template instantiation, we can not work out the
4002      template arguments from partial DIEs.  So, unfortunately, we have
4003      to go through the full DIEs.  At least any work we do building
4004      types here will be reused if full symbols are loaded later.  */
4005   if (pdi->has_template_arguments)
4006     {
4007       fixup_partial_die (pdi, cu);
4008
4009       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4010         {
4011           struct die_info *die;
4012           struct attribute attr;
4013           struct dwarf2_cu *ref_cu = cu;
4014
4015           attr.name = 0;
4016           attr.form = DW_FORM_ref_addr;
4017           attr.u.addr = pdi->offset;
4018           die = follow_die_ref (NULL, &attr, &ref_cu);
4019
4020           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4021         }
4022     }
4023
4024   parent_scope = partial_die_parent_scope (pdi, cu);
4025   if (parent_scope == NULL)
4026     return NULL;
4027   else
4028     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4029 }
4030
4031 static void
4032 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4033 {
4034   struct objfile *objfile = cu->objfile;
4035   CORE_ADDR addr = 0;
4036   char *actual_name = NULL;
4037   const struct partial_symbol *psym = NULL;
4038   CORE_ADDR baseaddr;
4039   int built_actual_name = 0;
4040
4041   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4042
4043   actual_name = partial_die_full_name (pdi, cu);
4044   if (actual_name)
4045     built_actual_name = 1;
4046
4047   if (actual_name == NULL)
4048     actual_name = pdi->name;
4049
4050   switch (pdi->tag)
4051     {
4052     case DW_TAG_subprogram:
4053       if (pdi->is_external || cu->language == language_ada)
4054         {
4055           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4056              of the global scope.  But in Ada, we want to be able to access
4057              nested procedures globally.  So all Ada subprograms are stored
4058              in the global scope.  */
4059           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4060              mst_text, objfile); */
4061           add_psymbol_to_list (actual_name, strlen (actual_name),
4062                                built_actual_name,
4063                                VAR_DOMAIN, LOC_BLOCK,
4064                                &objfile->global_psymbols,
4065                                0, pdi->lowpc + baseaddr,
4066                                cu->language, objfile);
4067         }
4068       else
4069         {
4070           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4071              mst_file_text, objfile); */
4072           add_psymbol_to_list (actual_name, strlen (actual_name),
4073                                built_actual_name,
4074                                VAR_DOMAIN, LOC_BLOCK,
4075                                &objfile->static_psymbols,
4076                                0, pdi->lowpc + baseaddr,
4077                                cu->language, objfile);
4078         }
4079       break;
4080     case DW_TAG_constant:
4081       {
4082         struct psymbol_allocation_list *list;
4083
4084         if (pdi->is_external)
4085           list = &objfile->global_psymbols;
4086         else
4087           list = &objfile->static_psymbols;
4088         add_psymbol_to_list (actual_name, strlen (actual_name),
4089                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4090                              list, 0, 0, cu->language, objfile);
4091       }
4092       break;
4093     case DW_TAG_variable:
4094       if (pdi->locdesc)
4095         addr = decode_locdesc (pdi->locdesc, cu);
4096
4097       if (pdi->locdesc
4098           && addr == 0
4099           && !dwarf2_per_objfile->has_section_at_zero)
4100         {
4101           /* A global or static variable may also have been stripped
4102              out by the linker if unused, in which case its address
4103              will be nullified; do not add such variables into partial
4104              symbol table then.  */
4105         }
4106       else if (pdi->is_external)
4107         {
4108           /* Global Variable.
4109              Don't enter into the minimal symbol tables as there is
4110              a minimal symbol table entry from the ELF symbols already.
4111              Enter into partial symbol table if it has a location
4112              descriptor or a type.
4113              If the location descriptor is missing, new_symbol will create
4114              a LOC_UNRESOLVED symbol, the address of the variable will then
4115              be determined from the minimal symbol table whenever the variable
4116              is referenced.
4117              The address for the partial symbol table entry is not
4118              used by GDB, but it comes in handy for debugging partial symbol
4119              table building.  */
4120
4121           if (pdi->locdesc || pdi->has_type)
4122             add_psymbol_to_list (actual_name, strlen (actual_name),
4123                                  built_actual_name,
4124                                  VAR_DOMAIN, LOC_STATIC,
4125                                  &objfile->global_psymbols,
4126                                  0, addr + baseaddr,
4127                                  cu->language, objfile);
4128         }
4129       else
4130         {
4131           /* Static Variable.  Skip symbols without location descriptors.  */
4132           if (pdi->locdesc == NULL)
4133             {
4134               if (built_actual_name)
4135                 xfree (actual_name);
4136               return;
4137             }
4138           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4139              mst_file_data, objfile); */
4140           add_psymbol_to_list (actual_name, strlen (actual_name),
4141                                built_actual_name,
4142                                VAR_DOMAIN, LOC_STATIC,
4143                                &objfile->static_psymbols,
4144                                0, addr + baseaddr,
4145                                cu->language, objfile);
4146         }
4147       break;
4148     case DW_TAG_typedef:
4149     case DW_TAG_base_type:
4150     case DW_TAG_subrange_type:
4151       add_psymbol_to_list (actual_name, strlen (actual_name),
4152                            built_actual_name,
4153                            VAR_DOMAIN, LOC_TYPEDEF,
4154                            &objfile->static_psymbols,
4155                            0, (CORE_ADDR) 0, cu->language, objfile);
4156       break;
4157     case DW_TAG_namespace:
4158       add_psymbol_to_list (actual_name, strlen (actual_name),
4159                            built_actual_name,
4160                            VAR_DOMAIN, LOC_TYPEDEF,
4161                            &objfile->global_psymbols,
4162                            0, (CORE_ADDR) 0, cu->language, objfile);
4163       break;
4164     case DW_TAG_class_type:
4165     case DW_TAG_interface_type:
4166     case DW_TAG_structure_type:
4167     case DW_TAG_union_type:
4168     case DW_TAG_enumeration_type:
4169       /* Skip external references.  The DWARF standard says in the section
4170          about "Structure, Union, and Class Type Entries": "An incomplete
4171          structure, union or class type is represented by a structure,
4172          union or class entry that does not have a byte size attribute
4173          and that has a DW_AT_declaration attribute."  */
4174       if (!pdi->has_byte_size && pdi->is_declaration)
4175         {
4176           if (built_actual_name)
4177             xfree (actual_name);
4178           return;
4179         }
4180
4181       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4182          static vs. global.  */
4183       add_psymbol_to_list (actual_name, strlen (actual_name),
4184                            built_actual_name,
4185                            STRUCT_DOMAIN, LOC_TYPEDEF,
4186                            (cu->language == language_cplus
4187                             || cu->language == language_java)
4188                            ? &objfile->global_psymbols
4189                            : &objfile->static_psymbols,
4190                            0, (CORE_ADDR) 0, cu->language, objfile);
4191
4192       break;
4193     case DW_TAG_enumerator:
4194       add_psymbol_to_list (actual_name, strlen (actual_name),
4195                            built_actual_name,
4196                            VAR_DOMAIN, LOC_CONST,
4197                            (cu->language == language_cplus
4198                             || cu->language == language_java)
4199                            ? &objfile->global_psymbols
4200                            : &objfile->static_psymbols,
4201                            0, (CORE_ADDR) 0, cu->language, objfile);
4202       break;
4203     default:
4204       break;
4205     }
4206
4207   if (built_actual_name)
4208     xfree (actual_name);
4209 }
4210
4211 /* Read a partial die corresponding to a namespace; also, add a symbol
4212    corresponding to that namespace to the symbol table.  NAMESPACE is
4213    the name of the enclosing namespace.  */
4214
4215 static void
4216 add_partial_namespace (struct partial_die_info *pdi,
4217                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4218                        int need_pc, struct dwarf2_cu *cu)
4219 {
4220   /* Add a symbol for the namespace.  */
4221
4222   add_partial_symbol (pdi, cu);
4223
4224   /* Now scan partial symbols in that namespace.  */
4225
4226   if (pdi->has_children)
4227     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4228 }
4229
4230 /* Read a partial die corresponding to a Fortran module.  */
4231
4232 static void
4233 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4234                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4235 {
4236   /* Now scan partial symbols in that module.  */
4237
4238   if (pdi->has_children)
4239     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4240 }
4241
4242 /* Read a partial die corresponding to a subprogram and create a partial
4243    symbol for that subprogram.  When the CU language allows it, this
4244    routine also defines a partial symbol for each nested subprogram
4245    that this subprogram contains.
4246
4247    DIE my also be a lexical block, in which case we simply search
4248    recursively for suprograms defined inside that lexical block.
4249    Again, this is only performed when the CU language allows this
4250    type of definitions.  */
4251
4252 static void
4253 add_partial_subprogram (struct partial_die_info *pdi,
4254                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4255                         int need_pc, struct dwarf2_cu *cu)
4256 {
4257   if (pdi->tag == DW_TAG_subprogram)
4258     {
4259       if (pdi->has_pc_info)
4260         {
4261           if (pdi->lowpc < *lowpc)
4262             *lowpc = pdi->lowpc;
4263           if (pdi->highpc > *highpc)
4264             *highpc = pdi->highpc;
4265           if (need_pc)
4266             {
4267               CORE_ADDR baseaddr;
4268               struct objfile *objfile = cu->objfile;
4269
4270               baseaddr = ANOFFSET (objfile->section_offsets,
4271                                    SECT_OFF_TEXT (objfile));
4272               addrmap_set_empty (objfile->psymtabs_addrmap,
4273                                  pdi->lowpc + baseaddr,
4274                                  pdi->highpc - 1 + baseaddr,
4275                                  cu->per_cu->v.psymtab);
4276             }
4277           if (!pdi->is_declaration)
4278             /* Ignore subprogram DIEs that do not have a name, they are
4279                illegal.  Do not emit a complaint at this point, we will
4280                do so when we convert this psymtab into a symtab.  */
4281             if (pdi->name)
4282               add_partial_symbol (pdi, cu);
4283         }
4284     }
4285
4286   if (! pdi->has_children)
4287     return;
4288
4289   if (cu->language == language_ada)
4290     {
4291       pdi = pdi->die_child;
4292       while (pdi != NULL)
4293         {
4294           fixup_partial_die (pdi, cu);
4295           if (pdi->tag == DW_TAG_subprogram
4296               || pdi->tag == DW_TAG_lexical_block)
4297             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4298           pdi = pdi->die_sibling;
4299         }
4300     }
4301 }
4302
4303 /* Read a partial die corresponding to an enumeration type.  */
4304
4305 static void
4306 add_partial_enumeration (struct partial_die_info *enum_pdi,
4307                          struct dwarf2_cu *cu)
4308 {
4309   struct partial_die_info *pdi;
4310
4311   if (enum_pdi->name != NULL)
4312     add_partial_symbol (enum_pdi, cu);
4313
4314   pdi = enum_pdi->die_child;
4315   while (pdi)
4316     {
4317       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4318         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4319       else
4320         add_partial_symbol (pdi, cu);
4321       pdi = pdi->die_sibling;
4322     }
4323 }
4324
4325 /* Return the initial uleb128 in the die at INFO_PTR.  */
4326
4327 static unsigned int
4328 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4329 {
4330   unsigned int bytes_read;
4331
4332   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4333 }
4334
4335 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4336    Return the corresponding abbrev, or NULL if the number is zero (indicating
4337    an empty DIE).  In either case *BYTES_READ will be set to the length of
4338    the initial number.  */
4339
4340 static struct abbrev_info *
4341 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4342                  struct dwarf2_cu *cu)
4343 {
4344   bfd *abfd = cu->objfile->obfd;
4345   unsigned int abbrev_number;
4346   struct abbrev_info *abbrev;
4347
4348   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4349
4350   if (abbrev_number == 0)
4351     return NULL;
4352
4353   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4354   if (!abbrev)
4355     {
4356       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4357              abbrev_number, bfd_get_filename (abfd));
4358     }
4359
4360   return abbrev;
4361 }
4362
4363 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4364    Returns a pointer to the end of a series of DIEs, terminated by an empty
4365    DIE.  Any children of the skipped DIEs will also be skipped.  */
4366
4367 static gdb_byte *
4368 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4369 {
4370   struct abbrev_info *abbrev;
4371   unsigned int bytes_read;
4372
4373   while (1)
4374     {
4375       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4376       if (abbrev == NULL)
4377         return info_ptr + bytes_read;
4378       else
4379         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4380     }
4381 }
4382
4383 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4384    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4385    abbrev corresponding to that skipped uleb128 should be passed in
4386    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4387    children.  */
4388
4389 static gdb_byte *
4390 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4391               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4392 {
4393   unsigned int bytes_read;
4394   struct attribute attr;
4395   bfd *abfd = cu->objfile->obfd;
4396   unsigned int form, i;
4397
4398   for (i = 0; i < abbrev->num_attrs; i++)
4399     {
4400       /* The only abbrev we care about is DW_AT_sibling.  */
4401       if (abbrev->attrs[i].name == DW_AT_sibling)
4402         {
4403           read_attribute (&attr, &abbrev->attrs[i],
4404                           abfd, info_ptr, cu);
4405           if (attr.form == DW_FORM_ref_addr)
4406             complaint (&symfile_complaints,
4407                        _("ignoring absolute DW_AT_sibling"));
4408           else
4409             return buffer + dwarf2_get_ref_die_offset (&attr);
4410         }
4411
4412       /* If it isn't DW_AT_sibling, skip this attribute.  */
4413       form = abbrev->attrs[i].form;
4414     skip_attribute:
4415       switch (form)
4416         {
4417         case DW_FORM_ref_addr:
4418           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4419              and later it is offset sized.  */
4420           if (cu->header.version == 2)
4421             info_ptr += cu->header.addr_size;
4422           else
4423             info_ptr += cu->header.offset_size;
4424           break;
4425         case DW_FORM_addr:
4426           info_ptr += cu->header.addr_size;
4427           break;
4428         case DW_FORM_data1:
4429         case DW_FORM_ref1:
4430         case DW_FORM_flag:
4431           info_ptr += 1;
4432           break;
4433         case DW_FORM_flag_present:
4434           break;
4435         case DW_FORM_data2:
4436         case DW_FORM_ref2:
4437           info_ptr += 2;
4438           break;
4439         case DW_FORM_data4:
4440         case DW_FORM_ref4:
4441           info_ptr += 4;
4442           break;
4443         case DW_FORM_data8:
4444         case DW_FORM_ref8:
4445         case DW_FORM_ref_sig8:
4446           info_ptr += 8;
4447           break;
4448         case DW_FORM_string:
4449           read_direct_string (abfd, info_ptr, &bytes_read);
4450           info_ptr += bytes_read;
4451           break;
4452         case DW_FORM_sec_offset:
4453         case DW_FORM_strp:
4454           info_ptr += cu->header.offset_size;
4455           break;
4456         case DW_FORM_exprloc:
4457         case DW_FORM_block:
4458           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4459           info_ptr += bytes_read;
4460           break;
4461         case DW_FORM_block1:
4462           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4463           break;
4464         case DW_FORM_block2:
4465           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4466           break;
4467         case DW_FORM_block4:
4468           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4469           break;
4470         case DW_FORM_sdata:
4471         case DW_FORM_udata:
4472         case DW_FORM_ref_udata:
4473           info_ptr = skip_leb128 (abfd, info_ptr);
4474           break;
4475         case DW_FORM_indirect:
4476           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4477           info_ptr += bytes_read;
4478           /* We need to continue parsing from here, so just go back to
4479              the top.  */
4480           goto skip_attribute;
4481
4482         default:
4483           error (_("Dwarf Error: Cannot handle %s "
4484                    "in DWARF reader [in module %s]"),
4485                  dwarf_form_name (form),
4486                  bfd_get_filename (abfd));
4487         }
4488     }
4489
4490   if (abbrev->has_children)
4491     return skip_children (buffer, info_ptr, cu);
4492   else
4493     return info_ptr;
4494 }
4495
4496 /* Locate ORIG_PDI's sibling.
4497    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4498    in BUFFER.  */
4499
4500 static gdb_byte *
4501 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4502                     gdb_byte *buffer, gdb_byte *info_ptr,
4503                     bfd *abfd, struct dwarf2_cu *cu)
4504 {
4505   /* Do we know the sibling already?  */
4506
4507   if (orig_pdi->sibling)
4508     return orig_pdi->sibling;
4509
4510   /* Are there any children to deal with?  */
4511
4512   if (!orig_pdi->has_children)
4513     return info_ptr;
4514
4515   /* Skip the children the long way.  */
4516
4517   return skip_children (buffer, info_ptr, cu);
4518 }
4519
4520 /* Expand this partial symbol table into a full symbol table.  */
4521
4522 static void
4523 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4524 {
4525   if (pst != NULL)
4526     {
4527       if (pst->readin)
4528         {
4529           warning (_("bug: psymtab for %s is already read in."),
4530                    pst->filename);
4531         }
4532       else
4533         {
4534           if (info_verbose)
4535             {
4536               printf_filtered (_("Reading in symbols for %s..."),
4537                                pst->filename);
4538               gdb_flush (gdb_stdout);
4539             }
4540
4541           /* Restore our global data.  */
4542           dwarf2_per_objfile = objfile_data (pst->objfile,
4543                                              dwarf2_objfile_data_key);
4544
4545           /* If this psymtab is constructed from a debug-only objfile, the
4546              has_section_at_zero flag will not necessarily be correct.  We
4547              can get the correct value for this flag by looking at the data
4548              associated with the (presumably stripped) associated objfile.  */
4549           if (pst->objfile->separate_debug_objfile_backlink)
4550             {
4551               struct dwarf2_per_objfile *dpo_backlink
4552                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4553                                 dwarf2_objfile_data_key);
4554
4555               dwarf2_per_objfile->has_section_at_zero
4556                 = dpo_backlink->has_section_at_zero;
4557             }
4558
4559           dwarf2_per_objfile->reading_partial_symbols = 0;
4560
4561           psymtab_to_symtab_1 (pst);
4562
4563           /* Finish up the debug error message.  */
4564           if (info_verbose)
4565             printf_filtered (_("done.\n"));
4566         }
4567     }
4568 }
4569 \f
4570 /* Reading in full CUs.  */
4571
4572 /* Add PER_CU to the queue.  */
4573
4574 static void
4575 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4576 {
4577   struct dwarf2_queue_item *item;
4578
4579   per_cu->queued = 1;
4580   item = xmalloc (sizeof (*item));
4581   item->per_cu = per_cu;
4582   item->next = NULL;
4583
4584   if (dwarf2_queue == NULL)
4585     dwarf2_queue = item;
4586   else
4587     dwarf2_queue_tail->next = item;
4588
4589   dwarf2_queue_tail = item;
4590 }
4591
4592 /* Process the queue.  */
4593
4594 static void
4595 process_queue (void)
4596 {
4597   struct dwarf2_queue_item *item, *next_item;
4598
4599   /* The queue starts out with one item, but following a DIE reference
4600      may load a new CU, adding it to the end of the queue.  */
4601   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4602     {
4603       if (dwarf2_per_objfile->using_index
4604           ? !item->per_cu->v.quick->symtab
4605           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4606         process_full_comp_unit (item->per_cu);
4607
4608       item->per_cu->queued = 0;
4609       next_item = item->next;
4610       xfree (item);
4611     }
4612
4613   dwarf2_queue_tail = NULL;
4614 }
4615
4616 /* Free all allocated queue entries.  This function only releases anything if
4617    an error was thrown; if the queue was processed then it would have been
4618    freed as we went along.  */
4619
4620 static void
4621 dwarf2_release_queue (void *dummy)
4622 {
4623   struct dwarf2_queue_item *item, *last;
4624
4625   item = dwarf2_queue;
4626   while (item)
4627     {
4628       /* Anything still marked queued is likely to be in an
4629          inconsistent state, so discard it.  */
4630       if (item->per_cu->queued)
4631         {
4632           if (item->per_cu->cu != NULL)
4633             free_one_cached_comp_unit (item->per_cu->cu);
4634           item->per_cu->queued = 0;
4635         }
4636
4637       last = item;
4638       item = item->next;
4639       xfree (last);
4640     }
4641
4642   dwarf2_queue = dwarf2_queue_tail = NULL;
4643 }
4644
4645 /* Read in full symbols for PST, and anything it depends on.  */
4646
4647 static void
4648 psymtab_to_symtab_1 (struct partial_symtab *pst)
4649 {
4650   struct dwarf2_per_cu_data *per_cu;
4651   struct cleanup *back_to;
4652   int i;
4653
4654   for (i = 0; i < pst->number_of_dependencies; i++)
4655     if (!pst->dependencies[i]->readin)
4656       {
4657         /* Inform about additional files that need to be read in.  */
4658         if (info_verbose)
4659           {
4660             /* FIXME: i18n: Need to make this a single string.  */
4661             fputs_filtered (" ", gdb_stdout);
4662             wrap_here ("");
4663             fputs_filtered ("and ", gdb_stdout);
4664             wrap_here ("");
4665             printf_filtered ("%s...", pst->dependencies[i]->filename);
4666             wrap_here ("");     /* Flush output.  */
4667             gdb_flush (gdb_stdout);
4668           }
4669         psymtab_to_symtab_1 (pst->dependencies[i]);
4670       }
4671
4672   per_cu = pst->read_symtab_private;
4673
4674   if (per_cu == NULL)
4675     {
4676       /* It's an include file, no symbols to read for it.
4677          Everything is in the parent symtab.  */
4678       pst->readin = 1;
4679       return;
4680     }
4681
4682   dw2_do_instantiate_symtab (per_cu);
4683 }
4684
4685 /* Load the DIEs associated with PER_CU into memory.  */
4686
4687 static void
4688 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4689 {
4690   struct objfile *objfile = per_cu->objfile;
4691   bfd *abfd = objfile->obfd;
4692   struct dwarf2_cu *cu;
4693   unsigned int offset;
4694   gdb_byte *info_ptr, *beg_of_comp_unit;
4695   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4696   struct attribute *attr;
4697   int read_cu = 0;
4698
4699   gdb_assert (! per_cu->debug_types_section);
4700
4701   /* Set local variables from the partial symbol table info.  */
4702   offset = per_cu->offset;
4703
4704   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4705   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4706   beg_of_comp_unit = info_ptr;
4707
4708   if (per_cu->cu == NULL)
4709     {
4710       cu = xmalloc (sizeof (*cu));
4711       init_one_comp_unit (cu, per_cu);
4712
4713       read_cu = 1;
4714
4715       /* If an error occurs while loading, release our storage.  */
4716       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4717
4718       /* Read in the comp_unit header.  */
4719       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4720
4721       /* Skip dummy compilation units.  */
4722       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4723                        + dwarf2_per_objfile->info.size)
4724           || peek_abbrev_code (abfd, info_ptr) == 0)
4725         {
4726           do_cleanups (free_cu_cleanup);
4727           return;
4728         }
4729
4730       /* Complete the cu_header.  */
4731       cu->header.offset = offset;
4732       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4733
4734       /* Read the abbrevs for this compilation unit.  */
4735       dwarf2_read_abbrevs (abfd, cu);
4736       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4737
4738       /* Link this CU into read_in_chain.  */
4739       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4740       dwarf2_per_objfile->read_in_chain = per_cu;
4741     }
4742   else
4743     {
4744       cu = per_cu->cu;
4745       info_ptr += cu->header.first_die_offset;
4746     }
4747
4748   cu->dies = read_comp_unit (info_ptr, cu);
4749
4750   /* We try not to read any attributes in this function, because not
4751      all CUs needed for references have been loaded yet, and symbol
4752      table processing isn't initialized.  But we have to set the CU language,
4753      or we won't be able to build types correctly.  */
4754   prepare_one_comp_unit (cu, cu->dies);
4755
4756   /* Similarly, if we do not read the producer, we can not apply
4757      producer-specific interpretation.  */
4758   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4759   if (attr)
4760     cu->producer = DW_STRING (attr);
4761
4762   if (read_cu)
4763     {
4764       do_cleanups (free_abbrevs_cleanup);
4765
4766       /* We've successfully allocated this compilation unit.  Let our
4767          caller clean it up when finished with it.  */
4768       discard_cleanups (free_cu_cleanup);
4769     }
4770 }
4771
4772 /* Add a DIE to the delayed physname list.  */
4773
4774 static void
4775 add_to_method_list (struct type *type, int fnfield_index, int index,
4776                     const char *name, struct die_info *die,
4777                     struct dwarf2_cu *cu)
4778 {
4779   struct delayed_method_info mi;
4780   mi.type = type;
4781   mi.fnfield_index = fnfield_index;
4782   mi.index = index;
4783   mi.name = name;
4784   mi.die = die;
4785   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4786 }
4787
4788 /* A cleanup for freeing the delayed method list.  */
4789
4790 static void
4791 free_delayed_list (void *ptr)
4792 {
4793   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4794   if (cu->method_list != NULL)
4795     {
4796       VEC_free (delayed_method_info, cu->method_list);
4797       cu->method_list = NULL;
4798     }
4799 }
4800
4801 /* Compute the physnames of any methods on the CU's method list.
4802
4803    The computation of method physnames is delayed in order to avoid the
4804    (bad) condition that one of the method's formal parameters is of an as yet
4805    incomplete type.  */
4806
4807 static void
4808 compute_delayed_physnames (struct dwarf2_cu *cu)
4809 {
4810   int i;
4811   struct delayed_method_info *mi;
4812   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4813     {
4814       const char *physname;
4815       struct fn_fieldlist *fn_flp
4816         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4817       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4818       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4819     }
4820 }
4821
4822 /* Generate full symbol information for PER_CU, whose DIEs have
4823    already been loaded into memory.  */
4824
4825 static void
4826 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4827 {
4828   struct dwarf2_cu *cu = per_cu->cu;
4829   struct objfile *objfile = per_cu->objfile;
4830   CORE_ADDR lowpc, highpc;
4831   struct symtab *symtab;
4832   struct cleanup *back_to, *delayed_list_cleanup;
4833   CORE_ADDR baseaddr;
4834
4835   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4836
4837   buildsym_init ();
4838   back_to = make_cleanup (really_free_pendings, NULL);
4839   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4840
4841   cu->list_in_scope = &file_symbols;
4842
4843   /* Do line number decoding in read_file_scope () */
4844   process_die (cu->dies, cu);
4845
4846   /* Now that we have processed all the DIEs in the CU, all the types 
4847      should be complete, and it should now be safe to compute all of the
4848      physnames.  */
4849   compute_delayed_physnames (cu);
4850   do_cleanups (delayed_list_cleanup);
4851
4852   /* Some compilers don't define a DW_AT_high_pc attribute for the
4853      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4854      it, by scanning the DIE's below the compilation unit.  */
4855   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4856
4857   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4858
4859   if (symtab != NULL)
4860     {
4861       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4862
4863       /* Set symtab language to language from DW_AT_language.  If the
4864          compilation is from a C file generated by language preprocessors, do
4865          not set the language if it was already deduced by start_subfile.  */
4866       if (!(cu->language == language_c && symtab->language != language_c))
4867         symtab->language = cu->language;
4868
4869       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4870          produce DW_AT_location with location lists but it can be possibly
4871          invalid without -fvar-tracking.
4872
4873          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4874          needed, it would be wrong due to missing DW_AT_producer there.
4875
4876          Still one can confuse GDB by using non-standard GCC compilation
4877          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4878          */ 
4879       if (cu->has_loclist && gcc_4_minor >= 0)
4880         symtab->locations_valid = 1;
4881
4882       if (gcc_4_minor >= 5)
4883         symtab->epilogue_unwind_valid = 1;
4884
4885       symtab->call_site_htab = cu->call_site_htab;
4886     }
4887
4888   if (dwarf2_per_objfile->using_index)
4889     per_cu->v.quick->symtab = symtab;
4890   else
4891     {
4892       struct partial_symtab *pst = per_cu->v.psymtab;
4893       pst->symtab = symtab;
4894       pst->readin = 1;
4895     }
4896
4897   do_cleanups (back_to);
4898 }
4899
4900 /* Process a die and its children.  */
4901
4902 static void
4903 process_die (struct die_info *die, struct dwarf2_cu *cu)
4904 {
4905   switch (die->tag)
4906     {
4907     case DW_TAG_padding:
4908       break;
4909     case DW_TAG_compile_unit:
4910       read_file_scope (die, cu);
4911       break;
4912     case DW_TAG_type_unit:
4913       read_type_unit_scope (die, cu);
4914       break;
4915     case DW_TAG_subprogram:
4916     case DW_TAG_inlined_subroutine:
4917       read_func_scope (die, cu);
4918       break;
4919     case DW_TAG_lexical_block:
4920     case DW_TAG_try_block:
4921     case DW_TAG_catch_block:
4922       read_lexical_block_scope (die, cu);
4923       break;
4924     case DW_TAG_GNU_call_site:
4925       read_call_site_scope (die, cu);
4926       break;
4927     case DW_TAG_class_type:
4928     case DW_TAG_interface_type:
4929     case DW_TAG_structure_type:
4930     case DW_TAG_union_type:
4931       process_structure_scope (die, cu);
4932       break;
4933     case DW_TAG_enumeration_type:
4934       process_enumeration_scope (die, cu);
4935       break;
4936
4937     /* These dies have a type, but processing them does not create
4938        a symbol or recurse to process the children.  Therefore we can
4939        read them on-demand through read_type_die.  */
4940     case DW_TAG_subroutine_type:
4941     case DW_TAG_set_type:
4942     case DW_TAG_array_type:
4943     case DW_TAG_pointer_type:
4944     case DW_TAG_ptr_to_member_type:
4945     case DW_TAG_reference_type:
4946     case DW_TAG_string_type:
4947       break;
4948
4949     case DW_TAG_base_type:
4950     case DW_TAG_subrange_type:
4951     case DW_TAG_typedef:
4952       /* Add a typedef symbol for the type definition, if it has a
4953          DW_AT_name.  */
4954       new_symbol (die, read_type_die (die, cu), cu);
4955       break;
4956     case DW_TAG_common_block:
4957       read_common_block (die, cu);
4958       break;
4959     case DW_TAG_common_inclusion:
4960       break;
4961     case DW_TAG_namespace:
4962       processing_has_namespace_info = 1;
4963       read_namespace (die, cu);
4964       break;
4965     case DW_TAG_module:
4966       processing_has_namespace_info = 1;
4967       read_module (die, cu);
4968       break;
4969     case DW_TAG_imported_declaration:
4970     case DW_TAG_imported_module:
4971       processing_has_namespace_info = 1;
4972       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4973                                  || cu->language != language_fortran))
4974         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4975                    dwarf_tag_name (die->tag));
4976       read_import_statement (die, cu);
4977       break;
4978     default:
4979       new_symbol (die, NULL, cu);
4980       break;
4981     }
4982 }
4983
4984 /* A helper function for dwarf2_compute_name which determines whether DIE
4985    needs to have the name of the scope prepended to the name listed in the
4986    die.  */
4987
4988 static int
4989 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4990 {
4991   struct attribute *attr;
4992
4993   switch (die->tag)
4994     {
4995     case DW_TAG_namespace:
4996     case DW_TAG_typedef:
4997     case DW_TAG_class_type:
4998     case DW_TAG_interface_type:
4999     case DW_TAG_structure_type:
5000     case DW_TAG_union_type:
5001     case DW_TAG_enumeration_type:
5002     case DW_TAG_enumerator:
5003     case DW_TAG_subprogram:
5004     case DW_TAG_member:
5005       return 1;
5006
5007     case DW_TAG_variable:
5008     case DW_TAG_constant:
5009       /* We only need to prefix "globally" visible variables.  These include
5010          any variable marked with DW_AT_external or any variable that
5011          lives in a namespace.  [Variables in anonymous namespaces
5012          require prefixing, but they are not DW_AT_external.]  */
5013
5014       if (dwarf2_attr (die, DW_AT_specification, cu))
5015         {
5016           struct dwarf2_cu *spec_cu = cu;
5017
5018           return die_needs_namespace (die_specification (die, &spec_cu),
5019                                       spec_cu);
5020         }
5021
5022       attr = dwarf2_attr (die, DW_AT_external, cu);
5023       if (attr == NULL && die->parent->tag != DW_TAG_namespace
5024           && die->parent->tag != DW_TAG_module)
5025         return 0;
5026       /* A variable in a lexical block of some kind does not need a
5027          namespace, even though in C++ such variables may be external
5028          and have a mangled name.  */
5029       if (die->parent->tag ==  DW_TAG_lexical_block
5030           || die->parent->tag ==  DW_TAG_try_block
5031           || die->parent->tag ==  DW_TAG_catch_block
5032           || die->parent->tag == DW_TAG_subprogram)
5033         return 0;
5034       return 1;
5035
5036     default:
5037       return 0;
5038     }
5039 }
5040
5041 /* Retrieve the last character from a mem_file.  */
5042
5043 static void
5044 do_ui_file_peek_last (void *object, const char *buffer, long length)
5045 {
5046   char *last_char_p = (char *) object;
5047
5048   if (length > 0)
5049     *last_char_p = buffer[length - 1];
5050 }
5051
5052 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
5053    compute the physname for the object, which include a method's
5054    formal parameters (C++/Java) and return type (Java).
5055
5056    For Ada, return the DIE's linkage name rather than the fully qualified
5057    name.  PHYSNAME is ignored..
5058
5059    The result is allocated on the objfile_obstack and canonicalized.  */
5060
5061 static const char *
5062 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5063                      int physname)
5064 {
5065   struct objfile *objfile = cu->objfile;
5066
5067   if (name == NULL)
5068     name = dwarf2_name (die, cu);
5069
5070   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5071      compute it by typename_concat inside GDB.  */
5072   if (cu->language == language_ada
5073       || (cu->language == language_fortran && physname))
5074     {
5075       /* For Ada unit, we prefer the linkage name over the name, as
5076          the former contains the exported name, which the user expects
5077          to be able to reference.  Ideally, we want the user to be able
5078          to reference this entity using either natural or linkage name,
5079          but we haven't started looking at this enhancement yet.  */
5080       struct attribute *attr;
5081
5082       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5083       if (attr == NULL)
5084         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5085       if (attr && DW_STRING (attr))
5086         return DW_STRING (attr);
5087     }
5088
5089   /* These are the only languages we know how to qualify names in.  */
5090   if (name != NULL
5091       && (cu->language == language_cplus || cu->language == language_java
5092           || cu->language == language_fortran))
5093     {
5094       if (die_needs_namespace (die, cu))
5095         {
5096           long length;
5097           char *prefix;
5098           struct ui_file *buf;
5099
5100           prefix = determine_prefix (die, cu);
5101           buf = mem_fileopen ();
5102           if (*prefix != '\0')
5103             {
5104               char *prefixed_name = typename_concat (NULL, prefix, name,
5105                                                      physname, cu);
5106
5107               fputs_unfiltered (prefixed_name, buf);
5108               xfree (prefixed_name);
5109             }
5110           else
5111             fputs_unfiltered (name, buf);
5112
5113           /* Template parameters may be specified in the DIE's DW_AT_name, or
5114              as children with DW_TAG_template_type_param or
5115              DW_TAG_value_type_param.  If the latter, add them to the name
5116              here.  If the name already has template parameters, then
5117              skip this step; some versions of GCC emit both, and
5118              it is more efficient to use the pre-computed name.
5119
5120              Something to keep in mind about this process: it is very
5121              unlikely, or in some cases downright impossible, to produce
5122              something that will match the mangled name of a function.
5123              If the definition of the function has the same debug info,
5124              we should be able to match up with it anyway.  But fallbacks
5125              using the minimal symbol, for instance to find a method
5126              implemented in a stripped copy of libstdc++, will not work.
5127              If we do not have debug info for the definition, we will have to
5128              match them up some other way.
5129
5130              When we do name matching there is a related problem with function
5131              templates; two instantiated function templates are allowed to
5132              differ only by their return types, which we do not add here.  */
5133
5134           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5135             {
5136               struct attribute *attr;
5137               struct die_info *child;
5138               int first = 1;
5139
5140               die->building_fullname = 1;
5141
5142               for (child = die->child; child != NULL; child = child->sibling)
5143                 {
5144                   struct type *type;
5145                   long value;
5146                   gdb_byte *bytes;
5147                   struct dwarf2_locexpr_baton *baton;
5148                   struct value *v;
5149
5150                   if (child->tag != DW_TAG_template_type_param
5151                       && child->tag != DW_TAG_template_value_param)
5152                     continue;
5153
5154                   if (first)
5155                     {
5156                       fputs_unfiltered ("<", buf);
5157                       first = 0;
5158                     }
5159                   else
5160                     fputs_unfiltered (", ", buf);
5161
5162                   attr = dwarf2_attr (child, DW_AT_type, cu);
5163                   if (attr == NULL)
5164                     {
5165                       complaint (&symfile_complaints,
5166                                  _("template parameter missing DW_AT_type"));
5167                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5168                       continue;
5169                     }
5170                   type = die_type (child, cu);
5171
5172                   if (child->tag == DW_TAG_template_type_param)
5173                     {
5174                       c_print_type (type, "", buf, -1, 0);
5175                       continue;
5176                     }
5177
5178                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5179                   if (attr == NULL)
5180                     {
5181                       complaint (&symfile_complaints,
5182                                  _("template parameter missing "
5183                                    "DW_AT_const_value"));
5184                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5185                       continue;
5186                     }
5187
5188                   dwarf2_const_value_attr (attr, type, name,
5189                                            &cu->comp_unit_obstack, cu,
5190                                            &value, &bytes, &baton);
5191
5192                   if (TYPE_NOSIGN (type))
5193                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5194                        changed, this can use value_print instead.  */
5195                     c_printchar (value, type, buf);
5196                   else
5197                     {
5198                       struct value_print_options opts;
5199
5200                       if (baton != NULL)
5201                         v = dwarf2_evaluate_loc_desc (type, NULL,
5202                                                       baton->data,
5203                                                       baton->size,
5204                                                       baton->per_cu);
5205                       else if (bytes != NULL)
5206                         {
5207                           v = allocate_value (type);
5208                           memcpy (value_contents_writeable (v), bytes,
5209                                   TYPE_LENGTH (type));
5210                         }
5211                       else
5212                         v = value_from_longest (type, value);
5213
5214                       /* Specify decimal so that we do not depend on
5215                          the radix.  */
5216                       get_formatted_print_options (&opts, 'd');
5217                       opts.raw = 1;
5218                       value_print (v, buf, &opts);
5219                       release_value (v);
5220                       value_free (v);
5221                     }
5222                 }
5223
5224               die->building_fullname = 0;
5225
5226               if (!first)
5227                 {
5228                   /* Close the argument list, with a space if necessary
5229                      (nested templates).  */
5230                   char last_char = '\0';
5231                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5232                   if (last_char == '>')
5233                     fputs_unfiltered (" >", buf);
5234                   else
5235                     fputs_unfiltered (">", buf);
5236                 }
5237             }
5238
5239           /* For Java and C++ methods, append formal parameter type
5240              information, if PHYSNAME.  */
5241
5242           if (physname && die->tag == DW_TAG_subprogram
5243               && (cu->language == language_cplus
5244                   || cu->language == language_java))
5245             {
5246               struct type *type = read_type_die (die, cu);
5247
5248               c_type_print_args (type, buf, 1, cu->language);
5249
5250               if (cu->language == language_java)
5251                 {
5252                   /* For java, we must append the return type to method
5253                      names.  */
5254                   if (die->tag == DW_TAG_subprogram)
5255                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5256                                      0, 0);
5257                 }
5258               else if (cu->language == language_cplus)
5259                 {
5260                   /* Assume that an artificial first parameter is
5261                      "this", but do not crash if it is not.  RealView
5262                      marks unnamed (and thus unused) parameters as
5263                      artificial; there is no way to differentiate
5264                      the two cases.  */
5265                   if (TYPE_NFIELDS (type) > 0
5266                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5267                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5268                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5269                                                                         0))))
5270                     fputs_unfiltered (" const", buf);
5271                 }
5272             }
5273
5274           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5275                                        &length);
5276           ui_file_delete (buf);
5277
5278           if (cu->language == language_cplus)
5279             {
5280               char *cname
5281                 = dwarf2_canonicalize_name (name, cu,
5282                                             &objfile->objfile_obstack);
5283
5284               if (cname != NULL)
5285                 name = cname;
5286             }
5287         }
5288     }
5289
5290   return name;
5291 }
5292
5293 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5294    If scope qualifiers are appropriate they will be added.  The result
5295    will be allocated on the objfile_obstack, or NULL if the DIE does
5296    not have a name.  NAME may either be from a previous call to
5297    dwarf2_name or NULL.
5298
5299    The output string will be canonicalized (if C++/Java).  */
5300
5301 static const char *
5302 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5303 {
5304   return dwarf2_compute_name (name, die, cu, 0);
5305 }
5306
5307 /* Construct a physname for the given DIE in CU.  NAME may either be
5308    from a previous call to dwarf2_name or NULL.  The result will be
5309    allocated on the objfile_objstack or NULL if the DIE does not have a
5310    name.
5311
5312    The output string will be canonicalized (if C++/Java).  */
5313
5314 static const char *
5315 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5316 {
5317   struct objfile *objfile = cu->objfile;
5318   struct attribute *attr;
5319   const char *retval, *mangled = NULL, *canon = NULL;
5320   struct cleanup *back_to;
5321   int need_copy = 1;
5322
5323   /* In this case dwarf2_compute_name is just a shortcut not building anything
5324      on its own.  */
5325   if (!die_needs_namespace (die, cu))
5326     return dwarf2_compute_name (name, die, cu, 1);
5327
5328   back_to = make_cleanup (null_cleanup, NULL);
5329
5330   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5331   if (!attr)
5332     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5333
5334   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5335      has computed.  */
5336   if (attr && DW_STRING (attr))
5337     {
5338       char *demangled;
5339
5340       mangled = DW_STRING (attr);
5341
5342       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5343          type.  It is easier for GDB users to search for such functions as
5344          `name(params)' than `long name(params)'.  In such case the minimal
5345          symbol names do not match the full symbol names but for template
5346          functions there is never a need to look up their definition from their
5347          declaration so the only disadvantage remains the minimal symbol
5348          variant `long name(params)' does not have the proper inferior type.
5349          */
5350
5351       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5352                                             | (cu->language == language_java
5353                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5354                                                : DMGL_RET_DROP)));
5355       if (demangled)
5356         {
5357           make_cleanup (xfree, demangled);
5358           canon = demangled;
5359         }
5360       else
5361         {
5362           canon = mangled;
5363           need_copy = 0;
5364         }
5365     }
5366
5367   if (canon == NULL || check_physname)
5368     {
5369       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5370
5371       if (canon != NULL && strcmp (physname, canon) != 0)
5372         {
5373           /* It may not mean a bug in GDB.  The compiler could also
5374              compute DW_AT_linkage_name incorrectly.  But in such case
5375              GDB would need to be bug-to-bug compatible.  */
5376
5377           complaint (&symfile_complaints,
5378                      _("Computed physname <%s> does not match demangled <%s> "
5379                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5380                      physname, canon, mangled, die->offset, objfile->name);
5381
5382           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5383              is available here - over computed PHYSNAME.  It is safer
5384              against both buggy GDB and buggy compilers.  */
5385
5386           retval = canon;
5387         }
5388       else
5389         {
5390           retval = physname;
5391           need_copy = 0;
5392         }
5393     }
5394   else
5395     retval = canon;
5396
5397   if (need_copy)
5398     retval = obsavestring (retval, strlen (retval),
5399                            &objfile->objfile_obstack);
5400
5401   do_cleanups (back_to);
5402   return retval;
5403 }
5404
5405 /* Read the import statement specified by the given die and record it.  */
5406
5407 static void
5408 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5409 {
5410   struct objfile *objfile = cu->objfile;
5411   struct attribute *import_attr;
5412   struct die_info *imported_die, *child_die;
5413   struct dwarf2_cu *imported_cu;
5414   const char *imported_name;
5415   const char *imported_name_prefix;
5416   const char *canonical_name;
5417   const char *import_alias;
5418   const char *imported_declaration = NULL;
5419   const char *import_prefix;
5420   VEC (const_char_ptr) *excludes = NULL;
5421   struct cleanup *cleanups;
5422
5423   char *temp;
5424
5425   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5426   if (import_attr == NULL)
5427     {
5428       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5429                  dwarf_tag_name (die->tag));
5430       return;
5431     }
5432
5433   imported_cu = cu;
5434   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5435   imported_name = dwarf2_name (imported_die, imported_cu);
5436   if (imported_name == NULL)
5437     {
5438       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5439
5440         The import in the following code:
5441         namespace A
5442           {
5443             typedef int B;
5444           }
5445
5446         int main ()
5447           {
5448             using A::B;
5449             B b;
5450             return b;
5451           }
5452
5453         ...
5454          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5455             <52>   DW_AT_decl_file   : 1
5456             <53>   DW_AT_decl_line   : 6
5457             <54>   DW_AT_import      : <0x75>
5458          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5459             <59>   DW_AT_name        : B
5460             <5b>   DW_AT_decl_file   : 1
5461             <5c>   DW_AT_decl_line   : 2
5462             <5d>   DW_AT_type        : <0x6e>
5463         ...
5464          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5465             <76>   DW_AT_byte_size   : 4
5466             <77>   DW_AT_encoding    : 5        (signed)
5467
5468         imports the wrong die ( 0x75 instead of 0x58 ).
5469         This case will be ignored until the gcc bug is fixed.  */
5470       return;
5471     }
5472
5473   /* Figure out the local name after import.  */
5474   import_alias = dwarf2_name (die, cu);
5475
5476   /* Figure out where the statement is being imported to.  */
5477   import_prefix = determine_prefix (die, cu);
5478
5479   /* Figure out what the scope of the imported die is and prepend it
5480      to the name of the imported die.  */
5481   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5482
5483   if (imported_die->tag != DW_TAG_namespace
5484       && imported_die->tag != DW_TAG_module)
5485     {
5486       imported_declaration = imported_name;
5487       canonical_name = imported_name_prefix;
5488     }
5489   else if (strlen (imported_name_prefix) > 0)
5490     {
5491       temp = alloca (strlen (imported_name_prefix)
5492                      + 2 + strlen (imported_name) + 1);
5493       strcpy (temp, imported_name_prefix);
5494       strcat (temp, "::");
5495       strcat (temp, imported_name);
5496       canonical_name = temp;
5497     }
5498   else
5499     canonical_name = imported_name;
5500
5501   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5502
5503   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5504     for (child_die = die->child; child_die && child_die->tag;
5505          child_die = sibling_die (child_die))
5506       {
5507         /* DWARF-4: A Fortran use statement with a “rename list” may be
5508            represented by an imported module entry with an import attribute
5509            referring to the module and owned entries corresponding to those
5510            entities that are renamed as part of being imported.  */
5511
5512         if (child_die->tag != DW_TAG_imported_declaration)
5513           {
5514             complaint (&symfile_complaints,
5515                        _("child DW_TAG_imported_declaration expected "
5516                          "- DIE at 0x%x [in module %s]"),
5517                        child_die->offset, objfile->name);
5518             continue;
5519           }
5520
5521         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5522         if (import_attr == NULL)
5523           {
5524             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5525                        dwarf_tag_name (child_die->tag));
5526             continue;
5527           }
5528
5529         imported_cu = cu;
5530         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5531                                               &imported_cu);
5532         imported_name = dwarf2_name (imported_die, imported_cu);
5533         if (imported_name == NULL)
5534           {
5535             complaint (&symfile_complaints,
5536                        _("child DW_TAG_imported_declaration has unknown "
5537                          "imported name - DIE at 0x%x [in module %s]"),
5538                        child_die->offset, objfile->name);
5539             continue;
5540           }
5541
5542         VEC_safe_push (const_char_ptr, excludes, imported_name);
5543
5544         process_die (child_die, cu);
5545       }
5546
5547   cp_add_using_directive (import_prefix,
5548                           canonical_name,
5549                           import_alias,
5550                           imported_declaration,
5551                           excludes,
5552                           &objfile->objfile_obstack);
5553
5554   do_cleanups (cleanups);
5555 }
5556
5557 /* Cleanup function for read_file_scope.  */
5558
5559 static void
5560 free_cu_line_header (void *arg)
5561 {
5562   struct dwarf2_cu *cu = arg;
5563
5564   free_line_header (cu->line_header);
5565   cu->line_header = NULL;
5566 }
5567
5568 static void
5569 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5570                          char **name, char **comp_dir)
5571 {
5572   struct attribute *attr;
5573
5574   *name = NULL;
5575   *comp_dir = NULL;
5576
5577   /* Find the filename.  Do not use dwarf2_name here, since the filename
5578      is not a source language identifier.  */
5579   attr = dwarf2_attr (die, DW_AT_name, cu);
5580   if (attr)
5581     {
5582       *name = DW_STRING (attr);
5583     }
5584
5585   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5586   if (attr)
5587     *comp_dir = DW_STRING (attr);
5588   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5589     {
5590       *comp_dir = ldirname (*name);
5591       if (*comp_dir != NULL)
5592         make_cleanup (xfree, *comp_dir);
5593     }
5594   if (*comp_dir != NULL)
5595     {
5596       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5597          directory, get rid of it.  */
5598       char *cp = strchr (*comp_dir, ':');
5599
5600       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5601         *comp_dir = cp + 1;
5602     }
5603
5604   if (*name == NULL)
5605     *name = "<unknown>";
5606 }
5607
5608 /* Handle DW_AT_stmt_list for a compilation unit.  */
5609
5610 static void
5611 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5612                         const char *comp_dir)
5613 {
5614   struct attribute *attr;
5615   struct objfile *objfile = cu->objfile;
5616   bfd *abfd = objfile->obfd;
5617
5618   /* Decode line number information if present.  We do this before
5619      processing child DIEs, so that the line header table is available
5620      for DW_AT_decl_file.  */
5621   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5622   if (attr)
5623     {
5624       unsigned int line_offset = DW_UNSND (attr);
5625       struct line_header *line_header
5626         = dwarf_decode_line_header (line_offset, abfd, cu);
5627
5628       if (line_header)
5629         {
5630           cu->line_header = line_header;
5631           make_cleanup (free_cu_line_header, cu);
5632           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5633         }
5634     }
5635 }
5636
5637 /* Process DW_TAG_compile_unit.  */
5638
5639 static void
5640 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5641 {
5642   struct objfile *objfile = cu->objfile;
5643   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5644   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5645   CORE_ADDR highpc = ((CORE_ADDR) 0);
5646   struct attribute *attr;
5647   char *name = NULL;
5648   char *comp_dir = NULL;
5649   struct die_info *child_die;
5650   bfd *abfd = objfile->obfd;
5651   CORE_ADDR baseaddr;
5652
5653   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5654
5655   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5656
5657   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5658      from finish_block.  */
5659   if (lowpc == ((CORE_ADDR) -1))
5660     lowpc = highpc;
5661   lowpc += baseaddr;
5662   highpc += baseaddr;
5663
5664   find_file_and_directory (die, cu, &name, &comp_dir);
5665
5666   attr = dwarf2_attr (die, DW_AT_language, cu);
5667   if (attr)
5668     {
5669       set_cu_language (DW_UNSND (attr), cu);
5670     }
5671
5672   attr = dwarf2_attr (die, DW_AT_producer, cu);
5673   if (attr)
5674     cu->producer = DW_STRING (attr);
5675
5676   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5677      standardised yet.  As a workaround for the language detection we fall
5678      back to the DW_AT_producer string.  */
5679   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5680     cu->language = language_opencl;
5681
5682   /* We assume that we're processing GCC output.  */
5683   processing_gcc_compilation = 2;
5684
5685   processing_has_namespace_info = 0;
5686
5687   start_symtab (name, comp_dir, lowpc);
5688   record_debugformat ("DWARF 2");
5689   record_producer (cu->producer);
5690
5691   handle_DW_AT_stmt_list (die, cu, comp_dir);
5692
5693   /* Process all dies in compilation unit.  */
5694   if (die->child != NULL)
5695     {
5696       child_die = die->child;
5697       while (child_die && child_die->tag)
5698         {
5699           process_die (child_die, cu);
5700           child_die = sibling_die (child_die);
5701         }
5702     }
5703
5704   /* Decode macro information, if present.  Dwarf 2 macro information
5705      refers to information in the line number info statement program
5706      header, so we can only read it if we've read the header
5707      successfully.  */
5708   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5709   if (attr && cu->line_header)
5710     {
5711       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5712         complaint (&symfile_complaints,
5713                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5714
5715       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5716                            comp_dir, abfd, cu,
5717                            &dwarf2_per_objfile->macro, 1);
5718     }
5719   else
5720     {
5721       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5722       if (attr && cu->line_header)
5723         {
5724           unsigned int macro_offset = DW_UNSND (attr);
5725
5726           dwarf_decode_macros (cu->line_header, macro_offset,
5727                                comp_dir, abfd, cu,
5728                                &dwarf2_per_objfile->macinfo, 0);
5729         }
5730     }
5731
5732   do_cleanups (back_to);
5733 }
5734
5735 /* Process DW_TAG_type_unit.
5736    For TUs we want to skip the first top level sibling if it's not the
5737    actual type being defined by this TU.  In this case the first top
5738    level sibling is there to provide context only.  */
5739
5740 static void
5741 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5742 {
5743   struct objfile *objfile = cu->objfile;
5744   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5745   CORE_ADDR lowpc;
5746   struct attribute *attr;
5747   char *name = NULL;
5748   char *comp_dir = NULL;
5749   struct die_info *child_die;
5750   bfd *abfd = objfile->obfd;
5751
5752   /* start_symtab needs a low pc, but we don't really have one.
5753      Do what read_file_scope would do in the absence of such info.  */
5754   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5755
5756   /* Find the filename.  Do not use dwarf2_name here, since the filename
5757      is not a source language identifier.  */
5758   attr = dwarf2_attr (die, DW_AT_name, cu);
5759   if (attr)
5760     name = DW_STRING (attr);
5761
5762   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5763   if (attr)
5764     comp_dir = DW_STRING (attr);
5765   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5766     {
5767       comp_dir = ldirname (name);
5768       if (comp_dir != NULL)
5769         make_cleanup (xfree, comp_dir);
5770     }
5771
5772   if (name == NULL)
5773     name = "<unknown>";
5774
5775   attr = dwarf2_attr (die, DW_AT_language, cu);
5776   if (attr)
5777     set_cu_language (DW_UNSND (attr), cu);
5778
5779   /* This isn't technically needed today.  It is done for symmetry
5780      with read_file_scope.  */
5781   attr = dwarf2_attr (die, DW_AT_producer, cu);
5782   if (attr)
5783     cu->producer = DW_STRING (attr);
5784
5785   /* We assume that we're processing GCC output.  */
5786   processing_gcc_compilation = 2;
5787
5788   processing_has_namespace_info = 0;
5789
5790   start_symtab (name, comp_dir, lowpc);
5791   record_debugformat ("DWARF 2");
5792   record_producer (cu->producer);
5793
5794   handle_DW_AT_stmt_list (die, cu, comp_dir);
5795
5796   /* Process the dies in the type unit.  */
5797   if (die->child == NULL)
5798     {
5799       dump_die_for_error (die);
5800       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5801              bfd_get_filename (abfd));
5802     }
5803
5804   child_die = die->child;
5805
5806   while (child_die && child_die->tag)
5807     {
5808       process_die (child_die, cu);
5809
5810       child_die = sibling_die (child_die);
5811     }
5812
5813   do_cleanups (back_to);
5814 }
5815
5816 /* qsort helper for inherit_abstract_dies.  */
5817
5818 static int
5819 unsigned_int_compar (const void *ap, const void *bp)
5820 {
5821   unsigned int a = *(unsigned int *) ap;
5822   unsigned int b = *(unsigned int *) bp;
5823
5824   return (a > b) - (b > a);
5825 }
5826
5827 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5828    Inherit only the children of the DW_AT_abstract_origin DIE not being
5829    already referenced by DW_AT_abstract_origin from the children of the
5830    current DIE.  */
5831
5832 static void
5833 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5834 {
5835   struct die_info *child_die;
5836   unsigned die_children_count;
5837   /* CU offsets which were referenced by children of the current DIE.  */
5838   unsigned *offsets;
5839   unsigned *offsets_end, *offsetp;
5840   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5841   struct die_info *origin_die;
5842   /* Iterator of the ORIGIN_DIE children.  */
5843   struct die_info *origin_child_die;
5844   struct cleanup *cleanups;
5845   struct attribute *attr;
5846   struct dwarf2_cu *origin_cu;
5847   struct pending **origin_previous_list_in_scope;
5848
5849   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5850   if (!attr)
5851     return;
5852
5853   /* Note that following die references may follow to a die in a
5854      different cu.  */
5855
5856   origin_cu = cu;
5857   origin_die = follow_die_ref (die, attr, &origin_cu);
5858
5859   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5860      symbols in.  */
5861   origin_previous_list_in_scope = origin_cu->list_in_scope;
5862   origin_cu->list_in_scope = cu->list_in_scope;
5863
5864   if (die->tag != origin_die->tag
5865       && !(die->tag == DW_TAG_inlined_subroutine
5866            && origin_die->tag == DW_TAG_subprogram))
5867     complaint (&symfile_complaints,
5868                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5869                die->offset, origin_die->offset);
5870
5871   child_die = die->child;
5872   die_children_count = 0;
5873   while (child_die && child_die->tag)
5874     {
5875       child_die = sibling_die (child_die);
5876       die_children_count++;
5877     }
5878   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5879   cleanups = make_cleanup (xfree, offsets);
5880
5881   offsets_end = offsets;
5882   child_die = die->child;
5883   while (child_die && child_die->tag)
5884     {
5885       /* For each CHILD_DIE, find the corresponding child of
5886          ORIGIN_DIE.  If there is more than one layer of
5887          DW_AT_abstract_origin, follow them all; there shouldn't be,
5888          but GCC versions at least through 4.4 generate this (GCC PR
5889          40573).  */
5890       struct die_info *child_origin_die = child_die;
5891       struct dwarf2_cu *child_origin_cu = cu;
5892
5893       while (1)
5894         {
5895           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5896                               child_origin_cu);
5897           if (attr == NULL)
5898             break;
5899           child_origin_die = follow_die_ref (child_origin_die, attr,
5900                                              &child_origin_cu);
5901         }
5902
5903       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5904          counterpart may exist.  */
5905       if (child_origin_die != child_die)
5906         {
5907           if (child_die->tag != child_origin_die->tag
5908               && !(child_die->tag == DW_TAG_inlined_subroutine
5909                    && child_origin_die->tag == DW_TAG_subprogram))
5910             complaint (&symfile_complaints,
5911                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5912                          "different tags"), child_die->offset,
5913                        child_origin_die->offset);
5914           if (child_origin_die->parent != origin_die)
5915             complaint (&symfile_complaints,
5916                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5917                          "different parents"), child_die->offset,
5918                        child_origin_die->offset);
5919           else
5920             *offsets_end++ = child_origin_die->offset;
5921         }
5922       child_die = sibling_die (child_die);
5923     }
5924   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5925          unsigned_int_compar);
5926   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5927     if (offsetp[-1] == *offsetp)
5928       complaint (&symfile_complaints,
5929                  _("Multiple children of DIE 0x%x refer "
5930                    "to DIE 0x%x as their abstract origin"),
5931                  die->offset, *offsetp);
5932
5933   offsetp = offsets;
5934   origin_child_die = origin_die->child;
5935   while (origin_child_die && origin_child_die->tag)
5936     {
5937       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5938       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5939         offsetp++;
5940       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5941         {
5942           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5943           process_die (origin_child_die, origin_cu);
5944         }
5945       origin_child_die = sibling_die (origin_child_die);
5946     }
5947   origin_cu->list_in_scope = origin_previous_list_in_scope;
5948
5949   do_cleanups (cleanups);
5950 }
5951
5952 static void
5953 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5954 {
5955   struct objfile *objfile = cu->objfile;
5956   struct context_stack *new;
5957   CORE_ADDR lowpc;
5958   CORE_ADDR highpc;
5959   struct die_info *child_die;
5960   struct attribute *attr, *call_line, *call_file;
5961   char *name;
5962   CORE_ADDR baseaddr;
5963   struct block *block;
5964   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5965   VEC (symbolp) *template_args = NULL;
5966   struct template_symbol *templ_func = NULL;
5967
5968   if (inlined_func)
5969     {
5970       /* If we do not have call site information, we can't show the
5971          caller of this inlined function.  That's too confusing, so
5972          only use the scope for local variables.  */
5973       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5974       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5975       if (call_line == NULL || call_file == NULL)
5976         {
5977           read_lexical_block_scope (die, cu);
5978           return;
5979         }
5980     }
5981
5982   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5983
5984   name = dwarf2_name (die, cu);
5985
5986   /* Ignore functions with missing or empty names.  These are actually
5987      illegal according to the DWARF standard.  */
5988   if (name == NULL)
5989     {
5990       complaint (&symfile_complaints,
5991                  _("missing name for subprogram DIE at %d"), die->offset);
5992       return;
5993     }
5994
5995   /* Ignore functions with missing or invalid low and high pc attributes.  */
5996   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5997     {
5998       attr = dwarf2_attr (die, DW_AT_external, cu);
5999       if (!attr || !DW_UNSND (attr))
6000         complaint (&symfile_complaints,
6001                    _("cannot get low and high bounds "
6002                      "for subprogram DIE at %d"),
6003                    die->offset);
6004       return;
6005     }
6006
6007   lowpc += baseaddr;
6008   highpc += baseaddr;
6009
6010   /* If we have any template arguments, then we must allocate a
6011      different sort of symbol.  */
6012   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6013     {
6014       if (child_die->tag == DW_TAG_template_type_param
6015           || child_die->tag == DW_TAG_template_value_param)
6016         {
6017           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6018                                        struct template_symbol);
6019           templ_func->base.is_cplus_template_function = 1;
6020           break;
6021         }
6022     }
6023
6024   new = push_context (0, lowpc);
6025   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6026                                (struct symbol *) templ_func);
6027
6028   /* If there is a location expression for DW_AT_frame_base, record
6029      it.  */
6030   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6031   if (attr)
6032     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6033        expression is being recorded directly in the function's symbol
6034        and not in a separate frame-base object.  I guess this hack is
6035        to avoid adding some sort of frame-base adjunct/annex to the
6036        function's symbol :-(.  The problem with doing this is that it
6037        results in a function symbol with a location expression that
6038        has nothing to do with the location of the function, ouch!  The
6039        relationship should be: a function's symbol has-a frame base; a
6040        frame-base has-a location expression.  */
6041     dwarf2_symbol_mark_computed (attr, new->name, cu);
6042
6043   cu->list_in_scope = &local_symbols;
6044
6045   if (die->child != NULL)
6046     {
6047       child_die = die->child;
6048       while (child_die && child_die->tag)
6049         {
6050           if (child_die->tag == DW_TAG_template_type_param
6051               || child_die->tag == DW_TAG_template_value_param)
6052             {
6053               struct symbol *arg = new_symbol (child_die, NULL, cu);
6054
6055               if (arg != NULL)
6056                 VEC_safe_push (symbolp, template_args, arg);
6057             }
6058           else
6059             process_die (child_die, cu);
6060           child_die = sibling_die (child_die);
6061         }
6062     }
6063
6064   inherit_abstract_dies (die, cu);
6065
6066   /* If we have a DW_AT_specification, we might need to import using
6067      directives from the context of the specification DIE.  See the
6068      comment in determine_prefix.  */
6069   if (cu->language == language_cplus
6070       && dwarf2_attr (die, DW_AT_specification, cu))
6071     {
6072       struct dwarf2_cu *spec_cu = cu;
6073       struct die_info *spec_die = die_specification (die, &spec_cu);
6074
6075       while (spec_die)
6076         {
6077           child_die = spec_die->child;
6078           while (child_die && child_die->tag)
6079             {
6080               if (child_die->tag == DW_TAG_imported_module)
6081                 process_die (child_die, spec_cu);
6082               child_die = sibling_die (child_die);
6083             }
6084
6085           /* In some cases, GCC generates specification DIEs that
6086              themselves contain DW_AT_specification attributes.  */
6087           spec_die = die_specification (spec_die, &spec_cu);
6088         }
6089     }
6090
6091   new = pop_context ();
6092   /* Make a block for the local symbols within.  */
6093   block = finish_block (new->name, &local_symbols, new->old_blocks,
6094                         lowpc, highpc, objfile);
6095
6096   /* For C++, set the block's scope.  */
6097   if (cu->language == language_cplus || cu->language == language_fortran)
6098     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6099                         determine_prefix (die, cu),
6100                         processing_has_namespace_info);
6101
6102   /* If we have address ranges, record them.  */
6103   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6104
6105   /* Attach template arguments to function.  */
6106   if (! VEC_empty (symbolp, template_args))
6107     {
6108       gdb_assert (templ_func != NULL);
6109
6110       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6111       templ_func->template_arguments
6112         = obstack_alloc (&objfile->objfile_obstack,
6113                          (templ_func->n_template_arguments
6114                           * sizeof (struct symbol *)));
6115       memcpy (templ_func->template_arguments,
6116               VEC_address (symbolp, template_args),
6117               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6118       VEC_free (symbolp, template_args);
6119     }
6120
6121   /* In C++, we can have functions nested inside functions (e.g., when
6122      a function declares a class that has methods).  This means that
6123      when we finish processing a function scope, we may need to go
6124      back to building a containing block's symbol lists.  */
6125   local_symbols = new->locals;
6126   param_symbols = new->params;
6127   using_directives = new->using_directives;
6128
6129   /* If we've finished processing a top-level function, subsequent
6130      symbols go in the file symbol list.  */
6131   if (outermost_context_p ())
6132     cu->list_in_scope = &file_symbols;
6133 }
6134
6135 /* Process all the DIES contained within a lexical block scope.  Start
6136    a new scope, process the dies, and then close the scope.  */
6137
6138 static void
6139 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6140 {
6141   struct objfile *objfile = cu->objfile;
6142   struct context_stack *new;
6143   CORE_ADDR lowpc, highpc;
6144   struct die_info *child_die;
6145   CORE_ADDR baseaddr;
6146
6147   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6148
6149   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6150   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6151      as multiple lexical blocks?  Handling children in a sane way would
6152      be nasty.  Might be easier to properly extend generic blocks to
6153      describe ranges.  */
6154   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6155     return;
6156   lowpc += baseaddr;
6157   highpc += baseaddr;
6158
6159   push_context (0, lowpc);
6160   if (die->child != NULL)
6161     {
6162       child_die = die->child;
6163       while (child_die && child_die->tag)
6164         {
6165           process_die (child_die, cu);
6166           child_die = sibling_die (child_die);
6167         }
6168     }
6169   new = pop_context ();
6170
6171   if (local_symbols != NULL || using_directives != NULL)
6172     {
6173       struct block *block
6174         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6175                         highpc, objfile);
6176
6177       /* Note that recording ranges after traversing children, as we
6178          do here, means that recording a parent's ranges entails
6179          walking across all its children's ranges as they appear in
6180          the address map, which is quadratic behavior.
6181
6182          It would be nicer to record the parent's ranges before
6183          traversing its children, simply overriding whatever you find
6184          there.  But since we don't even decide whether to create a
6185          block until after we've traversed its children, that's hard
6186          to do.  */
6187       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6188     }
6189   local_symbols = new->locals;
6190   using_directives = new->using_directives;
6191 }
6192
6193 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6194
6195 static void
6196 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6197 {
6198   struct objfile *objfile = cu->objfile;
6199   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6200   CORE_ADDR pc, baseaddr;
6201   struct attribute *attr;
6202   struct call_site *call_site, call_site_local;
6203   void **slot;
6204   int nparams;
6205   struct die_info *child_die;
6206
6207   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6208
6209   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6210   if (!attr)
6211     {
6212       complaint (&symfile_complaints,
6213                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6214                    "DIE 0x%x [in module %s]"),
6215                  die->offset, objfile->name);
6216       return;
6217     }
6218   pc = DW_ADDR (attr) + baseaddr;
6219
6220   if (cu->call_site_htab == NULL)
6221     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6222                                                NULL, &objfile->objfile_obstack,
6223                                                hashtab_obstack_allocate, NULL);
6224   call_site_local.pc = pc;
6225   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6226   if (*slot != NULL)
6227     {
6228       complaint (&symfile_complaints,
6229                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6230                    "DIE 0x%x [in module %s]"),
6231                  paddress (gdbarch, pc), die->offset, objfile->name);
6232       return;
6233     }
6234
6235   /* Count parameters at the caller.  */
6236
6237   nparams = 0;
6238   for (child_die = die->child; child_die && child_die->tag;
6239        child_die = sibling_die (child_die))
6240     {
6241       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6242         {
6243           complaint (&symfile_complaints,
6244                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6245                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6246                      child_die->tag, child_die->offset, objfile->name);
6247           continue;
6248         }
6249
6250       nparams++;
6251     }
6252
6253   call_site = obstack_alloc (&objfile->objfile_obstack,
6254                              (sizeof (*call_site)
6255                               + (sizeof (*call_site->parameter)
6256                                  * (nparams - 1))));
6257   *slot = call_site;
6258   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6259   call_site->pc = pc;
6260
6261   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6262     {
6263       struct die_info *func_die;
6264
6265       /* Skip also over DW_TAG_inlined_subroutine.  */
6266       for (func_die = die->parent;
6267            func_die && func_die->tag != DW_TAG_subprogram
6268            && func_die->tag != DW_TAG_subroutine_type;
6269            func_die = func_die->parent);
6270
6271       /* DW_AT_GNU_all_call_sites is a superset
6272          of DW_AT_GNU_all_tail_call_sites.  */
6273       if (func_die
6274           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6275           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6276         {
6277           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6278              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6279              both the initial caller containing the real return address PC and
6280              the final callee containing the current PC of a chain of tail
6281              calls do not need to have the tail call list complete.  But any
6282              function candidate for a virtual tail call frame searched via
6283              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6284              determined unambiguously.  */
6285         }
6286       else
6287         {
6288           struct type *func_type = NULL;
6289
6290           if (func_die)
6291             func_type = get_die_type (func_die, cu);
6292           if (func_type != NULL)
6293             {
6294               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6295
6296               /* Enlist this call site to the function.  */
6297               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6298               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6299             }
6300           else
6301             complaint (&symfile_complaints,
6302                        _("Cannot find function owning DW_TAG_GNU_call_site "
6303                          "DIE 0x%x [in module %s]"),
6304                        die->offset, objfile->name);
6305         }
6306     }
6307
6308   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6309   if (attr == NULL)
6310     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6311   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6312   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6313     /* Keep NULL DWARF_BLOCK.  */;
6314   else if (attr_form_is_block (attr))
6315     {
6316       struct dwarf2_locexpr_baton *dlbaton;
6317
6318       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6319       dlbaton->data = DW_BLOCK (attr)->data;
6320       dlbaton->size = DW_BLOCK (attr)->size;
6321       dlbaton->per_cu = cu->per_cu;
6322
6323       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6324     }
6325   else if (is_ref_attr (attr))
6326     {
6327       struct dwarf2_cu *target_cu = cu;
6328       struct die_info *target_die;
6329
6330       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6331       gdb_assert (target_cu->objfile == objfile);
6332       if (die_is_declaration (target_die, target_cu))
6333         {
6334           const char *target_physname;
6335
6336           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6337           if (target_physname == NULL)
6338             complaint (&symfile_complaints,
6339                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6340                          "physname, for referencing DIE 0x%x [in module %s]"),
6341                        die->offset, objfile->name);
6342           else
6343             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6344         }
6345       else
6346         {
6347           CORE_ADDR lowpc;
6348
6349           /* DW_AT_entry_pc should be preferred.  */
6350           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6351             complaint (&symfile_complaints,
6352                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6353                          "low pc, for referencing DIE 0x%x [in module %s]"),
6354                        die->offset, objfile->name);
6355           else
6356             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6357         }
6358     }
6359   else
6360     complaint (&symfile_complaints,
6361                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6362                  "block nor reference, for DIE 0x%x [in module %s]"),
6363                die->offset, objfile->name);
6364
6365   call_site->per_cu = cu->per_cu;
6366
6367   for (child_die = die->child;
6368        child_die && child_die->tag;
6369        child_die = sibling_die (child_die))
6370     {
6371       struct dwarf2_locexpr_baton *dlbaton;
6372       struct call_site_parameter *parameter;
6373
6374       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6375         {
6376           /* Already printed the complaint above.  */
6377           continue;
6378         }
6379
6380       gdb_assert (call_site->parameter_count < nparams);
6381       parameter = &call_site->parameter[call_site->parameter_count];
6382
6383       /* DW_AT_location specifies the register number.  Value of the data
6384          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6385
6386       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6387       if (!attr || !attr_form_is_block (attr))
6388         {
6389           complaint (&symfile_complaints,
6390                      _("No DW_FORM_block* DW_AT_location for "
6391                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6392                      child_die->offset, objfile->name);
6393           continue;
6394         }
6395       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6396                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6397       if (parameter->dwarf_reg == -1
6398           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6399                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6400                                         &parameter->fb_offset))
6401         {
6402           complaint (&symfile_complaints,
6403                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6404                        "for DW_FORM_block* DW_AT_location for "
6405                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6406                      child_die->offset, objfile->name);
6407           continue;
6408         }
6409
6410       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6411       if (!attr_form_is_block (attr))
6412         {
6413           complaint (&symfile_complaints,
6414                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6415                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6416                      child_die->offset, objfile->name);
6417           continue;
6418         }
6419       parameter->value = DW_BLOCK (attr)->data;
6420       parameter->value_size = DW_BLOCK (attr)->size;
6421
6422       /* Parameters are not pre-cleared by memset above.  */
6423       parameter->data_value = NULL;
6424       parameter->data_value_size = 0;
6425       call_site->parameter_count++;
6426
6427       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6428       if (attr)
6429         {
6430           if (!attr_form_is_block (attr))
6431             complaint (&symfile_complaints,
6432                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6433                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6434                        child_die->offset, objfile->name);
6435           else
6436             {
6437               parameter->data_value = DW_BLOCK (attr)->data;
6438               parameter->data_value_size = DW_BLOCK (attr)->size;
6439             }
6440         }
6441     }
6442 }
6443
6444 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6445    Return 1 if the attributes are present and valid, otherwise, return 0.
6446    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6447
6448 static int
6449 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6450                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6451                     struct partial_symtab *ranges_pst)
6452 {
6453   struct objfile *objfile = cu->objfile;
6454   struct comp_unit_head *cu_header = &cu->header;
6455   bfd *obfd = objfile->obfd;
6456   unsigned int addr_size = cu_header->addr_size;
6457   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6458   /* Base address selection entry.  */
6459   CORE_ADDR base;
6460   int found_base;
6461   unsigned int dummy;
6462   gdb_byte *buffer;
6463   CORE_ADDR marker;
6464   int low_set;
6465   CORE_ADDR low = 0;
6466   CORE_ADDR high = 0;
6467   CORE_ADDR baseaddr;
6468
6469   found_base = cu->base_known;
6470   base = cu->base_address;
6471
6472   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6473   if (offset >= dwarf2_per_objfile->ranges.size)
6474     {
6475       complaint (&symfile_complaints,
6476                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6477                  offset);
6478       return 0;
6479     }
6480   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6481
6482   /* Read in the largest possible address.  */
6483   marker = read_address (obfd, buffer, cu, &dummy);
6484   if ((marker & mask) == mask)
6485     {
6486       /* If we found the largest possible address, then
6487          read the base address.  */
6488       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6489       buffer += 2 * addr_size;
6490       offset += 2 * addr_size;
6491       found_base = 1;
6492     }
6493
6494   low_set = 0;
6495
6496   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6497
6498   while (1)
6499     {
6500       CORE_ADDR range_beginning, range_end;
6501
6502       range_beginning = read_address (obfd, buffer, cu, &dummy);
6503       buffer += addr_size;
6504       range_end = read_address (obfd, buffer, cu, &dummy);
6505       buffer += addr_size;
6506       offset += 2 * addr_size;
6507
6508       /* An end of list marker is a pair of zero addresses.  */
6509       if (range_beginning == 0 && range_end == 0)
6510         /* Found the end of list entry.  */
6511         break;
6512
6513       /* Each base address selection entry is a pair of 2 values.
6514          The first is the largest possible address, the second is
6515          the base address.  Check for a base address here.  */
6516       if ((range_beginning & mask) == mask)
6517         {
6518           /* If we found the largest possible address, then
6519              read the base address.  */
6520           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6521           found_base = 1;
6522           continue;
6523         }
6524
6525       if (!found_base)
6526         {
6527           /* We have no valid base address for the ranges
6528              data.  */
6529           complaint (&symfile_complaints,
6530                      _("Invalid .debug_ranges data (no base address)"));
6531           return 0;
6532         }
6533
6534       if (range_beginning > range_end)
6535         {
6536           /* Inverted range entries are invalid.  */
6537           complaint (&symfile_complaints,
6538                      _("Invalid .debug_ranges data (inverted range)"));
6539           return 0;
6540         }
6541
6542       /* Empty range entries have no effect.  */
6543       if (range_beginning == range_end)
6544         continue;
6545
6546       range_beginning += base;
6547       range_end += base;
6548
6549       if (ranges_pst != NULL)
6550         addrmap_set_empty (objfile->psymtabs_addrmap,
6551                            range_beginning + baseaddr,
6552                            range_end - 1 + baseaddr,
6553                            ranges_pst);
6554
6555       /* FIXME: This is recording everything as a low-high
6556          segment of consecutive addresses.  We should have a
6557          data structure for discontiguous block ranges
6558          instead.  */
6559       if (! low_set)
6560         {
6561           low = range_beginning;
6562           high = range_end;
6563           low_set = 1;
6564         }
6565       else
6566         {
6567           if (range_beginning < low)
6568             low = range_beginning;
6569           if (range_end > high)
6570             high = range_end;
6571         }
6572     }
6573
6574   if (! low_set)
6575     /* If the first entry is an end-of-list marker, the range
6576        describes an empty scope, i.e. no instructions.  */
6577     return 0;
6578
6579   if (low_return)
6580     *low_return = low;
6581   if (high_return)
6582     *high_return = high;
6583   return 1;
6584 }
6585
6586 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6587    are present and valid, otherwise, return 0.  Return -1 if the range is
6588    discontinuous, i.e. derived from DW_AT_ranges information.  */
6589 static int
6590 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6591                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6592                       struct partial_symtab *pst)
6593 {
6594   struct attribute *attr;
6595   CORE_ADDR low = 0;
6596   CORE_ADDR high = 0;
6597   int ret = 0;
6598
6599   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6600   if (attr)
6601     {
6602       high = DW_ADDR (attr);
6603       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6604       if (attr)
6605         low = DW_ADDR (attr);
6606       else
6607         /* Found high w/o low attribute.  */
6608         return 0;
6609
6610       /* Found consecutive range of addresses.  */
6611       ret = 1;
6612     }
6613   else
6614     {
6615       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6616       if (attr != NULL)
6617         {
6618           /* Value of the DW_AT_ranges attribute is the offset in the
6619              .debug_ranges section.  */
6620           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6621             return 0;
6622           /* Found discontinuous range of addresses.  */
6623           ret = -1;
6624         }
6625     }
6626
6627   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6628   if (high <= low)
6629     return 0;
6630
6631   /* When using the GNU linker, .gnu.linkonce. sections are used to
6632      eliminate duplicate copies of functions and vtables and such.
6633      The linker will arbitrarily choose one and discard the others.
6634      The AT_*_pc values for such functions refer to local labels in
6635      these sections.  If the section from that file was discarded, the
6636      labels are not in the output, so the relocs get a value of 0.
6637      If this is a discarded function, mark the pc bounds as invalid,
6638      so that GDB will ignore it.  */
6639   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6640     return 0;
6641
6642   *lowpc = low;
6643   if (highpc)
6644     *highpc = high;
6645   return ret;
6646 }
6647
6648 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6649    its low and high PC addresses.  Do nothing if these addresses could not
6650    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6651    and HIGHPC to the high address if greater than HIGHPC.  */
6652
6653 static void
6654 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6655                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6656                                  struct dwarf2_cu *cu)
6657 {
6658   CORE_ADDR low, high;
6659   struct die_info *child = die->child;
6660
6661   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6662     {
6663       *lowpc = min (*lowpc, low);
6664       *highpc = max (*highpc, high);
6665     }
6666
6667   /* If the language does not allow nested subprograms (either inside
6668      subprograms or lexical blocks), we're done.  */
6669   if (cu->language != language_ada)
6670     return;
6671
6672   /* Check all the children of the given DIE.  If it contains nested
6673      subprograms, then check their pc bounds.  Likewise, we need to
6674      check lexical blocks as well, as they may also contain subprogram
6675      definitions.  */
6676   while (child && child->tag)
6677     {
6678       if (child->tag == DW_TAG_subprogram
6679           || child->tag == DW_TAG_lexical_block)
6680         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6681       child = sibling_die (child);
6682     }
6683 }
6684
6685 /* Get the low and high pc's represented by the scope DIE, and store
6686    them in *LOWPC and *HIGHPC.  If the correct values can't be
6687    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6688
6689 static void
6690 get_scope_pc_bounds (struct die_info *die,
6691                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6692                      struct dwarf2_cu *cu)
6693 {
6694   CORE_ADDR best_low = (CORE_ADDR) -1;
6695   CORE_ADDR best_high = (CORE_ADDR) 0;
6696   CORE_ADDR current_low, current_high;
6697
6698   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6699     {
6700       best_low = current_low;
6701       best_high = current_high;
6702     }
6703   else
6704     {
6705       struct die_info *child = die->child;
6706
6707       while (child && child->tag)
6708         {
6709           switch (child->tag) {
6710           case DW_TAG_subprogram:
6711             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6712             break;
6713           case DW_TAG_namespace:
6714           case DW_TAG_module:
6715             /* FIXME: carlton/2004-01-16: Should we do this for
6716                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6717                that current GCC's always emit the DIEs corresponding
6718                to definitions of methods of classes as children of a
6719                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6720                the DIEs giving the declarations, which could be
6721                anywhere).  But I don't see any reason why the
6722                standards says that they have to be there.  */
6723             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6724
6725             if (current_low != ((CORE_ADDR) -1))
6726               {
6727                 best_low = min (best_low, current_low);
6728                 best_high = max (best_high, current_high);
6729               }
6730             break;
6731           default:
6732             /* Ignore.  */
6733             break;
6734           }
6735
6736           child = sibling_die (child);
6737         }
6738     }
6739
6740   *lowpc = best_low;
6741   *highpc = best_high;
6742 }
6743
6744 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6745    in DIE.  */
6746 static void
6747 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6748                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6749 {
6750   struct objfile *objfile = cu->objfile;
6751   struct attribute *attr;
6752
6753   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6754   if (attr)
6755     {
6756       CORE_ADDR high = DW_ADDR (attr);
6757
6758       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6759       if (attr)
6760         {
6761           CORE_ADDR low = DW_ADDR (attr);
6762
6763           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6764         }
6765     }
6766
6767   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6768   if (attr)
6769     {
6770       bfd *obfd = objfile->obfd;
6771
6772       /* The value of the DW_AT_ranges attribute is the offset of the
6773          address range list in the .debug_ranges section.  */
6774       unsigned long offset = DW_UNSND (attr);
6775       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6776
6777       /* For some target architectures, but not others, the
6778          read_address function sign-extends the addresses it returns.
6779          To recognize base address selection entries, we need a
6780          mask.  */
6781       unsigned int addr_size = cu->header.addr_size;
6782       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6783
6784       /* The base address, to which the next pair is relative.  Note
6785          that this 'base' is a DWARF concept: most entries in a range
6786          list are relative, to reduce the number of relocs against the
6787          debugging information.  This is separate from this function's
6788          'baseaddr' argument, which GDB uses to relocate debugging
6789          information from a shared library based on the address at
6790          which the library was loaded.  */
6791       CORE_ADDR base = cu->base_address;
6792       int base_known = cu->base_known;
6793
6794       gdb_assert (dwarf2_per_objfile->ranges.readin);
6795       if (offset >= dwarf2_per_objfile->ranges.size)
6796         {
6797           complaint (&symfile_complaints,
6798                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6799                      offset);
6800           return;
6801         }
6802
6803       for (;;)
6804         {
6805           unsigned int bytes_read;
6806           CORE_ADDR start, end;
6807
6808           start = read_address (obfd, buffer, cu, &bytes_read);
6809           buffer += bytes_read;
6810           end = read_address (obfd, buffer, cu, &bytes_read);
6811           buffer += bytes_read;
6812
6813           /* Did we find the end of the range list?  */
6814           if (start == 0 && end == 0)
6815             break;
6816
6817           /* Did we find a base address selection entry?  */
6818           else if ((start & base_select_mask) == base_select_mask)
6819             {
6820               base = end;
6821               base_known = 1;
6822             }
6823
6824           /* We found an ordinary address range.  */
6825           else
6826             {
6827               if (!base_known)
6828                 {
6829                   complaint (&symfile_complaints,
6830                              _("Invalid .debug_ranges data "
6831                                "(no base address)"));
6832                   return;
6833                 }
6834
6835               if (start > end)
6836                 {
6837                   /* Inverted range entries are invalid.  */
6838                   complaint (&symfile_complaints,
6839                              _("Invalid .debug_ranges data "
6840                                "(inverted range)"));
6841                   return;
6842                 }
6843
6844               /* Empty range entries have no effect.  */
6845               if (start == end)
6846                 continue;
6847
6848               record_block_range (block,
6849                                   baseaddr + base + start,
6850                                   baseaddr + base + end - 1);
6851             }
6852         }
6853     }
6854 }
6855
6856 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6857    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6858    during 4.6.0 experimental.  */
6859
6860 static int
6861 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6862 {
6863   const char *cs;
6864   int major, minor, release;
6865
6866   if (cu->producer == NULL)
6867     {
6868       /* For unknown compilers expect their behavior is DWARF version
6869          compliant.
6870
6871          GCC started to support .debug_types sections by -gdwarf-4 since
6872          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6873          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6874          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6875          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6876
6877       return 0;
6878     }
6879
6880   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6881
6882   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6883     {
6884       /* For non-GCC compilers expect their behavior is DWARF version
6885          compliant.  */
6886
6887       return 0;
6888     }
6889   cs = &cu->producer[strlen ("GNU ")];
6890   while (*cs && !isdigit (*cs))
6891     cs++;
6892   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6893     {
6894       /* Not recognized as GCC.  */
6895
6896       return 0;
6897     }
6898
6899   return major < 4 || (major == 4 && minor < 6);
6900 }
6901
6902 /* Return the default accessibility type if it is not overriden by
6903    DW_AT_accessibility.  */
6904
6905 static enum dwarf_access_attribute
6906 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6907 {
6908   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6909     {
6910       /* The default DWARF 2 accessibility for members is public, the default
6911          accessibility for inheritance is private.  */
6912
6913       if (die->tag != DW_TAG_inheritance)
6914         return DW_ACCESS_public;
6915       else
6916         return DW_ACCESS_private;
6917     }
6918   else
6919     {
6920       /* DWARF 3+ defines the default accessibility a different way.  The same
6921          rules apply now for DW_TAG_inheritance as for the members and it only
6922          depends on the container kind.  */
6923
6924       if (die->parent->tag == DW_TAG_class_type)
6925         return DW_ACCESS_private;
6926       else
6927         return DW_ACCESS_public;
6928     }
6929 }
6930
6931 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6932    offset.  If the attribute was not found return 0, otherwise return
6933    1.  If it was found but could not properly be handled, set *OFFSET
6934    to 0.  */
6935
6936 static int
6937 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6938                              LONGEST *offset)
6939 {
6940   struct attribute *attr;
6941
6942   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6943   if (attr != NULL)
6944     {
6945       *offset = 0;
6946
6947       /* Note that we do not check for a section offset first here.
6948          This is because DW_AT_data_member_location is new in DWARF 4,
6949          so if we see it, we can assume that a constant form is really
6950          a constant and not a section offset.  */
6951       if (attr_form_is_constant (attr))
6952         *offset = dwarf2_get_attr_constant_value (attr, 0);
6953       else if (attr_form_is_section_offset (attr))
6954         dwarf2_complex_location_expr_complaint ();
6955       else if (attr_form_is_block (attr))
6956         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6957       else
6958         dwarf2_complex_location_expr_complaint ();
6959
6960       return 1;
6961     }
6962
6963   return 0;
6964 }
6965
6966 /* Add an aggregate field to the field list.  */
6967
6968 static void
6969 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6970                   struct dwarf2_cu *cu)
6971 {
6972   struct objfile *objfile = cu->objfile;
6973   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6974   struct nextfield *new_field;
6975   struct attribute *attr;
6976   struct field *fp;
6977   char *fieldname = "";
6978
6979   /* Allocate a new field list entry and link it in.  */
6980   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6981   make_cleanup (xfree, new_field);
6982   memset (new_field, 0, sizeof (struct nextfield));
6983
6984   if (die->tag == DW_TAG_inheritance)
6985     {
6986       new_field->next = fip->baseclasses;
6987       fip->baseclasses = new_field;
6988     }
6989   else
6990     {
6991       new_field->next = fip->fields;
6992       fip->fields = new_field;
6993     }
6994   fip->nfields++;
6995
6996   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6997   if (attr)
6998     new_field->accessibility = DW_UNSND (attr);
6999   else
7000     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7001   if (new_field->accessibility != DW_ACCESS_public)
7002     fip->non_public_fields = 1;
7003
7004   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7005   if (attr)
7006     new_field->virtuality = DW_UNSND (attr);
7007   else
7008     new_field->virtuality = DW_VIRTUALITY_none;
7009
7010   fp = &new_field->field;
7011
7012   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7013     {
7014       LONGEST offset;
7015
7016       /* Data member other than a C++ static data member.  */
7017
7018       /* Get type of field.  */
7019       fp->type = die_type (die, cu);
7020
7021       SET_FIELD_BITPOS (*fp, 0);
7022
7023       /* Get bit size of field (zero if none).  */
7024       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7025       if (attr)
7026         {
7027           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7028         }
7029       else
7030         {
7031           FIELD_BITSIZE (*fp) = 0;
7032         }
7033
7034       /* Get bit offset of field.  */
7035       if (handle_data_member_location (die, cu, &offset))
7036         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7037       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7038       if (attr)
7039         {
7040           if (gdbarch_bits_big_endian (gdbarch))
7041             {
7042               /* For big endian bits, the DW_AT_bit_offset gives the
7043                  additional bit offset from the MSB of the containing
7044                  anonymous object to the MSB of the field.  We don't
7045                  have to do anything special since we don't need to
7046                  know the size of the anonymous object.  */
7047               FIELD_BITPOS (*fp) += DW_UNSND (attr);
7048             }
7049           else
7050             {
7051               /* For little endian bits, compute the bit offset to the
7052                  MSB of the anonymous object, subtract off the number of
7053                  bits from the MSB of the field to the MSB of the
7054                  object, and then subtract off the number of bits of
7055                  the field itself.  The result is the bit offset of
7056                  the LSB of the field.  */
7057               int anonymous_size;
7058               int bit_offset = DW_UNSND (attr);
7059
7060               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7061               if (attr)
7062                 {
7063                   /* The size of the anonymous object containing
7064                      the bit field is explicit, so use the
7065                      indicated size (in bytes).  */
7066                   anonymous_size = DW_UNSND (attr);
7067                 }
7068               else
7069                 {
7070                   /* The size of the anonymous object containing
7071                      the bit field must be inferred from the type
7072                      attribute of the data member containing the
7073                      bit field.  */
7074                   anonymous_size = TYPE_LENGTH (fp->type);
7075                 }
7076               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7077                 - bit_offset - FIELD_BITSIZE (*fp);
7078             }
7079         }
7080
7081       /* Get name of field.  */
7082       fieldname = dwarf2_name (die, cu);
7083       if (fieldname == NULL)
7084         fieldname = "";
7085
7086       /* The name is already allocated along with this objfile, so we don't
7087          need to duplicate it for the type.  */
7088       fp->name = fieldname;
7089
7090       /* Change accessibility for artificial fields (e.g. virtual table
7091          pointer or virtual base class pointer) to private.  */
7092       if (dwarf2_attr (die, DW_AT_artificial, cu))
7093         {
7094           FIELD_ARTIFICIAL (*fp) = 1;
7095           new_field->accessibility = DW_ACCESS_private;
7096           fip->non_public_fields = 1;
7097         }
7098     }
7099   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7100     {
7101       /* C++ static member.  */
7102
7103       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7104          is a declaration, but all versions of G++ as of this writing
7105          (so through at least 3.2.1) incorrectly generate
7106          DW_TAG_variable tags.  */
7107
7108       const char *physname;
7109
7110       /* Get name of field.  */
7111       fieldname = dwarf2_name (die, cu);
7112       if (fieldname == NULL)
7113         return;
7114
7115       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7116       if (attr
7117           /* Only create a symbol if this is an external value.
7118              new_symbol checks this and puts the value in the global symbol
7119              table, which we want.  If it is not external, new_symbol
7120              will try to put the value in cu->list_in_scope which is wrong.  */
7121           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7122         {
7123           /* A static const member, not much different than an enum as far as
7124              we're concerned, except that we can support more types.  */
7125           new_symbol (die, NULL, cu);
7126         }
7127
7128       /* Get physical name.  */
7129       physname = dwarf2_physname (fieldname, die, cu);
7130
7131       /* The name is already allocated along with this objfile, so we don't
7132          need to duplicate it for the type.  */
7133       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7134       FIELD_TYPE (*fp) = die_type (die, cu);
7135       FIELD_NAME (*fp) = fieldname;
7136     }
7137   else if (die->tag == DW_TAG_inheritance)
7138     {
7139       LONGEST offset;
7140
7141       /* C++ base class field.  */
7142       if (handle_data_member_location (die, cu, &offset))
7143         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7144       FIELD_BITSIZE (*fp) = 0;
7145       FIELD_TYPE (*fp) = die_type (die, cu);
7146       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7147       fip->nbaseclasses++;
7148     }
7149 }
7150
7151 /* Add a typedef defined in the scope of the FIP's class.  */
7152
7153 static void
7154 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7155                     struct dwarf2_cu *cu)
7156 {
7157   struct objfile *objfile = cu->objfile;
7158   struct typedef_field_list *new_field;
7159   struct attribute *attr;
7160   struct typedef_field *fp;
7161   char *fieldname = "";
7162
7163   /* Allocate a new field list entry and link it in.  */
7164   new_field = xzalloc (sizeof (*new_field));
7165   make_cleanup (xfree, new_field);
7166
7167   gdb_assert (die->tag == DW_TAG_typedef);
7168
7169   fp = &new_field->field;
7170
7171   /* Get name of field.  */
7172   fp->name = dwarf2_name (die, cu);
7173   if (fp->name == NULL)
7174     return;
7175
7176   fp->type = read_type_die (die, cu);
7177
7178   new_field->next = fip->typedef_field_list;
7179   fip->typedef_field_list = new_field;
7180   fip->typedef_field_list_count++;
7181 }
7182
7183 /* Create the vector of fields, and attach it to the type.  */
7184
7185 static void
7186 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7187                               struct dwarf2_cu *cu)
7188 {
7189   int nfields = fip->nfields;
7190
7191   /* Record the field count, allocate space for the array of fields,
7192      and create blank accessibility bitfields if necessary.  */
7193   TYPE_NFIELDS (type) = nfields;
7194   TYPE_FIELDS (type) = (struct field *)
7195     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7196   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7197
7198   if (fip->non_public_fields && cu->language != language_ada)
7199     {
7200       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7201
7202       TYPE_FIELD_PRIVATE_BITS (type) =
7203         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7204       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7205
7206       TYPE_FIELD_PROTECTED_BITS (type) =
7207         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7208       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7209
7210       TYPE_FIELD_IGNORE_BITS (type) =
7211         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7212       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7213     }
7214
7215   /* If the type has baseclasses, allocate and clear a bit vector for
7216      TYPE_FIELD_VIRTUAL_BITS.  */
7217   if (fip->nbaseclasses && cu->language != language_ada)
7218     {
7219       int num_bytes = B_BYTES (fip->nbaseclasses);
7220       unsigned char *pointer;
7221
7222       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7223       pointer = TYPE_ALLOC (type, num_bytes);
7224       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7225       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7226       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7227     }
7228
7229   /* Copy the saved-up fields into the field vector.  Start from the head of
7230      the list, adding to the tail of the field array, so that they end up in
7231      the same order in the array in which they were added to the list.  */
7232   while (nfields-- > 0)
7233     {
7234       struct nextfield *fieldp;
7235
7236       if (fip->fields)
7237         {
7238           fieldp = fip->fields;
7239           fip->fields = fieldp->next;
7240         }
7241       else
7242         {
7243           fieldp = fip->baseclasses;
7244           fip->baseclasses = fieldp->next;
7245         }
7246
7247       TYPE_FIELD (type, nfields) = fieldp->field;
7248       switch (fieldp->accessibility)
7249         {
7250         case DW_ACCESS_private:
7251           if (cu->language != language_ada)
7252             SET_TYPE_FIELD_PRIVATE (type, nfields);
7253           break;
7254
7255         case DW_ACCESS_protected:
7256           if (cu->language != language_ada)
7257             SET_TYPE_FIELD_PROTECTED (type, nfields);
7258           break;
7259
7260         case DW_ACCESS_public:
7261           break;
7262
7263         default:
7264           /* Unknown accessibility.  Complain and treat it as public.  */
7265           {
7266             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7267                        fieldp->accessibility);
7268           }
7269           break;
7270         }
7271       if (nfields < fip->nbaseclasses)
7272         {
7273           switch (fieldp->virtuality)
7274             {
7275             case DW_VIRTUALITY_virtual:
7276             case DW_VIRTUALITY_pure_virtual:
7277               if (cu->language == language_ada)
7278                 error (_("unexpected virtuality in component of Ada type"));
7279               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7280               break;
7281             }
7282         }
7283     }
7284 }
7285
7286 /* Add a member function to the proper fieldlist.  */
7287
7288 static void
7289 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7290                       struct type *type, struct dwarf2_cu *cu)
7291 {
7292   struct objfile *objfile = cu->objfile;
7293   struct attribute *attr;
7294   struct fnfieldlist *flp;
7295   int i;
7296   struct fn_field *fnp;
7297   char *fieldname;
7298   struct nextfnfield *new_fnfield;
7299   struct type *this_type;
7300   enum dwarf_access_attribute accessibility;
7301
7302   if (cu->language == language_ada)
7303     error (_("unexpected member function in Ada type"));
7304
7305   /* Get name of member function.  */
7306   fieldname = dwarf2_name (die, cu);
7307   if (fieldname == NULL)
7308     return;
7309
7310   /* Look up member function name in fieldlist.  */
7311   for (i = 0; i < fip->nfnfields; i++)
7312     {
7313       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7314         break;
7315     }
7316
7317   /* Create new list element if necessary.  */
7318   if (i < fip->nfnfields)
7319     flp = &fip->fnfieldlists[i];
7320   else
7321     {
7322       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7323         {
7324           fip->fnfieldlists = (struct fnfieldlist *)
7325             xrealloc (fip->fnfieldlists,
7326                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7327                       * sizeof (struct fnfieldlist));
7328           if (fip->nfnfields == 0)
7329             make_cleanup (free_current_contents, &fip->fnfieldlists);
7330         }
7331       flp = &fip->fnfieldlists[fip->nfnfields];
7332       flp->name = fieldname;
7333       flp->length = 0;
7334       flp->head = NULL;
7335       i = fip->nfnfields++;
7336     }
7337
7338   /* Create a new member function field and chain it to the field list
7339      entry.  */
7340   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7341   make_cleanup (xfree, new_fnfield);
7342   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7343   new_fnfield->next = flp->head;
7344   flp->head = new_fnfield;
7345   flp->length++;
7346
7347   /* Fill in the member function field info.  */
7348   fnp = &new_fnfield->fnfield;
7349
7350   /* Delay processing of the physname until later.  */
7351   if (cu->language == language_cplus || cu->language == language_java)
7352     {
7353       add_to_method_list (type, i, flp->length - 1, fieldname,
7354                           die, cu);
7355     }
7356   else
7357     {
7358       const char *physname = dwarf2_physname (fieldname, die, cu);
7359       fnp->physname = physname ? physname : "";
7360     }
7361
7362   fnp->type = alloc_type (objfile);
7363   this_type = read_type_die (die, cu);
7364   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7365     {
7366       int nparams = TYPE_NFIELDS (this_type);
7367
7368       /* TYPE is the domain of this method, and THIS_TYPE is the type
7369            of the method itself (TYPE_CODE_METHOD).  */
7370       smash_to_method_type (fnp->type, type,
7371                             TYPE_TARGET_TYPE (this_type),
7372                             TYPE_FIELDS (this_type),
7373                             TYPE_NFIELDS (this_type),
7374                             TYPE_VARARGS (this_type));
7375
7376       /* Handle static member functions.
7377          Dwarf2 has no clean way to discern C++ static and non-static
7378          member functions.  G++ helps GDB by marking the first
7379          parameter for non-static member functions (which is the this
7380          pointer) as artificial.  We obtain this information from
7381          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7382       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7383         fnp->voffset = VOFFSET_STATIC;
7384     }
7385   else
7386     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7387                dwarf2_full_name (fieldname, die, cu));
7388
7389   /* Get fcontext from DW_AT_containing_type if present.  */
7390   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7391     fnp->fcontext = die_containing_type (die, cu);
7392
7393   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7394      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7395
7396   /* Get accessibility.  */
7397   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7398   if (attr)
7399     accessibility = DW_UNSND (attr);
7400   else
7401     accessibility = dwarf2_default_access_attribute (die, cu);
7402   switch (accessibility)
7403     {
7404     case DW_ACCESS_private:
7405       fnp->is_private = 1;
7406       break;
7407     case DW_ACCESS_protected:
7408       fnp->is_protected = 1;
7409       break;
7410     }
7411
7412   /* Check for artificial methods.  */
7413   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7414   if (attr && DW_UNSND (attr) != 0)
7415     fnp->is_artificial = 1;
7416
7417   /* Get index in virtual function table if it is a virtual member
7418      function.  For older versions of GCC, this is an offset in the
7419      appropriate virtual table, as specified by DW_AT_containing_type.
7420      For everyone else, it is an expression to be evaluated relative
7421      to the object address.  */
7422
7423   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7424   if (attr)
7425     {
7426       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7427         {
7428           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7429             {
7430               /* Old-style GCC.  */
7431               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7432             }
7433           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7434                    || (DW_BLOCK (attr)->size > 1
7435                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7436                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7437             {
7438               struct dwarf_block blk;
7439               int offset;
7440
7441               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7442                         ? 1 : 2);
7443               blk.size = DW_BLOCK (attr)->size - offset;
7444               blk.data = DW_BLOCK (attr)->data + offset;
7445               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7446               if ((fnp->voffset % cu->header.addr_size) != 0)
7447                 dwarf2_complex_location_expr_complaint ();
7448               else
7449                 fnp->voffset /= cu->header.addr_size;
7450               fnp->voffset += 2;
7451             }
7452           else
7453             dwarf2_complex_location_expr_complaint ();
7454
7455           if (!fnp->fcontext)
7456             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7457         }
7458       else if (attr_form_is_section_offset (attr))
7459         {
7460           dwarf2_complex_location_expr_complaint ();
7461         }
7462       else
7463         {
7464           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7465                                                  fieldname);
7466         }
7467     }
7468   else
7469     {
7470       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7471       if (attr && DW_UNSND (attr))
7472         {
7473           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7474           complaint (&symfile_complaints,
7475                      _("Member function \"%s\" (offset %d) is virtual "
7476                        "but the vtable offset is not specified"),
7477                      fieldname, die->offset);
7478           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7479           TYPE_CPLUS_DYNAMIC (type) = 1;
7480         }
7481     }
7482 }
7483
7484 /* Create the vector of member function fields, and attach it to the type.  */
7485
7486 static void
7487 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7488                                  struct dwarf2_cu *cu)
7489 {
7490   struct fnfieldlist *flp;
7491   int total_length = 0;
7492   int i;
7493
7494   if (cu->language == language_ada)
7495     error (_("unexpected member functions in Ada type"));
7496
7497   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7498   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7499     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7500
7501   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7502     {
7503       struct nextfnfield *nfp = flp->head;
7504       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7505       int k;
7506
7507       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7508       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7509       fn_flp->fn_fields = (struct fn_field *)
7510         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7511       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7512         fn_flp->fn_fields[k] = nfp->fnfield;
7513
7514       total_length += flp->length;
7515     }
7516
7517   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7518   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7519 }
7520
7521 /* Returns non-zero if NAME is the name of a vtable member in CU's
7522    language, zero otherwise.  */
7523 static int
7524 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7525 {
7526   static const char vptr[] = "_vptr";
7527   static const char vtable[] = "vtable";
7528
7529   /* Look for the C++ and Java forms of the vtable.  */
7530   if ((cu->language == language_java
7531        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7532        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7533        && is_cplus_marker (name[sizeof (vptr) - 1])))
7534     return 1;
7535
7536   return 0;
7537 }
7538
7539 /* GCC outputs unnamed structures that are really pointers to member
7540    functions, with the ABI-specified layout.  If TYPE describes
7541    such a structure, smash it into a member function type.
7542
7543    GCC shouldn't do this; it should just output pointer to member DIEs.
7544    This is GCC PR debug/28767.  */
7545
7546 static void
7547 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7548 {
7549   struct type *pfn_type, *domain_type, *new_type;
7550
7551   /* Check for a structure with no name and two children.  */
7552   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7553     return;
7554
7555   /* Check for __pfn and __delta members.  */
7556   if (TYPE_FIELD_NAME (type, 0) == NULL
7557       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7558       || TYPE_FIELD_NAME (type, 1) == NULL
7559       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7560     return;
7561
7562   /* Find the type of the method.  */
7563   pfn_type = TYPE_FIELD_TYPE (type, 0);
7564   if (pfn_type == NULL
7565       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7566       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7567     return;
7568
7569   /* Look for the "this" argument.  */
7570   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7571   if (TYPE_NFIELDS (pfn_type) == 0
7572       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7573       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7574     return;
7575
7576   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7577   new_type = alloc_type (objfile);
7578   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7579                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7580                         TYPE_VARARGS (pfn_type));
7581   smash_to_methodptr_type (type, new_type);
7582 }
7583
7584 /* Called when we find the DIE that starts a structure or union scope
7585    (definition) to create a type for the structure or union.  Fill in
7586    the type's name and general properties; the members will not be
7587    processed until process_structure_type.
7588
7589    NOTE: we need to call these functions regardless of whether or not the
7590    DIE has a DW_AT_name attribute, since it might be an anonymous
7591    structure or union.  This gets the type entered into our set of
7592    user defined types.
7593
7594    However, if the structure is incomplete (an opaque struct/union)
7595    then suppress creating a symbol table entry for it since gdb only
7596    wants to find the one with the complete definition.  Note that if
7597    it is complete, we just call new_symbol, which does it's own
7598    checking about whether the struct/union is anonymous or not (and
7599    suppresses creating a symbol table entry itself).  */
7600
7601 static struct type *
7602 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7603 {
7604   struct objfile *objfile = cu->objfile;
7605   struct type *type;
7606   struct attribute *attr;
7607   char *name;
7608
7609   /* If the definition of this type lives in .debug_types, read that type.
7610      Don't follow DW_AT_specification though, that will take us back up
7611      the chain and we want to go down.  */
7612   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7613   if (attr)
7614     {
7615       struct dwarf2_cu *type_cu = cu;
7616       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7617
7618       /* We could just recurse on read_structure_type, but we need to call
7619          get_die_type to ensure only one type for this DIE is created.
7620          This is important, for example, because for c++ classes we need
7621          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7622       type = read_type_die (type_die, type_cu);
7623
7624       /* TYPE_CU may not be the same as CU.
7625          Ensure TYPE is recorded in CU's type_hash table.  */
7626       return set_die_type (die, type, cu);
7627     }
7628
7629   type = alloc_type (objfile);
7630   INIT_CPLUS_SPECIFIC (type);
7631
7632   name = dwarf2_name (die, cu);
7633   if (name != NULL)
7634     {
7635       if (cu->language == language_cplus
7636           || cu->language == language_java)
7637         {
7638           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7639
7640           /* dwarf2_full_name might have already finished building the DIE's
7641              type.  If so, there is no need to continue.  */
7642           if (get_die_type (die, cu) != NULL)
7643             return get_die_type (die, cu);
7644
7645           TYPE_TAG_NAME (type) = full_name;
7646           if (die->tag == DW_TAG_structure_type
7647               || die->tag == DW_TAG_class_type)
7648             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7649         }
7650       else
7651         {
7652           /* The name is already allocated along with this objfile, so
7653              we don't need to duplicate it for the type.  */
7654           TYPE_TAG_NAME (type) = (char *) name;
7655           if (die->tag == DW_TAG_class_type)
7656             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7657         }
7658     }
7659
7660   if (die->tag == DW_TAG_structure_type)
7661     {
7662       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7663     }
7664   else if (die->tag == DW_TAG_union_type)
7665     {
7666       TYPE_CODE (type) = TYPE_CODE_UNION;
7667     }
7668   else
7669     {
7670       TYPE_CODE (type) = TYPE_CODE_CLASS;
7671     }
7672
7673   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7674     TYPE_DECLARED_CLASS (type) = 1;
7675
7676   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7677   if (attr)
7678     {
7679       TYPE_LENGTH (type) = DW_UNSND (attr);
7680     }
7681   else
7682     {
7683       TYPE_LENGTH (type) = 0;
7684     }
7685
7686   TYPE_STUB_SUPPORTED (type) = 1;
7687   if (die_is_declaration (die, cu))
7688     TYPE_STUB (type) = 1;
7689   else if (attr == NULL && die->child == NULL
7690            && producer_is_realview (cu->producer))
7691     /* RealView does not output the required DW_AT_declaration
7692        on incomplete types.  */
7693     TYPE_STUB (type) = 1;
7694
7695   /* We need to add the type field to the die immediately so we don't
7696      infinitely recurse when dealing with pointers to the structure
7697      type within the structure itself.  */
7698   set_die_type (die, type, cu);
7699
7700   /* set_die_type should be already done.  */
7701   set_descriptive_type (type, die, cu);
7702
7703   return type;
7704 }
7705
7706 /* Finish creating a structure or union type, including filling in
7707    its members and creating a symbol for it.  */
7708
7709 static void
7710 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7711 {
7712   struct objfile *objfile = cu->objfile;
7713   struct die_info *child_die = die->child;
7714   struct type *type;
7715
7716   type = get_die_type (die, cu);
7717   if (type == NULL)
7718     type = read_structure_type (die, cu);
7719
7720   if (die->child != NULL && ! die_is_declaration (die, cu))
7721     {
7722       struct field_info fi;
7723       struct die_info *child_die;
7724       VEC (symbolp) *template_args = NULL;
7725       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7726
7727       memset (&fi, 0, sizeof (struct field_info));
7728
7729       child_die = die->child;
7730
7731       while (child_die && child_die->tag)
7732         {
7733           if (child_die->tag == DW_TAG_member
7734               || child_die->tag == DW_TAG_variable)
7735             {
7736               /* NOTE: carlton/2002-11-05: A C++ static data member
7737                  should be a DW_TAG_member that is a declaration, but
7738                  all versions of G++ as of this writing (so through at
7739                  least 3.2.1) incorrectly generate DW_TAG_variable
7740                  tags for them instead.  */
7741               dwarf2_add_field (&fi, child_die, cu);
7742             }
7743           else if (child_die->tag == DW_TAG_subprogram)
7744             {
7745               /* C++ member function.  */
7746               dwarf2_add_member_fn (&fi, child_die, type, cu);
7747             }
7748           else if (child_die->tag == DW_TAG_inheritance)
7749             {
7750               /* C++ base class field.  */
7751               dwarf2_add_field (&fi, child_die, cu);
7752             }
7753           else if (child_die->tag == DW_TAG_typedef)
7754             dwarf2_add_typedef (&fi, child_die, cu);
7755           else if (child_die->tag == DW_TAG_template_type_param
7756                    || child_die->tag == DW_TAG_template_value_param)
7757             {
7758               struct symbol *arg = new_symbol (child_die, NULL, cu);
7759
7760               if (arg != NULL)
7761                 VEC_safe_push (symbolp, template_args, arg);
7762             }
7763
7764           child_die = sibling_die (child_die);
7765         }
7766
7767       /* Attach template arguments to type.  */
7768       if (! VEC_empty (symbolp, template_args))
7769         {
7770           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7771           TYPE_N_TEMPLATE_ARGUMENTS (type)
7772             = VEC_length (symbolp, template_args);
7773           TYPE_TEMPLATE_ARGUMENTS (type)
7774             = obstack_alloc (&objfile->objfile_obstack,
7775                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7776                               * sizeof (struct symbol *)));
7777           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7778                   VEC_address (symbolp, template_args),
7779                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7780                    * sizeof (struct symbol *)));
7781           VEC_free (symbolp, template_args);
7782         }
7783
7784       /* Attach fields and member functions to the type.  */
7785       if (fi.nfields)
7786         dwarf2_attach_fields_to_type (&fi, type, cu);
7787       if (fi.nfnfields)
7788         {
7789           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7790
7791           /* Get the type which refers to the base class (possibly this
7792              class itself) which contains the vtable pointer for the current
7793              class from the DW_AT_containing_type attribute.  This use of
7794              DW_AT_containing_type is a GNU extension.  */
7795
7796           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7797             {
7798               struct type *t = die_containing_type (die, cu);
7799
7800               TYPE_VPTR_BASETYPE (type) = t;
7801               if (type == t)
7802                 {
7803                   int i;
7804
7805                   /* Our own class provides vtbl ptr.  */
7806                   for (i = TYPE_NFIELDS (t) - 1;
7807                        i >= TYPE_N_BASECLASSES (t);
7808                        --i)
7809                     {
7810                       char *fieldname = TYPE_FIELD_NAME (t, i);
7811
7812                       if (is_vtable_name (fieldname, cu))
7813                         {
7814                           TYPE_VPTR_FIELDNO (type) = i;
7815                           break;
7816                         }
7817                     }
7818
7819                   /* Complain if virtual function table field not found.  */
7820                   if (i < TYPE_N_BASECLASSES (t))
7821                     complaint (&symfile_complaints,
7822                                _("virtual function table pointer "
7823                                  "not found when defining class '%s'"),
7824                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7825                                "");
7826                 }
7827               else
7828                 {
7829                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7830                 }
7831             }
7832           else if (cu->producer
7833                    && strncmp (cu->producer,
7834                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7835             {
7836               /* The IBM XLC compiler does not provide direct indication
7837                  of the containing type, but the vtable pointer is
7838                  always named __vfp.  */
7839
7840               int i;
7841
7842               for (i = TYPE_NFIELDS (type) - 1;
7843                    i >= TYPE_N_BASECLASSES (type);
7844                    --i)
7845                 {
7846                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7847                     {
7848                       TYPE_VPTR_FIELDNO (type) = i;
7849                       TYPE_VPTR_BASETYPE (type) = type;
7850                       break;
7851                     }
7852                 }
7853             }
7854         }
7855
7856       /* Copy fi.typedef_field_list linked list elements content into the
7857          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7858       if (fi.typedef_field_list)
7859         {
7860           int i = fi.typedef_field_list_count;
7861
7862           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7863           TYPE_TYPEDEF_FIELD_ARRAY (type)
7864             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7865           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7866
7867           /* Reverse the list order to keep the debug info elements order.  */
7868           while (--i >= 0)
7869             {
7870               struct typedef_field *dest, *src;
7871
7872               dest = &TYPE_TYPEDEF_FIELD (type, i);
7873               src = &fi.typedef_field_list->field;
7874               fi.typedef_field_list = fi.typedef_field_list->next;
7875               *dest = *src;
7876             }
7877         }
7878
7879       do_cleanups (back_to);
7880
7881       if (HAVE_CPLUS_STRUCT (type))
7882         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7883     }
7884
7885   quirk_gcc_member_function_pointer (type, objfile);
7886
7887   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7888      snapshots) has been known to create a die giving a declaration
7889      for a class that has, as a child, a die giving a definition for a
7890      nested class.  So we have to process our children even if the
7891      current die is a declaration.  Normally, of course, a declaration
7892      won't have any children at all.  */
7893
7894   while (child_die != NULL && child_die->tag)
7895     {
7896       if (child_die->tag == DW_TAG_member
7897           || child_die->tag == DW_TAG_variable
7898           || child_die->tag == DW_TAG_inheritance
7899           || child_die->tag == DW_TAG_template_value_param
7900           || child_die->tag == DW_TAG_template_type_param)
7901         {
7902           /* Do nothing.  */
7903         }
7904       else
7905         process_die (child_die, cu);
7906
7907       child_die = sibling_die (child_die);
7908     }
7909
7910   /* Do not consider external references.  According to the DWARF standard,
7911      these DIEs are identified by the fact that they have no byte_size
7912      attribute, and a declaration attribute.  */
7913   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7914       || !die_is_declaration (die, cu))
7915     new_symbol (die, type, cu);
7916 }
7917
7918 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7919    complete the type's fields yet, or create any symbols.  */
7920
7921 static struct type *
7922 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7923 {
7924   struct objfile *objfile = cu->objfile;
7925   struct type *type;
7926   struct attribute *attr;
7927   const char *name;
7928
7929   /* If the definition of this type lives in .debug_types, read that type.
7930      Don't follow DW_AT_specification though, that will take us back up
7931      the chain and we want to go down.  */
7932   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7933   if (attr)
7934     {
7935       struct dwarf2_cu *type_cu = cu;
7936       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7937
7938       type = read_type_die (type_die, type_cu);
7939
7940       /* TYPE_CU may not be the same as CU.
7941          Ensure TYPE is recorded in CU's type_hash table.  */
7942       return set_die_type (die, type, cu);
7943     }
7944
7945   type = alloc_type (objfile);
7946
7947   TYPE_CODE (type) = TYPE_CODE_ENUM;
7948   name = dwarf2_full_name (NULL, die, cu);
7949   if (name != NULL)
7950     TYPE_TAG_NAME (type) = (char *) name;
7951
7952   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7953   if (attr)
7954     {
7955       TYPE_LENGTH (type) = DW_UNSND (attr);
7956     }
7957   else
7958     {
7959       TYPE_LENGTH (type) = 0;
7960     }
7961
7962   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7963      declared as private in the package spec, and then defined only
7964      inside the package body.  Such types are known as Taft Amendment
7965      Types.  When another package uses such a type, an incomplete DIE
7966      may be generated by the compiler.  */
7967   if (die_is_declaration (die, cu))
7968     TYPE_STUB (type) = 1;
7969
7970   return set_die_type (die, type, cu);
7971 }
7972
7973 /* Given a pointer to a die which begins an enumeration, process all
7974    the dies that define the members of the enumeration, and create the
7975    symbol for the enumeration type.
7976
7977    NOTE: We reverse the order of the element list.  */
7978
7979 static void
7980 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7981 {
7982   struct type *this_type;
7983
7984   this_type = get_die_type (die, cu);
7985   if (this_type == NULL)
7986     this_type = read_enumeration_type (die, cu);
7987
7988   if (die->child != NULL)
7989     {
7990       struct die_info *child_die;
7991       struct symbol *sym;
7992       struct field *fields = NULL;
7993       int num_fields = 0;
7994       int unsigned_enum = 1;
7995       char *name;
7996
7997       child_die = die->child;
7998       while (child_die && child_die->tag)
7999         {
8000           if (child_die->tag != DW_TAG_enumerator)
8001             {
8002               process_die (child_die, cu);
8003             }
8004           else
8005             {
8006               name = dwarf2_name (child_die, cu);
8007               if (name)
8008                 {
8009                   sym = new_symbol (child_die, this_type, cu);
8010                   if (SYMBOL_VALUE (sym) < 0)
8011                     unsigned_enum = 0;
8012
8013                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8014                     {
8015                       fields = (struct field *)
8016                         xrealloc (fields,
8017                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
8018                                   * sizeof (struct field));
8019                     }
8020
8021                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8022                   FIELD_TYPE (fields[num_fields]) = NULL;
8023                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8024                   FIELD_BITSIZE (fields[num_fields]) = 0;
8025
8026                   num_fields++;
8027                 }
8028             }
8029
8030           child_die = sibling_die (child_die);
8031         }
8032
8033       if (num_fields)
8034         {
8035           TYPE_NFIELDS (this_type) = num_fields;
8036           TYPE_FIELDS (this_type) = (struct field *)
8037             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8038           memcpy (TYPE_FIELDS (this_type), fields,
8039                   sizeof (struct field) * num_fields);
8040           xfree (fields);
8041         }
8042       if (unsigned_enum)
8043         TYPE_UNSIGNED (this_type) = 1;
8044     }
8045
8046   /* If we are reading an enum from a .debug_types unit, and the enum
8047      is a declaration, and the enum is not the signatured type in the
8048      unit, then we do not want to add a symbol for it.  Adding a
8049      symbol would in some cases obscure the true definition of the
8050      enum, giving users an incomplete type when the definition is
8051      actually available.  Note that we do not want to do this for all
8052      enums which are just declarations, because C++0x allows forward
8053      enum declarations.  */
8054   if (cu->per_cu->debug_types_section
8055       && die_is_declaration (die, cu))
8056     {
8057       struct signatured_type *type_sig;
8058
8059       type_sig
8060         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8061                                             cu->per_cu->debug_types_section,
8062                                             cu->per_cu->offset);
8063       if (type_sig->type_offset != die->offset)
8064         return;
8065     }
8066
8067   new_symbol (die, this_type, cu);
8068 }
8069
8070 /* Extract all information from a DW_TAG_array_type DIE and put it in
8071    the DIE's type field.  For now, this only handles one dimensional
8072    arrays.  */
8073
8074 static struct type *
8075 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8076 {
8077   struct objfile *objfile = cu->objfile;
8078   struct die_info *child_die;
8079   struct type *type;
8080   struct type *element_type, *range_type, *index_type;
8081   struct type **range_types = NULL;
8082   struct attribute *attr;
8083   int ndim = 0;
8084   struct cleanup *back_to;
8085   char *name;
8086
8087   element_type = die_type (die, cu);
8088
8089   /* The die_type call above may have already set the type for this DIE.  */
8090   type = get_die_type (die, cu);
8091   if (type)
8092     return type;
8093
8094   /* Irix 6.2 native cc creates array types without children for
8095      arrays with unspecified length.  */
8096   if (die->child == NULL)
8097     {
8098       index_type = objfile_type (objfile)->builtin_int;
8099       range_type = create_range_type (NULL, index_type, 0, -1);
8100       type = create_array_type (NULL, element_type, range_type);
8101       return set_die_type (die, type, cu);
8102     }
8103
8104   back_to = make_cleanup (null_cleanup, NULL);
8105   child_die = die->child;
8106   while (child_die && child_die->tag)
8107     {
8108       if (child_die->tag == DW_TAG_subrange_type)
8109         {
8110           struct type *child_type = read_type_die (child_die, cu);
8111
8112           if (child_type != NULL)
8113             {
8114               /* The range type was succesfully read.  Save it for the
8115                  array type creation.  */
8116               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8117                 {
8118                   range_types = (struct type **)
8119                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8120                               * sizeof (struct type *));
8121                   if (ndim == 0)
8122                     make_cleanup (free_current_contents, &range_types);
8123                 }
8124               range_types[ndim++] = child_type;
8125             }
8126         }
8127       child_die = sibling_die (child_die);
8128     }
8129
8130   /* Dwarf2 dimensions are output from left to right, create the
8131      necessary array types in backwards order.  */
8132
8133   type = element_type;
8134
8135   if (read_array_order (die, cu) == DW_ORD_col_major)
8136     {
8137       int i = 0;
8138
8139       while (i < ndim)
8140         type = create_array_type (NULL, type, range_types[i++]);
8141     }
8142   else
8143     {
8144       while (ndim-- > 0)
8145         type = create_array_type (NULL, type, range_types[ndim]);
8146     }
8147
8148   /* Understand Dwarf2 support for vector types (like they occur on
8149      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8150      array type.  This is not part of the Dwarf2/3 standard yet, but a
8151      custom vendor extension.  The main difference between a regular
8152      array and the vector variant is that vectors are passed by value
8153      to functions.  */
8154   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8155   if (attr)
8156     make_vector_type (type);
8157
8158   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8159      implementation may choose to implement triple vectors using this
8160      attribute.  */
8161   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8162   if (attr)
8163     {
8164       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8165         TYPE_LENGTH (type) = DW_UNSND (attr);
8166       else
8167         complaint (&symfile_complaints,
8168                    _("DW_AT_byte_size for array type smaller "
8169                      "than the total size of elements"));
8170     }
8171
8172   name = dwarf2_name (die, cu);
8173   if (name)
8174     TYPE_NAME (type) = name;
8175
8176   /* Install the type in the die.  */
8177   set_die_type (die, type, cu);
8178
8179   /* set_die_type should be already done.  */
8180   set_descriptive_type (type, die, cu);
8181
8182   do_cleanups (back_to);
8183
8184   return type;
8185 }
8186
8187 static enum dwarf_array_dim_ordering
8188 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8189 {
8190   struct attribute *attr;
8191
8192   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8193
8194   if (attr) return DW_SND (attr);
8195
8196   /* GNU F77 is a special case, as at 08/2004 array type info is the
8197      opposite order to the dwarf2 specification, but data is still
8198      laid out as per normal fortran.
8199
8200      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8201      version checking.  */
8202
8203   if (cu->language == language_fortran
8204       && cu->producer && strstr (cu->producer, "GNU F77"))
8205     {
8206       return DW_ORD_row_major;
8207     }
8208
8209   switch (cu->language_defn->la_array_ordering)
8210     {
8211     case array_column_major:
8212       return DW_ORD_col_major;
8213     case array_row_major:
8214     default:
8215       return DW_ORD_row_major;
8216     };
8217 }
8218
8219 /* Extract all information from a DW_TAG_set_type DIE and put it in
8220    the DIE's type field.  */
8221
8222 static struct type *
8223 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8224 {
8225   struct type *domain_type, *set_type;
8226   struct attribute *attr;
8227
8228   domain_type = die_type (die, cu);
8229
8230   /* The die_type call above may have already set the type for this DIE.  */
8231   set_type = get_die_type (die, cu);
8232   if (set_type)
8233     return set_type;
8234
8235   set_type = create_set_type (NULL, domain_type);
8236
8237   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8238   if (attr)
8239     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8240
8241   return set_die_type (die, set_type, cu);
8242 }
8243
8244 /* First cut: install each common block member as a global variable.  */
8245
8246 static void
8247 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8248 {
8249   struct die_info *child_die;
8250   struct attribute *attr;
8251   struct symbol *sym;
8252   CORE_ADDR base = (CORE_ADDR) 0;
8253
8254   attr = dwarf2_attr (die, DW_AT_location, cu);
8255   if (attr)
8256     {
8257       /* Support the .debug_loc offsets.  */
8258       if (attr_form_is_block (attr))
8259         {
8260           base = decode_locdesc (DW_BLOCK (attr), cu);
8261         }
8262       else if (attr_form_is_section_offset (attr))
8263         {
8264           dwarf2_complex_location_expr_complaint ();
8265         }
8266       else
8267         {
8268           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8269                                                  "common block member");
8270         }
8271     }
8272   if (die->child != NULL)
8273     {
8274       child_die = die->child;
8275       while (child_die && child_die->tag)
8276         {
8277           LONGEST offset;
8278
8279           sym = new_symbol (child_die, NULL, cu);
8280           if (sym != NULL
8281               && handle_data_member_location (child_die, cu, &offset))
8282             {
8283               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8284               add_symbol_to_list (sym, &global_symbols);
8285             }
8286           child_die = sibling_die (child_die);
8287         }
8288     }
8289 }
8290
8291 /* Create a type for a C++ namespace.  */
8292
8293 static struct type *
8294 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8295 {
8296   struct objfile *objfile = cu->objfile;
8297   const char *previous_prefix, *name;
8298   int is_anonymous;
8299   struct type *type;
8300
8301   /* For extensions, reuse the type of the original namespace.  */
8302   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8303     {
8304       struct die_info *ext_die;
8305       struct dwarf2_cu *ext_cu = cu;
8306
8307       ext_die = dwarf2_extension (die, &ext_cu);
8308       type = read_type_die (ext_die, ext_cu);
8309
8310       /* EXT_CU may not be the same as CU.
8311          Ensure TYPE is recorded in CU's type_hash table.  */
8312       return set_die_type (die, type, cu);
8313     }
8314
8315   name = namespace_name (die, &is_anonymous, cu);
8316
8317   /* Now build the name of the current namespace.  */
8318
8319   previous_prefix = determine_prefix (die, cu);
8320   if (previous_prefix[0] != '\0')
8321     name = typename_concat (&objfile->objfile_obstack,
8322                             previous_prefix, name, 0, cu);
8323
8324   /* Create the type.  */
8325   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8326                     objfile);
8327   TYPE_NAME (type) = (char *) name;
8328   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8329
8330   return set_die_type (die, type, cu);
8331 }
8332
8333 /* Read a C++ namespace.  */
8334
8335 static void
8336 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8337 {
8338   struct objfile *objfile = cu->objfile;
8339   int is_anonymous;
8340
8341   /* Add a symbol associated to this if we haven't seen the namespace
8342      before.  Also, add a using directive if it's an anonymous
8343      namespace.  */
8344
8345   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8346     {
8347       struct type *type;
8348
8349       type = read_type_die (die, cu);
8350       new_symbol (die, type, cu);
8351
8352       namespace_name (die, &is_anonymous, cu);
8353       if (is_anonymous)
8354         {
8355           const char *previous_prefix = determine_prefix (die, cu);
8356
8357           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8358                                   NULL, NULL, &objfile->objfile_obstack);
8359         }
8360     }
8361
8362   if (die->child != NULL)
8363     {
8364       struct die_info *child_die = die->child;
8365
8366       while (child_die && child_die->tag)
8367         {
8368           process_die (child_die, cu);
8369           child_die = sibling_die (child_die);
8370         }
8371     }
8372 }
8373
8374 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8375    imported module.  Still we need that type as local Fortran "use ... only"
8376    declaration imports depend on the created type in determine_prefix.  */
8377
8378 static struct type *
8379 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8380 {
8381   struct objfile *objfile = cu->objfile;
8382   char *module_name;
8383   struct type *type;
8384
8385   module_name = dwarf2_name (die, cu);
8386   if (!module_name)
8387     complaint (&symfile_complaints,
8388                _("DW_TAG_module has no name, offset 0x%x"),
8389                die->offset);
8390   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8391
8392   /* determine_prefix uses TYPE_TAG_NAME.  */
8393   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8394
8395   return set_die_type (die, type, cu);
8396 }
8397
8398 /* Read a Fortran module.  */
8399
8400 static void
8401 read_module (struct die_info *die, struct dwarf2_cu *cu)
8402 {
8403   struct die_info *child_die = die->child;
8404
8405   while (child_die && child_die->tag)
8406     {
8407       process_die (child_die, cu);
8408       child_die = sibling_die (child_die);
8409     }
8410 }
8411
8412 /* Return the name of the namespace represented by DIE.  Set
8413    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8414    namespace.  */
8415
8416 static const char *
8417 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8418 {
8419   struct die_info *current_die;
8420   const char *name = NULL;
8421
8422   /* Loop through the extensions until we find a name.  */
8423
8424   for (current_die = die;
8425        current_die != NULL;
8426        current_die = dwarf2_extension (die, &cu))
8427     {
8428       name = dwarf2_name (current_die, cu);
8429       if (name != NULL)
8430         break;
8431     }
8432
8433   /* Is it an anonymous namespace?  */
8434
8435   *is_anonymous = (name == NULL);
8436   if (*is_anonymous)
8437     name = CP_ANONYMOUS_NAMESPACE_STR;
8438
8439   return name;
8440 }
8441
8442 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8443    the user defined type vector.  */
8444
8445 static struct type *
8446 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8447 {
8448   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8449   struct comp_unit_head *cu_header = &cu->header;
8450   struct type *type;
8451   struct attribute *attr_byte_size;
8452   struct attribute *attr_address_class;
8453   int byte_size, addr_class;
8454   struct type *target_type;
8455
8456   target_type = die_type (die, cu);
8457
8458   /* The die_type call above may have already set the type for this DIE.  */
8459   type = get_die_type (die, cu);
8460   if (type)
8461     return type;
8462
8463   type = lookup_pointer_type (target_type);
8464
8465   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8466   if (attr_byte_size)
8467     byte_size = DW_UNSND (attr_byte_size);
8468   else
8469     byte_size = cu_header->addr_size;
8470
8471   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8472   if (attr_address_class)
8473     addr_class = DW_UNSND (attr_address_class);
8474   else
8475     addr_class = DW_ADDR_none;
8476
8477   /* If the pointer size or address class is different than the
8478      default, create a type variant marked as such and set the
8479      length accordingly.  */
8480   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8481     {
8482       if (gdbarch_address_class_type_flags_p (gdbarch))
8483         {
8484           int type_flags;
8485
8486           type_flags = gdbarch_address_class_type_flags
8487                          (gdbarch, byte_size, addr_class);
8488           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8489                       == 0);
8490           type = make_type_with_address_space (type, type_flags);
8491         }
8492       else if (TYPE_LENGTH (type) != byte_size)
8493         {
8494           complaint (&symfile_complaints,
8495                      _("invalid pointer size %d"), byte_size);
8496         }
8497       else
8498         {
8499           /* Should we also complain about unhandled address classes?  */
8500         }
8501     }
8502
8503   TYPE_LENGTH (type) = byte_size;
8504   return set_die_type (die, type, cu);
8505 }
8506
8507 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8508    the user defined type vector.  */
8509
8510 static struct type *
8511 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8512 {
8513   struct type *type;
8514   struct type *to_type;
8515   struct type *domain;
8516
8517   to_type = die_type (die, cu);
8518   domain = die_containing_type (die, cu);
8519
8520   /* The calls above may have already set the type for this DIE.  */
8521   type = get_die_type (die, cu);
8522   if (type)
8523     return type;
8524
8525   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8526     type = lookup_methodptr_type (to_type);
8527   else
8528     type = lookup_memberptr_type (to_type, domain);
8529
8530   return set_die_type (die, type, cu);
8531 }
8532
8533 /* Extract all information from a DW_TAG_reference_type DIE and add to
8534    the user defined type vector.  */
8535
8536 static struct type *
8537 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8538 {
8539   struct comp_unit_head *cu_header = &cu->header;
8540   struct type *type, *target_type;
8541   struct attribute *attr;
8542
8543   target_type = die_type (die, cu);
8544
8545   /* The die_type call above may have already set the type for this DIE.  */
8546   type = get_die_type (die, cu);
8547   if (type)
8548     return type;
8549
8550   type = lookup_reference_type (target_type);
8551   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8552   if (attr)
8553     {
8554       TYPE_LENGTH (type) = DW_UNSND (attr);
8555     }
8556   else
8557     {
8558       TYPE_LENGTH (type) = cu_header->addr_size;
8559     }
8560   return set_die_type (die, type, cu);
8561 }
8562
8563 static struct type *
8564 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8565 {
8566   struct type *base_type, *cv_type;
8567
8568   base_type = die_type (die, cu);
8569
8570   /* The die_type call above may have already set the type for this DIE.  */
8571   cv_type = get_die_type (die, cu);
8572   if (cv_type)
8573     return cv_type;
8574
8575   /* In case the const qualifier is applied to an array type, the element type
8576      is so qualified, not the array type (section 6.7.3 of C99).  */
8577   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8578     {
8579       struct type *el_type, *inner_array;
8580
8581       base_type = copy_type (base_type);
8582       inner_array = base_type;
8583
8584       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8585         {
8586           TYPE_TARGET_TYPE (inner_array) =
8587             copy_type (TYPE_TARGET_TYPE (inner_array));
8588           inner_array = TYPE_TARGET_TYPE (inner_array);
8589         }
8590
8591       el_type = TYPE_TARGET_TYPE (inner_array);
8592       TYPE_TARGET_TYPE (inner_array) =
8593         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8594
8595       return set_die_type (die, base_type, cu);
8596     }
8597
8598   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8599   return set_die_type (die, cv_type, cu);
8600 }
8601
8602 static struct type *
8603 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8604 {
8605   struct type *base_type, *cv_type;
8606
8607   base_type = die_type (die, cu);
8608
8609   /* The die_type call above may have already set the type for this DIE.  */
8610   cv_type = get_die_type (die, cu);
8611   if (cv_type)
8612     return cv_type;
8613
8614   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8615   return set_die_type (die, cv_type, cu);
8616 }
8617
8618 /* Extract all information from a DW_TAG_string_type DIE and add to
8619    the user defined type vector.  It isn't really a user defined type,
8620    but it behaves like one, with other DIE's using an AT_user_def_type
8621    attribute to reference it.  */
8622
8623 static struct type *
8624 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8625 {
8626   struct objfile *objfile = cu->objfile;
8627   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8628   struct type *type, *range_type, *index_type, *char_type;
8629   struct attribute *attr;
8630   unsigned int length;
8631
8632   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8633   if (attr)
8634     {
8635       length = DW_UNSND (attr);
8636     }
8637   else
8638     {
8639       /* Check for the DW_AT_byte_size attribute.  */
8640       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8641       if (attr)
8642         {
8643           length = DW_UNSND (attr);
8644         }
8645       else
8646         {
8647           length = 1;
8648         }
8649     }
8650
8651   index_type = objfile_type (objfile)->builtin_int;
8652   range_type = create_range_type (NULL, index_type, 1, length);
8653   char_type = language_string_char_type (cu->language_defn, gdbarch);
8654   type = create_string_type (NULL, char_type, range_type);
8655
8656   return set_die_type (die, type, cu);
8657 }
8658
8659 /* Handle DIES due to C code like:
8660
8661    struct foo
8662    {
8663    int (*funcp)(int a, long l);
8664    int b;
8665    };
8666
8667    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8668
8669 static struct type *
8670 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8671 {
8672   struct objfile *objfile = cu->objfile;
8673   struct type *type;            /* Type that this function returns.  */
8674   struct type *ftype;           /* Function that returns above type.  */
8675   struct attribute *attr;
8676
8677   type = die_type (die, cu);
8678
8679   /* The die_type call above may have already set the type for this DIE.  */
8680   ftype = get_die_type (die, cu);
8681   if (ftype)
8682     return ftype;
8683
8684   ftype = lookup_function_type (type);
8685
8686   /* All functions in C++, Pascal and Java have prototypes.  */
8687   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8688   if ((attr && (DW_UNSND (attr) != 0))
8689       || cu->language == language_cplus
8690       || cu->language == language_java
8691       || cu->language == language_pascal)
8692     TYPE_PROTOTYPED (ftype) = 1;
8693   else if (producer_is_realview (cu->producer))
8694     /* RealView does not emit DW_AT_prototyped.  We can not
8695        distinguish prototyped and unprototyped functions; default to
8696        prototyped, since that is more common in modern code (and
8697        RealView warns about unprototyped functions).  */
8698     TYPE_PROTOTYPED (ftype) = 1;
8699
8700   /* Store the calling convention in the type if it's available in
8701      the subroutine die.  Otherwise set the calling convention to
8702      the default value DW_CC_normal.  */
8703   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8704   if (attr)
8705     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8706   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8707     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8708   else
8709     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8710
8711   /* We need to add the subroutine type to the die immediately so
8712      we don't infinitely recurse when dealing with parameters
8713      declared as the same subroutine type.  */
8714   set_die_type (die, ftype, cu);
8715
8716   if (die->child != NULL)
8717     {
8718       struct type *void_type = objfile_type (objfile)->builtin_void;
8719       struct die_info *child_die;
8720       int nparams, iparams;
8721
8722       /* Count the number of parameters.
8723          FIXME: GDB currently ignores vararg functions, but knows about
8724          vararg member functions.  */
8725       nparams = 0;
8726       child_die = die->child;
8727       while (child_die && child_die->tag)
8728         {
8729           if (child_die->tag == DW_TAG_formal_parameter)
8730             nparams++;
8731           else if (child_die->tag == DW_TAG_unspecified_parameters)
8732             TYPE_VARARGS (ftype) = 1;
8733           child_die = sibling_die (child_die);
8734         }
8735
8736       /* Allocate storage for parameters and fill them in.  */
8737       TYPE_NFIELDS (ftype) = nparams;
8738       TYPE_FIELDS (ftype) = (struct field *)
8739         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8740
8741       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8742          even if we error out during the parameters reading below.  */
8743       for (iparams = 0; iparams < nparams; iparams++)
8744         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8745
8746       iparams = 0;
8747       child_die = die->child;
8748       while (child_die && child_die->tag)
8749         {
8750           if (child_die->tag == DW_TAG_formal_parameter)
8751             {
8752               struct type *arg_type;
8753
8754               /* DWARF version 2 has no clean way to discern C++
8755                  static and non-static member functions.  G++ helps
8756                  GDB by marking the first parameter for non-static
8757                  member functions (which is the this pointer) as
8758                  artificial.  We pass this information to
8759                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8760
8761                  DWARF version 3 added DW_AT_object_pointer, which GCC
8762                  4.5 does not yet generate.  */
8763               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8764               if (attr)
8765                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8766               else
8767                 {
8768                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8769
8770                   /* GCC/43521: In java, the formal parameter
8771                      "this" is sometimes not marked with DW_AT_artificial.  */
8772                   if (cu->language == language_java)
8773                     {
8774                       const char *name = dwarf2_name (child_die, cu);
8775
8776                       if (name && !strcmp (name, "this"))
8777                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8778                     }
8779                 }
8780               arg_type = die_type (child_die, cu);
8781
8782               /* RealView does not mark THIS as const, which the testsuite
8783                  expects.  GCC marks THIS as const in method definitions,
8784                  but not in the class specifications (GCC PR 43053).  */
8785               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8786                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8787                 {
8788                   int is_this = 0;
8789                   struct dwarf2_cu *arg_cu = cu;
8790                   const char *name = dwarf2_name (child_die, cu);
8791
8792                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8793                   if (attr)
8794                     {
8795                       /* If the compiler emits this, use it.  */
8796                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8797                         is_this = 1;
8798                     }
8799                   else if (name && strcmp (name, "this") == 0)
8800                     /* Function definitions will have the argument names.  */
8801                     is_this = 1;
8802                   else if (name == NULL && iparams == 0)
8803                     /* Declarations may not have the names, so like
8804                        elsewhere in GDB, assume an artificial first
8805                        argument is "this".  */
8806                     is_this = 1;
8807
8808                   if (is_this)
8809                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8810                                              arg_type, 0);
8811                 }
8812
8813               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8814               iparams++;
8815             }
8816           child_die = sibling_die (child_die);
8817         }
8818     }
8819
8820   return ftype;
8821 }
8822
8823 static struct type *
8824 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8825 {
8826   struct objfile *objfile = cu->objfile;
8827   const char *name = NULL;
8828   struct type *this_type, *target_type;
8829
8830   name = dwarf2_full_name (NULL, die, cu);
8831   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8832                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8833   TYPE_NAME (this_type) = (char *) name;
8834   set_die_type (die, this_type, cu);
8835   target_type = die_type (die, cu);
8836   if (target_type != this_type)
8837     TYPE_TARGET_TYPE (this_type) = target_type;
8838   else
8839     {
8840       /* Self-referential typedefs are, it seems, not allowed by the DWARF
8841          spec and cause infinite loops in GDB.  */
8842       complaint (&symfile_complaints,
8843                  _("Self-referential DW_TAG_typedef "
8844                    "- DIE at 0x%x [in module %s]"),
8845                  die->offset, objfile->name);
8846       TYPE_TARGET_TYPE (this_type) = NULL;
8847     }
8848   return this_type;
8849 }
8850
8851 /* Find a representation of a given base type and install
8852    it in the TYPE field of the die.  */
8853
8854 static struct type *
8855 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8856 {
8857   struct objfile *objfile = cu->objfile;
8858   struct type *type;
8859   struct attribute *attr;
8860   int encoding = 0, size = 0;
8861   char *name;
8862   enum type_code code = TYPE_CODE_INT;
8863   int type_flags = 0;
8864   struct type *target_type = NULL;
8865
8866   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8867   if (attr)
8868     {
8869       encoding = DW_UNSND (attr);
8870     }
8871   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8872   if (attr)
8873     {
8874       size = DW_UNSND (attr);
8875     }
8876   name = dwarf2_name (die, cu);
8877   if (!name)
8878     {
8879       complaint (&symfile_complaints,
8880                  _("DW_AT_name missing from DW_TAG_base_type"));
8881     }
8882
8883   switch (encoding)
8884     {
8885       case DW_ATE_address:
8886         /* Turn DW_ATE_address into a void * pointer.  */
8887         code = TYPE_CODE_PTR;
8888         type_flags |= TYPE_FLAG_UNSIGNED;
8889         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8890         break;
8891       case DW_ATE_boolean:
8892         code = TYPE_CODE_BOOL;
8893         type_flags |= TYPE_FLAG_UNSIGNED;
8894         break;
8895       case DW_ATE_complex_float:
8896         code = TYPE_CODE_COMPLEX;
8897         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8898         break;
8899       case DW_ATE_decimal_float:
8900         code = TYPE_CODE_DECFLOAT;
8901         break;
8902       case DW_ATE_float:
8903         code = TYPE_CODE_FLT;
8904         break;
8905       case DW_ATE_signed:
8906         break;
8907       case DW_ATE_unsigned:
8908         type_flags |= TYPE_FLAG_UNSIGNED;
8909         if (cu->language == language_fortran
8910             && name
8911             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8912           code = TYPE_CODE_CHAR;
8913         break;
8914       case DW_ATE_signed_char:
8915         if (cu->language == language_ada || cu->language == language_m2
8916             || cu->language == language_pascal
8917             || cu->language == language_fortran)
8918           code = TYPE_CODE_CHAR;
8919         break;
8920       case DW_ATE_unsigned_char:
8921         if (cu->language == language_ada || cu->language == language_m2
8922             || cu->language == language_pascal
8923             || cu->language == language_fortran)
8924           code = TYPE_CODE_CHAR;
8925         type_flags |= TYPE_FLAG_UNSIGNED;
8926         break;
8927       case DW_ATE_UTF:
8928         /* We just treat this as an integer and then recognize the
8929            type by name elsewhere.  */
8930         break;
8931
8932       default:
8933         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8934                    dwarf_type_encoding_name (encoding));
8935         break;
8936     }
8937
8938   type = init_type (code, size, type_flags, NULL, objfile);
8939   TYPE_NAME (type) = name;
8940   TYPE_TARGET_TYPE (type) = target_type;
8941
8942   if (name && strcmp (name, "char") == 0)
8943     TYPE_NOSIGN (type) = 1;
8944
8945   return set_die_type (die, type, cu);
8946 }
8947
8948 /* Read the given DW_AT_subrange DIE.  */
8949
8950 static struct type *
8951 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8952 {
8953   struct type *base_type;
8954   struct type *range_type;
8955   struct attribute *attr;
8956   LONGEST low = 0;
8957   LONGEST high = -1;
8958   char *name;
8959   LONGEST negative_mask;
8960
8961   base_type = die_type (die, cu);
8962   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8963   check_typedef (base_type);
8964
8965   /* The die_type call above may have already set the type for this DIE.  */
8966   range_type = get_die_type (die, cu);
8967   if (range_type)
8968     return range_type;
8969
8970   if (cu->language == language_fortran)
8971     {
8972       /* FORTRAN implies a lower bound of 1, if not given.  */
8973       low = 1;
8974     }
8975
8976   /* FIXME: For variable sized arrays either of these could be
8977      a variable rather than a constant value.  We'll allow it,
8978      but we don't know how to handle it.  */
8979   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8980   if (attr)
8981     low = dwarf2_get_attr_constant_value (attr, 0);
8982
8983   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8984   if (attr)
8985     {
8986       if (attr_form_is_block (attr) || is_ref_attr (attr))
8987         {
8988           /* GCC encodes arrays with unspecified or dynamic length
8989              with a DW_FORM_block1 attribute or a reference attribute.
8990              FIXME: GDB does not yet know how to handle dynamic
8991              arrays properly, treat them as arrays with unspecified
8992              length for now.
8993
8994              FIXME: jimb/2003-09-22: GDB does not really know
8995              how to handle arrays of unspecified length
8996              either; we just represent them as zero-length
8997              arrays.  Choose an appropriate upper bound given
8998              the lower bound we've computed above.  */
8999           high = low - 1;
9000         }
9001       else
9002         high = dwarf2_get_attr_constant_value (attr, 1);
9003     }
9004   else
9005     {
9006       attr = dwarf2_attr (die, DW_AT_count, cu);
9007       if (attr)
9008         {
9009           int count = dwarf2_get_attr_constant_value (attr, 1);
9010           high = low + count - 1;
9011         }
9012       else
9013         {
9014           /* Unspecified array length.  */
9015           high = low - 1;
9016         }
9017     }
9018
9019   /* Dwarf-2 specifications explicitly allows to create subrange types
9020      without specifying a base type.
9021      In that case, the base type must be set to the type of
9022      the lower bound, upper bound or count, in that order, if any of these
9023      three attributes references an object that has a type.
9024      If no base type is found, the Dwarf-2 specifications say that
9025      a signed integer type of size equal to the size of an address should
9026      be used.
9027      For the following C code: `extern char gdb_int [];'
9028      GCC produces an empty range DIE.
9029      FIXME: muller/2010-05-28: Possible references to object for low bound,
9030      high bound or count are not yet handled by this code.  */
9031   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9032     {
9033       struct objfile *objfile = cu->objfile;
9034       struct gdbarch *gdbarch = get_objfile_arch (objfile);
9035       int addr_size = gdbarch_addr_bit (gdbarch) /8;
9036       struct type *int_type = objfile_type (objfile)->builtin_int;
9037
9038       /* Test "int", "long int", and "long long int" objfile types,
9039          and select the first one having a size above or equal to the
9040          architecture address size.  */
9041       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9042         base_type = int_type;
9043       else
9044         {
9045           int_type = objfile_type (objfile)->builtin_long;
9046           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9047             base_type = int_type;
9048           else
9049             {
9050               int_type = objfile_type (objfile)->builtin_long_long;
9051               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9052                 base_type = int_type;
9053             }
9054         }
9055     }
9056
9057   negative_mask =
9058     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9059   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9060     low |= negative_mask;
9061   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9062     high |= negative_mask;
9063
9064   range_type = create_range_type (NULL, base_type, low, high);
9065
9066   /* Mark arrays with dynamic length at least as an array of unspecified
9067      length.  GDB could check the boundary but before it gets implemented at
9068      least allow accessing the array elements.  */
9069   if (attr && attr_form_is_block (attr))
9070     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9071
9072   /* Ada expects an empty array on no boundary attributes.  */
9073   if (attr == NULL && cu->language != language_ada)
9074     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9075
9076   name = dwarf2_name (die, cu);
9077   if (name)
9078     TYPE_NAME (range_type) = name;
9079
9080   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9081   if (attr)
9082     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9083
9084   set_die_type (die, range_type, cu);
9085
9086   /* set_die_type should be already done.  */
9087   set_descriptive_type (range_type, die, cu);
9088
9089   return range_type;
9090 }
9091
9092 static struct type *
9093 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9094 {
9095   struct type *type;
9096
9097   /* For now, we only support the C meaning of an unspecified type: void.  */
9098
9099   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9100   TYPE_NAME (type) = dwarf2_name (die, cu);
9101
9102   return set_die_type (die, type, cu);
9103 }
9104
9105 /* Trivial hash function for die_info: the hash value of a DIE
9106    is its offset in .debug_info for this objfile.  */
9107
9108 static hashval_t
9109 die_hash (const void *item)
9110 {
9111   const struct die_info *die = item;
9112
9113   return die->offset;
9114 }
9115
9116 /* Trivial comparison function for die_info structures: two DIEs
9117    are equal if they have the same offset.  */
9118
9119 static int
9120 die_eq (const void *item_lhs, const void *item_rhs)
9121 {
9122   const struct die_info *die_lhs = item_lhs;
9123   const struct die_info *die_rhs = item_rhs;
9124
9125   return die_lhs->offset == die_rhs->offset;
9126 }
9127
9128 /* Read a whole compilation unit into a linked list of dies.  */
9129
9130 static struct die_info *
9131 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9132 {
9133   struct die_reader_specs reader_specs;
9134   int read_abbrevs = 0;
9135   struct cleanup *back_to = NULL;
9136   struct die_info *die;
9137
9138   if (cu->dwarf2_abbrevs == NULL)
9139     {
9140       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9141       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9142       read_abbrevs = 1;
9143     }
9144
9145   gdb_assert (cu->die_hash == NULL);
9146   cu->die_hash
9147     = htab_create_alloc_ex (cu->header.length / 12,
9148                             die_hash,
9149                             die_eq,
9150                             NULL,
9151                             &cu->comp_unit_obstack,
9152                             hashtab_obstack_allocate,
9153                             dummy_obstack_deallocate);
9154
9155   init_cu_die_reader (&reader_specs, cu);
9156
9157   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9158
9159   if (read_abbrevs)
9160     do_cleanups (back_to);
9161
9162   return die;
9163 }
9164
9165 /* Main entry point for reading a DIE and all children.
9166    Read the DIE and dump it if requested.  */
9167
9168 static struct die_info *
9169 read_die_and_children (const struct die_reader_specs *reader,
9170                        gdb_byte *info_ptr,
9171                        gdb_byte **new_info_ptr,
9172                        struct die_info *parent)
9173 {
9174   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9175                                                      new_info_ptr, parent);
9176
9177   if (dwarf2_die_debug)
9178     {
9179       fprintf_unfiltered (gdb_stdlog,
9180                           "\nRead die from %s of %s:\n",
9181                           (reader->cu->per_cu->debug_types_section
9182                            ? ".debug_types"
9183                            : ".debug_info"),
9184                           reader->abfd->filename);
9185       dump_die (result, dwarf2_die_debug);
9186     }
9187
9188   return result;
9189 }
9190
9191 /* Read a single die and all its descendents.  Set the die's sibling
9192    field to NULL; set other fields in the die correctly, and set all
9193    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9194    location of the info_ptr after reading all of those dies.  PARENT
9195    is the parent of the die in question.  */
9196
9197 static struct die_info *
9198 read_die_and_children_1 (const struct die_reader_specs *reader,
9199                          gdb_byte *info_ptr,
9200                          gdb_byte **new_info_ptr,
9201                          struct die_info *parent)
9202 {
9203   struct die_info *die;
9204   gdb_byte *cur_ptr;
9205   int has_children;
9206
9207   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9208   if (die == NULL)
9209     {
9210       *new_info_ptr = cur_ptr;
9211       return NULL;
9212     }
9213   store_in_ref_table (die, reader->cu);
9214
9215   if (has_children)
9216     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9217   else
9218     {
9219       die->child = NULL;
9220       *new_info_ptr = cur_ptr;
9221     }
9222
9223   die->sibling = NULL;
9224   die->parent = parent;
9225   return die;
9226 }
9227
9228 /* Read a die, all of its descendents, and all of its siblings; set
9229    all of the fields of all of the dies correctly.  Arguments are as
9230    in read_die_and_children.  */
9231
9232 static struct die_info *
9233 read_die_and_siblings (const struct die_reader_specs *reader,
9234                        gdb_byte *info_ptr,
9235                        gdb_byte **new_info_ptr,
9236                        struct die_info *parent)
9237 {
9238   struct die_info *first_die, *last_sibling;
9239   gdb_byte *cur_ptr;
9240
9241   cur_ptr = info_ptr;
9242   first_die = last_sibling = NULL;
9243
9244   while (1)
9245     {
9246       struct die_info *die
9247         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9248
9249       if (die == NULL)
9250         {
9251           *new_info_ptr = cur_ptr;
9252           return first_die;
9253         }
9254
9255       if (!first_die)
9256         first_die = die;
9257       else
9258         last_sibling->sibling = die;
9259
9260       last_sibling = die;
9261     }
9262 }
9263
9264 /* Read the die from the .debug_info section buffer.  Set DIEP to
9265    point to a newly allocated die with its information, except for its
9266    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9267    whether the die has children or not.  */
9268
9269 static gdb_byte *
9270 read_full_die (const struct die_reader_specs *reader,
9271                struct die_info **diep, gdb_byte *info_ptr,
9272                int *has_children)
9273 {
9274   unsigned int abbrev_number, bytes_read, i, offset;
9275   struct abbrev_info *abbrev;
9276   struct die_info *die;
9277   struct dwarf2_cu *cu = reader->cu;
9278   bfd *abfd = reader->abfd;
9279
9280   offset = info_ptr - reader->buffer;
9281   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9282   info_ptr += bytes_read;
9283   if (!abbrev_number)
9284     {
9285       *diep = NULL;
9286       *has_children = 0;
9287       return info_ptr;
9288     }
9289
9290   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9291   if (!abbrev)
9292     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9293            abbrev_number,
9294            bfd_get_filename (abfd));
9295
9296   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9297   die->offset = offset;
9298   die->tag = abbrev->tag;
9299   die->abbrev = abbrev_number;
9300
9301   die->num_attrs = abbrev->num_attrs;
9302
9303   for (i = 0; i < abbrev->num_attrs; ++i)
9304     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9305                                abfd, info_ptr, cu);
9306
9307   *diep = die;
9308   *has_children = abbrev->has_children;
9309   return info_ptr;
9310 }
9311
9312 /* In DWARF version 2, the description of the debugging information is
9313    stored in a separate .debug_abbrev section.  Before we read any
9314    dies from a section we read in all abbreviations and install them
9315    in a hash table.  This function also sets flags in CU describing
9316    the data found in the abbrev table.  */
9317
9318 static void
9319 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9320 {
9321   struct comp_unit_head *cu_header = &cu->header;
9322   gdb_byte *abbrev_ptr;
9323   struct abbrev_info *cur_abbrev;
9324   unsigned int abbrev_number, bytes_read, abbrev_name;
9325   unsigned int abbrev_form, hash_number;
9326   struct attr_abbrev *cur_attrs;
9327   unsigned int allocated_attrs;
9328
9329   /* Initialize dwarf2 abbrevs.  */
9330   obstack_init (&cu->abbrev_obstack);
9331   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9332                                       (ABBREV_HASH_SIZE
9333                                        * sizeof (struct abbrev_info *)));
9334   memset (cu->dwarf2_abbrevs, 0,
9335           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9336
9337   dwarf2_read_section (dwarf2_per_objfile->objfile,
9338                        &dwarf2_per_objfile->abbrev);
9339   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9340   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9341   abbrev_ptr += bytes_read;
9342
9343   allocated_attrs = ATTR_ALLOC_CHUNK;
9344   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9345
9346   /* Loop until we reach an abbrev number of 0.  */
9347   while (abbrev_number)
9348     {
9349       cur_abbrev = dwarf_alloc_abbrev (cu);
9350
9351       /* read in abbrev header */
9352       cur_abbrev->number = abbrev_number;
9353       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9354       abbrev_ptr += bytes_read;
9355       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9356       abbrev_ptr += 1;
9357
9358       if (cur_abbrev->tag == DW_TAG_namespace)
9359         cu->has_namespace_info = 1;
9360
9361       /* now read in declarations */
9362       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9363       abbrev_ptr += bytes_read;
9364       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9365       abbrev_ptr += bytes_read;
9366       while (abbrev_name)
9367         {
9368           if (cur_abbrev->num_attrs == allocated_attrs)
9369             {
9370               allocated_attrs += ATTR_ALLOC_CHUNK;
9371               cur_attrs
9372                 = xrealloc (cur_attrs, (allocated_attrs
9373                                         * sizeof (struct attr_abbrev)));
9374             }
9375
9376           /* Record whether this compilation unit might have
9377              inter-compilation-unit references.  If we don't know what form
9378              this attribute will have, then it might potentially be a
9379              DW_FORM_ref_addr, so we conservatively expect inter-CU
9380              references.  */
9381
9382           if (abbrev_form == DW_FORM_ref_addr
9383               || abbrev_form == DW_FORM_indirect)
9384             cu->has_form_ref_addr = 1;
9385
9386           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9387           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9388           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9389           abbrev_ptr += bytes_read;
9390           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9391           abbrev_ptr += bytes_read;
9392         }
9393
9394       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9395                                          (cur_abbrev->num_attrs
9396                                           * sizeof (struct attr_abbrev)));
9397       memcpy (cur_abbrev->attrs, cur_attrs,
9398               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9399
9400       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9401       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9402       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9403
9404       /* Get next abbreviation.
9405          Under Irix6 the abbreviations for a compilation unit are not
9406          always properly terminated with an abbrev number of 0.
9407          Exit loop if we encounter an abbreviation which we have
9408          already read (which means we are about to read the abbreviations
9409          for the next compile unit) or if the end of the abbreviation
9410          table is reached.  */
9411       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9412           >= dwarf2_per_objfile->abbrev.size)
9413         break;
9414       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9415       abbrev_ptr += bytes_read;
9416       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9417         break;
9418     }
9419
9420   xfree (cur_attrs);
9421 }
9422
9423 /* Release the memory used by the abbrev table for a compilation unit.  */
9424
9425 static void
9426 dwarf2_free_abbrev_table (void *ptr_to_cu)
9427 {
9428   struct dwarf2_cu *cu = ptr_to_cu;
9429
9430   obstack_free (&cu->abbrev_obstack, NULL);
9431   cu->dwarf2_abbrevs = NULL;
9432 }
9433
9434 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9435
9436 static struct abbrev_info *
9437 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9438 {
9439   unsigned int hash_number;
9440   struct abbrev_info *abbrev;
9441
9442   hash_number = number % ABBREV_HASH_SIZE;
9443   abbrev = cu->dwarf2_abbrevs[hash_number];
9444
9445   while (abbrev)
9446     {
9447       if (abbrev->number == number)
9448         return abbrev;
9449       else
9450         abbrev = abbrev->next;
9451     }
9452   return NULL;
9453 }
9454
9455 /* Returns nonzero if TAG represents a type that we might generate a partial
9456    symbol for.  */
9457
9458 static int
9459 is_type_tag_for_partial (int tag)
9460 {
9461   switch (tag)
9462     {
9463 #if 0
9464     /* Some types that would be reasonable to generate partial symbols for,
9465        that we don't at present.  */
9466     case DW_TAG_array_type:
9467     case DW_TAG_file_type:
9468     case DW_TAG_ptr_to_member_type:
9469     case DW_TAG_set_type:
9470     case DW_TAG_string_type:
9471     case DW_TAG_subroutine_type:
9472 #endif
9473     case DW_TAG_base_type:
9474     case DW_TAG_class_type:
9475     case DW_TAG_interface_type:
9476     case DW_TAG_enumeration_type:
9477     case DW_TAG_structure_type:
9478     case DW_TAG_subrange_type:
9479     case DW_TAG_typedef:
9480     case DW_TAG_union_type:
9481       return 1;
9482     default:
9483       return 0;
9484     }
9485 }
9486
9487 /* Load all DIEs that are interesting for partial symbols into memory.  */
9488
9489 static struct partial_die_info *
9490 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9491                    int building_psymtab, struct dwarf2_cu *cu)
9492 {
9493   struct objfile *objfile = cu->objfile;
9494   struct partial_die_info *part_die;
9495   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9496   struct abbrev_info *abbrev;
9497   unsigned int bytes_read;
9498   unsigned int load_all = 0;
9499
9500   int nesting_level = 1;
9501
9502   parent_die = NULL;
9503   last_die = NULL;
9504
9505   if (cu->per_cu && cu->per_cu->load_all_dies)
9506     load_all = 1;
9507
9508   cu->partial_dies
9509     = htab_create_alloc_ex (cu->header.length / 12,
9510                             partial_die_hash,
9511                             partial_die_eq,
9512                             NULL,
9513                             &cu->comp_unit_obstack,
9514                             hashtab_obstack_allocate,
9515                             dummy_obstack_deallocate);
9516
9517   part_die = obstack_alloc (&cu->comp_unit_obstack,
9518                             sizeof (struct partial_die_info));
9519
9520   while (1)
9521     {
9522       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9523
9524       /* A NULL abbrev means the end of a series of children.  */
9525       if (abbrev == NULL)
9526         {
9527           if (--nesting_level == 0)
9528             {
9529               /* PART_DIE was probably the last thing allocated on the
9530                  comp_unit_obstack, so we could call obstack_free
9531                  here.  We don't do that because the waste is small,
9532                  and will be cleaned up when we're done with this
9533                  compilation unit.  This way, we're also more robust
9534                  against other users of the comp_unit_obstack.  */
9535               return first_die;
9536             }
9537           info_ptr += bytes_read;
9538           last_die = parent_die;
9539           parent_die = parent_die->die_parent;
9540           continue;
9541         }
9542
9543       /* Check for template arguments.  We never save these; if
9544          they're seen, we just mark the parent, and go on our way.  */
9545       if (parent_die != NULL
9546           && cu->language == language_cplus
9547           && (abbrev->tag == DW_TAG_template_type_param
9548               || abbrev->tag == DW_TAG_template_value_param))
9549         {
9550           parent_die->has_template_arguments = 1;
9551
9552           if (!load_all)
9553             {
9554               /* We don't need a partial DIE for the template argument.  */
9555               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9556                                        cu);
9557               continue;
9558             }
9559         }
9560
9561       /* We only recurse into subprograms looking for template arguments.
9562          Skip their other children.  */
9563       if (!load_all
9564           && cu->language == language_cplus
9565           && parent_die != NULL
9566           && parent_die->tag == DW_TAG_subprogram)
9567         {
9568           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9569           continue;
9570         }
9571
9572       /* Check whether this DIE is interesting enough to save.  Normally
9573          we would not be interested in members here, but there may be
9574          later variables referencing them via DW_AT_specification (for
9575          static members).  */
9576       if (!load_all
9577           && !is_type_tag_for_partial (abbrev->tag)
9578           && abbrev->tag != DW_TAG_constant
9579           && abbrev->tag != DW_TAG_enumerator
9580           && abbrev->tag != DW_TAG_subprogram
9581           && abbrev->tag != DW_TAG_lexical_block
9582           && abbrev->tag != DW_TAG_variable
9583           && abbrev->tag != DW_TAG_namespace
9584           && abbrev->tag != DW_TAG_module
9585           && abbrev->tag != DW_TAG_member)
9586         {
9587           /* Otherwise we skip to the next sibling, if any.  */
9588           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9589           continue;
9590         }
9591
9592       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9593                                    buffer, info_ptr, cu);
9594
9595       /* This two-pass algorithm for processing partial symbols has a
9596          high cost in cache pressure.  Thus, handle some simple cases
9597          here which cover the majority of C partial symbols.  DIEs
9598          which neither have specification tags in them, nor could have
9599          specification tags elsewhere pointing at them, can simply be
9600          processed and discarded.
9601
9602          This segment is also optional; scan_partial_symbols and
9603          add_partial_symbol will handle these DIEs if we chain
9604          them in normally.  When compilers which do not emit large
9605          quantities of duplicate debug information are more common,
9606          this code can probably be removed.  */
9607
9608       /* Any complete simple types at the top level (pretty much all
9609          of them, for a language without namespaces), can be processed
9610          directly.  */
9611       if (parent_die == NULL
9612           && part_die->has_specification == 0
9613           && part_die->is_declaration == 0
9614           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9615               || part_die->tag == DW_TAG_base_type
9616               || part_die->tag == DW_TAG_subrange_type))
9617         {
9618           if (building_psymtab && part_die->name != NULL)
9619             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9620                                  VAR_DOMAIN, LOC_TYPEDEF,
9621                                  &objfile->static_psymbols,
9622                                  0, (CORE_ADDR) 0, cu->language, objfile);
9623           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9624           continue;
9625         }
9626
9627       /* The exception for DW_TAG_typedef with has_children above is
9628          a workaround of GCC PR debug/47510.  In the case of this complaint
9629          type_name_no_tag_or_error will error on such types later.
9630
9631          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9632          it could not find the child DIEs referenced later, this is checked
9633          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9634
9635       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9636         complaint (&symfile_complaints,
9637                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9638                      "- DIE at 0x%x [in module %s]"),
9639                    part_die->offset, objfile->name);
9640
9641       /* If we're at the second level, and we're an enumerator, and
9642          our parent has no specification (meaning possibly lives in a
9643          namespace elsewhere), then we can add the partial symbol now
9644          instead of queueing it.  */
9645       if (part_die->tag == DW_TAG_enumerator
9646           && parent_die != NULL
9647           && parent_die->die_parent == NULL
9648           && parent_die->tag == DW_TAG_enumeration_type
9649           && parent_die->has_specification == 0)
9650         {
9651           if (part_die->name == NULL)
9652             complaint (&symfile_complaints,
9653                        _("malformed enumerator DIE ignored"));
9654           else if (building_psymtab)
9655             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9656                                  VAR_DOMAIN, LOC_CONST,
9657                                  (cu->language == language_cplus
9658                                   || cu->language == language_java)
9659                                  ? &objfile->global_psymbols
9660                                  : &objfile->static_psymbols,
9661                                  0, (CORE_ADDR) 0, cu->language, objfile);
9662
9663           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9664           continue;
9665         }
9666
9667       /* We'll save this DIE so link it in.  */
9668       part_die->die_parent = parent_die;
9669       part_die->die_sibling = NULL;
9670       part_die->die_child = NULL;
9671
9672       if (last_die && last_die == parent_die)
9673         last_die->die_child = part_die;
9674       else if (last_die)
9675         last_die->die_sibling = part_die;
9676
9677       last_die = part_die;
9678
9679       if (first_die == NULL)
9680         first_die = part_die;
9681
9682       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9683          find interesting need to be in the hash table, because we
9684          also have the parent/sibling/child chains; only those that we
9685          might refer to by offset later during partial symbol reading.
9686
9687          For now this means things that might have be the target of a
9688          DW_AT_specification, DW_AT_abstract_origin, or
9689          DW_AT_extension.  DW_AT_extension will refer only to
9690          namespaces; DW_AT_abstract_origin refers to functions (and
9691          many things under the function DIE, but we do not recurse
9692          into function DIEs during partial symbol reading) and
9693          possibly variables as well; DW_AT_specification refers to
9694          declarations.  Declarations ought to have the DW_AT_declaration
9695          flag.  It happens that GCC forgets to put it in sometimes, but
9696          only for functions, not for types.
9697
9698          Adding more things than necessary to the hash table is harmless
9699          except for the performance cost.  Adding too few will result in
9700          wasted time in find_partial_die, when we reread the compilation
9701          unit with load_all_dies set.  */
9702
9703       if (load_all
9704           || abbrev->tag == DW_TAG_constant
9705           || abbrev->tag == DW_TAG_subprogram
9706           || abbrev->tag == DW_TAG_variable
9707           || abbrev->tag == DW_TAG_namespace
9708           || part_die->is_declaration)
9709         {
9710           void **slot;
9711
9712           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9713                                            part_die->offset, INSERT);
9714           *slot = part_die;
9715         }
9716
9717       part_die = obstack_alloc (&cu->comp_unit_obstack,
9718                                 sizeof (struct partial_die_info));
9719
9720       /* For some DIEs we want to follow their children (if any).  For C
9721          we have no reason to follow the children of structures; for other
9722          languages we have to, so that we can get at method physnames
9723          to infer fully qualified class names, for DW_AT_specification,
9724          and for C++ template arguments.  For C++, we also look one level
9725          inside functions to find template arguments (if the name of the
9726          function does not already contain the template arguments).
9727
9728          For Ada, we need to scan the children of subprograms and lexical
9729          blocks as well because Ada allows the definition of nested
9730          entities that could be interesting for the debugger, such as
9731          nested subprograms for instance.  */
9732       if (last_die->has_children
9733           && (load_all
9734               || last_die->tag == DW_TAG_namespace
9735               || last_die->tag == DW_TAG_module
9736               || last_die->tag == DW_TAG_enumeration_type
9737               || (cu->language == language_cplus
9738                   && last_die->tag == DW_TAG_subprogram
9739                   && (last_die->name == NULL
9740                       || strchr (last_die->name, '<') == NULL))
9741               || (cu->language != language_c
9742                   && (last_die->tag == DW_TAG_class_type
9743                       || last_die->tag == DW_TAG_interface_type
9744                       || last_die->tag == DW_TAG_structure_type
9745                       || last_die->tag == DW_TAG_union_type))
9746               || (cu->language == language_ada
9747                   && (last_die->tag == DW_TAG_subprogram
9748                       || last_die->tag == DW_TAG_lexical_block))))
9749         {
9750           nesting_level++;
9751           parent_die = last_die;
9752           continue;
9753         }
9754
9755       /* Otherwise we skip to the next sibling, if any.  */
9756       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9757
9758       /* Back to the top, do it again.  */
9759     }
9760 }
9761
9762 /* Read a minimal amount of information into the minimal die structure.  */
9763
9764 static gdb_byte *
9765 read_partial_die (struct partial_die_info *part_die,
9766                   struct abbrev_info *abbrev,
9767                   unsigned int abbrev_len, bfd *abfd,
9768                   gdb_byte *buffer, gdb_byte *info_ptr,
9769                   struct dwarf2_cu *cu)
9770 {
9771   struct objfile *objfile = cu->objfile;
9772   unsigned int i;
9773   struct attribute attr;
9774   int has_low_pc_attr = 0;
9775   int has_high_pc_attr = 0;
9776
9777   memset (part_die, 0, sizeof (struct partial_die_info));
9778
9779   part_die->offset = info_ptr - buffer;
9780
9781   info_ptr += abbrev_len;
9782
9783   if (abbrev == NULL)
9784     return info_ptr;
9785
9786   part_die->tag = abbrev->tag;
9787   part_die->has_children = abbrev->has_children;
9788
9789   for (i = 0; i < abbrev->num_attrs; ++i)
9790     {
9791       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9792
9793       /* Store the data if it is of an attribute we want to keep in a
9794          partial symbol table.  */
9795       switch (attr.name)
9796         {
9797         case DW_AT_name:
9798           switch (part_die->tag)
9799             {
9800             case DW_TAG_compile_unit:
9801             case DW_TAG_type_unit:
9802               /* Compilation units have a DW_AT_name that is a filename, not
9803                  a source language identifier.  */
9804             case DW_TAG_enumeration_type:
9805             case DW_TAG_enumerator:
9806               /* These tags always have simple identifiers already; no need
9807                  to canonicalize them.  */
9808               part_die->name = DW_STRING (&attr);
9809               break;
9810             default:
9811               part_die->name
9812                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9813                                             &objfile->objfile_obstack);
9814               break;
9815             }
9816           break;
9817         case DW_AT_linkage_name:
9818         case DW_AT_MIPS_linkage_name:
9819           /* Note that both forms of linkage name might appear.  We
9820              assume they will be the same, and we only store the last
9821              one we see.  */
9822           if (cu->language == language_ada)
9823             part_die->name = DW_STRING (&attr);
9824           part_die->linkage_name = DW_STRING (&attr);
9825           break;
9826         case DW_AT_low_pc:
9827           has_low_pc_attr = 1;
9828           part_die->lowpc = DW_ADDR (&attr);
9829           break;
9830         case DW_AT_high_pc:
9831           has_high_pc_attr = 1;
9832           part_die->highpc = DW_ADDR (&attr);
9833           break;
9834         case DW_AT_location:
9835           /* Support the .debug_loc offsets.  */
9836           if (attr_form_is_block (&attr))
9837             {
9838                part_die->locdesc = DW_BLOCK (&attr);
9839             }
9840           else if (attr_form_is_section_offset (&attr))
9841             {
9842               dwarf2_complex_location_expr_complaint ();
9843             }
9844           else
9845             {
9846               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9847                                                      "partial symbol information");
9848             }
9849           break;
9850         case DW_AT_external:
9851           part_die->is_external = DW_UNSND (&attr);
9852           break;
9853         case DW_AT_declaration:
9854           part_die->is_declaration = DW_UNSND (&attr);
9855           break;
9856         case DW_AT_type:
9857           part_die->has_type = 1;
9858           break;
9859         case DW_AT_abstract_origin:
9860         case DW_AT_specification:
9861         case DW_AT_extension:
9862           part_die->has_specification = 1;
9863           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9864           break;
9865         case DW_AT_sibling:
9866           /* Ignore absolute siblings, they might point outside of
9867              the current compile unit.  */
9868           if (attr.form == DW_FORM_ref_addr)
9869             complaint (&symfile_complaints,
9870                        _("ignoring absolute DW_AT_sibling"));
9871           else
9872             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9873           break;
9874         case DW_AT_byte_size:
9875           part_die->has_byte_size = 1;
9876           break;
9877         case DW_AT_calling_convention:
9878           /* DWARF doesn't provide a way to identify a program's source-level
9879              entry point.  DW_AT_calling_convention attributes are only meant
9880              to describe functions' calling conventions.
9881
9882              However, because it's a necessary piece of information in
9883              Fortran, and because DW_CC_program is the only piece of debugging
9884              information whose definition refers to a 'main program' at all,
9885              several compilers have begun marking Fortran main programs with
9886              DW_CC_program --- even when those functions use the standard
9887              calling conventions.
9888
9889              So until DWARF specifies a way to provide this information and
9890              compilers pick up the new representation, we'll support this
9891              practice.  */
9892           if (DW_UNSND (&attr) == DW_CC_program
9893               && cu->language == language_fortran)
9894             {
9895               set_main_name (part_die->name);
9896
9897               /* As this DIE has a static linkage the name would be difficult
9898                  to look up later.  */
9899               language_of_main = language_fortran;
9900             }
9901           break;
9902         default:
9903           break;
9904         }
9905     }
9906
9907   if (has_low_pc_attr && has_high_pc_attr)
9908     {
9909       /* When using the GNU linker, .gnu.linkonce. sections are used to
9910          eliminate duplicate copies of functions and vtables and such.
9911          The linker will arbitrarily choose one and discard the others.
9912          The AT_*_pc values for such functions refer to local labels in
9913          these sections.  If the section from that file was discarded, the
9914          labels are not in the output, so the relocs get a value of 0.
9915          If this is a discarded function, mark the pc bounds as invalid,
9916          so that GDB will ignore it.  */
9917       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9918         {
9919           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9920
9921           complaint (&symfile_complaints,
9922                      _("DW_AT_low_pc %s is zero "
9923                        "for DIE at 0x%x [in module %s]"),
9924                      paddress (gdbarch, part_die->lowpc),
9925                      part_die->offset, objfile->name);
9926         }
9927       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9928       else if (part_die->lowpc >= part_die->highpc)
9929         {
9930           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9931
9932           complaint (&symfile_complaints,
9933                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9934                        "for DIE at 0x%x [in module %s]"),
9935                      paddress (gdbarch, part_die->lowpc),
9936                      paddress (gdbarch, part_die->highpc),
9937                      part_die->offset, objfile->name);
9938         }
9939       else
9940         part_die->has_pc_info = 1;
9941     }
9942
9943   return info_ptr;
9944 }
9945
9946 /* Find a cached partial DIE at OFFSET in CU.  */
9947
9948 static struct partial_die_info *
9949 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9950 {
9951   struct partial_die_info *lookup_die = NULL;
9952   struct partial_die_info part_die;
9953
9954   part_die.offset = offset;
9955   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9956
9957   return lookup_die;
9958 }
9959
9960 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9961    except in the case of .debug_types DIEs which do not reference
9962    outside their CU (they do however referencing other types via
9963    DW_FORM_ref_sig8).  */
9964
9965 static struct partial_die_info *
9966 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9967 {
9968   struct objfile *objfile = cu->objfile;
9969   struct dwarf2_per_cu_data *per_cu = NULL;
9970   struct partial_die_info *pd = NULL;
9971
9972   if (cu->per_cu->debug_types_section)
9973     {
9974       pd = find_partial_die_in_comp_unit (offset, cu);
9975       if (pd != NULL)
9976         return pd;
9977       goto not_found;
9978     }
9979
9980   if (offset_in_cu_p (&cu->header, offset))
9981     {
9982       pd = find_partial_die_in_comp_unit (offset, cu);
9983       if (pd != NULL)
9984         return pd;
9985     }
9986
9987   per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9988
9989   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9990     load_partial_comp_unit (per_cu);
9991
9992   per_cu->cu->last_used = 0;
9993   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9994
9995   if (pd == NULL && per_cu->load_all_dies == 0)
9996     {
9997       struct cleanup *back_to;
9998       struct partial_die_info comp_unit_die;
9999       struct abbrev_info *abbrev;
10000       unsigned int bytes_read;
10001       char *info_ptr;
10002
10003       per_cu->load_all_dies = 1;
10004
10005       /* Re-read the DIEs.  */
10006       back_to = make_cleanup (null_cleanup, 0);
10007       if (per_cu->cu->dwarf2_abbrevs == NULL)
10008         {
10009           dwarf2_read_abbrevs (objfile->obfd, per_cu->cu);
10010           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10011         }
10012       info_ptr = (dwarf2_per_objfile->info.buffer
10013                   + per_cu->cu->header.offset
10014                   + per_cu->cu->header.first_die_offset);
10015       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10016       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10017                                    objfile->obfd,
10018                                    dwarf2_per_objfile->info.buffer, info_ptr,
10019                                    per_cu->cu);
10020       if (comp_unit_die.has_children)
10021         load_partial_dies (objfile->obfd,
10022                            dwarf2_per_objfile->info.buffer, info_ptr,
10023                            0, per_cu->cu);
10024       do_cleanups (back_to);
10025
10026       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10027     }
10028
10029  not_found:
10030
10031   if (pd == NULL)
10032     internal_error (__FILE__, __LINE__,
10033                     _("could not find partial DIE 0x%x "
10034                       "in cache [from module %s]\n"),
10035                     offset, bfd_get_filename (objfile->obfd));
10036   return pd;
10037 }
10038
10039 /* See if we can figure out if the class lives in a namespace.  We do
10040    this by looking for a member function; its demangled name will
10041    contain namespace info, if there is any.  */
10042
10043 static void
10044 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10045                                   struct dwarf2_cu *cu)
10046 {
10047   /* NOTE: carlton/2003-10-07: Getting the info this way changes
10048      what template types look like, because the demangler
10049      frequently doesn't give the same name as the debug info.  We
10050      could fix this by only using the demangled name to get the
10051      prefix (but see comment in read_structure_type).  */
10052
10053   struct partial_die_info *real_pdi;
10054   struct partial_die_info *child_pdi;
10055
10056   /* If this DIE (this DIE's specification, if any) has a parent, then
10057      we should not do this.  We'll prepend the parent's fully qualified
10058      name when we create the partial symbol.  */
10059
10060   real_pdi = struct_pdi;
10061   while (real_pdi->has_specification)
10062     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10063
10064   if (real_pdi->die_parent != NULL)
10065     return;
10066
10067   for (child_pdi = struct_pdi->die_child;
10068        child_pdi != NULL;
10069        child_pdi = child_pdi->die_sibling)
10070     {
10071       if (child_pdi->tag == DW_TAG_subprogram
10072           && child_pdi->linkage_name != NULL)
10073         {
10074           char *actual_class_name
10075             = language_class_name_from_physname (cu->language_defn,
10076                                                  child_pdi->linkage_name);
10077           if (actual_class_name != NULL)
10078             {
10079               struct_pdi->name
10080                 = obsavestring (actual_class_name,
10081                                 strlen (actual_class_name),
10082                                 &cu->objfile->objfile_obstack);
10083               xfree (actual_class_name);
10084             }
10085           break;
10086         }
10087     }
10088 }
10089
10090 /* Adjust PART_DIE before generating a symbol for it.  This function
10091    may set the is_external flag or change the DIE's name.  */
10092
10093 static void
10094 fixup_partial_die (struct partial_die_info *part_die,
10095                    struct dwarf2_cu *cu)
10096 {
10097   /* Once we've fixed up a die, there's no point in doing so again.
10098      This also avoids a memory leak if we were to call
10099      guess_partial_die_structure_name multiple times.  */
10100   if (part_die->fixup_called)
10101     return;
10102
10103   /* If we found a reference attribute and the DIE has no name, try
10104      to find a name in the referred to DIE.  */
10105
10106   if (part_die->name == NULL && part_die->has_specification)
10107     {
10108       struct partial_die_info *spec_die;
10109
10110       spec_die = find_partial_die (part_die->spec_offset, cu);
10111
10112       fixup_partial_die (spec_die, cu);
10113
10114       if (spec_die->name)
10115         {
10116           part_die->name = spec_die->name;
10117
10118           /* Copy DW_AT_external attribute if it is set.  */
10119           if (spec_die->is_external)
10120             part_die->is_external = spec_die->is_external;
10121         }
10122     }
10123
10124   /* Set default names for some unnamed DIEs.  */
10125
10126   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10127     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10128
10129   /* If there is no parent die to provide a namespace, and there are
10130      children, see if we can determine the namespace from their linkage
10131      name.
10132      NOTE: We need to do this even if cu->has_namespace_info != 0.
10133      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
10134   if (cu->language == language_cplus
10135       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10136       && part_die->die_parent == NULL
10137       && part_die->has_children
10138       && (part_die->tag == DW_TAG_class_type
10139           || part_die->tag == DW_TAG_structure_type
10140           || part_die->tag == DW_TAG_union_type))
10141     guess_partial_die_structure_name (part_die, cu);
10142
10143   /* GCC might emit a nameless struct or union that has a linkage
10144      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10145   if (part_die->name == NULL
10146       && (part_die->tag == DW_TAG_class_type
10147           || part_die->tag == DW_TAG_interface_type
10148           || part_die->tag == DW_TAG_structure_type
10149           || part_die->tag == DW_TAG_union_type)
10150       && part_die->linkage_name != NULL)
10151     {
10152       char *demangled;
10153
10154       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10155       if (demangled)
10156         {
10157           const char *base;
10158
10159           /* Strip any leading namespaces/classes, keep only the base name.
10160              DW_AT_name for named DIEs does not contain the prefixes.  */
10161           base = strrchr (demangled, ':');
10162           if (base && base > demangled && base[-1] == ':')
10163             base++;
10164           else
10165             base = demangled;
10166
10167           part_die->name = obsavestring (base, strlen (base),
10168                                          &cu->objfile->objfile_obstack);
10169           xfree (demangled);
10170         }
10171     }
10172
10173   part_die->fixup_called = 1;
10174 }
10175
10176 /* Read an attribute value described by an attribute form.  */
10177
10178 static gdb_byte *
10179 read_attribute_value (struct attribute *attr, unsigned form,
10180                       bfd *abfd, gdb_byte *info_ptr,
10181                       struct dwarf2_cu *cu)
10182 {
10183   struct comp_unit_head *cu_header = &cu->header;
10184   unsigned int bytes_read;
10185   struct dwarf_block *blk;
10186
10187   attr->form = form;
10188   switch (form)
10189     {
10190     case DW_FORM_ref_addr:
10191       if (cu->header.version == 2)
10192         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10193       else
10194         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10195                                       &cu->header, &bytes_read);
10196       info_ptr += bytes_read;
10197       break;
10198     case DW_FORM_addr:
10199       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10200       info_ptr += bytes_read;
10201       break;
10202     case DW_FORM_block2:
10203       blk = dwarf_alloc_block (cu);
10204       blk->size = read_2_bytes (abfd, info_ptr);
10205       info_ptr += 2;
10206       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10207       info_ptr += blk->size;
10208       DW_BLOCK (attr) = blk;
10209       break;
10210     case DW_FORM_block4:
10211       blk = dwarf_alloc_block (cu);
10212       blk->size = read_4_bytes (abfd, info_ptr);
10213       info_ptr += 4;
10214       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10215       info_ptr += blk->size;
10216       DW_BLOCK (attr) = blk;
10217       break;
10218     case DW_FORM_data2:
10219       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10220       info_ptr += 2;
10221       break;
10222     case DW_FORM_data4:
10223       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10224       info_ptr += 4;
10225       break;
10226     case DW_FORM_data8:
10227       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10228       info_ptr += 8;
10229       break;
10230     case DW_FORM_sec_offset:
10231       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10232       info_ptr += bytes_read;
10233       break;
10234     case DW_FORM_string:
10235       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10236       DW_STRING_IS_CANONICAL (attr) = 0;
10237       info_ptr += bytes_read;
10238       break;
10239     case DW_FORM_strp:
10240       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10241                                                &bytes_read);
10242       DW_STRING_IS_CANONICAL (attr) = 0;
10243       info_ptr += bytes_read;
10244       break;
10245     case DW_FORM_exprloc:
10246     case DW_FORM_block:
10247       blk = dwarf_alloc_block (cu);
10248       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10249       info_ptr += bytes_read;
10250       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10251       info_ptr += blk->size;
10252       DW_BLOCK (attr) = blk;
10253       break;
10254     case DW_FORM_block1:
10255       blk = dwarf_alloc_block (cu);
10256       blk->size = read_1_byte (abfd, info_ptr);
10257       info_ptr += 1;
10258       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10259       info_ptr += blk->size;
10260       DW_BLOCK (attr) = blk;
10261       break;
10262     case DW_FORM_data1:
10263       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10264       info_ptr += 1;
10265       break;
10266     case DW_FORM_flag:
10267       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10268       info_ptr += 1;
10269       break;
10270     case DW_FORM_flag_present:
10271       DW_UNSND (attr) = 1;
10272       break;
10273     case DW_FORM_sdata:
10274       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10275       info_ptr += bytes_read;
10276       break;
10277     case DW_FORM_udata:
10278       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10279       info_ptr += bytes_read;
10280       break;
10281     case DW_FORM_ref1:
10282       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10283       info_ptr += 1;
10284       break;
10285     case DW_FORM_ref2:
10286       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10287       info_ptr += 2;
10288       break;
10289     case DW_FORM_ref4:
10290       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10291       info_ptr += 4;
10292       break;
10293     case DW_FORM_ref8:
10294       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10295       info_ptr += 8;
10296       break;
10297     case DW_FORM_ref_sig8:
10298       /* Convert the signature to something we can record in DW_UNSND
10299          for later lookup.
10300          NOTE: This is NULL if the type wasn't found.  */
10301       DW_SIGNATURED_TYPE (attr) =
10302         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10303       info_ptr += 8;
10304       break;
10305     case DW_FORM_ref_udata:
10306       DW_ADDR (attr) = (cu->header.offset
10307                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10308       info_ptr += bytes_read;
10309       break;
10310     case DW_FORM_indirect:
10311       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10312       info_ptr += bytes_read;
10313       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10314       break;
10315     default:
10316       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10317              dwarf_form_name (form),
10318              bfd_get_filename (abfd));
10319     }
10320
10321   /* We have seen instances where the compiler tried to emit a byte
10322      size attribute of -1 which ended up being encoded as an unsigned
10323      0xffffffff.  Although 0xffffffff is technically a valid size value,
10324      an object of this size seems pretty unlikely so we can relatively
10325      safely treat these cases as if the size attribute was invalid and
10326      treat them as zero by default.  */
10327   if (attr->name == DW_AT_byte_size
10328       && form == DW_FORM_data4
10329       && DW_UNSND (attr) >= 0xffffffff)
10330     {
10331       complaint
10332         (&symfile_complaints,
10333          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10334          hex_string (DW_UNSND (attr)));
10335       DW_UNSND (attr) = 0;
10336     }
10337
10338   return info_ptr;
10339 }
10340
10341 /* Read an attribute described by an abbreviated attribute.  */
10342
10343 static gdb_byte *
10344 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10345                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10346 {
10347   attr->name = abbrev->name;
10348   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10349 }
10350
10351 /* Read dwarf information from a buffer.  */
10352
10353 static unsigned int
10354 read_1_byte (bfd *abfd, gdb_byte *buf)
10355 {
10356   return bfd_get_8 (abfd, buf);
10357 }
10358
10359 static int
10360 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10361 {
10362   return bfd_get_signed_8 (abfd, buf);
10363 }
10364
10365 static unsigned int
10366 read_2_bytes (bfd *abfd, gdb_byte *buf)
10367 {
10368   return bfd_get_16 (abfd, buf);
10369 }
10370
10371 static int
10372 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10373 {
10374   return bfd_get_signed_16 (abfd, buf);
10375 }
10376
10377 static unsigned int
10378 read_4_bytes (bfd *abfd, gdb_byte *buf)
10379 {
10380   return bfd_get_32 (abfd, buf);
10381 }
10382
10383 static int
10384 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10385 {
10386   return bfd_get_signed_32 (abfd, buf);
10387 }
10388
10389 static ULONGEST
10390 read_8_bytes (bfd *abfd, gdb_byte *buf)
10391 {
10392   return bfd_get_64 (abfd, buf);
10393 }
10394
10395 static CORE_ADDR
10396 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10397               unsigned int *bytes_read)
10398 {
10399   struct comp_unit_head *cu_header = &cu->header;
10400   CORE_ADDR retval = 0;
10401
10402   if (cu_header->signed_addr_p)
10403     {
10404       switch (cu_header->addr_size)
10405         {
10406         case 2:
10407           retval = bfd_get_signed_16 (abfd, buf);
10408           break;
10409         case 4:
10410           retval = bfd_get_signed_32 (abfd, buf);
10411           break;
10412         case 8:
10413           retval = bfd_get_signed_64 (abfd, buf);
10414           break;
10415         default:
10416           internal_error (__FILE__, __LINE__,
10417                           _("read_address: bad switch, signed [in module %s]"),
10418                           bfd_get_filename (abfd));
10419         }
10420     }
10421   else
10422     {
10423       switch (cu_header->addr_size)
10424         {
10425         case 2:
10426           retval = bfd_get_16 (abfd, buf);
10427           break;
10428         case 4:
10429           retval = bfd_get_32 (abfd, buf);
10430           break;
10431         case 8:
10432           retval = bfd_get_64 (abfd, buf);
10433           break;
10434         default:
10435           internal_error (__FILE__, __LINE__,
10436                           _("read_address: bad switch, "
10437                             "unsigned [in module %s]"),
10438                           bfd_get_filename (abfd));
10439         }
10440     }
10441
10442   *bytes_read = cu_header->addr_size;
10443   return retval;
10444 }
10445
10446 /* Read the initial length from a section.  The (draft) DWARF 3
10447    specification allows the initial length to take up either 4 bytes
10448    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10449    bytes describe the length and all offsets will be 8 bytes in length
10450    instead of 4.
10451
10452    An older, non-standard 64-bit format is also handled by this
10453    function.  The older format in question stores the initial length
10454    as an 8-byte quantity without an escape value.  Lengths greater
10455    than 2^32 aren't very common which means that the initial 4 bytes
10456    is almost always zero.  Since a length value of zero doesn't make
10457    sense for the 32-bit format, this initial zero can be considered to
10458    be an escape value which indicates the presence of the older 64-bit
10459    format.  As written, the code can't detect (old format) lengths
10460    greater than 4GB.  If it becomes necessary to handle lengths
10461    somewhat larger than 4GB, we could allow other small values (such
10462    as the non-sensical values of 1, 2, and 3) to also be used as
10463    escape values indicating the presence of the old format.
10464
10465    The value returned via bytes_read should be used to increment the
10466    relevant pointer after calling read_initial_length().
10467
10468    [ Note:  read_initial_length() and read_offset() are based on the
10469      document entitled "DWARF Debugging Information Format", revision
10470      3, draft 8, dated November 19, 2001.  This document was obtained
10471      from:
10472
10473         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10474
10475      This document is only a draft and is subject to change.  (So beware.)
10476
10477      Details regarding the older, non-standard 64-bit format were
10478      determined empirically by examining 64-bit ELF files produced by
10479      the SGI toolchain on an IRIX 6.5 machine.
10480
10481      - Kevin, July 16, 2002
10482    ] */
10483
10484 static LONGEST
10485 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10486 {
10487   LONGEST length = bfd_get_32 (abfd, buf);
10488
10489   if (length == 0xffffffff)
10490     {
10491       length = bfd_get_64 (abfd, buf + 4);
10492       *bytes_read = 12;
10493     }
10494   else if (length == 0)
10495     {
10496       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10497       length = bfd_get_64 (abfd, buf);
10498       *bytes_read = 8;
10499     }
10500   else
10501     {
10502       *bytes_read = 4;
10503     }
10504
10505   return length;
10506 }
10507
10508 /* Cover function for read_initial_length.
10509    Returns the length of the object at BUF, and stores the size of the
10510    initial length in *BYTES_READ and stores the size that offsets will be in
10511    *OFFSET_SIZE.
10512    If the initial length size is not equivalent to that specified in
10513    CU_HEADER then issue a complaint.
10514    This is useful when reading non-comp-unit headers.  */
10515
10516 static LONGEST
10517 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10518                                         const struct comp_unit_head *cu_header,
10519                                         unsigned int *bytes_read,
10520                                         unsigned int *offset_size)
10521 {
10522   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10523
10524   gdb_assert (cu_header->initial_length_size == 4
10525               || cu_header->initial_length_size == 8
10526               || cu_header->initial_length_size == 12);
10527
10528   if (cu_header->initial_length_size != *bytes_read)
10529     complaint (&symfile_complaints,
10530                _("intermixed 32-bit and 64-bit DWARF sections"));
10531
10532   *offset_size = (*bytes_read == 4) ? 4 : 8;
10533   return length;
10534 }
10535
10536 /* Read an offset from the data stream.  The size of the offset is
10537    given by cu_header->offset_size.  */
10538
10539 static LONGEST
10540 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10541              unsigned int *bytes_read)
10542 {
10543   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10544
10545   *bytes_read = cu_header->offset_size;
10546   return offset;
10547 }
10548
10549 /* Read an offset from the data stream.  */
10550
10551 static LONGEST
10552 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10553 {
10554   LONGEST retval = 0;
10555
10556   switch (offset_size)
10557     {
10558     case 4:
10559       retval = bfd_get_32 (abfd, buf);
10560       break;
10561     case 8:
10562       retval = bfd_get_64 (abfd, buf);
10563       break;
10564     default:
10565       internal_error (__FILE__, __LINE__,
10566                       _("read_offset_1: bad switch [in module %s]"),
10567                       bfd_get_filename (abfd));
10568     }
10569
10570   return retval;
10571 }
10572
10573 static gdb_byte *
10574 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10575 {
10576   /* If the size of a host char is 8 bits, we can return a pointer
10577      to the buffer, otherwise we have to copy the data to a buffer
10578      allocated on the temporary obstack.  */
10579   gdb_assert (HOST_CHAR_BIT == 8);
10580   return buf;
10581 }
10582
10583 static char *
10584 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10585 {
10586   /* If the size of a host char is 8 bits, we can return a pointer
10587      to the string, otherwise we have to copy the string to a buffer
10588      allocated on the temporary obstack.  */
10589   gdb_assert (HOST_CHAR_BIT == 8);
10590   if (*buf == '\0')
10591     {
10592       *bytes_read_ptr = 1;
10593       return NULL;
10594     }
10595   *bytes_read_ptr = strlen ((char *) buf) + 1;
10596   return (char *) buf;
10597 }
10598
10599 static char *
10600 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10601 {
10602   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10603   if (dwarf2_per_objfile->str.buffer == NULL)
10604     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10605            bfd_get_filename (abfd));
10606   if (str_offset >= dwarf2_per_objfile->str.size)
10607     error (_("DW_FORM_strp pointing outside of "
10608              ".debug_str section [in module %s]"),
10609            bfd_get_filename (abfd));
10610   gdb_assert (HOST_CHAR_BIT == 8);
10611   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10612     return NULL;
10613   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10614 }
10615
10616 static char *
10617 read_indirect_string (bfd *abfd, gdb_byte *buf,
10618                       const struct comp_unit_head *cu_header,
10619                       unsigned int *bytes_read_ptr)
10620 {
10621   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10622
10623   return read_indirect_string_at_offset (abfd, str_offset);
10624 }
10625
10626 static unsigned long
10627 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10628 {
10629   unsigned long result;
10630   unsigned int num_read;
10631   int i, shift;
10632   unsigned char byte;
10633
10634   result = 0;
10635   shift = 0;
10636   num_read = 0;
10637   i = 0;
10638   while (1)
10639     {
10640       byte = bfd_get_8 (abfd, buf);
10641       buf++;
10642       num_read++;
10643       result |= ((unsigned long)(byte & 127) << shift);
10644       if ((byte & 128) == 0)
10645         {
10646           break;
10647         }
10648       shift += 7;
10649     }
10650   *bytes_read_ptr = num_read;
10651   return result;
10652 }
10653
10654 static long
10655 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10656 {
10657   long result;
10658   int i, shift, num_read;
10659   unsigned char byte;
10660
10661   result = 0;
10662   shift = 0;
10663   num_read = 0;
10664   i = 0;
10665   while (1)
10666     {
10667       byte = bfd_get_8 (abfd, buf);
10668       buf++;
10669       num_read++;
10670       result |= ((long)(byte & 127) << shift);
10671       shift += 7;
10672       if ((byte & 128) == 0)
10673         {
10674           break;
10675         }
10676     }
10677   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10678     result |= -(((long)1) << shift);
10679   *bytes_read_ptr = num_read;
10680   return result;
10681 }
10682
10683 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10684
10685 static gdb_byte *
10686 skip_leb128 (bfd *abfd, gdb_byte *buf)
10687 {
10688   int byte;
10689
10690   while (1)
10691     {
10692       byte = bfd_get_8 (abfd, buf);
10693       buf++;
10694       if ((byte & 128) == 0)
10695         return buf;
10696     }
10697 }
10698
10699 static void
10700 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10701 {
10702   switch (lang)
10703     {
10704     case DW_LANG_C89:
10705     case DW_LANG_C99:
10706     case DW_LANG_C:
10707       cu->language = language_c;
10708       break;
10709     case DW_LANG_C_plus_plus:
10710       cu->language = language_cplus;
10711       break;
10712     case DW_LANG_D:
10713       cu->language = language_d;
10714       break;
10715     case DW_LANG_Fortran77:
10716     case DW_LANG_Fortran90:
10717     case DW_LANG_Fortran95:
10718       cu->language = language_fortran;
10719       break;
10720     case DW_LANG_Mips_Assembler:
10721       cu->language = language_asm;
10722       break;
10723     case DW_LANG_Java:
10724       cu->language = language_java;
10725       break;
10726     case DW_LANG_Ada83:
10727     case DW_LANG_Ada95:
10728       cu->language = language_ada;
10729       break;
10730     case DW_LANG_Modula2:
10731       cu->language = language_m2;
10732       break;
10733     case DW_LANG_Pascal83:
10734       cu->language = language_pascal;
10735       break;
10736     case DW_LANG_ObjC:
10737       cu->language = language_objc;
10738       break;
10739     case DW_LANG_Cobol74:
10740     case DW_LANG_Cobol85:
10741     default:
10742       cu->language = language_minimal;
10743       break;
10744     }
10745   cu->language_defn = language_def (cu->language);
10746 }
10747
10748 /* Return the named attribute or NULL if not there.  */
10749
10750 static struct attribute *
10751 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10752 {
10753   unsigned int i;
10754   struct attribute *spec = NULL;
10755
10756   for (i = 0; i < die->num_attrs; ++i)
10757     {
10758       if (die->attrs[i].name == name)
10759         return &die->attrs[i];
10760       if (die->attrs[i].name == DW_AT_specification
10761           || die->attrs[i].name == DW_AT_abstract_origin)
10762         spec = &die->attrs[i];
10763     }
10764
10765   if (spec)
10766     {
10767       die = follow_die_ref (die, spec, &cu);
10768       return dwarf2_attr (die, name, cu);
10769     }
10770
10771   return NULL;
10772 }
10773
10774 /* Return the named attribute or NULL if not there,
10775    but do not follow DW_AT_specification, etc.
10776    This is for use in contexts where we're reading .debug_types dies.
10777    Following DW_AT_specification, DW_AT_abstract_origin will take us
10778    back up the chain, and we want to go down.  */
10779
10780 static struct attribute *
10781 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10782                        struct dwarf2_cu *cu)
10783 {
10784   unsigned int i;
10785
10786   for (i = 0; i < die->num_attrs; ++i)
10787     if (die->attrs[i].name == name)
10788       return &die->attrs[i];
10789
10790   return NULL;
10791 }
10792
10793 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10794    and holds a non-zero value.  This function should only be used for
10795    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10796
10797 static int
10798 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10799 {
10800   struct attribute *attr = dwarf2_attr (die, name, cu);
10801
10802   return (attr && DW_UNSND (attr));
10803 }
10804
10805 static int
10806 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10807 {
10808   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10809      which value is non-zero.  However, we have to be careful with
10810      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10811      (via dwarf2_flag_true_p) follows this attribute.  So we may
10812      end up accidently finding a declaration attribute that belongs
10813      to a different DIE referenced by the specification attribute,
10814      even though the given DIE does not have a declaration attribute.  */
10815   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10816           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10817 }
10818
10819 /* Return the die giving the specification for DIE, if there is
10820    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10821    containing the return value on output.  If there is no
10822    specification, but there is an abstract origin, that is
10823    returned.  */
10824
10825 static struct die_info *
10826 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10827 {
10828   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10829                                              *spec_cu);
10830
10831   if (spec_attr == NULL)
10832     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10833
10834   if (spec_attr == NULL)
10835     return NULL;
10836   else
10837     return follow_die_ref (die, spec_attr, spec_cu);
10838 }
10839
10840 /* Free the line_header structure *LH, and any arrays and strings it
10841    refers to.
10842    NOTE: This is also used as a "cleanup" function.  */
10843
10844 static void
10845 free_line_header (struct line_header *lh)
10846 {
10847   if (lh->standard_opcode_lengths)
10848     xfree (lh->standard_opcode_lengths);
10849
10850   /* Remember that all the lh->file_names[i].name pointers are
10851      pointers into debug_line_buffer, and don't need to be freed.  */
10852   if (lh->file_names)
10853     xfree (lh->file_names);
10854
10855   /* Similarly for the include directory names.  */
10856   if (lh->include_dirs)
10857     xfree (lh->include_dirs);
10858
10859   xfree (lh);
10860 }
10861
10862 /* Add an entry to LH's include directory table.  */
10863
10864 static void
10865 add_include_dir (struct line_header *lh, char *include_dir)
10866 {
10867   /* Grow the array if necessary.  */
10868   if (lh->include_dirs_size == 0)
10869     {
10870       lh->include_dirs_size = 1; /* for testing */
10871       lh->include_dirs = xmalloc (lh->include_dirs_size
10872                                   * sizeof (*lh->include_dirs));
10873     }
10874   else if (lh->num_include_dirs >= lh->include_dirs_size)
10875     {
10876       lh->include_dirs_size *= 2;
10877       lh->include_dirs = xrealloc (lh->include_dirs,
10878                                    (lh->include_dirs_size
10879                                     * sizeof (*lh->include_dirs)));
10880     }
10881
10882   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10883 }
10884
10885 /* Add an entry to LH's file name table.  */
10886
10887 static void
10888 add_file_name (struct line_header *lh,
10889                char *name,
10890                unsigned int dir_index,
10891                unsigned int mod_time,
10892                unsigned int length)
10893 {
10894   struct file_entry *fe;
10895
10896   /* Grow the array if necessary.  */
10897   if (lh->file_names_size == 0)
10898     {
10899       lh->file_names_size = 1; /* for testing */
10900       lh->file_names = xmalloc (lh->file_names_size
10901                                 * sizeof (*lh->file_names));
10902     }
10903   else if (lh->num_file_names >= lh->file_names_size)
10904     {
10905       lh->file_names_size *= 2;
10906       lh->file_names = xrealloc (lh->file_names,
10907                                  (lh->file_names_size
10908                                   * sizeof (*lh->file_names)));
10909     }
10910
10911   fe = &lh->file_names[lh->num_file_names++];
10912   fe->name = name;
10913   fe->dir_index = dir_index;
10914   fe->mod_time = mod_time;
10915   fe->length = length;
10916   fe->included_p = 0;
10917   fe->symtab = NULL;
10918 }
10919
10920 /* Read the statement program header starting at OFFSET in
10921    .debug_line, according to the endianness of ABFD.  Return a pointer
10922    to a struct line_header, allocated using xmalloc.
10923
10924    NOTE: the strings in the include directory and file name tables of
10925    the returned object point into debug_line_buffer, and must not be
10926    freed.  */
10927
10928 static struct line_header *
10929 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10930                           struct dwarf2_cu *cu)
10931 {
10932   struct cleanup *back_to;
10933   struct line_header *lh;
10934   gdb_byte *line_ptr;
10935   unsigned int bytes_read, offset_size;
10936   int i;
10937   char *cur_dir, *cur_file;
10938
10939   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10940   if (dwarf2_per_objfile->line.buffer == NULL)
10941     {
10942       complaint (&symfile_complaints, _("missing .debug_line section"));
10943       return 0;
10944     }
10945
10946   /* Make sure that at least there's room for the total_length field.
10947      That could be 12 bytes long, but we're just going to fudge that.  */
10948   if (offset + 4 >= dwarf2_per_objfile->line.size)
10949     {
10950       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10951       return 0;
10952     }
10953
10954   lh = xmalloc (sizeof (*lh));
10955   memset (lh, 0, sizeof (*lh));
10956   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10957                           (void *) lh);
10958
10959   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10960
10961   /* Read in the header.  */
10962   lh->total_length =
10963     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10964                                             &bytes_read, &offset_size);
10965   line_ptr += bytes_read;
10966   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10967                                      + dwarf2_per_objfile->line.size))
10968     {
10969       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10970       return 0;
10971     }
10972   lh->statement_program_end = line_ptr + lh->total_length;
10973   lh->version = read_2_bytes (abfd, line_ptr);
10974   line_ptr += 2;
10975   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10976   line_ptr += offset_size;
10977   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10978   line_ptr += 1;
10979   if (lh->version >= 4)
10980     {
10981       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10982       line_ptr += 1;
10983     }
10984   else
10985     lh->maximum_ops_per_instruction = 1;
10986
10987   if (lh->maximum_ops_per_instruction == 0)
10988     {
10989       lh->maximum_ops_per_instruction = 1;
10990       complaint (&symfile_complaints,
10991                  _("invalid maximum_ops_per_instruction "
10992                    "in `.debug_line' section"));
10993     }
10994
10995   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10996   line_ptr += 1;
10997   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10998   line_ptr += 1;
10999   lh->line_range = read_1_byte (abfd, line_ptr);
11000   line_ptr += 1;
11001   lh->opcode_base = read_1_byte (abfd, line_ptr);
11002   line_ptr += 1;
11003   lh->standard_opcode_lengths
11004     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11005
11006   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
11007   for (i = 1; i < lh->opcode_base; ++i)
11008     {
11009       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11010       line_ptr += 1;
11011     }
11012
11013   /* Read directory table.  */
11014   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11015     {
11016       line_ptr += bytes_read;
11017       add_include_dir (lh, cur_dir);
11018     }
11019   line_ptr += bytes_read;
11020
11021   /* Read file name table.  */
11022   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11023     {
11024       unsigned int dir_index, mod_time, length;
11025
11026       line_ptr += bytes_read;
11027       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11028       line_ptr += bytes_read;
11029       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11030       line_ptr += bytes_read;
11031       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11032       line_ptr += bytes_read;
11033
11034       add_file_name (lh, cur_file, dir_index, mod_time, length);
11035     }
11036   line_ptr += bytes_read;
11037   lh->statement_program_start = line_ptr;
11038
11039   if (line_ptr > (dwarf2_per_objfile->line.buffer
11040                   + dwarf2_per_objfile->line.size))
11041     complaint (&symfile_complaints,
11042                _("line number info header doesn't "
11043                  "fit in `.debug_line' section"));
11044
11045   discard_cleanups (back_to);
11046   return lh;
11047 }
11048
11049 /* Subroutine of dwarf_decode_lines to simplify it.
11050    Return the file name of the psymtab for included file FILE_INDEX
11051    in line header LH of PST.
11052    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11053    If space for the result is malloc'd, it will be freed by a cleanup.
11054    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
11055
11056 static char *
11057 psymtab_include_file_name (const struct line_header *lh, int file_index,
11058                            const struct partial_symtab *pst,
11059                            const char *comp_dir)
11060 {
11061   const struct file_entry fe = lh->file_names [file_index];
11062   char *include_name = fe.name;
11063   char *include_name_to_compare = include_name;
11064   char *dir_name = NULL;
11065   const char *pst_filename;
11066   char *copied_name = NULL;
11067   int file_is_pst;
11068
11069   if (fe.dir_index)
11070     dir_name = lh->include_dirs[fe.dir_index - 1];
11071
11072   if (!IS_ABSOLUTE_PATH (include_name)
11073       && (dir_name != NULL || comp_dir != NULL))
11074     {
11075       /* Avoid creating a duplicate psymtab for PST.
11076          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11077          Before we do the comparison, however, we need to account
11078          for DIR_NAME and COMP_DIR.
11079          First prepend dir_name (if non-NULL).  If we still don't
11080          have an absolute path prepend comp_dir (if non-NULL).
11081          However, the directory we record in the include-file's
11082          psymtab does not contain COMP_DIR (to match the
11083          corresponding symtab(s)).
11084
11085          Example:
11086
11087          bash$ cd /tmp
11088          bash$ gcc -g ./hello.c
11089          include_name = "hello.c"
11090          dir_name = "."
11091          DW_AT_comp_dir = comp_dir = "/tmp"
11092          DW_AT_name = "./hello.c"  */
11093
11094       if (dir_name != NULL)
11095         {
11096           include_name = concat (dir_name, SLASH_STRING,
11097                                  include_name, (char *)NULL);
11098           include_name_to_compare = include_name;
11099           make_cleanup (xfree, include_name);
11100         }
11101       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11102         {
11103           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11104                                             include_name, (char *)NULL);
11105         }
11106     }
11107
11108   pst_filename = pst->filename;
11109   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11110     {
11111       copied_name = concat (pst->dirname, SLASH_STRING,
11112                             pst_filename, (char *)NULL);
11113       pst_filename = copied_name;
11114     }
11115
11116   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11117
11118   if (include_name_to_compare != include_name)
11119     xfree (include_name_to_compare);
11120   if (copied_name != NULL)
11121     xfree (copied_name);
11122
11123   if (file_is_pst)
11124     return NULL;
11125   return include_name;
11126 }
11127
11128 /* Ignore this record_line request.  */
11129
11130 static void
11131 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11132 {
11133   return;
11134 }
11135
11136 /* Decode the Line Number Program (LNP) for the given line_header
11137    structure and CU.  The actual information extracted and the type
11138    of structures created from the LNP depends on the value of PST.
11139
11140    1. If PST is NULL, then this procedure uses the data from the program
11141       to create all necessary symbol tables, and their linetables.
11142
11143    2. If PST is not NULL, this procedure reads the program to determine
11144       the list of files included by the unit represented by PST, and
11145       builds all the associated partial symbol tables.
11146
11147    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11148    It is used for relative paths in the line table.
11149    NOTE: When processing partial symtabs (pst != NULL),
11150    comp_dir == pst->dirname.
11151
11152    NOTE: It is important that psymtabs have the same file name (via strcmp)
11153    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11154    symtab we don't use it in the name of the psymtabs we create.
11155    E.g. expand_line_sal requires this when finding psymtabs to expand.
11156    A good testcase for this is mb-inline.exp.  */
11157
11158 static void
11159 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11160                     struct dwarf2_cu *cu, struct partial_symtab *pst)
11161 {
11162   gdb_byte *line_ptr, *extended_end;
11163   gdb_byte *line_end;
11164   unsigned int bytes_read, extended_len;
11165   unsigned char op_code, extended_op, adj_opcode;
11166   CORE_ADDR baseaddr;
11167   struct objfile *objfile = cu->objfile;
11168   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11169   const int decode_for_pst_p = (pst != NULL);
11170   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11171   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11172     = record_line;
11173
11174   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11175
11176   line_ptr = lh->statement_program_start;
11177   line_end = lh->statement_program_end;
11178
11179   /* Read the statement sequences until there's nothing left.  */
11180   while (line_ptr < line_end)
11181     {
11182       /* state machine registers  */
11183       CORE_ADDR address = 0;
11184       unsigned int file = 1;
11185       unsigned int line = 1;
11186       unsigned int column = 0;
11187       int is_stmt = lh->default_is_stmt;
11188       int basic_block = 0;
11189       int end_sequence = 0;
11190       CORE_ADDR addr;
11191       unsigned char op_index = 0;
11192
11193       if (!decode_for_pst_p && lh->num_file_names >= file)
11194         {
11195           /* Start a subfile for the current file of the state machine.  */
11196           /* lh->include_dirs and lh->file_names are 0-based, but the
11197              directory and file name numbers in the statement program
11198              are 1-based.  */
11199           struct file_entry *fe = &lh->file_names[file - 1];
11200           char *dir = NULL;
11201
11202           if (fe->dir_index)
11203             dir = lh->include_dirs[fe->dir_index - 1];
11204
11205           dwarf2_start_subfile (fe->name, dir, comp_dir);
11206         }
11207
11208       /* Decode the table.  */
11209       while (!end_sequence)
11210         {
11211           op_code = read_1_byte (abfd, line_ptr);
11212           line_ptr += 1;
11213           if (line_ptr > line_end)
11214             {
11215               dwarf2_debug_line_missing_end_sequence_complaint ();
11216               break;
11217             }
11218
11219           if (op_code >= lh->opcode_base)
11220             {
11221               /* Special operand.  */
11222               adj_opcode = op_code - lh->opcode_base;
11223               address += (((op_index + (adj_opcode / lh->line_range))
11224                            / lh->maximum_ops_per_instruction)
11225                           * lh->minimum_instruction_length);
11226               op_index = ((op_index + (adj_opcode / lh->line_range))
11227                           % lh->maximum_ops_per_instruction);
11228               line += lh->line_base + (adj_opcode % lh->line_range);
11229               if (lh->num_file_names < file || file == 0)
11230                 dwarf2_debug_line_missing_file_complaint ();
11231               /* For now we ignore lines not starting on an
11232                  instruction boundary.  */
11233               else if (op_index == 0)
11234                 {
11235                   lh->file_names[file - 1].included_p = 1;
11236                   if (!decode_for_pst_p && is_stmt)
11237                     {
11238                       if (last_subfile != current_subfile)
11239                         {
11240                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11241                           if (last_subfile)
11242                             (*p_record_line) (last_subfile, 0, addr);
11243                           last_subfile = current_subfile;
11244                         }
11245                       /* Append row to matrix using current values.  */
11246                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11247                       (*p_record_line) (current_subfile, line, addr);
11248                     }
11249                 }
11250               basic_block = 0;
11251             }
11252           else switch (op_code)
11253             {
11254             case DW_LNS_extended_op:
11255               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11256                                                    &bytes_read);
11257               line_ptr += bytes_read;
11258               extended_end = line_ptr + extended_len;
11259               extended_op = read_1_byte (abfd, line_ptr);
11260               line_ptr += 1;
11261               switch (extended_op)
11262                 {
11263                 case DW_LNE_end_sequence:
11264                   p_record_line = record_line;
11265                   end_sequence = 1;
11266                   break;
11267                 case DW_LNE_set_address:
11268                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11269
11270                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11271                     {
11272                       /* This line table is for a function which has been
11273                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11274
11275                       long line_offset
11276                         = line_ptr - dwarf2_per_objfile->line.buffer;
11277
11278                       complaint (&symfile_complaints,
11279                                  _(".debug_line address at offset 0x%lx is 0 "
11280                                    "[in module %s]"),
11281                                  line_offset, objfile->name);
11282                       p_record_line = noop_record_line;
11283                     }
11284
11285                   op_index = 0;
11286                   line_ptr += bytes_read;
11287                   address += baseaddr;
11288                   break;
11289                 case DW_LNE_define_file:
11290                   {
11291                     char *cur_file;
11292                     unsigned int dir_index, mod_time, length;
11293
11294                     cur_file = read_direct_string (abfd, line_ptr,
11295                                                    &bytes_read);
11296                     line_ptr += bytes_read;
11297                     dir_index =
11298                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11299                     line_ptr += bytes_read;
11300                     mod_time =
11301                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11302                     line_ptr += bytes_read;
11303                     length =
11304                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11305                     line_ptr += bytes_read;
11306                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11307                   }
11308                   break;
11309                 case DW_LNE_set_discriminator:
11310                   /* The discriminator is not interesting to the debugger;
11311                      just ignore it.  */
11312                   line_ptr = extended_end;
11313                   break;
11314                 default:
11315                   complaint (&symfile_complaints,
11316                              _("mangled .debug_line section"));
11317                   return;
11318                 }
11319               /* Make sure that we parsed the extended op correctly.  If e.g.
11320                  we expected a different address size than the producer used,
11321                  we may have read the wrong number of bytes.  */
11322               if (line_ptr != extended_end)
11323                 {
11324                   complaint (&symfile_complaints,
11325                              _("mangled .debug_line section"));
11326                   return;
11327                 }
11328               break;
11329             case DW_LNS_copy:
11330               if (lh->num_file_names < file || file == 0)
11331                 dwarf2_debug_line_missing_file_complaint ();
11332               else
11333                 {
11334                   lh->file_names[file - 1].included_p = 1;
11335                   if (!decode_for_pst_p && is_stmt)
11336                     {
11337                       if (last_subfile != current_subfile)
11338                         {
11339                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11340                           if (last_subfile)
11341                             (*p_record_line) (last_subfile, 0, addr);
11342                           last_subfile = current_subfile;
11343                         }
11344                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11345                       (*p_record_line) (current_subfile, line, addr);
11346                     }
11347                 }
11348               basic_block = 0;
11349               break;
11350             case DW_LNS_advance_pc:
11351               {
11352                 CORE_ADDR adjust
11353                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11354
11355                 address += (((op_index + adjust)
11356                              / lh->maximum_ops_per_instruction)
11357                             * lh->minimum_instruction_length);
11358                 op_index = ((op_index + adjust)
11359                             % lh->maximum_ops_per_instruction);
11360                 line_ptr += bytes_read;
11361               }
11362               break;
11363             case DW_LNS_advance_line:
11364               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11365               line_ptr += bytes_read;
11366               break;
11367             case DW_LNS_set_file:
11368               {
11369                 /* The arrays lh->include_dirs and lh->file_names are
11370                    0-based, but the directory and file name numbers in
11371                    the statement program are 1-based.  */
11372                 struct file_entry *fe;
11373                 char *dir = NULL;
11374
11375                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11376                 line_ptr += bytes_read;
11377                 if (lh->num_file_names < file || file == 0)
11378                   dwarf2_debug_line_missing_file_complaint ();
11379                 else
11380                   {
11381                     fe = &lh->file_names[file - 1];
11382                     if (fe->dir_index)
11383                       dir = lh->include_dirs[fe->dir_index - 1];
11384                     if (!decode_for_pst_p)
11385                       {
11386                         last_subfile = current_subfile;
11387                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11388                       }
11389                   }
11390               }
11391               break;
11392             case DW_LNS_set_column:
11393               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11394               line_ptr += bytes_read;
11395               break;
11396             case DW_LNS_negate_stmt:
11397               is_stmt = (!is_stmt);
11398               break;
11399             case DW_LNS_set_basic_block:
11400               basic_block = 1;
11401               break;
11402             /* Add to the address register of the state machine the
11403                address increment value corresponding to special opcode
11404                255.  I.e., this value is scaled by the minimum
11405                instruction length since special opcode 255 would have
11406                scaled the increment.  */
11407             case DW_LNS_const_add_pc:
11408               {
11409                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11410
11411                 address += (((op_index + adjust)
11412                              / lh->maximum_ops_per_instruction)
11413                             * lh->minimum_instruction_length);
11414                 op_index = ((op_index + adjust)
11415                             % lh->maximum_ops_per_instruction);
11416               }
11417               break;
11418             case DW_LNS_fixed_advance_pc:
11419               address += read_2_bytes (abfd, line_ptr);
11420               op_index = 0;
11421               line_ptr += 2;
11422               break;
11423             default:
11424               {
11425                 /* Unknown standard opcode, ignore it.  */
11426                 int i;
11427
11428                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11429                   {
11430                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11431                     line_ptr += bytes_read;
11432                   }
11433               }
11434             }
11435         }
11436       if (lh->num_file_names < file || file == 0)
11437         dwarf2_debug_line_missing_file_complaint ();
11438       else
11439         {
11440           lh->file_names[file - 1].included_p = 1;
11441           if (!decode_for_pst_p)
11442             {
11443               addr = gdbarch_addr_bits_remove (gdbarch, address);
11444               (*p_record_line) (current_subfile, 0, addr);
11445             }
11446         }
11447     }
11448
11449   if (decode_for_pst_p)
11450     {
11451       int file_index;
11452
11453       /* Now that we're done scanning the Line Header Program, we can
11454          create the psymtab of each included file.  */
11455       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11456         if (lh->file_names[file_index].included_p == 1)
11457           {
11458             char *include_name =
11459               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11460             if (include_name != NULL)
11461               dwarf2_create_include_psymtab (include_name, pst, objfile);
11462           }
11463     }
11464   else
11465     {
11466       /* Make sure a symtab is created for every file, even files
11467          which contain only variables (i.e. no code with associated
11468          line numbers).  */
11469
11470       int i;
11471       struct file_entry *fe;
11472
11473       for (i = 0; i < lh->num_file_names; i++)
11474         {
11475           char *dir = NULL;
11476
11477           fe = &lh->file_names[i];
11478           if (fe->dir_index)
11479             dir = lh->include_dirs[fe->dir_index - 1];
11480           dwarf2_start_subfile (fe->name, dir, comp_dir);
11481
11482           /* Skip the main file; we don't need it, and it must be
11483              allocated last, so that it will show up before the
11484              non-primary symtabs in the objfile's symtab list.  */
11485           if (current_subfile == first_subfile)
11486             continue;
11487
11488           if (current_subfile->symtab == NULL)
11489             current_subfile->symtab = allocate_symtab (current_subfile->name,
11490                                                        objfile);
11491           fe->symtab = current_subfile->symtab;
11492         }
11493     }
11494 }
11495
11496 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11497    DIRNAME the name of the source directory which contains FILENAME
11498    or NULL if not known.  COMP_DIR is the compilation directory for the
11499    linetable's compilation unit or NULL if not known.
11500    This routine tries to keep line numbers from identical absolute and
11501    relative file names in a common subfile.
11502
11503    Using the `list' example from the GDB testsuite, which resides in
11504    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11505    of /srcdir/list0.c yields the following debugging information for list0.c:
11506
11507    DW_AT_name:          /srcdir/list0.c
11508    DW_AT_comp_dir:              /compdir
11509    files.files[0].name: list0.h
11510    files.files[0].dir:  /srcdir
11511    files.files[1].name: list0.c
11512    files.files[1].dir:  /srcdir
11513
11514    The line number information for list0.c has to end up in a single
11515    subfile, so that `break /srcdir/list0.c:1' works as expected.
11516    start_subfile will ensure that this happens provided that we pass the
11517    concatenation of files.files[1].dir and files.files[1].name as the
11518    subfile's name.  */
11519
11520 static void
11521 dwarf2_start_subfile (char *filename, const char *dirname,
11522                       const char *comp_dir)
11523 {
11524   char *fullname;
11525
11526   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11527      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11528      second argument to start_subfile.  To be consistent, we do the
11529      same here.  In order not to lose the line information directory,
11530      we concatenate it to the filename when it makes sense.
11531      Note that the Dwarf3 standard says (speaking of filenames in line
11532      information): ``The directory index is ignored for file names
11533      that represent full path names''.  Thus ignoring dirname in the
11534      `else' branch below isn't an issue.  */
11535
11536   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11537     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11538   else
11539     fullname = filename;
11540
11541   start_subfile (fullname, comp_dir);
11542
11543   if (fullname != filename)
11544     xfree (fullname);
11545 }
11546
11547 static void
11548 var_decode_location (struct attribute *attr, struct symbol *sym,
11549                      struct dwarf2_cu *cu)
11550 {
11551   struct objfile *objfile = cu->objfile;
11552   struct comp_unit_head *cu_header = &cu->header;
11553
11554   /* NOTE drow/2003-01-30: There used to be a comment and some special
11555      code here to turn a symbol with DW_AT_external and a
11556      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11557      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11558      with some versions of binutils) where shared libraries could have
11559      relocations against symbols in their debug information - the
11560      minimal symbol would have the right address, but the debug info
11561      would not.  It's no longer necessary, because we will explicitly
11562      apply relocations when we read in the debug information now.  */
11563
11564   /* A DW_AT_location attribute with no contents indicates that a
11565      variable has been optimized away.  */
11566   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11567     {
11568       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11569       return;
11570     }
11571
11572   /* Handle one degenerate form of location expression specially, to
11573      preserve GDB's previous behavior when section offsets are
11574      specified.  If this is just a DW_OP_addr then mark this symbol
11575      as LOC_STATIC.  */
11576
11577   if (attr_form_is_block (attr)
11578       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11579       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11580     {
11581       unsigned int dummy;
11582
11583       SYMBOL_VALUE_ADDRESS (sym) =
11584         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11585       SYMBOL_CLASS (sym) = LOC_STATIC;
11586       fixup_symbol_section (sym, objfile);
11587       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11588                                               SYMBOL_SECTION (sym));
11589       return;
11590     }
11591
11592   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11593      expression evaluator, and use LOC_COMPUTED only when necessary
11594      (i.e. when the value of a register or memory location is
11595      referenced, or a thread-local block, etc.).  Then again, it might
11596      not be worthwhile.  I'm assuming that it isn't unless performance
11597      or memory numbers show me otherwise.  */
11598
11599   dwarf2_symbol_mark_computed (attr, sym, cu);
11600   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11601
11602   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11603     cu->has_loclist = 1;
11604 }
11605
11606 /* Given a pointer to a DWARF information entry, figure out if we need
11607    to make a symbol table entry for it, and if so, create a new entry
11608    and return a pointer to it.
11609    If TYPE is NULL, determine symbol type from the die, otherwise
11610    used the passed type.
11611    If SPACE is not NULL, use it to hold the new symbol.  If it is
11612    NULL, allocate a new symbol on the objfile's obstack.  */
11613
11614 static struct symbol *
11615 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11616                  struct symbol *space)
11617 {
11618   struct objfile *objfile = cu->objfile;
11619   struct symbol *sym = NULL;
11620   char *name;
11621   struct attribute *attr = NULL;
11622   struct attribute *attr2 = NULL;
11623   CORE_ADDR baseaddr;
11624   struct pending **list_to_add = NULL;
11625
11626   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11627
11628   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11629
11630   name = dwarf2_name (die, cu);
11631   if (name)
11632     {
11633       const char *linkagename;
11634       int suppress_add = 0;
11635
11636       if (space)
11637         sym = space;
11638       else
11639         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11640       OBJSTAT (objfile, n_syms++);
11641
11642       /* Cache this symbol's name and the name's demangled form (if any).  */
11643       SYMBOL_SET_LANGUAGE (sym, cu->language);
11644       linkagename = dwarf2_physname (name, die, cu);
11645       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11646
11647       /* Fortran does not have mangling standard and the mangling does differ
11648          between gfortran, iFort etc.  */
11649       if (cu->language == language_fortran
11650           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11651         symbol_set_demangled_name (&(sym->ginfo),
11652                                    (char *) dwarf2_full_name (name, die, cu),
11653                                    NULL);
11654
11655       /* Default assumptions.
11656          Use the passed type or decode it from the die.  */
11657       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11658       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11659       if (type != NULL)
11660         SYMBOL_TYPE (sym) = type;
11661       else
11662         SYMBOL_TYPE (sym) = die_type (die, cu);
11663       attr = dwarf2_attr (die,
11664                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11665                           cu);
11666       if (attr)
11667         {
11668           SYMBOL_LINE (sym) = DW_UNSND (attr);
11669         }
11670
11671       attr = dwarf2_attr (die,
11672                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11673                           cu);
11674       if (attr)
11675         {
11676           int file_index = DW_UNSND (attr);
11677
11678           if (cu->line_header == NULL
11679               || file_index > cu->line_header->num_file_names)
11680             complaint (&symfile_complaints,
11681                        _("file index out of range"));
11682           else if (file_index > 0)
11683             {
11684               struct file_entry *fe;
11685
11686               fe = &cu->line_header->file_names[file_index - 1];
11687               SYMBOL_SYMTAB (sym) = fe->symtab;
11688             }
11689         }
11690
11691       switch (die->tag)
11692         {
11693         case DW_TAG_label:
11694           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11695           if (attr)
11696             {
11697               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11698             }
11699           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11700           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11701           SYMBOL_CLASS (sym) = LOC_LABEL;
11702           add_symbol_to_list (sym, cu->list_in_scope);
11703           break;
11704         case DW_TAG_subprogram:
11705           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11706              finish_block.  */
11707           SYMBOL_CLASS (sym) = LOC_BLOCK;
11708           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11709           if ((attr2 && (DW_UNSND (attr2) != 0))
11710               || cu->language == language_ada)
11711             {
11712               /* Subprograms marked external are stored as a global symbol.
11713                  Ada subprograms, whether marked external or not, are always
11714                  stored as a global symbol, because we want to be able to
11715                  access them globally.  For instance, we want to be able
11716                  to break on a nested subprogram without having to
11717                  specify the context.  */
11718               list_to_add = &global_symbols;
11719             }
11720           else
11721             {
11722               list_to_add = cu->list_in_scope;
11723             }
11724           break;
11725         case DW_TAG_inlined_subroutine:
11726           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11727              finish_block.  */
11728           SYMBOL_CLASS (sym) = LOC_BLOCK;
11729           SYMBOL_INLINED (sym) = 1;
11730           /* Do not add the symbol to any lists.  It will be found via
11731              BLOCK_FUNCTION from the blockvector.  */
11732           break;
11733         case DW_TAG_template_value_param:
11734           suppress_add = 1;
11735           /* Fall through.  */
11736         case DW_TAG_constant:
11737         case DW_TAG_variable:
11738         case DW_TAG_member:
11739           /* Compilation with minimal debug info may result in
11740              variables with missing type entries.  Change the
11741              misleading `void' type to something sensible.  */
11742           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11743             SYMBOL_TYPE (sym)
11744               = objfile_type (objfile)->nodebug_data_symbol;
11745
11746           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11747           /* In the case of DW_TAG_member, we should only be called for
11748              static const members.  */
11749           if (die->tag == DW_TAG_member)
11750             {
11751               /* dwarf2_add_field uses die_is_declaration,
11752                  so we do the same.  */
11753               gdb_assert (die_is_declaration (die, cu));
11754               gdb_assert (attr);
11755             }
11756           if (attr)
11757             {
11758               dwarf2_const_value (attr, sym, cu);
11759               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11760               if (!suppress_add)
11761                 {
11762                   if (attr2 && (DW_UNSND (attr2) != 0))
11763                     list_to_add = &global_symbols;
11764                   else
11765                     list_to_add = cu->list_in_scope;
11766                 }
11767               break;
11768             }
11769           attr = dwarf2_attr (die, DW_AT_location, cu);
11770           if (attr)
11771             {
11772               var_decode_location (attr, sym, cu);
11773               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11774               if (SYMBOL_CLASS (sym) == LOC_STATIC
11775                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11776                   && !dwarf2_per_objfile->has_section_at_zero)
11777                 {
11778                   /* When a static variable is eliminated by the linker,
11779                      the corresponding debug information is not stripped
11780                      out, but the variable address is set to null;
11781                      do not add such variables into symbol table.  */
11782                 }
11783               else if (attr2 && (DW_UNSND (attr2) != 0))
11784                 {
11785                   /* Workaround gfortran PR debug/40040 - it uses
11786                      DW_AT_location for variables in -fPIC libraries which may
11787                      get overriden by other libraries/executable and get
11788                      a different address.  Resolve it by the minimal symbol
11789                      which may come from inferior's executable using copy
11790                      relocation.  Make this workaround only for gfortran as for
11791                      other compilers GDB cannot guess the minimal symbol
11792                      Fortran mangling kind.  */
11793                   if (cu->language == language_fortran && die->parent
11794                       && die->parent->tag == DW_TAG_module
11795                       && cu->producer
11796                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11797                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11798
11799                   /* A variable with DW_AT_external is never static,
11800                      but it may be block-scoped.  */
11801                   list_to_add = (cu->list_in_scope == &file_symbols
11802                                  ? &global_symbols : cu->list_in_scope);
11803                 }
11804               else
11805                 list_to_add = cu->list_in_scope;
11806             }
11807           else
11808             {
11809               /* We do not know the address of this symbol.
11810                  If it is an external symbol and we have type information
11811                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11812                  The address of the variable will then be determined from
11813                  the minimal symbol table whenever the variable is
11814                  referenced.  */
11815               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11816               if (attr2 && (DW_UNSND (attr2) != 0)
11817                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11818                 {
11819                   /* A variable with DW_AT_external is never static, but it
11820                      may be block-scoped.  */
11821                   list_to_add = (cu->list_in_scope == &file_symbols
11822                                  ? &global_symbols : cu->list_in_scope);
11823
11824                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11825                 }
11826               else if (!die_is_declaration (die, cu))
11827                 {
11828                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11829                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11830                   if (!suppress_add)
11831                     list_to_add = cu->list_in_scope;
11832                 }
11833             }
11834           break;
11835         case DW_TAG_formal_parameter:
11836           /* If we are inside a function, mark this as an argument.  If
11837              not, we might be looking at an argument to an inlined function
11838              when we do not have enough information to show inlined frames;
11839              pretend it's a local variable in that case so that the user can
11840              still see it.  */
11841           if (context_stack_depth > 0
11842               && context_stack[context_stack_depth - 1].name != NULL)
11843             SYMBOL_IS_ARGUMENT (sym) = 1;
11844           attr = dwarf2_attr (die, DW_AT_location, cu);
11845           if (attr)
11846             {
11847               var_decode_location (attr, sym, cu);
11848             }
11849           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11850           if (attr)
11851             {
11852               dwarf2_const_value (attr, sym, cu);
11853             }
11854
11855           list_to_add = cu->list_in_scope;
11856           break;
11857         case DW_TAG_unspecified_parameters:
11858           /* From varargs functions; gdb doesn't seem to have any
11859              interest in this information, so just ignore it for now.
11860              (FIXME?) */
11861           break;
11862         case DW_TAG_template_type_param:
11863           suppress_add = 1;
11864           /* Fall through.  */
11865         case DW_TAG_class_type:
11866         case DW_TAG_interface_type:
11867         case DW_TAG_structure_type:
11868         case DW_TAG_union_type:
11869         case DW_TAG_set_type:
11870         case DW_TAG_enumeration_type:
11871           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11872           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11873
11874           {
11875             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11876                really ever be static objects: otherwise, if you try
11877                to, say, break of a class's method and you're in a file
11878                which doesn't mention that class, it won't work unless
11879                the check for all static symbols in lookup_symbol_aux
11880                saves you.  See the OtherFileClass tests in
11881                gdb.c++/namespace.exp.  */
11882
11883             if (!suppress_add)
11884               {
11885                 list_to_add = (cu->list_in_scope == &file_symbols
11886                                && (cu->language == language_cplus
11887                                    || cu->language == language_java)
11888                                ? &global_symbols : cu->list_in_scope);
11889
11890                 /* The semantics of C++ state that "struct foo {
11891                    ... }" also defines a typedef for "foo".  A Java
11892                    class declaration also defines a typedef for the
11893                    class.  */
11894                 if (cu->language == language_cplus
11895                     || cu->language == language_java
11896                     || cu->language == language_ada)
11897                   {
11898                     /* The symbol's name is already allocated along
11899                        with this objfile, so we don't need to
11900                        duplicate it for the type.  */
11901                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11902                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11903                   }
11904               }
11905           }
11906           break;
11907         case DW_TAG_typedef:
11908           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11909           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11910           list_to_add = cu->list_in_scope;
11911           break;
11912         case DW_TAG_base_type:
11913         case DW_TAG_subrange_type:
11914           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11915           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11916           list_to_add = cu->list_in_scope;
11917           break;
11918         case DW_TAG_enumerator:
11919           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11920           if (attr)
11921             {
11922               dwarf2_const_value (attr, sym, cu);
11923             }
11924           {
11925             /* NOTE: carlton/2003-11-10: See comment above in the
11926                DW_TAG_class_type, etc. block.  */
11927
11928             list_to_add = (cu->list_in_scope == &file_symbols
11929                            && (cu->language == language_cplus
11930                                || cu->language == language_java)
11931                            ? &global_symbols : cu->list_in_scope);
11932           }
11933           break;
11934         case DW_TAG_namespace:
11935           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11936           list_to_add = &global_symbols;
11937           break;
11938         default:
11939           /* Not a tag we recognize.  Hopefully we aren't processing
11940              trash data, but since we must specifically ignore things
11941              we don't recognize, there is nothing else we should do at
11942              this point.  */
11943           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11944                      dwarf_tag_name (die->tag));
11945           break;
11946         }
11947
11948       if (suppress_add)
11949         {
11950           sym->hash_next = objfile->template_symbols;
11951           objfile->template_symbols = sym;
11952           list_to_add = NULL;
11953         }
11954
11955       if (list_to_add != NULL)
11956         add_symbol_to_list (sym, list_to_add);
11957
11958       /* For the benefit of old versions of GCC, check for anonymous
11959          namespaces based on the demangled name.  */
11960       if (!processing_has_namespace_info
11961           && cu->language == language_cplus)
11962         cp_scan_for_anonymous_namespaces (sym, objfile);
11963     }
11964   return (sym);
11965 }
11966
11967 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11968
11969 static struct symbol *
11970 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11971 {
11972   return new_symbol_full (die, type, cu, NULL);
11973 }
11974
11975 /* Given an attr with a DW_FORM_dataN value in host byte order,
11976    zero-extend it as appropriate for the symbol's type.  The DWARF
11977    standard (v4) is not entirely clear about the meaning of using
11978    DW_FORM_dataN for a constant with a signed type, where the type is
11979    wider than the data.  The conclusion of a discussion on the DWARF
11980    list was that this is unspecified.  We choose to always zero-extend
11981    because that is the interpretation long in use by GCC.  */
11982
11983 static gdb_byte *
11984 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11985                          const char *name, struct obstack *obstack,
11986                          struct dwarf2_cu *cu, long *value, int bits)
11987 {
11988   struct objfile *objfile = cu->objfile;
11989   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11990                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11991   LONGEST l = DW_UNSND (attr);
11992
11993   if (bits < sizeof (*value) * 8)
11994     {
11995       l &= ((LONGEST) 1 << bits) - 1;
11996       *value = l;
11997     }
11998   else if (bits == sizeof (*value) * 8)
11999     *value = l;
12000   else
12001     {
12002       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12003       store_unsigned_integer (bytes, bits / 8, byte_order, l);
12004       return bytes;
12005     }
12006
12007   return NULL;
12008 }
12009
12010 /* Read a constant value from an attribute.  Either set *VALUE, or if
12011    the value does not fit in *VALUE, set *BYTES - either already
12012    allocated on the objfile obstack, or newly allocated on OBSTACK,
12013    or, set *BATON, if we translated the constant to a location
12014    expression.  */
12015
12016 static void
12017 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12018                          const char *name, struct obstack *obstack,
12019                          struct dwarf2_cu *cu,
12020                          long *value, gdb_byte **bytes,
12021                          struct dwarf2_locexpr_baton **baton)
12022 {
12023   struct objfile *objfile = cu->objfile;
12024   struct comp_unit_head *cu_header = &cu->header;
12025   struct dwarf_block *blk;
12026   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12027                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12028
12029   *value = 0;
12030   *bytes = NULL;
12031   *baton = NULL;
12032
12033   switch (attr->form)
12034     {
12035     case DW_FORM_addr:
12036       {
12037         gdb_byte *data;
12038
12039         if (TYPE_LENGTH (type) != cu_header->addr_size)
12040           dwarf2_const_value_length_mismatch_complaint (name,
12041                                                         cu_header->addr_size,
12042                                                         TYPE_LENGTH (type));
12043         /* Symbols of this form are reasonably rare, so we just
12044            piggyback on the existing location code rather than writing
12045            a new implementation of symbol_computed_ops.  */
12046         *baton = obstack_alloc (&objfile->objfile_obstack,
12047                                 sizeof (struct dwarf2_locexpr_baton));
12048         (*baton)->per_cu = cu->per_cu;
12049         gdb_assert ((*baton)->per_cu);
12050
12051         (*baton)->size = 2 + cu_header->addr_size;
12052         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12053         (*baton)->data = data;
12054
12055         data[0] = DW_OP_addr;
12056         store_unsigned_integer (&data[1], cu_header->addr_size,
12057                                 byte_order, DW_ADDR (attr));
12058         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12059       }
12060       break;
12061     case DW_FORM_string:
12062     case DW_FORM_strp:
12063       /* DW_STRING is already allocated on the objfile obstack, point
12064          directly to it.  */
12065       *bytes = (gdb_byte *) DW_STRING (attr);
12066       break;
12067     case DW_FORM_block1:
12068     case DW_FORM_block2:
12069     case DW_FORM_block4:
12070     case DW_FORM_block:
12071     case DW_FORM_exprloc:
12072       blk = DW_BLOCK (attr);
12073       if (TYPE_LENGTH (type) != blk->size)
12074         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12075                                                       TYPE_LENGTH (type));
12076       *bytes = blk->data;
12077       break;
12078
12079       /* The DW_AT_const_value attributes are supposed to carry the
12080          symbol's value "represented as it would be on the target
12081          architecture."  By the time we get here, it's already been
12082          converted to host endianness, so we just need to sign- or
12083          zero-extend it as appropriate.  */
12084     case DW_FORM_data1:
12085       *bytes = dwarf2_const_value_data (attr, type, name,
12086                                         obstack, cu, value, 8);
12087       break;
12088     case DW_FORM_data2:
12089       *bytes = dwarf2_const_value_data (attr, type, name,
12090                                         obstack, cu, value, 16);
12091       break;
12092     case DW_FORM_data4:
12093       *bytes = dwarf2_const_value_data (attr, type, name,
12094                                         obstack, cu, value, 32);
12095       break;
12096     case DW_FORM_data8:
12097       *bytes = dwarf2_const_value_data (attr, type, name,
12098                                         obstack, cu, value, 64);
12099       break;
12100
12101     case DW_FORM_sdata:
12102       *value = DW_SND (attr);
12103       break;
12104
12105     case DW_FORM_udata:
12106       *value = DW_UNSND (attr);
12107       break;
12108
12109     default:
12110       complaint (&symfile_complaints,
12111                  _("unsupported const value attribute form: '%s'"),
12112                  dwarf_form_name (attr->form));
12113       *value = 0;
12114       break;
12115     }
12116 }
12117
12118
12119 /* Copy constant value from an attribute to a symbol.  */
12120
12121 static void
12122 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12123                     struct dwarf2_cu *cu)
12124 {
12125   struct objfile *objfile = cu->objfile;
12126   struct comp_unit_head *cu_header = &cu->header;
12127   long value;
12128   gdb_byte *bytes;
12129   struct dwarf2_locexpr_baton *baton;
12130
12131   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12132                            SYMBOL_PRINT_NAME (sym),
12133                            &objfile->objfile_obstack, cu,
12134                            &value, &bytes, &baton);
12135
12136   if (baton != NULL)
12137     {
12138       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12139       SYMBOL_LOCATION_BATON (sym) = baton;
12140       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12141     }
12142   else if (bytes != NULL)
12143      {
12144       SYMBOL_VALUE_BYTES (sym) = bytes;
12145       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12146     }
12147   else
12148     {
12149       SYMBOL_VALUE (sym) = value;
12150       SYMBOL_CLASS (sym) = LOC_CONST;
12151     }
12152 }
12153
12154 /* Return the type of the die in question using its DW_AT_type attribute.  */
12155
12156 static struct type *
12157 die_type (struct die_info *die, struct dwarf2_cu *cu)
12158 {
12159   struct attribute *type_attr;
12160
12161   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12162   if (!type_attr)
12163     {
12164       /* A missing DW_AT_type represents a void type.  */
12165       return objfile_type (cu->objfile)->builtin_void;
12166     }
12167
12168   return lookup_die_type (die, type_attr, cu);
12169 }
12170
12171 /* True iff CU's producer generates GNAT Ada auxiliary information
12172    that allows to find parallel types through that information instead
12173    of having to do expensive parallel lookups by type name.  */
12174
12175 static int
12176 need_gnat_info (struct dwarf2_cu *cu)
12177 {
12178   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12179      of GNAT produces this auxiliary information, without any indication
12180      that it is produced.  Part of enhancing the FSF version of GNAT
12181      to produce that information will be to put in place an indicator
12182      that we can use in order to determine whether the descriptive type
12183      info is available or not.  One suggestion that has been made is
12184      to use a new attribute, attached to the CU die.  For now, assume
12185      that the descriptive type info is not available.  */
12186   return 0;
12187 }
12188
12189 /* Return the auxiliary type of the die in question using its
12190    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12191    attribute is not present.  */
12192
12193 static struct type *
12194 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12195 {
12196   struct attribute *type_attr;
12197
12198   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12199   if (!type_attr)
12200     return NULL;
12201
12202   return lookup_die_type (die, type_attr, cu);
12203 }
12204
12205 /* If DIE has a descriptive_type attribute, then set the TYPE's
12206    descriptive type accordingly.  */
12207
12208 static void
12209 set_descriptive_type (struct type *type, struct die_info *die,
12210                       struct dwarf2_cu *cu)
12211 {
12212   struct type *descriptive_type = die_descriptive_type (die, cu);
12213
12214   if (descriptive_type)
12215     {
12216       ALLOCATE_GNAT_AUX_TYPE (type);
12217       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12218     }
12219 }
12220
12221 /* Return the containing type of the die in question using its
12222    DW_AT_containing_type attribute.  */
12223
12224 static struct type *
12225 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12226 {
12227   struct attribute *type_attr;
12228
12229   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12230   if (!type_attr)
12231     error (_("Dwarf Error: Problem turning containing type into gdb type "
12232              "[in module %s]"), cu->objfile->name);
12233
12234   return lookup_die_type (die, type_attr, cu);
12235 }
12236
12237 /* Look up the type of DIE in CU using its type attribute ATTR.
12238    If there is no type substitute an error marker.  */
12239
12240 static struct type *
12241 lookup_die_type (struct die_info *die, struct attribute *attr,
12242                  struct dwarf2_cu *cu)
12243 {
12244   struct objfile *objfile = cu->objfile;
12245   struct type *this_type;
12246
12247   /* First see if we have it cached.  */
12248
12249   if (is_ref_attr (attr))
12250     {
12251       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12252
12253       this_type = get_die_type_at_offset (offset, cu->per_cu);
12254     }
12255   else if (attr->form == DW_FORM_ref_sig8)
12256     {
12257       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12258       struct dwarf2_cu *sig_cu;
12259       unsigned int offset;
12260
12261       /* sig_type will be NULL if the signatured type is missing from
12262          the debug info.  */
12263       if (sig_type == NULL)
12264         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12265                  "at 0x%x [in module %s]"),
12266                die->offset, objfile->name);
12267
12268       gdb_assert (sig_type->per_cu.debug_types_section);
12269       offset = sig_type->per_cu.offset + sig_type->type_offset;
12270       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12271     }
12272   else
12273     {
12274       dump_die_for_error (die);
12275       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12276              dwarf_attr_name (attr->name), objfile->name);
12277     }
12278
12279   /* If not cached we need to read it in.  */
12280
12281   if (this_type == NULL)
12282     {
12283       struct die_info *type_die;
12284       struct dwarf2_cu *type_cu = cu;
12285
12286       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12287       /* If the type is cached, we should have found it above.  */
12288       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12289       this_type = read_type_die_1 (type_die, type_cu);
12290     }
12291
12292   /* If we still don't have a type use an error marker.  */
12293
12294   if (this_type == NULL)
12295     {
12296       char *message, *saved;
12297
12298       /* read_type_die already issued a complaint.  */
12299       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12300                             objfile->name,
12301                             cu->header.offset,
12302                             die->offset);
12303       saved = obstack_copy0 (&objfile->objfile_obstack,
12304                              message, strlen (message));
12305       xfree (message);
12306
12307       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12308     }
12309
12310   return this_type;
12311 }
12312
12313 /* Return the type in DIE, CU.
12314    Returns NULL for invalid types.
12315
12316    This first does a lookup in the appropriate type_hash table,
12317    and only reads the die in if necessary.
12318
12319    NOTE: This can be called when reading in partial or full symbols.  */
12320
12321 static struct type *
12322 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12323 {
12324   struct type *this_type;
12325
12326   this_type = get_die_type (die, cu);
12327   if (this_type)
12328     return this_type;
12329
12330   return read_type_die_1 (die, cu);
12331 }
12332
12333 /* Read the type in DIE, CU.
12334    Returns NULL for invalid types.  */
12335
12336 static struct type *
12337 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12338 {
12339   struct type *this_type = NULL;
12340
12341   switch (die->tag)
12342     {
12343     case DW_TAG_class_type:
12344     case DW_TAG_interface_type:
12345     case DW_TAG_structure_type:
12346     case DW_TAG_union_type:
12347       this_type = read_structure_type (die, cu);
12348       break;
12349     case DW_TAG_enumeration_type:
12350       this_type = read_enumeration_type (die, cu);
12351       break;
12352     case DW_TAG_subprogram:
12353     case DW_TAG_subroutine_type:
12354     case DW_TAG_inlined_subroutine:
12355       this_type = read_subroutine_type (die, cu);
12356       break;
12357     case DW_TAG_array_type:
12358       this_type = read_array_type (die, cu);
12359       break;
12360     case DW_TAG_set_type:
12361       this_type = read_set_type (die, cu);
12362       break;
12363     case DW_TAG_pointer_type:
12364       this_type = read_tag_pointer_type (die, cu);
12365       break;
12366     case DW_TAG_ptr_to_member_type:
12367       this_type = read_tag_ptr_to_member_type (die, cu);
12368       break;
12369     case DW_TAG_reference_type:
12370       this_type = read_tag_reference_type (die, cu);
12371       break;
12372     case DW_TAG_const_type:
12373       this_type = read_tag_const_type (die, cu);
12374       break;
12375     case DW_TAG_volatile_type:
12376       this_type = read_tag_volatile_type (die, cu);
12377       break;
12378     case DW_TAG_string_type:
12379       this_type = read_tag_string_type (die, cu);
12380       break;
12381     case DW_TAG_typedef:
12382       this_type = read_typedef (die, cu);
12383       break;
12384     case DW_TAG_subrange_type:
12385       this_type = read_subrange_type (die, cu);
12386       break;
12387     case DW_TAG_base_type:
12388       this_type = read_base_type (die, cu);
12389       break;
12390     case DW_TAG_unspecified_type:
12391       this_type = read_unspecified_type (die, cu);
12392       break;
12393     case DW_TAG_namespace:
12394       this_type = read_namespace_type (die, cu);
12395       break;
12396     case DW_TAG_module:
12397       this_type = read_module_type (die, cu);
12398       break;
12399     default:
12400       complaint (&symfile_complaints,
12401                  _("unexpected tag in read_type_die: '%s'"),
12402                  dwarf_tag_name (die->tag));
12403       break;
12404     }
12405
12406   return this_type;
12407 }
12408
12409 /* See if we can figure out if the class lives in a namespace.  We do
12410    this by looking for a member function; its demangled name will
12411    contain namespace info, if there is any.
12412    Return the computed name or NULL.
12413    Space for the result is allocated on the objfile's obstack.
12414    This is the full-die version of guess_partial_die_structure_name.
12415    In this case we know DIE has no useful parent.  */
12416
12417 static char *
12418 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12419 {
12420   struct die_info *spec_die;
12421   struct dwarf2_cu *spec_cu;
12422   struct die_info *child;
12423
12424   spec_cu = cu;
12425   spec_die = die_specification (die, &spec_cu);
12426   if (spec_die != NULL)
12427     {
12428       die = spec_die;
12429       cu = spec_cu;
12430     }
12431
12432   for (child = die->child;
12433        child != NULL;
12434        child = child->sibling)
12435     {
12436       if (child->tag == DW_TAG_subprogram)
12437         {
12438           struct attribute *attr;
12439
12440           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12441           if (attr == NULL)
12442             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12443           if (attr != NULL)
12444             {
12445               char *actual_name
12446                 = language_class_name_from_physname (cu->language_defn,
12447                                                      DW_STRING (attr));
12448               char *name = NULL;
12449
12450               if (actual_name != NULL)
12451                 {
12452                   char *die_name = dwarf2_name (die, cu);
12453
12454                   if (die_name != NULL
12455                       && strcmp (die_name, actual_name) != 0)
12456                     {
12457                       /* Strip off the class name from the full name.
12458                          We want the prefix.  */
12459                       int die_name_len = strlen (die_name);
12460                       int actual_name_len = strlen (actual_name);
12461
12462                       /* Test for '::' as a sanity check.  */
12463                       if (actual_name_len > die_name_len + 2
12464                           && actual_name[actual_name_len
12465                                          - die_name_len - 1] == ':')
12466                         name =
12467                           obsavestring (actual_name,
12468                                         actual_name_len - die_name_len - 2,
12469                                         &cu->objfile->objfile_obstack);
12470                     }
12471                 }
12472               xfree (actual_name);
12473               return name;
12474             }
12475         }
12476     }
12477
12478   return NULL;
12479 }
12480
12481 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12482    prefix part in such case.  See
12483    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12484
12485 static char *
12486 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12487 {
12488   struct attribute *attr;
12489   char *base;
12490
12491   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12492       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12493     return NULL;
12494
12495   attr = dwarf2_attr (die, DW_AT_name, cu);
12496   if (attr != NULL && DW_STRING (attr) != NULL)
12497     return NULL;
12498
12499   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12500   if (attr == NULL)
12501     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12502   if (attr == NULL || DW_STRING (attr) == NULL)
12503     return NULL;
12504
12505   /* dwarf2_name had to be already called.  */
12506   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12507
12508   /* Strip the base name, keep any leading namespaces/classes.  */
12509   base = strrchr (DW_STRING (attr), ':');
12510   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12511     return "";
12512
12513   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12514                        &cu->objfile->objfile_obstack);
12515 }
12516
12517 /* Return the name of the namespace/class that DIE is defined within,
12518    or "" if we can't tell.  The caller should not xfree the result.
12519
12520    For example, if we're within the method foo() in the following
12521    code:
12522
12523    namespace N {
12524      class C {
12525        void foo () {
12526        }
12527      };
12528    }
12529
12530    then determine_prefix on foo's die will return "N::C".  */
12531
12532 static char *
12533 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12534 {
12535   struct die_info *parent, *spec_die;
12536   struct dwarf2_cu *spec_cu;
12537   struct type *parent_type;
12538   char *retval;
12539
12540   if (cu->language != language_cplus && cu->language != language_java
12541       && cu->language != language_fortran)
12542     return "";
12543
12544   retval = anonymous_struct_prefix (die, cu);
12545   if (retval)
12546     return retval;
12547
12548   /* We have to be careful in the presence of DW_AT_specification.
12549      For example, with GCC 3.4, given the code
12550
12551      namespace N {
12552        void foo() {
12553          // Definition of N::foo.
12554        }
12555      }
12556
12557      then we'll have a tree of DIEs like this:
12558
12559      1: DW_TAG_compile_unit
12560        2: DW_TAG_namespace        // N
12561          3: DW_TAG_subprogram     // declaration of N::foo
12562        4: DW_TAG_subprogram       // definition of N::foo
12563             DW_AT_specification   // refers to die #3
12564
12565      Thus, when processing die #4, we have to pretend that we're in
12566      the context of its DW_AT_specification, namely the contex of die
12567      #3.  */
12568   spec_cu = cu;
12569   spec_die = die_specification (die, &spec_cu);
12570   if (spec_die == NULL)
12571     parent = die->parent;
12572   else
12573     {
12574       parent = spec_die->parent;
12575       cu = spec_cu;
12576     }
12577
12578   if (parent == NULL)
12579     return "";
12580   else if (parent->building_fullname)
12581     {
12582       const char *name;
12583       const char *parent_name;
12584
12585       /* It has been seen on RealView 2.2 built binaries,
12586          DW_TAG_template_type_param types actually _defined_ as
12587          children of the parent class:
12588
12589          enum E {};
12590          template class <class Enum> Class{};
12591          Class<enum E> class_e;
12592
12593          1: DW_TAG_class_type (Class)
12594            2: DW_TAG_enumeration_type (E)
12595              3: DW_TAG_enumerator (enum1:0)
12596              3: DW_TAG_enumerator (enum2:1)
12597              ...
12598            2: DW_TAG_template_type_param
12599               DW_AT_type  DW_FORM_ref_udata (E)
12600
12601          Besides being broken debug info, it can put GDB into an
12602          infinite loop.  Consider:
12603
12604          When we're building the full name for Class<E>, we'll start
12605          at Class, and go look over its template type parameters,
12606          finding E.  We'll then try to build the full name of E, and
12607          reach here.  We're now trying to build the full name of E,
12608          and look over the parent DIE for containing scope.  In the
12609          broken case, if we followed the parent DIE of E, we'd again
12610          find Class, and once again go look at its template type
12611          arguments, etc., etc.  Simply don't consider such parent die
12612          as source-level parent of this die (it can't be, the language
12613          doesn't allow it), and break the loop here.  */
12614       name = dwarf2_name (die, cu);
12615       parent_name = dwarf2_name (parent, cu);
12616       complaint (&symfile_complaints,
12617                  _("template param type '%s' defined within parent '%s'"),
12618                  name ? name : "<unknown>",
12619                  parent_name ? parent_name : "<unknown>");
12620       return "";
12621     }
12622   else
12623     switch (parent->tag)
12624       {
12625       case DW_TAG_namespace:
12626         parent_type = read_type_die (parent, cu);
12627         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12628            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12629            Work around this problem here.  */
12630         if (cu->language == language_cplus
12631             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12632           return "";
12633         /* We give a name to even anonymous namespaces.  */
12634         return TYPE_TAG_NAME (parent_type);
12635       case DW_TAG_class_type:
12636       case DW_TAG_interface_type:
12637       case DW_TAG_structure_type:
12638       case DW_TAG_union_type:
12639       case DW_TAG_module:
12640         parent_type = read_type_die (parent, cu);
12641         if (TYPE_TAG_NAME (parent_type) != NULL)
12642           return TYPE_TAG_NAME (parent_type);
12643         else
12644           /* An anonymous structure is only allowed non-static data
12645              members; no typedefs, no member functions, et cetera.
12646              So it does not need a prefix.  */
12647           return "";
12648       case DW_TAG_compile_unit:
12649         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12650         if (cu->language == language_cplus
12651             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12652             && die->child != NULL
12653             && (die->tag == DW_TAG_class_type
12654                 || die->tag == DW_TAG_structure_type
12655                 || die->tag == DW_TAG_union_type))
12656           {
12657             char *name = guess_full_die_structure_name (die, cu);
12658             if (name != NULL)
12659               return name;
12660           }
12661         return "";
12662       default:
12663         return determine_prefix (parent, cu);
12664       }
12665 }
12666
12667 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12668    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12669    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12670    an obconcat, otherwise allocate storage for the result.  The CU argument is
12671    used to determine the language and hence, the appropriate separator.  */
12672
12673 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12674
12675 static char *
12676 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12677                  int physname, struct dwarf2_cu *cu)
12678 {
12679   const char *lead = "";
12680   const char *sep;
12681
12682   if (suffix == NULL || suffix[0] == '\0'
12683       || prefix == NULL || prefix[0] == '\0')
12684     sep = "";
12685   else if (cu->language == language_java)
12686     sep = ".";
12687   else if (cu->language == language_fortran && physname)
12688     {
12689       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12690          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12691
12692       lead = "__";
12693       sep = "_MOD_";
12694     }
12695   else
12696     sep = "::";
12697
12698   if (prefix == NULL)
12699     prefix = "";
12700   if (suffix == NULL)
12701     suffix = "";
12702
12703   if (obs == NULL)
12704     {
12705       char *retval
12706         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12707
12708       strcpy (retval, lead);
12709       strcat (retval, prefix);
12710       strcat (retval, sep);
12711       strcat (retval, suffix);
12712       return retval;
12713     }
12714   else
12715     {
12716       /* We have an obstack.  */
12717       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12718     }
12719 }
12720
12721 /* Return sibling of die, NULL if no sibling.  */
12722
12723 static struct die_info *
12724 sibling_die (struct die_info *die)
12725 {
12726   return die->sibling;
12727 }
12728
12729 /* Get name of a die, return NULL if not found.  */
12730
12731 static char *
12732 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12733                           struct obstack *obstack)
12734 {
12735   if (name && cu->language == language_cplus)
12736     {
12737       char *canon_name = cp_canonicalize_string (name);
12738
12739       if (canon_name != NULL)
12740         {
12741           if (strcmp (canon_name, name) != 0)
12742             name = obsavestring (canon_name, strlen (canon_name),
12743                                  obstack);
12744           xfree (canon_name);
12745         }
12746     }
12747
12748   return name;
12749 }
12750
12751 /* Get name of a die, return NULL if not found.  */
12752
12753 static char *
12754 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12755 {
12756   struct attribute *attr;
12757
12758   attr = dwarf2_attr (die, DW_AT_name, cu);
12759   if ((!attr || !DW_STRING (attr))
12760       && die->tag != DW_TAG_class_type
12761       && die->tag != DW_TAG_interface_type
12762       && die->tag != DW_TAG_structure_type
12763       && die->tag != DW_TAG_union_type)
12764     return NULL;
12765
12766   switch (die->tag)
12767     {
12768     case DW_TAG_compile_unit:
12769       /* Compilation units have a DW_AT_name that is a filename, not
12770          a source language identifier.  */
12771     case DW_TAG_enumeration_type:
12772     case DW_TAG_enumerator:
12773       /* These tags always have simple identifiers already; no need
12774          to canonicalize them.  */
12775       return DW_STRING (attr);
12776
12777     case DW_TAG_subprogram:
12778       /* Java constructors will all be named "<init>", so return
12779          the class name when we see this special case.  */
12780       if (cu->language == language_java
12781           && DW_STRING (attr) != NULL
12782           && strcmp (DW_STRING (attr), "<init>") == 0)
12783         {
12784           struct dwarf2_cu *spec_cu = cu;
12785           struct die_info *spec_die;
12786
12787           /* GCJ will output '<init>' for Java constructor names.
12788              For this special case, return the name of the parent class.  */
12789
12790           /* GCJ may output suprogram DIEs with AT_specification set.
12791              If so, use the name of the specified DIE.  */
12792           spec_die = die_specification (die, &spec_cu);
12793           if (spec_die != NULL)
12794             return dwarf2_name (spec_die, spec_cu);
12795
12796           do
12797             {
12798               die = die->parent;
12799               if (die->tag == DW_TAG_class_type)
12800                 return dwarf2_name (die, cu);
12801             }
12802           while (die->tag != DW_TAG_compile_unit);
12803         }
12804       break;
12805
12806     case DW_TAG_class_type:
12807     case DW_TAG_interface_type:
12808     case DW_TAG_structure_type:
12809     case DW_TAG_union_type:
12810       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12811          structures or unions.  These were of the form "._%d" in GCC 4.1,
12812          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12813          and GCC 4.4.  We work around this problem by ignoring these.  */
12814       if (attr && DW_STRING (attr)
12815           && (strncmp (DW_STRING (attr), "._", 2) == 0
12816               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12817         return NULL;
12818
12819       /* GCC might emit a nameless typedef that has a linkage name.  See
12820          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12821       if (!attr || DW_STRING (attr) == NULL)
12822         {
12823           char *demangled = NULL;
12824
12825           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12826           if (attr == NULL)
12827             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12828
12829           if (attr == NULL || DW_STRING (attr) == NULL)
12830             return NULL;
12831
12832           /* Avoid demangling DW_STRING (attr) the second time on a second
12833              call for the same DIE.  */
12834           if (!DW_STRING_IS_CANONICAL (attr))
12835             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12836
12837           if (demangled)
12838             {
12839               char *base;
12840
12841               /* FIXME: we already did this for the partial symbol... */
12842               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12843                                                &cu->objfile->objfile_obstack);
12844               DW_STRING_IS_CANONICAL (attr) = 1;
12845               xfree (demangled);
12846
12847               /* Strip any leading namespaces/classes, keep only the base name.
12848                  DW_AT_name for named DIEs does not contain the prefixes.  */
12849               base = strrchr (DW_STRING (attr), ':');
12850               if (base && base > DW_STRING (attr) && base[-1] == ':')
12851                 return &base[1];
12852               else
12853                 return DW_STRING (attr);
12854             }
12855         }
12856       break;
12857
12858     default:
12859       break;
12860     }
12861
12862   if (!DW_STRING_IS_CANONICAL (attr))
12863     {
12864       DW_STRING (attr)
12865         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12866                                     &cu->objfile->objfile_obstack);
12867       DW_STRING_IS_CANONICAL (attr) = 1;
12868     }
12869   return DW_STRING (attr);
12870 }
12871
12872 /* Return the die that this die in an extension of, or NULL if there
12873    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12874    containing the return value on output.  */
12875
12876 static struct die_info *
12877 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12878 {
12879   struct attribute *attr;
12880
12881   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12882   if (attr == NULL)
12883     return NULL;
12884
12885   return follow_die_ref (die, attr, ext_cu);
12886 }
12887
12888 /* Convert a DIE tag into its string name.  */
12889
12890 static char *
12891 dwarf_tag_name (unsigned tag)
12892 {
12893   switch (tag)
12894     {
12895     case DW_TAG_padding:
12896       return "DW_TAG_padding";
12897     case DW_TAG_array_type:
12898       return "DW_TAG_array_type";
12899     case DW_TAG_class_type:
12900       return "DW_TAG_class_type";
12901     case DW_TAG_entry_point:
12902       return "DW_TAG_entry_point";
12903     case DW_TAG_enumeration_type:
12904       return "DW_TAG_enumeration_type";
12905     case DW_TAG_formal_parameter:
12906       return "DW_TAG_formal_parameter";
12907     case DW_TAG_imported_declaration:
12908       return "DW_TAG_imported_declaration";
12909     case DW_TAG_label:
12910       return "DW_TAG_label";
12911     case DW_TAG_lexical_block:
12912       return "DW_TAG_lexical_block";
12913     case DW_TAG_member:
12914       return "DW_TAG_member";
12915     case DW_TAG_pointer_type:
12916       return "DW_TAG_pointer_type";
12917     case DW_TAG_reference_type:
12918       return "DW_TAG_reference_type";
12919     case DW_TAG_compile_unit:
12920       return "DW_TAG_compile_unit";
12921     case DW_TAG_string_type:
12922       return "DW_TAG_string_type";
12923     case DW_TAG_structure_type:
12924       return "DW_TAG_structure_type";
12925     case DW_TAG_subroutine_type:
12926       return "DW_TAG_subroutine_type";
12927     case DW_TAG_typedef:
12928       return "DW_TAG_typedef";
12929     case DW_TAG_union_type:
12930       return "DW_TAG_union_type";
12931     case DW_TAG_unspecified_parameters:
12932       return "DW_TAG_unspecified_parameters";
12933     case DW_TAG_variant:
12934       return "DW_TAG_variant";
12935     case DW_TAG_common_block:
12936       return "DW_TAG_common_block";
12937     case DW_TAG_common_inclusion:
12938       return "DW_TAG_common_inclusion";
12939     case DW_TAG_inheritance:
12940       return "DW_TAG_inheritance";
12941     case DW_TAG_inlined_subroutine:
12942       return "DW_TAG_inlined_subroutine";
12943     case DW_TAG_module:
12944       return "DW_TAG_module";
12945     case DW_TAG_ptr_to_member_type:
12946       return "DW_TAG_ptr_to_member_type";
12947     case DW_TAG_set_type:
12948       return "DW_TAG_set_type";
12949     case DW_TAG_subrange_type:
12950       return "DW_TAG_subrange_type";
12951     case DW_TAG_with_stmt:
12952       return "DW_TAG_with_stmt";
12953     case DW_TAG_access_declaration:
12954       return "DW_TAG_access_declaration";
12955     case DW_TAG_base_type:
12956       return "DW_TAG_base_type";
12957     case DW_TAG_catch_block:
12958       return "DW_TAG_catch_block";
12959     case DW_TAG_const_type:
12960       return "DW_TAG_const_type";
12961     case DW_TAG_constant:
12962       return "DW_TAG_constant";
12963     case DW_TAG_enumerator:
12964       return "DW_TAG_enumerator";
12965     case DW_TAG_file_type:
12966       return "DW_TAG_file_type";
12967     case DW_TAG_friend:
12968       return "DW_TAG_friend";
12969     case DW_TAG_namelist:
12970       return "DW_TAG_namelist";
12971     case DW_TAG_namelist_item:
12972       return "DW_TAG_namelist_item";
12973     case DW_TAG_packed_type:
12974       return "DW_TAG_packed_type";
12975     case DW_TAG_subprogram:
12976       return "DW_TAG_subprogram";
12977     case DW_TAG_template_type_param:
12978       return "DW_TAG_template_type_param";
12979     case DW_TAG_template_value_param:
12980       return "DW_TAG_template_value_param";
12981     case DW_TAG_thrown_type:
12982       return "DW_TAG_thrown_type";
12983     case DW_TAG_try_block:
12984       return "DW_TAG_try_block";
12985     case DW_TAG_variant_part:
12986       return "DW_TAG_variant_part";
12987     case DW_TAG_variable:
12988       return "DW_TAG_variable";
12989     case DW_TAG_volatile_type:
12990       return "DW_TAG_volatile_type";
12991     case DW_TAG_dwarf_procedure:
12992       return "DW_TAG_dwarf_procedure";
12993     case DW_TAG_restrict_type:
12994       return "DW_TAG_restrict_type";
12995     case DW_TAG_interface_type:
12996       return "DW_TAG_interface_type";
12997     case DW_TAG_namespace:
12998       return "DW_TAG_namespace";
12999     case DW_TAG_imported_module:
13000       return "DW_TAG_imported_module";
13001     case DW_TAG_unspecified_type:
13002       return "DW_TAG_unspecified_type";
13003     case DW_TAG_partial_unit:
13004       return "DW_TAG_partial_unit";
13005     case DW_TAG_imported_unit:
13006       return "DW_TAG_imported_unit";
13007     case DW_TAG_condition:
13008       return "DW_TAG_condition";
13009     case DW_TAG_shared_type:
13010       return "DW_TAG_shared_type";
13011     case DW_TAG_type_unit:
13012       return "DW_TAG_type_unit";
13013     case DW_TAG_MIPS_loop:
13014       return "DW_TAG_MIPS_loop";
13015     case DW_TAG_HP_array_descriptor:
13016       return "DW_TAG_HP_array_descriptor";
13017     case DW_TAG_format_label:
13018       return "DW_TAG_format_label";
13019     case DW_TAG_function_template:
13020       return "DW_TAG_function_template";
13021     case DW_TAG_class_template:
13022       return "DW_TAG_class_template";
13023     case DW_TAG_GNU_BINCL:
13024       return "DW_TAG_GNU_BINCL";
13025     case DW_TAG_GNU_EINCL:
13026       return "DW_TAG_GNU_EINCL";
13027     case DW_TAG_upc_shared_type:
13028       return "DW_TAG_upc_shared_type";
13029     case DW_TAG_upc_strict_type:
13030       return "DW_TAG_upc_strict_type";
13031     case DW_TAG_upc_relaxed_type:
13032       return "DW_TAG_upc_relaxed_type";
13033     case DW_TAG_PGI_kanji_type:
13034       return "DW_TAG_PGI_kanji_type";
13035     case DW_TAG_PGI_interface_block:
13036       return "DW_TAG_PGI_interface_block";
13037     case DW_TAG_GNU_call_site:
13038       return "DW_TAG_GNU_call_site";
13039     default:
13040       return "DW_TAG_<unknown>";
13041     }
13042 }
13043
13044 /* Convert a DWARF attribute code into its string name.  */
13045
13046 static char *
13047 dwarf_attr_name (unsigned attr)
13048 {
13049   switch (attr)
13050     {
13051     case DW_AT_sibling:
13052       return "DW_AT_sibling";
13053     case DW_AT_location:
13054       return "DW_AT_location";
13055     case DW_AT_name:
13056       return "DW_AT_name";
13057     case DW_AT_ordering:
13058       return "DW_AT_ordering";
13059     case DW_AT_subscr_data:
13060       return "DW_AT_subscr_data";
13061     case DW_AT_byte_size:
13062       return "DW_AT_byte_size";
13063     case DW_AT_bit_offset:
13064       return "DW_AT_bit_offset";
13065     case DW_AT_bit_size:
13066       return "DW_AT_bit_size";
13067     case DW_AT_element_list:
13068       return "DW_AT_element_list";
13069     case DW_AT_stmt_list:
13070       return "DW_AT_stmt_list";
13071     case DW_AT_low_pc:
13072       return "DW_AT_low_pc";
13073     case DW_AT_high_pc:
13074       return "DW_AT_high_pc";
13075     case DW_AT_language:
13076       return "DW_AT_language";
13077     case DW_AT_member:
13078       return "DW_AT_member";
13079     case DW_AT_discr:
13080       return "DW_AT_discr";
13081     case DW_AT_discr_value:
13082       return "DW_AT_discr_value";
13083     case DW_AT_visibility:
13084       return "DW_AT_visibility";
13085     case DW_AT_import:
13086       return "DW_AT_import";
13087     case DW_AT_string_length:
13088       return "DW_AT_string_length";
13089     case DW_AT_common_reference:
13090       return "DW_AT_common_reference";
13091     case DW_AT_comp_dir:
13092       return "DW_AT_comp_dir";
13093     case DW_AT_const_value:
13094       return "DW_AT_const_value";
13095     case DW_AT_containing_type:
13096       return "DW_AT_containing_type";
13097     case DW_AT_default_value:
13098       return "DW_AT_default_value";
13099     case DW_AT_inline:
13100       return "DW_AT_inline";
13101     case DW_AT_is_optional:
13102       return "DW_AT_is_optional";
13103     case DW_AT_lower_bound:
13104       return "DW_AT_lower_bound";
13105     case DW_AT_producer:
13106       return "DW_AT_producer";
13107     case DW_AT_prototyped:
13108       return "DW_AT_prototyped";
13109     case DW_AT_return_addr:
13110       return "DW_AT_return_addr";
13111     case DW_AT_start_scope:
13112       return "DW_AT_start_scope";
13113     case DW_AT_bit_stride:
13114       return "DW_AT_bit_stride";
13115     case DW_AT_upper_bound:
13116       return "DW_AT_upper_bound";
13117     case DW_AT_abstract_origin:
13118       return "DW_AT_abstract_origin";
13119     case DW_AT_accessibility:
13120       return "DW_AT_accessibility";
13121     case DW_AT_address_class:
13122       return "DW_AT_address_class";
13123     case DW_AT_artificial:
13124       return "DW_AT_artificial";
13125     case DW_AT_base_types:
13126       return "DW_AT_base_types";
13127     case DW_AT_calling_convention:
13128       return "DW_AT_calling_convention";
13129     case DW_AT_count:
13130       return "DW_AT_count";
13131     case DW_AT_data_member_location:
13132       return "DW_AT_data_member_location";
13133     case DW_AT_decl_column:
13134       return "DW_AT_decl_column";
13135     case DW_AT_decl_file:
13136       return "DW_AT_decl_file";
13137     case DW_AT_decl_line:
13138       return "DW_AT_decl_line";
13139     case DW_AT_declaration:
13140       return "DW_AT_declaration";
13141     case DW_AT_discr_list:
13142       return "DW_AT_discr_list";
13143     case DW_AT_encoding:
13144       return "DW_AT_encoding";
13145     case DW_AT_external:
13146       return "DW_AT_external";
13147     case DW_AT_frame_base:
13148       return "DW_AT_frame_base";
13149     case DW_AT_friend:
13150       return "DW_AT_friend";
13151     case DW_AT_identifier_case:
13152       return "DW_AT_identifier_case";
13153     case DW_AT_macro_info:
13154       return "DW_AT_macro_info";
13155     case DW_AT_namelist_items:
13156       return "DW_AT_namelist_items";
13157     case DW_AT_priority:
13158       return "DW_AT_priority";
13159     case DW_AT_segment:
13160       return "DW_AT_segment";
13161     case DW_AT_specification:
13162       return "DW_AT_specification";
13163     case DW_AT_static_link:
13164       return "DW_AT_static_link";
13165     case DW_AT_type:
13166       return "DW_AT_type";
13167     case DW_AT_use_location:
13168       return "DW_AT_use_location";
13169     case DW_AT_variable_parameter:
13170       return "DW_AT_variable_parameter";
13171     case DW_AT_virtuality:
13172       return "DW_AT_virtuality";
13173     case DW_AT_vtable_elem_location:
13174       return "DW_AT_vtable_elem_location";
13175     /* DWARF 3 values.  */
13176     case DW_AT_allocated:
13177       return "DW_AT_allocated";
13178     case DW_AT_associated:
13179       return "DW_AT_associated";
13180     case DW_AT_data_location:
13181       return "DW_AT_data_location";
13182     case DW_AT_byte_stride:
13183       return "DW_AT_byte_stride";
13184     case DW_AT_entry_pc:
13185       return "DW_AT_entry_pc";
13186     case DW_AT_use_UTF8:
13187       return "DW_AT_use_UTF8";
13188     case DW_AT_extension:
13189       return "DW_AT_extension";
13190     case DW_AT_ranges:
13191       return "DW_AT_ranges";
13192     case DW_AT_trampoline:
13193       return "DW_AT_trampoline";
13194     case DW_AT_call_column:
13195       return "DW_AT_call_column";
13196     case DW_AT_call_file:
13197       return "DW_AT_call_file";
13198     case DW_AT_call_line:
13199       return "DW_AT_call_line";
13200     case DW_AT_description:
13201       return "DW_AT_description";
13202     case DW_AT_binary_scale:
13203       return "DW_AT_binary_scale";
13204     case DW_AT_decimal_scale:
13205       return "DW_AT_decimal_scale";
13206     case DW_AT_small:
13207       return "DW_AT_small";
13208     case DW_AT_decimal_sign:
13209       return "DW_AT_decimal_sign";
13210     case DW_AT_digit_count:
13211       return "DW_AT_digit_count";
13212     case DW_AT_picture_string:
13213       return "DW_AT_picture_string";
13214     case DW_AT_mutable:
13215       return "DW_AT_mutable";
13216     case DW_AT_threads_scaled:
13217       return "DW_AT_threads_scaled";
13218     case DW_AT_explicit:
13219       return "DW_AT_explicit";
13220     case DW_AT_object_pointer:
13221       return "DW_AT_object_pointer";
13222     case DW_AT_endianity:
13223       return "DW_AT_endianity";
13224     case DW_AT_elemental:
13225       return "DW_AT_elemental";
13226     case DW_AT_pure:
13227       return "DW_AT_pure";
13228     case DW_AT_recursive:
13229       return "DW_AT_recursive";
13230     /* DWARF 4 values.  */
13231     case DW_AT_signature:
13232       return "DW_AT_signature";
13233     case DW_AT_linkage_name:
13234       return "DW_AT_linkage_name";
13235     /* SGI/MIPS extensions.  */
13236 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13237     case DW_AT_MIPS_fde:
13238       return "DW_AT_MIPS_fde";
13239 #endif
13240     case DW_AT_MIPS_loop_begin:
13241       return "DW_AT_MIPS_loop_begin";
13242     case DW_AT_MIPS_tail_loop_begin:
13243       return "DW_AT_MIPS_tail_loop_begin";
13244     case DW_AT_MIPS_epilog_begin:
13245       return "DW_AT_MIPS_epilog_begin";
13246     case DW_AT_MIPS_loop_unroll_factor:
13247       return "DW_AT_MIPS_loop_unroll_factor";
13248     case DW_AT_MIPS_software_pipeline_depth:
13249       return "DW_AT_MIPS_software_pipeline_depth";
13250     case DW_AT_MIPS_linkage_name:
13251       return "DW_AT_MIPS_linkage_name";
13252     case DW_AT_MIPS_stride:
13253       return "DW_AT_MIPS_stride";
13254     case DW_AT_MIPS_abstract_name:
13255       return "DW_AT_MIPS_abstract_name";
13256     case DW_AT_MIPS_clone_origin:
13257       return "DW_AT_MIPS_clone_origin";
13258     case DW_AT_MIPS_has_inlines:
13259       return "DW_AT_MIPS_has_inlines";
13260     /* HP extensions.  */
13261 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13262     case DW_AT_HP_block_index:
13263       return "DW_AT_HP_block_index";
13264 #endif
13265     case DW_AT_HP_unmodifiable:
13266       return "DW_AT_HP_unmodifiable";
13267     case DW_AT_HP_actuals_stmt_list:
13268       return "DW_AT_HP_actuals_stmt_list";
13269     case DW_AT_HP_proc_per_section:
13270       return "DW_AT_HP_proc_per_section";
13271     case DW_AT_HP_raw_data_ptr:
13272       return "DW_AT_HP_raw_data_ptr";
13273     case DW_AT_HP_pass_by_reference:
13274       return "DW_AT_HP_pass_by_reference";
13275     case DW_AT_HP_opt_level:
13276       return "DW_AT_HP_opt_level";
13277     case DW_AT_HP_prof_version_id:
13278       return "DW_AT_HP_prof_version_id";
13279     case DW_AT_HP_opt_flags:
13280       return "DW_AT_HP_opt_flags";
13281     case DW_AT_HP_cold_region_low_pc:
13282       return "DW_AT_HP_cold_region_low_pc";
13283     case DW_AT_HP_cold_region_high_pc:
13284       return "DW_AT_HP_cold_region_high_pc";
13285     case DW_AT_HP_all_variables_modifiable:
13286       return "DW_AT_HP_all_variables_modifiable";
13287     case DW_AT_HP_linkage_name:
13288       return "DW_AT_HP_linkage_name";
13289     case DW_AT_HP_prof_flags:
13290       return "DW_AT_HP_prof_flags";
13291     /* GNU extensions.  */
13292     case DW_AT_sf_names:
13293       return "DW_AT_sf_names";
13294     case DW_AT_src_info:
13295       return "DW_AT_src_info";
13296     case DW_AT_mac_info:
13297       return "DW_AT_mac_info";
13298     case DW_AT_src_coords:
13299       return "DW_AT_src_coords";
13300     case DW_AT_body_begin:
13301       return "DW_AT_body_begin";
13302     case DW_AT_body_end:
13303       return "DW_AT_body_end";
13304     case DW_AT_GNU_vector:
13305       return "DW_AT_GNU_vector";
13306     case DW_AT_GNU_odr_signature:
13307       return "DW_AT_GNU_odr_signature";
13308     /* VMS extensions.  */
13309     case DW_AT_VMS_rtnbeg_pd_address:
13310       return "DW_AT_VMS_rtnbeg_pd_address";
13311     /* UPC extension.  */
13312     case DW_AT_upc_threads_scaled:
13313       return "DW_AT_upc_threads_scaled";
13314     /* PGI (STMicroelectronics) extensions.  */
13315     case DW_AT_PGI_lbase:
13316       return "DW_AT_PGI_lbase";
13317     case DW_AT_PGI_soffset:
13318       return "DW_AT_PGI_soffset";
13319     case DW_AT_PGI_lstride:
13320       return "DW_AT_PGI_lstride";
13321     default:
13322       return "DW_AT_<unknown>";
13323     }
13324 }
13325
13326 /* Convert a DWARF value form code into its string name.  */
13327
13328 static char *
13329 dwarf_form_name (unsigned form)
13330 {
13331   switch (form)
13332     {
13333     case DW_FORM_addr:
13334       return "DW_FORM_addr";
13335     case DW_FORM_block2:
13336       return "DW_FORM_block2";
13337     case DW_FORM_block4:
13338       return "DW_FORM_block4";
13339     case DW_FORM_data2:
13340       return "DW_FORM_data2";
13341     case DW_FORM_data4:
13342       return "DW_FORM_data4";
13343     case DW_FORM_data8:
13344       return "DW_FORM_data8";
13345     case DW_FORM_string:
13346       return "DW_FORM_string";
13347     case DW_FORM_block:
13348       return "DW_FORM_block";
13349     case DW_FORM_block1:
13350       return "DW_FORM_block1";
13351     case DW_FORM_data1:
13352       return "DW_FORM_data1";
13353     case DW_FORM_flag:
13354       return "DW_FORM_flag";
13355     case DW_FORM_sdata:
13356       return "DW_FORM_sdata";
13357     case DW_FORM_strp:
13358       return "DW_FORM_strp";
13359     case DW_FORM_udata:
13360       return "DW_FORM_udata";
13361     case DW_FORM_ref_addr:
13362       return "DW_FORM_ref_addr";
13363     case DW_FORM_ref1:
13364       return "DW_FORM_ref1";
13365     case DW_FORM_ref2:
13366       return "DW_FORM_ref2";
13367     case DW_FORM_ref4:
13368       return "DW_FORM_ref4";
13369     case DW_FORM_ref8:
13370       return "DW_FORM_ref8";
13371     case DW_FORM_ref_udata:
13372       return "DW_FORM_ref_udata";
13373     case DW_FORM_indirect:
13374       return "DW_FORM_indirect";
13375     case DW_FORM_sec_offset:
13376       return "DW_FORM_sec_offset";
13377     case DW_FORM_exprloc:
13378       return "DW_FORM_exprloc";
13379     case DW_FORM_flag_present:
13380       return "DW_FORM_flag_present";
13381     case DW_FORM_ref_sig8:
13382       return "DW_FORM_ref_sig8";
13383     default:
13384       return "DW_FORM_<unknown>";
13385     }
13386 }
13387
13388 /* Convert a DWARF stack opcode into its string name.  */
13389
13390 const char *
13391 dwarf_stack_op_name (unsigned op)
13392 {
13393   switch (op)
13394     {
13395     case DW_OP_addr:
13396       return "DW_OP_addr";
13397     case DW_OP_deref:
13398       return "DW_OP_deref";
13399     case DW_OP_const1u:
13400       return "DW_OP_const1u";
13401     case DW_OP_const1s:
13402       return "DW_OP_const1s";
13403     case DW_OP_const2u:
13404       return "DW_OP_const2u";
13405     case DW_OP_const2s:
13406       return "DW_OP_const2s";
13407     case DW_OP_const4u:
13408       return "DW_OP_const4u";
13409     case DW_OP_const4s:
13410       return "DW_OP_const4s";
13411     case DW_OP_const8u:
13412       return "DW_OP_const8u";
13413     case DW_OP_const8s:
13414       return "DW_OP_const8s";
13415     case DW_OP_constu:
13416       return "DW_OP_constu";
13417     case DW_OP_consts:
13418       return "DW_OP_consts";
13419     case DW_OP_dup:
13420       return "DW_OP_dup";
13421     case DW_OP_drop:
13422       return "DW_OP_drop";
13423     case DW_OP_over:
13424       return "DW_OP_over";
13425     case DW_OP_pick:
13426       return "DW_OP_pick";
13427     case DW_OP_swap:
13428       return "DW_OP_swap";
13429     case DW_OP_rot:
13430       return "DW_OP_rot";
13431     case DW_OP_xderef:
13432       return "DW_OP_xderef";
13433     case DW_OP_abs:
13434       return "DW_OP_abs";
13435     case DW_OP_and:
13436       return "DW_OP_and";
13437     case DW_OP_div:
13438       return "DW_OP_div";
13439     case DW_OP_minus:
13440       return "DW_OP_minus";
13441     case DW_OP_mod:
13442       return "DW_OP_mod";
13443     case DW_OP_mul:
13444       return "DW_OP_mul";
13445     case DW_OP_neg:
13446       return "DW_OP_neg";
13447     case DW_OP_not:
13448       return "DW_OP_not";
13449     case DW_OP_or:
13450       return "DW_OP_or";
13451     case DW_OP_plus:
13452       return "DW_OP_plus";
13453     case DW_OP_plus_uconst:
13454       return "DW_OP_plus_uconst";
13455     case DW_OP_shl:
13456       return "DW_OP_shl";
13457     case DW_OP_shr:
13458       return "DW_OP_shr";
13459     case DW_OP_shra:
13460       return "DW_OP_shra";
13461     case DW_OP_xor:
13462       return "DW_OP_xor";
13463     case DW_OP_bra:
13464       return "DW_OP_bra";
13465     case DW_OP_eq:
13466       return "DW_OP_eq";
13467     case DW_OP_ge:
13468       return "DW_OP_ge";
13469     case DW_OP_gt:
13470       return "DW_OP_gt";
13471     case DW_OP_le:
13472       return "DW_OP_le";
13473     case DW_OP_lt:
13474       return "DW_OP_lt";
13475     case DW_OP_ne:
13476       return "DW_OP_ne";
13477     case DW_OP_skip:
13478       return "DW_OP_skip";
13479     case DW_OP_lit0:
13480       return "DW_OP_lit0";
13481     case DW_OP_lit1:
13482       return "DW_OP_lit1";
13483     case DW_OP_lit2:
13484       return "DW_OP_lit2";
13485     case DW_OP_lit3:
13486       return "DW_OP_lit3";
13487     case DW_OP_lit4:
13488       return "DW_OP_lit4";
13489     case DW_OP_lit5:
13490       return "DW_OP_lit5";
13491     case DW_OP_lit6:
13492       return "DW_OP_lit6";
13493     case DW_OP_lit7:
13494       return "DW_OP_lit7";
13495     case DW_OP_lit8:
13496       return "DW_OP_lit8";
13497     case DW_OP_lit9:
13498       return "DW_OP_lit9";
13499     case DW_OP_lit10:
13500       return "DW_OP_lit10";
13501     case DW_OP_lit11:
13502       return "DW_OP_lit11";
13503     case DW_OP_lit12:
13504       return "DW_OP_lit12";
13505     case DW_OP_lit13:
13506       return "DW_OP_lit13";
13507     case DW_OP_lit14:
13508       return "DW_OP_lit14";
13509     case DW_OP_lit15:
13510       return "DW_OP_lit15";
13511     case DW_OP_lit16:
13512       return "DW_OP_lit16";
13513     case DW_OP_lit17:
13514       return "DW_OP_lit17";
13515     case DW_OP_lit18:
13516       return "DW_OP_lit18";
13517     case DW_OP_lit19:
13518       return "DW_OP_lit19";
13519     case DW_OP_lit20:
13520       return "DW_OP_lit20";
13521     case DW_OP_lit21:
13522       return "DW_OP_lit21";
13523     case DW_OP_lit22:
13524       return "DW_OP_lit22";
13525     case DW_OP_lit23:
13526       return "DW_OP_lit23";
13527     case DW_OP_lit24:
13528       return "DW_OP_lit24";
13529     case DW_OP_lit25:
13530       return "DW_OP_lit25";
13531     case DW_OP_lit26:
13532       return "DW_OP_lit26";
13533     case DW_OP_lit27:
13534       return "DW_OP_lit27";
13535     case DW_OP_lit28:
13536       return "DW_OP_lit28";
13537     case DW_OP_lit29:
13538       return "DW_OP_lit29";
13539     case DW_OP_lit30:
13540       return "DW_OP_lit30";
13541     case DW_OP_lit31:
13542       return "DW_OP_lit31";
13543     case DW_OP_reg0:
13544       return "DW_OP_reg0";
13545     case DW_OP_reg1:
13546       return "DW_OP_reg1";
13547     case DW_OP_reg2:
13548       return "DW_OP_reg2";
13549     case DW_OP_reg3:
13550       return "DW_OP_reg3";
13551     case DW_OP_reg4:
13552       return "DW_OP_reg4";
13553     case DW_OP_reg5:
13554       return "DW_OP_reg5";
13555     case DW_OP_reg6:
13556       return "DW_OP_reg6";
13557     case DW_OP_reg7:
13558       return "DW_OP_reg7";
13559     case DW_OP_reg8:
13560       return "DW_OP_reg8";
13561     case DW_OP_reg9:
13562       return "DW_OP_reg9";
13563     case DW_OP_reg10:
13564       return "DW_OP_reg10";
13565     case DW_OP_reg11:
13566       return "DW_OP_reg11";
13567     case DW_OP_reg12:
13568       return "DW_OP_reg12";
13569     case DW_OP_reg13:
13570       return "DW_OP_reg13";
13571     case DW_OP_reg14:
13572       return "DW_OP_reg14";
13573     case DW_OP_reg15:
13574       return "DW_OP_reg15";
13575     case DW_OP_reg16:
13576       return "DW_OP_reg16";
13577     case DW_OP_reg17:
13578       return "DW_OP_reg17";
13579     case DW_OP_reg18:
13580       return "DW_OP_reg18";
13581     case DW_OP_reg19:
13582       return "DW_OP_reg19";
13583     case DW_OP_reg20:
13584       return "DW_OP_reg20";
13585     case DW_OP_reg21:
13586       return "DW_OP_reg21";
13587     case DW_OP_reg22:
13588       return "DW_OP_reg22";
13589     case DW_OP_reg23:
13590       return "DW_OP_reg23";
13591     case DW_OP_reg24:
13592       return "DW_OP_reg24";
13593     case DW_OP_reg25:
13594       return "DW_OP_reg25";
13595     case DW_OP_reg26:
13596       return "DW_OP_reg26";
13597     case DW_OP_reg27:
13598       return "DW_OP_reg27";
13599     case DW_OP_reg28:
13600       return "DW_OP_reg28";
13601     case DW_OP_reg29:
13602       return "DW_OP_reg29";
13603     case DW_OP_reg30:
13604       return "DW_OP_reg30";
13605     case DW_OP_reg31:
13606       return "DW_OP_reg31";
13607     case DW_OP_breg0:
13608       return "DW_OP_breg0";
13609     case DW_OP_breg1:
13610       return "DW_OP_breg1";
13611     case DW_OP_breg2:
13612       return "DW_OP_breg2";
13613     case DW_OP_breg3:
13614       return "DW_OP_breg3";
13615     case DW_OP_breg4:
13616       return "DW_OP_breg4";
13617     case DW_OP_breg5:
13618       return "DW_OP_breg5";
13619     case DW_OP_breg6:
13620       return "DW_OP_breg6";
13621     case DW_OP_breg7:
13622       return "DW_OP_breg7";
13623     case DW_OP_breg8:
13624       return "DW_OP_breg8";
13625     case DW_OP_breg9:
13626       return "DW_OP_breg9";
13627     case DW_OP_breg10:
13628       return "DW_OP_breg10";
13629     case DW_OP_breg11:
13630       return "DW_OP_breg11";
13631     case DW_OP_breg12:
13632       return "DW_OP_breg12";
13633     case DW_OP_breg13:
13634       return "DW_OP_breg13";
13635     case DW_OP_breg14:
13636       return "DW_OP_breg14";
13637     case DW_OP_breg15:
13638       return "DW_OP_breg15";
13639     case DW_OP_breg16:
13640       return "DW_OP_breg16";
13641     case DW_OP_breg17:
13642       return "DW_OP_breg17";
13643     case DW_OP_breg18:
13644       return "DW_OP_breg18";
13645     case DW_OP_breg19:
13646       return "DW_OP_breg19";
13647     case DW_OP_breg20:
13648       return "DW_OP_breg20";
13649     case DW_OP_breg21:
13650       return "DW_OP_breg21";
13651     case DW_OP_breg22:
13652       return "DW_OP_breg22";
13653     case DW_OP_breg23:
13654       return "DW_OP_breg23";
13655     case DW_OP_breg24:
13656       return "DW_OP_breg24";
13657     case DW_OP_breg25:
13658       return "DW_OP_breg25";
13659     case DW_OP_breg26:
13660       return "DW_OP_breg26";
13661     case DW_OP_breg27:
13662       return "DW_OP_breg27";
13663     case DW_OP_breg28:
13664       return "DW_OP_breg28";
13665     case DW_OP_breg29:
13666       return "DW_OP_breg29";
13667     case DW_OP_breg30:
13668       return "DW_OP_breg30";
13669     case DW_OP_breg31:
13670       return "DW_OP_breg31";
13671     case DW_OP_regx:
13672       return "DW_OP_regx";
13673     case DW_OP_fbreg:
13674       return "DW_OP_fbreg";
13675     case DW_OP_bregx:
13676       return "DW_OP_bregx";
13677     case DW_OP_piece:
13678       return "DW_OP_piece";
13679     case DW_OP_deref_size:
13680       return "DW_OP_deref_size";
13681     case DW_OP_xderef_size:
13682       return "DW_OP_xderef_size";
13683     case DW_OP_nop:
13684       return "DW_OP_nop";
13685     /* DWARF 3 extensions.  */
13686     case DW_OP_push_object_address:
13687       return "DW_OP_push_object_address";
13688     case DW_OP_call2:
13689       return "DW_OP_call2";
13690     case DW_OP_call4:
13691       return "DW_OP_call4";
13692     case DW_OP_call_ref:
13693       return "DW_OP_call_ref";
13694     case DW_OP_form_tls_address:
13695       return "DW_OP_form_tls_address";
13696     case DW_OP_call_frame_cfa:
13697       return "DW_OP_call_frame_cfa";
13698     case DW_OP_bit_piece:
13699       return "DW_OP_bit_piece";
13700     /* DWARF 4 extensions.  */
13701     case DW_OP_implicit_value:
13702       return "DW_OP_implicit_value";
13703     case DW_OP_stack_value:
13704       return "DW_OP_stack_value";
13705     /* GNU extensions.  */
13706     case DW_OP_GNU_push_tls_address:
13707       return "DW_OP_GNU_push_tls_address";
13708     case DW_OP_GNU_uninit:
13709       return "DW_OP_GNU_uninit";
13710     case DW_OP_GNU_implicit_pointer:
13711       return "DW_OP_GNU_implicit_pointer";
13712     case DW_OP_GNU_entry_value:
13713       return "DW_OP_GNU_entry_value";
13714     case DW_OP_GNU_const_type:
13715       return "DW_OP_GNU_const_type";
13716     case DW_OP_GNU_regval_type:
13717       return "DW_OP_GNU_regval_type";
13718     case DW_OP_GNU_deref_type:
13719       return "DW_OP_GNU_deref_type";
13720     case DW_OP_GNU_convert:
13721       return "DW_OP_GNU_convert";
13722     case DW_OP_GNU_reinterpret:
13723       return "DW_OP_GNU_reinterpret";
13724     default:
13725       return NULL;
13726     }
13727 }
13728
13729 static char *
13730 dwarf_bool_name (unsigned mybool)
13731 {
13732   if (mybool)
13733     return "TRUE";
13734   else
13735     return "FALSE";
13736 }
13737
13738 /* Convert a DWARF type code into its string name.  */
13739
13740 static char *
13741 dwarf_type_encoding_name (unsigned enc)
13742 {
13743   switch (enc)
13744     {
13745     case DW_ATE_void:
13746       return "DW_ATE_void";
13747     case DW_ATE_address:
13748       return "DW_ATE_address";
13749     case DW_ATE_boolean:
13750       return "DW_ATE_boolean";
13751     case DW_ATE_complex_float:
13752       return "DW_ATE_complex_float";
13753     case DW_ATE_float:
13754       return "DW_ATE_float";
13755     case DW_ATE_signed:
13756       return "DW_ATE_signed";
13757     case DW_ATE_signed_char:
13758       return "DW_ATE_signed_char";
13759     case DW_ATE_unsigned:
13760       return "DW_ATE_unsigned";
13761     case DW_ATE_unsigned_char:
13762       return "DW_ATE_unsigned_char";
13763     /* DWARF 3.  */
13764     case DW_ATE_imaginary_float:
13765       return "DW_ATE_imaginary_float";
13766     case DW_ATE_packed_decimal:
13767       return "DW_ATE_packed_decimal";
13768     case DW_ATE_numeric_string:
13769       return "DW_ATE_numeric_string";
13770     case DW_ATE_edited:
13771       return "DW_ATE_edited";
13772     case DW_ATE_signed_fixed:
13773       return "DW_ATE_signed_fixed";
13774     case DW_ATE_unsigned_fixed:
13775       return "DW_ATE_unsigned_fixed";
13776     case DW_ATE_decimal_float:
13777       return "DW_ATE_decimal_float";
13778     /* DWARF 4.  */
13779     case DW_ATE_UTF:
13780       return "DW_ATE_UTF";
13781     /* HP extensions.  */
13782     case DW_ATE_HP_float80:
13783       return "DW_ATE_HP_float80";
13784     case DW_ATE_HP_complex_float80:
13785       return "DW_ATE_HP_complex_float80";
13786     case DW_ATE_HP_float128:
13787       return "DW_ATE_HP_float128";
13788     case DW_ATE_HP_complex_float128:
13789       return "DW_ATE_HP_complex_float128";
13790     case DW_ATE_HP_floathpintel:
13791       return "DW_ATE_HP_floathpintel";
13792     case DW_ATE_HP_imaginary_float80:
13793       return "DW_ATE_HP_imaginary_float80";
13794     case DW_ATE_HP_imaginary_float128:
13795       return "DW_ATE_HP_imaginary_float128";
13796     default:
13797       return "DW_ATE_<unknown>";
13798     }
13799 }
13800
13801 /* Convert a DWARF call frame info operation to its string name.  */
13802
13803 #if 0
13804 static char *
13805 dwarf_cfi_name (unsigned cfi_opc)
13806 {
13807   switch (cfi_opc)
13808     {
13809     case DW_CFA_advance_loc:
13810       return "DW_CFA_advance_loc";
13811     case DW_CFA_offset:
13812       return "DW_CFA_offset";
13813     case DW_CFA_restore:
13814       return "DW_CFA_restore";
13815     case DW_CFA_nop:
13816       return "DW_CFA_nop";
13817     case DW_CFA_set_loc:
13818       return "DW_CFA_set_loc";
13819     case DW_CFA_advance_loc1:
13820       return "DW_CFA_advance_loc1";
13821     case DW_CFA_advance_loc2:
13822       return "DW_CFA_advance_loc2";
13823     case DW_CFA_advance_loc4:
13824       return "DW_CFA_advance_loc4";
13825     case DW_CFA_offset_extended:
13826       return "DW_CFA_offset_extended";
13827     case DW_CFA_restore_extended:
13828       return "DW_CFA_restore_extended";
13829     case DW_CFA_undefined:
13830       return "DW_CFA_undefined";
13831     case DW_CFA_same_value:
13832       return "DW_CFA_same_value";
13833     case DW_CFA_register:
13834       return "DW_CFA_register";
13835     case DW_CFA_remember_state:
13836       return "DW_CFA_remember_state";
13837     case DW_CFA_restore_state:
13838       return "DW_CFA_restore_state";
13839     case DW_CFA_def_cfa:
13840       return "DW_CFA_def_cfa";
13841     case DW_CFA_def_cfa_register:
13842       return "DW_CFA_def_cfa_register";
13843     case DW_CFA_def_cfa_offset:
13844       return "DW_CFA_def_cfa_offset";
13845     /* DWARF 3.  */
13846     case DW_CFA_def_cfa_expression:
13847       return "DW_CFA_def_cfa_expression";
13848     case DW_CFA_expression:
13849       return "DW_CFA_expression";
13850     case DW_CFA_offset_extended_sf:
13851       return "DW_CFA_offset_extended_sf";
13852     case DW_CFA_def_cfa_sf:
13853       return "DW_CFA_def_cfa_sf";
13854     case DW_CFA_def_cfa_offset_sf:
13855       return "DW_CFA_def_cfa_offset_sf";
13856     case DW_CFA_val_offset:
13857       return "DW_CFA_val_offset";
13858     case DW_CFA_val_offset_sf:
13859       return "DW_CFA_val_offset_sf";
13860     case DW_CFA_val_expression:
13861       return "DW_CFA_val_expression";
13862     /* SGI/MIPS specific.  */
13863     case DW_CFA_MIPS_advance_loc8:
13864       return "DW_CFA_MIPS_advance_loc8";
13865     /* GNU extensions.  */
13866     case DW_CFA_GNU_window_save:
13867       return "DW_CFA_GNU_window_save";
13868     case DW_CFA_GNU_args_size:
13869       return "DW_CFA_GNU_args_size";
13870     case DW_CFA_GNU_negative_offset_extended:
13871       return "DW_CFA_GNU_negative_offset_extended";
13872     default:
13873       return "DW_CFA_<unknown>";
13874     }
13875 }
13876 #endif
13877
13878 static void
13879 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13880 {
13881   unsigned int i;
13882
13883   print_spaces (indent, f);
13884   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13885            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13886
13887   if (die->parent != NULL)
13888     {
13889       print_spaces (indent, f);
13890       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13891                           die->parent->offset);
13892     }
13893
13894   print_spaces (indent, f);
13895   fprintf_unfiltered (f, "  has children: %s\n",
13896            dwarf_bool_name (die->child != NULL));
13897
13898   print_spaces (indent, f);
13899   fprintf_unfiltered (f, "  attributes:\n");
13900
13901   for (i = 0; i < die->num_attrs; ++i)
13902     {
13903       print_spaces (indent, f);
13904       fprintf_unfiltered (f, "    %s (%s) ",
13905                dwarf_attr_name (die->attrs[i].name),
13906                dwarf_form_name (die->attrs[i].form));
13907
13908       switch (die->attrs[i].form)
13909         {
13910         case DW_FORM_ref_addr:
13911         case DW_FORM_addr:
13912           fprintf_unfiltered (f, "address: ");
13913           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13914           break;
13915         case DW_FORM_block2:
13916         case DW_FORM_block4:
13917         case DW_FORM_block:
13918         case DW_FORM_block1:
13919           fprintf_unfiltered (f, "block: size %d",
13920                               DW_BLOCK (&die->attrs[i])->size);
13921           break;
13922         case DW_FORM_exprloc:
13923           fprintf_unfiltered (f, "expression: size %u",
13924                               DW_BLOCK (&die->attrs[i])->size);
13925           break;
13926         case DW_FORM_ref1:
13927         case DW_FORM_ref2:
13928         case DW_FORM_ref4:
13929           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13930                               (long) (DW_ADDR (&die->attrs[i])));
13931           break;
13932         case DW_FORM_data1:
13933         case DW_FORM_data2:
13934         case DW_FORM_data4:
13935         case DW_FORM_data8:
13936         case DW_FORM_udata:
13937         case DW_FORM_sdata:
13938           fprintf_unfiltered (f, "constant: %s",
13939                               pulongest (DW_UNSND (&die->attrs[i])));
13940           break;
13941         case DW_FORM_sec_offset:
13942           fprintf_unfiltered (f, "section offset: %s",
13943                               pulongest (DW_UNSND (&die->attrs[i])));
13944           break;
13945         case DW_FORM_ref_sig8:
13946           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13947             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13948                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13949           else
13950             fprintf_unfiltered (f, "signatured type, offset: unknown");
13951           break;
13952         case DW_FORM_string:
13953         case DW_FORM_strp:
13954           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13955                    DW_STRING (&die->attrs[i])
13956                    ? DW_STRING (&die->attrs[i]) : "",
13957                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13958           break;
13959         case DW_FORM_flag:
13960           if (DW_UNSND (&die->attrs[i]))
13961             fprintf_unfiltered (f, "flag: TRUE");
13962           else
13963             fprintf_unfiltered (f, "flag: FALSE");
13964           break;
13965         case DW_FORM_flag_present:
13966           fprintf_unfiltered (f, "flag: TRUE");
13967           break;
13968         case DW_FORM_indirect:
13969           /* The reader will have reduced the indirect form to
13970              the "base form" so this form should not occur.  */
13971           fprintf_unfiltered (f, 
13972                               "unexpected attribute form: DW_FORM_indirect");
13973           break;
13974         default:
13975           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13976                    die->attrs[i].form);
13977           break;
13978         }
13979       fprintf_unfiltered (f, "\n");
13980     }
13981 }
13982
13983 static void
13984 dump_die_for_error (struct die_info *die)
13985 {
13986   dump_die_shallow (gdb_stderr, 0, die);
13987 }
13988
13989 static void
13990 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13991 {
13992   int indent = level * 4;
13993
13994   gdb_assert (die != NULL);
13995
13996   if (level >= max_level)
13997     return;
13998
13999   dump_die_shallow (f, indent, die);
14000
14001   if (die->child != NULL)
14002     {
14003       print_spaces (indent, f);
14004       fprintf_unfiltered (f, "  Children:");
14005       if (level + 1 < max_level)
14006         {
14007           fprintf_unfiltered (f, "\n");
14008           dump_die_1 (f, level + 1, max_level, die->child);
14009         }
14010       else
14011         {
14012           fprintf_unfiltered (f,
14013                               " [not printed, max nesting level reached]\n");
14014         }
14015     }
14016
14017   if (die->sibling != NULL && level > 0)
14018     {
14019       dump_die_1 (f, level, max_level, die->sibling);
14020     }
14021 }
14022
14023 /* This is called from the pdie macro in gdbinit.in.
14024    It's not static so gcc will keep a copy callable from gdb.  */
14025
14026 void
14027 dump_die (struct die_info *die, int max_level)
14028 {
14029   dump_die_1 (gdb_stdlog, 0, max_level, die);
14030 }
14031
14032 static void
14033 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14034 {
14035   void **slot;
14036
14037   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14038
14039   *slot = die;
14040 }
14041
14042 static int
14043 is_ref_attr (struct attribute *attr)
14044 {
14045   switch (attr->form)
14046     {
14047     case DW_FORM_ref_addr:
14048     case DW_FORM_ref1:
14049     case DW_FORM_ref2:
14050     case DW_FORM_ref4:
14051     case DW_FORM_ref8:
14052     case DW_FORM_ref_udata:
14053       return 1;
14054     default:
14055       return 0;
14056     }
14057 }
14058
14059 static unsigned int
14060 dwarf2_get_ref_die_offset (struct attribute *attr)
14061 {
14062   if (is_ref_attr (attr))
14063     return DW_ADDR (attr);
14064
14065   complaint (&symfile_complaints,
14066              _("unsupported die ref attribute form: '%s'"),
14067              dwarf_form_name (attr->form));
14068   return 0;
14069 }
14070
14071 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14072  * the value held by the attribute is not constant.  */
14073
14074 static LONGEST
14075 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14076 {
14077   if (attr->form == DW_FORM_sdata)
14078     return DW_SND (attr);
14079   else if (attr->form == DW_FORM_udata
14080            || attr->form == DW_FORM_data1
14081            || attr->form == DW_FORM_data2
14082            || attr->form == DW_FORM_data4
14083            || attr->form == DW_FORM_data8)
14084     return DW_UNSND (attr);
14085   else
14086     {
14087       complaint (&symfile_complaints,
14088                  _("Attribute value is not a constant (%s)"),
14089                  dwarf_form_name (attr->form));
14090       return default_value;
14091     }
14092 }
14093
14094 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14095    unit and add it to our queue.
14096    The result is non-zero if PER_CU was queued, otherwise the result is zero
14097    meaning either PER_CU is already queued or it is already loaded.  */
14098
14099 static int
14100 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14101                        struct dwarf2_per_cu_data *per_cu)
14102 {
14103   /* We may arrive here during partial symbol reading, if we need full
14104      DIEs to process an unusual case (e.g. template arguments).  Do
14105      not queue PER_CU, just tell our caller to load its DIEs.  */
14106   if (dwarf2_per_objfile->reading_partial_symbols)
14107     {
14108       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14109         return 1;
14110       return 0;
14111     }
14112
14113   /* Mark the dependence relation so that we don't flush PER_CU
14114      too early.  */
14115   dwarf2_add_dependence (this_cu, per_cu);
14116
14117   /* If it's already on the queue, we have nothing to do.  */
14118   if (per_cu->queued)
14119     return 0;
14120
14121   /* If the compilation unit is already loaded, just mark it as
14122      used.  */
14123   if (per_cu->cu != NULL)
14124     {
14125       per_cu->cu->last_used = 0;
14126       return 0;
14127     }
14128
14129   /* Add it to the queue.  */
14130   queue_comp_unit (per_cu);
14131
14132   return 1;
14133 }
14134
14135 /* Follow reference or signature attribute ATTR of SRC_DIE.
14136    On entry *REF_CU is the CU of SRC_DIE.
14137    On exit *REF_CU is the CU of the result.  */
14138
14139 static struct die_info *
14140 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14141                        struct dwarf2_cu **ref_cu)
14142 {
14143   struct die_info *die;
14144
14145   if (is_ref_attr (attr))
14146     die = follow_die_ref (src_die, attr, ref_cu);
14147   else if (attr->form == DW_FORM_ref_sig8)
14148     die = follow_die_sig (src_die, attr, ref_cu);
14149   else
14150     {
14151       dump_die_for_error (src_die);
14152       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14153              (*ref_cu)->objfile->name);
14154     }
14155
14156   return die;
14157 }
14158
14159 /* Follow reference OFFSET.
14160    On entry *REF_CU is the CU of the source die referencing OFFSET.
14161    On exit *REF_CU is the CU of the result.
14162    Returns NULL if OFFSET is invalid.  */
14163
14164 static struct die_info *
14165 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14166 {
14167   struct die_info temp_die;
14168   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14169
14170   gdb_assert (cu->per_cu != NULL);
14171
14172   target_cu = cu;
14173
14174   if (cu->per_cu->debug_types_section)
14175     {
14176       /* .debug_types CUs cannot reference anything outside their CU.
14177          If they need to, they have to reference a signatured type via
14178          DW_FORM_ref_sig8.  */
14179       if (! offset_in_cu_p (&cu->header, offset))
14180         return NULL;
14181     }
14182   else if (! offset_in_cu_p (&cu->header, offset))
14183     {
14184       struct dwarf2_per_cu_data *per_cu;
14185
14186       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14187
14188       /* If necessary, add it to the queue and load its DIEs.  */
14189       if (maybe_queue_comp_unit (cu, per_cu))
14190         load_full_comp_unit (per_cu);
14191
14192       target_cu = per_cu->cu;
14193     }
14194   else if (cu->dies == NULL)
14195     {
14196       /* We're loading full DIEs during partial symbol reading.  */
14197       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14198       load_full_comp_unit (cu->per_cu);
14199     }
14200
14201   *ref_cu = target_cu;
14202   temp_die.offset = offset;
14203   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14204 }
14205
14206 /* Follow reference attribute ATTR of SRC_DIE.
14207    On entry *REF_CU is the CU of SRC_DIE.
14208    On exit *REF_CU is the CU of the result.  */
14209
14210 static struct die_info *
14211 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14212                 struct dwarf2_cu **ref_cu)
14213 {
14214   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14215   struct dwarf2_cu *cu = *ref_cu;
14216   struct die_info *die;
14217
14218   die = follow_die_offset (offset, ref_cu);
14219   if (!die)
14220     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14221            "at 0x%x [in module %s]"),
14222            offset, src_die->offset, cu->objfile->name);
14223
14224   return die;
14225 }
14226
14227 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14228    Returned value is intended for DW_OP_call*.  Returned
14229    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14230
14231 struct dwarf2_locexpr_baton
14232 dwarf2_fetch_die_location_block (unsigned int offset,
14233                                  struct dwarf2_per_cu_data *per_cu,
14234                                  CORE_ADDR (*get_frame_pc) (void *baton),
14235                                  void *baton)
14236 {
14237   struct dwarf2_cu *cu;
14238   struct die_info *die;
14239   struct attribute *attr;
14240   struct dwarf2_locexpr_baton retval;
14241
14242   dw2_setup (per_cu->objfile);
14243
14244   if (per_cu->cu == NULL)
14245     load_cu (per_cu);
14246   cu = per_cu->cu;
14247
14248   die = follow_die_offset (offset, &cu);
14249   if (!die)
14250     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14251            offset, per_cu->objfile->name);
14252
14253   attr = dwarf2_attr (die, DW_AT_location, cu);
14254   if (!attr)
14255     {
14256       /* DWARF: "If there is no such attribute, then there is no effect.".
14257          DATA is ignored if SIZE is 0.  */
14258
14259       retval.data = NULL;
14260       retval.size = 0;
14261     }
14262   else if (attr_form_is_section_offset (attr))
14263     {
14264       struct dwarf2_loclist_baton loclist_baton;
14265       CORE_ADDR pc = (*get_frame_pc) (baton);
14266       size_t size;
14267
14268       fill_in_loclist_baton (cu, &loclist_baton, attr);
14269
14270       retval.data = dwarf2_find_location_expression (&loclist_baton,
14271                                                      &size, pc);
14272       retval.size = size;
14273     }
14274   else
14275     {
14276       if (!attr_form_is_block (attr))
14277         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14278                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14279                offset, per_cu->objfile->name);
14280
14281       retval.data = DW_BLOCK (attr)->data;
14282       retval.size = DW_BLOCK (attr)->size;
14283     }
14284   retval.per_cu = cu->per_cu;
14285
14286   age_cached_comp_units ();
14287
14288   return retval;
14289 }
14290
14291 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14292    PER_CU.  */
14293
14294 struct type *
14295 dwarf2_get_die_type (unsigned int die_offset,
14296                      struct dwarf2_per_cu_data *per_cu)
14297 {
14298   dw2_setup (per_cu->objfile);
14299   return get_die_type_at_offset (die_offset, per_cu);
14300 }
14301
14302 /* Follow the signature attribute ATTR in SRC_DIE.
14303    On entry *REF_CU is the CU of SRC_DIE.
14304    On exit *REF_CU is the CU of the result.  */
14305
14306 static struct die_info *
14307 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14308                 struct dwarf2_cu **ref_cu)
14309 {
14310   struct objfile *objfile = (*ref_cu)->objfile;
14311   struct die_info temp_die;
14312   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14313   struct dwarf2_cu *sig_cu;
14314   struct die_info *die;
14315
14316   /* sig_type will be NULL if the signatured type is missing from
14317      the debug info.  */
14318   if (sig_type == NULL)
14319     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14320              "at 0x%x [in module %s]"),
14321            src_die->offset, objfile->name);
14322
14323   /* If necessary, add it to the queue and load its DIEs.  */
14324
14325   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14326     read_signatured_type (sig_type);
14327
14328   gdb_assert (sig_type->per_cu.cu != NULL);
14329
14330   sig_cu = sig_type->per_cu.cu;
14331   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14332   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14333   if (die)
14334     {
14335       *ref_cu = sig_cu;
14336       return die;
14337     }
14338
14339   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14340          "from DIE at 0x%x [in module %s]"),
14341          sig_type->type_offset, src_die->offset, objfile->name);
14342 }
14343
14344 /* Given an offset of a signatured type, return its signatured_type.  */
14345
14346 static struct signatured_type *
14347 lookup_signatured_type_at_offset (struct objfile *objfile,
14348                                   struct dwarf2_section_info *section,
14349                                   unsigned int offset)
14350 {
14351   gdb_byte *info_ptr = section->buffer + offset;
14352   unsigned int length, initial_length_size;
14353   unsigned int sig_offset;
14354   struct signatured_type find_entry, *type_sig;
14355
14356   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14357   sig_offset = (initial_length_size
14358                 + 2 /*version*/
14359                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14360                 + 1 /*address_size*/);
14361   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14362   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14363
14364   /* This is only used to lookup previously recorded types.
14365      If we didn't find it, it's our bug.  */
14366   gdb_assert (type_sig != NULL);
14367   gdb_assert (offset == type_sig->per_cu.offset);
14368
14369   return type_sig;
14370 }
14371
14372 /* Read in signatured type at OFFSET and build its CU and die(s).  */
14373
14374 static void
14375 read_signatured_type_at_offset (struct objfile *objfile,
14376                                 struct dwarf2_section_info *sect,
14377                                 unsigned int offset)
14378 {
14379   struct signatured_type *type_sig;
14380
14381   dwarf2_read_section (objfile, sect);
14382
14383   /* We have the section offset, but we need the signature to do the
14384      hash table lookup.  */
14385   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14386
14387   gdb_assert (type_sig->per_cu.cu == NULL);
14388
14389   read_signatured_type (type_sig);
14390
14391   gdb_assert (type_sig->per_cu.cu != NULL);
14392 }
14393
14394 /* Read in a signatured type and build its CU and DIEs.  */
14395
14396 static void
14397 read_signatured_type (struct signatured_type *type_sig)
14398 {
14399   struct objfile *objfile = type_sig->per_cu.objfile;
14400   gdb_byte *types_ptr;
14401   struct die_reader_specs reader_specs;
14402   struct dwarf2_cu *cu;
14403   ULONGEST signature;
14404   struct cleanup *back_to, *free_cu_cleanup;
14405   struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14406
14407   dwarf2_read_section (objfile, section);
14408   types_ptr = section->buffer + type_sig->per_cu.offset;
14409
14410   gdb_assert (type_sig->per_cu.cu == NULL);
14411
14412   cu = xmalloc (sizeof (*cu));
14413   init_one_comp_unit (cu, &type_sig->per_cu);
14414
14415   /* If an error occurs while loading, release our storage.  */
14416   free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14417
14418   types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14419                                         types_ptr, objfile->obfd);
14420   gdb_assert (signature == type_sig->signature);
14421
14422   cu->die_hash
14423     = htab_create_alloc_ex (cu->header.length / 12,
14424                             die_hash,
14425                             die_eq,
14426                             NULL,
14427                             &cu->comp_unit_obstack,
14428                             hashtab_obstack_allocate,
14429                             dummy_obstack_deallocate);
14430
14431   dwarf2_read_abbrevs (objfile->obfd, cu);
14432   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14433
14434   init_cu_die_reader (&reader_specs, cu);
14435
14436   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14437                                     NULL /*parent*/);
14438
14439   /* We try not to read any attributes in this function, because not
14440      all CUs needed for references have been loaded yet, and symbol
14441      table processing isn't initialized.  But we have to set the CU language,
14442      or we won't be able to build types correctly.  */
14443   prepare_one_comp_unit (cu, cu->dies);
14444
14445   do_cleanups (back_to);
14446
14447   /* We've successfully allocated this compilation unit.  Let our caller
14448      clean it up when finished with it.  */
14449   discard_cleanups (free_cu_cleanup);
14450
14451   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14452   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14453 }
14454
14455 /* Decode simple location descriptions.
14456    Given a pointer to a dwarf block that defines a location, compute
14457    the location and return the value.
14458
14459    NOTE drow/2003-11-18: This function is called in two situations
14460    now: for the address of static or global variables (partial symbols
14461    only) and for offsets into structures which are expected to be
14462    (more or less) constant.  The partial symbol case should go away,
14463    and only the constant case should remain.  That will let this
14464    function complain more accurately.  A few special modes are allowed
14465    without complaint for global variables (for instance, global
14466    register values and thread-local values).
14467
14468    A location description containing no operations indicates that the
14469    object is optimized out.  The return value is 0 for that case.
14470    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14471    callers will only want a very basic result and this can become a
14472    complaint.
14473
14474    Note that stack[0] is unused except as a default error return.  */
14475
14476 static CORE_ADDR
14477 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14478 {
14479   struct objfile *objfile = cu->objfile;
14480   int i;
14481   int size = blk->size;
14482   gdb_byte *data = blk->data;
14483   CORE_ADDR stack[64];
14484   int stacki;
14485   unsigned int bytes_read, unsnd;
14486   gdb_byte op;
14487
14488   i = 0;
14489   stacki = 0;
14490   stack[stacki] = 0;
14491   stack[++stacki] = 0;
14492
14493   while (i < size)
14494     {
14495       op = data[i++];
14496       switch (op)
14497         {
14498         case DW_OP_lit0:
14499         case DW_OP_lit1:
14500         case DW_OP_lit2:
14501         case DW_OP_lit3:
14502         case DW_OP_lit4:
14503         case DW_OP_lit5:
14504         case DW_OP_lit6:
14505         case DW_OP_lit7:
14506         case DW_OP_lit8:
14507         case DW_OP_lit9:
14508         case DW_OP_lit10:
14509         case DW_OP_lit11:
14510         case DW_OP_lit12:
14511         case DW_OP_lit13:
14512         case DW_OP_lit14:
14513         case DW_OP_lit15:
14514         case DW_OP_lit16:
14515         case DW_OP_lit17:
14516         case DW_OP_lit18:
14517         case DW_OP_lit19:
14518         case DW_OP_lit20:
14519         case DW_OP_lit21:
14520         case DW_OP_lit22:
14521         case DW_OP_lit23:
14522         case DW_OP_lit24:
14523         case DW_OP_lit25:
14524         case DW_OP_lit26:
14525         case DW_OP_lit27:
14526         case DW_OP_lit28:
14527         case DW_OP_lit29:
14528         case DW_OP_lit30:
14529         case DW_OP_lit31:
14530           stack[++stacki] = op - DW_OP_lit0;
14531           break;
14532
14533         case DW_OP_reg0:
14534         case DW_OP_reg1:
14535         case DW_OP_reg2:
14536         case DW_OP_reg3:
14537         case DW_OP_reg4:
14538         case DW_OP_reg5:
14539         case DW_OP_reg6:
14540         case DW_OP_reg7:
14541         case DW_OP_reg8:
14542         case DW_OP_reg9:
14543         case DW_OP_reg10:
14544         case DW_OP_reg11:
14545         case DW_OP_reg12:
14546         case DW_OP_reg13:
14547         case DW_OP_reg14:
14548         case DW_OP_reg15:
14549         case DW_OP_reg16:
14550         case DW_OP_reg17:
14551         case DW_OP_reg18:
14552         case DW_OP_reg19:
14553         case DW_OP_reg20:
14554         case DW_OP_reg21:
14555         case DW_OP_reg22:
14556         case DW_OP_reg23:
14557         case DW_OP_reg24:
14558         case DW_OP_reg25:
14559         case DW_OP_reg26:
14560         case DW_OP_reg27:
14561         case DW_OP_reg28:
14562         case DW_OP_reg29:
14563         case DW_OP_reg30:
14564         case DW_OP_reg31:
14565           stack[++stacki] = op - DW_OP_reg0;
14566           if (i < size)
14567             dwarf2_complex_location_expr_complaint ();
14568           break;
14569
14570         case DW_OP_regx:
14571           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14572           i += bytes_read;
14573           stack[++stacki] = unsnd;
14574           if (i < size)
14575             dwarf2_complex_location_expr_complaint ();
14576           break;
14577
14578         case DW_OP_addr:
14579           stack[++stacki] = read_address (objfile->obfd, &data[i],
14580                                           cu, &bytes_read);
14581           i += bytes_read;
14582           break;
14583
14584         case DW_OP_const1u:
14585           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14586           i += 1;
14587           break;
14588
14589         case DW_OP_const1s:
14590           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14591           i += 1;
14592           break;
14593
14594         case DW_OP_const2u:
14595           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14596           i += 2;
14597           break;
14598
14599         case DW_OP_const2s:
14600           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14601           i += 2;
14602           break;
14603
14604         case DW_OP_const4u:
14605           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14606           i += 4;
14607           break;
14608
14609         case DW_OP_const4s:
14610           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14611           i += 4;
14612           break;
14613
14614         case DW_OP_const8u:
14615           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14616           i += 8;
14617           break;
14618
14619         case DW_OP_constu:
14620           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14621                                                   &bytes_read);
14622           i += bytes_read;
14623           break;
14624
14625         case DW_OP_consts:
14626           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14627           i += bytes_read;
14628           break;
14629
14630         case DW_OP_dup:
14631           stack[stacki + 1] = stack[stacki];
14632           stacki++;
14633           break;
14634
14635         case DW_OP_plus:
14636           stack[stacki - 1] += stack[stacki];
14637           stacki--;
14638           break;
14639
14640         case DW_OP_plus_uconst:
14641           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14642                                                  &bytes_read);
14643           i += bytes_read;
14644           break;
14645
14646         case DW_OP_minus:
14647           stack[stacki - 1] -= stack[stacki];
14648           stacki--;
14649           break;
14650
14651         case DW_OP_deref:
14652           /* If we're not the last op, then we definitely can't encode
14653              this using GDB's address_class enum.  This is valid for partial
14654              global symbols, although the variable's address will be bogus
14655              in the psymtab.  */
14656           if (i < size)
14657             dwarf2_complex_location_expr_complaint ();
14658           break;
14659
14660         case DW_OP_GNU_push_tls_address:
14661           /* The top of the stack has the offset from the beginning
14662              of the thread control block at which the variable is located.  */
14663           /* Nothing should follow this operator, so the top of stack would
14664              be returned.  */
14665           /* This is valid for partial global symbols, but the variable's
14666              address will be bogus in the psymtab.  Make it always at least
14667              non-zero to not look as a variable garbage collected by linker
14668              which have DW_OP_addr 0.  */
14669           if (i < size)
14670             dwarf2_complex_location_expr_complaint ();
14671           stack[stacki]++;
14672           break;
14673
14674         case DW_OP_GNU_uninit:
14675           break;
14676
14677         default:
14678           {
14679             const char *name = dwarf_stack_op_name (op);
14680
14681             if (name)
14682               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14683                          name);
14684             else
14685               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14686                          op);
14687           }
14688
14689           return (stack[stacki]);
14690         }
14691
14692       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14693          outside of the allocated space.  Also enforce minimum>0.  */
14694       if (stacki >= ARRAY_SIZE (stack) - 1)
14695         {
14696           complaint (&symfile_complaints,
14697                      _("location description stack overflow"));
14698           return 0;
14699         }
14700
14701       if (stacki <= 0)
14702         {
14703           complaint (&symfile_complaints,
14704                      _("location description stack underflow"));
14705           return 0;
14706         }
14707     }
14708   return (stack[stacki]);
14709 }
14710
14711 /* memory allocation interface */
14712
14713 static struct dwarf_block *
14714 dwarf_alloc_block (struct dwarf2_cu *cu)
14715 {
14716   struct dwarf_block *blk;
14717
14718   blk = (struct dwarf_block *)
14719     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14720   return (blk);
14721 }
14722
14723 static struct abbrev_info *
14724 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14725 {
14726   struct abbrev_info *abbrev;
14727
14728   abbrev = (struct abbrev_info *)
14729     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14730   memset (abbrev, 0, sizeof (struct abbrev_info));
14731   return (abbrev);
14732 }
14733
14734 static struct die_info *
14735 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14736 {
14737   struct die_info *die;
14738   size_t size = sizeof (struct die_info);
14739
14740   if (num_attrs > 1)
14741     size += (num_attrs - 1) * sizeof (struct attribute);
14742
14743   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14744   memset (die, 0, sizeof (struct die_info));
14745   return (die);
14746 }
14747
14748 \f
14749 /* Macro support.  */
14750
14751 /* Return the full name of file number I in *LH's file name table.
14752    Use COMP_DIR as the name of the current directory of the
14753    compilation.  The result is allocated using xmalloc; the caller is
14754    responsible for freeing it.  */
14755 static char *
14756 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14757 {
14758   /* Is the file number a valid index into the line header's file name
14759      table?  Remember that file numbers start with one, not zero.  */
14760   if (1 <= file && file <= lh->num_file_names)
14761     {
14762       struct file_entry *fe = &lh->file_names[file - 1];
14763
14764       if (IS_ABSOLUTE_PATH (fe->name))
14765         return xstrdup (fe->name);
14766       else
14767         {
14768           const char *dir;
14769           int dir_len;
14770           char *full_name;
14771
14772           if (fe->dir_index)
14773             dir = lh->include_dirs[fe->dir_index - 1];
14774           else
14775             dir = comp_dir;
14776
14777           if (dir)
14778             {
14779               dir_len = strlen (dir);
14780               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14781               strcpy (full_name, dir);
14782               full_name[dir_len] = '/';
14783               strcpy (full_name + dir_len + 1, fe->name);
14784               return full_name;
14785             }
14786           else
14787             return xstrdup (fe->name);
14788         }
14789     }
14790   else
14791     {
14792       /* The compiler produced a bogus file number.  We can at least
14793          record the macro definitions made in the file, even if we
14794          won't be able to find the file by name.  */
14795       char fake_name[80];
14796
14797       sprintf (fake_name, "<bad macro file number %d>", file);
14798
14799       complaint (&symfile_complaints,
14800                  _("bad file number in macro information (%d)"),
14801                  file);
14802
14803       return xstrdup (fake_name);
14804     }
14805 }
14806
14807
14808 static struct macro_source_file *
14809 macro_start_file (int file, int line,
14810                   struct macro_source_file *current_file,
14811                   const char *comp_dir,
14812                   struct line_header *lh, struct objfile *objfile)
14813 {
14814   /* The full name of this source file.  */
14815   char *full_name = file_full_name (file, lh, comp_dir);
14816
14817   /* We don't create a macro table for this compilation unit
14818      at all until we actually get a filename.  */
14819   if (! pending_macros)
14820     pending_macros = new_macro_table (&objfile->objfile_obstack,
14821                                       objfile->macro_cache);
14822
14823   if (! current_file)
14824     /* If we have no current file, then this must be the start_file
14825        directive for the compilation unit's main source file.  */
14826     current_file = macro_set_main (pending_macros, full_name);
14827   else
14828     current_file = macro_include (current_file, line, full_name);
14829
14830   xfree (full_name);
14831
14832   return current_file;
14833 }
14834
14835
14836 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14837    followed by a null byte.  */
14838 static char *
14839 copy_string (const char *buf, int len)
14840 {
14841   char *s = xmalloc (len + 1);
14842
14843   memcpy (s, buf, len);
14844   s[len] = '\0';
14845   return s;
14846 }
14847
14848
14849 static const char *
14850 consume_improper_spaces (const char *p, const char *body)
14851 {
14852   if (*p == ' ')
14853     {
14854       complaint (&symfile_complaints,
14855                  _("macro definition contains spaces "
14856                    "in formal argument list:\n`%s'"),
14857                  body);
14858
14859       while (*p == ' ')
14860         p++;
14861     }
14862
14863   return p;
14864 }
14865
14866
14867 static void
14868 parse_macro_definition (struct macro_source_file *file, int line,
14869                         const char *body)
14870 {
14871   const char *p;
14872
14873   /* The body string takes one of two forms.  For object-like macro
14874      definitions, it should be:
14875
14876         <macro name> " " <definition>
14877
14878      For function-like macro definitions, it should be:
14879
14880         <macro name> "() " <definition>
14881      or
14882         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14883
14884      Spaces may appear only where explicitly indicated, and in the
14885      <definition>.
14886
14887      The Dwarf 2 spec says that an object-like macro's name is always
14888      followed by a space, but versions of GCC around March 2002 omit
14889      the space when the macro's definition is the empty string.
14890
14891      The Dwarf 2 spec says that there should be no spaces between the
14892      formal arguments in a function-like macro's formal argument list,
14893      but versions of GCC around March 2002 include spaces after the
14894      commas.  */
14895
14896
14897   /* Find the extent of the macro name.  The macro name is terminated
14898      by either a space or null character (for an object-like macro) or
14899      an opening paren (for a function-like macro).  */
14900   for (p = body; *p; p++)
14901     if (*p == ' ' || *p == '(')
14902       break;
14903
14904   if (*p == ' ' || *p == '\0')
14905     {
14906       /* It's an object-like macro.  */
14907       int name_len = p - body;
14908       char *name = copy_string (body, name_len);
14909       const char *replacement;
14910
14911       if (*p == ' ')
14912         replacement = body + name_len + 1;
14913       else
14914         {
14915           dwarf2_macro_malformed_definition_complaint (body);
14916           replacement = body + name_len;
14917         }
14918
14919       macro_define_object (file, line, name, replacement);
14920
14921       xfree (name);
14922     }
14923   else if (*p == '(')
14924     {
14925       /* It's a function-like macro.  */
14926       char *name = copy_string (body, p - body);
14927       int argc = 0;
14928       int argv_size = 1;
14929       char **argv = xmalloc (argv_size * sizeof (*argv));
14930
14931       p++;
14932
14933       p = consume_improper_spaces (p, body);
14934
14935       /* Parse the formal argument list.  */
14936       while (*p && *p != ')')
14937         {
14938           /* Find the extent of the current argument name.  */
14939           const char *arg_start = p;
14940
14941           while (*p && *p != ',' && *p != ')' && *p != ' ')
14942             p++;
14943
14944           if (! *p || p == arg_start)
14945             dwarf2_macro_malformed_definition_complaint (body);
14946           else
14947             {
14948               /* Make sure argv has room for the new argument.  */
14949               if (argc >= argv_size)
14950                 {
14951                   argv_size *= 2;
14952                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14953                 }
14954
14955               argv[argc++] = copy_string (arg_start, p - arg_start);
14956             }
14957
14958           p = consume_improper_spaces (p, body);
14959
14960           /* Consume the comma, if present.  */
14961           if (*p == ',')
14962             {
14963               p++;
14964
14965               p = consume_improper_spaces (p, body);
14966             }
14967         }
14968
14969       if (*p == ')')
14970         {
14971           p++;
14972
14973           if (*p == ' ')
14974             /* Perfectly formed definition, no complaints.  */
14975             macro_define_function (file, line, name,
14976                                    argc, (const char **) argv,
14977                                    p + 1);
14978           else if (*p == '\0')
14979             {
14980               /* Complain, but do define it.  */
14981               dwarf2_macro_malformed_definition_complaint (body);
14982               macro_define_function (file, line, name,
14983                                      argc, (const char **) argv,
14984                                      p);
14985             }
14986           else
14987             /* Just complain.  */
14988             dwarf2_macro_malformed_definition_complaint (body);
14989         }
14990       else
14991         /* Just complain.  */
14992         dwarf2_macro_malformed_definition_complaint (body);
14993
14994       xfree (name);
14995       {
14996         int i;
14997
14998         for (i = 0; i < argc; i++)
14999           xfree (argv[i]);
15000       }
15001       xfree (argv);
15002     }
15003   else
15004     dwarf2_macro_malformed_definition_complaint (body);
15005 }
15006
15007 /* Skip some bytes from BYTES according to the form given in FORM.
15008    Returns the new pointer.  */
15009
15010 static gdb_byte *
15011 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15012                  enum dwarf_form form,
15013                  unsigned int offset_size,
15014                  struct dwarf2_section_info *section)
15015 {
15016   unsigned int bytes_read;
15017
15018   switch (form)
15019     {
15020     case DW_FORM_data1:
15021     case DW_FORM_flag:
15022       ++bytes;
15023       break;
15024
15025     case DW_FORM_data2:
15026       bytes += 2;
15027       break;
15028
15029     case DW_FORM_data4:
15030       bytes += 4;
15031       break;
15032
15033     case DW_FORM_data8:
15034       bytes += 8;
15035       break;
15036
15037     case DW_FORM_string:
15038       read_direct_string (abfd, bytes, &bytes_read);
15039       bytes += bytes_read;
15040       break;
15041
15042     case DW_FORM_sec_offset:
15043     case DW_FORM_strp:
15044       bytes += offset_size;
15045       break;
15046
15047     case DW_FORM_block:
15048       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15049       bytes += bytes_read;
15050       break;
15051
15052     case DW_FORM_block1:
15053       bytes += 1 + read_1_byte (abfd, bytes);
15054       break;
15055     case DW_FORM_block2:
15056       bytes += 2 + read_2_bytes (abfd, bytes);
15057       break;
15058     case DW_FORM_block4:
15059       bytes += 4 + read_4_bytes (abfd, bytes);
15060       break;
15061
15062     case DW_FORM_sdata:
15063     case DW_FORM_udata:
15064       bytes = skip_leb128 (abfd, bytes);
15065       break;
15066
15067     default:
15068       {
15069       complain:
15070         complaint (&symfile_complaints,
15071                    _("invalid form 0x%x in `%s'"),
15072                    form,
15073                    section->asection->name);
15074         return NULL;
15075       }
15076     }
15077
15078   return bytes;
15079 }
15080
15081 /* A helper for dwarf_decode_macros that handles skipping an unknown
15082    opcode.  Returns an updated pointer to the macro data buffer; or,
15083    on error, issues a complaint and returns NULL.  */
15084
15085 static gdb_byte *
15086 skip_unknown_opcode (unsigned int opcode,
15087                      gdb_byte **opcode_definitions,
15088                      gdb_byte *mac_ptr,
15089                      bfd *abfd,
15090                      unsigned int offset_size,
15091                      struct dwarf2_section_info *section)
15092 {
15093   unsigned int bytes_read, i;
15094   unsigned long arg;
15095   gdb_byte *defn;
15096
15097   if (opcode_definitions[opcode] == NULL)
15098     {
15099       complaint (&symfile_complaints,
15100                  _("unrecognized DW_MACFINO opcode 0x%x"),
15101                  opcode);
15102       return NULL;
15103     }
15104
15105   defn = opcode_definitions[opcode];
15106   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15107   defn += bytes_read;
15108
15109   for (i = 0; i < arg; ++i)
15110     {
15111       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15112       if (mac_ptr == NULL)
15113         {
15114           /* skip_form_bytes already issued the complaint.  */
15115           return NULL;
15116         }
15117     }
15118
15119   return mac_ptr;
15120 }
15121
15122 /* A helper function which parses the header of a macro section.
15123    If the macro section is the extended (for now called "GNU") type,
15124    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15125    the header, or issues a complaint and returns NULL on error.  */
15126
15127 static gdb_byte *
15128 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15129                           bfd *abfd,
15130                           gdb_byte *mac_ptr,
15131                           unsigned int *offset_size,
15132                           int section_is_gnu)
15133 {
15134   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15135
15136   if (section_is_gnu)
15137     {
15138       unsigned int version, flags;
15139
15140       version = read_2_bytes (abfd, mac_ptr);
15141       if (version != 4)
15142         {
15143           complaint (&symfile_complaints,
15144                      _("unrecognized version `%d' in .debug_macro section"),
15145                      version);
15146           return NULL;
15147         }
15148       mac_ptr += 2;
15149
15150       flags = read_1_byte (abfd, mac_ptr);
15151       ++mac_ptr;
15152       *offset_size = (flags & 1) ? 8 : 4;
15153
15154       if ((flags & 2) != 0)
15155         /* We don't need the line table offset.  */
15156         mac_ptr += *offset_size;
15157
15158       /* Vendor opcode descriptions.  */
15159       if ((flags & 4) != 0)
15160         {
15161           unsigned int i, count;
15162
15163           count = read_1_byte (abfd, mac_ptr);
15164           ++mac_ptr;
15165           for (i = 0; i < count; ++i)
15166             {
15167               unsigned int opcode, bytes_read;
15168               unsigned long arg;
15169
15170               opcode = read_1_byte (abfd, mac_ptr);
15171               ++mac_ptr;
15172               opcode_definitions[opcode] = mac_ptr;
15173               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15174               mac_ptr += bytes_read;
15175               mac_ptr += arg;
15176             }
15177         }
15178     }
15179
15180   return mac_ptr;
15181 }
15182
15183 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15184    including DW_GNU_MACINFO_transparent_include.  */
15185
15186 static void
15187 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15188                           struct macro_source_file *current_file,
15189                           struct line_header *lh, char *comp_dir,
15190                           struct dwarf2_section_info *section,
15191                           int section_is_gnu,
15192                           unsigned int offset_size,
15193                           struct objfile *objfile)
15194 {
15195   enum dwarf_macro_record_type macinfo_type;
15196   int at_commandline;
15197   gdb_byte *opcode_definitions[256];
15198
15199   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15200                                       &offset_size, section_is_gnu);
15201   if (mac_ptr == NULL)
15202     {
15203       /* We already issued a complaint.  */
15204       return;
15205     }
15206
15207   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15208      GDB is still reading the definitions from command line.  First
15209      DW_MACINFO_start_file will need to be ignored as it was already executed
15210      to create CURRENT_FILE for the main source holding also the command line
15211      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15212      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15213
15214   at_commandline = 1;
15215
15216   do
15217     {
15218       /* Do we at least have room for a macinfo type byte?  */
15219       if (mac_ptr >= mac_end)
15220         {
15221           dwarf2_macros_too_long_complaint (section);
15222           break;
15223         }
15224
15225       macinfo_type = read_1_byte (abfd, mac_ptr);
15226       mac_ptr++;
15227
15228       /* Note that we rely on the fact that the corresponding GNU and
15229          DWARF constants are the same.  */
15230       switch (macinfo_type)
15231         {
15232           /* A zero macinfo type indicates the end of the macro
15233              information.  */
15234         case 0:
15235           break;
15236
15237         case DW_MACRO_GNU_define:
15238         case DW_MACRO_GNU_undef:
15239         case DW_MACRO_GNU_define_indirect:
15240         case DW_MACRO_GNU_undef_indirect:
15241           {
15242             unsigned int bytes_read;
15243             int line;
15244             char *body;
15245             int is_define;
15246
15247             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15248             mac_ptr += bytes_read;
15249
15250             if (macinfo_type == DW_MACRO_GNU_define
15251                 || macinfo_type == DW_MACRO_GNU_undef)
15252               {
15253                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15254                 mac_ptr += bytes_read;
15255               }
15256             else
15257               {
15258                 LONGEST str_offset;
15259
15260                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15261                 mac_ptr += offset_size;
15262
15263                 body = read_indirect_string_at_offset (abfd, str_offset);
15264               }
15265
15266             is_define = (macinfo_type == DW_MACRO_GNU_define
15267                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15268             if (! current_file)
15269               {
15270                 /* DWARF violation as no main source is present.  */
15271                 complaint (&symfile_complaints,
15272                            _("debug info with no main source gives macro %s "
15273                              "on line %d: %s"),
15274                            is_define ? _("definition") : _("undefinition"),
15275                            line, body);
15276                 break;
15277               }
15278             if ((line == 0 && !at_commandline)
15279                 || (line != 0 && at_commandline))
15280               complaint (&symfile_complaints,
15281                          _("debug info gives %s macro %s with %s line %d: %s"),
15282                          at_commandline ? _("command-line") : _("in-file"),
15283                          is_define ? _("definition") : _("undefinition"),
15284                          line == 0 ? _("zero") : _("non-zero"), line, body);
15285
15286             if (is_define)
15287               parse_macro_definition (current_file, line, body);
15288             else
15289               {
15290                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15291                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15292                 macro_undef (current_file, line, body);
15293               }
15294           }
15295           break;
15296
15297         case DW_MACRO_GNU_start_file:
15298           {
15299             unsigned int bytes_read;
15300             int line, file;
15301
15302             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15303             mac_ptr += bytes_read;
15304             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15305             mac_ptr += bytes_read;
15306
15307             if ((line == 0 && !at_commandline)
15308                 || (line != 0 && at_commandline))
15309               complaint (&symfile_complaints,
15310                          _("debug info gives source %d included "
15311                            "from %s at %s line %d"),
15312                          file, at_commandline ? _("command-line") : _("file"),
15313                          line == 0 ? _("zero") : _("non-zero"), line);
15314
15315             if (at_commandline)
15316               {
15317                 /* This DW_MACRO_GNU_start_file was executed in the
15318                    pass one.  */
15319                 at_commandline = 0;
15320               }
15321             else
15322               current_file = macro_start_file (file, line,
15323                                                current_file, comp_dir,
15324                                                lh, objfile);
15325           }
15326           break;
15327
15328         case DW_MACRO_GNU_end_file:
15329           if (! current_file)
15330             complaint (&symfile_complaints,
15331                        _("macro debug info has an unmatched "
15332                          "`close_file' directive"));
15333           else
15334             {
15335               current_file = current_file->included_by;
15336               if (! current_file)
15337                 {
15338                   enum dwarf_macro_record_type next_type;
15339
15340                   /* GCC circa March 2002 doesn't produce the zero
15341                      type byte marking the end of the compilation
15342                      unit.  Complain if it's not there, but exit no
15343                      matter what.  */
15344
15345                   /* Do we at least have room for a macinfo type byte?  */
15346                   if (mac_ptr >= mac_end)
15347                     {
15348                       dwarf2_macros_too_long_complaint (section);
15349                       return;
15350                     }
15351
15352                   /* We don't increment mac_ptr here, so this is just
15353                      a look-ahead.  */
15354                   next_type = read_1_byte (abfd, mac_ptr);
15355                   if (next_type != 0)
15356                     complaint (&symfile_complaints,
15357                                _("no terminating 0-type entry for "
15358                                  "macros in `.debug_macinfo' section"));
15359
15360                   return;
15361                 }
15362             }
15363           break;
15364
15365         case DW_MACRO_GNU_transparent_include:
15366           {
15367             LONGEST offset;
15368
15369             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15370             mac_ptr += offset_size;
15371
15372             dwarf_decode_macro_bytes (abfd,
15373                                       section->buffer + offset,
15374                                       mac_end, current_file,
15375                                       lh, comp_dir,
15376                                       section, section_is_gnu,
15377                                       offset_size, objfile);
15378           }
15379           break;
15380
15381         case DW_MACINFO_vendor_ext:
15382           if (!section_is_gnu)
15383             {
15384               unsigned int bytes_read;
15385               int constant;
15386
15387               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15388               mac_ptr += bytes_read;
15389               read_direct_string (abfd, mac_ptr, &bytes_read);
15390               mac_ptr += bytes_read;
15391
15392               /* We don't recognize any vendor extensions.  */
15393               break;
15394             }
15395           /* FALLTHROUGH */
15396
15397         default:
15398           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15399                                          mac_ptr, abfd, offset_size,
15400                                          section);
15401           if (mac_ptr == NULL)
15402             return;
15403           break;
15404         }
15405     } while (macinfo_type != 0);
15406 }
15407
15408 static void
15409 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15410                      char *comp_dir, bfd *abfd,
15411                      struct dwarf2_cu *cu,
15412                      struct dwarf2_section_info *section,
15413                      int section_is_gnu)
15414 {
15415   struct objfile *objfile = dwarf2_per_objfile->objfile;
15416   gdb_byte *mac_ptr, *mac_end;
15417   struct macro_source_file *current_file = 0;
15418   enum dwarf_macro_record_type macinfo_type;
15419   unsigned int offset_size = cu->header.offset_size;
15420   gdb_byte *opcode_definitions[256];
15421
15422   dwarf2_read_section (objfile, section);
15423   if (section->buffer == NULL)
15424     {
15425       complaint (&symfile_complaints, _("missing %s section"),
15426                  section->asection->name);
15427       return;
15428     }
15429
15430   /* First pass: Find the name of the base filename.
15431      This filename is needed in order to process all macros whose definition
15432      (or undefinition) comes from the command line.  These macros are defined
15433      before the first DW_MACINFO_start_file entry, and yet still need to be
15434      associated to the base file.
15435
15436      To determine the base file name, we scan the macro definitions until we
15437      reach the first DW_MACINFO_start_file entry.  We then initialize
15438      CURRENT_FILE accordingly so that any macro definition found before the
15439      first DW_MACINFO_start_file can still be associated to the base file.  */
15440
15441   mac_ptr = section->buffer + offset;
15442   mac_end = section->buffer + section->size;
15443
15444   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15445                                       &offset_size, section_is_gnu);
15446   if (mac_ptr == NULL)
15447     {
15448       /* We already issued a complaint.  */
15449       return;
15450     }
15451
15452   do
15453     {
15454       /* Do we at least have room for a macinfo type byte?  */
15455       if (mac_ptr >= mac_end)
15456         {
15457           /* Complaint is printed during the second pass as GDB will probably
15458              stop the first pass earlier upon finding
15459              DW_MACINFO_start_file.  */
15460           break;
15461         }
15462
15463       macinfo_type = read_1_byte (abfd, mac_ptr);
15464       mac_ptr++;
15465
15466       /* Note that we rely on the fact that the corresponding GNU and
15467          DWARF constants are the same.  */
15468       switch (macinfo_type)
15469         {
15470           /* A zero macinfo type indicates the end of the macro
15471              information.  */
15472         case 0:
15473           break;
15474
15475         case DW_MACRO_GNU_define:
15476         case DW_MACRO_GNU_undef:
15477           /* Only skip the data by MAC_PTR.  */
15478           {
15479             unsigned int bytes_read;
15480
15481             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15482             mac_ptr += bytes_read;
15483             read_direct_string (abfd, mac_ptr, &bytes_read);
15484             mac_ptr += bytes_read;
15485           }
15486           break;
15487
15488         case DW_MACRO_GNU_start_file:
15489           {
15490             unsigned int bytes_read;
15491             int line, file;
15492
15493             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15494             mac_ptr += bytes_read;
15495             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15496             mac_ptr += bytes_read;
15497
15498             current_file = macro_start_file (file, line, current_file,
15499                                              comp_dir, lh, objfile);
15500           }
15501           break;
15502
15503         case DW_MACRO_GNU_end_file:
15504           /* No data to skip by MAC_PTR.  */
15505           break;
15506
15507         case DW_MACRO_GNU_define_indirect:
15508         case DW_MACRO_GNU_undef_indirect:
15509           {
15510             unsigned int bytes_read;
15511
15512             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15513             mac_ptr += bytes_read;
15514             mac_ptr += offset_size;
15515           }
15516           break;
15517
15518         case DW_MACRO_GNU_transparent_include:
15519           /* Note that, according to the spec, a transparent include
15520              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15521              skip this opcode.  */
15522           mac_ptr += offset_size;
15523           break;
15524
15525         case DW_MACINFO_vendor_ext:
15526           /* Only skip the data by MAC_PTR.  */
15527           if (!section_is_gnu)
15528             {
15529               unsigned int bytes_read;
15530
15531               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15532               mac_ptr += bytes_read;
15533               read_direct_string (abfd, mac_ptr, &bytes_read);
15534               mac_ptr += bytes_read;
15535             }
15536           /* FALLTHROUGH */
15537
15538         default:
15539           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15540                                          mac_ptr, abfd, offset_size,
15541                                          section);
15542           if (mac_ptr == NULL)
15543             return;
15544           break;
15545         }
15546     } while (macinfo_type != 0 && current_file == NULL);
15547
15548   /* Second pass: Process all entries.
15549
15550      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15551      command-line macro definitions/undefinitions.  This flag is unset when we
15552      reach the first DW_MACINFO_start_file entry.  */
15553
15554   dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15555                             current_file, lh, comp_dir, section, section_is_gnu,
15556                             offset_size, objfile);
15557 }
15558
15559 /* Check if the attribute's form is a DW_FORM_block*
15560    if so return true else false.  */
15561 static int
15562 attr_form_is_block (struct attribute *attr)
15563 {
15564   return (attr == NULL ? 0 :
15565       attr->form == DW_FORM_block1
15566       || attr->form == DW_FORM_block2
15567       || attr->form == DW_FORM_block4
15568       || attr->form == DW_FORM_block
15569       || attr->form == DW_FORM_exprloc);
15570 }
15571
15572 /* Return non-zero if ATTR's value is a section offset --- classes
15573    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15574    You may use DW_UNSND (attr) to retrieve such offsets.
15575
15576    Section 7.5.4, "Attribute Encodings", explains that no attribute
15577    may have a value that belongs to more than one of these classes; it
15578    would be ambiguous if we did, because we use the same forms for all
15579    of them.  */
15580 static int
15581 attr_form_is_section_offset (struct attribute *attr)
15582 {
15583   return (attr->form == DW_FORM_data4
15584           || attr->form == DW_FORM_data8
15585           || attr->form == DW_FORM_sec_offset);
15586 }
15587
15588
15589 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15590    zero otherwise.  When this function returns true, you can apply
15591    dwarf2_get_attr_constant_value to it.
15592
15593    However, note that for some attributes you must check
15594    attr_form_is_section_offset before using this test.  DW_FORM_data4
15595    and DW_FORM_data8 are members of both the constant class, and of
15596    the classes that contain offsets into other debug sections
15597    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15598    that, if an attribute's can be either a constant or one of the
15599    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15600    taken as section offsets, not constants.  */
15601 static int
15602 attr_form_is_constant (struct attribute *attr)
15603 {
15604   switch (attr->form)
15605     {
15606     case DW_FORM_sdata:
15607     case DW_FORM_udata:
15608     case DW_FORM_data1:
15609     case DW_FORM_data2:
15610     case DW_FORM_data4:
15611     case DW_FORM_data8:
15612       return 1;
15613     default:
15614       return 0;
15615     }
15616 }
15617
15618 /* A helper function that fills in a dwarf2_loclist_baton.  */
15619
15620 static void
15621 fill_in_loclist_baton (struct dwarf2_cu *cu,
15622                        struct dwarf2_loclist_baton *baton,
15623                        struct attribute *attr)
15624 {
15625   dwarf2_read_section (dwarf2_per_objfile->objfile,
15626                        &dwarf2_per_objfile->loc);
15627
15628   baton->per_cu = cu->per_cu;
15629   gdb_assert (baton->per_cu);
15630   /* We don't know how long the location list is, but make sure we
15631      don't run off the edge of the section.  */
15632   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15633   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15634   baton->base_address = cu->base_address;
15635 }
15636
15637 static void
15638 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15639                              struct dwarf2_cu *cu)
15640 {
15641   struct objfile *objfile = dwarf2_per_objfile->objfile;
15642
15643   if (attr_form_is_section_offset (attr)
15644       /* ".debug_loc" may not exist at all, or the offset may be outside
15645          the section.  If so, fall through to the complaint in the
15646          other branch.  */
15647       && DW_UNSND (attr) < dwarf2_section_size (objfile,
15648                                                 &dwarf2_per_objfile->loc))
15649     {
15650       struct dwarf2_loclist_baton *baton;
15651
15652       baton = obstack_alloc (&objfile->objfile_obstack,
15653                              sizeof (struct dwarf2_loclist_baton));
15654
15655       fill_in_loclist_baton (cu, baton, attr);
15656
15657       if (cu->base_known == 0)
15658         complaint (&symfile_complaints,
15659                    _("Location list used without "
15660                      "specifying the CU base address."));
15661
15662       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15663       SYMBOL_LOCATION_BATON (sym) = baton;
15664     }
15665   else
15666     {
15667       struct dwarf2_locexpr_baton *baton;
15668
15669       baton = obstack_alloc (&objfile->objfile_obstack,
15670                              sizeof (struct dwarf2_locexpr_baton));
15671       baton->per_cu = cu->per_cu;
15672       gdb_assert (baton->per_cu);
15673
15674       if (attr_form_is_block (attr))
15675         {
15676           /* Note that we're just copying the block's data pointer
15677              here, not the actual data.  We're still pointing into the
15678              info_buffer for SYM's objfile; right now we never release
15679              that buffer, but when we do clean up properly this may
15680              need to change.  */
15681           baton->size = DW_BLOCK (attr)->size;
15682           baton->data = DW_BLOCK (attr)->data;
15683         }
15684       else
15685         {
15686           dwarf2_invalid_attrib_class_complaint ("location description",
15687                                                  SYMBOL_NATURAL_NAME (sym));
15688           baton->size = 0;
15689         }
15690
15691       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15692       SYMBOL_LOCATION_BATON (sym) = baton;
15693     }
15694 }
15695
15696 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15697    came from a separate debuginfo file, then the master objfile is
15698    returned.  */
15699
15700 struct objfile *
15701 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15702 {
15703   struct objfile *objfile = per_cu->objfile;
15704
15705   /* Return the master objfile, so that we can report and look up the
15706      correct file containing this variable.  */
15707   if (objfile->separate_debug_objfile_backlink)
15708     objfile = objfile->separate_debug_objfile_backlink;
15709
15710   return objfile;
15711 }
15712
15713 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15714    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15715    CU_HEADERP first.  */
15716
15717 static const struct comp_unit_head *
15718 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15719                        struct dwarf2_per_cu_data *per_cu)
15720 {
15721   struct objfile *objfile;
15722   struct dwarf2_per_objfile *per_objfile;
15723   gdb_byte *info_ptr;
15724
15725   if (per_cu->cu)
15726     return &per_cu->cu->header;
15727
15728   objfile = per_cu->objfile;
15729   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15730   info_ptr = per_objfile->info.buffer + per_cu->offset;
15731
15732   memset (cu_headerp, 0, sizeof (*cu_headerp));
15733   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15734
15735   return cu_headerp;
15736 }
15737
15738 /* Return the address size given in the compilation unit header for CU.  */
15739
15740 CORE_ADDR
15741 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15742 {
15743   struct comp_unit_head cu_header_local;
15744   const struct comp_unit_head *cu_headerp;
15745
15746   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15747
15748   return cu_headerp->addr_size;
15749 }
15750
15751 /* Return the offset size given in the compilation unit header for CU.  */
15752
15753 int
15754 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15755 {
15756   struct comp_unit_head cu_header_local;
15757   const struct comp_unit_head *cu_headerp;
15758
15759   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15760
15761   return cu_headerp->offset_size;
15762 }
15763
15764 /* See its dwarf2loc.h declaration.  */
15765
15766 int
15767 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15768 {
15769   struct comp_unit_head cu_header_local;
15770   const struct comp_unit_head *cu_headerp;
15771
15772   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15773
15774   if (cu_headerp->version == 2)
15775     return cu_headerp->addr_size;
15776   else
15777     return cu_headerp->offset_size;
15778 }
15779
15780 /* Return the text offset of the CU.  The returned offset comes from
15781    this CU's objfile.  If this objfile came from a separate debuginfo
15782    file, then the offset may be different from the corresponding
15783    offset in the parent objfile.  */
15784
15785 CORE_ADDR
15786 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15787 {
15788   struct objfile *objfile = per_cu->objfile;
15789
15790   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15791 }
15792
15793 /* Locate the .debug_info compilation unit from CU's objfile which contains
15794    the DIE at OFFSET.  Raises an error on failure.  */
15795
15796 static struct dwarf2_per_cu_data *
15797 dwarf2_find_containing_comp_unit (unsigned int offset,
15798                                   struct objfile *objfile)
15799 {
15800   struct dwarf2_per_cu_data *this_cu;
15801   int low, high;
15802
15803   low = 0;
15804   high = dwarf2_per_objfile->n_comp_units - 1;
15805   while (high > low)
15806     {
15807       int mid = low + (high - low) / 2;
15808
15809       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15810         high = mid;
15811       else
15812         low = mid + 1;
15813     }
15814   gdb_assert (low == high);
15815   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15816     {
15817       if (low == 0)
15818         error (_("Dwarf Error: could not find partial DIE containing "
15819                "offset 0x%lx [in module %s]"),
15820                (long) offset, bfd_get_filename (objfile->obfd));
15821
15822       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15823       return dwarf2_per_objfile->all_comp_units[low-1];
15824     }
15825   else
15826     {
15827       this_cu = dwarf2_per_objfile->all_comp_units[low];
15828       if (low == dwarf2_per_objfile->n_comp_units - 1
15829           && offset >= this_cu->offset + this_cu->length)
15830         error (_("invalid dwarf2 offset %u"), offset);
15831       gdb_assert (offset < this_cu->offset + this_cu->length);
15832       return this_cu;
15833     }
15834 }
15835
15836 /* Locate the compilation unit from OBJFILE which is located at exactly
15837    OFFSET.  Raises an error on failure.  */
15838
15839 static struct dwarf2_per_cu_data *
15840 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15841 {
15842   struct dwarf2_per_cu_data *this_cu;
15843
15844   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15845   if (this_cu->offset != offset)
15846     error (_("no compilation unit with offset %u."), offset);
15847   return this_cu;
15848 }
15849
15850 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
15851
15852 static void
15853 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15854 {
15855   memset (cu, 0, sizeof (*cu));
15856   per_cu->cu = cu;
15857   cu->per_cu = per_cu;
15858   cu->objfile = per_cu->objfile;
15859   obstack_init (&cu->comp_unit_obstack);
15860 }
15861
15862 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15863
15864 static void
15865 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15866 {
15867   struct attribute *attr;
15868
15869   /* Set the language we're debugging.  */
15870   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15871   if (attr)
15872     set_cu_language (DW_UNSND (attr), cu);
15873   else
15874     {
15875       cu->language = language_minimal;
15876       cu->language_defn = language_def (cu->language);
15877     }
15878 }
15879
15880 /* Release one cached compilation unit, CU.  We unlink it from the tree
15881    of compilation units, but we don't remove it from the read_in_chain;
15882    the caller is responsible for that.
15883    NOTE: DATA is a void * because this function is also used as a
15884    cleanup routine.  */
15885
15886 static void
15887 free_heap_comp_unit (void *data)
15888 {
15889   struct dwarf2_cu *cu = data;
15890
15891   gdb_assert (cu->per_cu != NULL);
15892   cu->per_cu->cu = NULL;
15893   cu->per_cu = NULL;
15894
15895   obstack_free (&cu->comp_unit_obstack, NULL);
15896
15897   xfree (cu);
15898 }
15899
15900 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15901    when we're finished with it.  We can't free the pointer itself, but be
15902    sure to unlink it from the cache.  Also release any associated storage
15903    and perform cache maintenance.
15904
15905    Only used during partial symbol parsing.  */
15906
15907 static void
15908 free_stack_comp_unit (void *data)
15909 {
15910   struct dwarf2_cu *cu = data;
15911
15912   gdb_assert (cu->per_cu != NULL);
15913   cu->per_cu->cu = NULL;
15914   cu->per_cu = NULL;
15915
15916   obstack_free (&cu->comp_unit_obstack, NULL);
15917   cu->partial_dies = NULL;
15918
15919   /* The previous code only did this if per_cu != NULL.
15920      But that would always succeed, so now we just unconditionally do
15921      the aging.  This seems like the wrong place to do such aging,
15922      but cleaning that up is left for later.  */
15923   age_cached_comp_units ();
15924 }
15925
15926 /* Free all cached compilation units.  */
15927
15928 static void
15929 free_cached_comp_units (void *data)
15930 {
15931   struct dwarf2_per_cu_data *per_cu, **last_chain;
15932
15933   per_cu = dwarf2_per_objfile->read_in_chain;
15934   last_chain = &dwarf2_per_objfile->read_in_chain;
15935   while (per_cu != NULL)
15936     {
15937       struct dwarf2_per_cu_data *next_cu;
15938
15939       next_cu = per_cu->cu->read_in_chain;
15940
15941       free_heap_comp_unit (per_cu->cu);
15942       *last_chain = next_cu;
15943
15944       per_cu = next_cu;
15945     }
15946 }
15947
15948 /* Increase the age counter on each cached compilation unit, and free
15949    any that are too old.  */
15950
15951 static void
15952 age_cached_comp_units (void)
15953 {
15954   struct dwarf2_per_cu_data *per_cu, **last_chain;
15955
15956   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15957   per_cu = dwarf2_per_objfile->read_in_chain;
15958   while (per_cu != NULL)
15959     {
15960       per_cu->cu->last_used ++;
15961       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15962         dwarf2_mark (per_cu->cu);
15963       per_cu = per_cu->cu->read_in_chain;
15964     }
15965
15966   per_cu = dwarf2_per_objfile->read_in_chain;
15967   last_chain = &dwarf2_per_objfile->read_in_chain;
15968   while (per_cu != NULL)
15969     {
15970       struct dwarf2_per_cu_data *next_cu;
15971
15972       next_cu = per_cu->cu->read_in_chain;
15973
15974       if (!per_cu->cu->mark)
15975         {
15976           free_heap_comp_unit (per_cu->cu);
15977           *last_chain = next_cu;
15978         }
15979       else
15980         last_chain = &per_cu->cu->read_in_chain;
15981
15982       per_cu = next_cu;
15983     }
15984 }
15985
15986 /* Remove a single compilation unit from the cache.  */
15987
15988 static void
15989 free_one_cached_comp_unit (void *target_cu)
15990 {
15991   struct dwarf2_per_cu_data *per_cu, **last_chain;
15992
15993   per_cu = dwarf2_per_objfile->read_in_chain;
15994   last_chain = &dwarf2_per_objfile->read_in_chain;
15995   while (per_cu != NULL)
15996     {
15997       struct dwarf2_per_cu_data *next_cu;
15998
15999       next_cu = per_cu->cu->read_in_chain;
16000
16001       if (per_cu->cu == target_cu)
16002         {
16003           free_heap_comp_unit (per_cu->cu);
16004           *last_chain = next_cu;
16005           break;
16006         }
16007       else
16008         last_chain = &per_cu->cu->read_in_chain;
16009
16010       per_cu = next_cu;
16011     }
16012 }
16013
16014 /* Release all extra memory associated with OBJFILE.  */
16015
16016 void
16017 dwarf2_free_objfile (struct objfile *objfile)
16018 {
16019   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16020
16021   if (dwarf2_per_objfile == NULL)
16022     return;
16023
16024   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
16025   free_cached_comp_units (NULL);
16026
16027   if (dwarf2_per_objfile->quick_file_names_table)
16028     htab_delete (dwarf2_per_objfile->quick_file_names_table);
16029
16030   /* Everything else should be on the objfile obstack.  */
16031 }
16032
16033 /* A pair of DIE offset and GDB type pointer.  We store these
16034    in a hash table separate from the DIEs, and preserve them
16035    when the DIEs are flushed out of cache.  */
16036
16037 struct dwarf2_offset_and_type
16038 {
16039   unsigned int offset;
16040   struct type *type;
16041 };
16042
16043 /* Hash function for a dwarf2_offset_and_type.  */
16044
16045 static hashval_t
16046 offset_and_type_hash (const void *item)
16047 {
16048   const struct dwarf2_offset_and_type *ofs = item;
16049
16050   return ofs->offset;
16051 }
16052
16053 /* Equality function for a dwarf2_offset_and_type.  */
16054
16055 static int
16056 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16057 {
16058   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16059   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16060
16061   return ofs_lhs->offset == ofs_rhs->offset;
16062 }
16063
16064 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16065    table if necessary.  For convenience, return TYPE.
16066
16067    The DIEs reading must have careful ordering to:
16068     * Not cause infite loops trying to read in DIEs as a prerequisite for
16069       reading current DIE.
16070     * Not trying to dereference contents of still incompletely read in types
16071       while reading in other DIEs.
16072     * Enable referencing still incompletely read in types just by a pointer to
16073       the type without accessing its fields.
16074
16075    Therefore caller should follow these rules:
16076      * Try to fetch any prerequisite types we may need to build this DIE type
16077        before building the type and calling set_die_type.
16078      * After building type call set_die_type for current DIE as soon as
16079        possible before fetching more types to complete the current type.
16080      * Make the type as complete as possible before fetching more types.  */
16081
16082 static struct type *
16083 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16084 {
16085   struct dwarf2_offset_and_type **slot, ofs;
16086   struct objfile *objfile = cu->objfile;
16087   htab_t *type_hash_ptr;
16088
16089   /* For Ada types, make sure that the gnat-specific data is always
16090      initialized (if not already set).  There are a few types where
16091      we should not be doing so, because the type-specific area is
16092      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16093      where the type-specific area is used to store the floatformat).
16094      But this is not a problem, because the gnat-specific information
16095      is actually not needed for these types.  */
16096   if (need_gnat_info (cu)
16097       && TYPE_CODE (type) != TYPE_CODE_FUNC
16098       && TYPE_CODE (type) != TYPE_CODE_FLT
16099       && !HAVE_GNAT_AUX_INFO (type))
16100     INIT_GNAT_SPECIFIC (type);
16101
16102   if (cu->per_cu->debug_types_section)
16103     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16104   else
16105     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16106
16107   if (*type_hash_ptr == NULL)
16108     {
16109       *type_hash_ptr
16110         = htab_create_alloc_ex (127,
16111                                 offset_and_type_hash,
16112                                 offset_and_type_eq,
16113                                 NULL,
16114                                 &objfile->objfile_obstack,
16115                                 hashtab_obstack_allocate,
16116                                 dummy_obstack_deallocate);
16117     }
16118
16119   ofs.offset = die->offset;
16120   ofs.type = type;
16121   slot = (struct dwarf2_offset_and_type **)
16122     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16123   if (*slot)
16124     complaint (&symfile_complaints,
16125                _("A problem internal to GDB: DIE 0x%x has type already set"),
16126                die->offset);
16127   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16128   **slot = ofs;
16129   return type;
16130 }
16131
16132 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16133    table, or return NULL if the die does not have a saved type.  */
16134
16135 static struct type *
16136 get_die_type_at_offset (unsigned int offset,
16137                         struct dwarf2_per_cu_data *per_cu)
16138 {
16139   struct dwarf2_offset_and_type *slot, ofs;
16140   htab_t type_hash;
16141
16142   if (per_cu->debug_types_section)
16143     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16144   else
16145     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16146   if (type_hash == NULL)
16147     return NULL;
16148
16149   ofs.offset = offset;
16150   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16151   if (slot)
16152     return slot->type;
16153   else
16154     return NULL;
16155 }
16156
16157 /* Look up the type for DIE in the appropriate type_hash table,
16158    or return NULL if DIE does not have a saved type.  */
16159
16160 static struct type *
16161 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16162 {
16163   return get_die_type_at_offset (die->offset, cu->per_cu);
16164 }
16165
16166 /* Add a dependence relationship from CU to REF_PER_CU.  */
16167
16168 static void
16169 dwarf2_add_dependence (struct dwarf2_cu *cu,
16170                        struct dwarf2_per_cu_data *ref_per_cu)
16171 {
16172   void **slot;
16173
16174   if (cu->dependencies == NULL)
16175     cu->dependencies
16176       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16177                               NULL, &cu->comp_unit_obstack,
16178                               hashtab_obstack_allocate,
16179                               dummy_obstack_deallocate);
16180
16181   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16182   if (*slot == NULL)
16183     *slot = ref_per_cu;
16184 }
16185
16186 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16187    Set the mark field in every compilation unit in the
16188    cache that we must keep because we are keeping CU.  */
16189
16190 static int
16191 dwarf2_mark_helper (void **slot, void *data)
16192 {
16193   struct dwarf2_per_cu_data *per_cu;
16194
16195   per_cu = (struct dwarf2_per_cu_data *) *slot;
16196
16197   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16198      reading of the chain.  As such dependencies remain valid it is not much
16199      useful to track and undo them during QUIT cleanups.  */
16200   if (per_cu->cu == NULL)
16201     return 1;
16202
16203   if (per_cu->cu->mark)
16204     return 1;
16205   per_cu->cu->mark = 1;
16206
16207   if (per_cu->cu->dependencies != NULL)
16208     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16209
16210   return 1;
16211 }
16212
16213 /* Set the mark field in CU and in every other compilation unit in the
16214    cache that we must keep because we are keeping CU.  */
16215
16216 static void
16217 dwarf2_mark (struct dwarf2_cu *cu)
16218 {
16219   if (cu->mark)
16220     return;
16221   cu->mark = 1;
16222   if (cu->dependencies != NULL)
16223     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16224 }
16225
16226 static void
16227 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16228 {
16229   while (per_cu)
16230     {
16231       per_cu->cu->mark = 0;
16232       per_cu = per_cu->cu->read_in_chain;
16233     }
16234 }
16235
16236 /* Trivial hash function for partial_die_info: the hash value of a DIE
16237    is its offset in .debug_info for this objfile.  */
16238
16239 static hashval_t
16240 partial_die_hash (const void *item)
16241 {
16242   const struct partial_die_info *part_die = item;
16243
16244   return part_die->offset;
16245 }
16246
16247 /* Trivial comparison function for partial_die_info structures: two DIEs
16248    are equal if they have the same offset.  */
16249
16250 static int
16251 partial_die_eq (const void *item_lhs, const void *item_rhs)
16252 {
16253   const struct partial_die_info *part_die_lhs = item_lhs;
16254   const struct partial_die_info *part_die_rhs = item_rhs;
16255
16256   return part_die_lhs->offset == part_die_rhs->offset;
16257 }
16258
16259 static struct cmd_list_element *set_dwarf2_cmdlist;
16260 static struct cmd_list_element *show_dwarf2_cmdlist;
16261
16262 static void
16263 set_dwarf2_cmd (char *args, int from_tty)
16264 {
16265   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16266 }
16267
16268 static void
16269 show_dwarf2_cmd (char *args, int from_tty)
16270 {
16271   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16272 }
16273
16274 /* If section described by INFO was mmapped, munmap it now.  */
16275
16276 static void
16277 munmap_section_buffer (struct dwarf2_section_info *info)
16278 {
16279   if (info->map_addr != NULL)
16280     {
16281 #ifdef HAVE_MMAP
16282       int res;
16283
16284       res = munmap (info->map_addr, info->map_len);
16285       gdb_assert (res == 0);
16286 #else
16287       /* Without HAVE_MMAP, we should never be here to begin with.  */
16288       gdb_assert_not_reached ("no mmap support");
16289 #endif
16290     }
16291 }
16292
16293 /* munmap debug sections for OBJFILE, if necessary.  */
16294
16295 static void
16296 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16297 {
16298   struct dwarf2_per_objfile *data = d;
16299   int ix;
16300   struct dwarf2_section_info *section;
16301
16302   /* This is sorted according to the order they're defined in to make it easier
16303      to keep in sync.  */
16304   munmap_section_buffer (&data->info);
16305   munmap_section_buffer (&data->abbrev);
16306   munmap_section_buffer (&data->line);
16307   munmap_section_buffer (&data->loc);
16308   munmap_section_buffer (&data->macinfo);
16309   munmap_section_buffer (&data->macro);
16310   munmap_section_buffer (&data->str);
16311   munmap_section_buffer (&data->ranges);
16312   munmap_section_buffer (&data->frame);
16313   munmap_section_buffer (&data->eh_frame);
16314   munmap_section_buffer (&data->gdb_index);
16315
16316   for (ix = 0;
16317        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16318        ++ix)
16319     munmap_section_buffer (section);
16320
16321   VEC_free (dwarf2_section_info_def, data->types);
16322 }
16323
16324 \f
16325 /* The "save gdb-index" command.  */
16326
16327 /* The contents of the hash table we create when building the string
16328    table.  */
16329 struct strtab_entry
16330 {
16331   offset_type offset;
16332   const char *str;
16333 };
16334
16335 /* Hash function for a strtab_entry.
16336
16337    Function is used only during write_hash_table so no index format backward
16338    compatibility is needed.  */
16339
16340 static hashval_t
16341 hash_strtab_entry (const void *e)
16342 {
16343   const struct strtab_entry *entry = e;
16344   return mapped_index_string_hash (INT_MAX, entry->str);
16345 }
16346
16347 /* Equality function for a strtab_entry.  */
16348
16349 static int
16350 eq_strtab_entry (const void *a, const void *b)
16351 {
16352   const struct strtab_entry *ea = a;
16353   const struct strtab_entry *eb = b;
16354   return !strcmp (ea->str, eb->str);
16355 }
16356
16357 /* Create a strtab_entry hash table.  */
16358
16359 static htab_t
16360 create_strtab (void)
16361 {
16362   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16363                             xfree, xcalloc, xfree);
16364 }
16365
16366 /* Add a string to the constant pool.  Return the string's offset in
16367    host order.  */
16368
16369 static offset_type
16370 add_string (htab_t table, struct obstack *cpool, const char *str)
16371 {
16372   void **slot;
16373   struct strtab_entry entry;
16374   struct strtab_entry *result;
16375
16376   entry.str = str;
16377   slot = htab_find_slot (table, &entry, INSERT);
16378   if (*slot)
16379     result = *slot;
16380   else
16381     {
16382       result = XNEW (struct strtab_entry);
16383       result->offset = obstack_object_size (cpool);
16384       result->str = str;
16385       obstack_grow_str0 (cpool, str);
16386       *slot = result;
16387     }
16388   return result->offset;
16389 }
16390
16391 /* An entry in the symbol table.  */
16392 struct symtab_index_entry
16393 {
16394   /* The name of the symbol.  */
16395   const char *name;
16396   /* The offset of the name in the constant pool.  */
16397   offset_type index_offset;
16398   /* A sorted vector of the indices of all the CUs that hold an object
16399      of this name.  */
16400   VEC (offset_type) *cu_indices;
16401 };
16402
16403 /* The symbol table.  This is a power-of-2-sized hash table.  */
16404 struct mapped_symtab
16405 {
16406   offset_type n_elements;
16407   offset_type size;
16408   struct symtab_index_entry **data;
16409 };
16410
16411 /* Hash function for a symtab_index_entry.  */
16412
16413 static hashval_t
16414 hash_symtab_entry (const void *e)
16415 {
16416   const struct symtab_index_entry *entry = e;
16417   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16418                          sizeof (offset_type) * VEC_length (offset_type,
16419                                                             entry->cu_indices),
16420                          0);
16421 }
16422
16423 /* Equality function for a symtab_index_entry.  */
16424
16425 static int
16426 eq_symtab_entry (const void *a, const void *b)
16427 {
16428   const struct symtab_index_entry *ea = a;
16429   const struct symtab_index_entry *eb = b;
16430   int len = VEC_length (offset_type, ea->cu_indices);
16431   if (len != VEC_length (offset_type, eb->cu_indices))
16432     return 0;
16433   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16434                   VEC_address (offset_type, eb->cu_indices),
16435                   sizeof (offset_type) * len);
16436 }
16437
16438 /* Destroy a symtab_index_entry.  */
16439
16440 static void
16441 delete_symtab_entry (void *p)
16442 {
16443   struct symtab_index_entry *entry = p;
16444   VEC_free (offset_type, entry->cu_indices);
16445   xfree (entry);
16446 }
16447
16448 /* Create a hash table holding symtab_index_entry objects.  */
16449
16450 static htab_t
16451 create_symbol_hash_table (void)
16452 {
16453   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16454                             delete_symtab_entry, xcalloc, xfree);
16455 }
16456
16457 /* Create a new mapped symtab object.  */
16458
16459 static struct mapped_symtab *
16460 create_mapped_symtab (void)
16461 {
16462   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16463   symtab->n_elements = 0;
16464   symtab->size = 1024;
16465   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16466   return symtab;
16467 }
16468
16469 /* Destroy a mapped_symtab.  */
16470
16471 static void
16472 cleanup_mapped_symtab (void *p)
16473 {
16474   struct mapped_symtab *symtab = p;
16475   /* The contents of the array are freed when the other hash table is
16476      destroyed.  */
16477   xfree (symtab->data);
16478   xfree (symtab);
16479 }
16480
16481 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16482    the slot.
16483    
16484    Function is used only during write_hash_table so no index format backward
16485    compatibility is needed.  */
16486
16487 static struct symtab_index_entry **
16488 find_slot (struct mapped_symtab *symtab, const char *name)
16489 {
16490   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16491
16492   index = hash & (symtab->size - 1);
16493   step = ((hash * 17) & (symtab->size - 1)) | 1;
16494
16495   for (;;)
16496     {
16497       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16498         return &symtab->data[index];
16499       index = (index + step) & (symtab->size - 1);
16500     }
16501 }
16502
16503 /* Expand SYMTAB's hash table.  */
16504
16505 static void
16506 hash_expand (struct mapped_symtab *symtab)
16507 {
16508   offset_type old_size = symtab->size;
16509   offset_type i;
16510   struct symtab_index_entry **old_entries = symtab->data;
16511
16512   symtab->size *= 2;
16513   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16514
16515   for (i = 0; i < old_size; ++i)
16516     {
16517       if (old_entries[i])
16518         {
16519           struct symtab_index_entry **slot = find_slot (symtab,
16520                                                         old_entries[i]->name);
16521           *slot = old_entries[i];
16522         }
16523     }
16524
16525   xfree (old_entries);
16526 }
16527
16528 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16529    is the index of the CU in which the symbol appears.  */
16530
16531 static void
16532 add_index_entry (struct mapped_symtab *symtab, const char *name,
16533                  offset_type cu_index)
16534 {
16535   struct symtab_index_entry **slot;
16536
16537   ++symtab->n_elements;
16538   if (4 * symtab->n_elements / 3 >= symtab->size)
16539     hash_expand (symtab);
16540
16541   slot = find_slot (symtab, name);
16542   if (!*slot)
16543     {
16544       *slot = XNEW (struct symtab_index_entry);
16545       (*slot)->name = name;
16546       (*slot)->cu_indices = NULL;
16547     }
16548   /* Don't push an index twice.  Due to how we add entries we only
16549      have to check the last one.  */ 
16550   if (VEC_empty (offset_type, (*slot)->cu_indices)
16551       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16552     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16553 }
16554
16555 /* Add a vector of indices to the constant pool.  */
16556
16557 static offset_type
16558 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16559                       struct symtab_index_entry *entry)
16560 {
16561   void **slot;
16562
16563   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16564   if (!*slot)
16565     {
16566       offset_type len = VEC_length (offset_type, entry->cu_indices);
16567       offset_type val = MAYBE_SWAP (len);
16568       offset_type iter;
16569       int i;
16570
16571       *slot = entry;
16572       entry->index_offset = obstack_object_size (cpool);
16573
16574       obstack_grow (cpool, &val, sizeof (val));
16575       for (i = 0;
16576            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16577            ++i)
16578         {
16579           val = MAYBE_SWAP (iter);
16580           obstack_grow (cpool, &val, sizeof (val));
16581         }
16582     }
16583   else
16584     {
16585       struct symtab_index_entry *old_entry = *slot;
16586       entry->index_offset = old_entry->index_offset;
16587       entry = old_entry;
16588     }
16589   return entry->index_offset;
16590 }
16591
16592 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16593    constant pool entries going into the obstack CPOOL.  */
16594
16595 static void
16596 write_hash_table (struct mapped_symtab *symtab,
16597                   struct obstack *output, struct obstack *cpool)
16598 {
16599   offset_type i;
16600   htab_t symbol_hash_table;
16601   htab_t str_table;
16602
16603   symbol_hash_table = create_symbol_hash_table ();
16604   str_table = create_strtab ();
16605
16606   /* We add all the index vectors to the constant pool first, to
16607      ensure alignment is ok.  */
16608   for (i = 0; i < symtab->size; ++i)
16609     {
16610       if (symtab->data[i])
16611         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16612     }
16613
16614   /* Now write out the hash table.  */
16615   for (i = 0; i < symtab->size; ++i)
16616     {
16617       offset_type str_off, vec_off;
16618
16619       if (symtab->data[i])
16620         {
16621           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16622           vec_off = symtab->data[i]->index_offset;
16623         }
16624       else
16625         {
16626           /* While 0 is a valid constant pool index, it is not valid
16627              to have 0 for both offsets.  */
16628           str_off = 0;
16629           vec_off = 0;
16630         }
16631
16632       str_off = MAYBE_SWAP (str_off);
16633       vec_off = MAYBE_SWAP (vec_off);
16634
16635       obstack_grow (output, &str_off, sizeof (str_off));
16636       obstack_grow (output, &vec_off, sizeof (vec_off));
16637     }
16638
16639   htab_delete (str_table);
16640   htab_delete (symbol_hash_table);
16641 }
16642
16643 /* Struct to map psymtab to CU index in the index file.  */
16644 struct psymtab_cu_index_map
16645 {
16646   struct partial_symtab *psymtab;
16647   unsigned int cu_index;
16648 };
16649
16650 static hashval_t
16651 hash_psymtab_cu_index (const void *item)
16652 {
16653   const struct psymtab_cu_index_map *map = item;
16654
16655   return htab_hash_pointer (map->psymtab);
16656 }
16657
16658 static int
16659 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16660 {
16661   const struct psymtab_cu_index_map *lhs = item_lhs;
16662   const struct psymtab_cu_index_map *rhs = item_rhs;
16663
16664   return lhs->psymtab == rhs->psymtab;
16665 }
16666
16667 /* Helper struct for building the address table.  */
16668 struct addrmap_index_data
16669 {
16670   struct objfile *objfile;
16671   struct obstack *addr_obstack;
16672   htab_t cu_index_htab;
16673
16674   /* Non-zero if the previous_* fields are valid.
16675      We can't write an entry until we see the next entry (since it is only then
16676      that we know the end of the entry).  */
16677   int previous_valid;
16678   /* Index of the CU in the table of all CUs in the index file.  */
16679   unsigned int previous_cu_index;
16680   /* Start address of the CU.  */
16681   CORE_ADDR previous_cu_start;
16682 };
16683
16684 /* Write an address entry to OBSTACK.  */
16685
16686 static void
16687 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16688                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16689 {
16690   offset_type cu_index_to_write;
16691   char addr[8];
16692   CORE_ADDR baseaddr;
16693
16694   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16695
16696   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16697   obstack_grow (obstack, addr, 8);
16698   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16699   obstack_grow (obstack, addr, 8);
16700   cu_index_to_write = MAYBE_SWAP (cu_index);
16701   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16702 }
16703
16704 /* Worker function for traversing an addrmap to build the address table.  */
16705
16706 static int
16707 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16708 {
16709   struct addrmap_index_data *data = datap;
16710   struct partial_symtab *pst = obj;
16711   offset_type cu_index;
16712   void **slot;
16713
16714   if (data->previous_valid)
16715     add_address_entry (data->objfile, data->addr_obstack,
16716                        data->previous_cu_start, start_addr,
16717                        data->previous_cu_index);
16718
16719   data->previous_cu_start = start_addr;
16720   if (pst != NULL)
16721     {
16722       struct psymtab_cu_index_map find_map, *map;
16723       find_map.psymtab = pst;
16724       map = htab_find (data->cu_index_htab, &find_map);
16725       gdb_assert (map != NULL);
16726       data->previous_cu_index = map->cu_index;
16727       data->previous_valid = 1;
16728     }
16729   else
16730       data->previous_valid = 0;
16731
16732   return 0;
16733 }
16734
16735 /* Write OBJFILE's address map to OBSTACK.
16736    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16737    in the index file.  */
16738
16739 static void
16740 write_address_map (struct objfile *objfile, struct obstack *obstack,
16741                    htab_t cu_index_htab)
16742 {
16743   struct addrmap_index_data addrmap_index_data;
16744
16745   /* When writing the address table, we have to cope with the fact that
16746      the addrmap iterator only provides the start of a region; we have to
16747      wait until the next invocation to get the start of the next region.  */
16748
16749   addrmap_index_data.objfile = objfile;
16750   addrmap_index_data.addr_obstack = obstack;
16751   addrmap_index_data.cu_index_htab = cu_index_htab;
16752   addrmap_index_data.previous_valid = 0;
16753
16754   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16755                    &addrmap_index_data);
16756
16757   /* It's highly unlikely the last entry (end address = 0xff...ff)
16758      is valid, but we should still handle it.
16759      The end address is recorded as the start of the next region, but that
16760      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16761      anyway.  */
16762   if (addrmap_index_data.previous_valid)
16763     add_address_entry (objfile, obstack,
16764                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16765                        addrmap_index_data.previous_cu_index);
16766 }
16767
16768 /* Add a list of partial symbols to SYMTAB.  */
16769
16770 static void
16771 write_psymbols (struct mapped_symtab *symtab,
16772                 htab_t psyms_seen,
16773                 struct partial_symbol **psymp,
16774                 int count,
16775                 offset_type cu_index,
16776                 int is_static)
16777 {
16778   for (; count-- > 0; ++psymp)
16779     {
16780       void **slot, *lookup;
16781
16782       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16783         error (_("Ada is not currently supported by the index"));
16784
16785       /* We only want to add a given psymbol once.  However, we also
16786          want to account for whether it is global or static.  So, we
16787          may add it twice, using slightly different values.  */
16788       if (is_static)
16789         {
16790           uintptr_t val = 1 | (uintptr_t) *psymp;
16791
16792           lookup = (void *) val;
16793         }
16794       else
16795         lookup = *psymp;
16796
16797       /* Only add a given psymbol once.  */
16798       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16799       if (!*slot)
16800         {
16801           *slot = lookup;
16802           add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16803         }
16804     }
16805 }
16806
16807 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16808    exception if there is an error.  */
16809
16810 static void
16811 write_obstack (FILE *file, struct obstack *obstack)
16812 {
16813   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16814               file)
16815       != obstack_object_size (obstack))
16816     error (_("couldn't data write to file"));
16817 }
16818
16819 /* Unlink a file if the argument is not NULL.  */
16820
16821 static void
16822 unlink_if_set (void *p)
16823 {
16824   char **filename = p;
16825   if (*filename)
16826     unlink (*filename);
16827 }
16828
16829 /* A helper struct used when iterating over debug_types.  */
16830 struct signatured_type_index_data
16831 {
16832   struct objfile *objfile;
16833   struct mapped_symtab *symtab;
16834   struct obstack *types_list;
16835   htab_t psyms_seen;
16836   int cu_index;
16837 };
16838
16839 /* A helper function that writes a single signatured_type to an
16840    obstack.  */
16841
16842 static int
16843 write_one_signatured_type (void **slot, void *d)
16844 {
16845   struct signatured_type_index_data *info = d;
16846   struct signatured_type *entry = (struct signatured_type *) *slot;
16847   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16848   struct partial_symtab *psymtab = per_cu->v.psymtab;
16849   gdb_byte val[8];
16850
16851   write_psymbols (info->symtab,
16852                   info->psyms_seen,
16853                   info->objfile->global_psymbols.list
16854                   + psymtab->globals_offset,
16855                   psymtab->n_global_syms, info->cu_index,
16856                   0);
16857   write_psymbols (info->symtab,
16858                   info->psyms_seen,
16859                   info->objfile->static_psymbols.list
16860                   + psymtab->statics_offset,
16861                   psymtab->n_static_syms, info->cu_index,
16862                   1);
16863
16864   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16865   obstack_grow (info->types_list, val, 8);
16866   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16867   obstack_grow (info->types_list, val, 8);
16868   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16869   obstack_grow (info->types_list, val, 8);
16870
16871   ++info->cu_index;
16872
16873   return 1;
16874 }
16875
16876 /* Create an index file for OBJFILE in the directory DIR.  */
16877
16878 static void
16879 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16880 {
16881   struct cleanup *cleanup;
16882   char *filename, *cleanup_filename;
16883   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16884   struct obstack cu_list, types_cu_list;
16885   int i;
16886   FILE *out_file;
16887   struct mapped_symtab *symtab;
16888   offset_type val, size_of_contents, total_len;
16889   struct stat st;
16890   char buf[8];
16891   htab_t psyms_seen;
16892   htab_t cu_index_htab;
16893   struct psymtab_cu_index_map *psymtab_cu_index_map;
16894
16895   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16896     return;
16897
16898   if (dwarf2_per_objfile->using_index)
16899     error (_("Cannot use an index to create the index"));
16900
16901   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16902     error (_("Cannot make an index when the file has multiple .debug_types sections"));
16903
16904   if (stat (objfile->name, &st) < 0)
16905     perror_with_name (objfile->name);
16906
16907   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16908                      INDEX_SUFFIX, (char *) NULL);
16909   cleanup = make_cleanup (xfree, filename);
16910
16911   out_file = fopen (filename, "wb");
16912   if (!out_file)
16913     error (_("Can't open `%s' for writing"), filename);
16914
16915   cleanup_filename = filename;
16916   make_cleanup (unlink_if_set, &cleanup_filename);
16917
16918   symtab = create_mapped_symtab ();
16919   make_cleanup (cleanup_mapped_symtab, symtab);
16920
16921   obstack_init (&addr_obstack);
16922   make_cleanup_obstack_free (&addr_obstack);
16923
16924   obstack_init (&cu_list);
16925   make_cleanup_obstack_free (&cu_list);
16926
16927   obstack_init (&types_cu_list);
16928   make_cleanup_obstack_free (&types_cu_list);
16929
16930   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16931                                   NULL, xcalloc, xfree);
16932   make_cleanup_htab_delete (psyms_seen);
16933
16934   /* While we're scanning CU's create a table that maps a psymtab pointer
16935      (which is what addrmap records) to its index (which is what is recorded
16936      in the index file).  This will later be needed to write the address
16937      table.  */
16938   cu_index_htab = htab_create_alloc (100,
16939                                      hash_psymtab_cu_index,
16940                                      eq_psymtab_cu_index,
16941                                      NULL, xcalloc, xfree);
16942   make_cleanup_htab_delete (cu_index_htab);
16943   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16944     xmalloc (sizeof (struct psymtab_cu_index_map)
16945              * dwarf2_per_objfile->n_comp_units);
16946   make_cleanup (xfree, psymtab_cu_index_map);
16947
16948   /* The CU list is already sorted, so we don't need to do additional
16949      work here.  Also, the debug_types entries do not appear in
16950      all_comp_units, but only in their own hash table.  */
16951   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16952     {
16953       struct dwarf2_per_cu_data *per_cu
16954         = dwarf2_per_objfile->all_comp_units[i];
16955       struct partial_symtab *psymtab = per_cu->v.psymtab;
16956       gdb_byte val[8];
16957       struct psymtab_cu_index_map *map;
16958       void **slot;
16959
16960       write_psymbols (symtab,
16961                       psyms_seen,
16962                       objfile->global_psymbols.list + psymtab->globals_offset,
16963                       psymtab->n_global_syms, i,
16964                       0);
16965       write_psymbols (symtab,
16966                       psyms_seen,
16967                       objfile->static_psymbols.list + psymtab->statics_offset,
16968                       psymtab->n_static_syms, i,
16969                       1);
16970
16971       map = &psymtab_cu_index_map[i];
16972       map->psymtab = psymtab;
16973       map->cu_index = i;
16974       slot = htab_find_slot (cu_index_htab, map, INSERT);
16975       gdb_assert (slot != NULL);
16976       gdb_assert (*slot == NULL);
16977       *slot = map;
16978
16979       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16980       obstack_grow (&cu_list, val, 8);
16981       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16982       obstack_grow (&cu_list, val, 8);
16983     }
16984
16985   /* Dump the address map.  */
16986   write_address_map (objfile, &addr_obstack, cu_index_htab);
16987
16988   /* Write out the .debug_type entries, if any.  */
16989   if (dwarf2_per_objfile->signatured_types)
16990     {
16991       struct signatured_type_index_data sig_data;
16992
16993       sig_data.objfile = objfile;
16994       sig_data.symtab = symtab;
16995       sig_data.types_list = &types_cu_list;
16996       sig_data.psyms_seen = psyms_seen;
16997       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16998       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16999                               write_one_signatured_type, &sig_data);
17000     }
17001
17002   obstack_init (&constant_pool);
17003   make_cleanup_obstack_free (&constant_pool);
17004   obstack_init (&symtab_obstack);
17005   make_cleanup_obstack_free (&symtab_obstack);
17006   write_hash_table (symtab, &symtab_obstack, &constant_pool);
17007
17008   obstack_init (&contents);
17009   make_cleanup_obstack_free (&contents);
17010   size_of_contents = 6 * sizeof (offset_type);
17011   total_len = size_of_contents;
17012
17013   /* The version number.  */
17014   val = MAYBE_SWAP (5);
17015   obstack_grow (&contents, &val, sizeof (val));
17016
17017   /* The offset of the CU list from the start of the file.  */
17018   val = MAYBE_SWAP (total_len);
17019   obstack_grow (&contents, &val, sizeof (val));
17020   total_len += obstack_object_size (&cu_list);
17021
17022   /* The offset of the types CU list from the start of the file.  */
17023   val = MAYBE_SWAP (total_len);
17024   obstack_grow (&contents, &val, sizeof (val));
17025   total_len += obstack_object_size (&types_cu_list);
17026
17027   /* The offset of the address table from the start of the file.  */
17028   val = MAYBE_SWAP (total_len);
17029   obstack_grow (&contents, &val, sizeof (val));
17030   total_len += obstack_object_size (&addr_obstack);
17031
17032   /* The offset of the symbol table from the start of the file.  */
17033   val = MAYBE_SWAP (total_len);
17034   obstack_grow (&contents, &val, sizeof (val));
17035   total_len += obstack_object_size (&symtab_obstack);
17036
17037   /* The offset of the constant pool from the start of the file.  */
17038   val = MAYBE_SWAP (total_len);
17039   obstack_grow (&contents, &val, sizeof (val));
17040   total_len += obstack_object_size (&constant_pool);
17041
17042   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17043
17044   write_obstack (out_file, &contents);
17045   write_obstack (out_file, &cu_list);
17046   write_obstack (out_file, &types_cu_list);
17047   write_obstack (out_file, &addr_obstack);
17048   write_obstack (out_file, &symtab_obstack);
17049   write_obstack (out_file, &constant_pool);
17050
17051   fclose (out_file);
17052
17053   /* We want to keep the file, so we set cleanup_filename to NULL
17054      here.  See unlink_if_set.  */
17055   cleanup_filename = NULL;
17056
17057   do_cleanups (cleanup);
17058 }
17059
17060 /* Implementation of the `save gdb-index' command.
17061    
17062    Note that the file format used by this command is documented in the
17063    GDB manual.  Any changes here must be documented there.  */
17064
17065 static void
17066 save_gdb_index_command (char *arg, int from_tty)
17067 {
17068   struct objfile *objfile;
17069
17070   if (!arg || !*arg)
17071     error (_("usage: save gdb-index DIRECTORY"));
17072
17073   ALL_OBJFILES (objfile)
17074   {
17075     struct stat st;
17076
17077     /* If the objfile does not correspond to an actual file, skip it.  */
17078     if (stat (objfile->name, &st) < 0)
17079       continue;
17080
17081     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17082     if (dwarf2_per_objfile)
17083       {
17084         volatile struct gdb_exception except;
17085
17086         TRY_CATCH (except, RETURN_MASK_ERROR)
17087           {
17088             write_psymtabs_to_index (objfile, arg);
17089           }
17090         if (except.reason < 0)
17091           exception_fprintf (gdb_stderr, except,
17092                              _("Error while writing index for `%s': "),
17093                              objfile->name);
17094       }
17095   }
17096 }
17097
17098 \f
17099
17100 int dwarf2_always_disassemble;
17101
17102 static void
17103 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17104                                 struct cmd_list_element *c, const char *value)
17105 {
17106   fprintf_filtered (file,
17107                     _("Whether to always disassemble "
17108                       "DWARF expressions is %s.\n"),
17109                     value);
17110 }
17111
17112 static void
17113 show_check_physname (struct ui_file *file, int from_tty,
17114                      struct cmd_list_element *c, const char *value)
17115 {
17116   fprintf_filtered (file,
17117                     _("Whether to check \"physname\" is %s.\n"),
17118                     value);
17119 }
17120
17121 void _initialize_dwarf2_read (void);
17122
17123 void
17124 _initialize_dwarf2_read (void)
17125 {
17126   struct cmd_list_element *c;
17127
17128   dwarf2_objfile_data_key
17129     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17130
17131   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17132 Set DWARF 2 specific variables.\n\
17133 Configure DWARF 2 variables such as the cache size"),
17134                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17135                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17136
17137   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17138 Show DWARF 2 specific variables\n\
17139 Show DWARF 2 variables such as the cache size"),
17140                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17141                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17142
17143   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17144                             &dwarf2_max_cache_age, _("\
17145 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17146 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17147 A higher limit means that cached compilation units will be stored\n\
17148 in memory longer, and more total memory will be used.  Zero disables\n\
17149 caching, which can slow down startup."),
17150                             NULL,
17151                             show_dwarf2_max_cache_age,
17152                             &set_dwarf2_cmdlist,
17153                             &show_dwarf2_cmdlist);
17154
17155   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17156                            &dwarf2_always_disassemble, _("\
17157 Set whether `info address' always disassembles DWARF expressions."), _("\
17158 Show whether `info address' always disassembles DWARF expressions."), _("\
17159 When enabled, DWARF expressions are always printed in an assembly-like\n\
17160 syntax.  When disabled, expressions will be printed in a more\n\
17161 conversational style, when possible."),
17162                            NULL,
17163                            show_dwarf2_always_disassemble,
17164                            &set_dwarf2_cmdlist,
17165                            &show_dwarf2_cmdlist);
17166
17167   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17168 Set debugging of the dwarf2 DIE reader."), _("\
17169 Show debugging of the dwarf2 DIE reader."), _("\
17170 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17171 The value is the maximum depth to print."),
17172                             NULL,
17173                             NULL,
17174                             &setdebuglist, &showdebuglist);
17175
17176   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17177 Set cross-checking of \"physname\" code against demangler."), _("\
17178 Show cross-checking of \"physname\" code against demangler."), _("\
17179 When enabled, GDB's internal \"physname\" code is checked against\n\
17180 the demangler."),
17181                            NULL, show_check_physname,
17182                            &setdebuglist, &showdebuglist);
17183
17184   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17185                _("\
17186 Save a gdb-index file.\n\
17187 Usage: save gdb-index DIRECTORY"),
17188                &save_cmdlist);
17189   set_cmd_completer (c, filename_completer);
17190 }