OSDN Git Service

Workaround for gcc/45682.
[pf3gnuchains/sourceware.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80    Because of alignment constraints, this structure has padding and cannot
81    be mapped directly onto the beginning of the .debug_info section.  */
82 typedef struct comp_unit_header
83   {
84     unsigned int length;        /* length of the .debug_info
85                                    contribution */
86     unsigned short version;     /* version number -- 2 for DWARF
87                                    version 2 */
88     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89     unsigned char addr_size;    /* byte size of an address -- 4 */
90   }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96    Because of alignment constraints, this structure has padding and cannot
97    be mapped directly onto the beginning of the .debug_info section.  */
98 typedef struct statement_prologue
99   {
100     unsigned int total_length;  /* byte length of the statement
101                                    information */
102     unsigned short version;     /* version number -- 2 for DWARF
103                                    version 2 */
104     unsigned int prologue_length;       /* # bytes between prologue &
105                                            stmt program */
106     unsigned char minimum_instruction_length;   /* byte size of
107                                                    smallest instr */
108     unsigned char default_is_stmt;      /* initial value of is_stmt
109                                            register */
110     char line_base;
111     unsigned char line_range;
112     unsigned char opcode_base;  /* number assigned to first special
113                                    opcode */
114     unsigned char *standard_opcode_lengths;
115   }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in.  */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124    info for C++ namespaces.  GCC 3.3.x did not produce this information,
125    but later versions do.  */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133   asection *asection;
134   gdb_byte *buffer;
135   bfd_size_type size;
136   int was_mmapped;
137   /* True if we have tried to read this section.  */
138   int readin;
139 };
140
141 /* All offsets in the index are of this type.  It must be
142    architecture-independent.  */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index.  The file format is described in
148    a comment by the code that writes the index.  */
149 struct mapped_index
150 {
151   /* The total length of the buffer.  */
152   off_t total_size;
153   /* A pointer to the address table data.  */
154   const gdb_byte *address_table;
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157   /* The hash table.  */
158   const offset_type *index_table;
159   /* Size in slots, each slot is 2 offset_types.  */
160   offset_type index_table_slots;
161   /* A pointer to the constant pool.  */
162   const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167   struct dwarf2_section_info info;
168   struct dwarf2_section_info abbrev;
169   struct dwarf2_section_info line;
170   struct dwarf2_section_info loc;
171   struct dwarf2_section_info macinfo;
172   struct dwarf2_section_info str;
173   struct dwarf2_section_info ranges;
174   struct dwarf2_section_info types;
175   struct dwarf2_section_info frame;
176   struct dwarf2_section_info eh_frame;
177   struct dwarf2_section_info gdb_index;
178
179   /* Back link.  */
180   struct objfile *objfile;
181
182   /* A list of all the compilation units.  This is used to locate
183      the target compilation unit of a particular reference.  */
184   struct dwarf2_per_cu_data **all_comp_units;
185
186   /* The number of compilation units in ALL_COMP_UNITS.  */
187   int n_comp_units;
188
189   /* The number of .debug_types-related CUs.  */
190   int n_type_comp_units;
191
192   /* The .debug_types-related CUs.  */
193   struct dwarf2_per_cu_data **type_comp_units;
194
195   /* A chain of compilation units that are currently read in, so that
196      they can be freed later.  */
197   struct dwarf2_per_cu_data *read_in_chain;
198
199   /* A table mapping .debug_types signatures to its signatured_type entry.
200      This is NULL if the .debug_types section hasn't been read in yet.  */
201   htab_t signatured_types;
202
203   /* A flag indicating wether this objfile has a section loaded at a
204      VMA of 0.  */
205   int has_section_at_zero;
206
207   /* True if we are using the mapped index.  */
208   unsigned char using_index;
209
210   /* The mapped index.  */
211   struct mapped_index *index_table;
212
213   /* Set during partial symbol reading, to prevent queueing of full
214      symbols.  */
215   int reading_partial_symbols;
216
217   /* Table mapping type .debug_info DIE offsets to types.
218      This is NULL if not allocated yet.
219      It (currently) makes sense to allocate debug_types_type_hash lazily.
220      To keep things simple we allocate both lazily.  */
221   htab_t debug_info_type_hash;
222
223   /* Table mapping type .debug_types DIE offsets to types.
224      This is NULL if not allocated yet.  */
225   htab_t debug_types_type_hash;
226 };
227
228 static struct dwarf2_per_objfile *dwarf2_per_objfile;
229
230 /* names of the debugging sections */
231
232 /* Note that if the debugging section has been compressed, it might
233    have a name like .zdebug_info.  */
234
235 #define INFO_SECTION     "debug_info"
236 #define ABBREV_SECTION   "debug_abbrev"
237 #define LINE_SECTION     "debug_line"
238 #define LOC_SECTION      "debug_loc"
239 #define MACINFO_SECTION  "debug_macinfo"
240 #define STR_SECTION      "debug_str"
241 #define RANGES_SECTION   "debug_ranges"
242 #define TYPES_SECTION    "debug_types"
243 #define FRAME_SECTION    "debug_frame"
244 #define EH_FRAME_SECTION "eh_frame"
245 #define GDB_INDEX_SECTION "gdb_index"
246
247 /* local data types */
248
249 /* We hold several abbreviation tables in memory at the same time. */
250 #ifndef ABBREV_HASH_SIZE
251 #define ABBREV_HASH_SIZE 121
252 #endif
253
254 /* The data in a compilation unit header, after target2host
255    translation, looks like this.  */
256 struct comp_unit_head
257 {
258   unsigned int length;
259   short version;
260   unsigned char addr_size;
261   unsigned char signed_addr_p;
262   unsigned int abbrev_offset;
263
264   /* Size of file offsets; either 4 or 8.  */
265   unsigned int offset_size;
266
267   /* Size of the length field; either 4 or 12.  */
268   unsigned int initial_length_size;
269
270   /* Offset to the first byte of this compilation unit header in the
271      .debug_info section, for resolving relative reference dies.  */
272   unsigned int offset;
273
274   /* Offset to first die in this cu from the start of the cu.
275      This will be the first byte following the compilation unit header.  */
276   unsigned int first_die_offset;
277 };
278
279 /* Type used for delaying computation of method physnames.
280    See comments for compute_delayed_physnames.  */
281 struct delayed_method_info
282 {
283   /* The type to which the method is attached, i.e., its parent class.  */
284   struct type *type;
285
286   /* The index of the method in the type's function fieldlists.  */
287   int fnfield_index;
288
289   /* The index of the method in the fieldlist.  */
290   int index;
291
292   /* The name of the DIE.  */
293   const char *name;
294
295   /*  The DIE associated with this method.  */
296   struct die_info *die;
297 };
298
299 typedef struct delayed_method_info delayed_method_info;
300 DEF_VEC_O (delayed_method_info);
301
302 /* Internal state when decoding a particular compilation unit.  */
303 struct dwarf2_cu
304 {
305   /* The objfile containing this compilation unit.  */
306   struct objfile *objfile;
307
308   /* The header of the compilation unit.  */
309   struct comp_unit_head header;
310
311   /* Base address of this compilation unit.  */
312   CORE_ADDR base_address;
313
314   /* Non-zero if base_address has been set.  */
315   int base_known;
316
317   struct function_range *first_fn, *last_fn, *cached_fn;
318
319   /* The language we are debugging.  */
320   enum language language;
321   const struct language_defn *language_defn;
322
323   const char *producer;
324
325   /* The generic symbol table building routines have separate lists for
326      file scope symbols and all all other scopes (local scopes).  So
327      we need to select the right one to pass to add_symbol_to_list().
328      We do it by keeping a pointer to the correct list in list_in_scope.
329
330      FIXME: The original dwarf code just treated the file scope as the
331      first local scope, and all other local scopes as nested local
332      scopes, and worked fine.  Check to see if we really need to
333      distinguish these in buildsym.c.  */
334   struct pending **list_in_scope;
335
336   /* DWARF abbreviation table associated with this compilation unit.  */
337   struct abbrev_info **dwarf2_abbrevs;
338
339   /* Storage for the abbrev table.  */
340   struct obstack abbrev_obstack;
341
342   /* Hash table holding all the loaded partial DIEs.  */
343   htab_t partial_dies;
344
345   /* Storage for things with the same lifetime as this read-in compilation
346      unit, including partial DIEs.  */
347   struct obstack comp_unit_obstack;
348
349   /* When multiple dwarf2_cu structures are living in memory, this field
350      chains them all together, so that they can be released efficiently.
351      We will probably also want a generation counter so that most-recently-used
352      compilation units are cached...  */
353   struct dwarf2_per_cu_data *read_in_chain;
354
355   /* Backchain to our per_cu entry if the tree has been built.  */
356   struct dwarf2_per_cu_data *per_cu;
357
358   /* How many compilation units ago was this CU last referenced?  */
359   int last_used;
360
361   /* A hash table of die offsets for following references.  */
362   htab_t die_hash;
363
364   /* Full DIEs if read in.  */
365   struct die_info *dies;
366
367   /* A set of pointers to dwarf2_per_cu_data objects for compilation
368      units referenced by this one.  Only set during full symbol processing;
369      partial symbol tables do not have dependencies.  */
370   htab_t dependencies;
371
372   /* Header data from the line table, during full symbol processing.  */
373   struct line_header *line_header;
374
375   /* A list of methods which need to have physnames computed
376      after all type information has been read.  */
377   VEC (delayed_method_info) *method_list;
378
379   /* Mark used when releasing cached dies.  */
380   unsigned int mark : 1;
381
382   /* This flag will be set if this compilation unit might include
383      inter-compilation-unit references.  */
384   unsigned int has_form_ref_addr : 1;
385
386   /* This flag will be set if this compilation unit includes any
387      DW_TAG_namespace DIEs.  If we know that there are explicit
388      DIEs for namespaces, we don't need to try to infer them
389      from mangled names.  */
390   unsigned int has_namespace_info : 1;
391 };
392
393 /* When using the index (and thus not using psymtabs), each CU has an
394    object of this type.  This is used to hold information needed by
395    the various "quick" methods.  */
396 struct dwarf2_per_cu_quick_data
397 {
398   /* The line table.  This can be NULL if there was no line table.  */
399   struct line_header *lines;
400
401   /* The file names from the line table.  */
402   const char **file_names;
403   /* The file names from the line table after being run through
404      gdb_realpath.  */
405   const char **full_names;
406
407   /* The corresponding symbol table.  This is NULL if symbols for this
408      CU have not yet been read.  */
409   struct symtab *symtab;
410
411   /* A temporary mark bit used when iterating over all CUs in
412      expand_symtabs_matching.  */
413   unsigned int mark : 1;
414
415   /* True if we've tried to read the line table.  */
416   unsigned int read_lines : 1;
417 };
418
419 /* Persistent data held for a compilation unit, even when not
420    processing it.  We put a pointer to this structure in the
421    read_symtab_private field of the psymtab.  If we encounter
422    inter-compilation-unit references, we also maintain a sorted
423    list of all compilation units.  */
424
425 struct dwarf2_per_cu_data
426 {
427   /* The start offset and length of this compilation unit.  2**29-1
428      bytes should suffice to store the length of any compilation unit
429      - if it doesn't, GDB will fall over anyway.
430      NOTE: Unlike comp_unit_head.length, this length includes
431      initial_length_size.  */
432   unsigned int offset;
433   unsigned int length : 29;
434
435   /* Flag indicating this compilation unit will be read in before
436      any of the current compilation units are processed.  */
437   unsigned int queued : 1;
438
439   /* This flag will be set if we need to load absolutely all DIEs
440      for this compilation unit, instead of just the ones we think
441      are interesting.  It gets set if we look for a DIE in the
442      hash table and don't find it.  */
443   unsigned int load_all_dies : 1;
444
445   /* Non-zero if this CU is from .debug_types.
446      Otherwise it's from .debug_info.  */
447   unsigned int from_debug_types : 1;
448
449   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
450      of the CU cache it gets reset to NULL again.  */
451   struct dwarf2_cu *cu;
452
453   /* The corresponding objfile.  */
454   struct objfile *objfile;
455
456   /* When using partial symbol tables, the 'psymtab' field is active.
457      Otherwise the 'quick' field is active.  */
458   union
459   {
460     /* The partial symbol table associated with this compilation unit,
461        or NULL for partial units (which do not have an associated
462        symtab).  */
463     struct partial_symtab *psymtab;
464
465     /* Data needed by the "quick" functions.  */
466     struct dwarf2_per_cu_quick_data *quick;
467   } v;
468 };
469
470 /* Entry in the signatured_types hash table.  */
471
472 struct signatured_type
473 {
474   ULONGEST signature;
475
476   /* Offset in .debug_types of the TU (type_unit) for this type.  */
477   unsigned int offset;
478
479   /* Offset in .debug_types of the type defined by this TU.  */
480   unsigned int type_offset;
481
482   /* The CU(/TU) of this type.  */
483   struct dwarf2_per_cu_data per_cu;
484 };
485
486 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
487    which are used for both .debug_info and .debug_types dies.
488    All parameters here are unchanging for the life of the call.
489    This struct exists to abstract away the constant parameters of
490    die reading.  */
491
492 struct die_reader_specs
493 {
494   /* The bfd of this objfile.  */
495   bfd* abfd;
496
497   /* The CU of the DIE we are parsing.  */
498   struct dwarf2_cu *cu;
499
500   /* Pointer to start of section buffer.
501      This is either the start of .debug_info or .debug_types.  */
502   const gdb_byte *buffer;
503 };
504
505 /* The line number information for a compilation unit (found in the
506    .debug_line section) begins with a "statement program header",
507    which contains the following information.  */
508 struct line_header
509 {
510   unsigned int total_length;
511   unsigned short version;
512   unsigned int header_length;
513   unsigned char minimum_instruction_length;
514   unsigned char maximum_ops_per_instruction;
515   unsigned char default_is_stmt;
516   int line_base;
517   unsigned char line_range;
518   unsigned char opcode_base;
519
520   /* standard_opcode_lengths[i] is the number of operands for the
521      standard opcode whose value is i.  This means that
522      standard_opcode_lengths[0] is unused, and the last meaningful
523      element is standard_opcode_lengths[opcode_base - 1].  */
524   unsigned char *standard_opcode_lengths;
525
526   /* The include_directories table.  NOTE!  These strings are not
527      allocated with xmalloc; instead, they are pointers into
528      debug_line_buffer.  If you try to free them, `free' will get
529      indigestion.  */
530   unsigned int num_include_dirs, include_dirs_size;
531   char **include_dirs;
532
533   /* The file_names table.  NOTE!  These strings are not allocated
534      with xmalloc; instead, they are pointers into debug_line_buffer.
535      Don't try to free them directly.  */
536   unsigned int num_file_names, file_names_size;
537   struct file_entry
538   {
539     char *name;
540     unsigned int dir_index;
541     unsigned int mod_time;
542     unsigned int length;
543     int included_p; /* Non-zero if referenced by the Line Number Program.  */
544     struct symtab *symtab; /* The associated symbol table, if any.  */
545   } *file_names;
546
547   /* The start and end of the statement program following this
548      header.  These point into dwarf2_per_objfile->line_buffer.  */
549   gdb_byte *statement_program_start, *statement_program_end;
550 };
551
552 /* When we construct a partial symbol table entry we only
553    need this much information. */
554 struct partial_die_info
555   {
556     /* Offset of this DIE.  */
557     unsigned int offset;
558
559     /* DWARF-2 tag for this DIE.  */
560     ENUM_BITFIELD(dwarf_tag) tag : 16;
561
562     /* Assorted flags describing the data found in this DIE.  */
563     unsigned int has_children : 1;
564     unsigned int is_external : 1;
565     unsigned int is_declaration : 1;
566     unsigned int has_type : 1;
567     unsigned int has_specification : 1;
568     unsigned int has_pc_info : 1;
569
570     /* Flag set if the SCOPE field of this structure has been
571        computed.  */
572     unsigned int scope_set : 1;
573
574     /* Flag set if the DIE has a byte_size attribute.  */
575     unsigned int has_byte_size : 1;
576
577     /* Flag set if any of the DIE's children are template arguments.  */
578     unsigned int has_template_arguments : 1;
579
580     /* Flag set if fixup_partial_die has been called on this die.  */
581     unsigned int fixup_called : 1;
582
583     /* The name of this DIE.  Normally the value of DW_AT_name, but
584        sometimes a default name for unnamed DIEs.  */
585     char *name;
586
587     /* The linkage name, if present.  */
588     const char *linkage_name;
589
590     /* The scope to prepend to our children.  This is generally
591        allocated on the comp_unit_obstack, so will disappear
592        when this compilation unit leaves the cache.  */
593     char *scope;
594
595     /* The location description associated with this DIE, if any.  */
596     struct dwarf_block *locdesc;
597
598     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
599     CORE_ADDR lowpc;
600     CORE_ADDR highpc;
601
602     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
603        DW_AT_sibling, if any.  */
604     /* NOTE: This member isn't strictly necessary, read_partial_die could
605        return DW_AT_sibling values to its caller load_partial_dies.  */
606     gdb_byte *sibling;
607
608     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
609        DW_AT_specification (or DW_AT_abstract_origin or
610        DW_AT_extension).  */
611     unsigned int spec_offset;
612
613     /* Pointers to this DIE's parent, first child, and next sibling,
614        if any.  */
615     struct partial_die_info *die_parent, *die_child, *die_sibling;
616   };
617
618 /* This data structure holds the information of an abbrev. */
619 struct abbrev_info
620   {
621     unsigned int number;        /* number identifying abbrev */
622     enum dwarf_tag tag;         /* dwarf tag */
623     unsigned short has_children;                /* boolean */
624     unsigned short num_attrs;   /* number of attributes */
625     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
626     struct abbrev_info *next;   /* next in chain */
627   };
628
629 struct attr_abbrev
630   {
631     ENUM_BITFIELD(dwarf_attribute) name : 16;
632     ENUM_BITFIELD(dwarf_form) form : 16;
633   };
634
635 /* Attributes have a name and a value */
636 struct attribute
637   {
638     ENUM_BITFIELD(dwarf_attribute) name : 16;
639     ENUM_BITFIELD(dwarf_form) form : 15;
640
641     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
642        field should be in u.str (existing only for DW_STRING) but it is kept
643        here for better struct attribute alignment.  */
644     unsigned int string_is_canonical : 1;
645
646     union
647       {
648         char *str;
649         struct dwarf_block *blk;
650         ULONGEST unsnd;
651         LONGEST snd;
652         CORE_ADDR addr;
653         struct signatured_type *signatured_type;
654       }
655     u;
656   };
657
658 /* This data structure holds a complete die structure. */
659 struct die_info
660   {
661     /* DWARF-2 tag for this DIE.  */
662     ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664     /* Number of attributes */
665     unsigned char num_attrs;
666
667     /* True if we're presently building the full type name for the
668        type derived from this DIE.  */
669     unsigned char building_fullname : 1;
670
671     /* Abbrev number */
672     unsigned int abbrev;
673
674     /* Offset in .debug_info or .debug_types section.  */
675     unsigned int offset;
676
677     /* The dies in a compilation unit form an n-ary tree.  PARENT
678        points to this die's parent; CHILD points to the first child of
679        this node; and all the children of a given node are chained
680        together via their SIBLING fields.  */
681     struct die_info *child;     /* Its first child, if any.  */
682     struct die_info *sibling;   /* Its next sibling, if any.  */
683     struct die_info *parent;    /* Its parent, if any.  */
684
685     /* An array of attributes, with NUM_ATTRS elements.  There may be
686        zero, but it's not common and zero-sized arrays are not
687        sufficiently portable C.  */
688     struct attribute attrs[1];
689   };
690
691 struct function_range
692 {
693   const char *name;
694   CORE_ADDR lowpc, highpc;
695   int seen_line;
696   struct function_range *next;
697 };
698
699 /* Get at parts of an attribute structure */
700
701 #define DW_STRING(attr)    ((attr)->u.str)
702 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
703 #define DW_UNSND(attr)     ((attr)->u.unsnd)
704 #define DW_BLOCK(attr)     ((attr)->u.blk)
705 #define DW_SND(attr)       ((attr)->u.snd)
706 #define DW_ADDR(attr)      ((attr)->u.addr)
707 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
708
709 /* Blocks are a bunch of untyped bytes. */
710 struct dwarf_block
711   {
712     unsigned int size;
713     gdb_byte *data;
714   };
715
716 #ifndef ATTR_ALLOC_CHUNK
717 #define ATTR_ALLOC_CHUNK 4
718 #endif
719
720 /* Allocate fields for structs, unions and enums in this size.  */
721 #ifndef DW_FIELD_ALLOC_CHUNK
722 #define DW_FIELD_ALLOC_CHUNK 4
723 #endif
724
725 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
726    but this would require a corresponding change in unpack_field_as_long
727    and friends.  */
728 static int bits_per_byte = 8;
729
730 /* The routines that read and process dies for a C struct or C++ class
731    pass lists of data member fields and lists of member function fields
732    in an instance of a field_info structure, as defined below.  */
733 struct field_info
734   {
735     /* List of data member and baseclasses fields. */
736     struct nextfield
737       {
738         struct nextfield *next;
739         int accessibility;
740         int virtuality;
741         struct field field;
742       }
743      *fields, *baseclasses;
744
745     /* Number of fields (including baseclasses).  */
746     int nfields;
747
748     /* Number of baseclasses.  */
749     int nbaseclasses;
750
751     /* Set if the accesibility of one of the fields is not public.  */
752     int non_public_fields;
753
754     /* Member function fields array, entries are allocated in the order they
755        are encountered in the object file.  */
756     struct nextfnfield
757       {
758         struct nextfnfield *next;
759         struct fn_field fnfield;
760       }
761      *fnfields;
762
763     /* Member function fieldlist array, contains name of possibly overloaded
764        member function, number of overloaded member functions and a pointer
765        to the head of the member function field chain.  */
766     struct fnfieldlist
767       {
768         char *name;
769         int length;
770         struct nextfnfield *head;
771       }
772      *fnfieldlists;
773
774     /* Number of entries in the fnfieldlists array.  */
775     int nfnfields;
776
777     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
778        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
779     struct typedef_field_list
780       {
781         struct typedef_field field;
782         struct typedef_field_list *next;
783       }
784     *typedef_field_list;
785     unsigned typedef_field_list_count;
786   };
787
788 /* One item on the queue of compilation units to read in full symbols
789    for.  */
790 struct dwarf2_queue_item
791 {
792   struct dwarf2_per_cu_data *per_cu;
793   struct dwarf2_queue_item *next;
794 };
795
796 /* The current queue.  */
797 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
798
799 /* Loaded secondary compilation units are kept in memory until they
800    have not been referenced for the processing of this many
801    compilation units.  Set this to zero to disable caching.  Cache
802    sizes of up to at least twenty will improve startup time for
803    typical inter-CU-reference binaries, at an obvious memory cost.  */
804 static int dwarf2_max_cache_age = 5;
805 static void
806 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
807                            struct cmd_list_element *c, const char *value)
808 {
809   fprintf_filtered (file, _("\
810 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
811                     value);
812 }
813
814
815 /* Various complaints about symbol reading that don't abort the process */
816
817 static void
818 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
819 {
820   complaint (&symfile_complaints,
821              _("statement list doesn't fit in .debug_line section"));
822 }
823
824 static void
825 dwarf2_debug_line_missing_file_complaint (void)
826 {
827   complaint (&symfile_complaints,
828              _(".debug_line section has line data without a file"));
829 }
830
831 static void
832 dwarf2_debug_line_missing_end_sequence_complaint (void)
833 {
834   complaint (&symfile_complaints,
835              _(".debug_line section has line program sequence without an end"));
836 }
837
838 static void
839 dwarf2_complex_location_expr_complaint (void)
840 {
841   complaint (&symfile_complaints, _("location expression too complex"));
842 }
843
844 static void
845 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846                                               int arg3)
847 {
848   complaint (&symfile_complaints,
849              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
850              arg2, arg3);
851 }
852
853 static void
854 dwarf2_macros_too_long_complaint (void)
855 {
856   complaint (&symfile_complaints,
857              _("macro info runs off end of `.debug_macinfo' section"));
858 }
859
860 static void
861 dwarf2_macro_malformed_definition_complaint (const char *arg1)
862 {
863   complaint (&symfile_complaints,
864              _("macro debug info contains a malformed macro definition:\n`%s'"),
865              arg1);
866 }
867
868 static void
869 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
870 {
871   complaint (&symfile_complaints,
872              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
873 }
874
875 /* local function prototypes */
876
877 static void dwarf2_locate_sections (bfd *, asection *, void *);
878
879 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880                                            struct objfile *);
881
882 static void dwarf2_build_psymtabs_hard (struct objfile *);
883
884 static void scan_partial_symbols (struct partial_die_info *,
885                                   CORE_ADDR *, CORE_ADDR *,
886                                   int, struct dwarf2_cu *);
887
888 static void add_partial_symbol (struct partial_die_info *,
889                                 struct dwarf2_cu *);
890
891 static void add_partial_namespace (struct partial_die_info *pdi,
892                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
893                                    int need_pc, struct dwarf2_cu *cu);
894
895 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896                                 CORE_ADDR *highpc, int need_pc,
897                                 struct dwarf2_cu *cu);
898
899 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900                                      struct dwarf2_cu *cu);
901
902 static void add_partial_subprogram (struct partial_die_info *pdi,
903                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
904                                     int need_pc, struct dwarf2_cu *cu);
905
906 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
907                                      gdb_byte *buffer, gdb_byte *info_ptr,
908                                      bfd *abfd, struct dwarf2_cu *cu);
909
910 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
911
912 static void psymtab_to_symtab_1 (struct partial_symtab *);
913
914 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
915
916 static void dwarf2_free_abbrev_table (void *);
917
918 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
919                                             struct dwarf2_cu *);
920
921 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
922                                                  struct dwarf2_cu *);
923
924 static struct partial_die_info *load_partial_dies (bfd *,
925                                                    gdb_byte *, gdb_byte *,
926                                                    int, struct dwarf2_cu *);
927
928 static gdb_byte *read_partial_die (struct partial_die_info *,
929                                    struct abbrev_info *abbrev,
930                                    unsigned int, bfd *,
931                                    gdb_byte *, gdb_byte *,
932                                    struct dwarf2_cu *);
933
934 static struct partial_die_info *find_partial_die (unsigned int,
935                                                   struct dwarf2_cu *);
936
937 static void fixup_partial_die (struct partial_die_info *,
938                                struct dwarf2_cu *);
939
940 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941                                  bfd *, gdb_byte *, struct dwarf2_cu *);
942
943 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944                                        bfd *, gdb_byte *, struct dwarf2_cu *);
945
946 static unsigned int read_1_byte (bfd *, gdb_byte *);
947
948 static int read_1_signed_byte (bfd *, gdb_byte *);
949
950 static unsigned int read_2_bytes (bfd *, gdb_byte *);
951
952 static unsigned int read_4_bytes (bfd *, gdb_byte *);
953
954 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
955
956 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
957                                unsigned int *);
958
959 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961 static LONGEST read_checked_initial_length_and_offset
962   (bfd *, gdb_byte *, const struct comp_unit_head *,
963    unsigned int *, unsigned int *);
964
965 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
966                             unsigned int *);
967
968 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
969
970 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
971
972 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
973
974 static char *read_indirect_string (bfd *, gdb_byte *,
975                                    const struct comp_unit_head *,
976                                    unsigned int *);
977
978 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
979
980 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
981
982 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
983
984 static void set_cu_language (unsigned int, struct dwarf2_cu *);
985
986 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987                                       struct dwarf2_cu *);
988
989 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990                                                 unsigned int,
991                                                 struct dwarf2_cu *);
992
993 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994                                struct dwarf2_cu *cu);
995
996 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
997
998 static struct die_info *die_specification (struct die_info *die,
999                                            struct dwarf2_cu **);
1000
1001 static void free_line_header (struct line_header *lh);
1002
1003 static void add_file_name (struct line_header *, char *, unsigned int,
1004                            unsigned int, unsigned int);
1005
1006 static struct line_header *(dwarf_decode_line_header
1007                             (unsigned int offset,
1008                              bfd *abfd, struct dwarf2_cu *cu));
1009
1010 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1011                                 struct dwarf2_cu *, struct partial_symtab *);
1012
1013 static void dwarf2_start_subfile (char *, char *, char *);
1014
1015 static struct symbol *new_symbol (struct die_info *, struct type *,
1016                                   struct dwarf2_cu *);
1017
1018 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019                                        struct dwarf2_cu *, struct symbol *);
1020
1021 static void dwarf2_const_value (struct attribute *, struct symbol *,
1022                                 struct dwarf2_cu *);
1023
1024 static void dwarf2_const_value_attr (struct attribute *attr,
1025                                      struct type *type,
1026                                      const char *name,
1027                                      struct obstack *obstack,
1028                                      struct dwarf2_cu *cu, long *value,
1029                                      gdb_byte **bytes,
1030                                      struct dwarf2_locexpr_baton **baton);
1031
1032 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1033
1034 static int need_gnat_info (struct dwarf2_cu *);
1035
1036 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1037
1038 static void set_descriptive_type (struct type *, struct die_info *,
1039                                   struct dwarf2_cu *);
1040
1041 static struct type *die_containing_type (struct die_info *,
1042                                          struct dwarf2_cu *);
1043
1044 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045                                      struct dwarf2_cu *);
1046
1047 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1048
1049 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
1051 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1052
1053 static char *typename_concat (struct obstack *obs, const char *prefix,
1054                               const char *suffix, int physname,
1055                               struct dwarf2_cu *cu);
1056
1057 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1062
1063 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1064
1065 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066                                struct dwarf2_cu *, struct partial_symtab *);
1067
1068 static int dwarf2_get_pc_bounds (struct die_info *,
1069                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070                                  struct partial_symtab *);
1071
1072 static void get_scope_pc_bounds (struct die_info *,
1073                                  CORE_ADDR *, CORE_ADDR *,
1074                                  struct dwarf2_cu *);
1075
1076 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077                                         CORE_ADDR, struct dwarf2_cu *);
1078
1079 static void dwarf2_add_field (struct field_info *, struct die_info *,
1080                               struct dwarf2_cu *);
1081
1082 static void dwarf2_attach_fields_to_type (struct field_info *,
1083                                           struct type *, struct dwarf2_cu *);
1084
1085 static void dwarf2_add_member_fn (struct field_info *,
1086                                   struct die_info *, struct type *,
1087                                   struct dwarf2_cu *);
1088
1089 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1090                                              struct type *, struct dwarf2_cu *);
1091
1092 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1093
1094 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1095
1096 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1097
1098 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1099
1100 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1101
1102 static struct type *read_module_type (struct die_info *die,
1103                                       struct dwarf2_cu *cu);
1104
1105 static const char *namespace_name (struct die_info *die,
1106                                    int *is_anonymous, struct dwarf2_cu *);
1107
1108 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1109
1110 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1111
1112 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1113                                                        struct dwarf2_cu *);
1114
1115 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1116
1117 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1118                                                  gdb_byte *info_ptr,
1119                                                  gdb_byte **new_info_ptr,
1120                                                  struct die_info *parent);
1121
1122 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1123                                                gdb_byte *info_ptr,
1124                                                gdb_byte **new_info_ptr,
1125                                                struct die_info *parent);
1126
1127 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1128                                                gdb_byte *info_ptr,
1129                                                gdb_byte **new_info_ptr,
1130                                                struct die_info *parent);
1131
1132 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1133                                 struct die_info **, gdb_byte *,
1134                                 int *);
1135
1136 static void process_die (struct die_info *, struct dwarf2_cu *);
1137
1138 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1139                                        struct obstack *);
1140
1141 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1142
1143 static const char *dwarf2_full_name (char *name,
1144                                      struct die_info *die,
1145                                      struct dwarf2_cu *cu);
1146
1147 static struct die_info *dwarf2_extension (struct die_info *die,
1148                                           struct dwarf2_cu **);
1149
1150 static char *dwarf_tag_name (unsigned int);
1151
1152 static char *dwarf_attr_name (unsigned int);
1153
1154 static char *dwarf_form_name (unsigned int);
1155
1156 static char *dwarf_bool_name (unsigned int);
1157
1158 static char *dwarf_type_encoding_name (unsigned int);
1159
1160 #if 0
1161 static char *dwarf_cfi_name (unsigned int);
1162 #endif
1163
1164 static struct die_info *sibling_die (struct die_info *);
1165
1166 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1167
1168 static void dump_die_for_error (struct die_info *);
1169
1170 static void dump_die_1 (struct ui_file *, int level, int max_level,
1171                         struct die_info *);
1172
1173 /*static*/ void dump_die (struct die_info *, int max_level);
1174
1175 static void store_in_ref_table (struct die_info *,
1176                                 struct dwarf2_cu *);
1177
1178 static int is_ref_attr (struct attribute *);
1179
1180 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1181
1182 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1183
1184 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1185                                                struct attribute *,
1186                                                struct dwarf2_cu **);
1187
1188 static struct die_info *follow_die_ref (struct die_info *,
1189                                         struct attribute *,
1190                                         struct dwarf2_cu **);
1191
1192 static struct die_info *follow_die_sig (struct die_info *,
1193                                         struct attribute *,
1194                                         struct dwarf2_cu **);
1195
1196 static void read_signatured_type_at_offset (struct objfile *objfile,
1197                                             unsigned int offset);
1198
1199 static void read_signatured_type (struct objfile *,
1200                                   struct signatured_type *type_sig);
1201
1202 /* memory allocation interface */
1203
1204 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1205
1206 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1207
1208 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1209
1210 static void initialize_cu_func_list (struct dwarf2_cu *);
1211
1212 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1213                                  struct dwarf2_cu *);
1214
1215 static void dwarf_decode_macros (struct line_header *, unsigned int,
1216                                  char *, bfd *, struct dwarf2_cu *);
1217
1218 static int attr_form_is_block (struct attribute *);
1219
1220 static int attr_form_is_section_offset (struct attribute *);
1221
1222 static int attr_form_is_constant (struct attribute *);
1223
1224 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1225                                          struct symbol *sym,
1226                                          struct dwarf2_cu *cu);
1227
1228 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1229                                struct abbrev_info *abbrev,
1230                                struct dwarf2_cu *cu);
1231
1232 static void free_stack_comp_unit (void *);
1233
1234 static hashval_t partial_die_hash (const void *item);
1235
1236 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1237
1238 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1239   (unsigned int offset, struct objfile *objfile);
1240
1241 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1242   (unsigned int offset, struct objfile *objfile);
1243
1244 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1245
1246 static void free_one_comp_unit (void *);
1247
1248 static void free_cached_comp_units (void *);
1249
1250 static void age_cached_comp_units (void);
1251
1252 static void free_one_cached_comp_unit (void *);
1253
1254 static struct type *set_die_type (struct die_info *, struct type *,
1255                                   struct dwarf2_cu *);
1256
1257 static void create_all_comp_units (struct objfile *);
1258
1259 static int create_debug_types_hash_table (struct objfile *objfile);
1260
1261 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1262                                  struct objfile *);
1263
1264 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1265
1266 static void dwarf2_add_dependence (struct dwarf2_cu *,
1267                                    struct dwarf2_per_cu_data *);
1268
1269 static void dwarf2_mark (struct dwarf2_cu *);
1270
1271 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1272
1273 static struct type *get_die_type_at_offset (unsigned int,
1274                                             struct dwarf2_per_cu_data *per_cu);
1275
1276 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1277
1278 static void dwarf2_release_queue (void *dummy);
1279
1280 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1281                              struct objfile *objfile);
1282
1283 static void process_queue (struct objfile *objfile);
1284
1285 static void find_file_and_directory (struct die_info *die,
1286                                      struct dwarf2_cu *cu,
1287                                      char **name, char **comp_dir);
1288
1289 static char *file_full_name (int file, struct line_header *lh,
1290                              const char *comp_dir);
1291
1292 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1293                                               gdb_byte *info_ptr,
1294                                               gdb_byte *buffer,
1295                                               unsigned int buffer_size,
1296                                               bfd *abfd);
1297
1298 static void init_cu_die_reader (struct die_reader_specs *reader,
1299                                 struct dwarf2_cu *cu);
1300
1301 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1302
1303 #if WORDS_BIGENDIAN
1304
1305 /* Convert VALUE between big- and little-endian.  */
1306 static offset_type
1307 byte_swap (offset_type value)
1308 {
1309   offset_type result;
1310
1311   result = (value & 0xff) << 24;
1312   result |= (value & 0xff00) << 8;
1313   result |= (value & 0xff0000) >> 8;
1314   result |= (value & 0xff000000) >> 24;
1315   return result;
1316 }
1317
1318 #define MAYBE_SWAP(V)  byte_swap (V)
1319
1320 #else
1321 #define MAYBE_SWAP(V) (V)
1322 #endif /* WORDS_BIGENDIAN */
1323
1324 /* The suffix for an index file.  */
1325 #define INDEX_SUFFIX ".gdb-index"
1326
1327 static const char *dwarf2_physname (char *name, struct die_info *die,
1328                                     struct dwarf2_cu *cu);
1329
1330 /* Try to locate the sections we need for DWARF 2 debugging
1331    information and return true if we have enough to do something.  */
1332
1333 int
1334 dwarf2_has_info (struct objfile *objfile)
1335 {
1336   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1337   if (!dwarf2_per_objfile)
1338     {
1339       /* Initialize per-objfile state.  */
1340       struct dwarf2_per_objfile *data
1341         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1342
1343       memset (data, 0, sizeof (*data));
1344       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1345       dwarf2_per_objfile = data;
1346
1347       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1348       dwarf2_per_objfile->objfile = objfile;
1349     }
1350   return (dwarf2_per_objfile->info.asection != NULL
1351           && dwarf2_per_objfile->abbrev.asection != NULL);
1352 }
1353
1354 /* When loading sections, we can either look for ".<name>", or for
1355  * ".z<name>", which indicates a compressed section.  */
1356
1357 static int
1358 section_is_p (const char *section_name, const char *name)
1359 {
1360   return (section_name[0] == '.'
1361           && (strcmp (section_name + 1, name) == 0
1362               || (section_name[1] == 'z'
1363                   && strcmp (section_name + 2, name) == 0)));
1364 }
1365
1366 /* This function is mapped across the sections and remembers the
1367    offset and size of each of the debugging sections we are interested
1368    in.  */
1369
1370 static void
1371 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1372 {
1373   if (section_is_p (sectp->name, INFO_SECTION))
1374     {
1375       dwarf2_per_objfile->info.asection = sectp;
1376       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1377     }
1378   else if (section_is_p (sectp->name, ABBREV_SECTION))
1379     {
1380       dwarf2_per_objfile->abbrev.asection = sectp;
1381       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1382     }
1383   else if (section_is_p (sectp->name, LINE_SECTION))
1384     {
1385       dwarf2_per_objfile->line.asection = sectp;
1386       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1387     }
1388   else if (section_is_p (sectp->name, LOC_SECTION))
1389     {
1390       dwarf2_per_objfile->loc.asection = sectp;
1391       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1392     }
1393   else if (section_is_p (sectp->name, MACINFO_SECTION))
1394     {
1395       dwarf2_per_objfile->macinfo.asection = sectp;
1396       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1397     }
1398   else if (section_is_p (sectp->name, STR_SECTION))
1399     {
1400       dwarf2_per_objfile->str.asection = sectp;
1401       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1402     }
1403   else if (section_is_p (sectp->name, FRAME_SECTION))
1404     {
1405       dwarf2_per_objfile->frame.asection = sectp;
1406       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1407     }
1408   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1409     {
1410       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1411
1412       if (aflag & SEC_HAS_CONTENTS)
1413         {
1414           dwarf2_per_objfile->eh_frame.asection = sectp;
1415           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1416         }
1417     }
1418   else if (section_is_p (sectp->name, RANGES_SECTION))
1419     {
1420       dwarf2_per_objfile->ranges.asection = sectp;
1421       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1422     }
1423   else if (section_is_p (sectp->name, TYPES_SECTION))
1424     {
1425       dwarf2_per_objfile->types.asection = sectp;
1426       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1427     }
1428   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1429     {
1430       dwarf2_per_objfile->gdb_index.asection = sectp;
1431       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1432     }
1433
1434   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1435       && bfd_section_vma (abfd, sectp) == 0)
1436     dwarf2_per_objfile->has_section_at_zero = 1;
1437 }
1438
1439 /* Decompress a section that was compressed using zlib.  Store the
1440    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1441
1442 static void
1443 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1444                          gdb_byte **outbuf, bfd_size_type *outsize)
1445 {
1446   bfd *abfd = objfile->obfd;
1447 #ifndef HAVE_ZLIB_H
1448   error (_("Support for zlib-compressed DWARF data (from '%s') "
1449            "is disabled in this copy of GDB"),
1450          bfd_get_filename (abfd));
1451 #else
1452   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1453   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1454   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1455   bfd_size_type uncompressed_size;
1456   gdb_byte *uncompressed_buffer;
1457   z_stream strm;
1458   int rc;
1459   int header_size = 12;
1460
1461   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1462       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1463     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1464            bfd_get_filename (abfd));
1465
1466   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1467      by the uncompressed section size, 8 bytes in big-endian order.  */
1468   if (compressed_size < header_size
1469       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1470     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1471            bfd_get_filename (abfd));
1472   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1473   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1474   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1475   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1476   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1477   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1478   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1479   uncompressed_size += compressed_buffer[11];
1480
1481   /* It is possible the section consists of several compressed
1482      buffers concatenated together, so we uncompress in a loop.  */
1483   strm.zalloc = NULL;
1484   strm.zfree = NULL;
1485   strm.opaque = NULL;
1486   strm.avail_in = compressed_size - header_size;
1487   strm.next_in = (Bytef*) compressed_buffer + header_size;
1488   strm.avail_out = uncompressed_size;
1489   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1490                                        uncompressed_size);
1491   rc = inflateInit (&strm);
1492   while (strm.avail_in > 0)
1493     {
1494       if (rc != Z_OK)
1495         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1496                bfd_get_filename (abfd), rc);
1497       strm.next_out = ((Bytef*) uncompressed_buffer
1498                        + (uncompressed_size - strm.avail_out));
1499       rc = inflate (&strm, Z_FINISH);
1500       if (rc != Z_STREAM_END)
1501         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1502                bfd_get_filename (abfd), rc);
1503       rc = inflateReset (&strm);
1504     }
1505   rc = inflateEnd (&strm);
1506   if (rc != Z_OK
1507       || strm.avail_out != 0)
1508     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1509            bfd_get_filename (abfd), rc);
1510
1511   do_cleanups (cleanup);
1512   *outbuf = uncompressed_buffer;
1513   *outsize = uncompressed_size;
1514 #endif
1515 }
1516
1517 /* Read the contents of the section SECTP from object file specified by
1518    OBJFILE, store info about the section into INFO.
1519    If the section is compressed, uncompress it before returning.  */
1520
1521 static void
1522 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1523 {
1524   bfd *abfd = objfile->obfd;
1525   asection *sectp = info->asection;
1526   gdb_byte *buf, *retbuf;
1527   unsigned char header[4];
1528
1529   if (info->readin)
1530     return;
1531   info->buffer = NULL;
1532   info->was_mmapped = 0;
1533   info->readin = 1;
1534
1535   if (info->asection == NULL || info->size == 0)
1536     return;
1537
1538   /* Check if the file has a 4-byte header indicating compression.  */
1539   if (info->size > sizeof (header)
1540       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1541       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1542     {
1543       /* Upon decompression, update the buffer and its size.  */
1544       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1545         {
1546           zlib_decompress_section (objfile, sectp, &info->buffer,
1547                                    &info->size);
1548           return;
1549         }
1550     }
1551
1552 #ifdef HAVE_MMAP
1553   if (pagesize == 0)
1554     pagesize = getpagesize ();
1555
1556   /* Only try to mmap sections which are large enough: we don't want to
1557      waste space due to fragmentation.  Also, only try mmap for sections
1558      without relocations.  */
1559
1560   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1561     {
1562       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1563       size_t map_length = info->size + sectp->filepos - pg_offset;
1564       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1565                                  MAP_PRIVATE, pg_offset);
1566
1567       if (retbuf != MAP_FAILED)
1568         {
1569           info->was_mmapped = 1;
1570           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1571 #if HAVE_POSIX_MADVISE
1572           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1573 #endif
1574           return;
1575         }
1576     }
1577 #endif
1578
1579   /* If we get here, we are a normal, not-compressed section.  */
1580   info->buffer = buf
1581     = obstack_alloc (&objfile->objfile_obstack, info->size);
1582
1583   /* When debugging .o files, we may need to apply relocations; see
1584      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1585      We never compress sections in .o files, so we only need to
1586      try this when the section is not compressed.  */
1587   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1588   if (retbuf != NULL)
1589     {
1590       info->buffer = retbuf;
1591       return;
1592     }
1593
1594   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1595       || bfd_bread (buf, info->size, abfd) != info->size)
1596     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1597            bfd_get_filename (abfd));
1598 }
1599
1600 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1601    SECTION_NAME. */
1602
1603 void
1604 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1605                          asection **sectp, gdb_byte **bufp,
1606                          bfd_size_type *sizep)
1607 {
1608   struct dwarf2_per_objfile *data
1609     = objfile_data (objfile, dwarf2_objfile_data_key);
1610   struct dwarf2_section_info *info;
1611
1612   /* We may see an objfile without any DWARF, in which case we just
1613      return nothing.  */
1614   if (data == NULL)
1615     {
1616       *sectp = NULL;
1617       *bufp = NULL;
1618       *sizep = 0;
1619       return;
1620     }
1621   if (section_is_p (section_name, EH_FRAME_SECTION))
1622     info = &data->eh_frame;
1623   else if (section_is_p (section_name, FRAME_SECTION))
1624     info = &data->frame;
1625   else
1626     gdb_assert_not_reached ("unexpected section");
1627
1628   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1629     /* We haven't read this section in yet.  Do it now.  */
1630     dwarf2_read_section (objfile, info);
1631
1632   *sectp = info->asection;
1633   *bufp = info->buffer;
1634   *sizep = info->size;
1635 }
1636
1637 \f
1638
1639 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1640    this CU came.  */
1641 static void
1642 dw2_do_instantiate_symtab (struct objfile *objfile,
1643                            struct dwarf2_per_cu_data *per_cu)
1644 {
1645   struct cleanup *back_to;
1646
1647   back_to = make_cleanup (dwarf2_release_queue, NULL);
1648
1649   queue_comp_unit (per_cu, objfile);
1650
1651   if (per_cu->from_debug_types)
1652     read_signatured_type_at_offset (objfile, per_cu->offset);
1653   else
1654     load_full_comp_unit (per_cu, objfile);
1655
1656   process_queue (objfile);
1657
1658   /* Age the cache, releasing compilation units that have not
1659      been used recently.  */
1660   age_cached_comp_units ();
1661
1662   do_cleanups (back_to);
1663 }
1664
1665 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1666    the objfile from which this CU came.  Returns the resulting symbol
1667    table.  */
1668 static struct symtab *
1669 dw2_instantiate_symtab (struct objfile *objfile,
1670                         struct dwarf2_per_cu_data *per_cu)
1671 {
1672   if (!per_cu->v.quick->symtab)
1673     {
1674       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1675       increment_reading_symtab ();
1676       dw2_do_instantiate_symtab (objfile, per_cu);
1677       do_cleanups (back_to);
1678     }
1679   return per_cu->v.quick->symtab;
1680 }
1681
1682 /* Return the CU given its index.  */
1683 static struct dwarf2_per_cu_data *
1684 dw2_get_cu (int index)
1685 {
1686   if (index >= dwarf2_per_objfile->n_comp_units)
1687     {
1688       index -= dwarf2_per_objfile->n_comp_units;
1689       return dwarf2_per_objfile->type_comp_units[index];
1690     }
1691   return dwarf2_per_objfile->all_comp_units[index];
1692 }
1693
1694 /* A helper function that knows how to read a 64-bit value in a way
1695    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1696    otherwise.  */
1697 static int
1698 extract_cu_value (const char *bytes, ULONGEST *result)
1699 {
1700   if (sizeof (ULONGEST) < 8)
1701     {
1702       int i;
1703
1704       /* Ignore the upper 4 bytes if they are all zero.  */
1705       for (i = 0; i < 4; ++i)
1706         if (bytes[i + 4] != 0)
1707           return 0;
1708
1709       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1710     }
1711   else
1712     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1713   return 1;
1714 }
1715
1716 /* Read the CU list from the mapped index, and use it to create all
1717    the CU objects for this objfile.  Return 0 if something went wrong,
1718    1 if everything went ok.  */
1719 static int
1720 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1721                        offset_type cu_list_elements)
1722 {
1723   offset_type i;
1724
1725   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1726   dwarf2_per_objfile->all_comp_units
1727     = obstack_alloc (&objfile->objfile_obstack,
1728                      dwarf2_per_objfile->n_comp_units
1729                      * sizeof (struct dwarf2_per_cu_data *));
1730
1731   for (i = 0; i < cu_list_elements; i += 2)
1732     {
1733       struct dwarf2_per_cu_data *the_cu;
1734       ULONGEST offset, length;
1735
1736       if (!extract_cu_value (cu_list, &offset)
1737           || !extract_cu_value (cu_list + 8, &length))
1738         return 0;
1739       cu_list += 2 * 8;
1740
1741       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1742                                struct dwarf2_per_cu_data);
1743       the_cu->offset = offset;
1744       the_cu->length = length;
1745       the_cu->objfile = objfile;
1746       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1747                                         struct dwarf2_per_cu_quick_data);
1748       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1749     }
1750
1751   return 1;
1752 }
1753
1754 /* Create the signatured type hash table from the index.  */
1755
1756 static int
1757 create_signatured_type_table_from_index (struct objfile *objfile,
1758                                          const gdb_byte *bytes,
1759                                          offset_type elements)
1760 {
1761   offset_type i;
1762   htab_t sig_types_hash;
1763
1764   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1765   dwarf2_per_objfile->type_comp_units
1766     = obstack_alloc (&objfile->objfile_obstack,
1767                      dwarf2_per_objfile->n_type_comp_units
1768                      * sizeof (struct dwarf2_per_cu_data *));
1769
1770   sig_types_hash = allocate_signatured_type_table (objfile);
1771
1772   for (i = 0; i < elements; i += 3)
1773     {
1774       struct signatured_type *type_sig;
1775       ULONGEST offset, type_offset, signature;
1776       void **slot;
1777
1778       if (!extract_cu_value (bytes, &offset)
1779           || !extract_cu_value (bytes + 8, &type_offset))
1780         return 0;
1781       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1782       bytes += 3 * 8;
1783
1784       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1785                                  struct signatured_type);
1786       type_sig->signature = signature;
1787       type_sig->offset = offset;
1788       type_sig->type_offset = type_offset;
1789       type_sig->per_cu.from_debug_types = 1;
1790       type_sig->per_cu.offset = offset;
1791       type_sig->per_cu.objfile = objfile;
1792       type_sig->per_cu.v.quick
1793         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1794                           struct dwarf2_per_cu_quick_data);
1795
1796       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1797       *slot = type_sig;
1798
1799       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1800     }
1801
1802   dwarf2_per_objfile->signatured_types = sig_types_hash;
1803
1804   return 1;
1805 }
1806
1807 /* Read the address map data from the mapped index, and use it to
1808    populate the objfile's psymtabs_addrmap.  */
1809 static void
1810 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1811 {
1812   const gdb_byte *iter, *end;
1813   struct obstack temp_obstack;
1814   struct addrmap *mutable_map;
1815   struct cleanup *cleanup;
1816   CORE_ADDR baseaddr;
1817
1818   obstack_init (&temp_obstack);
1819   cleanup = make_cleanup_obstack_free (&temp_obstack);
1820   mutable_map = addrmap_create_mutable (&temp_obstack);
1821
1822   iter = index->address_table;
1823   end = iter + index->address_table_size;
1824
1825   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1826
1827   while (iter < end)
1828     {
1829       ULONGEST hi, lo, cu_index;
1830       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1831       iter += 8;
1832       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1833       iter += 8;
1834       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1835       iter += 4;
1836       
1837       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1838                          dw2_get_cu (cu_index));
1839     }
1840
1841   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1842                                                     &objfile->objfile_obstack);
1843   do_cleanups (cleanup);
1844 }
1845
1846 /* The hash function for strings in the mapped index.  This is the
1847    same as the hashtab.c hash function, but we keep a separate copy to
1848    maintain control over the implementation.  This is necessary
1849    because the hash function is tied to the format of the mapped index
1850    file.  */
1851 static hashval_t
1852 mapped_index_string_hash (const void *p)
1853 {
1854   const unsigned char *str = (const unsigned char *) p;
1855   hashval_t r = 0;
1856   unsigned char c;
1857
1858   while ((c = *str++) != 0)
1859     r = r * 67 + c - 113;
1860
1861   return r;
1862 }
1863
1864 /* Find a slot in the mapped index INDEX for the object named NAME.
1865    If NAME is found, set *VEC_OUT to point to the CU vector in the
1866    constant pool and return 1.  If NAME cannot be found, return 0.  */
1867 static int
1868 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1869                           offset_type **vec_out)
1870 {
1871   offset_type hash = mapped_index_string_hash (name);
1872   offset_type slot, step;
1873
1874   slot = hash & (index->index_table_slots - 1);
1875   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1876
1877   for (;;)
1878     {
1879       /* Convert a slot number to an offset into the table.  */
1880       offset_type i = 2 * slot;
1881       const char *str;
1882       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1883         return 0;
1884
1885       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1886       if (!strcmp (name, str))
1887         {
1888           *vec_out = (offset_type *) (index->constant_pool
1889                                       + MAYBE_SWAP (index->index_table[i + 1]));
1890           return 1;
1891         }
1892
1893       slot = (slot + step) & (index->index_table_slots - 1);
1894     }
1895 }
1896
1897 /* Read the index file.  If everything went ok, initialize the "quick"
1898    elements of all the CUs and return 1.  Otherwise, return 0.  */
1899 static int
1900 dwarf2_read_index (struct objfile *objfile)
1901 {
1902   char *addr;
1903   struct mapped_index *map;
1904   offset_type *metadata;
1905   const gdb_byte *cu_list;
1906   const gdb_byte *types_list = NULL;
1907   offset_type version, cu_list_elements;
1908   offset_type types_list_elements = 0;
1909   int i;
1910
1911   if (dwarf2_per_objfile->gdb_index.asection == NULL
1912       || dwarf2_per_objfile->gdb_index.size == 0)
1913     return 0;
1914
1915   /* Older elfutils strip versions could keep the section in the main
1916      executable while splitting it for the separate debug info file.  */
1917   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1918        & SEC_HAS_CONTENTS) == 0)
1919     return 0;
1920
1921   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1922
1923   addr = dwarf2_per_objfile->gdb_index.buffer;
1924   /* Version check.  */
1925   version = MAYBE_SWAP (*(offset_type *) addr);
1926   /* Versions earlier than 3 emitted every copy of a psymbol.  This
1927      causes the index to behave very poorly for certain requests.  So,
1928      it seems better to just ignore such indices.  */
1929   if (version < 3)
1930     return 0;
1931
1932   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1933   map->total_size = dwarf2_per_objfile->gdb_index.size;
1934
1935   metadata = (offset_type *) (addr + sizeof (offset_type));
1936
1937   i = 0;
1938   cu_list = addr + MAYBE_SWAP (metadata[i]);
1939   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1940                       / 8);
1941   ++i;
1942
1943   types_list = addr + MAYBE_SWAP (metadata[i]);
1944   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1945                           - MAYBE_SWAP (metadata[i]))
1946                          / 8);
1947   ++i;
1948
1949   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1950   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1951                              - MAYBE_SWAP (metadata[i]));
1952   ++i;
1953
1954   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1955   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1956                              - MAYBE_SWAP (metadata[i]))
1957                             / (2 * sizeof (offset_type)));
1958   ++i;
1959
1960   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1961
1962   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1963     return 0;
1964
1965   if (types_list_elements
1966       && !create_signatured_type_table_from_index (objfile, types_list,
1967                                                    types_list_elements))
1968     return 0;
1969
1970   create_addrmap_from_index (objfile, map);
1971
1972   dwarf2_per_objfile->index_table = map;
1973   dwarf2_per_objfile->using_index = 1;
1974
1975   return 1;
1976 }
1977
1978 /* A helper for the "quick" functions which sets the global
1979    dwarf2_per_objfile according to OBJFILE.  */
1980 static void
1981 dw2_setup (struct objfile *objfile)
1982 {
1983   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1984   gdb_assert (dwarf2_per_objfile);
1985 }
1986
1987 /* A helper for the "quick" functions which attempts to read the line
1988    table for THIS_CU.  */
1989 static void
1990 dw2_require_line_header (struct objfile *objfile,
1991                          struct dwarf2_per_cu_data *this_cu)
1992 {
1993   bfd *abfd = objfile->obfd;
1994   struct line_header *lh = NULL;
1995   struct attribute *attr;
1996   struct cleanup *cleanups;
1997   struct die_info *comp_unit_die;
1998   struct dwarf2_section_info* sec;
1999   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2000   int has_children, i;
2001   struct dwarf2_cu cu;
2002   unsigned int bytes_read, buffer_size;
2003   struct die_reader_specs reader_specs;
2004   char *name, *comp_dir;
2005
2006   if (this_cu->v.quick->read_lines)
2007     return;
2008   this_cu->v.quick->read_lines = 1;
2009
2010   memset (&cu, 0, sizeof (cu));
2011   cu.objfile = objfile;
2012   obstack_init (&cu.comp_unit_obstack);
2013
2014   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2015
2016   if (this_cu->from_debug_types)
2017     sec = &dwarf2_per_objfile->types;
2018   else
2019     sec = &dwarf2_per_objfile->info;
2020   dwarf2_read_section (objfile, sec);
2021   buffer_size = sec->size;
2022   buffer = sec->buffer;
2023   info_ptr = buffer + this_cu->offset;
2024   beg_of_comp_unit = info_ptr;
2025
2026   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2027                                           buffer, buffer_size,
2028                                           abfd);
2029
2030   /* Complete the cu_header.  */
2031   cu.header.offset = beg_of_comp_unit - buffer;
2032   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2033
2034   this_cu->cu = &cu;
2035   cu.per_cu = this_cu;
2036
2037   dwarf2_read_abbrevs (abfd, &cu);
2038   make_cleanup (dwarf2_free_abbrev_table, &cu);
2039
2040   if (this_cu->from_debug_types)
2041     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2042   init_cu_die_reader (&reader_specs, &cu);
2043   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2044                             &has_children);
2045
2046   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2047   if (attr)
2048     {
2049       unsigned int line_offset = DW_UNSND (attr);
2050       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2051     }
2052   if (lh == NULL)
2053     {
2054       do_cleanups (cleanups);
2055       return;
2056     }
2057
2058   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2059
2060   this_cu->v.quick->lines = lh;
2061
2062   this_cu->v.quick->file_names
2063     = obstack_alloc (&objfile->objfile_obstack,
2064                      lh->num_file_names * sizeof (char *));
2065   for (i = 0; i < lh->num_file_names; ++i)
2066     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2067
2068   do_cleanups (cleanups);
2069 }
2070
2071 /* A helper for the "quick" functions which computes and caches the
2072    real path for a given file name from the line table.
2073    dw2_require_line_header must have been called before this is
2074    invoked.  */
2075 static const char *
2076 dw2_require_full_path (struct objfile *objfile,
2077                        struct dwarf2_per_cu_data *per_cu,
2078                        int index)
2079 {
2080   if (!per_cu->v.quick->full_names)
2081     per_cu->v.quick->full_names
2082       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2083                         per_cu->v.quick->lines->num_file_names,
2084                         sizeof (char *));
2085
2086   if (!per_cu->v.quick->full_names[index])
2087     per_cu->v.quick->full_names[index]
2088       = gdb_realpath (per_cu->v.quick->file_names[index]);
2089
2090   return per_cu->v.quick->full_names[index];
2091 }
2092
2093 static struct symtab *
2094 dw2_find_last_source_symtab (struct objfile *objfile)
2095 {
2096   int index;
2097   dw2_setup (objfile);
2098   index = dwarf2_per_objfile->n_comp_units - 1;
2099   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2100 }
2101
2102 static void
2103 dw2_forget_cached_source_info (struct objfile *objfile)
2104 {
2105   int i;
2106
2107   dw2_setup (objfile);
2108   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2109                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2110     {
2111       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2112
2113       if (per_cu->v.quick->full_names)
2114         {
2115           int j;
2116
2117           for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2118             xfree ((void *) per_cu->v.quick->full_names[j]);
2119         }
2120     }
2121 }
2122
2123 static int
2124 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2125                    const char *full_path, const char *real_path,
2126                    struct symtab **result)
2127 {
2128   int i;
2129   int check_basename = lbasename (name) == name;
2130   struct dwarf2_per_cu_data *base_cu = NULL;
2131
2132   dw2_setup (objfile);
2133   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2134                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2135     {
2136       int j;
2137       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2138
2139       if (per_cu->v.quick->symtab)
2140         continue;
2141
2142       dw2_require_line_header (objfile, per_cu);
2143       if (!per_cu->v.quick->lines)
2144         continue;
2145
2146       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2147         {
2148           const char *this_name = per_cu->v.quick->file_names[j];
2149
2150           if (FILENAME_CMP (name, this_name) == 0)
2151             {
2152               *result = dw2_instantiate_symtab (objfile, per_cu);
2153               return 1;
2154             }
2155
2156           if (check_basename && ! base_cu
2157               && FILENAME_CMP (lbasename (this_name), name) == 0)
2158             base_cu = per_cu;
2159
2160           if (full_path != NULL)
2161             {
2162               const char *this_full_name = dw2_require_full_path (objfile,
2163                                                                   per_cu, j);
2164
2165               if (this_full_name
2166                   && FILENAME_CMP (full_path, this_full_name) == 0)
2167                 {
2168                   *result = dw2_instantiate_symtab (objfile, per_cu);
2169                   return 1;
2170                 }
2171             }
2172
2173           if (real_path != NULL)
2174             {
2175               const char *this_full_name = dw2_require_full_path (objfile,
2176                                                                   per_cu, j);
2177
2178               if (this_full_name != NULL)
2179                 {
2180                   char *rp = gdb_realpath (this_full_name);
2181                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2182                     {
2183                       xfree (rp);
2184                       *result = dw2_instantiate_symtab (objfile, per_cu);
2185                       return 1;
2186                     }
2187                   xfree (rp);
2188                 }
2189             }
2190         }
2191     }
2192
2193   if (base_cu)
2194     {
2195       *result = dw2_instantiate_symtab (objfile, base_cu);
2196       return 1;
2197     }
2198
2199   return 0;
2200 }
2201
2202 static struct symtab *
2203 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2204                    const char *name, domain_enum domain)
2205 {
2206   /* We do all the work in the pre_expand_symtabs_matching hook
2207      instead.  */
2208   return NULL;
2209 }
2210
2211 /* A helper function that expands all symtabs that hold an object
2212    named NAME.  */
2213 static void
2214 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2215 {
2216   dw2_setup (objfile);
2217
2218   if (dwarf2_per_objfile->index_table)
2219     {
2220       offset_type *vec;
2221
2222       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2223                                     name, &vec))
2224         {
2225           offset_type i, len = MAYBE_SWAP (*vec);
2226           for (i = 0; i < len; ++i)
2227             {
2228               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2229               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2230
2231               dw2_instantiate_symtab (objfile, per_cu);
2232             }
2233         }
2234     }
2235 }
2236
2237 static void
2238 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2239                                  int kind, const char *name,
2240                                  domain_enum domain)
2241 {
2242   dw2_do_expand_symtabs_matching (objfile, name);
2243 }
2244
2245 static void
2246 dw2_print_stats (struct objfile *objfile)
2247 {
2248   int i, count;
2249
2250   dw2_setup (objfile);
2251   count = 0;
2252   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2253                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2254     {
2255       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2256
2257       if (!per_cu->v.quick->symtab)
2258         ++count;
2259     }
2260   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2261 }
2262
2263 static void
2264 dw2_dump (struct objfile *objfile)
2265 {
2266   /* Nothing worth printing.  */
2267 }
2268
2269 static void
2270 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2271               struct section_offsets *delta)
2272 {
2273   /* There's nothing to relocate here.  */
2274 }
2275
2276 static void
2277 dw2_expand_symtabs_for_function (struct objfile *objfile,
2278                                  const char *func_name)
2279 {
2280   dw2_do_expand_symtabs_matching (objfile, func_name);
2281 }
2282
2283 static void
2284 dw2_expand_all_symtabs (struct objfile *objfile)
2285 {
2286   int i;
2287
2288   dw2_setup (objfile);
2289
2290   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2291                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2292     {
2293       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2294
2295       dw2_instantiate_symtab (objfile, per_cu);
2296     }
2297 }
2298
2299 static void
2300 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2301                                   const char *filename)
2302 {
2303   int i;
2304
2305   dw2_setup (objfile);
2306   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2307                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2308     {
2309       int j;
2310       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2311
2312       if (per_cu->v.quick->symtab)
2313         continue;
2314
2315       dw2_require_line_header (objfile, per_cu);
2316       if (!per_cu->v.quick->lines)
2317         continue;
2318
2319       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2320         {
2321           const char *this_name = per_cu->v.quick->file_names[j];
2322           if (strcmp (this_name, filename) == 0)
2323             {
2324               dw2_instantiate_symtab (objfile, per_cu);
2325               break;
2326             }
2327         }
2328     }
2329 }
2330
2331 static const char *
2332 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2333 {
2334   struct dwarf2_per_cu_data *per_cu;
2335   offset_type *vec;
2336
2337   dw2_setup (objfile);
2338
2339   if (!dwarf2_per_objfile->index_table)
2340     return NULL;
2341
2342   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2343                                  name, &vec))
2344     return NULL;
2345
2346   /* Note that this just looks at the very first one named NAME -- but
2347      actually we are looking for a function.  find_main_filename
2348      should be rewritten so that it doesn't require a custom hook.  It
2349      could just use the ordinary symbol tables.  */
2350   /* vec[0] is the length, which must always be >0.  */
2351   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2352
2353   dw2_require_line_header (objfile, per_cu);
2354   if (!per_cu->v.quick->lines)
2355     return NULL;
2356
2357   return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2358 }
2359
2360 static void
2361 dw2_map_ada_symtabs (struct objfile *objfile,
2362                      int (*wild_match) (const char *, int, const char *),
2363                      int (*is_name_suffix) (const char *),
2364                      void (*callback) (struct objfile *,
2365                                        struct symtab *, void *),
2366                      const char *name, int global,
2367                      domain_enum namespace, int wild,
2368                      void *data)
2369 {
2370   /* For now, we don't support Ada.  Still the function can be called if the
2371      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2372      does not look for non-Ada symbols this function should just return.  */
2373 }
2374
2375 static void
2376 dw2_expand_symtabs_matching (struct objfile *objfile,
2377                              int (*file_matcher) (const char *, void *),
2378                              int (*name_matcher) (const char *, void *),
2379                              domain_enum kind,
2380                              void *data)
2381 {
2382   int i;
2383   offset_type iter;
2384   struct mapped_index *index;
2385
2386   dw2_setup (objfile);
2387   if (!dwarf2_per_objfile->index_table)
2388     return;
2389   index = dwarf2_per_objfile->index_table;
2390
2391   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2392                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2393     {
2394       int j;
2395       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2396
2397       per_cu->v.quick->mark = 0;
2398       if (per_cu->v.quick->symtab)
2399         continue;
2400
2401       dw2_require_line_header (objfile, per_cu);
2402       if (!per_cu->v.quick->lines)
2403         continue;
2404
2405       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2406         {
2407           if (file_matcher (per_cu->v.quick->file_names[j], data))
2408             {
2409               per_cu->v.quick->mark = 1;
2410               break;
2411             }
2412         }
2413     }
2414
2415   for (iter = 0; iter < index->index_table_slots; ++iter)
2416     {
2417       offset_type idx = 2 * iter;
2418       const char *name;
2419       offset_type *vec, vec_len, vec_idx;
2420
2421       if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
2422         continue;
2423
2424       name = index->constant_pool + MAYBE_SWAP (index->index_table[idx]);
2425
2426       if (! (*name_matcher) (name, data))
2427         continue;
2428
2429       /* The name was matched, now expand corresponding CUs that were
2430          marked.  */
2431       vec = (offset_type *) (index->constant_pool
2432                              + MAYBE_SWAP (index->index_table[idx + 1]));
2433       vec_len = MAYBE_SWAP (vec[0]);
2434       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2435         {
2436           struct dwarf2_per_cu_data *per_cu;
2437
2438           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2439           if (per_cu->v.quick->mark)
2440             dw2_instantiate_symtab (objfile, per_cu);
2441         }
2442     }
2443 }
2444
2445 static struct symtab *
2446 dw2_find_pc_sect_symtab (struct objfile *objfile,
2447                          struct minimal_symbol *msymbol,
2448                          CORE_ADDR pc,
2449                          struct obj_section *section,
2450                          int warn_if_readin)
2451 {
2452   struct dwarf2_per_cu_data *data;
2453
2454   dw2_setup (objfile);
2455
2456   if (!objfile->psymtabs_addrmap)
2457     return NULL;
2458
2459   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2460   if (!data)
2461     return NULL;
2462
2463   if (warn_if_readin && data->v.quick->symtab)
2464     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2465              paddress (get_objfile_arch (objfile), pc));
2466
2467   return dw2_instantiate_symtab (objfile, data);
2468 }
2469
2470 static void
2471 dw2_map_symbol_names (struct objfile *objfile,
2472                       void (*fun) (const char *, void *),
2473                       void *data)
2474 {
2475   offset_type iter;
2476   struct mapped_index *index;
2477
2478   dw2_setup (objfile);
2479
2480   if (!dwarf2_per_objfile->index_table)
2481     return;
2482   index = dwarf2_per_objfile->index_table;
2483
2484   for (iter = 0; iter < index->index_table_slots; ++iter)
2485     {
2486       offset_type idx = 2 * iter;
2487       const char *name;
2488       offset_type *vec, vec_len, vec_idx;
2489
2490       if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
2491         continue;
2492
2493       name = (index->constant_pool + MAYBE_SWAP (index->index_table[idx]));
2494
2495       (*fun) (name, data);
2496     }
2497 }
2498
2499 static void
2500 dw2_map_symbol_filenames (struct objfile *objfile,
2501                           void (*fun) (const char *, const char *, void *),
2502                           void *data)
2503 {
2504   int i;
2505
2506   dw2_setup (objfile);
2507   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2508                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2509     {
2510       int j;
2511       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2512
2513       if (per_cu->v.quick->symtab)
2514         continue;
2515
2516       dw2_require_line_header (objfile, per_cu);
2517       if (!per_cu->v.quick->lines)
2518         continue;
2519
2520       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2521         {
2522           const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2523                                                               j);
2524           (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2525         }
2526     }
2527 }
2528
2529 static int
2530 dw2_has_symbols (struct objfile *objfile)
2531 {
2532   return 1;
2533 }
2534
2535 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2536 {
2537   dw2_has_symbols,
2538   dw2_find_last_source_symtab,
2539   dw2_forget_cached_source_info,
2540   dw2_lookup_symtab,
2541   dw2_lookup_symbol,
2542   dw2_pre_expand_symtabs_matching,
2543   dw2_print_stats,
2544   dw2_dump,
2545   dw2_relocate,
2546   dw2_expand_symtabs_for_function,
2547   dw2_expand_all_symtabs,
2548   dw2_expand_symtabs_with_filename,
2549   dw2_find_symbol_file,
2550   dw2_map_ada_symtabs,
2551   dw2_expand_symtabs_matching,
2552   dw2_find_pc_sect_symtab,
2553   dw2_map_symbol_names,
2554   dw2_map_symbol_filenames
2555 };
2556
2557 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2558    file will use psymtabs, or 1 if using the GNU index.  */
2559
2560 int
2561 dwarf2_initialize_objfile (struct objfile *objfile)
2562 {
2563   /* If we're about to read full symbols, don't bother with the
2564      indices.  In this case we also don't care if some other debug
2565      format is making psymtabs, because they are all about to be
2566      expanded anyway.  */
2567   if ((objfile->flags & OBJF_READNOW))
2568     {
2569       int i;
2570
2571       dwarf2_per_objfile->using_index = 1;
2572       create_all_comp_units (objfile);
2573       create_debug_types_hash_table (objfile);
2574
2575       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2576                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2577         {
2578           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2579
2580           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2581                                             struct dwarf2_per_cu_quick_data);
2582         }
2583
2584       /* Return 1 so that gdb sees the "quick" functions.  However,
2585          these functions will be no-ops because we will have expanded
2586          all symtabs.  */
2587       return 1;
2588     }
2589
2590   if (dwarf2_read_index (objfile))
2591     return 1;
2592
2593   dwarf2_build_psymtabs (objfile);
2594   return 0;
2595 }
2596
2597 \f
2598
2599 /* Build a partial symbol table.  */
2600
2601 void
2602 dwarf2_build_psymtabs (struct objfile *objfile)
2603 {
2604   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2605     {
2606       init_psymbol_list (objfile, 1024);
2607     }
2608
2609   dwarf2_build_psymtabs_hard (objfile);
2610 }
2611
2612 /* Return TRUE if OFFSET is within CU_HEADER.  */
2613
2614 static inline int
2615 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2616 {
2617   unsigned int bottom = cu_header->offset;
2618   unsigned int top = (cu_header->offset
2619                       + cu_header->length
2620                       + cu_header->initial_length_size);
2621
2622   return (offset >= bottom && offset < top);
2623 }
2624
2625 /* Read in the comp unit header information from the debug_info at info_ptr.
2626    NOTE: This leaves members offset, first_die_offset to be filled in
2627    by the caller.  */
2628
2629 static gdb_byte *
2630 read_comp_unit_head (struct comp_unit_head *cu_header,
2631                      gdb_byte *info_ptr, bfd *abfd)
2632 {
2633   int signed_addr;
2634   unsigned int bytes_read;
2635
2636   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2637   cu_header->initial_length_size = bytes_read;
2638   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2639   info_ptr += bytes_read;
2640   cu_header->version = read_2_bytes (abfd, info_ptr);
2641   info_ptr += 2;
2642   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2643                                           &bytes_read);
2644   info_ptr += bytes_read;
2645   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2646   info_ptr += 1;
2647   signed_addr = bfd_get_sign_extend_vma (abfd);
2648   if (signed_addr < 0)
2649     internal_error (__FILE__, __LINE__,
2650                     _("read_comp_unit_head: dwarf from non elf file"));
2651   cu_header->signed_addr_p = signed_addr;
2652
2653   return info_ptr;
2654 }
2655
2656 static gdb_byte *
2657 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2658                              gdb_byte *buffer, unsigned int buffer_size,
2659                              bfd *abfd)
2660 {
2661   gdb_byte *beg_of_comp_unit = info_ptr;
2662
2663   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2664
2665   if (header->version != 2 && header->version != 3 && header->version != 4)
2666     error (_("Dwarf Error: wrong version in compilation unit header "
2667            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2668            bfd_get_filename (abfd));
2669
2670   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2671     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2672            "(offset 0x%lx + 6) [in module %s]"),
2673            (long) header->abbrev_offset,
2674            (long) (beg_of_comp_unit - buffer),
2675            bfd_get_filename (abfd));
2676
2677   if (beg_of_comp_unit + header->length + header->initial_length_size
2678       > buffer + buffer_size)
2679     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2680            "(offset 0x%lx + 0) [in module %s]"),
2681            (long) header->length,
2682            (long) (beg_of_comp_unit - buffer),
2683            bfd_get_filename (abfd));
2684
2685   return info_ptr;
2686 }
2687
2688 /* Read in the types comp unit header information from .debug_types entry at
2689    types_ptr.  The result is a pointer to one past the end of the header.  */
2690
2691 static gdb_byte *
2692 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2693                           ULONGEST *signature,
2694                           gdb_byte *types_ptr, bfd *abfd)
2695 {
2696   gdb_byte *initial_types_ptr = types_ptr;
2697
2698   dwarf2_read_section (dwarf2_per_objfile->objfile,
2699                        &dwarf2_per_objfile->types);
2700   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2701
2702   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2703
2704   *signature = read_8_bytes (abfd, types_ptr);
2705   types_ptr += 8;
2706   types_ptr += cu_header->offset_size;
2707   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2708
2709   return types_ptr;
2710 }
2711
2712 /* Allocate a new partial symtab for file named NAME and mark this new
2713    partial symtab as being an include of PST.  */
2714
2715 static void
2716 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2717                                struct objfile *objfile)
2718 {
2719   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2720
2721   subpst->section_offsets = pst->section_offsets;
2722   subpst->textlow = 0;
2723   subpst->texthigh = 0;
2724
2725   subpst->dependencies = (struct partial_symtab **)
2726     obstack_alloc (&objfile->objfile_obstack,
2727                    sizeof (struct partial_symtab *));
2728   subpst->dependencies[0] = pst;
2729   subpst->number_of_dependencies = 1;
2730
2731   subpst->globals_offset = 0;
2732   subpst->n_global_syms = 0;
2733   subpst->statics_offset = 0;
2734   subpst->n_static_syms = 0;
2735   subpst->symtab = NULL;
2736   subpst->read_symtab = pst->read_symtab;
2737   subpst->readin = 0;
2738
2739   /* No private part is necessary for include psymtabs.  This property
2740      can be used to differentiate between such include psymtabs and
2741      the regular ones.  */
2742   subpst->read_symtab_private = NULL;
2743 }
2744
2745 /* Read the Line Number Program data and extract the list of files
2746    included by the source file represented by PST.  Build an include
2747    partial symtab for each of these included files.  */
2748
2749 static void
2750 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2751                                struct die_info *die,
2752                                struct partial_symtab *pst)
2753 {
2754   struct objfile *objfile = cu->objfile;
2755   bfd *abfd = objfile->obfd;
2756   struct line_header *lh = NULL;
2757   struct attribute *attr;
2758
2759   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2760   if (attr)
2761     {
2762       unsigned int line_offset = DW_UNSND (attr);
2763
2764       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2765     }
2766   if (lh == NULL)
2767     return;  /* No linetable, so no includes.  */
2768
2769   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2770   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2771
2772   free_line_header (lh);
2773 }
2774
2775 static hashval_t
2776 hash_type_signature (const void *item)
2777 {
2778   const struct signatured_type *type_sig = item;
2779
2780   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2781   return type_sig->signature;
2782 }
2783
2784 static int
2785 eq_type_signature (const void *item_lhs, const void *item_rhs)
2786 {
2787   const struct signatured_type *lhs = item_lhs;
2788   const struct signatured_type *rhs = item_rhs;
2789
2790   return lhs->signature == rhs->signature;
2791 }
2792
2793 /* Allocate a hash table for signatured types.  */
2794
2795 static htab_t
2796 allocate_signatured_type_table (struct objfile *objfile)
2797 {
2798   return htab_create_alloc_ex (41,
2799                                hash_type_signature,
2800                                eq_type_signature,
2801                                NULL,
2802                                &objfile->objfile_obstack,
2803                                hashtab_obstack_allocate,
2804                                dummy_obstack_deallocate);
2805 }
2806
2807 /* A helper function to add a signatured type CU to a list.  */
2808
2809 static int
2810 add_signatured_type_cu_to_list (void **slot, void *datum)
2811 {
2812   struct signatured_type *sigt = *slot;
2813   struct dwarf2_per_cu_data ***datap = datum;
2814
2815   **datap = &sigt->per_cu;
2816   ++*datap;
2817
2818   return 1;
2819 }
2820
2821 /* Create the hash table of all entries in the .debug_types section.
2822    The result is zero if there is an error (e.g. missing .debug_types section),
2823    otherwise non-zero.  */
2824
2825 static int
2826 create_debug_types_hash_table (struct objfile *objfile)
2827 {
2828   gdb_byte *info_ptr;
2829   htab_t types_htab;
2830   struct dwarf2_per_cu_data **iter;
2831
2832   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2833   info_ptr = dwarf2_per_objfile->types.buffer;
2834
2835   if (info_ptr == NULL)
2836     {
2837       dwarf2_per_objfile->signatured_types = NULL;
2838       return 0;
2839     }
2840
2841   types_htab = allocate_signatured_type_table (objfile);
2842
2843   if (dwarf2_die_debug)
2844     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2845
2846   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2847     {
2848       unsigned int offset;
2849       unsigned int offset_size;
2850       unsigned int type_offset;
2851       unsigned int length, initial_length_size;
2852       unsigned short version;
2853       ULONGEST signature;
2854       struct signatured_type *type_sig;
2855       void **slot;
2856       gdb_byte *ptr = info_ptr;
2857
2858       offset = ptr - dwarf2_per_objfile->types.buffer;
2859
2860       /* We need to read the type's signature in order to build the hash
2861          table, but we don't need to read anything else just yet.  */
2862
2863       /* Sanity check to ensure entire cu is present.  */
2864       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2865       if (ptr + length + initial_length_size
2866           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2867         {
2868           complaint (&symfile_complaints,
2869                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2870           break;
2871         }
2872
2873       offset_size = initial_length_size == 4 ? 4 : 8;
2874       ptr += initial_length_size;
2875       version = bfd_get_16 (objfile->obfd, ptr);
2876       ptr += 2;
2877       ptr += offset_size; /* abbrev offset */
2878       ptr += 1; /* address size */
2879       signature = bfd_get_64 (objfile->obfd, ptr);
2880       ptr += 8;
2881       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2882
2883       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2884       memset (type_sig, 0, sizeof (*type_sig));
2885       type_sig->signature = signature;
2886       type_sig->offset = offset;
2887       type_sig->type_offset = type_offset;
2888       type_sig->per_cu.objfile = objfile;
2889       type_sig->per_cu.from_debug_types = 1;
2890
2891       slot = htab_find_slot (types_htab, type_sig, INSERT);
2892       gdb_assert (slot != NULL);
2893       *slot = type_sig;
2894
2895       if (dwarf2_die_debug)
2896         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2897                             offset, phex (signature, sizeof (signature)));
2898
2899       info_ptr = info_ptr + initial_length_size + length;
2900     }
2901
2902   dwarf2_per_objfile->signatured_types = types_htab;
2903
2904   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2905   dwarf2_per_objfile->type_comp_units
2906     = obstack_alloc (&objfile->objfile_obstack,
2907                      dwarf2_per_objfile->n_type_comp_units
2908                      * sizeof (struct dwarf2_per_cu_data *));
2909   iter = &dwarf2_per_objfile->type_comp_units[0];
2910   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2911   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2912               == dwarf2_per_objfile->n_type_comp_units);
2913
2914   return 1;
2915 }
2916
2917 /* Lookup a signature based type.
2918    Returns NULL if SIG is not present in the table.  */
2919
2920 static struct signatured_type *
2921 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2922 {
2923   struct signatured_type find_entry, *entry;
2924
2925   if (dwarf2_per_objfile->signatured_types == NULL)
2926     {
2927       complaint (&symfile_complaints,
2928                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2929       return 0;
2930     }
2931
2932   find_entry.signature = sig;
2933   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2934   return entry;
2935 }
2936
2937 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2938
2939 static void
2940 init_cu_die_reader (struct die_reader_specs *reader,
2941                     struct dwarf2_cu *cu)
2942 {
2943   reader->abfd = cu->objfile->obfd;
2944   reader->cu = cu;
2945   if (cu->per_cu->from_debug_types)
2946     {
2947       gdb_assert (dwarf2_per_objfile->types.readin);
2948       reader->buffer = dwarf2_per_objfile->types.buffer;
2949     }
2950   else
2951     {
2952       gdb_assert (dwarf2_per_objfile->info.readin);
2953       reader->buffer = dwarf2_per_objfile->info.buffer;
2954     }
2955 }
2956
2957 /* Find the base address of the compilation unit for range lists and
2958    location lists.  It will normally be specified by DW_AT_low_pc.
2959    In DWARF-3 draft 4, the base address could be overridden by
2960    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2961    compilation units with discontinuous ranges.  */
2962
2963 static void
2964 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2965 {
2966   struct attribute *attr;
2967
2968   cu->base_known = 0;
2969   cu->base_address = 0;
2970
2971   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2972   if (attr)
2973     {
2974       cu->base_address = DW_ADDR (attr);
2975       cu->base_known = 1;
2976     }
2977   else
2978     {
2979       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2980       if (attr)
2981         {
2982           cu->base_address = DW_ADDR (attr);
2983           cu->base_known = 1;
2984         }
2985     }
2986 }
2987
2988 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2989    to combine the common parts.
2990    Process a compilation unit for a psymtab.
2991    BUFFER is a pointer to the beginning of the dwarf section buffer,
2992    either .debug_info or debug_types.
2993    INFO_PTR is a pointer to the start of the CU.
2994    Returns a pointer to the next CU.  */
2995
2996 static gdb_byte *
2997 process_psymtab_comp_unit (struct objfile *objfile,
2998                            struct dwarf2_per_cu_data *this_cu,
2999                            gdb_byte *buffer, gdb_byte *info_ptr,
3000                            unsigned int buffer_size)
3001 {
3002   bfd *abfd = objfile->obfd;
3003   gdb_byte *beg_of_comp_unit = info_ptr;
3004   struct die_info *comp_unit_die;
3005   struct partial_symtab *pst;
3006   CORE_ADDR baseaddr;
3007   struct cleanup *back_to_inner;
3008   struct dwarf2_cu cu;
3009   int has_children, has_pc_info;
3010   struct attribute *attr;
3011   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3012   struct die_reader_specs reader_specs;
3013
3014   memset (&cu, 0, sizeof (cu));
3015   cu.objfile = objfile;
3016   obstack_init (&cu.comp_unit_obstack);
3017
3018   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3019
3020   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3021                                           buffer, buffer_size,
3022                                           abfd);
3023
3024   /* Complete the cu_header.  */
3025   cu.header.offset = beg_of_comp_unit - buffer;
3026   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3027
3028   cu.list_in_scope = &file_symbols;
3029
3030   /* If this compilation unit was already read in, free the
3031      cached copy in order to read it in again.  This is
3032      necessary because we skipped some symbols when we first
3033      read in the compilation unit (see load_partial_dies).
3034      This problem could be avoided, but the benefit is
3035      unclear.  */
3036   if (this_cu->cu != NULL)
3037     free_one_cached_comp_unit (this_cu->cu);
3038
3039   /* Note that this is a pointer to our stack frame, being
3040      added to a global data structure.  It will be cleaned up
3041      in free_stack_comp_unit when we finish with this
3042      compilation unit.  */
3043   this_cu->cu = &cu;
3044   cu.per_cu = this_cu;
3045
3046   /* Read the abbrevs for this compilation unit into a table.  */
3047   dwarf2_read_abbrevs (abfd, &cu);
3048   make_cleanup (dwarf2_free_abbrev_table, &cu);
3049
3050   /* Read the compilation unit die.  */
3051   if (this_cu->from_debug_types)
3052     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3053   init_cu_die_reader (&reader_specs, &cu);
3054   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3055                             &has_children);
3056
3057   if (this_cu->from_debug_types)
3058     {
3059       /* offset,length haven't been set yet for type units.  */
3060       this_cu->offset = cu.header.offset;
3061       this_cu->length = cu.header.length + cu.header.initial_length_size;
3062     }
3063   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3064     {
3065       info_ptr = (beg_of_comp_unit + cu.header.length
3066                   + cu.header.initial_length_size);
3067       do_cleanups (back_to_inner);
3068       return info_ptr;
3069     }
3070
3071   /* Set the language we're debugging.  */
3072   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3073   if (attr)
3074     set_cu_language (DW_UNSND (attr), &cu);
3075   else
3076     set_cu_language (language_minimal, &cu);
3077
3078   /* Allocate a new partial symbol table structure.  */
3079   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3080   pst = start_psymtab_common (objfile, objfile->section_offsets,
3081                               (attr != NULL) ? DW_STRING (attr) : "",
3082                               /* TEXTLOW and TEXTHIGH are set below.  */
3083                               0,
3084                               objfile->global_psymbols.next,
3085                               objfile->static_psymbols.next);
3086
3087   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3088   if (attr != NULL)
3089     pst->dirname = DW_STRING (attr);
3090
3091   pst->read_symtab_private = this_cu;
3092
3093   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3094
3095   /* Store the function that reads in the rest of the symbol table */
3096   pst->read_symtab = dwarf2_psymtab_to_symtab;
3097
3098   this_cu->v.psymtab = pst;
3099
3100   dwarf2_find_base_address (comp_unit_die, &cu);
3101
3102   /* Possibly set the default values of LOWPC and HIGHPC from
3103      `DW_AT_ranges'.  */
3104   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3105                                       &best_highpc, &cu, pst);
3106   if (has_pc_info == 1 && best_lowpc < best_highpc)
3107     /* Store the contiguous range if it is not empty; it can be empty for
3108        CUs with no code.  */
3109     addrmap_set_empty (objfile->psymtabs_addrmap,
3110                        best_lowpc + baseaddr,
3111                        best_highpc + baseaddr - 1, pst);
3112
3113   /* Check if comp unit has_children.
3114      If so, read the rest of the partial symbols from this comp unit.
3115      If not, there's no more debug_info for this comp unit. */
3116   if (has_children)
3117     {
3118       struct partial_die_info *first_die;
3119       CORE_ADDR lowpc, highpc;
3120
3121       lowpc = ((CORE_ADDR) -1);
3122       highpc = ((CORE_ADDR) 0);
3123
3124       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3125
3126       scan_partial_symbols (first_die, &lowpc, &highpc,
3127                             ! has_pc_info, &cu);
3128
3129       /* If we didn't find a lowpc, set it to highpc to avoid
3130          complaints from `maint check'.  */
3131       if (lowpc == ((CORE_ADDR) -1))
3132         lowpc = highpc;
3133
3134       /* If the compilation unit didn't have an explicit address range,
3135          then use the information extracted from its child dies.  */
3136       if (! has_pc_info)
3137         {
3138           best_lowpc = lowpc;
3139           best_highpc = highpc;
3140         }
3141     }
3142   pst->textlow = best_lowpc + baseaddr;
3143   pst->texthigh = best_highpc + baseaddr;
3144
3145   pst->n_global_syms = objfile->global_psymbols.next -
3146     (objfile->global_psymbols.list + pst->globals_offset);
3147   pst->n_static_syms = objfile->static_psymbols.next -
3148     (objfile->static_psymbols.list + pst->statics_offset);
3149   sort_pst_symbols (pst);
3150
3151   info_ptr = (beg_of_comp_unit + cu.header.length
3152               + cu.header.initial_length_size);
3153
3154   if (this_cu->from_debug_types)
3155     {
3156       /* It's not clear we want to do anything with stmt lists here.
3157          Waiting to see what gcc ultimately does.  */
3158     }
3159   else
3160     {
3161       /* Get the list of files included in the current compilation unit,
3162          and build a psymtab for each of them.  */
3163       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3164     }
3165
3166   do_cleanups (back_to_inner);
3167
3168   return info_ptr;
3169 }
3170
3171 /* Traversal function for htab_traverse_noresize.
3172    Process one .debug_types comp-unit.  */
3173
3174 static int
3175 process_type_comp_unit (void **slot, void *info)
3176 {
3177   struct signatured_type *entry = (struct signatured_type *) *slot;
3178   struct objfile *objfile = (struct objfile *) info;
3179   struct dwarf2_per_cu_data *this_cu;
3180
3181   this_cu = &entry->per_cu;
3182
3183   gdb_assert (dwarf2_per_objfile->types.readin);
3184   process_psymtab_comp_unit (objfile, this_cu,
3185                              dwarf2_per_objfile->types.buffer,
3186                              dwarf2_per_objfile->types.buffer + entry->offset,
3187                              dwarf2_per_objfile->types.size);
3188
3189   return 1;
3190 }
3191
3192 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3193    Build partial symbol tables for the .debug_types comp-units.  */
3194
3195 static void
3196 build_type_psymtabs (struct objfile *objfile)
3197 {
3198   if (! create_debug_types_hash_table (objfile))
3199     return;
3200
3201   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3202                           process_type_comp_unit, objfile);
3203 }
3204
3205 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3206
3207 static void
3208 psymtabs_addrmap_cleanup (void *o)
3209 {
3210   struct objfile *objfile = o;
3211
3212   objfile->psymtabs_addrmap = NULL;
3213 }
3214
3215 /* Build the partial symbol table by doing a quick pass through the
3216    .debug_info and .debug_abbrev sections.  */
3217
3218 static void
3219 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3220 {
3221   gdb_byte *info_ptr;
3222   struct cleanup *back_to, *addrmap_cleanup;
3223   struct obstack temp_obstack;
3224
3225   dwarf2_per_objfile->reading_partial_symbols = 1;
3226
3227   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3228   info_ptr = dwarf2_per_objfile->info.buffer;
3229
3230   /* Any cached compilation units will be linked by the per-objfile
3231      read_in_chain.  Make sure to free them when we're done.  */
3232   back_to = make_cleanup (free_cached_comp_units, NULL);
3233
3234   build_type_psymtabs (objfile);
3235
3236   create_all_comp_units (objfile);
3237
3238   /* Create a temporary address map on a temporary obstack.  We later
3239      copy this to the final obstack.  */
3240   obstack_init (&temp_obstack);
3241   make_cleanup_obstack_free (&temp_obstack);
3242   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3243   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3244
3245   /* Since the objects we're extracting from .debug_info vary in
3246      length, only the individual functions to extract them (like
3247      read_comp_unit_head and load_partial_die) can really know whether
3248      the buffer is large enough to hold another complete object.
3249
3250      At the moment, they don't actually check that.  If .debug_info
3251      holds just one extra byte after the last compilation unit's dies,
3252      then read_comp_unit_head will happily read off the end of the
3253      buffer.  read_partial_die is similarly casual.  Those functions
3254      should be fixed.
3255
3256      For this loop condition, simply checking whether there's any data
3257      left at all should be sufficient.  */
3258
3259   while (info_ptr < (dwarf2_per_objfile->info.buffer
3260                      + dwarf2_per_objfile->info.size))
3261     {
3262       struct dwarf2_per_cu_data *this_cu;
3263
3264       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3265                                        objfile);
3266
3267       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3268                                             dwarf2_per_objfile->info.buffer,
3269                                             info_ptr,
3270                                             dwarf2_per_objfile->info.size);
3271     }
3272
3273   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3274                                                     &objfile->objfile_obstack);
3275   discard_cleanups (addrmap_cleanup);
3276
3277   do_cleanups (back_to);
3278 }
3279
3280 /* Load the partial DIEs for a secondary CU into memory.  */
3281
3282 static void
3283 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3284                         struct objfile *objfile)
3285 {
3286   bfd *abfd = objfile->obfd;
3287   gdb_byte *info_ptr, *beg_of_comp_unit;
3288   struct die_info *comp_unit_die;
3289   struct dwarf2_cu *cu;
3290   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3291   struct attribute *attr;
3292   int has_children;
3293   struct die_reader_specs reader_specs;
3294   int read_cu = 0;
3295
3296   gdb_assert (! this_cu->from_debug_types);
3297
3298   gdb_assert (dwarf2_per_objfile->info.readin);
3299   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3300   beg_of_comp_unit = info_ptr;
3301
3302   if (this_cu->cu == NULL)
3303     {
3304       cu = alloc_one_comp_unit (objfile);
3305
3306       read_cu = 1;
3307
3308       /* If an error occurs while loading, release our storage.  */
3309       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3310
3311       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3312                                               dwarf2_per_objfile->info.buffer,
3313                                               dwarf2_per_objfile->info.size,
3314                                               abfd);
3315
3316       /* Complete the cu_header.  */
3317       cu->header.offset = this_cu->offset;
3318       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3319
3320       /* Link this compilation unit into the compilation unit tree.  */
3321       this_cu->cu = cu;
3322       cu->per_cu = this_cu;
3323
3324       /* Link this CU into read_in_chain.  */
3325       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3326       dwarf2_per_objfile->read_in_chain = this_cu;
3327     }
3328   else
3329     {
3330       cu = this_cu->cu;
3331       info_ptr += cu->header.first_die_offset;
3332     }
3333
3334   /* Read the abbrevs for this compilation unit into a table.  */
3335   gdb_assert (cu->dwarf2_abbrevs == NULL);
3336   dwarf2_read_abbrevs (abfd, cu);
3337   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3338
3339   /* Read the compilation unit die.  */
3340   init_cu_die_reader (&reader_specs, cu);
3341   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3342                             &has_children);
3343
3344   /* Set the language we're debugging.  */
3345   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3346   if (attr)
3347     set_cu_language (DW_UNSND (attr), cu);
3348   else
3349     set_cu_language (language_minimal, cu);
3350
3351   /* Check if comp unit has_children.
3352      If so, read the rest of the partial symbols from this comp unit.
3353      If not, there's no more debug_info for this comp unit. */
3354   if (has_children)
3355     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3356
3357   do_cleanups (free_abbrevs_cleanup);
3358
3359   if (read_cu)
3360     {
3361       /* We've successfully allocated this compilation unit.  Let our
3362          caller clean it up when finished with it.  */
3363       discard_cleanups (free_cu_cleanup);
3364     }
3365 }
3366
3367 /* Create a list of all compilation units in OBJFILE.  We do this only
3368    if an inter-comp-unit reference is found; presumably if there is one,
3369    there will be many, and one will occur early in the .debug_info section.
3370    So there's no point in building this list incrementally.  */
3371
3372 static void
3373 create_all_comp_units (struct objfile *objfile)
3374 {
3375   int n_allocated;
3376   int n_comp_units;
3377   struct dwarf2_per_cu_data **all_comp_units;
3378   gdb_byte *info_ptr;
3379
3380   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3381   info_ptr = dwarf2_per_objfile->info.buffer;
3382
3383   n_comp_units = 0;
3384   n_allocated = 10;
3385   all_comp_units = xmalloc (n_allocated
3386                             * sizeof (struct dwarf2_per_cu_data *));
3387
3388   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3389     {
3390       unsigned int length, initial_length_size;
3391       struct dwarf2_per_cu_data *this_cu;
3392       unsigned int offset;
3393
3394       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3395
3396       /* Read just enough information to find out where the next
3397          compilation unit is.  */
3398       length = read_initial_length (objfile->obfd, info_ptr,
3399                                     &initial_length_size);
3400
3401       /* Save the compilation unit for later lookup.  */
3402       this_cu = obstack_alloc (&objfile->objfile_obstack,
3403                                sizeof (struct dwarf2_per_cu_data));
3404       memset (this_cu, 0, sizeof (*this_cu));
3405       this_cu->offset = offset;
3406       this_cu->length = length + initial_length_size;
3407       this_cu->objfile = objfile;
3408
3409       if (n_comp_units == n_allocated)
3410         {
3411           n_allocated *= 2;
3412           all_comp_units = xrealloc (all_comp_units,
3413                                      n_allocated
3414                                      * sizeof (struct dwarf2_per_cu_data *));
3415         }
3416       all_comp_units[n_comp_units++] = this_cu;
3417
3418       info_ptr = info_ptr + this_cu->length;
3419     }
3420
3421   dwarf2_per_objfile->all_comp_units
3422     = obstack_alloc (&objfile->objfile_obstack,
3423                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3424   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3425           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3426   xfree (all_comp_units);
3427   dwarf2_per_objfile->n_comp_units = n_comp_units;
3428 }
3429
3430 /* Process all loaded DIEs for compilation unit CU, starting at
3431    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3432    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3433    DW_AT_ranges).  If NEED_PC is set, then this function will set
3434    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3435    and record the covered ranges in the addrmap.  */
3436
3437 static void
3438 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3439                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3440 {
3441   struct partial_die_info *pdi;
3442
3443   /* Now, march along the PDI's, descending into ones which have
3444      interesting children but skipping the children of the other ones,
3445      until we reach the end of the compilation unit.  */
3446
3447   pdi = first_die;
3448
3449   while (pdi != NULL)
3450     {
3451       fixup_partial_die (pdi, cu);
3452
3453       /* Anonymous namespaces or modules have no name but have interesting
3454          children, so we need to look at them.  Ditto for anonymous
3455          enums.  */
3456
3457       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3458           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3459         {
3460           switch (pdi->tag)
3461             {
3462             case DW_TAG_subprogram:
3463               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3464               break;
3465             case DW_TAG_constant:
3466             case DW_TAG_variable:
3467             case DW_TAG_typedef:
3468             case DW_TAG_union_type:
3469               if (!pdi->is_declaration)
3470                 {
3471                   add_partial_symbol (pdi, cu);
3472                 }
3473               break;
3474             case DW_TAG_class_type:
3475             case DW_TAG_interface_type:
3476             case DW_TAG_structure_type:
3477               if (!pdi->is_declaration)
3478                 {
3479                   add_partial_symbol (pdi, cu);
3480                 }
3481               break;
3482             case DW_TAG_enumeration_type:
3483               if (!pdi->is_declaration)
3484                 add_partial_enumeration (pdi, cu);
3485               break;
3486             case DW_TAG_base_type:
3487             case DW_TAG_subrange_type:
3488               /* File scope base type definitions are added to the partial
3489                  symbol table.  */
3490               add_partial_symbol (pdi, cu);
3491               break;
3492             case DW_TAG_namespace:
3493               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3494               break;
3495             case DW_TAG_module:
3496               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3497               break;
3498             default:
3499               break;
3500             }
3501         }
3502
3503       /* If the die has a sibling, skip to the sibling.  */
3504
3505       pdi = pdi->die_sibling;
3506     }
3507 }
3508
3509 /* Functions used to compute the fully scoped name of a partial DIE.
3510
3511    Normally, this is simple.  For C++, the parent DIE's fully scoped
3512    name is concatenated with "::" and the partial DIE's name.  For
3513    Java, the same thing occurs except that "." is used instead of "::".
3514    Enumerators are an exception; they use the scope of their parent
3515    enumeration type, i.e. the name of the enumeration type is not
3516    prepended to the enumerator.
3517
3518    There are two complexities.  One is DW_AT_specification; in this
3519    case "parent" means the parent of the target of the specification,
3520    instead of the direct parent of the DIE.  The other is compilers
3521    which do not emit DW_TAG_namespace; in this case we try to guess
3522    the fully qualified name of structure types from their members'
3523    linkage names.  This must be done using the DIE's children rather
3524    than the children of any DW_AT_specification target.  We only need
3525    to do this for structures at the top level, i.e. if the target of
3526    any DW_AT_specification (if any; otherwise the DIE itself) does not
3527    have a parent.  */
3528
3529 /* Compute the scope prefix associated with PDI's parent, in
3530    compilation unit CU.  The result will be allocated on CU's
3531    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3532    field.  NULL is returned if no prefix is necessary.  */
3533 static char *
3534 partial_die_parent_scope (struct partial_die_info *pdi,
3535                           struct dwarf2_cu *cu)
3536 {
3537   char *grandparent_scope;
3538   struct partial_die_info *parent, *real_pdi;
3539
3540   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3541      then this means the parent of the specification DIE.  */
3542
3543   real_pdi = pdi;
3544   while (real_pdi->has_specification)
3545     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3546
3547   parent = real_pdi->die_parent;
3548   if (parent == NULL)
3549     return NULL;
3550
3551   if (parent->scope_set)
3552     return parent->scope;
3553
3554   fixup_partial_die (parent, cu);
3555
3556   grandparent_scope = partial_die_parent_scope (parent, cu);
3557
3558   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3559      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3560      Work around this problem here.  */
3561   if (cu->language == language_cplus
3562       && parent->tag == DW_TAG_namespace
3563       && strcmp (parent->name, "::") == 0
3564       && grandparent_scope == NULL)
3565     {
3566       parent->scope = NULL;
3567       parent->scope_set = 1;
3568       return NULL;
3569     }
3570
3571   if (parent->tag == DW_TAG_namespace
3572       || parent->tag == DW_TAG_module
3573       || parent->tag == DW_TAG_structure_type
3574       || parent->tag == DW_TAG_class_type
3575       || parent->tag == DW_TAG_interface_type
3576       || parent->tag == DW_TAG_union_type
3577       || parent->tag == DW_TAG_enumeration_type)
3578     {
3579       if (grandparent_scope == NULL)
3580         parent->scope = parent->name;
3581       else
3582         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3583                                          parent->name, 0, cu);
3584     }
3585   else if (parent->tag == DW_TAG_enumerator)
3586     /* Enumerators should not get the name of the enumeration as a prefix.  */
3587     parent->scope = grandparent_scope;
3588   else
3589     {
3590       /* FIXME drow/2004-04-01: What should we be doing with
3591          function-local names?  For partial symbols, we should probably be
3592          ignoring them.  */
3593       complaint (&symfile_complaints,
3594                  _("unhandled containing DIE tag %d for DIE at %d"),
3595                  parent->tag, pdi->offset);
3596       parent->scope = grandparent_scope;
3597     }
3598
3599   parent->scope_set = 1;
3600   return parent->scope;
3601 }
3602
3603 /* Return the fully scoped name associated with PDI, from compilation unit
3604    CU.  The result will be allocated with malloc.  */
3605 static char *
3606 partial_die_full_name (struct partial_die_info *pdi,
3607                        struct dwarf2_cu *cu)
3608 {
3609   char *parent_scope;
3610
3611   /* If this is a template instantiation, we can not work out the
3612      template arguments from partial DIEs.  So, unfortunately, we have
3613      to go through the full DIEs.  At least any work we do building
3614      types here will be reused if full symbols are loaded later.  */
3615   if (pdi->has_template_arguments)
3616     {
3617       fixup_partial_die (pdi, cu);
3618
3619       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3620         {
3621           struct die_info *die;
3622           struct attribute attr;
3623           struct dwarf2_cu *ref_cu = cu;
3624
3625           attr.name = 0;
3626           attr.form = DW_FORM_ref_addr;
3627           attr.u.addr = pdi->offset;
3628           die = follow_die_ref (NULL, &attr, &ref_cu);
3629
3630           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3631         }
3632     }
3633
3634   parent_scope = partial_die_parent_scope (pdi, cu);
3635   if (parent_scope == NULL)
3636     return NULL;
3637   else
3638     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3639 }
3640
3641 static void
3642 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3643 {
3644   struct objfile *objfile = cu->objfile;
3645   CORE_ADDR addr = 0;
3646   char *actual_name = NULL;
3647   const struct partial_symbol *psym = NULL;
3648   CORE_ADDR baseaddr;
3649   int built_actual_name = 0;
3650
3651   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3652
3653   actual_name = partial_die_full_name (pdi, cu);
3654   if (actual_name)
3655     built_actual_name = 1;
3656
3657   if (actual_name == NULL)
3658     actual_name = pdi->name;
3659
3660   switch (pdi->tag)
3661     {
3662     case DW_TAG_subprogram:
3663       if (pdi->is_external || cu->language == language_ada)
3664         {
3665           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3666              of the global scope.  But in Ada, we want to be able to access
3667              nested procedures globally.  So all Ada subprograms are stored
3668              in the global scope.  */
3669           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3670              mst_text, objfile); */
3671           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3672                                       built_actual_name,
3673                                       VAR_DOMAIN, LOC_BLOCK,
3674                                       &objfile->global_psymbols,
3675                                       0, pdi->lowpc + baseaddr,
3676                                       cu->language, objfile);
3677         }
3678       else
3679         {
3680           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3681              mst_file_text, objfile); */
3682           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3683                                       built_actual_name,
3684                                       VAR_DOMAIN, LOC_BLOCK,
3685                                       &objfile->static_psymbols,
3686                                       0, pdi->lowpc + baseaddr,
3687                                       cu->language, objfile);
3688         }
3689       break;
3690     case DW_TAG_constant:
3691       {
3692         struct psymbol_allocation_list *list;
3693
3694         if (pdi->is_external)
3695           list = &objfile->global_psymbols;
3696         else
3697           list = &objfile->static_psymbols;
3698         psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3699                                     built_actual_name, VAR_DOMAIN, LOC_STATIC,
3700                                     list, 0, 0, cu->language, objfile);
3701
3702       }
3703       break;
3704     case DW_TAG_variable:
3705       if (pdi->locdesc)
3706         addr = decode_locdesc (pdi->locdesc, cu);
3707
3708       if (pdi->locdesc
3709           && addr == 0
3710           && !dwarf2_per_objfile->has_section_at_zero)
3711         {
3712           /* A global or static variable may also have been stripped
3713              out by the linker if unused, in which case its address
3714              will be nullified; do not add such variables into partial
3715              symbol table then.  */
3716         }
3717       else if (pdi->is_external)
3718         {
3719           /* Global Variable.
3720              Don't enter into the minimal symbol tables as there is
3721              a minimal symbol table entry from the ELF symbols already.
3722              Enter into partial symbol table if it has a location
3723              descriptor or a type.
3724              If the location descriptor is missing, new_symbol will create
3725              a LOC_UNRESOLVED symbol, the address of the variable will then
3726              be determined from the minimal symbol table whenever the variable
3727              is referenced.
3728              The address for the partial symbol table entry is not
3729              used by GDB, but it comes in handy for debugging partial symbol
3730              table building.  */
3731
3732           if (pdi->locdesc || pdi->has_type)
3733             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3734                                         built_actual_name,
3735                                         VAR_DOMAIN, LOC_STATIC,
3736                                         &objfile->global_psymbols,
3737                                         0, addr + baseaddr,
3738                                         cu->language, objfile);
3739         }
3740       else
3741         {
3742           /* Static Variable. Skip symbols without location descriptors.  */
3743           if (pdi->locdesc == NULL)
3744             {
3745               if (built_actual_name)
3746                 xfree (actual_name);
3747               return;
3748             }
3749           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3750              mst_file_data, objfile); */
3751           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3752                                       built_actual_name,
3753                                       VAR_DOMAIN, LOC_STATIC,
3754                                       &objfile->static_psymbols,
3755                                       0, addr + baseaddr,
3756                                       cu->language, objfile);
3757         }
3758       break;
3759     case DW_TAG_typedef:
3760     case DW_TAG_base_type:
3761     case DW_TAG_subrange_type:
3762       add_psymbol_to_list (actual_name, strlen (actual_name),
3763                            built_actual_name,
3764                            VAR_DOMAIN, LOC_TYPEDEF,
3765                            &objfile->static_psymbols,
3766                            0, (CORE_ADDR) 0, cu->language, objfile);
3767       break;
3768     case DW_TAG_namespace:
3769       add_psymbol_to_list (actual_name, strlen (actual_name),
3770                            built_actual_name,
3771                            VAR_DOMAIN, LOC_TYPEDEF,
3772                            &objfile->global_psymbols,
3773                            0, (CORE_ADDR) 0, cu->language, objfile);
3774       break;
3775     case DW_TAG_class_type:
3776     case DW_TAG_interface_type:
3777     case DW_TAG_structure_type:
3778     case DW_TAG_union_type:
3779     case DW_TAG_enumeration_type:
3780       /* Skip external references.  The DWARF standard says in the section
3781          about "Structure, Union, and Class Type Entries": "An incomplete
3782          structure, union or class type is represented by a structure,
3783          union or class entry that does not have a byte size attribute
3784          and that has a DW_AT_declaration attribute."  */
3785       if (!pdi->has_byte_size && pdi->is_declaration)
3786         {
3787           if (built_actual_name)
3788             xfree (actual_name);
3789           return;
3790         }
3791
3792       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3793          static vs. global.  */
3794       add_psymbol_to_list (actual_name, strlen (actual_name),
3795                            built_actual_name,
3796                            STRUCT_DOMAIN, LOC_TYPEDEF,
3797                            (cu->language == language_cplus
3798                             || cu->language == language_java)
3799                            ? &objfile->global_psymbols
3800                            : &objfile->static_psymbols,
3801                            0, (CORE_ADDR) 0, cu->language, objfile);
3802
3803       break;
3804     case DW_TAG_enumerator:
3805       add_psymbol_to_list (actual_name, strlen (actual_name),
3806                            built_actual_name,
3807                            VAR_DOMAIN, LOC_CONST,
3808                            (cu->language == language_cplus
3809                             || cu->language == language_java)
3810                            ? &objfile->global_psymbols
3811                            : &objfile->static_psymbols,
3812                            0, (CORE_ADDR) 0, cu->language, objfile);
3813       break;
3814     default:
3815       break;
3816     }
3817
3818   if (built_actual_name)
3819     xfree (actual_name);
3820 }
3821
3822 /* Read a partial die corresponding to a namespace; also, add a symbol
3823    corresponding to that namespace to the symbol table.  NAMESPACE is
3824    the name of the enclosing namespace.  */
3825
3826 static void
3827 add_partial_namespace (struct partial_die_info *pdi,
3828                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3829                        int need_pc, struct dwarf2_cu *cu)
3830 {
3831   /* Add a symbol for the namespace.  */
3832
3833   add_partial_symbol (pdi, cu);
3834
3835   /* Now scan partial symbols in that namespace.  */
3836
3837   if (pdi->has_children)
3838     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3839 }
3840
3841 /* Read a partial die corresponding to a Fortran module.  */
3842
3843 static void
3844 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3845                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3846 {
3847   /* Now scan partial symbols in that module.  */
3848
3849   if (pdi->has_children)
3850     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3851 }
3852
3853 /* Read a partial die corresponding to a subprogram and create a partial
3854    symbol for that subprogram.  When the CU language allows it, this
3855    routine also defines a partial symbol for each nested subprogram
3856    that this subprogram contains.
3857
3858    DIE my also be a lexical block, in which case we simply search
3859    recursively for suprograms defined inside that lexical block.
3860    Again, this is only performed when the CU language allows this
3861    type of definitions.  */
3862
3863 static void
3864 add_partial_subprogram (struct partial_die_info *pdi,
3865                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3866                         int need_pc, struct dwarf2_cu *cu)
3867 {
3868   if (pdi->tag == DW_TAG_subprogram)
3869     {
3870       if (pdi->has_pc_info)
3871         {
3872           if (pdi->lowpc < *lowpc)
3873             *lowpc = pdi->lowpc;
3874           if (pdi->highpc > *highpc)
3875             *highpc = pdi->highpc;
3876           if (need_pc)
3877             {
3878               CORE_ADDR baseaddr;
3879               struct objfile *objfile = cu->objfile;
3880
3881               baseaddr = ANOFFSET (objfile->section_offsets,
3882                                    SECT_OFF_TEXT (objfile));
3883               addrmap_set_empty (objfile->psymtabs_addrmap,
3884                                  pdi->lowpc + baseaddr,
3885                                  pdi->highpc - 1 + baseaddr,
3886                                  cu->per_cu->v.psymtab);
3887             }
3888           if (!pdi->is_declaration)
3889             /* Ignore subprogram DIEs that do not have a name, they are
3890                illegal.  Do not emit a complaint at this point, we will
3891                do so when we convert this psymtab into a symtab.  */
3892             if (pdi->name)
3893               add_partial_symbol (pdi, cu);
3894         }
3895     }
3896
3897   if (! pdi->has_children)
3898     return;
3899
3900   if (cu->language == language_ada)
3901     {
3902       pdi = pdi->die_child;
3903       while (pdi != NULL)
3904         {
3905           fixup_partial_die (pdi, cu);
3906           if (pdi->tag == DW_TAG_subprogram
3907               || pdi->tag == DW_TAG_lexical_block)
3908             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3909           pdi = pdi->die_sibling;
3910         }
3911     }
3912 }
3913
3914 /* Read a partial die corresponding to an enumeration type.  */
3915
3916 static void
3917 add_partial_enumeration (struct partial_die_info *enum_pdi,
3918                          struct dwarf2_cu *cu)
3919 {
3920   struct partial_die_info *pdi;
3921
3922   if (enum_pdi->name != NULL)
3923     add_partial_symbol (enum_pdi, cu);
3924
3925   pdi = enum_pdi->die_child;
3926   while (pdi)
3927     {
3928       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3929         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3930       else
3931         add_partial_symbol (pdi, cu);
3932       pdi = pdi->die_sibling;
3933     }
3934 }
3935
3936 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3937    Return the corresponding abbrev, or NULL if the number is zero (indicating
3938    an empty DIE).  In either case *BYTES_READ will be set to the length of
3939    the initial number.  */
3940
3941 static struct abbrev_info *
3942 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3943                  struct dwarf2_cu *cu)
3944 {
3945   bfd *abfd = cu->objfile->obfd;
3946   unsigned int abbrev_number;
3947   struct abbrev_info *abbrev;
3948
3949   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3950
3951   if (abbrev_number == 0)
3952     return NULL;
3953
3954   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3955   if (!abbrev)
3956     {
3957       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3958                       bfd_get_filename (abfd));
3959     }
3960
3961   return abbrev;
3962 }
3963
3964 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3965    Returns a pointer to the end of a series of DIEs, terminated by an empty
3966    DIE.  Any children of the skipped DIEs will also be skipped.  */
3967
3968 static gdb_byte *
3969 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3970 {
3971   struct abbrev_info *abbrev;
3972   unsigned int bytes_read;
3973
3974   while (1)
3975     {
3976       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3977       if (abbrev == NULL)
3978         return info_ptr + bytes_read;
3979       else
3980         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3981     }
3982 }
3983
3984 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3985    INFO_PTR should point just after the initial uleb128 of a DIE, and the
3986    abbrev corresponding to that skipped uleb128 should be passed in
3987    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
3988    children.  */
3989
3990 static gdb_byte *
3991 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3992               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3993 {
3994   unsigned int bytes_read;
3995   struct attribute attr;
3996   bfd *abfd = cu->objfile->obfd;
3997   unsigned int form, i;
3998
3999   for (i = 0; i < abbrev->num_attrs; i++)
4000     {
4001       /* The only abbrev we care about is DW_AT_sibling.  */
4002       if (abbrev->attrs[i].name == DW_AT_sibling)
4003         {
4004           read_attribute (&attr, &abbrev->attrs[i],
4005                           abfd, info_ptr, cu);
4006           if (attr.form == DW_FORM_ref_addr)
4007             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4008           else
4009             return buffer + dwarf2_get_ref_die_offset (&attr);
4010         }
4011
4012       /* If it isn't DW_AT_sibling, skip this attribute.  */
4013       form = abbrev->attrs[i].form;
4014     skip_attribute:
4015       switch (form)
4016         {
4017         case DW_FORM_ref_addr:
4018           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4019              and later it is offset sized.  */
4020           if (cu->header.version == 2)
4021             info_ptr += cu->header.addr_size;
4022           else
4023             info_ptr += cu->header.offset_size;
4024           break;
4025         case DW_FORM_addr:
4026           info_ptr += cu->header.addr_size;
4027           break;
4028         case DW_FORM_data1:
4029         case DW_FORM_ref1:
4030         case DW_FORM_flag:
4031           info_ptr += 1;
4032           break;
4033         case DW_FORM_flag_present:
4034           break;
4035         case DW_FORM_data2:
4036         case DW_FORM_ref2:
4037           info_ptr += 2;
4038           break;
4039         case DW_FORM_data4:
4040         case DW_FORM_ref4:
4041           info_ptr += 4;
4042           break;
4043         case DW_FORM_data8:
4044         case DW_FORM_ref8:
4045         case DW_FORM_sig8:
4046           info_ptr += 8;
4047           break;
4048         case DW_FORM_string:
4049           read_direct_string (abfd, info_ptr, &bytes_read);
4050           info_ptr += bytes_read;
4051           break;
4052         case DW_FORM_sec_offset:
4053         case DW_FORM_strp:
4054           info_ptr += cu->header.offset_size;
4055           break;
4056         case DW_FORM_exprloc:
4057         case DW_FORM_block:
4058           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4059           info_ptr += bytes_read;
4060           break;
4061         case DW_FORM_block1:
4062           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4063           break;
4064         case DW_FORM_block2:
4065           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4066           break;
4067         case DW_FORM_block4:
4068           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4069           break;
4070         case DW_FORM_sdata:
4071         case DW_FORM_udata:
4072         case DW_FORM_ref_udata:
4073           info_ptr = skip_leb128 (abfd, info_ptr);
4074           break;
4075         case DW_FORM_indirect:
4076           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4077           info_ptr += bytes_read;
4078           /* We need to continue parsing from here, so just go back to
4079              the top.  */
4080           goto skip_attribute;
4081
4082         default:
4083           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4084                  dwarf_form_name (form),
4085                  bfd_get_filename (abfd));
4086         }
4087     }
4088
4089   if (abbrev->has_children)
4090     return skip_children (buffer, info_ptr, cu);
4091   else
4092     return info_ptr;
4093 }
4094
4095 /* Locate ORIG_PDI's sibling.
4096    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4097    in BUFFER.  */
4098
4099 static gdb_byte *
4100 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4101                     gdb_byte *buffer, gdb_byte *info_ptr,
4102                     bfd *abfd, struct dwarf2_cu *cu)
4103 {
4104   /* Do we know the sibling already?  */
4105
4106   if (orig_pdi->sibling)
4107     return orig_pdi->sibling;
4108
4109   /* Are there any children to deal with?  */
4110
4111   if (!orig_pdi->has_children)
4112     return info_ptr;
4113
4114   /* Skip the children the long way.  */
4115
4116   return skip_children (buffer, info_ptr, cu);
4117 }
4118
4119 /* Expand this partial symbol table into a full symbol table.  */
4120
4121 static void
4122 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4123 {
4124   if (pst != NULL)
4125     {
4126       if (pst->readin)
4127         {
4128           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4129         }
4130       else
4131         {
4132           if (info_verbose)
4133             {
4134               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4135               gdb_flush (gdb_stdout);
4136             }
4137
4138           /* Restore our global data.  */
4139           dwarf2_per_objfile = objfile_data (pst->objfile,
4140                                              dwarf2_objfile_data_key);
4141
4142           /* If this psymtab is constructed from a debug-only objfile, the
4143              has_section_at_zero flag will not necessarily be correct.  We
4144              can get the correct value for this flag by looking at the data
4145              associated with the (presumably stripped) associated objfile.  */
4146           if (pst->objfile->separate_debug_objfile_backlink)
4147             {
4148               struct dwarf2_per_objfile *dpo_backlink
4149                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4150                                 dwarf2_objfile_data_key);
4151
4152               dwarf2_per_objfile->has_section_at_zero
4153                 = dpo_backlink->has_section_at_zero;
4154             }
4155
4156           dwarf2_per_objfile->reading_partial_symbols = 0;
4157
4158           psymtab_to_symtab_1 (pst);
4159
4160           /* Finish up the debug error message.  */
4161           if (info_verbose)
4162             printf_filtered (_("done.\n"));
4163         }
4164     }
4165 }
4166
4167 /* Add PER_CU to the queue.  */
4168
4169 static void
4170 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4171 {
4172   struct dwarf2_queue_item *item;
4173
4174   per_cu->queued = 1;
4175   item = xmalloc (sizeof (*item));
4176   item->per_cu = per_cu;
4177   item->next = NULL;
4178
4179   if (dwarf2_queue == NULL)
4180     dwarf2_queue = item;
4181   else
4182     dwarf2_queue_tail->next = item;
4183
4184   dwarf2_queue_tail = item;
4185 }
4186
4187 /* Process the queue.  */
4188
4189 static void
4190 process_queue (struct objfile *objfile)
4191 {
4192   struct dwarf2_queue_item *item, *next_item;
4193
4194   /* The queue starts out with one item, but following a DIE reference
4195      may load a new CU, adding it to the end of the queue.  */
4196   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4197     {
4198       if (dwarf2_per_objfile->using_index
4199           ? !item->per_cu->v.quick->symtab
4200           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4201         process_full_comp_unit (item->per_cu);
4202
4203       item->per_cu->queued = 0;
4204       next_item = item->next;
4205       xfree (item);
4206     }
4207
4208   dwarf2_queue_tail = NULL;
4209 }
4210
4211 /* Free all allocated queue entries.  This function only releases anything if
4212    an error was thrown; if the queue was processed then it would have been
4213    freed as we went along.  */
4214
4215 static void
4216 dwarf2_release_queue (void *dummy)
4217 {
4218   struct dwarf2_queue_item *item, *last;
4219
4220   item = dwarf2_queue;
4221   while (item)
4222     {
4223       /* Anything still marked queued is likely to be in an
4224          inconsistent state, so discard it.  */
4225       if (item->per_cu->queued)
4226         {
4227           if (item->per_cu->cu != NULL)
4228             free_one_cached_comp_unit (item->per_cu->cu);
4229           item->per_cu->queued = 0;
4230         }
4231
4232       last = item;
4233       item = item->next;
4234       xfree (last);
4235     }
4236
4237   dwarf2_queue = dwarf2_queue_tail = NULL;
4238 }
4239
4240 /* Read in full symbols for PST, and anything it depends on.  */
4241
4242 static void
4243 psymtab_to_symtab_1 (struct partial_symtab *pst)
4244 {
4245   struct dwarf2_per_cu_data *per_cu;
4246   struct cleanup *back_to;
4247   int i;
4248
4249   for (i = 0; i < pst->number_of_dependencies; i++)
4250     if (!pst->dependencies[i]->readin)
4251       {
4252         /* Inform about additional files that need to be read in.  */
4253         if (info_verbose)
4254           {
4255             /* FIXME: i18n: Need to make this a single string.  */
4256             fputs_filtered (" ", gdb_stdout);
4257             wrap_here ("");
4258             fputs_filtered ("and ", gdb_stdout);
4259             wrap_here ("");
4260             printf_filtered ("%s...", pst->dependencies[i]->filename);
4261             wrap_here ("");     /* Flush output */
4262             gdb_flush (gdb_stdout);
4263           }
4264         psymtab_to_symtab_1 (pst->dependencies[i]);
4265       }
4266
4267   per_cu = pst->read_symtab_private;
4268
4269   if (per_cu == NULL)
4270     {
4271       /* It's an include file, no symbols to read for it.
4272          Everything is in the parent symtab.  */
4273       pst->readin = 1;
4274       return;
4275     }
4276
4277   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4278 }
4279
4280 /* Load the DIEs associated with PER_CU into memory.  */
4281
4282 static void
4283 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4284 {
4285   bfd *abfd = objfile->obfd;
4286   struct dwarf2_cu *cu;
4287   unsigned int offset;
4288   gdb_byte *info_ptr, *beg_of_comp_unit;
4289   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4290   struct attribute *attr;
4291   int read_cu = 0;
4292
4293   gdb_assert (! per_cu->from_debug_types);
4294
4295   /* Set local variables from the partial symbol table info.  */
4296   offset = per_cu->offset;
4297
4298   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4299   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4300   beg_of_comp_unit = info_ptr;
4301
4302   if (per_cu->cu == NULL)
4303     {
4304       cu = alloc_one_comp_unit (objfile);
4305
4306       read_cu = 1;
4307
4308       /* If an error occurs while loading, release our storage.  */
4309       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4310
4311       /* Read in the comp_unit header.  */
4312       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4313
4314       /* Complete the cu_header.  */
4315       cu->header.offset = offset;
4316       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4317
4318       /* Read the abbrevs for this compilation unit.  */
4319       dwarf2_read_abbrevs (abfd, cu);
4320       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4321
4322       /* Link this compilation unit into the compilation unit tree.  */
4323       per_cu->cu = cu;
4324       cu->per_cu = per_cu;
4325
4326       /* Link this CU into read_in_chain.  */
4327       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4328       dwarf2_per_objfile->read_in_chain = per_cu;
4329     }
4330   else
4331     {
4332       cu = per_cu->cu;
4333       info_ptr += cu->header.first_die_offset;
4334     }
4335
4336   cu->dies = read_comp_unit (info_ptr, cu);
4337
4338   /* We try not to read any attributes in this function, because not
4339      all objfiles needed for references have been loaded yet, and symbol
4340      table processing isn't initialized.  But we have to set the CU language,
4341      or we won't be able to build types correctly.  */
4342   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4343   if (attr)
4344     set_cu_language (DW_UNSND (attr), cu);
4345   else
4346     set_cu_language (language_minimal, cu);
4347
4348   /* Similarly, if we do not read the producer, we can not apply
4349      producer-specific interpretation.  */
4350   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4351   if (attr)
4352     cu->producer = DW_STRING (attr);
4353
4354   if (read_cu)
4355     {
4356       do_cleanups (free_abbrevs_cleanup);
4357
4358       /* We've successfully allocated this compilation unit.  Let our
4359          caller clean it up when finished with it.  */
4360       discard_cleanups (free_cu_cleanup);
4361     }
4362 }
4363
4364 /* Add a DIE to the delayed physname list.  */
4365
4366 static void
4367 add_to_method_list (struct type *type, int fnfield_index, int index,
4368                     const char *name, struct die_info *die,
4369                     struct dwarf2_cu *cu)
4370 {
4371   struct delayed_method_info mi;
4372   mi.type = type;
4373   mi.fnfield_index = fnfield_index;
4374   mi.index = index;
4375   mi.name = name;
4376   mi.die = die;
4377   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4378 }
4379
4380 /* A cleanup for freeing the delayed method list.  */
4381
4382 static void
4383 free_delayed_list (void *ptr)
4384 {
4385   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4386   if (cu->method_list != NULL)
4387     {
4388       VEC_free (delayed_method_info, cu->method_list);
4389       cu->method_list = NULL;
4390     }
4391 }
4392
4393 /* Compute the physnames of any methods on the CU's method list.
4394
4395    The computation of method physnames is delayed in order to avoid the
4396    (bad) condition that one of the method's formal parameters is of an as yet
4397    incomplete type.  */
4398
4399 static void
4400 compute_delayed_physnames (struct dwarf2_cu *cu)
4401 {
4402   int i;
4403   struct delayed_method_info *mi;
4404   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4405     {
4406       char *physname;
4407       struct fn_fieldlist *fn_flp
4408         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4409       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4410       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4411     }
4412 }
4413
4414 /* Generate full symbol information for PST and CU, whose DIEs have
4415    already been loaded into memory.  */
4416
4417 static void
4418 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4419 {
4420   struct dwarf2_cu *cu = per_cu->cu;
4421   struct objfile *objfile = per_cu->objfile;
4422   CORE_ADDR lowpc, highpc;
4423   struct symtab *symtab;
4424   struct cleanup *back_to, *delayed_list_cleanup;
4425   CORE_ADDR baseaddr;
4426
4427   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4428
4429   buildsym_init ();
4430   back_to = make_cleanup (really_free_pendings, NULL);
4431   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4432
4433   cu->list_in_scope = &file_symbols;
4434
4435   dwarf2_find_base_address (cu->dies, cu);
4436
4437   /* Do line number decoding in read_file_scope () */
4438   process_die (cu->dies, cu);
4439
4440   /* Now that we have processed all the DIEs in the CU, all the types 
4441      should be complete, and it should now be safe to compute all of the
4442      physnames.  */
4443   compute_delayed_physnames (cu);
4444   do_cleanups (delayed_list_cleanup);
4445
4446   /* Some compilers don't define a DW_AT_high_pc attribute for the
4447      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4448      it, by scanning the DIE's below the compilation unit.  */
4449   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4450
4451   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4452
4453   /* Set symtab language to language from DW_AT_language.
4454      If the compilation is from a C file generated by language preprocessors,
4455      do not set the language if it was already deduced by start_subfile.  */
4456   if (symtab != NULL
4457       && !(cu->language == language_c && symtab->language != language_c))
4458     {
4459       symtab->language = cu->language;
4460     }
4461
4462   if (dwarf2_per_objfile->using_index)
4463     per_cu->v.quick->symtab = symtab;
4464   else
4465     {
4466       struct partial_symtab *pst = per_cu->v.psymtab;
4467       pst->symtab = symtab;
4468       pst->readin = 1;
4469     }
4470
4471   do_cleanups (back_to);
4472 }
4473
4474 /* Process a die and its children.  */
4475
4476 static void
4477 process_die (struct die_info *die, struct dwarf2_cu *cu)
4478 {
4479   switch (die->tag)
4480     {
4481     case DW_TAG_padding:
4482       break;
4483     case DW_TAG_compile_unit:
4484       read_file_scope (die, cu);
4485       break;
4486     case DW_TAG_type_unit:
4487       read_type_unit_scope (die, cu);
4488       break;
4489     case DW_TAG_subprogram:
4490     case DW_TAG_inlined_subroutine:
4491       read_func_scope (die, cu);
4492       break;
4493     case DW_TAG_lexical_block:
4494     case DW_TAG_try_block:
4495     case DW_TAG_catch_block:
4496       read_lexical_block_scope (die, cu);
4497       break;
4498     case DW_TAG_class_type:
4499     case DW_TAG_interface_type:
4500     case DW_TAG_structure_type:
4501     case DW_TAG_union_type:
4502       process_structure_scope (die, cu);
4503       break;
4504     case DW_TAG_enumeration_type:
4505       process_enumeration_scope (die, cu);
4506       break;
4507
4508     /* These dies have a type, but processing them does not create
4509        a symbol or recurse to process the children.  Therefore we can
4510        read them on-demand through read_type_die.  */
4511     case DW_TAG_subroutine_type:
4512     case DW_TAG_set_type:
4513     case DW_TAG_array_type:
4514     case DW_TAG_pointer_type:
4515     case DW_TAG_ptr_to_member_type:
4516     case DW_TAG_reference_type:
4517     case DW_TAG_string_type:
4518       break;
4519
4520     case DW_TAG_base_type:
4521     case DW_TAG_subrange_type:
4522     case DW_TAG_typedef:
4523       /* Add a typedef symbol for the type definition, if it has a
4524          DW_AT_name.  */
4525       new_symbol (die, read_type_die (die, cu), cu);
4526       break;
4527     case DW_TAG_common_block:
4528       read_common_block (die, cu);
4529       break;
4530     case DW_TAG_common_inclusion:
4531       break;
4532     case DW_TAG_namespace:
4533       processing_has_namespace_info = 1;
4534       read_namespace (die, cu);
4535       break;
4536     case DW_TAG_module:
4537       processing_has_namespace_info = 1;
4538       read_module (die, cu);
4539       break;
4540     case DW_TAG_imported_declaration:
4541     case DW_TAG_imported_module:
4542       processing_has_namespace_info = 1;
4543       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4544                                  || cu->language != language_fortran))
4545         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4546                    dwarf_tag_name (die->tag));
4547       read_import_statement (die, cu);
4548       break;
4549     default:
4550       new_symbol (die, NULL, cu);
4551       break;
4552     }
4553 }
4554
4555 /* A helper function for dwarf2_compute_name which determines whether DIE
4556    needs to have the name of the scope prepended to the name listed in the
4557    die.  */
4558
4559 static int
4560 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4561 {
4562   struct attribute *attr;
4563
4564   switch (die->tag)
4565     {
4566     case DW_TAG_namespace:
4567     case DW_TAG_typedef:
4568     case DW_TAG_class_type:
4569     case DW_TAG_interface_type:
4570     case DW_TAG_structure_type:
4571     case DW_TAG_union_type:
4572     case DW_TAG_enumeration_type:
4573     case DW_TAG_enumerator:
4574     case DW_TAG_subprogram:
4575     case DW_TAG_member:
4576       return 1;
4577
4578     case DW_TAG_variable:
4579     case DW_TAG_constant:
4580       /* We only need to prefix "globally" visible variables.  These include
4581          any variable marked with DW_AT_external or any variable that
4582          lives in a namespace.  [Variables in anonymous namespaces
4583          require prefixing, but they are not DW_AT_external.]  */
4584
4585       if (dwarf2_attr (die, DW_AT_specification, cu))
4586         {
4587           struct dwarf2_cu *spec_cu = cu;
4588
4589           return die_needs_namespace (die_specification (die, &spec_cu),
4590                                       spec_cu);
4591         }
4592
4593       attr = dwarf2_attr (die, DW_AT_external, cu);
4594       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4595           && die->parent->tag != DW_TAG_module)
4596         return 0;
4597       /* A variable in a lexical block of some kind does not need a
4598          namespace, even though in C++ such variables may be external
4599          and have a mangled name.  */
4600       if (die->parent->tag ==  DW_TAG_lexical_block
4601           || die->parent->tag ==  DW_TAG_try_block
4602           || die->parent->tag ==  DW_TAG_catch_block
4603           || die->parent->tag == DW_TAG_subprogram)
4604         return 0;
4605       return 1;
4606
4607     default:
4608       return 0;
4609     }
4610 }
4611
4612 /* Retrieve the last character from a mem_file.  */
4613
4614 static void
4615 do_ui_file_peek_last (void *object, const char *buffer, long length)
4616 {
4617   char *last_char_p = (char *) object;
4618
4619   if (length > 0)
4620     *last_char_p = buffer[length - 1];
4621 }
4622
4623 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4624    compute the physname for the object, which include a method's
4625    formal parameters (C++/Java) and return type (Java).
4626
4627    For Ada, return the DIE's linkage name rather than the fully qualified
4628    name.  PHYSNAME is ignored..
4629
4630    The result is allocated on the objfile_obstack and canonicalized.  */
4631
4632 static const char *
4633 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4634                      int physname)
4635 {
4636   if (name == NULL)
4637     name = dwarf2_name (die, cu);
4638
4639   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4640      compute it by typename_concat inside GDB.  */
4641   if (cu->language == language_ada
4642       || (cu->language == language_fortran && physname))
4643     {
4644       /* For Ada unit, we prefer the linkage name over the name, as
4645          the former contains the exported name, which the user expects
4646          to be able to reference.  Ideally, we want the user to be able
4647          to reference this entity using either natural or linkage name,
4648          but we haven't started looking at this enhancement yet.  */
4649       struct attribute *attr;
4650
4651       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4652       if (attr == NULL)
4653         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4654       if (attr && DW_STRING (attr))
4655         return DW_STRING (attr);
4656     }
4657
4658   /* These are the only languages we know how to qualify names in.  */
4659   if (name != NULL
4660       && (cu->language == language_cplus || cu->language == language_java
4661           || cu->language == language_fortran))
4662     {
4663       if (die_needs_namespace (die, cu))
4664         {
4665           long length;
4666           char *prefix;
4667           struct ui_file *buf;
4668
4669           prefix = determine_prefix (die, cu);
4670           buf = mem_fileopen ();
4671           if (*prefix != '\0')
4672             {
4673               char *prefixed_name = typename_concat (NULL, prefix, name,
4674                                                      physname, cu);
4675
4676               fputs_unfiltered (prefixed_name, buf);
4677               xfree (prefixed_name);
4678             }
4679           else
4680             fputs_unfiltered (name ? name : "", buf);
4681
4682           /* Template parameters may be specified in the DIE's DW_AT_name, or
4683              as children with DW_TAG_template_type_param or
4684              DW_TAG_value_type_param.  If the latter, add them to the name
4685              here.  If the name already has template parameters, then
4686              skip this step; some versions of GCC emit both, and
4687              it is more efficient to use the pre-computed name.
4688
4689              Something to keep in mind about this process: it is very
4690              unlikely, or in some cases downright impossible, to produce
4691              something that will match the mangled name of a function.
4692              If the definition of the function has the same debug info,
4693              we should be able to match up with it anyway.  But fallbacks
4694              using the minimal symbol, for instance to find a method
4695              implemented in a stripped copy of libstdc++, will not work.
4696              If we do not have debug info for the definition, we will have to
4697              match them up some other way.
4698
4699              When we do name matching there is a related problem with function
4700              templates; two instantiated function templates are allowed to
4701              differ only by their return types, which we do not add here.  */
4702
4703           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4704             {
4705               struct attribute *attr;
4706               struct die_info *child;
4707               int first = 1;
4708
4709               die->building_fullname = 1;
4710
4711               for (child = die->child; child != NULL; child = child->sibling)
4712                 {
4713                   struct type *type;
4714                   long value;
4715                   gdb_byte *bytes;
4716                   struct dwarf2_locexpr_baton *baton;
4717                   struct value *v;
4718
4719                   if (child->tag != DW_TAG_template_type_param
4720                       && child->tag != DW_TAG_template_value_param)
4721                     continue;
4722
4723                   if (first)
4724                     {
4725                       fputs_unfiltered ("<", buf);
4726                       first = 0;
4727                     }
4728                   else
4729                     fputs_unfiltered (", ", buf);
4730
4731                   attr = dwarf2_attr (child, DW_AT_type, cu);
4732                   if (attr == NULL)
4733                     {
4734                       complaint (&symfile_complaints,
4735                                  _("template parameter missing DW_AT_type"));
4736                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4737                       continue;
4738                     }
4739                   type = die_type (child, cu);
4740
4741                   if (child->tag == DW_TAG_template_type_param)
4742                     {
4743                       c_print_type (type, "", buf, -1, 0);
4744                       continue;
4745                     }
4746
4747                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4748                   if (attr == NULL)
4749                     {
4750                       complaint (&symfile_complaints,
4751                                  _("template parameter missing DW_AT_const_value"));
4752                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4753                       continue;
4754                     }
4755
4756                   dwarf2_const_value_attr (attr, type, name,
4757                                            &cu->comp_unit_obstack, cu,
4758                                            &value, &bytes, &baton);
4759
4760                   if (TYPE_NOSIGN (type))
4761                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4762                        changed, this can use value_print instead.  */
4763                     c_printchar (value, type, buf);
4764                   else
4765                     {
4766                       struct value_print_options opts;
4767
4768                       if (baton != NULL)
4769                         v = dwarf2_evaluate_loc_desc (type, NULL,
4770                                                       baton->data,
4771                                                       baton->size,
4772                                                       baton->per_cu);
4773                       else if (bytes != NULL)
4774                         {
4775                           v = allocate_value (type);
4776                           memcpy (value_contents_writeable (v), bytes,
4777                                   TYPE_LENGTH (type));
4778                         }
4779                       else
4780                         v = value_from_longest (type, value);
4781
4782                       /* Specify decimal so that we do not depend on the radix.  */
4783                       get_formatted_print_options (&opts, 'd');
4784                       opts.raw = 1;
4785                       value_print (v, buf, &opts);
4786                       release_value (v);
4787                       value_free (v);
4788                     }
4789                 }
4790
4791               die->building_fullname = 0;
4792
4793               if (!first)
4794                 {
4795                   /* Close the argument list, with a space if necessary
4796                      (nested templates).  */
4797                   char last_char = '\0';
4798                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4799                   if (last_char == '>')
4800                     fputs_unfiltered (" >", buf);
4801                   else
4802                     fputs_unfiltered (">", buf);
4803                 }
4804             }
4805
4806           /* For Java and C++ methods, append formal parameter type
4807              information, if PHYSNAME.  */
4808
4809           if (physname && die->tag == DW_TAG_subprogram
4810               && (cu->language == language_cplus
4811                   || cu->language == language_java))
4812             {
4813               struct type *type = read_type_die (die, cu);
4814
4815               c_type_print_args (type, buf, 0, cu->language);
4816
4817               if (cu->language == language_java)
4818                 {
4819                   /* For java, we must append the return type to method
4820                      names. */
4821                   if (die->tag == DW_TAG_subprogram)
4822                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4823                                      0, 0);
4824                 }
4825               else if (cu->language == language_cplus)
4826                 {
4827                   /* Assume that an artificial first parameter is
4828                      "this", but do not crash if it is not.  RealView
4829                      marks unnamed (and thus unused) parameters as
4830                      artificial; there is no way to differentiate
4831                      the two cases.  */
4832                   if (TYPE_NFIELDS (type) > 0
4833                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4834                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4835                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4836                     fputs_unfiltered (" const", buf);
4837                 }
4838             }
4839
4840           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4841                                        &length);
4842           ui_file_delete (buf);
4843
4844           if (cu->language == language_cplus)
4845             {
4846               char *cname
4847                 = dwarf2_canonicalize_name (name, cu,
4848                                             &cu->objfile->objfile_obstack);
4849
4850               if (cname != NULL)
4851                 name = cname;
4852             }
4853         }
4854     }
4855
4856   return name;
4857 }
4858
4859 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4860    If scope qualifiers are appropriate they will be added.  The result
4861    will be allocated on the objfile_obstack, or NULL if the DIE does
4862    not have a name.  NAME may either be from a previous call to
4863    dwarf2_name or NULL.
4864
4865    The output string will be canonicalized (if C++/Java). */
4866
4867 static const char *
4868 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4869 {
4870   return dwarf2_compute_name (name, die, cu, 0);
4871 }
4872
4873 /* Construct a physname for the given DIE in CU.  NAME may either be
4874    from a previous call to dwarf2_name or NULL.  The result will be
4875    allocated on the objfile_objstack or NULL if the DIE does not have a
4876    name.
4877
4878    The output string will be canonicalized (if C++/Java).  */
4879
4880 static const char *
4881 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4882 {
4883   return dwarf2_compute_name (name, die, cu, 1);
4884 }
4885
4886 /* Read the import statement specified by the given die and record it.  */
4887
4888 static void
4889 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4890 {
4891   struct attribute *import_attr;
4892   struct die_info *imported_die;
4893   struct dwarf2_cu *imported_cu;
4894   const char *imported_name;
4895   const char *imported_name_prefix;
4896   const char *canonical_name;
4897   const char *import_alias;
4898   const char *imported_declaration = NULL;
4899   const char *import_prefix;
4900
4901   char *temp;
4902
4903   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4904   if (import_attr == NULL)
4905     {
4906       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4907                  dwarf_tag_name (die->tag));
4908       return;
4909     }
4910
4911   imported_cu = cu;
4912   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4913   imported_name = dwarf2_name (imported_die, imported_cu);
4914   if (imported_name == NULL)
4915     {
4916       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4917
4918         The import in the following code:
4919         namespace A
4920           {
4921             typedef int B;
4922           }
4923
4924         int main ()
4925           {
4926             using A::B;
4927             B b;
4928             return b;
4929           }
4930
4931         ...
4932          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4933             <52>   DW_AT_decl_file   : 1
4934             <53>   DW_AT_decl_line   : 6
4935             <54>   DW_AT_import      : <0x75>
4936          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4937             <59>   DW_AT_name        : B
4938             <5b>   DW_AT_decl_file   : 1
4939             <5c>   DW_AT_decl_line   : 2
4940             <5d>   DW_AT_type        : <0x6e>
4941         ...
4942          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4943             <76>   DW_AT_byte_size   : 4
4944             <77>   DW_AT_encoding    : 5        (signed)
4945
4946         imports the wrong die ( 0x75 instead of 0x58 ).
4947         This case will be ignored until the gcc bug is fixed.  */
4948       return;
4949     }
4950
4951   /* Figure out the local name after import.  */
4952   import_alias = dwarf2_name (die, cu);
4953
4954   /* Figure out where the statement is being imported to.  */
4955   import_prefix = determine_prefix (die, cu);
4956
4957   /* Figure out what the scope of the imported die is and prepend it
4958      to the name of the imported die.  */
4959   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4960
4961   if (imported_die->tag != DW_TAG_namespace
4962       && imported_die->tag != DW_TAG_module)
4963     {
4964       imported_declaration = imported_name;
4965       canonical_name = imported_name_prefix;
4966     }
4967   else if (strlen (imported_name_prefix) > 0)
4968     {
4969       temp = alloca (strlen (imported_name_prefix)
4970                      + 2 + strlen (imported_name) + 1);
4971       strcpy (temp, imported_name_prefix);
4972       strcat (temp, "::");
4973       strcat (temp, imported_name);
4974       canonical_name = temp;
4975     }
4976   else
4977     canonical_name = imported_name;
4978
4979   cp_add_using_directive (import_prefix,
4980                           canonical_name,
4981                           import_alias,
4982                           imported_declaration,
4983                           &cu->objfile->objfile_obstack);
4984 }
4985
4986 static void
4987 initialize_cu_func_list (struct dwarf2_cu *cu)
4988 {
4989   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4990 }
4991
4992 static void
4993 free_cu_line_header (void *arg)
4994 {
4995   struct dwarf2_cu *cu = arg;
4996
4997   free_line_header (cu->line_header);
4998   cu->line_header = NULL;
4999 }
5000
5001 static void
5002 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5003                          char **name, char **comp_dir)
5004 {
5005   struct attribute *attr;
5006
5007   *name = NULL;
5008   *comp_dir = NULL;
5009
5010   /* Find the filename.  Do not use dwarf2_name here, since the filename
5011      is not a source language identifier.  */
5012   attr = dwarf2_attr (die, DW_AT_name, cu);
5013   if (attr)
5014     {
5015       *name = DW_STRING (attr);
5016     }
5017
5018   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5019   if (attr)
5020     *comp_dir = DW_STRING (attr);
5021   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5022     {
5023       *comp_dir = ldirname (*name);
5024       if (*comp_dir != NULL)
5025         make_cleanup (xfree, *comp_dir);
5026     }
5027   if (*comp_dir != NULL)
5028     {
5029       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5030          directory, get rid of it.  */
5031       char *cp = strchr (*comp_dir, ':');
5032
5033       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5034         *comp_dir = cp + 1;
5035     }
5036
5037   if (*name == NULL)
5038     *name = "<unknown>";
5039 }
5040
5041 static void
5042 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5043 {
5044   struct objfile *objfile = cu->objfile;
5045   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5046   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5047   CORE_ADDR highpc = ((CORE_ADDR) 0);
5048   struct attribute *attr;
5049   char *name = NULL;
5050   char *comp_dir = NULL;
5051   struct die_info *child_die;
5052   bfd *abfd = objfile->obfd;
5053   struct line_header *line_header = 0;
5054   CORE_ADDR baseaddr;
5055
5056   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5057
5058   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5059
5060   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5061      from finish_block.  */
5062   if (lowpc == ((CORE_ADDR) -1))
5063     lowpc = highpc;
5064   lowpc += baseaddr;
5065   highpc += baseaddr;
5066
5067   find_file_and_directory (die, cu, &name, &comp_dir);
5068
5069   attr = dwarf2_attr (die, DW_AT_language, cu);
5070   if (attr)
5071     {
5072       set_cu_language (DW_UNSND (attr), cu);
5073     }
5074
5075   attr = dwarf2_attr (die, DW_AT_producer, cu);
5076   if (attr)
5077     cu->producer = DW_STRING (attr);
5078
5079   /* We assume that we're processing GCC output. */
5080   processing_gcc_compilation = 2;
5081
5082   processing_has_namespace_info = 0;
5083
5084   start_symtab (name, comp_dir, lowpc);
5085   record_debugformat ("DWARF 2");
5086   record_producer (cu->producer);
5087
5088   initialize_cu_func_list (cu);
5089
5090   /* Decode line number information if present.  We do this before
5091      processing child DIEs, so that the line header table is available
5092      for DW_AT_decl_file.  */
5093   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5094   if (attr)
5095     {
5096       unsigned int line_offset = DW_UNSND (attr);
5097       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5098       if (line_header)
5099         {
5100           cu->line_header = line_header;
5101           make_cleanup (free_cu_line_header, cu);
5102           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5103         }
5104     }
5105
5106   /* Process all dies in compilation unit.  */
5107   if (die->child != NULL)
5108     {
5109       child_die = die->child;
5110       while (child_die && child_die->tag)
5111         {
5112           process_die (child_die, cu);
5113           child_die = sibling_die (child_die);
5114         }
5115     }
5116
5117   /* Decode macro information, if present.  Dwarf 2 macro information
5118      refers to information in the line number info statement program
5119      header, so we can only read it if we've read the header
5120      successfully.  */
5121   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5122   if (attr && line_header)
5123     {
5124       unsigned int macro_offset = DW_UNSND (attr);
5125
5126       dwarf_decode_macros (line_header, macro_offset,
5127                            comp_dir, abfd, cu);
5128     }
5129   do_cleanups (back_to);
5130 }
5131
5132 /* For TUs we want to skip the first top level sibling if it's not the
5133    actual type being defined by this TU.  In this case the first top
5134    level sibling is there to provide context only.  */
5135
5136 static void
5137 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5138 {
5139   struct objfile *objfile = cu->objfile;
5140   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5141   CORE_ADDR lowpc;
5142   struct attribute *attr;
5143   char *name = NULL;
5144   char *comp_dir = NULL;
5145   struct die_info *child_die;
5146   bfd *abfd = objfile->obfd;
5147
5148   /* start_symtab needs a low pc, but we don't really have one.
5149      Do what read_file_scope would do in the absence of such info.  */
5150   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5151
5152   /* Find the filename.  Do not use dwarf2_name here, since the filename
5153      is not a source language identifier.  */
5154   attr = dwarf2_attr (die, DW_AT_name, cu);
5155   if (attr)
5156     name = DW_STRING (attr);
5157
5158   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5159   if (attr)
5160     comp_dir = DW_STRING (attr);
5161   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5162     {
5163       comp_dir = ldirname (name);
5164       if (comp_dir != NULL)
5165         make_cleanup (xfree, comp_dir);
5166     }
5167
5168   if (name == NULL)
5169     name = "<unknown>";
5170
5171   attr = dwarf2_attr (die, DW_AT_language, cu);
5172   if (attr)
5173     set_cu_language (DW_UNSND (attr), cu);
5174
5175   /* This isn't technically needed today.  It is done for symmetry
5176      with read_file_scope.  */
5177   attr = dwarf2_attr (die, DW_AT_producer, cu);
5178   if (attr)
5179     cu->producer = DW_STRING (attr);
5180
5181   /* We assume that we're processing GCC output. */
5182   processing_gcc_compilation = 2;
5183
5184   processing_has_namespace_info = 0;
5185
5186   start_symtab (name, comp_dir, lowpc);
5187   record_debugformat ("DWARF 2");
5188   record_producer (cu->producer);
5189
5190   /* Process the dies in the type unit.  */
5191   if (die->child == NULL)
5192     {
5193       dump_die_for_error (die);
5194       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5195              bfd_get_filename (abfd));
5196     }
5197
5198   child_die = die->child;
5199
5200   while (child_die && child_die->tag)
5201     {
5202       process_die (child_die, cu);
5203
5204       child_die = sibling_die (child_die);
5205     }
5206
5207   do_cleanups (back_to);
5208 }
5209
5210 static void
5211 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5212                      struct dwarf2_cu *cu)
5213 {
5214   struct function_range *thisfn;
5215
5216   thisfn = (struct function_range *)
5217     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5218   thisfn->name = name;
5219   thisfn->lowpc = lowpc;
5220   thisfn->highpc = highpc;
5221   thisfn->seen_line = 0;
5222   thisfn->next = NULL;
5223
5224   if (cu->last_fn == NULL)
5225       cu->first_fn = thisfn;
5226   else
5227       cu->last_fn->next = thisfn;
5228
5229   cu->last_fn = thisfn;
5230 }
5231
5232 /* qsort helper for inherit_abstract_dies.  */
5233
5234 static int
5235 unsigned_int_compar (const void *ap, const void *bp)
5236 {
5237   unsigned int a = *(unsigned int *) ap;
5238   unsigned int b = *(unsigned int *) bp;
5239
5240   return (a > b) - (b > a);
5241 }
5242
5243 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5244    Inherit only the children of the DW_AT_abstract_origin DIE not being already
5245    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
5246
5247 static void
5248 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5249 {
5250   struct die_info *child_die;
5251   unsigned die_children_count;
5252   /* CU offsets which were referenced by children of the current DIE.  */
5253   unsigned *offsets;
5254   unsigned *offsets_end, *offsetp;
5255   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5256   struct die_info *origin_die;
5257   /* Iterator of the ORIGIN_DIE children.  */
5258   struct die_info *origin_child_die;
5259   struct cleanup *cleanups;
5260   struct attribute *attr;
5261   struct dwarf2_cu *origin_cu;
5262   struct pending **origin_previous_list_in_scope;
5263
5264   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5265   if (!attr)
5266     return;
5267
5268   /* Note that following die references may follow to a die in a
5269      different cu.  */
5270
5271   origin_cu = cu;
5272   origin_die = follow_die_ref (die, attr, &origin_cu);
5273
5274   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5275      symbols in.  */
5276   origin_previous_list_in_scope = origin_cu->list_in_scope;
5277   origin_cu->list_in_scope = cu->list_in_scope;
5278
5279   if (die->tag != origin_die->tag
5280       && !(die->tag == DW_TAG_inlined_subroutine
5281            && origin_die->tag == DW_TAG_subprogram))
5282     complaint (&symfile_complaints,
5283                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5284                die->offset, origin_die->offset);
5285
5286   child_die = die->child;
5287   die_children_count = 0;
5288   while (child_die && child_die->tag)
5289     {
5290       child_die = sibling_die (child_die);
5291       die_children_count++;
5292     }
5293   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5294   cleanups = make_cleanup (xfree, offsets);
5295
5296   offsets_end = offsets;
5297   child_die = die->child;
5298   while (child_die && child_die->tag)
5299     {
5300       /* For each CHILD_DIE, find the corresponding child of
5301          ORIGIN_DIE.  If there is more than one layer of
5302          DW_AT_abstract_origin, follow them all; there shouldn't be,
5303          but GCC versions at least through 4.4 generate this (GCC PR
5304          40573).  */
5305       struct die_info *child_origin_die = child_die;
5306       struct dwarf2_cu *child_origin_cu = cu;
5307
5308       while (1)
5309         {
5310           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5311                               child_origin_cu);
5312           if (attr == NULL)
5313             break;
5314           child_origin_die = follow_die_ref (child_origin_die, attr,
5315                                              &child_origin_cu);
5316         }
5317
5318       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5319          counterpart may exist.  */
5320       if (child_origin_die != child_die)
5321         {
5322           if (child_die->tag != child_origin_die->tag
5323               && !(child_die->tag == DW_TAG_inlined_subroutine
5324                    && child_origin_die->tag == DW_TAG_subprogram))
5325             complaint (&symfile_complaints,
5326                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5327                          "different tags"), child_die->offset,
5328                        child_origin_die->offset);
5329           if (child_origin_die->parent != origin_die)
5330             complaint (&symfile_complaints,
5331                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5332                          "different parents"), child_die->offset,
5333                        child_origin_die->offset);
5334           else
5335             *offsets_end++ = child_origin_die->offset;
5336         }
5337       child_die = sibling_die (child_die);
5338     }
5339   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5340          unsigned_int_compar);
5341   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5342     if (offsetp[-1] == *offsetp)
5343       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5344                                         "to DIE 0x%x as their abstract origin"),
5345                  die->offset, *offsetp);
5346
5347   offsetp = offsets;
5348   origin_child_die = origin_die->child;
5349   while (origin_child_die && origin_child_die->tag)
5350     {
5351       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5352       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5353         offsetp++;
5354       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5355         {
5356           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5357           process_die (origin_child_die, origin_cu);
5358         }
5359       origin_child_die = sibling_die (origin_child_die);
5360     }
5361   origin_cu->list_in_scope = origin_previous_list_in_scope;
5362
5363   do_cleanups (cleanups);
5364 }
5365
5366 static void
5367 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5368 {
5369   struct objfile *objfile = cu->objfile;
5370   struct context_stack *new;
5371   CORE_ADDR lowpc;
5372   CORE_ADDR highpc;
5373   struct die_info *child_die;
5374   struct attribute *attr, *call_line, *call_file;
5375   char *name;
5376   CORE_ADDR baseaddr;
5377   struct block *block;
5378   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5379   VEC (symbolp) *template_args = NULL;
5380   struct template_symbol *templ_func = NULL;
5381
5382   if (inlined_func)
5383     {
5384       /* If we do not have call site information, we can't show the
5385          caller of this inlined function.  That's too confusing, so
5386          only use the scope for local variables.  */
5387       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5388       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5389       if (call_line == NULL || call_file == NULL)
5390         {
5391           read_lexical_block_scope (die, cu);
5392           return;
5393         }
5394     }
5395
5396   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5397
5398   name = dwarf2_name (die, cu);
5399
5400   /* Ignore functions with missing or empty names.  These are actually
5401      illegal according to the DWARF standard.  */
5402   if (name == NULL)
5403     {
5404       complaint (&symfile_complaints,
5405                  _("missing name for subprogram DIE at %d"), die->offset);
5406       return;
5407     }
5408
5409   /* Ignore functions with missing or invalid low and high pc attributes.  */
5410   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5411     {
5412       attr = dwarf2_attr (die, DW_AT_external, cu);
5413       if (!attr || !DW_UNSND (attr))
5414         complaint (&symfile_complaints,
5415                    _("cannot get low and high bounds for subprogram DIE at %d"),
5416                    die->offset);
5417       return;
5418     }
5419
5420   lowpc += baseaddr;
5421   highpc += baseaddr;
5422
5423   /* Record the function range for dwarf_decode_lines.  */
5424   add_to_cu_func_list (name, lowpc, highpc, cu);
5425
5426   /* If we have any template arguments, then we must allocate a
5427      different sort of symbol.  */
5428   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5429     {
5430       if (child_die->tag == DW_TAG_template_type_param
5431           || child_die->tag == DW_TAG_template_value_param)
5432         {
5433           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5434                                        struct template_symbol);
5435           templ_func->base.is_cplus_template_function = 1;
5436           break;
5437         }
5438     }
5439
5440   new = push_context (0, lowpc);
5441   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5442                                (struct symbol *) templ_func);
5443
5444   /* If there is a location expression for DW_AT_frame_base, record
5445      it.  */
5446   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5447   if (attr)
5448     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5449        expression is being recorded directly in the function's symbol
5450        and not in a separate frame-base object.  I guess this hack is
5451        to avoid adding some sort of frame-base adjunct/annex to the
5452        function's symbol :-(.  The problem with doing this is that it
5453        results in a function symbol with a location expression that
5454        has nothing to do with the location of the function, ouch!  The
5455        relationship should be: a function's symbol has-a frame base; a
5456        frame-base has-a location expression.  */
5457     dwarf2_symbol_mark_computed (attr, new->name, cu);
5458
5459   cu->list_in_scope = &local_symbols;
5460
5461   if (die->child != NULL)
5462     {
5463       child_die = die->child;
5464       while (child_die && child_die->tag)
5465         {
5466           if (child_die->tag == DW_TAG_template_type_param
5467               || child_die->tag == DW_TAG_template_value_param)
5468             {
5469               struct symbol *arg = new_symbol (child_die, NULL, cu);
5470
5471               if (arg != NULL)
5472                 VEC_safe_push (symbolp, template_args, arg);
5473             }
5474           else
5475             process_die (child_die, cu);
5476           child_die = sibling_die (child_die);
5477         }
5478     }
5479
5480   inherit_abstract_dies (die, cu);
5481
5482   /* If we have a DW_AT_specification, we might need to import using
5483      directives from the context of the specification DIE.  See the
5484      comment in determine_prefix.  */
5485   if (cu->language == language_cplus
5486       && dwarf2_attr (die, DW_AT_specification, cu))
5487     {
5488       struct dwarf2_cu *spec_cu = cu;
5489       struct die_info *spec_die = die_specification (die, &spec_cu);
5490
5491       while (spec_die)
5492         {
5493           child_die = spec_die->child;
5494           while (child_die && child_die->tag)
5495             {
5496               if (child_die->tag == DW_TAG_imported_module)
5497                 process_die (child_die, spec_cu);
5498               child_die = sibling_die (child_die);
5499             }
5500
5501           /* In some cases, GCC generates specification DIEs that
5502              themselves contain DW_AT_specification attributes.  */
5503           spec_die = die_specification (spec_die, &spec_cu);
5504         }
5505     }
5506
5507   new = pop_context ();
5508   /* Make a block for the local symbols within.  */
5509   block = finish_block (new->name, &local_symbols, new->old_blocks,
5510                         lowpc, highpc, objfile);
5511
5512   /* For C++, set the block's scope.  */
5513   if (cu->language == language_cplus || cu->language == language_fortran)
5514     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5515                         determine_prefix (die, cu),
5516                         processing_has_namespace_info);
5517
5518   /* If we have address ranges, record them.  */
5519   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5520
5521   /* Attach template arguments to function.  */
5522   if (! VEC_empty (symbolp, template_args))
5523     {
5524       gdb_assert (templ_func != NULL);
5525
5526       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5527       templ_func->template_arguments
5528         = obstack_alloc (&objfile->objfile_obstack,
5529                          (templ_func->n_template_arguments
5530                           * sizeof (struct symbol *)));
5531       memcpy (templ_func->template_arguments,
5532               VEC_address (symbolp, template_args),
5533               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5534       VEC_free (symbolp, template_args);
5535     }
5536
5537   /* In C++, we can have functions nested inside functions (e.g., when
5538      a function declares a class that has methods).  This means that
5539      when we finish processing a function scope, we may need to go
5540      back to building a containing block's symbol lists.  */
5541   local_symbols = new->locals;
5542   param_symbols = new->params;
5543   using_directives = new->using_directives;
5544
5545   /* If we've finished processing a top-level function, subsequent
5546      symbols go in the file symbol list.  */
5547   if (outermost_context_p ())
5548     cu->list_in_scope = &file_symbols;
5549 }
5550
5551 /* Process all the DIES contained within a lexical block scope.  Start
5552    a new scope, process the dies, and then close the scope.  */
5553
5554 static void
5555 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5556 {
5557   struct objfile *objfile = cu->objfile;
5558   struct context_stack *new;
5559   CORE_ADDR lowpc, highpc;
5560   struct die_info *child_die;
5561   CORE_ADDR baseaddr;
5562
5563   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5564
5565   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5566   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5567      as multiple lexical blocks?  Handling children in a sane way would
5568      be nasty.  Might be easier to properly extend generic blocks to
5569      describe ranges.  */
5570   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5571     return;
5572   lowpc += baseaddr;
5573   highpc += baseaddr;
5574
5575   push_context (0, lowpc);
5576   if (die->child != NULL)
5577     {
5578       child_die = die->child;
5579       while (child_die && child_die->tag)
5580         {
5581           process_die (child_die, cu);
5582           child_die = sibling_die (child_die);
5583         }
5584     }
5585   new = pop_context ();
5586
5587   if (local_symbols != NULL || using_directives != NULL)
5588     {
5589       struct block *block
5590         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5591                         highpc, objfile);
5592
5593       /* Note that recording ranges after traversing children, as we
5594          do here, means that recording a parent's ranges entails
5595          walking across all its children's ranges as they appear in
5596          the address map, which is quadratic behavior.
5597
5598          It would be nicer to record the parent's ranges before
5599          traversing its children, simply overriding whatever you find
5600          there.  But since we don't even decide whether to create a
5601          block until after we've traversed its children, that's hard
5602          to do.  */
5603       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5604     }
5605   local_symbols = new->locals;
5606   using_directives = new->using_directives;
5607 }
5608
5609 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5610    Return 1 if the attributes are present and valid, otherwise, return 0.
5611    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5612
5613 static int
5614 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5615                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5616                     struct partial_symtab *ranges_pst)
5617 {
5618   struct objfile *objfile = cu->objfile;
5619   struct comp_unit_head *cu_header = &cu->header;
5620   bfd *obfd = objfile->obfd;
5621   unsigned int addr_size = cu_header->addr_size;
5622   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5623   /* Base address selection entry.  */
5624   CORE_ADDR base;
5625   int found_base;
5626   unsigned int dummy;
5627   gdb_byte *buffer;
5628   CORE_ADDR marker;
5629   int low_set;
5630   CORE_ADDR low = 0;
5631   CORE_ADDR high = 0;
5632   CORE_ADDR baseaddr;
5633
5634   found_base = cu->base_known;
5635   base = cu->base_address;
5636
5637   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5638   if (offset >= dwarf2_per_objfile->ranges.size)
5639     {
5640       complaint (&symfile_complaints,
5641                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5642                  offset);
5643       return 0;
5644     }
5645   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5646
5647   /* Read in the largest possible address.  */
5648   marker = read_address (obfd, buffer, cu, &dummy);
5649   if ((marker & mask) == mask)
5650     {
5651       /* If we found the largest possible address, then
5652          read the base address.  */
5653       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5654       buffer += 2 * addr_size;
5655       offset += 2 * addr_size;
5656       found_base = 1;
5657     }
5658
5659   low_set = 0;
5660
5661   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5662
5663   while (1)
5664     {
5665       CORE_ADDR range_beginning, range_end;
5666
5667       range_beginning = read_address (obfd, buffer, cu, &dummy);
5668       buffer += addr_size;
5669       range_end = read_address (obfd, buffer, cu, &dummy);
5670       buffer += addr_size;
5671       offset += 2 * addr_size;
5672
5673       /* An end of list marker is a pair of zero addresses.  */
5674       if (range_beginning == 0 && range_end == 0)
5675         /* Found the end of list entry.  */
5676         break;
5677
5678       /* Each base address selection entry is a pair of 2 values.
5679          The first is the largest possible address, the second is
5680          the base address.  Check for a base address here.  */
5681       if ((range_beginning & mask) == mask)
5682         {
5683           /* If we found the largest possible address, then
5684              read the base address.  */
5685           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5686           found_base = 1;
5687           continue;
5688         }
5689
5690       if (!found_base)
5691         {
5692           /* We have no valid base address for the ranges
5693              data.  */
5694           complaint (&symfile_complaints,
5695                      _("Invalid .debug_ranges data (no base address)"));
5696           return 0;
5697         }
5698
5699       range_beginning += base;
5700       range_end += base;
5701
5702       if (ranges_pst != NULL && range_beginning < range_end)
5703         addrmap_set_empty (objfile->psymtabs_addrmap,
5704                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5705                            ranges_pst);
5706
5707       /* FIXME: This is recording everything as a low-high
5708          segment of consecutive addresses.  We should have a
5709          data structure for discontiguous block ranges
5710          instead.  */
5711       if (! low_set)
5712         {
5713           low = range_beginning;
5714           high = range_end;
5715           low_set = 1;
5716         }
5717       else
5718         {
5719           if (range_beginning < low)
5720             low = range_beginning;
5721           if (range_end > high)
5722             high = range_end;
5723         }
5724     }
5725
5726   if (! low_set)
5727     /* If the first entry is an end-of-list marker, the range
5728        describes an empty scope, i.e. no instructions.  */
5729     return 0;
5730
5731   if (low_return)
5732     *low_return = low;
5733   if (high_return)
5734     *high_return = high;
5735   return 1;
5736 }
5737
5738 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5739    are present and valid, otherwise, return 0.  Return -1 if the range is
5740    discontinuous, i.e. derived from DW_AT_ranges information.  */
5741 static int
5742 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5743                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5744                       struct partial_symtab *pst)
5745 {
5746   struct attribute *attr;
5747   CORE_ADDR low = 0;
5748   CORE_ADDR high = 0;
5749   int ret = 0;
5750
5751   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5752   if (attr)
5753     {
5754       high = DW_ADDR (attr);
5755       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5756       if (attr)
5757         low = DW_ADDR (attr);
5758       else
5759         /* Found high w/o low attribute.  */
5760         return 0;
5761
5762       /* Found consecutive range of addresses.  */
5763       ret = 1;
5764     }
5765   else
5766     {
5767       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5768       if (attr != NULL)
5769         {
5770           /* Value of the DW_AT_ranges attribute is the offset in the
5771              .debug_ranges section.  */
5772           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5773             return 0;
5774           /* Found discontinuous range of addresses.  */
5775           ret = -1;
5776         }
5777     }
5778
5779   if (high < low)
5780     return 0;
5781
5782   /* When using the GNU linker, .gnu.linkonce. sections are used to
5783      eliminate duplicate copies of functions and vtables and such.
5784      The linker will arbitrarily choose one and discard the others.
5785      The AT_*_pc values for such functions refer to local labels in
5786      these sections.  If the section from that file was discarded, the
5787      labels are not in the output, so the relocs get a value of 0.
5788      If this is a discarded function, mark the pc bounds as invalid,
5789      so that GDB will ignore it.  */
5790   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5791     return 0;
5792
5793   *lowpc = low;
5794   *highpc = high;
5795   return ret;
5796 }
5797
5798 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5799    its low and high PC addresses.  Do nothing if these addresses could not
5800    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5801    and HIGHPC to the high address if greater than HIGHPC.  */
5802
5803 static void
5804 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5805                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5806                                  struct dwarf2_cu *cu)
5807 {
5808   CORE_ADDR low, high;
5809   struct die_info *child = die->child;
5810
5811   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5812     {
5813       *lowpc = min (*lowpc, low);
5814       *highpc = max (*highpc, high);
5815     }
5816
5817   /* If the language does not allow nested subprograms (either inside
5818      subprograms or lexical blocks), we're done.  */
5819   if (cu->language != language_ada)
5820     return;
5821
5822   /* Check all the children of the given DIE.  If it contains nested
5823      subprograms, then check their pc bounds.  Likewise, we need to
5824      check lexical blocks as well, as they may also contain subprogram
5825      definitions.  */
5826   while (child && child->tag)
5827     {
5828       if (child->tag == DW_TAG_subprogram
5829           || child->tag == DW_TAG_lexical_block)
5830         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5831       child = sibling_die (child);
5832     }
5833 }
5834
5835 /* Get the low and high pc's represented by the scope DIE, and store
5836    them in *LOWPC and *HIGHPC.  If the correct values can't be
5837    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5838
5839 static void
5840 get_scope_pc_bounds (struct die_info *die,
5841                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5842                      struct dwarf2_cu *cu)
5843 {
5844   CORE_ADDR best_low = (CORE_ADDR) -1;
5845   CORE_ADDR best_high = (CORE_ADDR) 0;
5846   CORE_ADDR current_low, current_high;
5847
5848   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5849     {
5850       best_low = current_low;
5851       best_high = current_high;
5852     }
5853   else
5854     {
5855       struct die_info *child = die->child;
5856
5857       while (child && child->tag)
5858         {
5859           switch (child->tag) {
5860           case DW_TAG_subprogram:
5861             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5862             break;
5863           case DW_TAG_namespace:
5864           case DW_TAG_module:
5865             /* FIXME: carlton/2004-01-16: Should we do this for
5866                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5867                that current GCC's always emit the DIEs corresponding
5868                to definitions of methods of classes as children of a
5869                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5870                the DIEs giving the declarations, which could be
5871                anywhere).  But I don't see any reason why the
5872                standards says that they have to be there.  */
5873             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5874
5875             if (current_low != ((CORE_ADDR) -1))
5876               {
5877                 best_low = min (best_low, current_low);
5878                 best_high = max (best_high, current_high);
5879               }
5880             break;
5881           default:
5882             /* Ignore. */
5883             break;
5884           }
5885
5886           child = sibling_die (child);
5887         }
5888     }
5889
5890   *lowpc = best_low;
5891   *highpc = best_high;
5892 }
5893
5894 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5895    in DIE.  */
5896 static void
5897 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5898                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5899 {
5900   struct attribute *attr;
5901
5902   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5903   if (attr)
5904     {
5905       CORE_ADDR high = DW_ADDR (attr);
5906
5907       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5908       if (attr)
5909         {
5910           CORE_ADDR low = DW_ADDR (attr);
5911
5912           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5913         }
5914     }
5915
5916   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5917   if (attr)
5918     {
5919       bfd *obfd = cu->objfile->obfd;
5920
5921       /* The value of the DW_AT_ranges attribute is the offset of the
5922          address range list in the .debug_ranges section.  */
5923       unsigned long offset = DW_UNSND (attr);
5924       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5925
5926       /* For some target architectures, but not others, the
5927          read_address function sign-extends the addresses it returns.
5928          To recognize base address selection entries, we need a
5929          mask.  */
5930       unsigned int addr_size = cu->header.addr_size;
5931       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5932
5933       /* The base address, to which the next pair is relative.  Note
5934          that this 'base' is a DWARF concept: most entries in a range
5935          list are relative, to reduce the number of relocs against the
5936          debugging information.  This is separate from this function's
5937          'baseaddr' argument, which GDB uses to relocate debugging
5938          information from a shared library based on the address at
5939          which the library was loaded.  */
5940       CORE_ADDR base = cu->base_address;
5941       int base_known = cu->base_known;
5942
5943       gdb_assert (dwarf2_per_objfile->ranges.readin);
5944       if (offset >= dwarf2_per_objfile->ranges.size)
5945         {
5946           complaint (&symfile_complaints,
5947                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5948                      offset);
5949           return;
5950         }
5951
5952       for (;;)
5953         {
5954           unsigned int bytes_read;
5955           CORE_ADDR start, end;
5956
5957           start = read_address (obfd, buffer, cu, &bytes_read);
5958           buffer += bytes_read;
5959           end = read_address (obfd, buffer, cu, &bytes_read);
5960           buffer += bytes_read;
5961
5962           /* Did we find the end of the range list?  */
5963           if (start == 0 && end == 0)
5964             break;
5965
5966           /* Did we find a base address selection entry?  */
5967           else if ((start & base_select_mask) == base_select_mask)
5968             {
5969               base = end;
5970               base_known = 1;
5971             }
5972
5973           /* We found an ordinary address range.  */
5974           else
5975             {
5976               if (!base_known)
5977                 {
5978                   complaint (&symfile_complaints,
5979                              _("Invalid .debug_ranges data (no base address)"));
5980                   return;
5981                 }
5982
5983               record_block_range (block,
5984                                   baseaddr + base + start,
5985                                   baseaddr + base + end - 1);
5986             }
5987         }
5988     }
5989 }
5990
5991 /* Add an aggregate field to the field list.  */
5992
5993 static void
5994 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5995                   struct dwarf2_cu *cu)
5996 {
5997   struct objfile *objfile = cu->objfile;
5998   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5999   struct nextfield *new_field;
6000   struct attribute *attr;
6001   struct field *fp;
6002   char *fieldname = "";
6003
6004   /* Allocate a new field list entry and link it in.  */
6005   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6006   make_cleanup (xfree, new_field);
6007   memset (new_field, 0, sizeof (struct nextfield));
6008
6009   if (die->tag == DW_TAG_inheritance)
6010     {
6011       new_field->next = fip->baseclasses;
6012       fip->baseclasses = new_field;
6013     }
6014   else
6015     {
6016       new_field->next = fip->fields;
6017       fip->fields = new_field;
6018     }
6019   fip->nfields++;
6020
6021   /* Handle accessibility and virtuality of field.
6022      The default accessibility for members is public, the default
6023      accessibility for inheritance is private.  */
6024   if (die->tag != DW_TAG_inheritance)
6025     new_field->accessibility = DW_ACCESS_public;
6026   else
6027     new_field->accessibility = DW_ACCESS_private;
6028   new_field->virtuality = DW_VIRTUALITY_none;
6029
6030   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6031   if (attr)
6032     new_field->accessibility = DW_UNSND (attr);
6033   if (new_field->accessibility != DW_ACCESS_public)
6034     fip->non_public_fields = 1;
6035   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6036   if (attr)
6037     new_field->virtuality = DW_UNSND (attr);
6038
6039   fp = &new_field->field;
6040
6041   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6042     {
6043       /* Data member other than a C++ static data member.  */
6044
6045       /* Get type of field.  */
6046       fp->type = die_type (die, cu);
6047
6048       SET_FIELD_BITPOS (*fp, 0);
6049
6050       /* Get bit size of field (zero if none).  */
6051       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6052       if (attr)
6053         {
6054           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6055         }
6056       else
6057         {
6058           FIELD_BITSIZE (*fp) = 0;
6059         }
6060
6061       /* Get bit offset of field.  */
6062       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6063       if (attr)
6064         {
6065           int byte_offset = 0;
6066
6067           if (attr_form_is_section_offset (attr))
6068             dwarf2_complex_location_expr_complaint ();
6069           else if (attr_form_is_constant (attr))
6070             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6071           else if (attr_form_is_block (attr))
6072             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6073           else
6074             dwarf2_complex_location_expr_complaint ();
6075
6076           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6077         }
6078       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6079       if (attr)
6080         {
6081           if (gdbarch_bits_big_endian (gdbarch))
6082             {
6083               /* For big endian bits, the DW_AT_bit_offset gives the
6084                  additional bit offset from the MSB of the containing
6085                  anonymous object to the MSB of the field.  We don't
6086                  have to do anything special since we don't need to
6087                  know the size of the anonymous object.  */
6088               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6089             }
6090           else
6091             {
6092               /* For little endian bits, compute the bit offset to the
6093                  MSB of the anonymous object, subtract off the number of
6094                  bits from the MSB of the field to the MSB of the
6095                  object, and then subtract off the number of bits of
6096                  the field itself.  The result is the bit offset of
6097                  the LSB of the field.  */
6098               int anonymous_size;
6099               int bit_offset = DW_UNSND (attr);
6100
6101               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6102               if (attr)
6103                 {
6104                   /* The size of the anonymous object containing
6105                      the bit field is explicit, so use the
6106                      indicated size (in bytes).  */
6107                   anonymous_size = DW_UNSND (attr);
6108                 }
6109               else
6110                 {
6111                   /* The size of the anonymous object containing
6112                      the bit field must be inferred from the type
6113                      attribute of the data member containing the
6114                      bit field.  */
6115                   anonymous_size = TYPE_LENGTH (fp->type);
6116                 }
6117               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6118                 - bit_offset - FIELD_BITSIZE (*fp);
6119             }
6120         }
6121
6122       /* Get name of field.  */
6123       fieldname = dwarf2_name (die, cu);
6124       if (fieldname == NULL)
6125         fieldname = "";
6126
6127       /* The name is already allocated along with this objfile, so we don't
6128          need to duplicate it for the type.  */
6129       fp->name = fieldname;
6130
6131       /* Change accessibility for artificial fields (e.g. virtual table
6132          pointer or virtual base class pointer) to private.  */
6133       if (dwarf2_attr (die, DW_AT_artificial, cu))
6134         {
6135           FIELD_ARTIFICIAL (*fp) = 1;
6136           new_field->accessibility = DW_ACCESS_private;
6137           fip->non_public_fields = 1;
6138         }
6139     }
6140   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6141     {
6142       /* C++ static member.  */
6143
6144       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6145          is a declaration, but all versions of G++ as of this writing
6146          (so through at least 3.2.1) incorrectly generate
6147          DW_TAG_variable tags.  */
6148
6149       char *physname;
6150
6151       /* Get name of field.  */
6152       fieldname = dwarf2_name (die, cu);
6153       if (fieldname == NULL)
6154         return;
6155
6156       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6157       if (attr
6158           /* Only create a symbol if this is an external value.
6159              new_symbol checks this and puts the value in the global symbol
6160              table, which we want.  If it is not external, new_symbol
6161              will try to put the value in cu->list_in_scope which is wrong.  */
6162           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6163         {
6164           /* A static const member, not much different than an enum as far as
6165              we're concerned, except that we can support more types.  */
6166           new_symbol (die, NULL, cu);
6167         }
6168
6169       /* Get physical name.  */
6170       physname = (char *) dwarf2_physname (fieldname, die, cu);
6171
6172       /* The name is already allocated along with this objfile, so we don't
6173          need to duplicate it for the type.  */
6174       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6175       FIELD_TYPE (*fp) = die_type (die, cu);
6176       FIELD_NAME (*fp) = fieldname;
6177     }
6178   else if (die->tag == DW_TAG_inheritance)
6179     {
6180       /* C++ base class field.  */
6181       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6182       if (attr)
6183         {
6184           int byte_offset = 0;
6185
6186           if (attr_form_is_section_offset (attr))
6187             dwarf2_complex_location_expr_complaint ();
6188           else if (attr_form_is_constant (attr))
6189             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6190           else if (attr_form_is_block (attr))
6191             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6192           else
6193             dwarf2_complex_location_expr_complaint ();
6194
6195           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6196         }
6197       FIELD_BITSIZE (*fp) = 0;
6198       FIELD_TYPE (*fp) = die_type (die, cu);
6199       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6200       fip->nbaseclasses++;
6201     }
6202 }
6203
6204 /* Add a typedef defined in the scope of the FIP's class.  */
6205
6206 static void
6207 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6208                     struct dwarf2_cu *cu)
6209 {
6210   struct objfile *objfile = cu->objfile;
6211   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6212   struct typedef_field_list *new_field;
6213   struct attribute *attr;
6214   struct typedef_field *fp;
6215   char *fieldname = "";
6216
6217   /* Allocate a new field list entry and link it in.  */
6218   new_field = xzalloc (sizeof (*new_field));
6219   make_cleanup (xfree, new_field);
6220
6221   gdb_assert (die->tag == DW_TAG_typedef);
6222
6223   fp = &new_field->field;
6224
6225   /* Get name of field.  */
6226   fp->name = dwarf2_name (die, cu);
6227   if (fp->name == NULL)
6228     return;
6229
6230   fp->type = read_type_die (die, cu);
6231
6232   new_field->next = fip->typedef_field_list;
6233   fip->typedef_field_list = new_field;
6234   fip->typedef_field_list_count++;
6235 }
6236
6237 /* Create the vector of fields, and attach it to the type.  */
6238
6239 static void
6240 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6241                               struct dwarf2_cu *cu)
6242 {
6243   int nfields = fip->nfields;
6244
6245   /* Record the field count, allocate space for the array of fields,
6246      and create blank accessibility bitfields if necessary.  */
6247   TYPE_NFIELDS (type) = nfields;
6248   TYPE_FIELDS (type) = (struct field *)
6249     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6250   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6251
6252   if (fip->non_public_fields && cu->language != language_ada)
6253     {
6254       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6255
6256       TYPE_FIELD_PRIVATE_BITS (type) =
6257         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6258       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6259
6260       TYPE_FIELD_PROTECTED_BITS (type) =
6261         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6262       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6263
6264       TYPE_FIELD_IGNORE_BITS (type) =
6265         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6266       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6267     }
6268
6269   /* If the type has baseclasses, allocate and clear a bit vector for
6270      TYPE_FIELD_VIRTUAL_BITS.  */
6271   if (fip->nbaseclasses && cu->language != language_ada)
6272     {
6273       int num_bytes = B_BYTES (fip->nbaseclasses);
6274       unsigned char *pointer;
6275
6276       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6277       pointer = TYPE_ALLOC (type, num_bytes);
6278       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6279       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6280       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6281     }
6282
6283   /* Copy the saved-up fields into the field vector.  Start from the head
6284      of the list, adding to the tail of the field array, so that they end
6285      up in the same order in the array in which they were added to the list.  */
6286   while (nfields-- > 0)
6287     {
6288       struct nextfield *fieldp;
6289
6290       if (fip->fields)
6291         {
6292           fieldp = fip->fields;
6293           fip->fields = fieldp->next;
6294         }
6295       else
6296         {
6297           fieldp = fip->baseclasses;
6298           fip->baseclasses = fieldp->next;
6299         }
6300
6301       TYPE_FIELD (type, nfields) = fieldp->field;
6302       switch (fieldp->accessibility)
6303         {
6304         case DW_ACCESS_private:
6305           if (cu->language != language_ada)
6306             SET_TYPE_FIELD_PRIVATE (type, nfields);
6307           break;
6308
6309         case DW_ACCESS_protected:
6310           if (cu->language != language_ada)
6311             SET_TYPE_FIELD_PROTECTED (type, nfields);
6312           break;
6313
6314         case DW_ACCESS_public:
6315           break;
6316
6317         default:
6318           /* Unknown accessibility.  Complain and treat it as public.  */
6319           {
6320             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6321                        fieldp->accessibility);
6322           }
6323           break;
6324         }
6325       if (nfields < fip->nbaseclasses)
6326         {
6327           switch (fieldp->virtuality)
6328             {
6329             case DW_VIRTUALITY_virtual:
6330             case DW_VIRTUALITY_pure_virtual:
6331               if (cu->language == language_ada)
6332                 error ("unexpected virtuality in component of Ada type");
6333               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6334               break;
6335             }
6336         }
6337     }
6338 }
6339
6340 /* Add a member function to the proper fieldlist.  */
6341
6342 static void
6343 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6344                       struct type *type, struct dwarf2_cu *cu)
6345 {
6346   struct objfile *objfile = cu->objfile;
6347   struct attribute *attr;
6348   struct fnfieldlist *flp;
6349   int i;
6350   struct fn_field *fnp;
6351   char *fieldname;
6352   struct nextfnfield *new_fnfield;
6353   struct type *this_type;
6354
6355   if (cu->language == language_ada)
6356     error ("unexpected member function in Ada type");
6357
6358   /* Get name of member function.  */
6359   fieldname = dwarf2_name (die, cu);
6360   if (fieldname == NULL)
6361     return;
6362
6363   /* Look up member function name in fieldlist.  */
6364   for (i = 0; i < fip->nfnfields; i++)
6365     {
6366       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6367         break;
6368     }
6369
6370   /* Create new list element if necessary.  */
6371   if (i < fip->nfnfields)
6372     flp = &fip->fnfieldlists[i];
6373   else
6374     {
6375       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6376         {
6377           fip->fnfieldlists = (struct fnfieldlist *)
6378             xrealloc (fip->fnfieldlists,
6379                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6380                       * sizeof (struct fnfieldlist));
6381           if (fip->nfnfields == 0)
6382             make_cleanup (free_current_contents, &fip->fnfieldlists);
6383         }
6384       flp = &fip->fnfieldlists[fip->nfnfields];
6385       flp->name = fieldname;
6386       flp->length = 0;
6387       flp->head = NULL;
6388       i = fip->nfnfields++;
6389     }
6390
6391   /* Create a new member function field and chain it to the field list
6392      entry. */
6393   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6394   make_cleanup (xfree, new_fnfield);
6395   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6396   new_fnfield->next = flp->head;
6397   flp->head = new_fnfield;
6398   flp->length++;
6399
6400   /* Fill in the member function field info.  */
6401   fnp = &new_fnfield->fnfield;
6402
6403   /* Delay processing of the physname until later.  */
6404   if (cu->language == language_cplus || cu->language == language_java)
6405     {
6406       add_to_method_list (type, i, flp->length - 1, fieldname,
6407                           die, cu);
6408     }
6409   else
6410     {
6411       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6412       fnp->physname = physname ? physname : "";
6413     }
6414
6415   fnp->type = alloc_type (objfile);
6416   this_type = read_type_die (die, cu);
6417   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6418     {
6419       int nparams = TYPE_NFIELDS (this_type);
6420
6421       /* TYPE is the domain of this method, and THIS_TYPE is the type
6422            of the method itself (TYPE_CODE_METHOD).  */
6423       smash_to_method_type (fnp->type, type,
6424                             TYPE_TARGET_TYPE (this_type),
6425                             TYPE_FIELDS (this_type),
6426                             TYPE_NFIELDS (this_type),
6427                             TYPE_VARARGS (this_type));
6428
6429       /* Handle static member functions.
6430          Dwarf2 has no clean way to discern C++ static and non-static
6431          member functions. G++ helps GDB by marking the first
6432          parameter for non-static member functions (which is the
6433          this pointer) as artificial. We obtain this information
6434          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6435       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6436         fnp->voffset = VOFFSET_STATIC;
6437     }
6438   else
6439     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6440                dwarf2_full_name (fieldname, die, cu));
6441
6442   /* Get fcontext from DW_AT_containing_type if present.  */
6443   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6444     fnp->fcontext = die_containing_type (die, cu);
6445
6446   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6447      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6448
6449   /* Get accessibility.  */
6450   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6451   if (attr)
6452     {
6453       switch (DW_UNSND (attr))
6454         {
6455         case DW_ACCESS_private:
6456           fnp->is_private = 1;
6457           break;
6458         case DW_ACCESS_protected:
6459           fnp->is_protected = 1;
6460           break;
6461         }
6462     }
6463
6464   /* Check for artificial methods.  */
6465   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6466   if (attr && DW_UNSND (attr) != 0)
6467     fnp->is_artificial = 1;
6468
6469   /* Get index in virtual function table if it is a virtual member
6470      function.  For older versions of GCC, this is an offset in the
6471      appropriate virtual table, as specified by DW_AT_containing_type.
6472      For everyone else, it is an expression to be evaluated relative
6473      to the object address.  */
6474
6475   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6476   if (attr)
6477     {
6478       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6479         {
6480           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6481             {
6482               /* Old-style GCC.  */
6483               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6484             }
6485           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6486                    || (DW_BLOCK (attr)->size > 1
6487                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6488                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6489             {
6490               struct dwarf_block blk;
6491               int offset;
6492
6493               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6494                         ? 1 : 2);
6495               blk.size = DW_BLOCK (attr)->size - offset;
6496               blk.data = DW_BLOCK (attr)->data + offset;
6497               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6498               if ((fnp->voffset % cu->header.addr_size) != 0)
6499                 dwarf2_complex_location_expr_complaint ();
6500               else
6501                 fnp->voffset /= cu->header.addr_size;
6502               fnp->voffset += 2;
6503             }
6504           else
6505             dwarf2_complex_location_expr_complaint ();
6506
6507           if (!fnp->fcontext)
6508             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6509         }
6510       else if (attr_form_is_section_offset (attr))
6511         {
6512           dwarf2_complex_location_expr_complaint ();
6513         }
6514       else
6515         {
6516           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6517                                                  fieldname);
6518         }
6519     }
6520   else
6521     {
6522       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6523       if (attr && DW_UNSND (attr))
6524         {
6525           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6526           complaint (&symfile_complaints,
6527                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6528                      fieldname, die->offset);
6529           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6530           TYPE_CPLUS_DYNAMIC (type) = 1;
6531         }
6532     }
6533 }
6534
6535 /* Create the vector of member function fields, and attach it to the type.  */
6536
6537 static void
6538 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6539                                  struct dwarf2_cu *cu)
6540 {
6541   struct fnfieldlist *flp;
6542   int total_length = 0;
6543   int i;
6544
6545   if (cu->language == language_ada)
6546     error ("unexpected member functions in Ada type");
6547
6548   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6549   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6550     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6551
6552   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6553     {
6554       struct nextfnfield *nfp = flp->head;
6555       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6556       int k;
6557
6558       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6559       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6560       fn_flp->fn_fields = (struct fn_field *)
6561         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6562       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6563         fn_flp->fn_fields[k] = nfp->fnfield;
6564
6565       total_length += flp->length;
6566     }
6567
6568   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6569   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6570 }
6571
6572 /* Returns non-zero if NAME is the name of a vtable member in CU's
6573    language, zero otherwise.  */
6574 static int
6575 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6576 {
6577   static const char vptr[] = "_vptr";
6578   static const char vtable[] = "vtable";
6579
6580   /* Look for the C++ and Java forms of the vtable.  */
6581   if ((cu->language == language_java
6582        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6583        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6584        && is_cplus_marker (name[sizeof (vptr) - 1])))
6585     return 1;
6586
6587   return 0;
6588 }
6589
6590 /* GCC outputs unnamed structures that are really pointers to member
6591    functions, with the ABI-specified layout.  If TYPE describes
6592    such a structure, smash it into a member function type.
6593
6594    GCC shouldn't do this; it should just output pointer to member DIEs.
6595    This is GCC PR debug/28767.  */
6596
6597 static void
6598 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6599 {
6600   struct type *pfn_type, *domain_type, *new_type;
6601
6602   /* Check for a structure with no name and two children.  */
6603   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6604     return;
6605
6606   /* Check for __pfn and __delta members.  */
6607   if (TYPE_FIELD_NAME (type, 0) == NULL
6608       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6609       || TYPE_FIELD_NAME (type, 1) == NULL
6610       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6611     return;
6612
6613   /* Find the type of the method.  */
6614   pfn_type = TYPE_FIELD_TYPE (type, 0);
6615   if (pfn_type == NULL
6616       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6617       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6618     return;
6619
6620   /* Look for the "this" argument.  */
6621   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6622   if (TYPE_NFIELDS (pfn_type) == 0
6623       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6624       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6625     return;
6626
6627   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6628   new_type = alloc_type (objfile);
6629   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6630                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6631                         TYPE_VARARGS (pfn_type));
6632   smash_to_methodptr_type (type, new_type);
6633 }
6634
6635 /* Called when we find the DIE that starts a structure or union scope
6636    (definition) to create a type for the structure or union.  Fill in
6637    the type's name and general properties; the members will not be
6638    processed until process_structure_type.
6639
6640    NOTE: we need to call these functions regardless of whether or not the
6641    DIE has a DW_AT_name attribute, since it might be an anonymous
6642    structure or union.  This gets the type entered into our set of
6643    user defined types.
6644
6645    However, if the structure is incomplete (an opaque struct/union)
6646    then suppress creating a symbol table entry for it since gdb only
6647    wants to find the one with the complete definition.  Note that if
6648    it is complete, we just call new_symbol, which does it's own
6649    checking about whether the struct/union is anonymous or not (and
6650    suppresses creating a symbol table entry itself).  */
6651
6652 static struct type *
6653 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6654 {
6655   struct objfile *objfile = cu->objfile;
6656   struct type *type;
6657   struct attribute *attr;
6658   char *name;
6659
6660   /* If the definition of this type lives in .debug_types, read that type.
6661      Don't follow DW_AT_specification though, that will take us back up
6662      the chain and we want to go down.  */
6663   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6664   if (attr)
6665     {
6666       struct dwarf2_cu *type_cu = cu;
6667       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6668
6669       /* We could just recurse on read_structure_type, but we need to call
6670          get_die_type to ensure only one type for this DIE is created.
6671          This is important, for example, because for c++ classes we need
6672          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6673       type = read_type_die (type_die, type_cu);
6674
6675       /* TYPE_CU may not be the same as CU.
6676          Ensure TYPE is recorded in CU's type_hash table.  */
6677       return set_die_type (die, type, cu);
6678     }
6679
6680   type = alloc_type (objfile);
6681   INIT_CPLUS_SPECIFIC (type);
6682
6683   name = dwarf2_name (die, cu);
6684   if (name != NULL)
6685     {
6686       if (cu->language == language_cplus
6687           || cu->language == language_java)
6688         {
6689           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6690
6691           /* dwarf2_full_name might have already finished building the DIE's
6692              type.  If so, there is no need to continue.  */
6693           if (get_die_type (die, cu) != NULL)
6694             return get_die_type (die, cu);
6695
6696           TYPE_TAG_NAME (type) = full_name;
6697           if (die->tag == DW_TAG_structure_type
6698               || die->tag == DW_TAG_class_type)
6699             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6700         }
6701       else
6702         {
6703           /* The name is already allocated along with this objfile, so
6704              we don't need to duplicate it for the type.  */
6705           TYPE_TAG_NAME (type) = (char *) name;
6706           if (die->tag == DW_TAG_class_type)
6707             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6708         }
6709     }
6710
6711   if (die->tag == DW_TAG_structure_type)
6712     {
6713       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6714     }
6715   else if (die->tag == DW_TAG_union_type)
6716     {
6717       TYPE_CODE (type) = TYPE_CODE_UNION;
6718     }
6719   else
6720     {
6721       TYPE_CODE (type) = TYPE_CODE_CLASS;
6722     }
6723
6724   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6725     TYPE_DECLARED_CLASS (type) = 1;
6726
6727   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6728   if (attr)
6729     {
6730       TYPE_LENGTH (type) = DW_UNSND (attr);
6731     }
6732   else
6733     {
6734       TYPE_LENGTH (type) = 0;
6735     }
6736
6737   TYPE_STUB_SUPPORTED (type) = 1;
6738   if (die_is_declaration (die, cu))
6739     TYPE_STUB (type) = 1;
6740   else if (attr == NULL && die->child == NULL
6741            && producer_is_realview (cu->producer))
6742     /* RealView does not output the required DW_AT_declaration
6743        on incomplete types.  */
6744     TYPE_STUB (type) = 1;
6745
6746   /* We need to add the type field to the die immediately so we don't
6747      infinitely recurse when dealing with pointers to the structure
6748      type within the structure itself. */
6749   set_die_type (die, type, cu);
6750
6751   /* set_die_type should be already done.  */
6752   set_descriptive_type (type, die, cu);
6753
6754   return type;
6755 }
6756
6757 /* Finish creating a structure or union type, including filling in
6758    its members and creating a symbol for it.  */
6759
6760 static void
6761 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6762 {
6763   struct objfile *objfile = cu->objfile;
6764   struct die_info *child_die = die->child;
6765   struct type *type;
6766
6767   type = get_die_type (die, cu);
6768   if (type == NULL)
6769     type = read_structure_type (die, cu);
6770
6771   if (die->child != NULL && ! die_is_declaration (die, cu))
6772     {
6773       struct field_info fi;
6774       struct die_info *child_die;
6775       VEC (symbolp) *template_args = NULL;
6776       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6777
6778       memset (&fi, 0, sizeof (struct field_info));
6779
6780       child_die = die->child;
6781
6782       while (child_die && child_die->tag)
6783         {
6784           if (child_die->tag == DW_TAG_member
6785               || child_die->tag == DW_TAG_variable)
6786             {
6787               /* NOTE: carlton/2002-11-05: A C++ static data member
6788                  should be a DW_TAG_member that is a declaration, but
6789                  all versions of G++ as of this writing (so through at
6790                  least 3.2.1) incorrectly generate DW_TAG_variable
6791                  tags for them instead.  */
6792               dwarf2_add_field (&fi, child_die, cu);
6793             }
6794           else if (child_die->tag == DW_TAG_subprogram)
6795             {
6796               /* C++ member function. */
6797               dwarf2_add_member_fn (&fi, child_die, type, cu);
6798             }
6799           else if (child_die->tag == DW_TAG_inheritance)
6800             {
6801               /* C++ base class field.  */
6802               dwarf2_add_field (&fi, child_die, cu);
6803             }
6804           else if (child_die->tag == DW_TAG_typedef)
6805             dwarf2_add_typedef (&fi, child_die, cu);
6806           else if (child_die->tag == DW_TAG_template_type_param
6807                    || child_die->tag == DW_TAG_template_value_param)
6808             {
6809               struct symbol *arg = new_symbol (child_die, NULL, cu);
6810
6811               if (arg != NULL)
6812                 VEC_safe_push (symbolp, template_args, arg);
6813             }
6814
6815           child_die = sibling_die (child_die);
6816         }
6817
6818       /* Attach template arguments to type.  */
6819       if (! VEC_empty (symbolp, template_args))
6820         {
6821           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6822           TYPE_N_TEMPLATE_ARGUMENTS (type)
6823             = VEC_length (symbolp, template_args);
6824           TYPE_TEMPLATE_ARGUMENTS (type)
6825             = obstack_alloc (&objfile->objfile_obstack,
6826                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
6827                               * sizeof (struct symbol *)));
6828           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6829                   VEC_address (symbolp, template_args),
6830                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
6831                    * sizeof (struct symbol *)));
6832           VEC_free (symbolp, template_args);
6833         }
6834
6835       /* Attach fields and member functions to the type.  */
6836       if (fi.nfields)
6837         dwarf2_attach_fields_to_type (&fi, type, cu);
6838       if (fi.nfnfields)
6839         {
6840           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6841
6842           /* Get the type which refers to the base class (possibly this
6843              class itself) which contains the vtable pointer for the current
6844              class from the DW_AT_containing_type attribute.  This use of
6845              DW_AT_containing_type is a GNU extension.  */
6846
6847           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6848             {
6849               struct type *t = die_containing_type (die, cu);
6850
6851               TYPE_VPTR_BASETYPE (type) = t;
6852               if (type == t)
6853                 {
6854                   int i;
6855
6856                   /* Our own class provides vtbl ptr.  */
6857                   for (i = TYPE_NFIELDS (t) - 1;
6858                        i >= TYPE_N_BASECLASSES (t);
6859                        --i)
6860                     {
6861                       char *fieldname = TYPE_FIELD_NAME (t, i);
6862
6863                       if (is_vtable_name (fieldname, cu))
6864                         {
6865                           TYPE_VPTR_FIELDNO (type) = i;
6866                           break;
6867                         }
6868                     }
6869
6870                   /* Complain if virtual function table field not found.  */
6871                   if (i < TYPE_N_BASECLASSES (t))
6872                     complaint (&symfile_complaints,
6873                                _("virtual function table pointer not found when defining class '%s'"),
6874                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6875                                "");
6876                 }
6877               else
6878                 {
6879                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6880                 }
6881             }
6882           else if (cu->producer
6883                    && strncmp (cu->producer,
6884                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6885             {
6886               /* The IBM XLC compiler does not provide direct indication
6887                  of the containing type, but the vtable pointer is
6888                  always named __vfp.  */
6889
6890               int i;
6891
6892               for (i = TYPE_NFIELDS (type) - 1;
6893                    i >= TYPE_N_BASECLASSES (type);
6894                    --i)
6895                 {
6896                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6897                     {
6898                       TYPE_VPTR_FIELDNO (type) = i;
6899                       TYPE_VPTR_BASETYPE (type) = type;
6900                       break;
6901                     }
6902                 }
6903             }
6904         }
6905
6906       /* Copy fi.typedef_field_list linked list elements content into the
6907          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6908       if (fi.typedef_field_list)
6909         {
6910           int i = fi.typedef_field_list_count;
6911
6912           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6913           TYPE_TYPEDEF_FIELD_ARRAY (type)
6914             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6915           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6916
6917           /* Reverse the list order to keep the debug info elements order.  */
6918           while (--i >= 0)
6919             {
6920               struct typedef_field *dest, *src;
6921
6922               dest = &TYPE_TYPEDEF_FIELD (type, i);
6923               src = &fi.typedef_field_list->field;
6924               fi.typedef_field_list = fi.typedef_field_list->next;
6925               *dest = *src;
6926             }
6927         }
6928
6929       do_cleanups (back_to);
6930     }
6931
6932   quirk_gcc_member_function_pointer (type, cu->objfile);
6933
6934   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6935      snapshots) has been known to create a die giving a declaration
6936      for a class that has, as a child, a die giving a definition for a
6937      nested class.  So we have to process our children even if the
6938      current die is a declaration.  Normally, of course, a declaration
6939      won't have any children at all.  */
6940
6941   while (child_die != NULL && child_die->tag)
6942     {
6943       if (child_die->tag == DW_TAG_member
6944           || child_die->tag == DW_TAG_variable
6945           || child_die->tag == DW_TAG_inheritance
6946           || child_die->tag == DW_TAG_template_value_param
6947           || child_die->tag == DW_TAG_template_type_param)
6948         {
6949           /* Do nothing.  */
6950         }
6951       else
6952         process_die (child_die, cu);
6953
6954       child_die = sibling_die (child_die);
6955     }
6956
6957   /* Do not consider external references.  According to the DWARF standard,
6958      these DIEs are identified by the fact that they have no byte_size
6959      attribute, and a declaration attribute.  */
6960   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6961       || !die_is_declaration (die, cu))
6962     new_symbol (die, type, cu);
6963 }
6964
6965 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6966    complete the type's fields yet, or create any symbols.  */
6967
6968 static struct type *
6969 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6970 {
6971   struct objfile *objfile = cu->objfile;
6972   struct type *type;
6973   struct attribute *attr;
6974   const char *name;
6975
6976   /* If the definition of this type lives in .debug_types, read that type.
6977      Don't follow DW_AT_specification though, that will take us back up
6978      the chain and we want to go down.  */
6979   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6980   if (attr)
6981     {
6982       struct dwarf2_cu *type_cu = cu;
6983       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6984
6985       type = read_type_die (type_die, type_cu);
6986
6987       /* TYPE_CU may not be the same as CU.
6988          Ensure TYPE is recorded in CU's type_hash table.  */
6989       return set_die_type (die, type, cu);
6990     }
6991
6992   type = alloc_type (objfile);
6993
6994   TYPE_CODE (type) = TYPE_CODE_ENUM;
6995   name = dwarf2_full_name (NULL, die, cu);
6996   if (name != NULL)
6997     TYPE_TAG_NAME (type) = (char *) name;
6998
6999   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7000   if (attr)
7001     {
7002       TYPE_LENGTH (type) = DW_UNSND (attr);
7003     }
7004   else
7005     {
7006       TYPE_LENGTH (type) = 0;
7007     }
7008
7009   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7010      declared as private in the package spec, and then defined only
7011      inside the package body.  Such types are known as Taft Amendment
7012      Types.  When another package uses such a type, an incomplete DIE
7013      may be generated by the compiler.  */
7014   if (die_is_declaration (die, cu))
7015     TYPE_STUB (type) = 1;
7016
7017   return set_die_type (die, type, cu);
7018 }
7019
7020 /* Given a pointer to a die which begins an enumeration, process all
7021    the dies that define the members of the enumeration, and create the
7022    symbol for the enumeration type.
7023
7024    NOTE: We reverse the order of the element list.  */
7025
7026 static void
7027 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7028 {
7029   struct type *this_type;
7030
7031   this_type = get_die_type (die, cu);
7032   if (this_type == NULL)
7033     this_type = read_enumeration_type (die, cu);
7034
7035   if (die->child != NULL)
7036     {
7037       struct die_info *child_die;
7038       struct symbol *sym;
7039       struct field *fields = NULL;
7040       int num_fields = 0;
7041       int unsigned_enum = 1;
7042       char *name;
7043
7044       child_die = die->child;
7045       while (child_die && child_die->tag)
7046         {
7047           if (child_die->tag != DW_TAG_enumerator)
7048             {
7049               process_die (child_die, cu);
7050             }
7051           else
7052             {
7053               name = dwarf2_name (child_die, cu);
7054               if (name)
7055                 {
7056                   sym = new_symbol (child_die, this_type, cu);
7057                   if (SYMBOL_VALUE (sym) < 0)
7058                     unsigned_enum = 0;
7059
7060                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7061                     {
7062                       fields = (struct field *)
7063                         xrealloc (fields,
7064                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7065                                   * sizeof (struct field));
7066                     }
7067
7068                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7069                   FIELD_TYPE (fields[num_fields]) = NULL;
7070                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7071                   FIELD_BITSIZE (fields[num_fields]) = 0;
7072
7073                   num_fields++;
7074                 }
7075             }
7076
7077           child_die = sibling_die (child_die);
7078         }
7079
7080       if (num_fields)
7081         {
7082           TYPE_NFIELDS (this_type) = num_fields;
7083           TYPE_FIELDS (this_type) = (struct field *)
7084             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7085           memcpy (TYPE_FIELDS (this_type), fields,
7086                   sizeof (struct field) * num_fields);
7087           xfree (fields);
7088         }
7089       if (unsigned_enum)
7090         TYPE_UNSIGNED (this_type) = 1;
7091     }
7092
7093   new_symbol (die, this_type, cu);
7094 }
7095
7096 /* Extract all information from a DW_TAG_array_type DIE and put it in
7097    the DIE's type field.  For now, this only handles one dimensional
7098    arrays.  */
7099
7100 static struct type *
7101 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7102 {
7103   struct objfile *objfile = cu->objfile;
7104   struct die_info *child_die;
7105   struct type *type;
7106   struct type *element_type, *range_type, *index_type;
7107   struct type **range_types = NULL;
7108   struct attribute *attr;
7109   int ndim = 0;
7110   struct cleanup *back_to;
7111   char *name;
7112
7113   element_type = die_type (die, cu);
7114
7115   /* The die_type call above may have already set the type for this DIE.  */
7116   type = get_die_type (die, cu);
7117   if (type)
7118     return type;
7119
7120   /* Irix 6.2 native cc creates array types without children for
7121      arrays with unspecified length.  */
7122   if (die->child == NULL)
7123     {
7124       index_type = objfile_type (objfile)->builtin_int;
7125       range_type = create_range_type (NULL, index_type, 0, -1);
7126       type = create_array_type (NULL, element_type, range_type);
7127       return set_die_type (die, type, cu);
7128     }
7129
7130   back_to = make_cleanup (null_cleanup, NULL);
7131   child_die = die->child;
7132   while (child_die && child_die->tag)
7133     {
7134       if (child_die->tag == DW_TAG_subrange_type)
7135         {
7136           struct type *child_type = read_type_die (child_die, cu);
7137
7138           if (child_type != NULL)
7139             {
7140               /* The range type was succesfully read. Save it for
7141                  the array type creation.  */
7142               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7143                 {
7144                   range_types = (struct type **)
7145                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7146                               * sizeof (struct type *));
7147                   if (ndim == 0)
7148                     make_cleanup (free_current_contents, &range_types);
7149                 }
7150               range_types[ndim++] = child_type;
7151             }
7152         }
7153       child_die = sibling_die (child_die);
7154     }
7155
7156   /* Dwarf2 dimensions are output from left to right, create the
7157      necessary array types in backwards order.  */
7158
7159   type = element_type;
7160
7161   if (read_array_order (die, cu) == DW_ORD_col_major)
7162     {
7163       int i = 0;
7164
7165       while (i < ndim)
7166         type = create_array_type (NULL, type, range_types[i++]);
7167     }
7168   else
7169     {
7170       while (ndim-- > 0)
7171         type = create_array_type (NULL, type, range_types[ndim]);
7172     }
7173
7174   /* Understand Dwarf2 support for vector types (like they occur on
7175      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7176      array type.  This is not part of the Dwarf2/3 standard yet, but a
7177      custom vendor extension.  The main difference between a regular
7178      array and the vector variant is that vectors are passed by value
7179      to functions.  */
7180   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7181   if (attr)
7182     make_vector_type (type);
7183
7184   name = dwarf2_name (die, cu);
7185   if (name)
7186     TYPE_NAME (type) = name;
7187
7188   /* Install the type in the die. */
7189   set_die_type (die, type, cu);
7190
7191   /* set_die_type should be already done.  */
7192   set_descriptive_type (type, die, cu);
7193
7194   do_cleanups (back_to);
7195
7196   return type;
7197 }
7198
7199 static enum dwarf_array_dim_ordering
7200 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7201 {
7202   struct attribute *attr;
7203
7204   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7205
7206   if (attr) return DW_SND (attr);
7207
7208   /*
7209     GNU F77 is a special case, as at 08/2004 array type info is the
7210     opposite order to the dwarf2 specification, but data is still
7211     laid out as per normal fortran.
7212
7213     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7214     version checking.
7215   */
7216
7217   if (cu->language == language_fortran
7218       && cu->producer && strstr (cu->producer, "GNU F77"))
7219     {
7220       return DW_ORD_row_major;
7221     }
7222
7223   switch (cu->language_defn->la_array_ordering)
7224     {
7225     case array_column_major:
7226       return DW_ORD_col_major;
7227     case array_row_major:
7228     default:
7229       return DW_ORD_row_major;
7230     };
7231 }
7232
7233 /* Extract all information from a DW_TAG_set_type DIE and put it in
7234    the DIE's type field. */
7235
7236 static struct type *
7237 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7238 {
7239   struct type *domain_type, *set_type;
7240   struct attribute *attr;
7241
7242   domain_type = die_type (die, cu);
7243
7244   /* The die_type call above may have already set the type for this DIE.  */
7245   set_type = get_die_type (die, cu);
7246   if (set_type)
7247     return set_type;
7248
7249   set_type = create_set_type (NULL, domain_type);
7250
7251   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7252   if (attr)
7253     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7254
7255   return set_die_type (die, set_type, cu);
7256 }
7257
7258 /* First cut: install each common block member as a global variable.  */
7259
7260 static void
7261 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7262 {
7263   struct die_info *child_die;
7264   struct attribute *attr;
7265   struct symbol *sym;
7266   CORE_ADDR base = (CORE_ADDR) 0;
7267
7268   attr = dwarf2_attr (die, DW_AT_location, cu);
7269   if (attr)
7270     {
7271       /* Support the .debug_loc offsets */
7272       if (attr_form_is_block (attr))
7273         {
7274           base = decode_locdesc (DW_BLOCK (attr), cu);
7275         }
7276       else if (attr_form_is_section_offset (attr))
7277         {
7278           dwarf2_complex_location_expr_complaint ();
7279         }
7280       else
7281         {
7282           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7283                                                  "common block member");
7284         }
7285     }
7286   if (die->child != NULL)
7287     {
7288       child_die = die->child;
7289       while (child_die && child_die->tag)
7290         {
7291           sym = new_symbol (child_die, NULL, cu);
7292           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7293           if (sym != NULL && attr != NULL)
7294             {
7295               CORE_ADDR byte_offset = 0;
7296
7297               if (attr_form_is_section_offset (attr))
7298                 dwarf2_complex_location_expr_complaint ();
7299               else if (attr_form_is_constant (attr))
7300                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7301               else if (attr_form_is_block (attr))
7302                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7303               else
7304                 dwarf2_complex_location_expr_complaint ();
7305
7306               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7307               add_symbol_to_list (sym, &global_symbols);
7308             }
7309           child_die = sibling_die (child_die);
7310         }
7311     }
7312 }
7313
7314 /* Create a type for a C++ namespace.  */
7315
7316 static struct type *
7317 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7318 {
7319   struct objfile *objfile = cu->objfile;
7320   const char *previous_prefix, *name;
7321   int is_anonymous;
7322   struct type *type;
7323
7324   /* For extensions, reuse the type of the original namespace.  */
7325   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7326     {
7327       struct die_info *ext_die;
7328       struct dwarf2_cu *ext_cu = cu;
7329
7330       ext_die = dwarf2_extension (die, &ext_cu);
7331       type = read_type_die (ext_die, ext_cu);
7332
7333       /* EXT_CU may not be the same as CU.
7334          Ensure TYPE is recorded in CU's type_hash table.  */
7335       return set_die_type (die, type, cu);
7336     }
7337
7338   name = namespace_name (die, &is_anonymous, cu);
7339
7340   /* Now build the name of the current namespace.  */
7341
7342   previous_prefix = determine_prefix (die, cu);
7343   if (previous_prefix[0] != '\0')
7344     name = typename_concat (&objfile->objfile_obstack,
7345                             previous_prefix, name, 0, cu);
7346
7347   /* Create the type.  */
7348   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7349                     objfile);
7350   TYPE_NAME (type) = (char *) name;
7351   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7352
7353   return set_die_type (die, type, cu);
7354 }
7355
7356 /* Read a C++ namespace.  */
7357
7358 static void
7359 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7360 {
7361   struct objfile *objfile = cu->objfile;
7362   const char *name;
7363   int is_anonymous;
7364
7365   /* Add a symbol associated to this if we haven't seen the namespace
7366      before.  Also, add a using directive if it's an anonymous
7367      namespace.  */
7368
7369   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7370     {
7371       struct type *type;
7372
7373       type = read_type_die (die, cu);
7374       new_symbol (die, type, cu);
7375
7376       name = namespace_name (die, &is_anonymous, cu);
7377       if (is_anonymous)
7378         {
7379           const char *previous_prefix = determine_prefix (die, cu);
7380
7381           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7382                                   NULL, &objfile->objfile_obstack);
7383         }
7384     }
7385
7386   if (die->child != NULL)
7387     {
7388       struct die_info *child_die = die->child;
7389
7390       while (child_die && child_die->tag)
7391         {
7392           process_die (child_die, cu);
7393           child_die = sibling_die (child_die);
7394         }
7395     }
7396 }
7397
7398 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7399    imported module.  Still we need that type as local Fortran "use ... only"
7400    declaration imports depend on the created type in determine_prefix.  */
7401
7402 static struct type *
7403 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7404 {
7405   struct objfile *objfile = cu->objfile;
7406   char *module_name;
7407   struct type *type;
7408
7409   module_name = dwarf2_name (die, cu);
7410   if (!module_name)
7411     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7412                die->offset);
7413   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7414
7415   /* determine_prefix uses TYPE_TAG_NAME.  */
7416   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7417
7418   return set_die_type (die, type, cu);
7419 }
7420
7421 /* Read a Fortran module.  */
7422
7423 static void
7424 read_module (struct die_info *die, struct dwarf2_cu *cu)
7425 {
7426   struct die_info *child_die = die->child;
7427
7428   while (child_die && child_die->tag)
7429     {
7430       process_die (child_die, cu);
7431       child_die = sibling_die (child_die);
7432     }
7433 }
7434
7435 /* Return the name of the namespace represented by DIE.  Set
7436    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7437    namespace.  */
7438
7439 static const char *
7440 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7441 {
7442   struct die_info *current_die;
7443   const char *name = NULL;
7444
7445   /* Loop through the extensions until we find a name.  */
7446
7447   for (current_die = die;
7448        current_die != NULL;
7449        current_die = dwarf2_extension (die, &cu))
7450     {
7451       name = dwarf2_name (current_die, cu);
7452       if (name != NULL)
7453         break;
7454     }
7455
7456   /* Is it an anonymous namespace?  */
7457
7458   *is_anonymous = (name == NULL);
7459   if (*is_anonymous)
7460     name = "(anonymous namespace)";
7461
7462   return name;
7463 }
7464
7465 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7466    the user defined type vector.  */
7467
7468 static struct type *
7469 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7470 {
7471   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7472   struct comp_unit_head *cu_header = &cu->header;
7473   struct type *type;
7474   struct attribute *attr_byte_size;
7475   struct attribute *attr_address_class;
7476   int byte_size, addr_class;
7477   struct type *target_type;
7478
7479   target_type = die_type (die, cu);
7480
7481   /* The die_type call above may have already set the type for this DIE.  */
7482   type = get_die_type (die, cu);
7483   if (type)
7484     return type;
7485
7486   type = lookup_pointer_type (target_type);
7487
7488   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7489   if (attr_byte_size)
7490     byte_size = DW_UNSND (attr_byte_size);
7491   else
7492     byte_size = cu_header->addr_size;
7493
7494   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7495   if (attr_address_class)
7496     addr_class = DW_UNSND (attr_address_class);
7497   else
7498     addr_class = DW_ADDR_none;
7499
7500   /* If the pointer size or address class is different than the
7501      default, create a type variant marked as such and set the
7502      length accordingly.  */
7503   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7504     {
7505       if (gdbarch_address_class_type_flags_p (gdbarch))
7506         {
7507           int type_flags;
7508
7509           type_flags = gdbarch_address_class_type_flags
7510                          (gdbarch, byte_size, addr_class);
7511           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7512                       == 0);
7513           type = make_type_with_address_space (type, type_flags);
7514         }
7515       else if (TYPE_LENGTH (type) != byte_size)
7516         {
7517           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7518         }
7519       else
7520         {
7521           /* Should we also complain about unhandled address classes?  */
7522         }
7523     }
7524
7525   TYPE_LENGTH (type) = byte_size;
7526   return set_die_type (die, type, cu);
7527 }
7528
7529 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7530    the user defined type vector.  */
7531
7532 static struct type *
7533 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7534 {
7535   struct type *type;
7536   struct type *to_type;
7537   struct type *domain;
7538
7539   to_type = die_type (die, cu);
7540   domain = die_containing_type (die, cu);
7541
7542   /* The calls above may have already set the type for this DIE.  */
7543   type = get_die_type (die, cu);
7544   if (type)
7545     return type;
7546
7547   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7548     type = lookup_methodptr_type (to_type);
7549   else
7550     type = lookup_memberptr_type (to_type, domain);
7551
7552   return set_die_type (die, type, cu);
7553 }
7554
7555 /* Extract all information from a DW_TAG_reference_type DIE and add to
7556    the user defined type vector.  */
7557
7558 static struct type *
7559 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7560 {
7561   struct comp_unit_head *cu_header = &cu->header;
7562   struct type *type, *target_type;
7563   struct attribute *attr;
7564
7565   target_type = die_type (die, cu);
7566
7567   /* The die_type call above may have already set the type for this DIE.  */
7568   type = get_die_type (die, cu);
7569   if (type)
7570     return type;
7571
7572   type = lookup_reference_type (target_type);
7573   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7574   if (attr)
7575     {
7576       TYPE_LENGTH (type) = DW_UNSND (attr);
7577     }
7578   else
7579     {
7580       TYPE_LENGTH (type) = cu_header->addr_size;
7581     }
7582   return set_die_type (die, type, cu);
7583 }
7584
7585 static struct type *
7586 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7587 {
7588   struct type *base_type, *cv_type;
7589
7590   base_type = die_type (die, cu);
7591
7592   /* The die_type call above may have already set the type for this DIE.  */
7593   cv_type = get_die_type (die, cu);
7594   if (cv_type)
7595     return cv_type;
7596
7597   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7598   return set_die_type (die, cv_type, cu);
7599 }
7600
7601 static struct type *
7602 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7603 {
7604   struct type *base_type, *cv_type;
7605
7606   base_type = die_type (die, cu);
7607
7608   /* The die_type call above may have already set the type for this DIE.  */
7609   cv_type = get_die_type (die, cu);
7610   if (cv_type)
7611     return cv_type;
7612
7613   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7614   return set_die_type (die, cv_type, cu);
7615 }
7616
7617 /* Extract all information from a DW_TAG_string_type DIE and add to
7618    the user defined type vector.  It isn't really a user defined type,
7619    but it behaves like one, with other DIE's using an AT_user_def_type
7620    attribute to reference it.  */
7621
7622 static struct type *
7623 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7624 {
7625   struct objfile *objfile = cu->objfile;
7626   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7627   struct type *type, *range_type, *index_type, *char_type;
7628   struct attribute *attr;
7629   unsigned int length;
7630
7631   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7632   if (attr)
7633     {
7634       length = DW_UNSND (attr);
7635     }
7636   else
7637     {
7638       /* check for the DW_AT_byte_size attribute */
7639       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7640       if (attr)
7641         {
7642           length = DW_UNSND (attr);
7643         }
7644       else
7645         {
7646           length = 1;
7647         }
7648     }
7649
7650   index_type = objfile_type (objfile)->builtin_int;
7651   range_type = create_range_type (NULL, index_type, 1, length);
7652   char_type = language_string_char_type (cu->language_defn, gdbarch);
7653   type = create_string_type (NULL, char_type, range_type);
7654
7655   return set_die_type (die, type, cu);
7656 }
7657
7658 /* Handle DIES due to C code like:
7659
7660    struct foo
7661    {
7662    int (*funcp)(int a, long l);
7663    int b;
7664    };
7665
7666    ('funcp' generates a DW_TAG_subroutine_type DIE)
7667  */
7668
7669 static struct type *
7670 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7671 {
7672   struct type *type;            /* Type that this function returns */
7673   struct type *ftype;           /* Function that returns above type */
7674   struct attribute *attr;
7675
7676   type = die_type (die, cu);
7677
7678   /* The die_type call above may have already set the type for this DIE.  */
7679   ftype = get_die_type (die, cu);
7680   if (ftype)
7681     return ftype;
7682
7683   ftype = lookup_function_type (type);
7684
7685   /* All functions in C++, Pascal and Java have prototypes.  */
7686   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7687   if ((attr && (DW_UNSND (attr) != 0))
7688       || cu->language == language_cplus
7689       || cu->language == language_java
7690       || cu->language == language_pascal)
7691     TYPE_PROTOTYPED (ftype) = 1;
7692   else if (producer_is_realview (cu->producer))
7693     /* RealView does not emit DW_AT_prototyped.  We can not
7694        distinguish prototyped and unprototyped functions; default to
7695        prototyped, since that is more common in modern code (and
7696        RealView warns about unprototyped functions).  */
7697     TYPE_PROTOTYPED (ftype) = 1;
7698
7699   /* Store the calling convention in the type if it's available in
7700      the subroutine die.  Otherwise set the calling convention to
7701      the default value DW_CC_normal.  */
7702   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7703   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7704
7705   /* We need to add the subroutine type to the die immediately so
7706      we don't infinitely recurse when dealing with parameters
7707      declared as the same subroutine type. */
7708   set_die_type (die, ftype, cu);
7709
7710   if (die->child != NULL)
7711     {
7712       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7713       struct die_info *child_die;
7714       int nparams, iparams;
7715
7716       /* Count the number of parameters.
7717          FIXME: GDB currently ignores vararg functions, but knows about
7718          vararg member functions.  */
7719       nparams = 0;
7720       child_die = die->child;
7721       while (child_die && child_die->tag)
7722         {
7723           if (child_die->tag == DW_TAG_formal_parameter)
7724             nparams++;
7725           else if (child_die->tag == DW_TAG_unspecified_parameters)
7726             TYPE_VARARGS (ftype) = 1;
7727           child_die = sibling_die (child_die);
7728         }
7729
7730       /* Allocate storage for parameters and fill them in.  */
7731       TYPE_NFIELDS (ftype) = nparams;
7732       TYPE_FIELDS (ftype) = (struct field *)
7733         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7734
7735       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7736          even if we error out during the parameters reading below.  */
7737       for (iparams = 0; iparams < nparams; iparams++)
7738         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7739
7740       iparams = 0;
7741       child_die = die->child;
7742       while (child_die && child_die->tag)
7743         {
7744           if (child_die->tag == DW_TAG_formal_parameter)
7745             {
7746               struct type *arg_type;
7747
7748               /* DWARF version 2 has no clean way to discern C++
7749                  static and non-static member functions.  G++ helps
7750                  GDB by marking the first parameter for non-static
7751                  member functions (which is the this pointer) as
7752                  artificial.  We pass this information to
7753                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7754
7755                  DWARF version 3 added DW_AT_object_pointer, which GCC
7756                  4.5 does not yet generate.  */
7757               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7758               if (attr)
7759                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7760               else
7761                 {
7762                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7763
7764                   /* GCC/43521: In java, the formal parameter
7765                      "this" is sometimes not marked with DW_AT_artificial.  */
7766                   if (cu->language == language_java)
7767                     {
7768                       const char *name = dwarf2_name (child_die, cu);
7769
7770                       if (name && !strcmp (name, "this"))
7771                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7772                     }
7773                 }
7774               arg_type = die_type (child_die, cu);
7775
7776               /* RealView does not mark THIS as const, which the testsuite
7777                  expects.  GCC marks THIS as const in method definitions,
7778                  but not in the class specifications (GCC PR 43053).  */
7779               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7780                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7781                 {
7782                   int is_this = 0;
7783                   struct dwarf2_cu *arg_cu = cu;
7784                   const char *name = dwarf2_name (child_die, cu);
7785
7786                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7787                   if (attr)
7788                     {
7789                       /* If the compiler emits this, use it.  */
7790                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
7791                         is_this = 1;
7792                     }
7793                   else if (name && strcmp (name, "this") == 0)
7794                     /* Function definitions will have the argument names.  */
7795                     is_this = 1;
7796                   else if (name == NULL && iparams == 0)
7797                     /* Declarations may not have the names, so like
7798                        elsewhere in GDB, assume an artificial first
7799                        argument is "this".  */
7800                     is_this = 1;
7801
7802                   if (is_this)
7803                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7804                                              arg_type, 0);
7805                 }
7806
7807               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7808               iparams++;
7809             }
7810           child_die = sibling_die (child_die);
7811         }
7812     }
7813
7814   return ftype;
7815 }
7816
7817 static struct type *
7818 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7819 {
7820   struct objfile *objfile = cu->objfile;
7821   const char *name = NULL;
7822   struct type *this_type;
7823
7824   name = dwarf2_full_name (NULL, die, cu);
7825   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7826                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7827   TYPE_NAME (this_type) = (char *) name;
7828   set_die_type (die, this_type, cu);
7829   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7830   return this_type;
7831 }
7832
7833 /* Find a representation of a given base type and install
7834    it in the TYPE field of the die.  */
7835
7836 static struct type *
7837 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7838 {
7839   struct objfile *objfile = cu->objfile;
7840   struct type *type;
7841   struct attribute *attr;
7842   int encoding = 0, size = 0;
7843   char *name;
7844   enum type_code code = TYPE_CODE_INT;
7845   int type_flags = 0;
7846   struct type *target_type = NULL;
7847
7848   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7849   if (attr)
7850     {
7851       encoding = DW_UNSND (attr);
7852     }
7853   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7854   if (attr)
7855     {
7856       size = DW_UNSND (attr);
7857     }
7858   name = dwarf2_name (die, cu);
7859   if (!name)
7860     {
7861       complaint (&symfile_complaints,
7862                  _("DW_AT_name missing from DW_TAG_base_type"));
7863     }
7864
7865   switch (encoding)
7866     {
7867       case DW_ATE_address:
7868         /* Turn DW_ATE_address into a void * pointer.  */
7869         code = TYPE_CODE_PTR;
7870         type_flags |= TYPE_FLAG_UNSIGNED;
7871         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7872         break;
7873       case DW_ATE_boolean:
7874         code = TYPE_CODE_BOOL;
7875         type_flags |= TYPE_FLAG_UNSIGNED;
7876         break;
7877       case DW_ATE_complex_float:
7878         code = TYPE_CODE_COMPLEX;
7879         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7880         break;
7881       case DW_ATE_decimal_float:
7882         code = TYPE_CODE_DECFLOAT;
7883         break;
7884       case DW_ATE_float:
7885         code = TYPE_CODE_FLT;
7886         break;
7887       case DW_ATE_signed:
7888         break;
7889       case DW_ATE_unsigned:
7890         type_flags |= TYPE_FLAG_UNSIGNED;
7891         break;
7892       case DW_ATE_signed_char:
7893         if (cu->language == language_ada || cu->language == language_m2
7894             || cu->language == language_pascal)
7895           code = TYPE_CODE_CHAR;
7896         break;
7897       case DW_ATE_unsigned_char:
7898         if (cu->language == language_ada || cu->language == language_m2
7899             || cu->language == language_pascal)
7900           code = TYPE_CODE_CHAR;
7901         type_flags |= TYPE_FLAG_UNSIGNED;
7902         break;
7903       case DW_ATE_UTF:
7904         /* We just treat this as an integer and then recognize the
7905            type by name elsewhere.  */
7906         break;
7907
7908       default:
7909         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7910                    dwarf_type_encoding_name (encoding));
7911         break;
7912     }
7913
7914   type = init_type (code, size, type_flags, NULL, objfile);
7915   TYPE_NAME (type) = name;
7916   TYPE_TARGET_TYPE (type) = target_type;
7917
7918   if (name && strcmp (name, "char") == 0)
7919     TYPE_NOSIGN (type) = 1;
7920
7921   return set_die_type (die, type, cu);
7922 }
7923
7924 /* Read the given DW_AT_subrange DIE.  */
7925
7926 static struct type *
7927 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7928 {
7929   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7930   struct type *base_type;
7931   struct type *range_type;
7932   struct attribute *attr;
7933   LONGEST low = 0;
7934   LONGEST high = -1;
7935   char *name;
7936   LONGEST negative_mask;
7937
7938   base_type = die_type (die, cu);
7939   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7940   check_typedef (base_type);
7941
7942   /* The die_type call above may have already set the type for this DIE.  */
7943   range_type = get_die_type (die, cu);
7944   if (range_type)
7945     return range_type;
7946
7947   if (cu->language == language_fortran)
7948     {
7949       /* FORTRAN implies a lower bound of 1, if not given.  */
7950       low = 1;
7951     }
7952
7953   /* FIXME: For variable sized arrays either of these could be
7954      a variable rather than a constant value.  We'll allow it,
7955      but we don't know how to handle it.  */
7956   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7957   if (attr)
7958     low = dwarf2_get_attr_constant_value (attr, 0);
7959
7960   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7961   if (attr)
7962     {
7963       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7964         {
7965           /* GCC encodes arrays with unspecified or dynamic length
7966              with a DW_FORM_block1 attribute or a reference attribute.
7967              FIXME: GDB does not yet know how to handle dynamic
7968              arrays properly, treat them as arrays with unspecified
7969              length for now.
7970
7971              FIXME: jimb/2003-09-22: GDB does not really know
7972              how to handle arrays of unspecified length
7973              either; we just represent them as zero-length
7974              arrays.  Choose an appropriate upper bound given
7975              the lower bound we've computed above.  */
7976           high = low - 1;
7977         }
7978       else
7979         high = dwarf2_get_attr_constant_value (attr, 1);
7980     }
7981   else
7982     {
7983       attr = dwarf2_attr (die, DW_AT_count, cu);
7984       if (attr)
7985         {
7986           int count = dwarf2_get_attr_constant_value (attr, 1);
7987           high = low + count - 1;
7988         }
7989     }
7990
7991   /* Dwarf-2 specifications explicitly allows to create subrange types
7992      without specifying a base type.
7993      In that case, the base type must be set to the type of
7994      the lower bound, upper bound or count, in that order, if any of these
7995      three attributes references an object that has a type.
7996      If no base type is found, the Dwarf-2 specifications say that
7997      a signed integer type of size equal to the size of an address should
7998      be used.
7999      For the following C code: `extern char gdb_int [];'
8000      GCC produces an empty range DIE.
8001      FIXME: muller/2010-05-28: Possible references to object for low bound,
8002      high bound or count are not yet handled by this code.
8003   */
8004   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8005     {
8006       struct objfile *objfile = cu->objfile;
8007       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8008       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8009       struct type *int_type = objfile_type (objfile)->builtin_int;
8010
8011       /* Test "int", "long int", and "long long int" objfile types,
8012          and select the first one having a size above or equal to the
8013          architecture address size.  */
8014       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8015         base_type = int_type;
8016       else
8017         {
8018           int_type = objfile_type (objfile)->builtin_long;
8019           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8020             base_type = int_type;
8021           else
8022             {
8023               int_type = objfile_type (objfile)->builtin_long_long;
8024               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8025                 base_type = int_type;
8026             }
8027         }
8028     }
8029
8030   negative_mask =
8031     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8032   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8033     low |= negative_mask;
8034   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8035     high |= negative_mask;
8036
8037   range_type = create_range_type (NULL, base_type, low, high);
8038
8039   /* Mark arrays with dynamic length at least as an array of unspecified
8040      length.  GDB could check the boundary but before it gets implemented at
8041      least allow accessing the array elements.  */
8042   if (attr && attr->form == DW_FORM_block1)
8043     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8044
8045   name = dwarf2_name (die, cu);
8046   if (name)
8047     TYPE_NAME (range_type) = name;
8048
8049   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8050   if (attr)
8051     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8052
8053   set_die_type (die, range_type, cu);
8054
8055   /* set_die_type should be already done.  */
8056   set_descriptive_type (range_type, die, cu);
8057
8058   return range_type;
8059 }
8060
8061 static struct type *
8062 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8063 {
8064   struct type *type;
8065
8066   /* For now, we only support the C meaning of an unspecified type: void.  */
8067
8068   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8069   TYPE_NAME (type) = dwarf2_name (die, cu);
8070
8071   return set_die_type (die, type, cu);
8072 }
8073
8074 /* Trivial hash function for die_info: the hash value of a DIE
8075    is its offset in .debug_info for this objfile.  */
8076
8077 static hashval_t
8078 die_hash (const void *item)
8079 {
8080   const struct die_info *die = item;
8081
8082   return die->offset;
8083 }
8084
8085 /* Trivial comparison function for die_info structures: two DIEs
8086    are equal if they have the same offset.  */
8087
8088 static int
8089 die_eq (const void *item_lhs, const void *item_rhs)
8090 {
8091   const struct die_info *die_lhs = item_lhs;
8092   const struct die_info *die_rhs = item_rhs;
8093
8094   return die_lhs->offset == die_rhs->offset;
8095 }
8096
8097 /* Read a whole compilation unit into a linked list of dies.  */
8098
8099 static struct die_info *
8100 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8101 {
8102   struct die_reader_specs reader_specs;
8103   int read_abbrevs = 0;
8104   struct cleanup *back_to = NULL;
8105   struct die_info *die;
8106
8107   if (cu->dwarf2_abbrevs == NULL)
8108     {
8109       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8110       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8111       read_abbrevs = 1;
8112     }
8113
8114   gdb_assert (cu->die_hash == NULL);
8115   cu->die_hash
8116     = htab_create_alloc_ex (cu->header.length / 12,
8117                             die_hash,
8118                             die_eq,
8119                             NULL,
8120                             &cu->comp_unit_obstack,
8121                             hashtab_obstack_allocate,
8122                             dummy_obstack_deallocate);
8123
8124   init_cu_die_reader (&reader_specs, cu);
8125
8126   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8127
8128   if (read_abbrevs)
8129     do_cleanups (back_to);
8130
8131   return die;
8132 }
8133
8134 /* Main entry point for reading a DIE and all children.
8135    Read the DIE and dump it if requested.  */
8136
8137 static struct die_info *
8138 read_die_and_children (const struct die_reader_specs *reader,
8139                        gdb_byte *info_ptr,
8140                        gdb_byte **new_info_ptr,
8141                        struct die_info *parent)
8142 {
8143   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8144                                                      new_info_ptr, parent);
8145
8146   if (dwarf2_die_debug)
8147     {
8148       fprintf_unfiltered (gdb_stdlog,
8149                           "\nRead die from %s of %s:\n",
8150                           reader->buffer == dwarf2_per_objfile->info.buffer
8151                           ? ".debug_info"
8152                           : reader->buffer == dwarf2_per_objfile->types.buffer
8153                           ? ".debug_types"
8154                           : "unknown section",
8155                           reader->abfd->filename);
8156       dump_die (result, dwarf2_die_debug);
8157     }
8158
8159   return result;
8160 }
8161
8162 /* Read a single die and all its descendents.  Set the die's sibling
8163    field to NULL; set other fields in the die correctly, and set all
8164    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8165    location of the info_ptr after reading all of those dies.  PARENT
8166    is the parent of the die in question.  */
8167
8168 static struct die_info *
8169 read_die_and_children_1 (const struct die_reader_specs *reader,
8170                          gdb_byte *info_ptr,
8171                          gdb_byte **new_info_ptr,
8172                          struct die_info *parent)
8173 {
8174   struct die_info *die;
8175   gdb_byte *cur_ptr;
8176   int has_children;
8177
8178   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8179   if (die == NULL)
8180     {
8181       *new_info_ptr = cur_ptr;
8182       return NULL;
8183     }
8184   store_in_ref_table (die, reader->cu);
8185
8186   if (has_children)
8187     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8188   else
8189     {
8190       die->child = NULL;
8191       *new_info_ptr = cur_ptr;
8192     }
8193
8194   die->sibling = NULL;
8195   die->parent = parent;
8196   return die;
8197 }
8198
8199 /* Read a die, all of its descendents, and all of its siblings; set
8200    all of the fields of all of the dies correctly.  Arguments are as
8201    in read_die_and_children.  */
8202
8203 static struct die_info *
8204 read_die_and_siblings (const struct die_reader_specs *reader,
8205                        gdb_byte *info_ptr,
8206                        gdb_byte **new_info_ptr,
8207                        struct die_info *parent)
8208 {
8209   struct die_info *first_die, *last_sibling;
8210   gdb_byte *cur_ptr;
8211
8212   cur_ptr = info_ptr;
8213   first_die = last_sibling = NULL;
8214
8215   while (1)
8216     {
8217       struct die_info *die
8218         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8219
8220       if (die == NULL)
8221         {
8222           *new_info_ptr = cur_ptr;
8223           return first_die;
8224         }
8225
8226       if (!first_die)
8227         first_die = die;
8228       else
8229         last_sibling->sibling = die;
8230
8231       last_sibling = die;
8232     }
8233 }
8234
8235 /* Read the die from the .debug_info section buffer.  Set DIEP to
8236    point to a newly allocated die with its information, except for its
8237    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8238    whether the die has children or not.  */
8239
8240 static gdb_byte *
8241 read_full_die (const struct die_reader_specs *reader,
8242                struct die_info **diep, gdb_byte *info_ptr,
8243                int *has_children)
8244 {
8245   unsigned int abbrev_number, bytes_read, i, offset;
8246   struct abbrev_info *abbrev;
8247   struct die_info *die;
8248   struct dwarf2_cu *cu = reader->cu;
8249   bfd *abfd = reader->abfd;
8250
8251   offset = info_ptr - reader->buffer;
8252   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8253   info_ptr += bytes_read;
8254   if (!abbrev_number)
8255     {
8256       *diep = NULL;
8257       *has_children = 0;
8258       return info_ptr;
8259     }
8260
8261   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8262   if (!abbrev)
8263     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8264            abbrev_number,
8265            bfd_get_filename (abfd));
8266
8267   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8268   die->offset = offset;
8269   die->tag = abbrev->tag;
8270   die->abbrev = abbrev_number;
8271
8272   die->num_attrs = abbrev->num_attrs;
8273
8274   for (i = 0; i < abbrev->num_attrs; ++i)
8275     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8276                                abfd, info_ptr, cu);
8277
8278   *diep = die;
8279   *has_children = abbrev->has_children;
8280   return info_ptr;
8281 }
8282
8283 /* In DWARF version 2, the description of the debugging information is
8284    stored in a separate .debug_abbrev section.  Before we read any
8285    dies from a section we read in all abbreviations and install them
8286    in a hash table.  This function also sets flags in CU describing
8287    the data found in the abbrev table.  */
8288
8289 static void
8290 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8291 {
8292   struct comp_unit_head *cu_header = &cu->header;
8293   gdb_byte *abbrev_ptr;
8294   struct abbrev_info *cur_abbrev;
8295   unsigned int abbrev_number, bytes_read, abbrev_name;
8296   unsigned int abbrev_form, hash_number;
8297   struct attr_abbrev *cur_attrs;
8298   unsigned int allocated_attrs;
8299
8300   /* Initialize dwarf2 abbrevs */
8301   obstack_init (&cu->abbrev_obstack);
8302   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8303                                       (ABBREV_HASH_SIZE
8304                                        * sizeof (struct abbrev_info *)));
8305   memset (cu->dwarf2_abbrevs, 0,
8306           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8307
8308   dwarf2_read_section (dwarf2_per_objfile->objfile,
8309                        &dwarf2_per_objfile->abbrev);
8310   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8311   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8312   abbrev_ptr += bytes_read;
8313
8314   allocated_attrs = ATTR_ALLOC_CHUNK;
8315   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8316
8317   /* loop until we reach an abbrev number of 0 */
8318   while (abbrev_number)
8319     {
8320       cur_abbrev = dwarf_alloc_abbrev (cu);
8321
8322       /* read in abbrev header */
8323       cur_abbrev->number = abbrev_number;
8324       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8325       abbrev_ptr += bytes_read;
8326       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8327       abbrev_ptr += 1;
8328
8329       if (cur_abbrev->tag == DW_TAG_namespace)
8330         cu->has_namespace_info = 1;
8331
8332       /* now read in declarations */
8333       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8334       abbrev_ptr += bytes_read;
8335       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8336       abbrev_ptr += bytes_read;
8337       while (abbrev_name)
8338         {
8339           if (cur_abbrev->num_attrs == allocated_attrs)
8340             {
8341               allocated_attrs += ATTR_ALLOC_CHUNK;
8342               cur_attrs
8343                 = xrealloc (cur_attrs, (allocated_attrs
8344                                         * sizeof (struct attr_abbrev)));
8345             }
8346
8347           /* Record whether this compilation unit might have
8348              inter-compilation-unit references.  If we don't know what form
8349              this attribute will have, then it might potentially be a
8350              DW_FORM_ref_addr, so we conservatively expect inter-CU
8351              references.  */
8352
8353           if (abbrev_form == DW_FORM_ref_addr
8354               || abbrev_form == DW_FORM_indirect)
8355             cu->has_form_ref_addr = 1;
8356
8357           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8358           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8359           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8360           abbrev_ptr += bytes_read;
8361           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8362           abbrev_ptr += bytes_read;
8363         }
8364
8365       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8366                                          (cur_abbrev->num_attrs
8367                                           * sizeof (struct attr_abbrev)));
8368       memcpy (cur_abbrev->attrs, cur_attrs,
8369               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8370
8371       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8372       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8373       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8374
8375       /* Get next abbreviation.
8376          Under Irix6 the abbreviations for a compilation unit are not
8377          always properly terminated with an abbrev number of 0.
8378          Exit loop if we encounter an abbreviation which we have
8379          already read (which means we are about to read the abbreviations
8380          for the next compile unit) or if the end of the abbreviation
8381          table is reached.  */
8382       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8383           >= dwarf2_per_objfile->abbrev.size)
8384         break;
8385       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8386       abbrev_ptr += bytes_read;
8387       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8388         break;
8389     }
8390
8391   xfree (cur_attrs);
8392 }
8393
8394 /* Release the memory used by the abbrev table for a compilation unit.  */
8395
8396 static void
8397 dwarf2_free_abbrev_table (void *ptr_to_cu)
8398 {
8399   struct dwarf2_cu *cu = ptr_to_cu;
8400
8401   obstack_free (&cu->abbrev_obstack, NULL);
8402   cu->dwarf2_abbrevs = NULL;
8403 }
8404
8405 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8406
8407 static struct abbrev_info *
8408 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8409 {
8410   unsigned int hash_number;
8411   struct abbrev_info *abbrev;
8412
8413   hash_number = number % ABBREV_HASH_SIZE;
8414   abbrev = cu->dwarf2_abbrevs[hash_number];
8415
8416   while (abbrev)
8417     {
8418       if (abbrev->number == number)
8419         return abbrev;
8420       else
8421         abbrev = abbrev->next;
8422     }
8423   return NULL;
8424 }
8425
8426 /* Returns nonzero if TAG represents a type that we might generate a partial
8427    symbol for.  */
8428
8429 static int
8430 is_type_tag_for_partial (int tag)
8431 {
8432   switch (tag)
8433     {
8434 #if 0
8435     /* Some types that would be reasonable to generate partial symbols for,
8436        that we don't at present.  */
8437     case DW_TAG_array_type:
8438     case DW_TAG_file_type:
8439     case DW_TAG_ptr_to_member_type:
8440     case DW_TAG_set_type:
8441     case DW_TAG_string_type:
8442     case DW_TAG_subroutine_type:
8443 #endif
8444     case DW_TAG_base_type:
8445     case DW_TAG_class_type:
8446     case DW_TAG_interface_type:
8447     case DW_TAG_enumeration_type:
8448     case DW_TAG_structure_type:
8449     case DW_TAG_subrange_type:
8450     case DW_TAG_typedef:
8451     case DW_TAG_union_type:
8452       return 1;
8453     default:
8454       return 0;
8455     }
8456 }
8457
8458 /* Load all DIEs that are interesting for partial symbols into memory.  */
8459
8460 static struct partial_die_info *
8461 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8462                    int building_psymtab, struct dwarf2_cu *cu)
8463 {
8464   struct partial_die_info *part_die;
8465   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8466   struct abbrev_info *abbrev;
8467   unsigned int bytes_read;
8468   unsigned int load_all = 0;
8469
8470   int nesting_level = 1;
8471
8472   parent_die = NULL;
8473   last_die = NULL;
8474
8475   if (cu->per_cu && cu->per_cu->load_all_dies)
8476     load_all = 1;
8477
8478   cu->partial_dies
8479     = htab_create_alloc_ex (cu->header.length / 12,
8480                             partial_die_hash,
8481                             partial_die_eq,
8482                             NULL,
8483                             &cu->comp_unit_obstack,
8484                             hashtab_obstack_allocate,
8485                             dummy_obstack_deallocate);
8486
8487   part_die = obstack_alloc (&cu->comp_unit_obstack,
8488                             sizeof (struct partial_die_info));
8489
8490   while (1)
8491     {
8492       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8493
8494       /* A NULL abbrev means the end of a series of children.  */
8495       if (abbrev == NULL)
8496         {
8497           if (--nesting_level == 0)
8498             {
8499               /* PART_DIE was probably the last thing allocated on the
8500                  comp_unit_obstack, so we could call obstack_free
8501                  here.  We don't do that because the waste is small,
8502                  and will be cleaned up when we're done with this
8503                  compilation unit.  This way, we're also more robust
8504                  against other users of the comp_unit_obstack.  */
8505               return first_die;
8506             }
8507           info_ptr += bytes_read;
8508           last_die = parent_die;
8509           parent_die = parent_die->die_parent;
8510           continue;
8511         }
8512
8513       /* Check for template arguments.  We never save these; if
8514          they're seen, we just mark the parent, and go on our way.  */
8515       if (parent_die != NULL
8516           && cu->language == language_cplus
8517           && (abbrev->tag == DW_TAG_template_type_param
8518               || abbrev->tag == DW_TAG_template_value_param))
8519         {
8520           parent_die->has_template_arguments = 1;
8521
8522           if (!load_all)
8523             {
8524               /* We don't need a partial DIE for the template argument.  */
8525               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8526                                        cu);
8527               continue;
8528             }
8529         }
8530
8531       /* We only recurse into subprograms looking for template arguments.
8532          Skip their other children.  */
8533       if (!load_all
8534           && cu->language == language_cplus
8535           && parent_die != NULL
8536           && parent_die->tag == DW_TAG_subprogram)
8537         {
8538           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8539           continue;
8540         }
8541
8542       /* Check whether this DIE is interesting enough to save.  Normally
8543          we would not be interested in members here, but there may be
8544          later variables referencing them via DW_AT_specification (for
8545          static members).  */
8546       if (!load_all
8547           && !is_type_tag_for_partial (abbrev->tag)
8548           && abbrev->tag != DW_TAG_constant
8549           && abbrev->tag != DW_TAG_enumerator
8550           && abbrev->tag != DW_TAG_subprogram
8551           && abbrev->tag != DW_TAG_lexical_block
8552           && abbrev->tag != DW_TAG_variable
8553           && abbrev->tag != DW_TAG_namespace
8554           && abbrev->tag != DW_TAG_module
8555           && abbrev->tag != DW_TAG_member)
8556         {
8557           /* Otherwise we skip to the next sibling, if any.  */
8558           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8559           continue;
8560         }
8561
8562       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8563                                    buffer, info_ptr, cu);
8564
8565       /* This two-pass algorithm for processing partial symbols has a
8566          high cost in cache pressure.  Thus, handle some simple cases
8567          here which cover the majority of C partial symbols.  DIEs
8568          which neither have specification tags in them, nor could have
8569          specification tags elsewhere pointing at them, can simply be
8570          processed and discarded.
8571
8572          This segment is also optional; scan_partial_symbols and
8573          add_partial_symbol will handle these DIEs if we chain
8574          them in normally.  When compilers which do not emit large
8575          quantities of duplicate debug information are more common,
8576          this code can probably be removed.  */
8577
8578       /* Any complete simple types at the top level (pretty much all
8579          of them, for a language without namespaces), can be processed
8580          directly.  */
8581       if (parent_die == NULL
8582           && part_die->has_specification == 0
8583           && part_die->is_declaration == 0
8584           && (part_die->tag == DW_TAG_typedef
8585               || part_die->tag == DW_TAG_base_type
8586               || part_die->tag == DW_TAG_subrange_type))
8587         {
8588           if (building_psymtab && part_die->name != NULL)
8589             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8590                                  VAR_DOMAIN, LOC_TYPEDEF,
8591                                  &cu->objfile->static_psymbols,
8592                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8593           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8594           continue;
8595         }
8596
8597       /* If we're at the second level, and we're an enumerator, and
8598          our parent has no specification (meaning possibly lives in a
8599          namespace elsewhere), then we can add the partial symbol now
8600          instead of queueing it.  */
8601       if (part_die->tag == DW_TAG_enumerator
8602           && parent_die != NULL
8603           && parent_die->die_parent == NULL
8604           && parent_die->tag == DW_TAG_enumeration_type
8605           && parent_die->has_specification == 0)
8606         {
8607           if (part_die->name == NULL)
8608             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8609           else if (building_psymtab)
8610             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8611                                  VAR_DOMAIN, LOC_CONST,
8612                                  (cu->language == language_cplus
8613                                   || cu->language == language_java)
8614                                  ? &cu->objfile->global_psymbols
8615                                  : &cu->objfile->static_psymbols,
8616                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8617
8618           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8619           continue;
8620         }
8621
8622       /* We'll save this DIE so link it in.  */
8623       part_die->die_parent = parent_die;
8624       part_die->die_sibling = NULL;
8625       part_die->die_child = NULL;
8626
8627       if (last_die && last_die == parent_die)
8628         last_die->die_child = part_die;
8629       else if (last_die)
8630         last_die->die_sibling = part_die;
8631
8632       last_die = part_die;
8633
8634       if (first_die == NULL)
8635         first_die = part_die;
8636
8637       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8638          find interesting need to be in the hash table, because we
8639          also have the parent/sibling/child chains; only those that we
8640          might refer to by offset later during partial symbol reading.
8641
8642          For now this means things that might have be the target of a
8643          DW_AT_specification, DW_AT_abstract_origin, or
8644          DW_AT_extension.  DW_AT_extension will refer only to
8645          namespaces; DW_AT_abstract_origin refers to functions (and
8646          many things under the function DIE, but we do not recurse
8647          into function DIEs during partial symbol reading) and
8648          possibly variables as well; DW_AT_specification refers to
8649          declarations.  Declarations ought to have the DW_AT_declaration
8650          flag.  It happens that GCC forgets to put it in sometimes, but
8651          only for functions, not for types.
8652
8653          Adding more things than necessary to the hash table is harmless
8654          except for the performance cost.  Adding too few will result in
8655          wasted time in find_partial_die, when we reread the compilation
8656          unit with load_all_dies set.  */
8657
8658       if (load_all
8659           || abbrev->tag == DW_TAG_constant
8660           || abbrev->tag == DW_TAG_subprogram
8661           || abbrev->tag == DW_TAG_variable
8662           || abbrev->tag == DW_TAG_namespace
8663           || part_die->is_declaration)
8664         {
8665           void **slot;
8666
8667           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8668                                            part_die->offset, INSERT);
8669           *slot = part_die;
8670         }
8671
8672       part_die = obstack_alloc (&cu->comp_unit_obstack,
8673                                 sizeof (struct partial_die_info));
8674
8675       /* For some DIEs we want to follow their children (if any).  For C
8676          we have no reason to follow the children of structures; for other
8677          languages we have to, so that we can get at method physnames
8678          to infer fully qualified class names, for DW_AT_specification,
8679          and for C++ template arguments.  For C++, we also look one level
8680          inside functions to find template arguments (if the name of the
8681          function does not already contain the template arguments).
8682
8683          For Ada, we need to scan the children of subprograms and lexical
8684          blocks as well because Ada allows the definition of nested
8685          entities that could be interesting for the debugger, such as
8686          nested subprograms for instance.  */
8687       if (last_die->has_children
8688           && (load_all
8689               || last_die->tag == DW_TAG_namespace
8690               || last_die->tag == DW_TAG_module
8691               || last_die->tag == DW_TAG_enumeration_type
8692               || (cu->language == language_cplus
8693                   && last_die->tag == DW_TAG_subprogram
8694                   && (last_die->name == NULL
8695                       || strchr (last_die->name, '<') == NULL))
8696               || (cu->language != language_c
8697                   && (last_die->tag == DW_TAG_class_type
8698                       || last_die->tag == DW_TAG_interface_type
8699                       || last_die->tag == DW_TAG_structure_type
8700                       || last_die->tag == DW_TAG_union_type))
8701               || (cu->language == language_ada
8702                   && (last_die->tag == DW_TAG_subprogram
8703                       || last_die->tag == DW_TAG_lexical_block))))
8704         {
8705           nesting_level++;
8706           parent_die = last_die;
8707           continue;
8708         }
8709
8710       /* Otherwise we skip to the next sibling, if any.  */
8711       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8712
8713       /* Back to the top, do it again.  */
8714     }
8715 }
8716
8717 /* Read a minimal amount of information into the minimal die structure.  */
8718
8719 static gdb_byte *
8720 read_partial_die (struct partial_die_info *part_die,
8721                   struct abbrev_info *abbrev,
8722                   unsigned int abbrev_len, bfd *abfd,
8723                   gdb_byte *buffer, gdb_byte *info_ptr,
8724                   struct dwarf2_cu *cu)
8725 {
8726   unsigned int i;
8727   struct attribute attr;
8728   int has_low_pc_attr = 0;
8729   int has_high_pc_attr = 0;
8730
8731   memset (part_die, 0, sizeof (struct partial_die_info));
8732
8733   part_die->offset = info_ptr - buffer;
8734
8735   info_ptr += abbrev_len;
8736
8737   if (abbrev == NULL)
8738     return info_ptr;
8739
8740   part_die->tag = abbrev->tag;
8741   part_die->has_children = abbrev->has_children;
8742
8743   for (i = 0; i < abbrev->num_attrs; ++i)
8744     {
8745       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8746
8747       /* Store the data if it is of an attribute we want to keep in a
8748          partial symbol table.  */
8749       switch (attr.name)
8750         {
8751         case DW_AT_name:
8752           switch (part_die->tag)
8753             {
8754             case DW_TAG_compile_unit:
8755             case DW_TAG_type_unit:
8756               /* Compilation units have a DW_AT_name that is a filename, not
8757                  a source language identifier.  */
8758             case DW_TAG_enumeration_type:
8759             case DW_TAG_enumerator:
8760               /* These tags always have simple identifiers already; no need
8761                  to canonicalize them.  */
8762               part_die->name = DW_STRING (&attr);
8763               break;
8764             default:
8765               part_die->name
8766                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8767                                             &cu->objfile->objfile_obstack);
8768               break;
8769             }
8770           break;
8771         case DW_AT_linkage_name:
8772         case DW_AT_MIPS_linkage_name:
8773           /* Note that both forms of linkage name might appear.  We
8774              assume they will be the same, and we only store the last
8775              one we see.  */
8776           if (cu->language == language_ada)
8777             part_die->name = DW_STRING (&attr);
8778           part_die->linkage_name = DW_STRING (&attr);
8779           break;
8780         case DW_AT_low_pc:
8781           has_low_pc_attr = 1;
8782           part_die->lowpc = DW_ADDR (&attr);
8783           break;
8784         case DW_AT_high_pc:
8785           has_high_pc_attr = 1;
8786           part_die->highpc = DW_ADDR (&attr);
8787           break;
8788         case DW_AT_location:
8789           /* Support the .debug_loc offsets */
8790           if (attr_form_is_block (&attr))
8791             {
8792                part_die->locdesc = DW_BLOCK (&attr);
8793             }
8794           else if (attr_form_is_section_offset (&attr))
8795             {
8796               dwarf2_complex_location_expr_complaint ();
8797             }
8798           else
8799             {
8800               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8801                                                      "partial symbol information");
8802             }
8803           break;
8804         case DW_AT_external:
8805           part_die->is_external = DW_UNSND (&attr);
8806           break;
8807         case DW_AT_declaration:
8808           part_die->is_declaration = DW_UNSND (&attr);
8809           break;
8810         case DW_AT_type:
8811           part_die->has_type = 1;
8812           break;
8813         case DW_AT_abstract_origin:
8814         case DW_AT_specification:
8815         case DW_AT_extension:
8816           part_die->has_specification = 1;
8817           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8818           break;
8819         case DW_AT_sibling:
8820           /* Ignore absolute siblings, they might point outside of
8821              the current compile unit.  */
8822           if (attr.form == DW_FORM_ref_addr)
8823             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8824           else
8825             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8826           break;
8827         case DW_AT_byte_size:
8828           part_die->has_byte_size = 1;
8829           break;
8830         case DW_AT_calling_convention:
8831           /* DWARF doesn't provide a way to identify a program's source-level
8832              entry point.  DW_AT_calling_convention attributes are only meant
8833              to describe functions' calling conventions.
8834
8835              However, because it's a necessary piece of information in
8836              Fortran, and because DW_CC_program is the only piece of debugging
8837              information whose definition refers to a 'main program' at all,
8838              several compilers have begun marking Fortran main programs with
8839              DW_CC_program --- even when those functions use the standard
8840              calling conventions.
8841
8842              So until DWARF specifies a way to provide this information and
8843              compilers pick up the new representation, we'll support this
8844              practice.  */
8845           if (DW_UNSND (&attr) == DW_CC_program
8846               && cu->language == language_fortran)
8847             set_main_name (part_die->name);
8848           break;
8849         default:
8850           break;
8851         }
8852     }
8853
8854   /* When using the GNU linker, .gnu.linkonce. sections are used to
8855      eliminate duplicate copies of functions and vtables and such.
8856      The linker will arbitrarily choose one and discard the others.
8857      The AT_*_pc values for such functions refer to local labels in
8858      these sections.  If the section from that file was discarded, the
8859      labels are not in the output, so the relocs get a value of 0.
8860      If this is a discarded function, mark the pc bounds as invalid,
8861      so that GDB will ignore it.  */
8862   if (has_low_pc_attr && has_high_pc_attr
8863       && part_die->lowpc < part_die->highpc
8864       && (part_die->lowpc != 0
8865           || dwarf2_per_objfile->has_section_at_zero))
8866     part_die->has_pc_info = 1;
8867
8868   return info_ptr;
8869 }
8870
8871 /* Find a cached partial DIE at OFFSET in CU.  */
8872
8873 static struct partial_die_info *
8874 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8875 {
8876   struct partial_die_info *lookup_die = NULL;
8877   struct partial_die_info part_die;
8878
8879   part_die.offset = offset;
8880   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8881
8882   return lookup_die;
8883 }
8884
8885 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8886    except in the case of .debug_types DIEs which do not reference
8887    outside their CU (they do however referencing other types via
8888    DW_FORM_sig8).  */
8889
8890 static struct partial_die_info *
8891 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8892 {
8893   struct dwarf2_per_cu_data *per_cu = NULL;
8894   struct partial_die_info *pd = NULL;
8895
8896   if (cu->per_cu->from_debug_types)
8897     {
8898       pd = find_partial_die_in_comp_unit (offset, cu);
8899       if (pd != NULL)
8900         return pd;
8901       goto not_found;
8902     }
8903
8904   if (offset_in_cu_p (&cu->header, offset))
8905     {
8906       pd = find_partial_die_in_comp_unit (offset, cu);
8907       if (pd != NULL)
8908         return pd;
8909     }
8910
8911   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8912
8913   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8914     load_partial_comp_unit (per_cu, cu->objfile);
8915
8916   per_cu->cu->last_used = 0;
8917   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8918
8919   if (pd == NULL && per_cu->load_all_dies == 0)
8920     {
8921       struct cleanup *back_to;
8922       struct partial_die_info comp_unit_die;
8923       struct abbrev_info *abbrev;
8924       unsigned int bytes_read;
8925       char *info_ptr;
8926
8927       per_cu->load_all_dies = 1;
8928
8929       /* Re-read the DIEs.  */
8930       back_to = make_cleanup (null_cleanup, 0);
8931       if (per_cu->cu->dwarf2_abbrevs == NULL)
8932         {
8933           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8934           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8935         }
8936       info_ptr = (dwarf2_per_objfile->info.buffer
8937                   + per_cu->cu->header.offset
8938                   + per_cu->cu->header.first_die_offset);
8939       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8940       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8941                                    per_cu->cu->objfile->obfd,
8942                                    dwarf2_per_objfile->info.buffer, info_ptr,
8943                                    per_cu->cu);
8944       if (comp_unit_die.has_children)
8945         load_partial_dies (per_cu->cu->objfile->obfd,
8946                            dwarf2_per_objfile->info.buffer, info_ptr,
8947                            0, per_cu->cu);
8948       do_cleanups (back_to);
8949
8950       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8951     }
8952
8953  not_found:
8954
8955   if (pd == NULL)
8956     internal_error (__FILE__, __LINE__,
8957                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8958                     offset, bfd_get_filename (cu->objfile->obfd));
8959   return pd;
8960 }
8961
8962 /* See if we can figure out if the class lives in a namespace.  We do
8963    this by looking for a member function; its demangled name will
8964    contain namespace info, if there is any.  */
8965
8966 static void
8967 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
8968                                   struct dwarf2_cu *cu)
8969 {
8970   /* NOTE: carlton/2003-10-07: Getting the info this way changes
8971      what template types look like, because the demangler
8972      frequently doesn't give the same name as the debug info.  We
8973      could fix this by only using the demangled name to get the
8974      prefix (but see comment in read_structure_type).  */
8975
8976   struct partial_die_info *real_pdi;
8977   struct partial_die_info *child_pdi;
8978
8979   /* If this DIE (this DIE's specification, if any) has a parent, then
8980      we should not do this.  We'll prepend the parent's fully qualified
8981      name when we create the partial symbol.  */
8982
8983   real_pdi = struct_pdi;
8984   while (real_pdi->has_specification)
8985     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
8986
8987   if (real_pdi->die_parent != NULL)
8988     return;
8989
8990   for (child_pdi = struct_pdi->die_child;
8991        child_pdi != NULL;
8992        child_pdi = child_pdi->die_sibling)
8993     {
8994       if (child_pdi->tag == DW_TAG_subprogram
8995           && child_pdi->linkage_name != NULL)
8996         {
8997           char *actual_class_name
8998             = language_class_name_from_physname (cu->language_defn,
8999                                                  child_pdi->linkage_name);
9000           if (actual_class_name != NULL)
9001             {
9002               struct_pdi->name
9003                 = obsavestring (actual_class_name,
9004                                 strlen (actual_class_name),
9005                                 &cu->objfile->objfile_obstack);
9006               xfree (actual_class_name);
9007             }
9008           break;
9009         }
9010     }
9011 }
9012
9013 /* Adjust PART_DIE before generating a symbol for it.  This function
9014    may set the is_external flag or change the DIE's name.  */
9015
9016 static void
9017 fixup_partial_die (struct partial_die_info *part_die,
9018                    struct dwarf2_cu *cu)
9019 {
9020   /* Once we've fixed up a die, there's no point in doing so again.
9021      This also avoids a memory leak if we were to call
9022      guess_partial_die_structure_name multiple times.  */
9023   if (part_die->fixup_called)
9024     return;
9025
9026   /* If we found a reference attribute and the DIE has no name, try
9027      to find a name in the referred to DIE.  */
9028
9029   if (part_die->name == NULL && part_die->has_specification)
9030     {
9031       struct partial_die_info *spec_die;
9032
9033       spec_die = find_partial_die (part_die->spec_offset, cu);
9034
9035       fixup_partial_die (spec_die, cu);
9036
9037       if (spec_die->name)
9038         {
9039           part_die->name = spec_die->name;
9040
9041           /* Copy DW_AT_external attribute if it is set.  */
9042           if (spec_die->is_external)
9043             part_die->is_external = spec_die->is_external;
9044         }
9045     }
9046
9047   /* Set default names for some unnamed DIEs.  */
9048
9049   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9050     part_die->name = "(anonymous namespace)";
9051
9052   /* If there is no parent die to provide a namespace, and there are
9053      children, see if we can determine the namespace from their linkage
9054      name.
9055      NOTE: We need to do this even if cu->has_namespace_info != 0.
9056      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9057   if (cu->language == language_cplus
9058       && dwarf2_per_objfile->types.asection != NULL
9059       && part_die->die_parent == NULL
9060       && part_die->has_children
9061       && (part_die->tag == DW_TAG_class_type
9062           || part_die->tag == DW_TAG_structure_type
9063           || part_die->tag == DW_TAG_union_type))
9064     guess_partial_die_structure_name (part_die, cu);
9065
9066   part_die->fixup_called = 1;
9067 }
9068
9069 /* Read an attribute value described by an attribute form.  */
9070
9071 static gdb_byte *
9072 read_attribute_value (struct attribute *attr, unsigned form,
9073                       bfd *abfd, gdb_byte *info_ptr,
9074                       struct dwarf2_cu *cu)
9075 {
9076   struct comp_unit_head *cu_header = &cu->header;
9077   unsigned int bytes_read;
9078   struct dwarf_block *blk;
9079
9080   attr->form = form;
9081   switch (form)
9082     {
9083     case DW_FORM_ref_addr:
9084       if (cu->header.version == 2)
9085         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9086       else
9087         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9088       info_ptr += bytes_read;
9089       break;
9090     case DW_FORM_addr:
9091       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9092       info_ptr += bytes_read;
9093       break;
9094     case DW_FORM_block2:
9095       blk = dwarf_alloc_block (cu);
9096       blk->size = read_2_bytes (abfd, info_ptr);
9097       info_ptr += 2;
9098       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9099       info_ptr += blk->size;
9100       DW_BLOCK (attr) = blk;
9101       break;
9102     case DW_FORM_block4:
9103       blk = dwarf_alloc_block (cu);
9104       blk->size = read_4_bytes (abfd, info_ptr);
9105       info_ptr += 4;
9106       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9107       info_ptr += blk->size;
9108       DW_BLOCK (attr) = blk;
9109       break;
9110     case DW_FORM_data2:
9111       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9112       info_ptr += 2;
9113       break;
9114     case DW_FORM_data4:
9115       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9116       info_ptr += 4;
9117       break;
9118     case DW_FORM_data8:
9119       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9120       info_ptr += 8;
9121       break;
9122     case DW_FORM_sec_offset:
9123       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9124       info_ptr += bytes_read;
9125       break;
9126     case DW_FORM_string:
9127       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9128       DW_STRING_IS_CANONICAL (attr) = 0;
9129       info_ptr += bytes_read;
9130       break;
9131     case DW_FORM_strp:
9132       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9133                                                &bytes_read);
9134       DW_STRING_IS_CANONICAL (attr) = 0;
9135       info_ptr += bytes_read;
9136       break;
9137     case DW_FORM_exprloc:
9138     case DW_FORM_block:
9139       blk = dwarf_alloc_block (cu);
9140       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9141       info_ptr += bytes_read;
9142       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9143       info_ptr += blk->size;
9144       DW_BLOCK (attr) = blk;
9145       break;
9146     case DW_FORM_block1:
9147       blk = dwarf_alloc_block (cu);
9148       blk->size = read_1_byte (abfd, info_ptr);
9149       info_ptr += 1;
9150       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9151       info_ptr += blk->size;
9152       DW_BLOCK (attr) = blk;
9153       break;
9154     case DW_FORM_data1:
9155       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9156       info_ptr += 1;
9157       break;
9158     case DW_FORM_flag:
9159       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9160       info_ptr += 1;
9161       break;
9162     case DW_FORM_flag_present:
9163       DW_UNSND (attr) = 1;
9164       break;
9165     case DW_FORM_sdata:
9166       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9167       info_ptr += bytes_read;
9168       break;
9169     case DW_FORM_udata:
9170       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9171       info_ptr += bytes_read;
9172       break;
9173     case DW_FORM_ref1:
9174       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9175       info_ptr += 1;
9176       break;
9177     case DW_FORM_ref2:
9178       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9179       info_ptr += 2;
9180       break;
9181     case DW_FORM_ref4:
9182       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9183       info_ptr += 4;
9184       break;
9185     case DW_FORM_ref8:
9186       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9187       info_ptr += 8;
9188       break;
9189     case DW_FORM_sig8:
9190       /* Convert the signature to something we can record in DW_UNSND
9191          for later lookup.
9192          NOTE: This is NULL if the type wasn't found.  */
9193       DW_SIGNATURED_TYPE (attr) =
9194         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9195       info_ptr += 8;
9196       break;
9197     case DW_FORM_ref_udata:
9198       DW_ADDR (attr) = (cu->header.offset
9199                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9200       info_ptr += bytes_read;
9201       break;
9202     case DW_FORM_indirect:
9203       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9204       info_ptr += bytes_read;
9205       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9206       break;
9207     default:
9208       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9209              dwarf_form_name (form),
9210              bfd_get_filename (abfd));
9211     }
9212
9213   /* We have seen instances where the compiler tried to emit a byte
9214      size attribute of -1 which ended up being encoded as an unsigned
9215      0xffffffff.  Although 0xffffffff is technically a valid size value,
9216      an object of this size seems pretty unlikely so we can relatively
9217      safely treat these cases as if the size attribute was invalid and
9218      treat them as zero by default.  */
9219   if (attr->name == DW_AT_byte_size
9220       && form == DW_FORM_data4
9221       && DW_UNSND (attr) >= 0xffffffff)
9222     {
9223       complaint
9224         (&symfile_complaints,
9225          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9226          hex_string (DW_UNSND (attr)));
9227       DW_UNSND (attr) = 0;
9228     }
9229
9230   return info_ptr;
9231 }
9232
9233 /* Read an attribute described by an abbreviated attribute.  */
9234
9235 static gdb_byte *
9236 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9237                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9238 {
9239   attr->name = abbrev->name;
9240   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9241 }
9242
9243 /* read dwarf information from a buffer */
9244
9245 static unsigned int
9246 read_1_byte (bfd *abfd, gdb_byte *buf)
9247 {
9248   return bfd_get_8 (abfd, buf);
9249 }
9250
9251 static int
9252 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9253 {
9254   return bfd_get_signed_8 (abfd, buf);
9255 }
9256
9257 static unsigned int
9258 read_2_bytes (bfd *abfd, gdb_byte *buf)
9259 {
9260   return bfd_get_16 (abfd, buf);
9261 }
9262
9263 static int
9264 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9265 {
9266   return bfd_get_signed_16 (abfd, buf);
9267 }
9268
9269 static unsigned int
9270 read_4_bytes (bfd *abfd, gdb_byte *buf)
9271 {
9272   return bfd_get_32 (abfd, buf);
9273 }
9274
9275 static int
9276 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9277 {
9278   return bfd_get_signed_32 (abfd, buf);
9279 }
9280
9281 static ULONGEST
9282 read_8_bytes (bfd *abfd, gdb_byte *buf)
9283 {
9284   return bfd_get_64 (abfd, buf);
9285 }
9286
9287 static CORE_ADDR
9288 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9289               unsigned int *bytes_read)
9290 {
9291   struct comp_unit_head *cu_header = &cu->header;
9292   CORE_ADDR retval = 0;
9293
9294   if (cu_header->signed_addr_p)
9295     {
9296       switch (cu_header->addr_size)
9297         {
9298         case 2:
9299           retval = bfd_get_signed_16 (abfd, buf);
9300           break;
9301         case 4:
9302           retval = bfd_get_signed_32 (abfd, buf);
9303           break;
9304         case 8:
9305           retval = bfd_get_signed_64 (abfd, buf);
9306           break;
9307         default:
9308           internal_error (__FILE__, __LINE__,
9309                           _("read_address: bad switch, signed [in module %s]"),
9310                           bfd_get_filename (abfd));
9311         }
9312     }
9313   else
9314     {
9315       switch (cu_header->addr_size)
9316         {
9317         case 2:
9318           retval = bfd_get_16 (abfd, buf);
9319           break;
9320         case 4:
9321           retval = bfd_get_32 (abfd, buf);
9322           break;
9323         case 8:
9324           retval = bfd_get_64 (abfd, buf);
9325           break;
9326         default:
9327           internal_error (__FILE__, __LINE__,
9328                           _("read_address: bad switch, unsigned [in module %s]"),
9329                           bfd_get_filename (abfd));
9330         }
9331     }
9332
9333   *bytes_read = cu_header->addr_size;
9334   return retval;
9335 }
9336
9337 /* Read the initial length from a section.  The (draft) DWARF 3
9338    specification allows the initial length to take up either 4 bytes
9339    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9340    bytes describe the length and all offsets will be 8 bytes in length
9341    instead of 4.
9342
9343    An older, non-standard 64-bit format is also handled by this
9344    function.  The older format in question stores the initial length
9345    as an 8-byte quantity without an escape value.  Lengths greater
9346    than 2^32 aren't very common which means that the initial 4 bytes
9347    is almost always zero.  Since a length value of zero doesn't make
9348    sense for the 32-bit format, this initial zero can be considered to
9349    be an escape value which indicates the presence of the older 64-bit
9350    format.  As written, the code can't detect (old format) lengths
9351    greater than 4GB.  If it becomes necessary to handle lengths
9352    somewhat larger than 4GB, we could allow other small values (such
9353    as the non-sensical values of 1, 2, and 3) to also be used as
9354    escape values indicating the presence of the old format.
9355
9356    The value returned via bytes_read should be used to increment the
9357    relevant pointer after calling read_initial_length().
9358
9359    [ Note:  read_initial_length() and read_offset() are based on the
9360      document entitled "DWARF Debugging Information Format", revision
9361      3, draft 8, dated November 19, 2001.  This document was obtained
9362      from:
9363
9364         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9365
9366      This document is only a draft and is subject to change.  (So beware.)
9367
9368      Details regarding the older, non-standard 64-bit format were
9369      determined empirically by examining 64-bit ELF files produced by
9370      the SGI toolchain on an IRIX 6.5 machine.
9371
9372      - Kevin, July 16, 2002
9373    ] */
9374
9375 static LONGEST
9376 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9377 {
9378   LONGEST length = bfd_get_32 (abfd, buf);
9379
9380   if (length == 0xffffffff)
9381     {
9382       length = bfd_get_64 (abfd, buf + 4);
9383       *bytes_read = 12;
9384     }
9385   else if (length == 0)
9386     {
9387       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9388       length = bfd_get_64 (abfd, buf);
9389       *bytes_read = 8;
9390     }
9391   else
9392     {
9393       *bytes_read = 4;
9394     }
9395
9396   return length;
9397 }
9398
9399 /* Cover function for read_initial_length.
9400    Returns the length of the object at BUF, and stores the size of the
9401    initial length in *BYTES_READ and stores the size that offsets will be in
9402    *OFFSET_SIZE.
9403    If the initial length size is not equivalent to that specified in
9404    CU_HEADER then issue a complaint.
9405    This is useful when reading non-comp-unit headers.  */
9406
9407 static LONGEST
9408 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9409                                         const struct comp_unit_head *cu_header,
9410                                         unsigned int *bytes_read,
9411                                         unsigned int *offset_size)
9412 {
9413   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9414
9415   gdb_assert (cu_header->initial_length_size == 4
9416               || cu_header->initial_length_size == 8
9417               || cu_header->initial_length_size == 12);
9418
9419   if (cu_header->initial_length_size != *bytes_read)
9420     complaint (&symfile_complaints,
9421                _("intermixed 32-bit and 64-bit DWARF sections"));
9422
9423   *offset_size = (*bytes_read == 4) ? 4 : 8;
9424   return length;
9425 }
9426
9427 /* Read an offset from the data stream.  The size of the offset is
9428    given by cu_header->offset_size.  */
9429
9430 static LONGEST
9431 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9432              unsigned int *bytes_read)
9433 {
9434   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9435
9436   *bytes_read = cu_header->offset_size;
9437   return offset;
9438 }
9439
9440 /* Read an offset from the data stream.  */
9441
9442 static LONGEST
9443 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9444 {
9445   LONGEST retval = 0;
9446
9447   switch (offset_size)
9448     {
9449     case 4:
9450       retval = bfd_get_32 (abfd, buf);
9451       break;
9452     case 8:
9453       retval = bfd_get_64 (abfd, buf);
9454       break;
9455     default:
9456       internal_error (__FILE__, __LINE__,
9457                       _("read_offset_1: bad switch [in module %s]"),
9458                       bfd_get_filename (abfd));
9459     }
9460
9461   return retval;
9462 }
9463
9464 static gdb_byte *
9465 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9466 {
9467   /* If the size of a host char is 8 bits, we can return a pointer
9468      to the buffer, otherwise we have to copy the data to a buffer
9469      allocated on the temporary obstack.  */
9470   gdb_assert (HOST_CHAR_BIT == 8);
9471   return buf;
9472 }
9473
9474 static char *
9475 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9476 {
9477   /* If the size of a host char is 8 bits, we can return a pointer
9478      to the string, otherwise we have to copy the string to a buffer
9479      allocated on the temporary obstack.  */
9480   gdb_assert (HOST_CHAR_BIT == 8);
9481   if (*buf == '\0')
9482     {
9483       *bytes_read_ptr = 1;
9484       return NULL;
9485     }
9486   *bytes_read_ptr = strlen ((char *) buf) + 1;
9487   return (char *) buf;
9488 }
9489
9490 static char *
9491 read_indirect_string (bfd *abfd, gdb_byte *buf,
9492                       const struct comp_unit_head *cu_header,
9493                       unsigned int *bytes_read_ptr)
9494 {
9495   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9496
9497   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9498   if (dwarf2_per_objfile->str.buffer == NULL)
9499     {
9500       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9501                       bfd_get_filename (abfd));
9502       return NULL;
9503     }
9504   if (str_offset >= dwarf2_per_objfile->str.size)
9505     {
9506       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9507                       bfd_get_filename (abfd));
9508       return NULL;
9509     }
9510   gdb_assert (HOST_CHAR_BIT == 8);
9511   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9512     return NULL;
9513   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9514 }
9515
9516 static unsigned long
9517 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9518 {
9519   unsigned long result;
9520   unsigned int num_read;
9521   int i, shift;
9522   unsigned char byte;
9523
9524   result = 0;
9525   shift = 0;
9526   num_read = 0;
9527   i = 0;
9528   while (1)
9529     {
9530       byte = bfd_get_8 (abfd, buf);
9531       buf++;
9532       num_read++;
9533       result |= ((unsigned long)(byte & 127) << shift);
9534       if ((byte & 128) == 0)
9535         {
9536           break;
9537         }
9538       shift += 7;
9539     }
9540   *bytes_read_ptr = num_read;
9541   return result;
9542 }
9543
9544 static long
9545 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9546 {
9547   long result;
9548   int i, shift, num_read;
9549   unsigned char byte;
9550
9551   result = 0;
9552   shift = 0;
9553   num_read = 0;
9554   i = 0;
9555   while (1)
9556     {
9557       byte = bfd_get_8 (abfd, buf);
9558       buf++;
9559       num_read++;
9560       result |= ((long)(byte & 127) << shift);
9561       shift += 7;
9562       if ((byte & 128) == 0)
9563         {
9564           break;
9565         }
9566     }
9567   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9568     result |= -(((long)1) << shift);
9569   *bytes_read_ptr = num_read;
9570   return result;
9571 }
9572
9573 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9574
9575 static gdb_byte *
9576 skip_leb128 (bfd *abfd, gdb_byte *buf)
9577 {
9578   int byte;
9579
9580   while (1)
9581     {
9582       byte = bfd_get_8 (abfd, buf);
9583       buf++;
9584       if ((byte & 128) == 0)
9585         return buf;
9586     }
9587 }
9588
9589 static void
9590 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9591 {
9592   switch (lang)
9593     {
9594     case DW_LANG_C89:
9595     case DW_LANG_C99:
9596     case DW_LANG_C:
9597       cu->language = language_c;
9598       break;
9599     case DW_LANG_C_plus_plus:
9600       cu->language = language_cplus;
9601       break;
9602     case DW_LANG_D:
9603       cu->language = language_d;
9604       break;
9605     case DW_LANG_Fortran77:
9606     case DW_LANG_Fortran90:
9607     case DW_LANG_Fortran95:
9608       cu->language = language_fortran;
9609       break;
9610     case DW_LANG_Mips_Assembler:
9611       cu->language = language_asm;
9612       break;
9613     case DW_LANG_Java:
9614       cu->language = language_java;
9615       break;
9616     case DW_LANG_Ada83:
9617     case DW_LANG_Ada95:
9618       cu->language = language_ada;
9619       break;
9620     case DW_LANG_Modula2:
9621       cu->language = language_m2;
9622       break;
9623     case DW_LANG_Pascal83:
9624       cu->language = language_pascal;
9625       break;
9626     case DW_LANG_ObjC:
9627       cu->language = language_objc;
9628       break;
9629     case DW_LANG_Cobol74:
9630     case DW_LANG_Cobol85:
9631     default:
9632       cu->language = language_minimal;
9633       break;
9634     }
9635   cu->language_defn = language_def (cu->language);
9636 }
9637
9638 /* Return the named attribute or NULL if not there.  */
9639
9640 static struct attribute *
9641 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9642 {
9643   unsigned int i;
9644   struct attribute *spec = NULL;
9645
9646   for (i = 0; i < die->num_attrs; ++i)
9647     {
9648       if (die->attrs[i].name == name)
9649         return &die->attrs[i];
9650       if (die->attrs[i].name == DW_AT_specification
9651           || die->attrs[i].name == DW_AT_abstract_origin)
9652         spec = &die->attrs[i];
9653     }
9654
9655   if (spec)
9656     {
9657       die = follow_die_ref (die, spec, &cu);
9658       return dwarf2_attr (die, name, cu);
9659     }
9660
9661   return NULL;
9662 }
9663
9664 /* Return the named attribute or NULL if not there,
9665    but do not follow DW_AT_specification, etc.
9666    This is for use in contexts where we're reading .debug_types dies.
9667    Following DW_AT_specification, DW_AT_abstract_origin will take us
9668    back up the chain, and we want to go down.  */
9669
9670 static struct attribute *
9671 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9672                        struct dwarf2_cu *cu)
9673 {
9674   unsigned int i;
9675
9676   for (i = 0; i < die->num_attrs; ++i)
9677     if (die->attrs[i].name == name)
9678       return &die->attrs[i];
9679
9680   return NULL;
9681 }
9682
9683 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9684    and holds a non-zero value.  This function should only be used for
9685    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9686
9687 static int
9688 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9689 {
9690   struct attribute *attr = dwarf2_attr (die, name, cu);
9691
9692   return (attr && DW_UNSND (attr));
9693 }
9694
9695 static int
9696 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9697 {
9698   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9699      which value is non-zero.  However, we have to be careful with
9700      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9701      (via dwarf2_flag_true_p) follows this attribute.  So we may
9702      end up accidently finding a declaration attribute that belongs
9703      to a different DIE referenced by the specification attribute,
9704      even though the given DIE does not have a declaration attribute.  */
9705   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9706           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9707 }
9708
9709 /* Return the die giving the specification for DIE, if there is
9710    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9711    containing the return value on output.  If there is no
9712    specification, but there is an abstract origin, that is
9713    returned.  */
9714
9715 static struct die_info *
9716 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9717 {
9718   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9719                                              *spec_cu);
9720
9721   if (spec_attr == NULL)
9722     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9723
9724   if (spec_attr == NULL)
9725     return NULL;
9726   else
9727     return follow_die_ref (die, spec_attr, spec_cu);
9728 }
9729
9730 /* Free the line_header structure *LH, and any arrays and strings it
9731    refers to.  */
9732 static void
9733 free_line_header (struct line_header *lh)
9734 {
9735   if (lh->standard_opcode_lengths)
9736     xfree (lh->standard_opcode_lengths);
9737
9738   /* Remember that all the lh->file_names[i].name pointers are
9739      pointers into debug_line_buffer, and don't need to be freed.  */
9740   if (lh->file_names)
9741     xfree (lh->file_names);
9742
9743   /* Similarly for the include directory names.  */
9744   if (lh->include_dirs)
9745     xfree (lh->include_dirs);
9746
9747   xfree (lh);
9748 }
9749
9750
9751 /* Add an entry to LH's include directory table.  */
9752 static void
9753 add_include_dir (struct line_header *lh, char *include_dir)
9754 {
9755   /* Grow the array if necessary.  */
9756   if (lh->include_dirs_size == 0)
9757     {
9758       lh->include_dirs_size = 1; /* for testing */
9759       lh->include_dirs = xmalloc (lh->include_dirs_size
9760                                   * sizeof (*lh->include_dirs));
9761     }
9762   else if (lh->num_include_dirs >= lh->include_dirs_size)
9763     {
9764       lh->include_dirs_size *= 2;
9765       lh->include_dirs = xrealloc (lh->include_dirs,
9766                                    (lh->include_dirs_size
9767                                     * sizeof (*lh->include_dirs)));
9768     }
9769
9770   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9771 }
9772
9773
9774 /* Add an entry to LH's file name table.  */
9775 static void
9776 add_file_name (struct line_header *lh,
9777                char *name,
9778                unsigned int dir_index,
9779                unsigned int mod_time,
9780                unsigned int length)
9781 {
9782   struct file_entry *fe;
9783
9784   /* Grow the array if necessary.  */
9785   if (lh->file_names_size == 0)
9786     {
9787       lh->file_names_size = 1; /* for testing */
9788       lh->file_names = xmalloc (lh->file_names_size
9789                                 * sizeof (*lh->file_names));
9790     }
9791   else if (lh->num_file_names >= lh->file_names_size)
9792     {
9793       lh->file_names_size *= 2;
9794       lh->file_names = xrealloc (lh->file_names,
9795                                  (lh->file_names_size
9796                                   * sizeof (*lh->file_names)));
9797     }
9798
9799   fe = &lh->file_names[lh->num_file_names++];
9800   fe->name = name;
9801   fe->dir_index = dir_index;
9802   fe->mod_time = mod_time;
9803   fe->length = length;
9804   fe->included_p = 0;
9805   fe->symtab = NULL;
9806 }
9807
9808
9809 /* Read the statement program header starting at OFFSET in
9810    .debug_line, according to the endianness of ABFD.  Return a pointer
9811    to a struct line_header, allocated using xmalloc.
9812
9813    NOTE: the strings in the include directory and file name tables of
9814    the returned object point into debug_line_buffer, and must not be
9815    freed.  */
9816 static struct line_header *
9817 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9818                           struct dwarf2_cu *cu)
9819 {
9820   struct cleanup *back_to;
9821   struct line_header *lh;
9822   gdb_byte *line_ptr;
9823   unsigned int bytes_read, offset_size;
9824   int i;
9825   char *cur_dir, *cur_file;
9826
9827   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9828   if (dwarf2_per_objfile->line.buffer == NULL)
9829     {
9830       complaint (&symfile_complaints, _("missing .debug_line section"));
9831       return 0;
9832     }
9833
9834   /* Make sure that at least there's room for the total_length field.
9835      That could be 12 bytes long, but we're just going to fudge that.  */
9836   if (offset + 4 >= dwarf2_per_objfile->line.size)
9837     {
9838       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9839       return 0;
9840     }
9841
9842   lh = xmalloc (sizeof (*lh));
9843   memset (lh, 0, sizeof (*lh));
9844   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9845                           (void *) lh);
9846
9847   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9848
9849   /* Read in the header.  */
9850   lh->total_length =
9851     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9852                                             &bytes_read, &offset_size);
9853   line_ptr += bytes_read;
9854   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9855                                      + dwarf2_per_objfile->line.size))
9856     {
9857       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9858       return 0;
9859     }
9860   lh->statement_program_end = line_ptr + lh->total_length;
9861   lh->version = read_2_bytes (abfd, line_ptr);
9862   line_ptr += 2;
9863   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9864   line_ptr += offset_size;
9865   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9866   line_ptr += 1;
9867   if (lh->version >= 4)
9868     {
9869       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9870       line_ptr += 1;
9871     }
9872   else
9873     lh->maximum_ops_per_instruction = 1;
9874
9875   if (lh->maximum_ops_per_instruction == 0)
9876     {
9877       lh->maximum_ops_per_instruction = 1;
9878       complaint (&symfile_complaints,
9879                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9880     }
9881
9882   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9883   line_ptr += 1;
9884   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9885   line_ptr += 1;
9886   lh->line_range = read_1_byte (abfd, line_ptr);
9887   line_ptr += 1;
9888   lh->opcode_base = read_1_byte (abfd, line_ptr);
9889   line_ptr += 1;
9890   lh->standard_opcode_lengths
9891     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9892
9893   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9894   for (i = 1; i < lh->opcode_base; ++i)
9895     {
9896       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9897       line_ptr += 1;
9898     }
9899
9900   /* Read directory table.  */
9901   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9902     {
9903       line_ptr += bytes_read;
9904       add_include_dir (lh, cur_dir);
9905     }
9906   line_ptr += bytes_read;
9907
9908   /* Read file name table.  */
9909   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9910     {
9911       unsigned int dir_index, mod_time, length;
9912
9913       line_ptr += bytes_read;
9914       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9915       line_ptr += bytes_read;
9916       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9917       line_ptr += bytes_read;
9918       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9919       line_ptr += bytes_read;
9920
9921       add_file_name (lh, cur_file, dir_index, mod_time, length);
9922     }
9923   line_ptr += bytes_read;
9924   lh->statement_program_start = line_ptr;
9925
9926   if (line_ptr > (dwarf2_per_objfile->line.buffer
9927                   + dwarf2_per_objfile->line.size))
9928     complaint (&symfile_complaints,
9929                _("line number info header doesn't fit in `.debug_line' section"));
9930
9931   discard_cleanups (back_to);
9932   return lh;
9933 }
9934
9935 /* This function exists to work around a bug in certain compilers
9936    (particularly GCC 2.95), in which the first line number marker of a
9937    function does not show up until after the prologue, right before
9938    the second line number marker.  This function shifts ADDRESS down
9939    to the beginning of the function if necessary, and is called on
9940    addresses passed to record_line.  */
9941
9942 static CORE_ADDR
9943 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9944 {
9945   struct function_range *fn;
9946
9947   /* Find the function_range containing address.  */
9948   if (!cu->first_fn)
9949     return address;
9950
9951   if (!cu->cached_fn)
9952     cu->cached_fn = cu->first_fn;
9953
9954   fn = cu->cached_fn;
9955   while (fn)
9956     if (fn->lowpc <= address && fn->highpc > address)
9957       goto found;
9958     else
9959       fn = fn->next;
9960
9961   fn = cu->first_fn;
9962   while (fn && fn != cu->cached_fn)
9963     if (fn->lowpc <= address && fn->highpc > address)
9964       goto found;
9965     else
9966       fn = fn->next;
9967
9968   return address;
9969
9970  found:
9971   if (fn->seen_line)
9972     return address;
9973   if (address != fn->lowpc)
9974     complaint (&symfile_complaints,
9975                _("misplaced first line number at 0x%lx for '%s'"),
9976                (unsigned long) address, fn->name);
9977   fn->seen_line = 1;
9978   return fn->lowpc;
9979 }
9980
9981 /* Subroutine of dwarf_decode_lines to simplify it.
9982    Return the file name of the psymtab for included file FILE_INDEX
9983    in line header LH of PST.
9984    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9985    If space for the result is malloc'd, it will be freed by a cleanup.
9986    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
9987
9988 static char *
9989 psymtab_include_file_name (const struct line_header *lh, int file_index,
9990                            const struct partial_symtab *pst,
9991                            const char *comp_dir)
9992 {
9993   const struct file_entry fe = lh->file_names [file_index];
9994   char *include_name = fe.name;
9995   char *include_name_to_compare = include_name;
9996   char *dir_name = NULL;
9997   char *pst_filename;
9998   int file_is_pst;
9999
10000   if (fe.dir_index)
10001     dir_name = lh->include_dirs[fe.dir_index - 1];
10002
10003   if (!IS_ABSOLUTE_PATH (include_name)
10004       && (dir_name != NULL || comp_dir != NULL))
10005     {
10006       /* Avoid creating a duplicate psymtab for PST.
10007          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10008          Before we do the comparison, however, we need to account
10009          for DIR_NAME and COMP_DIR.
10010          First prepend dir_name (if non-NULL).  If we still don't
10011          have an absolute path prepend comp_dir (if non-NULL).
10012          However, the directory we record in the include-file's
10013          psymtab does not contain COMP_DIR (to match the
10014          corresponding symtab(s)).
10015
10016          Example:
10017
10018          bash$ cd /tmp
10019          bash$ gcc -g ./hello.c
10020          include_name = "hello.c"
10021          dir_name = "."
10022          DW_AT_comp_dir = comp_dir = "/tmp"
10023          DW_AT_name = "./hello.c"  */
10024
10025       if (dir_name != NULL)
10026         {
10027           include_name = concat (dir_name, SLASH_STRING,
10028                                  include_name, (char *)NULL);
10029           include_name_to_compare = include_name;
10030           make_cleanup (xfree, include_name);
10031         }
10032       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10033         {
10034           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10035                                             include_name, (char *)NULL);
10036         }
10037     }
10038
10039   pst_filename = pst->filename;
10040   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10041     {
10042       pst_filename = concat (pst->dirname, SLASH_STRING,
10043                              pst_filename, (char *)NULL);
10044     }
10045
10046   file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
10047
10048   if (include_name_to_compare != include_name)
10049     xfree (include_name_to_compare);
10050   if (pst_filename != pst->filename)
10051     xfree (pst_filename);
10052
10053   if (file_is_pst)
10054     return NULL;
10055   return include_name;
10056 }
10057
10058 /* Decode the Line Number Program (LNP) for the given line_header
10059    structure and CU.  The actual information extracted and the type
10060    of structures created from the LNP depends on the value of PST.
10061
10062    1. If PST is NULL, then this procedure uses the data from the program
10063       to create all necessary symbol tables, and their linetables.
10064
10065    2. If PST is not NULL, this procedure reads the program to determine
10066       the list of files included by the unit represented by PST, and
10067       builds all the associated partial symbol tables.
10068
10069    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10070    It is used for relative paths in the line table.
10071    NOTE: When processing partial symtabs (pst != NULL),
10072    comp_dir == pst->dirname.
10073
10074    NOTE: It is important that psymtabs have the same file name (via strcmp)
10075    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10076    symtab we don't use it in the name of the psymtabs we create.
10077    E.g. expand_line_sal requires this when finding psymtabs to expand.
10078    A good testcase for this is mb-inline.exp.  */
10079
10080 static void
10081 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
10082                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10083 {
10084   gdb_byte *line_ptr, *extended_end;
10085   gdb_byte *line_end;
10086   unsigned int bytes_read, extended_len;
10087   unsigned char op_code, extended_op, adj_opcode;
10088   CORE_ADDR baseaddr;
10089   struct objfile *objfile = cu->objfile;
10090   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10091   const int decode_for_pst_p = (pst != NULL);
10092   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10093
10094   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10095
10096   line_ptr = lh->statement_program_start;
10097   line_end = lh->statement_program_end;
10098
10099   /* Read the statement sequences until there's nothing left.  */
10100   while (line_ptr < line_end)
10101     {
10102       /* state machine registers  */
10103       CORE_ADDR address = 0;
10104       unsigned int file = 1;
10105       unsigned int line = 1;
10106       unsigned int column = 0;
10107       int is_stmt = lh->default_is_stmt;
10108       int basic_block = 0;
10109       int end_sequence = 0;
10110       CORE_ADDR addr;
10111       unsigned char op_index = 0;
10112
10113       if (!decode_for_pst_p && lh->num_file_names >= file)
10114         {
10115           /* Start a subfile for the current file of the state machine.  */
10116           /* lh->include_dirs and lh->file_names are 0-based, but the
10117              directory and file name numbers in the statement program
10118              are 1-based.  */
10119           struct file_entry *fe = &lh->file_names[file - 1];
10120           char *dir = NULL;
10121
10122           if (fe->dir_index)
10123             dir = lh->include_dirs[fe->dir_index - 1];
10124
10125           dwarf2_start_subfile (fe->name, dir, comp_dir);
10126         }
10127
10128       /* Decode the table.  */
10129       while (!end_sequence)
10130         {
10131           op_code = read_1_byte (abfd, line_ptr);
10132           line_ptr += 1;
10133           if (line_ptr > line_end)
10134             {
10135               dwarf2_debug_line_missing_end_sequence_complaint ();
10136               break;
10137             }
10138
10139           if (op_code >= lh->opcode_base)
10140             {
10141               /* Special operand.  */
10142               adj_opcode = op_code - lh->opcode_base;
10143               address += (((op_index + (adj_opcode / lh->line_range))
10144                            / lh->maximum_ops_per_instruction)
10145                           * lh->minimum_instruction_length);
10146               op_index = ((op_index + (adj_opcode / lh->line_range))
10147                           % lh->maximum_ops_per_instruction);
10148               line += lh->line_base + (adj_opcode % lh->line_range);
10149               if (lh->num_file_names < file || file == 0)
10150                 dwarf2_debug_line_missing_file_complaint ();
10151               /* For now we ignore lines not starting on an
10152                  instruction boundary.  */
10153               else if (op_index == 0)
10154                 {
10155                   lh->file_names[file - 1].included_p = 1;
10156                   if (!decode_for_pst_p && is_stmt)
10157                     {
10158                       if (last_subfile != current_subfile)
10159                         {
10160                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10161                           if (last_subfile)
10162                             record_line (last_subfile, 0, addr);
10163                           last_subfile = current_subfile;
10164                         }
10165                       /* Append row to matrix using current values.  */
10166                       addr = check_cu_functions (address, cu);
10167                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10168                       record_line (current_subfile, line, addr);
10169                     }
10170                 }
10171               basic_block = 0;
10172             }
10173           else switch (op_code)
10174             {
10175             case DW_LNS_extended_op:
10176               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10177               line_ptr += bytes_read;
10178               extended_end = line_ptr + extended_len;
10179               extended_op = read_1_byte (abfd, line_ptr);
10180               line_ptr += 1;
10181               switch (extended_op)
10182                 {
10183                 case DW_LNE_end_sequence:
10184                   end_sequence = 1;
10185                   break;
10186                 case DW_LNE_set_address:
10187                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10188                   op_index = 0;
10189                   line_ptr += bytes_read;
10190                   address += baseaddr;
10191                   break;
10192                 case DW_LNE_define_file:
10193                   {
10194                     char *cur_file;
10195                     unsigned int dir_index, mod_time, length;
10196
10197                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10198                     line_ptr += bytes_read;
10199                     dir_index =
10200                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10201                     line_ptr += bytes_read;
10202                     mod_time =
10203                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10204                     line_ptr += bytes_read;
10205                     length =
10206                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10207                     line_ptr += bytes_read;
10208                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10209                   }
10210                   break;
10211                 case DW_LNE_set_discriminator:
10212                   /* The discriminator is not interesting to the debugger;
10213                      just ignore it.  */
10214                   line_ptr = extended_end;
10215                   break;
10216                 default:
10217                   complaint (&symfile_complaints,
10218                              _("mangled .debug_line section"));
10219                   return;
10220                 }
10221               /* Make sure that we parsed the extended op correctly.  If e.g.
10222                  we expected a different address size than the producer used,
10223                  we may have read the wrong number of bytes.  */
10224               if (line_ptr != extended_end)
10225                 {
10226                   complaint (&symfile_complaints,
10227                              _("mangled .debug_line section"));
10228                   return;
10229                 }
10230               break;
10231             case DW_LNS_copy:
10232               if (lh->num_file_names < file || file == 0)
10233                 dwarf2_debug_line_missing_file_complaint ();
10234               else
10235                 {
10236                   lh->file_names[file - 1].included_p = 1;
10237                   if (!decode_for_pst_p && is_stmt)
10238                     {
10239                       if (last_subfile != current_subfile)
10240                         {
10241                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10242                           if (last_subfile)
10243                             record_line (last_subfile, 0, addr);
10244                           last_subfile = current_subfile;
10245                         }
10246                       addr = check_cu_functions (address, cu);
10247                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10248                       record_line (current_subfile, line, addr);
10249                     }
10250                 }
10251               basic_block = 0;
10252               break;
10253             case DW_LNS_advance_pc:
10254               {
10255                 CORE_ADDR adjust
10256                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10257
10258                 address += (((op_index + adjust)
10259                              / lh->maximum_ops_per_instruction)
10260                             * lh->minimum_instruction_length);
10261                 op_index = ((op_index + adjust)
10262                             % lh->maximum_ops_per_instruction);
10263                 line_ptr += bytes_read;
10264               }
10265               break;
10266             case DW_LNS_advance_line:
10267               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10268               line_ptr += bytes_read;
10269               break;
10270             case DW_LNS_set_file:
10271               {
10272                 /* The arrays lh->include_dirs and lh->file_names are
10273                    0-based, but the directory and file name numbers in
10274                    the statement program are 1-based.  */
10275                 struct file_entry *fe;
10276                 char *dir = NULL;
10277
10278                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10279                 line_ptr += bytes_read;
10280                 if (lh->num_file_names < file || file == 0)
10281                   dwarf2_debug_line_missing_file_complaint ();
10282                 else
10283                   {
10284                     fe = &lh->file_names[file - 1];
10285                     if (fe->dir_index)
10286                       dir = lh->include_dirs[fe->dir_index - 1];
10287                     if (!decode_for_pst_p)
10288                       {
10289                         last_subfile = current_subfile;
10290                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10291                       }
10292                   }
10293               }
10294               break;
10295             case DW_LNS_set_column:
10296               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10297               line_ptr += bytes_read;
10298               break;
10299             case DW_LNS_negate_stmt:
10300               is_stmt = (!is_stmt);
10301               break;
10302             case DW_LNS_set_basic_block:
10303               basic_block = 1;
10304               break;
10305             /* Add to the address register of the state machine the
10306                address increment value corresponding to special opcode
10307                255.  I.e., this value is scaled by the minimum
10308                instruction length since special opcode 255 would have
10309                scaled the the increment.  */
10310             case DW_LNS_const_add_pc:
10311               {
10312                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10313
10314                 address += (((op_index + adjust)
10315                              / lh->maximum_ops_per_instruction)
10316                             * lh->minimum_instruction_length);
10317                 op_index = ((op_index + adjust)
10318                             % lh->maximum_ops_per_instruction);
10319               }
10320               break;
10321             case DW_LNS_fixed_advance_pc:
10322               address += read_2_bytes (abfd, line_ptr);
10323               op_index = 0;
10324               line_ptr += 2;
10325               break;
10326             default:
10327               {
10328                 /* Unknown standard opcode, ignore it.  */
10329                 int i;
10330
10331                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10332                   {
10333                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10334                     line_ptr += bytes_read;
10335                   }
10336               }
10337             }
10338         }
10339       if (lh->num_file_names < file || file == 0)
10340         dwarf2_debug_line_missing_file_complaint ();
10341       else
10342         {
10343           lh->file_names[file - 1].included_p = 1;
10344           if (!decode_for_pst_p)
10345             {
10346               addr = gdbarch_addr_bits_remove (gdbarch, address);
10347               record_line (current_subfile, 0, addr);
10348             }
10349         }
10350     }
10351
10352   if (decode_for_pst_p)
10353     {
10354       int file_index;
10355
10356       /* Now that we're done scanning the Line Header Program, we can
10357          create the psymtab of each included file.  */
10358       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10359         if (lh->file_names[file_index].included_p == 1)
10360           {
10361             char *include_name =
10362               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10363             if (include_name != NULL)
10364               dwarf2_create_include_psymtab (include_name, pst, objfile);
10365           }
10366     }
10367   else
10368     {
10369       /* Make sure a symtab is created for every file, even files
10370          which contain only variables (i.e. no code with associated
10371          line numbers).  */
10372
10373       int i;
10374       struct file_entry *fe;
10375
10376       for (i = 0; i < lh->num_file_names; i++)
10377         {
10378           char *dir = NULL;
10379
10380           fe = &lh->file_names[i];
10381           if (fe->dir_index)
10382             dir = lh->include_dirs[fe->dir_index - 1];
10383           dwarf2_start_subfile (fe->name, dir, comp_dir);
10384
10385           /* Skip the main file; we don't need it, and it must be
10386              allocated last, so that it will show up before the
10387              non-primary symtabs in the objfile's symtab list.  */
10388           if (current_subfile == first_subfile)
10389             continue;
10390
10391           if (current_subfile->symtab == NULL)
10392             current_subfile->symtab = allocate_symtab (current_subfile->name,
10393                                                        cu->objfile);
10394           fe->symtab = current_subfile->symtab;
10395         }
10396     }
10397 }
10398
10399 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10400    DIRNAME the name of the source directory which contains FILENAME
10401    or NULL if not known.  COMP_DIR is the compilation directory for the
10402    linetable's compilation unit or NULL if not known.
10403    This routine tries to keep line numbers from identical absolute and
10404    relative file names in a common subfile.
10405
10406    Using the `list' example from the GDB testsuite, which resides in
10407    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10408    of /srcdir/list0.c yields the following debugging information for list0.c:
10409
10410    DW_AT_name:          /srcdir/list0.c
10411    DW_AT_comp_dir:              /compdir
10412    files.files[0].name: list0.h
10413    files.files[0].dir:  /srcdir
10414    files.files[1].name: list0.c
10415    files.files[1].dir:  /srcdir
10416
10417    The line number information for list0.c has to end up in a single
10418    subfile, so that `break /srcdir/list0.c:1' works as expected.
10419    start_subfile will ensure that this happens provided that we pass the
10420    concatenation of files.files[1].dir and files.files[1].name as the
10421    subfile's name.  */
10422
10423 static void
10424 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10425 {
10426   char *fullname;
10427
10428   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10429      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10430      second argument to start_subfile.  To be consistent, we do the
10431      same here.  In order not to lose the line information directory,
10432      we concatenate it to the filename when it makes sense.
10433      Note that the Dwarf3 standard says (speaking of filenames in line
10434      information): ``The directory index is ignored for file names
10435      that represent full path names''.  Thus ignoring dirname in the
10436      `else' branch below isn't an issue.  */
10437
10438   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10439     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10440   else
10441     fullname = filename;
10442
10443   start_subfile (fullname, comp_dir);
10444
10445   if (fullname != filename)
10446     xfree (fullname);
10447 }
10448
10449 static void
10450 var_decode_location (struct attribute *attr, struct symbol *sym,
10451                      struct dwarf2_cu *cu)
10452 {
10453   struct objfile *objfile = cu->objfile;
10454   struct comp_unit_head *cu_header = &cu->header;
10455
10456   /* NOTE drow/2003-01-30: There used to be a comment and some special
10457      code here to turn a symbol with DW_AT_external and a
10458      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10459      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10460      with some versions of binutils) where shared libraries could have
10461      relocations against symbols in their debug information - the
10462      minimal symbol would have the right address, but the debug info
10463      would not.  It's no longer necessary, because we will explicitly
10464      apply relocations when we read in the debug information now.  */
10465
10466   /* A DW_AT_location attribute with no contents indicates that a
10467      variable has been optimized away.  */
10468   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10469     {
10470       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10471       return;
10472     }
10473
10474   /* Handle one degenerate form of location expression specially, to
10475      preserve GDB's previous behavior when section offsets are
10476      specified.  If this is just a DW_OP_addr then mark this symbol
10477      as LOC_STATIC.  */
10478
10479   if (attr_form_is_block (attr)
10480       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10481       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10482     {
10483       unsigned int dummy;
10484
10485       SYMBOL_VALUE_ADDRESS (sym) =
10486         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10487       SYMBOL_CLASS (sym) = LOC_STATIC;
10488       fixup_symbol_section (sym, objfile);
10489       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10490                                               SYMBOL_SECTION (sym));
10491       return;
10492     }
10493
10494   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10495      expression evaluator, and use LOC_COMPUTED only when necessary
10496      (i.e. when the value of a register or memory location is
10497      referenced, or a thread-local block, etc.).  Then again, it might
10498      not be worthwhile.  I'm assuming that it isn't unless performance
10499      or memory numbers show me otherwise.  */
10500
10501   dwarf2_symbol_mark_computed (attr, sym, cu);
10502   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10503 }
10504
10505 /* Given a pointer to a DWARF information entry, figure out if we need
10506    to make a symbol table entry for it, and if so, create a new entry
10507    and return a pointer to it.
10508    If TYPE is NULL, determine symbol type from the die, otherwise
10509    used the passed type.
10510    If SPACE is not NULL, use it to hold the new symbol.  If it is
10511    NULL, allocate a new symbol on the objfile's obstack.  */
10512
10513 static struct symbol *
10514 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10515                  struct symbol *space)
10516 {
10517   struct objfile *objfile = cu->objfile;
10518   struct symbol *sym = NULL;
10519   char *name;
10520   struct attribute *attr = NULL;
10521   struct attribute *attr2 = NULL;
10522   CORE_ADDR baseaddr;
10523   struct pending **list_to_add = NULL;
10524
10525   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10526
10527   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10528
10529   name = dwarf2_name (die, cu);
10530   if (name)
10531     {
10532       const char *linkagename;
10533       int suppress_add = 0;
10534
10535       if (space)
10536         sym = space;
10537       else
10538         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10539       OBJSTAT (objfile, n_syms++);
10540
10541       /* Cache this symbol's name and the name's demangled form (if any).  */
10542       SYMBOL_SET_LANGUAGE (sym, cu->language);
10543       linkagename = dwarf2_physname (name, die, cu);
10544       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10545
10546       /* Fortran does not have mangling standard and the mangling does differ
10547          between gfortran, iFort etc.  */
10548       if (cu->language == language_fortran
10549           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10550         symbol_set_demangled_name (&(sym->ginfo),
10551                                    (char *) dwarf2_full_name (name, die, cu),
10552                                    NULL);
10553
10554       /* Default assumptions.
10555          Use the passed type or decode it from the die.  */
10556       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10557       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10558       if (type != NULL)
10559         SYMBOL_TYPE (sym) = type;
10560       else
10561         SYMBOL_TYPE (sym) = die_type (die, cu);
10562       attr = dwarf2_attr (die,
10563                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10564                           cu);
10565       if (attr)
10566         {
10567           SYMBOL_LINE (sym) = DW_UNSND (attr);
10568         }
10569
10570       attr = dwarf2_attr (die,
10571                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10572                           cu);
10573       if (attr)
10574         {
10575           int file_index = DW_UNSND (attr);
10576
10577           if (cu->line_header == NULL
10578               || file_index > cu->line_header->num_file_names)
10579             complaint (&symfile_complaints,
10580                        _("file index out of range"));
10581           else if (file_index > 0)
10582             {
10583               struct file_entry *fe;
10584
10585               fe = &cu->line_header->file_names[file_index - 1];
10586               SYMBOL_SYMTAB (sym) = fe->symtab;
10587             }
10588         }
10589
10590       switch (die->tag)
10591         {
10592         case DW_TAG_label:
10593           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10594           if (attr)
10595             {
10596               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10597             }
10598           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10599           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10600           SYMBOL_CLASS (sym) = LOC_LABEL;
10601           add_symbol_to_list (sym, cu->list_in_scope);
10602           break;
10603         case DW_TAG_subprogram:
10604           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10605              finish_block.  */
10606           SYMBOL_CLASS (sym) = LOC_BLOCK;
10607           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10608           if ((attr2 && (DW_UNSND (attr2) != 0))
10609               || cu->language == language_ada)
10610             {
10611               /* Subprograms marked external are stored as a global symbol.
10612                  Ada subprograms, whether marked external or not, are always
10613                  stored as a global symbol, because we want to be able to
10614                  access them globally.  For instance, we want to be able
10615                  to break on a nested subprogram without having to
10616                  specify the context.  */
10617               list_to_add = &global_symbols;
10618             }
10619           else
10620             {
10621               list_to_add = cu->list_in_scope;
10622             }
10623           break;
10624         case DW_TAG_inlined_subroutine:
10625           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10626              finish_block.  */
10627           SYMBOL_CLASS (sym) = LOC_BLOCK;
10628           SYMBOL_INLINED (sym) = 1;
10629           /* Do not add the symbol to any lists.  It will be found via
10630              BLOCK_FUNCTION from the blockvector.  */
10631           break;
10632         case DW_TAG_template_value_param:
10633           suppress_add = 1;
10634           /* Fall through.  */
10635         case DW_TAG_constant:
10636         case DW_TAG_variable:
10637         case DW_TAG_member:
10638           /* Compilation with minimal debug info may result in variables
10639              with missing type entries. Change the misleading `void' type
10640              to something sensible.  */
10641           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10642             SYMBOL_TYPE (sym)
10643               = objfile_type (objfile)->nodebug_data_symbol;
10644
10645           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10646           /* In the case of DW_TAG_member, we should only be called for
10647              static const members.  */
10648           if (die->tag == DW_TAG_member)
10649             {
10650               /* dwarf2_add_field uses die_is_declaration,
10651                  so we do the same.  */
10652               gdb_assert (die_is_declaration (die, cu));
10653               gdb_assert (attr);
10654             }
10655           if (attr)
10656             {
10657               dwarf2_const_value (attr, sym, cu);
10658               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10659               if (!suppress_add)
10660                 {
10661                   if (attr2 && (DW_UNSND (attr2) != 0))
10662                     list_to_add = &global_symbols;
10663                   else
10664                     list_to_add = cu->list_in_scope;
10665                 }
10666               break;
10667             }
10668           attr = dwarf2_attr (die, DW_AT_location, cu);
10669           if (attr)
10670             {
10671               var_decode_location (attr, sym, cu);
10672               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10673               if (SYMBOL_CLASS (sym) == LOC_STATIC
10674                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10675                   && !dwarf2_per_objfile->has_section_at_zero)
10676                 {
10677                   /* When a static variable is eliminated by the linker,
10678                      the corresponding debug information is not stripped
10679                      out, but the variable address is set to null;
10680                      do not add such variables into symbol table.  */
10681                 }
10682               else if (attr2 && (DW_UNSND (attr2) != 0))
10683                 {
10684                   /* Workaround gfortran PR debug/40040 - it uses
10685                      DW_AT_location for variables in -fPIC libraries which may
10686                      get overriden by other libraries/executable and get
10687                      a different address.  Resolve it by the minimal symbol
10688                      which may come from inferior's executable using copy
10689                      relocation.  Make this workaround only for gfortran as for
10690                      other compilers GDB cannot guess the minimal symbol
10691                      Fortran mangling kind.  */
10692                   if (cu->language == language_fortran && die->parent
10693                       && die->parent->tag == DW_TAG_module
10694                       && cu->producer
10695                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10696                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10697
10698                   /* A variable with DW_AT_external is never static,
10699                      but it may be block-scoped.  */
10700                   list_to_add = (cu->list_in_scope == &file_symbols
10701                                  ? &global_symbols : cu->list_in_scope);
10702                 }
10703               else
10704                 list_to_add = cu->list_in_scope;
10705             }
10706           else
10707             {
10708               /* We do not know the address of this symbol.
10709                  If it is an external symbol and we have type information
10710                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10711                  The address of the variable will then be determined from
10712                  the minimal symbol table whenever the variable is
10713                  referenced.  */
10714               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10715               if (attr2 && (DW_UNSND (attr2) != 0)
10716                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10717                 {
10718                   /* A variable with DW_AT_external is never static, but it
10719                      may be block-scoped.  */
10720                   list_to_add = (cu->list_in_scope == &file_symbols
10721                                  ? &global_symbols : cu->list_in_scope);
10722
10723                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10724                 }
10725               else if (!die_is_declaration (die, cu))
10726                 {
10727                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10728                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10729                   if (!suppress_add)
10730                     list_to_add = cu->list_in_scope;
10731                 }
10732             }
10733           break;
10734         case DW_TAG_formal_parameter:
10735           /* If we are inside a function, mark this as an argument.  If
10736              not, we might be looking at an argument to an inlined function
10737              when we do not have enough information to show inlined frames;
10738              pretend it's a local variable in that case so that the user can
10739              still see it.  */
10740           if (context_stack_depth > 0
10741               && context_stack[context_stack_depth - 1].name != NULL)
10742             SYMBOL_IS_ARGUMENT (sym) = 1;
10743           attr = dwarf2_attr (die, DW_AT_location, cu);
10744           if (attr)
10745             {
10746               var_decode_location (attr, sym, cu);
10747             }
10748           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10749           if (attr)
10750             {
10751               dwarf2_const_value (attr, sym, cu);
10752             }
10753           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10754           if (attr && DW_UNSND (attr))
10755             {
10756               struct type *ref_type;
10757
10758               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10759               SYMBOL_TYPE (sym) = ref_type;
10760             }
10761
10762           list_to_add = cu->list_in_scope;
10763           break;
10764         case DW_TAG_unspecified_parameters:
10765           /* From varargs functions; gdb doesn't seem to have any
10766              interest in this information, so just ignore it for now.
10767              (FIXME?) */
10768           break;
10769         case DW_TAG_template_type_param:
10770           suppress_add = 1;
10771           /* Fall through.  */
10772         case DW_TAG_class_type:
10773         case DW_TAG_interface_type:
10774         case DW_TAG_structure_type:
10775         case DW_TAG_union_type:
10776         case DW_TAG_set_type:
10777         case DW_TAG_enumeration_type:
10778           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10779           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10780
10781           {
10782             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10783                really ever be static objects: otherwise, if you try
10784                to, say, break of a class's method and you're in a file
10785                which doesn't mention that class, it won't work unless
10786                the check for all static symbols in lookup_symbol_aux
10787                saves you.  See the OtherFileClass tests in
10788                gdb.c++/namespace.exp.  */
10789
10790             if (!suppress_add)
10791               {
10792                 list_to_add = (cu->list_in_scope == &file_symbols
10793                                && (cu->language == language_cplus
10794                                    || cu->language == language_java)
10795                                ? &global_symbols : cu->list_in_scope);
10796
10797                 /* The semantics of C++ state that "struct foo {
10798                    ... }" also defines a typedef for "foo".  A Java
10799                    class declaration also defines a typedef for the
10800                    class.  */
10801                 if (cu->language == language_cplus
10802                     || cu->language == language_java
10803                     || cu->language == language_ada)
10804                   {
10805                     /* The symbol's name is already allocated along
10806                        with this objfile, so we don't need to
10807                        duplicate it for the type.  */
10808                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10809                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10810                   }
10811               }
10812           }
10813           break;
10814         case DW_TAG_typedef:
10815           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10816           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10817           list_to_add = cu->list_in_scope;
10818           break;
10819         case DW_TAG_base_type:
10820         case DW_TAG_subrange_type:
10821           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10822           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10823           list_to_add = cu->list_in_scope;
10824           break;
10825         case DW_TAG_enumerator:
10826           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10827           if (attr)
10828             {
10829               dwarf2_const_value (attr, sym, cu);
10830             }
10831           {
10832             /* NOTE: carlton/2003-11-10: See comment above in the
10833                DW_TAG_class_type, etc. block.  */
10834
10835             list_to_add = (cu->list_in_scope == &file_symbols
10836                            && (cu->language == language_cplus
10837                                || cu->language == language_java)
10838                            ? &global_symbols : cu->list_in_scope);
10839           }
10840           break;
10841         case DW_TAG_namespace:
10842           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10843           list_to_add = &global_symbols;
10844           break;
10845         default:
10846           /* Not a tag we recognize.  Hopefully we aren't processing
10847              trash data, but since we must specifically ignore things
10848              we don't recognize, there is nothing else we should do at
10849              this point. */
10850           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10851                      dwarf_tag_name (die->tag));
10852           break;
10853         }
10854
10855       if (suppress_add)
10856         {
10857           sym->hash_next = objfile->template_symbols;
10858           objfile->template_symbols = sym;
10859           list_to_add = NULL;
10860         }
10861
10862       if (list_to_add != NULL)
10863         add_symbol_to_list (sym, list_to_add);
10864
10865       /* For the benefit of old versions of GCC, check for anonymous
10866          namespaces based on the demangled name.  */
10867       if (!processing_has_namespace_info
10868           && cu->language == language_cplus)
10869         cp_scan_for_anonymous_namespaces (sym);
10870     }
10871   return (sym);
10872 }
10873
10874 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
10875
10876 static struct symbol *
10877 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10878 {
10879   return new_symbol_full (die, type, cu, NULL);
10880 }
10881
10882 /* Given an attr with a DW_FORM_dataN value in host byte order,
10883    zero-extend it as appropriate for the symbol's type.  The DWARF
10884    standard (v4) is not entirely clear about the meaning of using
10885    DW_FORM_dataN for a constant with a signed type, where the type is
10886    wider than the data.  The conclusion of a discussion on the DWARF
10887    list was that this is unspecified.  We choose to always zero-extend
10888    because that is the interpretation long in use by GCC.  */
10889
10890 static gdb_byte *
10891 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10892                          const char *name, struct obstack *obstack,
10893                          struct dwarf2_cu *cu, long *value, int bits)
10894 {
10895   struct objfile *objfile = cu->objfile;
10896   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10897                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10898   LONGEST l = DW_UNSND (attr);
10899
10900   if (bits < sizeof (*value) * 8)
10901     {
10902       l &= ((LONGEST) 1 << bits) - 1;
10903       *value = l;
10904     }
10905   else if (bits == sizeof (*value) * 8)
10906     *value = l;
10907   else
10908     {
10909       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10910       store_unsigned_integer (bytes, bits / 8, byte_order, l);
10911       return bytes;
10912     }
10913
10914   return NULL;
10915 }
10916
10917 /* Read a constant value from an attribute.  Either set *VALUE, or if
10918    the value does not fit in *VALUE, set *BYTES - either already
10919    allocated on the objfile obstack, or newly allocated on OBSTACK,
10920    or, set *BATON, if we translated the constant to a location
10921    expression.  */
10922
10923 static void
10924 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10925                          const char *name, struct obstack *obstack,
10926                          struct dwarf2_cu *cu,
10927                          long *value, gdb_byte **bytes,
10928                          struct dwarf2_locexpr_baton **baton)
10929 {
10930   struct objfile *objfile = cu->objfile;
10931   struct comp_unit_head *cu_header = &cu->header;
10932   struct dwarf_block *blk;
10933   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10934                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10935
10936   *value = 0;
10937   *bytes = NULL;
10938   *baton = NULL;
10939
10940   switch (attr->form)
10941     {
10942     case DW_FORM_addr:
10943       {
10944         gdb_byte *data;
10945
10946         if (TYPE_LENGTH (type) != cu_header->addr_size)
10947           dwarf2_const_value_length_mismatch_complaint (name,
10948                                                         cu_header->addr_size,
10949                                                         TYPE_LENGTH (type));
10950         /* Symbols of this form are reasonably rare, so we just
10951            piggyback on the existing location code rather than writing
10952            a new implementation of symbol_computed_ops.  */
10953         *baton = obstack_alloc (&objfile->objfile_obstack,
10954                                 sizeof (struct dwarf2_locexpr_baton));
10955         (*baton)->per_cu = cu->per_cu;
10956         gdb_assert ((*baton)->per_cu);
10957
10958         (*baton)->size = 2 + cu_header->addr_size;
10959         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10960         (*baton)->data = data;
10961
10962         data[0] = DW_OP_addr;
10963         store_unsigned_integer (&data[1], cu_header->addr_size,
10964                                 byte_order, DW_ADDR (attr));
10965         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10966       }
10967       break;
10968     case DW_FORM_string:
10969     case DW_FORM_strp:
10970       /* DW_STRING is already allocated on the objfile obstack, point
10971          directly to it.  */
10972       *bytes = (gdb_byte *) DW_STRING (attr);
10973       break;
10974     case DW_FORM_block1:
10975     case DW_FORM_block2:
10976     case DW_FORM_block4:
10977     case DW_FORM_block:
10978     case DW_FORM_exprloc:
10979       blk = DW_BLOCK (attr);
10980       if (TYPE_LENGTH (type) != blk->size)
10981         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10982                                                       TYPE_LENGTH (type));
10983       *bytes = blk->data;
10984       break;
10985
10986       /* The DW_AT_const_value attributes are supposed to carry the
10987          symbol's value "represented as it would be on the target
10988          architecture."  By the time we get here, it's already been
10989          converted to host endianness, so we just need to sign- or
10990          zero-extend it as appropriate.  */
10991     case DW_FORM_data1:
10992       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10993       break;
10994     case DW_FORM_data2:
10995       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10996       break;
10997     case DW_FORM_data4:
10998       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10999       break;
11000     case DW_FORM_data8:
11001       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
11002       break;
11003
11004     case DW_FORM_sdata:
11005       *value = DW_SND (attr);
11006       break;
11007
11008     case DW_FORM_udata:
11009       *value = DW_UNSND (attr);
11010       break;
11011
11012     default:
11013       complaint (&symfile_complaints,
11014                  _("unsupported const value attribute form: '%s'"),
11015                  dwarf_form_name (attr->form));
11016       *value = 0;
11017       break;
11018     }
11019 }
11020
11021
11022 /* Copy constant value from an attribute to a symbol.  */
11023
11024 static void
11025 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11026                     struct dwarf2_cu *cu)
11027 {
11028   struct objfile *objfile = cu->objfile;
11029   struct comp_unit_head *cu_header = &cu->header;
11030   long value;
11031   gdb_byte *bytes;
11032   struct dwarf2_locexpr_baton *baton;
11033
11034   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11035                            SYMBOL_PRINT_NAME (sym),
11036                            &objfile->objfile_obstack, cu,
11037                            &value, &bytes, &baton);
11038
11039   if (baton != NULL)
11040     {
11041       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11042       SYMBOL_LOCATION_BATON (sym) = baton;
11043       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11044     }
11045   else if (bytes != NULL)
11046      {
11047       SYMBOL_VALUE_BYTES (sym) = bytes;
11048       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11049     }
11050   else
11051     {
11052       SYMBOL_VALUE (sym) = value;
11053       SYMBOL_CLASS (sym) = LOC_CONST;
11054     }
11055 }
11056
11057 /* Return the type of the die in question using its DW_AT_type attribute.  */
11058
11059 static struct type *
11060 die_type (struct die_info *die, struct dwarf2_cu *cu)
11061 {
11062   struct attribute *type_attr;
11063
11064   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11065   if (!type_attr)
11066     {
11067       /* A missing DW_AT_type represents a void type.  */
11068       return objfile_type (cu->objfile)->builtin_void;
11069     }
11070
11071   return lookup_die_type (die, type_attr, cu);
11072 }
11073
11074 /* True iff CU's producer generates GNAT Ada auxiliary information
11075    that allows to find parallel types through that information instead
11076    of having to do expensive parallel lookups by type name.  */
11077
11078 static int
11079 need_gnat_info (struct dwarf2_cu *cu)
11080 {
11081   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11082      of GNAT produces this auxiliary information, without any indication
11083      that it is produced.  Part of enhancing the FSF version of GNAT
11084      to produce that information will be to put in place an indicator
11085      that we can use in order to determine whether the descriptive type
11086      info is available or not.  One suggestion that has been made is
11087      to use a new attribute, attached to the CU die.  For now, assume
11088      that the descriptive type info is not available.  */
11089   return 0;
11090 }
11091
11092 /* Return the auxiliary type of the die in question using its
11093    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11094    attribute is not present.  */
11095
11096 static struct type *
11097 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11098 {
11099   struct attribute *type_attr;
11100
11101   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11102   if (!type_attr)
11103     return NULL;
11104
11105   return lookup_die_type (die, type_attr, cu);
11106 }
11107
11108 /* If DIE has a descriptive_type attribute, then set the TYPE's
11109    descriptive type accordingly.  */
11110
11111 static void
11112 set_descriptive_type (struct type *type, struct die_info *die,
11113                       struct dwarf2_cu *cu)
11114 {
11115   struct type *descriptive_type = die_descriptive_type (die, cu);
11116
11117   if (descriptive_type)
11118     {
11119       ALLOCATE_GNAT_AUX_TYPE (type);
11120       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11121     }
11122 }
11123
11124 /* Return the containing type of the die in question using its
11125    DW_AT_containing_type attribute.  */
11126
11127 static struct type *
11128 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11129 {
11130   struct attribute *type_attr;
11131
11132   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11133   if (!type_attr)
11134     error (_("Dwarf Error: Problem turning containing type into gdb type "
11135              "[in module %s]"), cu->objfile->name);
11136
11137   return lookup_die_type (die, type_attr, cu);
11138 }
11139
11140 /* Look up the type of DIE in CU using its type attribute ATTR.
11141    If there is no type substitute an error marker.  */
11142
11143 static struct type *
11144 lookup_die_type (struct die_info *die, struct attribute *attr,
11145                  struct dwarf2_cu *cu)
11146 {
11147   struct type *this_type;
11148
11149   /* First see if we have it cached.  */
11150
11151   if (is_ref_attr (attr))
11152     {
11153       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11154
11155       this_type = get_die_type_at_offset (offset, cu->per_cu);
11156     }
11157   else if (attr->form == DW_FORM_sig8)
11158     {
11159       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11160       struct dwarf2_cu *sig_cu;
11161       unsigned int offset;
11162
11163       /* sig_type will be NULL if the signatured type is missing from
11164          the debug info.  */
11165       if (sig_type == NULL)
11166         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11167                  "at 0x%x [in module %s]"),
11168                die->offset, cu->objfile->name);
11169
11170       gdb_assert (sig_type->per_cu.from_debug_types);
11171       offset = sig_type->offset + sig_type->type_offset;
11172       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11173     }
11174   else
11175     {
11176       dump_die_for_error (die);
11177       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11178              dwarf_attr_name (attr->name), cu->objfile->name);
11179     }
11180
11181   /* If not cached we need to read it in.  */
11182
11183   if (this_type == NULL)
11184     {
11185       struct die_info *type_die;
11186       struct dwarf2_cu *type_cu = cu;
11187
11188       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11189       /* If the type is cached, we should have found it above.  */
11190       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11191       this_type = read_type_die_1 (type_die, type_cu);
11192     }
11193
11194   /* If we still don't have a type use an error marker.  */
11195
11196   if (this_type == NULL)
11197     {
11198       char *message, *saved;
11199
11200       /* read_type_die already issued a complaint.  */
11201       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11202                             cu->objfile->name,
11203                             cu->header.offset,
11204                             die->offset);
11205       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11206                              message, strlen (message));
11207       xfree (message);
11208
11209       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11210     }
11211
11212   return this_type;
11213 }
11214
11215 /* Return the type in DIE, CU.
11216    Returns NULL for invalid types.
11217
11218    This first does a lookup in the appropriate type_hash table,
11219    and only reads the die in if necessary.
11220
11221    NOTE: This can be called when reading in partial or full symbols.  */
11222
11223 static struct type *
11224 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11225 {
11226   struct type *this_type;
11227
11228   this_type = get_die_type (die, cu);
11229   if (this_type)
11230     return this_type;
11231
11232   return read_type_die_1 (die, cu);
11233 }
11234
11235 /* Read the type in DIE, CU.
11236    Returns NULL for invalid types.  */
11237
11238 static struct type *
11239 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11240 {
11241   struct type *this_type = NULL;
11242
11243   switch (die->tag)
11244     {
11245     case DW_TAG_class_type:
11246     case DW_TAG_interface_type:
11247     case DW_TAG_structure_type:
11248     case DW_TAG_union_type:
11249       this_type = read_structure_type (die, cu);
11250       break;
11251     case DW_TAG_enumeration_type:
11252       this_type = read_enumeration_type (die, cu);
11253       break;
11254     case DW_TAG_subprogram:
11255     case DW_TAG_subroutine_type:
11256     case DW_TAG_inlined_subroutine:
11257       this_type = read_subroutine_type (die, cu);
11258       break;
11259     case DW_TAG_array_type:
11260       this_type = read_array_type (die, cu);
11261       break;
11262     case DW_TAG_set_type:
11263       this_type = read_set_type (die, cu);
11264       break;
11265     case DW_TAG_pointer_type:
11266       this_type = read_tag_pointer_type (die, cu);
11267       break;
11268     case DW_TAG_ptr_to_member_type:
11269       this_type = read_tag_ptr_to_member_type (die, cu);
11270       break;
11271     case DW_TAG_reference_type:
11272       this_type = read_tag_reference_type (die, cu);
11273       break;
11274     case DW_TAG_const_type:
11275       this_type = read_tag_const_type (die, cu);
11276       break;
11277     case DW_TAG_volatile_type:
11278       this_type = read_tag_volatile_type (die, cu);
11279       break;
11280     case DW_TAG_string_type:
11281       this_type = read_tag_string_type (die, cu);
11282       break;
11283     case DW_TAG_typedef:
11284       this_type = read_typedef (die, cu);
11285       break;
11286     case DW_TAG_subrange_type:
11287       this_type = read_subrange_type (die, cu);
11288       break;
11289     case DW_TAG_base_type:
11290       this_type = read_base_type (die, cu);
11291       break;
11292     case DW_TAG_unspecified_type:
11293       this_type = read_unspecified_type (die, cu);
11294       break;
11295     case DW_TAG_namespace:
11296       this_type = read_namespace_type (die, cu);
11297       break;
11298     case DW_TAG_module:
11299       this_type = read_module_type (die, cu);
11300       break;
11301     default:
11302       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11303                  dwarf_tag_name (die->tag));
11304       break;
11305     }
11306
11307   return this_type;
11308 }
11309
11310 /* See if we can figure out if the class lives in a namespace.  We do
11311    this by looking for a member function; its demangled name will
11312    contain namespace info, if there is any.
11313    Return the computed name or NULL.
11314    Space for the result is allocated on the objfile's obstack.
11315    This is the full-die version of guess_partial_die_structure_name.
11316    In this case we know DIE has no useful parent.  */
11317
11318 static char *
11319 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11320 {
11321   struct die_info *spec_die;
11322   struct dwarf2_cu *spec_cu;
11323   struct die_info *child;
11324
11325   spec_cu = cu;
11326   spec_die = die_specification (die, &spec_cu);
11327   if (spec_die != NULL)
11328     {
11329       die = spec_die;
11330       cu = spec_cu;
11331     }
11332
11333   for (child = die->child;
11334        child != NULL;
11335        child = child->sibling)
11336     {
11337       if (child->tag == DW_TAG_subprogram)
11338         {
11339           struct attribute *attr;
11340
11341           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11342           if (attr == NULL)
11343             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11344           if (attr != NULL)
11345             {
11346               char *actual_name
11347                 = language_class_name_from_physname (cu->language_defn,
11348                                                      DW_STRING (attr));
11349               char *name = NULL;
11350
11351               if (actual_name != NULL)
11352                 {
11353                   char *die_name = dwarf2_name (die, cu);
11354
11355                   if (die_name != NULL
11356                       && strcmp (die_name, actual_name) != 0)
11357                     {
11358                       /* Strip off the class name from the full name.
11359                          We want the prefix.  */
11360                       int die_name_len = strlen (die_name);
11361                       int actual_name_len = strlen (actual_name);
11362
11363                       /* Test for '::' as a sanity check.  */
11364                       if (actual_name_len > die_name_len + 2
11365                           && actual_name[actual_name_len - die_name_len - 1] == ':')
11366                         name =
11367                           obsavestring (actual_name,
11368                                         actual_name_len - die_name_len - 2,
11369                                         &cu->objfile->objfile_obstack);
11370                     }
11371                 }
11372               xfree (actual_name);
11373               return name;
11374             }
11375         }
11376     }
11377
11378   return NULL;
11379 }
11380
11381 /* Return the name of the namespace/class that DIE is defined within,
11382    or "" if we can't tell.  The caller should not xfree the result.
11383
11384    For example, if we're within the method foo() in the following
11385    code:
11386
11387    namespace N {
11388      class C {
11389        void foo () {
11390        }
11391      };
11392    }
11393
11394    then determine_prefix on foo's die will return "N::C".  */
11395
11396 static char *
11397 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11398 {
11399   struct die_info *parent, *spec_die;
11400   struct dwarf2_cu *spec_cu;
11401   struct type *parent_type;
11402
11403   if (cu->language != language_cplus && cu->language != language_java
11404       && cu->language != language_fortran)
11405     return "";
11406
11407   /* We have to be careful in the presence of DW_AT_specification.
11408      For example, with GCC 3.4, given the code
11409
11410      namespace N {
11411        void foo() {
11412          // Definition of N::foo.
11413        }
11414      }
11415
11416      then we'll have a tree of DIEs like this:
11417
11418      1: DW_TAG_compile_unit
11419        2: DW_TAG_namespace        // N
11420          3: DW_TAG_subprogram     // declaration of N::foo
11421        4: DW_TAG_subprogram       // definition of N::foo
11422             DW_AT_specification   // refers to die #3
11423
11424      Thus, when processing die #4, we have to pretend that we're in
11425      the context of its DW_AT_specification, namely the contex of die
11426      #3.  */
11427   spec_cu = cu;
11428   spec_die = die_specification (die, &spec_cu);
11429   if (spec_die == NULL)
11430     parent = die->parent;
11431   else
11432     {
11433       parent = spec_die->parent;
11434       cu = spec_cu;
11435     }
11436
11437   if (parent == NULL)
11438     return "";
11439   else if (parent->building_fullname)
11440     {
11441       const char *name;
11442       const char *parent_name;
11443
11444       /* It has been seen on RealView 2.2 built binaries,
11445          DW_TAG_template_type_param types actually _defined_ as
11446          children of the parent class:
11447
11448          enum E {};
11449          template class <class Enum> Class{};
11450          Class<enum E> class_e;
11451
11452          1: DW_TAG_class_type (Class)
11453            2: DW_TAG_enumeration_type (E)
11454              3: DW_TAG_enumerator (enum1:0)
11455              3: DW_TAG_enumerator (enum2:1)
11456              ...
11457            2: DW_TAG_template_type_param
11458               DW_AT_type  DW_FORM_ref_udata (E)
11459
11460          Besides being broken debug info, it can put GDB into an
11461          infinite loop.  Consider:
11462
11463          When we're building the full name for Class<E>, we'll start
11464          at Class, and go look over its template type parameters,
11465          finding E.  We'll then try to build the full name of E, and
11466          reach here.  We're now trying to build the full name of E,
11467          and look over the parent DIE for containing scope.  In the
11468          broken case, if we followed the parent DIE of E, we'd again
11469          find Class, and once again go look at its template type
11470          arguments, etc., etc.  Simply don't consider such parent die
11471          as source-level parent of this die (it can't be, the language
11472          doesn't allow it), and break the loop here.  */
11473       name = dwarf2_name (die, cu);
11474       parent_name = dwarf2_name (parent, cu);
11475       complaint (&symfile_complaints,
11476                  _("template param type '%s' defined within parent '%s'"),
11477                  name ? name : "<unknown>",
11478                  parent_name ? parent_name : "<unknown>");
11479       return "";
11480     }
11481   else
11482     switch (parent->tag)
11483       {
11484       case DW_TAG_namespace:
11485         parent_type = read_type_die (parent, cu);
11486         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11487            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11488            Work around this problem here.  */
11489         if (cu->language == language_cplus
11490             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11491           return "";
11492         /* We give a name to even anonymous namespaces.  */
11493         return TYPE_TAG_NAME (parent_type);
11494       case DW_TAG_class_type:
11495       case DW_TAG_interface_type:
11496       case DW_TAG_structure_type:
11497       case DW_TAG_union_type:
11498       case DW_TAG_module:
11499         parent_type = read_type_die (parent, cu);
11500         if (TYPE_TAG_NAME (parent_type) != NULL)
11501           return TYPE_TAG_NAME (parent_type);
11502         else
11503           /* An anonymous structure is only allowed non-static data
11504              members; no typedefs, no member functions, et cetera.
11505              So it does not need a prefix.  */
11506           return "";
11507       case DW_TAG_compile_unit:
11508         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
11509         if (cu->language == language_cplus
11510             && dwarf2_per_objfile->types.asection != NULL
11511             && die->child != NULL
11512             && (die->tag == DW_TAG_class_type
11513                 || die->tag == DW_TAG_structure_type
11514                 || die->tag == DW_TAG_union_type))
11515           {
11516             char *name = guess_full_die_structure_name (die, cu);
11517             if (name != NULL)
11518               return name;
11519           }
11520         return "";
11521       default:
11522         return determine_prefix (parent, cu);
11523       }
11524 }
11525
11526 /* Return a newly-allocated string formed by concatenating PREFIX and
11527    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11528    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
11529    perform an obconcat, otherwise allocate storage for the result.  The CU argument
11530    is used to determine the language and hence, the appropriate separator.  */
11531
11532 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11533
11534 static char *
11535 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11536                  int physname, struct dwarf2_cu *cu)
11537 {
11538   const char *lead = "";
11539   const char *sep;
11540
11541   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11542     sep = "";
11543   else if (cu->language == language_java)
11544     sep = ".";
11545   else if (cu->language == language_fortran && physname)
11546     {
11547       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11548          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11549
11550       lead = "__";
11551       sep = "_MOD_";
11552     }
11553   else
11554     sep = "::";
11555
11556   if (prefix == NULL)
11557     prefix = "";
11558   if (suffix == NULL)
11559     suffix = "";
11560
11561   if (obs == NULL)
11562     {
11563       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11564
11565       strcpy (retval, lead);
11566       strcat (retval, prefix);
11567       strcat (retval, sep);
11568       strcat (retval, suffix);
11569       return retval;
11570     }
11571   else
11572     {
11573       /* We have an obstack.  */
11574       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11575     }
11576 }
11577
11578 /* Return sibling of die, NULL if no sibling.  */
11579
11580 static struct die_info *
11581 sibling_die (struct die_info *die)
11582 {
11583   return die->sibling;
11584 }
11585
11586 /* Get name of a die, return NULL if not found.  */
11587
11588 static char *
11589 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11590                           struct obstack *obstack)
11591 {
11592   if (name && cu->language == language_cplus)
11593     {
11594       char *canon_name = cp_canonicalize_string (name);
11595
11596       if (canon_name != NULL)
11597         {
11598           if (strcmp (canon_name, name) != 0)
11599             name = obsavestring (canon_name, strlen (canon_name),
11600                                  obstack);
11601           xfree (canon_name);
11602         }
11603     }
11604
11605   return name;
11606 }
11607
11608 /* Get name of a die, return NULL if not found.  */
11609
11610 static char *
11611 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11612 {
11613   struct attribute *attr;
11614
11615   attr = dwarf2_attr (die, DW_AT_name, cu);
11616   if (!attr || !DW_STRING (attr))
11617     return NULL;
11618
11619   switch (die->tag)
11620     {
11621     case DW_TAG_compile_unit:
11622       /* Compilation units have a DW_AT_name that is a filename, not
11623          a source language identifier.  */
11624     case DW_TAG_enumeration_type:
11625     case DW_TAG_enumerator:
11626       /* These tags always have simple identifiers already; no need
11627          to canonicalize them.  */
11628       return DW_STRING (attr);
11629
11630     case DW_TAG_subprogram:
11631       /* Java constructors will all be named "<init>", so return
11632          the class name when we see this special case.  */
11633       if (cu->language == language_java
11634           && DW_STRING (attr) != NULL
11635           && strcmp (DW_STRING (attr), "<init>") == 0)
11636         {
11637           struct dwarf2_cu *spec_cu = cu;
11638           struct die_info *spec_die;
11639
11640           /* GCJ will output '<init>' for Java constructor names.
11641              For this special case, return the name of the parent class.  */
11642
11643           /* GCJ may output suprogram DIEs with AT_specification set.
11644              If so, use the name of the specified DIE.  */
11645           spec_die = die_specification (die, &spec_cu);
11646           if (spec_die != NULL)
11647             return dwarf2_name (spec_die, spec_cu);
11648
11649           do
11650             {
11651               die = die->parent;
11652               if (die->tag == DW_TAG_class_type)
11653                 return dwarf2_name (die, cu);
11654             }
11655           while (die->tag != DW_TAG_compile_unit);
11656         }
11657       break;
11658
11659     case DW_TAG_class_type:
11660     case DW_TAG_interface_type:
11661     case DW_TAG_structure_type:
11662     case DW_TAG_union_type:
11663       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11664          structures or unions.  These were of the form "._%d" in GCC 4.1,
11665          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11666          and GCC 4.4.  We work around this problem by ignoring these.  */
11667       if (strncmp (DW_STRING (attr), "._", 2) == 0
11668           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11669         return NULL;
11670       break;
11671
11672     default:
11673       break;
11674     }
11675
11676   if (!DW_STRING_IS_CANONICAL (attr))
11677     {
11678       DW_STRING (attr)
11679         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11680                                     &cu->objfile->objfile_obstack);
11681       DW_STRING_IS_CANONICAL (attr) = 1;
11682     }
11683   return DW_STRING (attr);
11684 }
11685
11686 /* Return the die that this die in an extension of, or NULL if there
11687    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11688    containing the return value on output.  */
11689
11690 static struct die_info *
11691 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11692 {
11693   struct attribute *attr;
11694
11695   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11696   if (attr == NULL)
11697     return NULL;
11698
11699   return follow_die_ref (die, attr, ext_cu);
11700 }
11701
11702 /* Convert a DIE tag into its string name.  */
11703
11704 static char *
11705 dwarf_tag_name (unsigned tag)
11706 {
11707   switch (tag)
11708     {
11709     case DW_TAG_padding:
11710       return "DW_TAG_padding";
11711     case DW_TAG_array_type:
11712       return "DW_TAG_array_type";
11713     case DW_TAG_class_type:
11714       return "DW_TAG_class_type";
11715     case DW_TAG_entry_point:
11716       return "DW_TAG_entry_point";
11717     case DW_TAG_enumeration_type:
11718       return "DW_TAG_enumeration_type";
11719     case DW_TAG_formal_parameter:
11720       return "DW_TAG_formal_parameter";
11721     case DW_TAG_imported_declaration:
11722       return "DW_TAG_imported_declaration";
11723     case DW_TAG_label:
11724       return "DW_TAG_label";
11725     case DW_TAG_lexical_block:
11726       return "DW_TAG_lexical_block";
11727     case DW_TAG_member:
11728       return "DW_TAG_member";
11729     case DW_TAG_pointer_type:
11730       return "DW_TAG_pointer_type";
11731     case DW_TAG_reference_type:
11732       return "DW_TAG_reference_type";
11733     case DW_TAG_compile_unit:
11734       return "DW_TAG_compile_unit";
11735     case DW_TAG_string_type:
11736       return "DW_TAG_string_type";
11737     case DW_TAG_structure_type:
11738       return "DW_TAG_structure_type";
11739     case DW_TAG_subroutine_type:
11740       return "DW_TAG_subroutine_type";
11741     case DW_TAG_typedef:
11742       return "DW_TAG_typedef";
11743     case DW_TAG_union_type:
11744       return "DW_TAG_union_type";
11745     case DW_TAG_unspecified_parameters:
11746       return "DW_TAG_unspecified_parameters";
11747     case DW_TAG_variant:
11748       return "DW_TAG_variant";
11749     case DW_TAG_common_block:
11750       return "DW_TAG_common_block";
11751     case DW_TAG_common_inclusion:
11752       return "DW_TAG_common_inclusion";
11753     case DW_TAG_inheritance:
11754       return "DW_TAG_inheritance";
11755     case DW_TAG_inlined_subroutine:
11756       return "DW_TAG_inlined_subroutine";
11757     case DW_TAG_module:
11758       return "DW_TAG_module";
11759     case DW_TAG_ptr_to_member_type:
11760       return "DW_TAG_ptr_to_member_type";
11761     case DW_TAG_set_type:
11762       return "DW_TAG_set_type";
11763     case DW_TAG_subrange_type:
11764       return "DW_TAG_subrange_type";
11765     case DW_TAG_with_stmt:
11766       return "DW_TAG_with_stmt";
11767     case DW_TAG_access_declaration:
11768       return "DW_TAG_access_declaration";
11769     case DW_TAG_base_type:
11770       return "DW_TAG_base_type";
11771     case DW_TAG_catch_block:
11772       return "DW_TAG_catch_block";
11773     case DW_TAG_const_type:
11774       return "DW_TAG_const_type";
11775     case DW_TAG_constant:
11776       return "DW_TAG_constant";
11777     case DW_TAG_enumerator:
11778       return "DW_TAG_enumerator";
11779     case DW_TAG_file_type:
11780       return "DW_TAG_file_type";
11781     case DW_TAG_friend:
11782       return "DW_TAG_friend";
11783     case DW_TAG_namelist:
11784       return "DW_TAG_namelist";
11785     case DW_TAG_namelist_item:
11786       return "DW_TAG_namelist_item";
11787     case DW_TAG_packed_type:
11788       return "DW_TAG_packed_type";
11789     case DW_TAG_subprogram:
11790       return "DW_TAG_subprogram";
11791     case DW_TAG_template_type_param:
11792       return "DW_TAG_template_type_param";
11793     case DW_TAG_template_value_param:
11794       return "DW_TAG_template_value_param";
11795     case DW_TAG_thrown_type:
11796       return "DW_TAG_thrown_type";
11797     case DW_TAG_try_block:
11798       return "DW_TAG_try_block";
11799     case DW_TAG_variant_part:
11800       return "DW_TAG_variant_part";
11801     case DW_TAG_variable:
11802       return "DW_TAG_variable";
11803     case DW_TAG_volatile_type:
11804       return "DW_TAG_volatile_type";
11805     case DW_TAG_dwarf_procedure:
11806       return "DW_TAG_dwarf_procedure";
11807     case DW_TAG_restrict_type:
11808       return "DW_TAG_restrict_type";
11809     case DW_TAG_interface_type:
11810       return "DW_TAG_interface_type";
11811     case DW_TAG_namespace:
11812       return "DW_TAG_namespace";
11813     case DW_TAG_imported_module:
11814       return "DW_TAG_imported_module";
11815     case DW_TAG_unspecified_type:
11816       return "DW_TAG_unspecified_type";
11817     case DW_TAG_partial_unit:
11818       return "DW_TAG_partial_unit";
11819     case DW_TAG_imported_unit:
11820       return "DW_TAG_imported_unit";
11821     case DW_TAG_condition:
11822       return "DW_TAG_condition";
11823     case DW_TAG_shared_type:
11824       return "DW_TAG_shared_type";
11825     case DW_TAG_type_unit:
11826       return "DW_TAG_type_unit";
11827     case DW_TAG_MIPS_loop:
11828       return "DW_TAG_MIPS_loop";
11829     case DW_TAG_HP_array_descriptor:
11830       return "DW_TAG_HP_array_descriptor";
11831     case DW_TAG_format_label:
11832       return "DW_TAG_format_label";
11833     case DW_TAG_function_template:
11834       return "DW_TAG_function_template";
11835     case DW_TAG_class_template:
11836       return "DW_TAG_class_template";
11837     case DW_TAG_GNU_BINCL:
11838       return "DW_TAG_GNU_BINCL";
11839     case DW_TAG_GNU_EINCL:
11840       return "DW_TAG_GNU_EINCL";
11841     case DW_TAG_upc_shared_type:
11842       return "DW_TAG_upc_shared_type";
11843     case DW_TAG_upc_strict_type:
11844       return "DW_TAG_upc_strict_type";
11845     case DW_TAG_upc_relaxed_type:
11846       return "DW_TAG_upc_relaxed_type";
11847     case DW_TAG_PGI_kanji_type:
11848       return "DW_TAG_PGI_kanji_type";
11849     case DW_TAG_PGI_interface_block:
11850       return "DW_TAG_PGI_interface_block";
11851     default:
11852       return "DW_TAG_<unknown>";
11853     }
11854 }
11855
11856 /* Convert a DWARF attribute code into its string name.  */
11857
11858 static char *
11859 dwarf_attr_name (unsigned attr)
11860 {
11861   switch (attr)
11862     {
11863     case DW_AT_sibling:
11864       return "DW_AT_sibling";
11865     case DW_AT_location:
11866       return "DW_AT_location";
11867     case DW_AT_name:
11868       return "DW_AT_name";
11869     case DW_AT_ordering:
11870       return "DW_AT_ordering";
11871     case DW_AT_subscr_data:
11872       return "DW_AT_subscr_data";
11873     case DW_AT_byte_size:
11874       return "DW_AT_byte_size";
11875     case DW_AT_bit_offset:
11876       return "DW_AT_bit_offset";
11877     case DW_AT_bit_size:
11878       return "DW_AT_bit_size";
11879     case DW_AT_element_list:
11880       return "DW_AT_element_list";
11881     case DW_AT_stmt_list:
11882       return "DW_AT_stmt_list";
11883     case DW_AT_low_pc:
11884       return "DW_AT_low_pc";
11885     case DW_AT_high_pc:
11886       return "DW_AT_high_pc";
11887     case DW_AT_language:
11888       return "DW_AT_language";
11889     case DW_AT_member:
11890       return "DW_AT_member";
11891     case DW_AT_discr:
11892       return "DW_AT_discr";
11893     case DW_AT_discr_value:
11894       return "DW_AT_discr_value";
11895     case DW_AT_visibility:
11896       return "DW_AT_visibility";
11897     case DW_AT_import:
11898       return "DW_AT_import";
11899     case DW_AT_string_length:
11900       return "DW_AT_string_length";
11901     case DW_AT_common_reference:
11902       return "DW_AT_common_reference";
11903     case DW_AT_comp_dir:
11904       return "DW_AT_comp_dir";
11905     case DW_AT_const_value:
11906       return "DW_AT_const_value";
11907     case DW_AT_containing_type:
11908       return "DW_AT_containing_type";
11909     case DW_AT_default_value:
11910       return "DW_AT_default_value";
11911     case DW_AT_inline:
11912       return "DW_AT_inline";
11913     case DW_AT_is_optional:
11914       return "DW_AT_is_optional";
11915     case DW_AT_lower_bound:
11916       return "DW_AT_lower_bound";
11917     case DW_AT_producer:
11918       return "DW_AT_producer";
11919     case DW_AT_prototyped:
11920       return "DW_AT_prototyped";
11921     case DW_AT_return_addr:
11922       return "DW_AT_return_addr";
11923     case DW_AT_start_scope:
11924       return "DW_AT_start_scope";
11925     case DW_AT_bit_stride:
11926       return "DW_AT_bit_stride";
11927     case DW_AT_upper_bound:
11928       return "DW_AT_upper_bound";
11929     case DW_AT_abstract_origin:
11930       return "DW_AT_abstract_origin";
11931     case DW_AT_accessibility:
11932       return "DW_AT_accessibility";
11933     case DW_AT_address_class:
11934       return "DW_AT_address_class";
11935     case DW_AT_artificial:
11936       return "DW_AT_artificial";
11937     case DW_AT_base_types:
11938       return "DW_AT_base_types";
11939     case DW_AT_calling_convention:
11940       return "DW_AT_calling_convention";
11941     case DW_AT_count:
11942       return "DW_AT_count";
11943     case DW_AT_data_member_location:
11944       return "DW_AT_data_member_location";
11945     case DW_AT_decl_column:
11946       return "DW_AT_decl_column";
11947     case DW_AT_decl_file:
11948       return "DW_AT_decl_file";
11949     case DW_AT_decl_line:
11950       return "DW_AT_decl_line";
11951     case DW_AT_declaration:
11952       return "DW_AT_declaration";
11953     case DW_AT_discr_list:
11954       return "DW_AT_discr_list";
11955     case DW_AT_encoding:
11956       return "DW_AT_encoding";
11957     case DW_AT_external:
11958       return "DW_AT_external";
11959     case DW_AT_frame_base:
11960       return "DW_AT_frame_base";
11961     case DW_AT_friend:
11962       return "DW_AT_friend";
11963     case DW_AT_identifier_case:
11964       return "DW_AT_identifier_case";
11965     case DW_AT_macro_info:
11966       return "DW_AT_macro_info";
11967     case DW_AT_namelist_items:
11968       return "DW_AT_namelist_items";
11969     case DW_AT_priority:
11970       return "DW_AT_priority";
11971     case DW_AT_segment:
11972       return "DW_AT_segment";
11973     case DW_AT_specification:
11974       return "DW_AT_specification";
11975     case DW_AT_static_link:
11976       return "DW_AT_static_link";
11977     case DW_AT_type:
11978       return "DW_AT_type";
11979     case DW_AT_use_location:
11980       return "DW_AT_use_location";
11981     case DW_AT_variable_parameter:
11982       return "DW_AT_variable_parameter";
11983     case DW_AT_virtuality:
11984       return "DW_AT_virtuality";
11985     case DW_AT_vtable_elem_location:
11986       return "DW_AT_vtable_elem_location";
11987     /* DWARF 3 values.  */
11988     case DW_AT_allocated:
11989       return "DW_AT_allocated";
11990     case DW_AT_associated:
11991       return "DW_AT_associated";
11992     case DW_AT_data_location:
11993       return "DW_AT_data_location";
11994     case DW_AT_byte_stride:
11995       return "DW_AT_byte_stride";
11996     case DW_AT_entry_pc:
11997       return "DW_AT_entry_pc";
11998     case DW_AT_use_UTF8:
11999       return "DW_AT_use_UTF8";
12000     case DW_AT_extension:
12001       return "DW_AT_extension";
12002     case DW_AT_ranges:
12003       return "DW_AT_ranges";
12004     case DW_AT_trampoline:
12005       return "DW_AT_trampoline";
12006     case DW_AT_call_column:
12007       return "DW_AT_call_column";
12008     case DW_AT_call_file:
12009       return "DW_AT_call_file";
12010     case DW_AT_call_line:
12011       return "DW_AT_call_line";
12012     case DW_AT_description:
12013       return "DW_AT_description";
12014     case DW_AT_binary_scale:
12015       return "DW_AT_binary_scale";
12016     case DW_AT_decimal_scale:
12017       return "DW_AT_decimal_scale";
12018     case DW_AT_small:
12019       return "DW_AT_small";
12020     case DW_AT_decimal_sign:
12021       return "DW_AT_decimal_sign";
12022     case DW_AT_digit_count:
12023       return "DW_AT_digit_count";
12024     case DW_AT_picture_string:
12025       return "DW_AT_picture_string";
12026     case DW_AT_mutable:
12027       return "DW_AT_mutable";
12028     case DW_AT_threads_scaled:
12029       return "DW_AT_threads_scaled";
12030     case DW_AT_explicit:
12031       return "DW_AT_explicit";
12032     case DW_AT_object_pointer:
12033       return "DW_AT_object_pointer";
12034     case DW_AT_endianity:
12035       return "DW_AT_endianity";
12036     case DW_AT_elemental:
12037       return "DW_AT_elemental";
12038     case DW_AT_pure:
12039       return "DW_AT_pure";
12040     case DW_AT_recursive:
12041       return "DW_AT_recursive";
12042     /* DWARF 4 values.  */
12043     case DW_AT_signature:
12044       return "DW_AT_signature";
12045     case DW_AT_linkage_name:
12046       return "DW_AT_linkage_name";
12047     /* SGI/MIPS extensions.  */
12048 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12049     case DW_AT_MIPS_fde:
12050       return "DW_AT_MIPS_fde";
12051 #endif
12052     case DW_AT_MIPS_loop_begin:
12053       return "DW_AT_MIPS_loop_begin";
12054     case DW_AT_MIPS_tail_loop_begin:
12055       return "DW_AT_MIPS_tail_loop_begin";
12056     case DW_AT_MIPS_epilog_begin:
12057       return "DW_AT_MIPS_epilog_begin";
12058     case DW_AT_MIPS_loop_unroll_factor:
12059       return "DW_AT_MIPS_loop_unroll_factor";
12060     case DW_AT_MIPS_software_pipeline_depth:
12061       return "DW_AT_MIPS_software_pipeline_depth";
12062     case DW_AT_MIPS_linkage_name:
12063       return "DW_AT_MIPS_linkage_name";
12064     case DW_AT_MIPS_stride:
12065       return "DW_AT_MIPS_stride";
12066     case DW_AT_MIPS_abstract_name:
12067       return "DW_AT_MIPS_abstract_name";
12068     case DW_AT_MIPS_clone_origin:
12069       return "DW_AT_MIPS_clone_origin";
12070     case DW_AT_MIPS_has_inlines:
12071       return "DW_AT_MIPS_has_inlines";
12072     /* HP extensions.  */
12073 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12074     case DW_AT_HP_block_index:
12075       return "DW_AT_HP_block_index";
12076 #endif
12077     case DW_AT_HP_unmodifiable:
12078       return "DW_AT_HP_unmodifiable";
12079     case DW_AT_HP_actuals_stmt_list:
12080       return "DW_AT_HP_actuals_stmt_list";
12081     case DW_AT_HP_proc_per_section:
12082       return "DW_AT_HP_proc_per_section";
12083     case DW_AT_HP_raw_data_ptr:
12084       return "DW_AT_HP_raw_data_ptr";
12085     case DW_AT_HP_pass_by_reference:
12086       return "DW_AT_HP_pass_by_reference";
12087     case DW_AT_HP_opt_level:
12088       return "DW_AT_HP_opt_level";
12089     case DW_AT_HP_prof_version_id:
12090       return "DW_AT_HP_prof_version_id";
12091     case DW_AT_HP_opt_flags:
12092       return "DW_AT_HP_opt_flags";
12093     case DW_AT_HP_cold_region_low_pc:
12094       return "DW_AT_HP_cold_region_low_pc";
12095     case DW_AT_HP_cold_region_high_pc:
12096       return "DW_AT_HP_cold_region_high_pc";
12097     case DW_AT_HP_all_variables_modifiable:
12098       return "DW_AT_HP_all_variables_modifiable";
12099     case DW_AT_HP_linkage_name:
12100       return "DW_AT_HP_linkage_name";
12101     case DW_AT_HP_prof_flags:
12102       return "DW_AT_HP_prof_flags";
12103     /* GNU extensions.  */
12104     case DW_AT_sf_names:
12105       return "DW_AT_sf_names";
12106     case DW_AT_src_info:
12107       return "DW_AT_src_info";
12108     case DW_AT_mac_info:
12109       return "DW_AT_mac_info";
12110     case DW_AT_src_coords:
12111       return "DW_AT_src_coords";
12112     case DW_AT_body_begin:
12113       return "DW_AT_body_begin";
12114     case DW_AT_body_end:
12115       return "DW_AT_body_end";
12116     case DW_AT_GNU_vector:
12117       return "DW_AT_GNU_vector";
12118     case DW_AT_GNU_odr_signature:
12119       return "DW_AT_GNU_odr_signature";
12120     /* VMS extensions.  */
12121     case DW_AT_VMS_rtnbeg_pd_address:
12122       return "DW_AT_VMS_rtnbeg_pd_address";
12123     /* UPC extension.  */
12124     case DW_AT_upc_threads_scaled:
12125       return "DW_AT_upc_threads_scaled";
12126     /* PGI (STMicroelectronics) extensions.  */
12127     case DW_AT_PGI_lbase:
12128       return "DW_AT_PGI_lbase";
12129     case DW_AT_PGI_soffset:
12130       return "DW_AT_PGI_soffset";
12131     case DW_AT_PGI_lstride:
12132       return "DW_AT_PGI_lstride";
12133     default:
12134       return "DW_AT_<unknown>";
12135     }
12136 }
12137
12138 /* Convert a DWARF value form code into its string name.  */
12139
12140 static char *
12141 dwarf_form_name (unsigned form)
12142 {
12143   switch (form)
12144     {
12145     case DW_FORM_addr:
12146       return "DW_FORM_addr";
12147     case DW_FORM_block2:
12148       return "DW_FORM_block2";
12149     case DW_FORM_block4:
12150       return "DW_FORM_block4";
12151     case DW_FORM_data2:
12152       return "DW_FORM_data2";
12153     case DW_FORM_data4:
12154       return "DW_FORM_data4";
12155     case DW_FORM_data8:
12156       return "DW_FORM_data8";
12157     case DW_FORM_string:
12158       return "DW_FORM_string";
12159     case DW_FORM_block:
12160       return "DW_FORM_block";
12161     case DW_FORM_block1:
12162       return "DW_FORM_block1";
12163     case DW_FORM_data1:
12164       return "DW_FORM_data1";
12165     case DW_FORM_flag:
12166       return "DW_FORM_flag";
12167     case DW_FORM_sdata:
12168       return "DW_FORM_sdata";
12169     case DW_FORM_strp:
12170       return "DW_FORM_strp";
12171     case DW_FORM_udata:
12172       return "DW_FORM_udata";
12173     case DW_FORM_ref_addr:
12174       return "DW_FORM_ref_addr";
12175     case DW_FORM_ref1:
12176       return "DW_FORM_ref1";
12177     case DW_FORM_ref2:
12178       return "DW_FORM_ref2";
12179     case DW_FORM_ref4:
12180       return "DW_FORM_ref4";
12181     case DW_FORM_ref8:
12182       return "DW_FORM_ref8";
12183     case DW_FORM_ref_udata:
12184       return "DW_FORM_ref_udata";
12185     case DW_FORM_indirect:
12186       return "DW_FORM_indirect";
12187     case DW_FORM_sec_offset:
12188       return "DW_FORM_sec_offset";
12189     case DW_FORM_exprloc:
12190       return "DW_FORM_exprloc";
12191     case DW_FORM_flag_present:
12192       return "DW_FORM_flag_present";
12193     case DW_FORM_sig8:
12194       return "DW_FORM_sig8";
12195     default:
12196       return "DW_FORM_<unknown>";
12197     }
12198 }
12199
12200 /* Convert a DWARF stack opcode into its string name.  */
12201
12202 const char *
12203 dwarf_stack_op_name (unsigned op, int def)
12204 {
12205   switch (op)
12206     {
12207     case DW_OP_addr:
12208       return "DW_OP_addr";
12209     case DW_OP_deref:
12210       return "DW_OP_deref";
12211     case DW_OP_const1u:
12212       return "DW_OP_const1u";
12213     case DW_OP_const1s:
12214       return "DW_OP_const1s";
12215     case DW_OP_const2u:
12216       return "DW_OP_const2u";
12217     case DW_OP_const2s:
12218       return "DW_OP_const2s";
12219     case DW_OP_const4u:
12220       return "DW_OP_const4u";
12221     case DW_OP_const4s:
12222       return "DW_OP_const4s";
12223     case DW_OP_const8u:
12224       return "DW_OP_const8u";
12225     case DW_OP_const8s:
12226       return "DW_OP_const8s";
12227     case DW_OP_constu:
12228       return "DW_OP_constu";
12229     case DW_OP_consts:
12230       return "DW_OP_consts";
12231     case DW_OP_dup:
12232       return "DW_OP_dup";
12233     case DW_OP_drop:
12234       return "DW_OP_drop";
12235     case DW_OP_over:
12236       return "DW_OP_over";
12237     case DW_OP_pick:
12238       return "DW_OP_pick";
12239     case DW_OP_swap:
12240       return "DW_OP_swap";
12241     case DW_OP_rot:
12242       return "DW_OP_rot";
12243     case DW_OP_xderef:
12244       return "DW_OP_xderef";
12245     case DW_OP_abs:
12246       return "DW_OP_abs";
12247     case DW_OP_and:
12248       return "DW_OP_and";
12249     case DW_OP_div:
12250       return "DW_OP_div";
12251     case DW_OP_minus:
12252       return "DW_OP_minus";
12253     case DW_OP_mod:
12254       return "DW_OP_mod";
12255     case DW_OP_mul:
12256       return "DW_OP_mul";
12257     case DW_OP_neg:
12258       return "DW_OP_neg";
12259     case DW_OP_not:
12260       return "DW_OP_not";
12261     case DW_OP_or:
12262       return "DW_OP_or";
12263     case DW_OP_plus:
12264       return "DW_OP_plus";
12265     case DW_OP_plus_uconst:
12266       return "DW_OP_plus_uconst";
12267     case DW_OP_shl:
12268       return "DW_OP_shl";
12269     case DW_OP_shr:
12270       return "DW_OP_shr";
12271     case DW_OP_shra:
12272       return "DW_OP_shra";
12273     case DW_OP_xor:
12274       return "DW_OP_xor";
12275     case DW_OP_bra:
12276       return "DW_OP_bra";
12277     case DW_OP_eq:
12278       return "DW_OP_eq";
12279     case DW_OP_ge:
12280       return "DW_OP_ge";
12281     case DW_OP_gt:
12282       return "DW_OP_gt";
12283     case DW_OP_le:
12284       return "DW_OP_le";
12285     case DW_OP_lt:
12286       return "DW_OP_lt";
12287     case DW_OP_ne:
12288       return "DW_OP_ne";
12289     case DW_OP_skip:
12290       return "DW_OP_skip";
12291     case DW_OP_lit0:
12292       return "DW_OP_lit0";
12293     case DW_OP_lit1:
12294       return "DW_OP_lit1";
12295     case DW_OP_lit2:
12296       return "DW_OP_lit2";
12297     case DW_OP_lit3:
12298       return "DW_OP_lit3";
12299     case DW_OP_lit4:
12300       return "DW_OP_lit4";
12301     case DW_OP_lit5:
12302       return "DW_OP_lit5";
12303     case DW_OP_lit6:
12304       return "DW_OP_lit6";
12305     case DW_OP_lit7:
12306       return "DW_OP_lit7";
12307     case DW_OP_lit8:
12308       return "DW_OP_lit8";
12309     case DW_OP_lit9:
12310       return "DW_OP_lit9";
12311     case DW_OP_lit10:
12312       return "DW_OP_lit10";
12313     case DW_OP_lit11:
12314       return "DW_OP_lit11";
12315     case DW_OP_lit12:
12316       return "DW_OP_lit12";
12317     case DW_OP_lit13:
12318       return "DW_OP_lit13";
12319     case DW_OP_lit14:
12320       return "DW_OP_lit14";
12321     case DW_OP_lit15:
12322       return "DW_OP_lit15";
12323     case DW_OP_lit16:
12324       return "DW_OP_lit16";
12325     case DW_OP_lit17:
12326       return "DW_OP_lit17";
12327     case DW_OP_lit18:
12328       return "DW_OP_lit18";
12329     case DW_OP_lit19:
12330       return "DW_OP_lit19";
12331     case DW_OP_lit20:
12332       return "DW_OP_lit20";
12333     case DW_OP_lit21:
12334       return "DW_OP_lit21";
12335     case DW_OP_lit22:
12336       return "DW_OP_lit22";
12337     case DW_OP_lit23:
12338       return "DW_OP_lit23";
12339     case DW_OP_lit24:
12340       return "DW_OP_lit24";
12341     case DW_OP_lit25:
12342       return "DW_OP_lit25";
12343     case DW_OP_lit26:
12344       return "DW_OP_lit26";
12345     case DW_OP_lit27:
12346       return "DW_OP_lit27";
12347     case DW_OP_lit28:
12348       return "DW_OP_lit28";
12349     case DW_OP_lit29:
12350       return "DW_OP_lit29";
12351     case DW_OP_lit30:
12352       return "DW_OP_lit30";
12353     case DW_OP_lit31:
12354       return "DW_OP_lit31";
12355     case DW_OP_reg0:
12356       return "DW_OP_reg0";
12357     case DW_OP_reg1:
12358       return "DW_OP_reg1";
12359     case DW_OP_reg2:
12360       return "DW_OP_reg2";
12361     case DW_OP_reg3:
12362       return "DW_OP_reg3";
12363     case DW_OP_reg4:
12364       return "DW_OP_reg4";
12365     case DW_OP_reg5:
12366       return "DW_OP_reg5";
12367     case DW_OP_reg6:
12368       return "DW_OP_reg6";
12369     case DW_OP_reg7:
12370       return "DW_OP_reg7";
12371     case DW_OP_reg8:
12372       return "DW_OP_reg8";
12373     case DW_OP_reg9:
12374       return "DW_OP_reg9";
12375     case DW_OP_reg10:
12376       return "DW_OP_reg10";
12377     case DW_OP_reg11:
12378       return "DW_OP_reg11";
12379     case DW_OP_reg12:
12380       return "DW_OP_reg12";
12381     case DW_OP_reg13:
12382       return "DW_OP_reg13";
12383     case DW_OP_reg14:
12384       return "DW_OP_reg14";
12385     case DW_OP_reg15:
12386       return "DW_OP_reg15";
12387     case DW_OP_reg16:
12388       return "DW_OP_reg16";
12389     case DW_OP_reg17:
12390       return "DW_OP_reg17";
12391     case DW_OP_reg18:
12392       return "DW_OP_reg18";
12393     case DW_OP_reg19:
12394       return "DW_OP_reg19";
12395     case DW_OP_reg20:
12396       return "DW_OP_reg20";
12397     case DW_OP_reg21:
12398       return "DW_OP_reg21";
12399     case DW_OP_reg22:
12400       return "DW_OP_reg22";
12401     case DW_OP_reg23:
12402       return "DW_OP_reg23";
12403     case DW_OP_reg24:
12404       return "DW_OP_reg24";
12405     case DW_OP_reg25:
12406       return "DW_OP_reg25";
12407     case DW_OP_reg26:
12408       return "DW_OP_reg26";
12409     case DW_OP_reg27:
12410       return "DW_OP_reg27";
12411     case DW_OP_reg28:
12412       return "DW_OP_reg28";
12413     case DW_OP_reg29:
12414       return "DW_OP_reg29";
12415     case DW_OP_reg30:
12416       return "DW_OP_reg30";
12417     case DW_OP_reg31:
12418       return "DW_OP_reg31";
12419     case DW_OP_breg0:
12420       return "DW_OP_breg0";
12421     case DW_OP_breg1:
12422       return "DW_OP_breg1";
12423     case DW_OP_breg2:
12424       return "DW_OP_breg2";
12425     case DW_OP_breg3:
12426       return "DW_OP_breg3";
12427     case DW_OP_breg4:
12428       return "DW_OP_breg4";
12429     case DW_OP_breg5:
12430       return "DW_OP_breg5";
12431     case DW_OP_breg6:
12432       return "DW_OP_breg6";
12433     case DW_OP_breg7:
12434       return "DW_OP_breg7";
12435     case DW_OP_breg8:
12436       return "DW_OP_breg8";
12437     case DW_OP_breg9:
12438       return "DW_OP_breg9";
12439     case DW_OP_breg10:
12440       return "DW_OP_breg10";
12441     case DW_OP_breg11:
12442       return "DW_OP_breg11";
12443     case DW_OP_breg12:
12444       return "DW_OP_breg12";
12445     case DW_OP_breg13:
12446       return "DW_OP_breg13";
12447     case DW_OP_breg14:
12448       return "DW_OP_breg14";
12449     case DW_OP_breg15:
12450       return "DW_OP_breg15";
12451     case DW_OP_breg16:
12452       return "DW_OP_breg16";
12453     case DW_OP_breg17:
12454       return "DW_OP_breg17";
12455     case DW_OP_breg18:
12456       return "DW_OP_breg18";
12457     case DW_OP_breg19:
12458       return "DW_OP_breg19";
12459     case DW_OP_breg20:
12460       return "DW_OP_breg20";
12461     case DW_OP_breg21:
12462       return "DW_OP_breg21";
12463     case DW_OP_breg22:
12464       return "DW_OP_breg22";
12465     case DW_OP_breg23:
12466       return "DW_OP_breg23";
12467     case DW_OP_breg24:
12468       return "DW_OP_breg24";
12469     case DW_OP_breg25:
12470       return "DW_OP_breg25";
12471     case DW_OP_breg26:
12472       return "DW_OP_breg26";
12473     case DW_OP_breg27:
12474       return "DW_OP_breg27";
12475     case DW_OP_breg28:
12476       return "DW_OP_breg28";
12477     case DW_OP_breg29:
12478       return "DW_OP_breg29";
12479     case DW_OP_breg30:
12480       return "DW_OP_breg30";
12481     case DW_OP_breg31:
12482       return "DW_OP_breg31";
12483     case DW_OP_regx:
12484       return "DW_OP_regx";
12485     case DW_OP_fbreg:
12486       return "DW_OP_fbreg";
12487     case DW_OP_bregx:
12488       return "DW_OP_bregx";
12489     case DW_OP_piece:
12490       return "DW_OP_piece";
12491     case DW_OP_deref_size:
12492       return "DW_OP_deref_size";
12493     case DW_OP_xderef_size:
12494       return "DW_OP_xderef_size";
12495     case DW_OP_nop:
12496       return "DW_OP_nop";
12497     /* DWARF 3 extensions.  */
12498     case DW_OP_push_object_address:
12499       return "DW_OP_push_object_address";
12500     case DW_OP_call2:
12501       return "DW_OP_call2";
12502     case DW_OP_call4:
12503       return "DW_OP_call4";
12504     case DW_OP_call_ref:
12505       return "DW_OP_call_ref";
12506     case DW_OP_form_tls_address:
12507       return "DW_OP_form_tls_address";
12508     case DW_OP_call_frame_cfa:
12509       return "DW_OP_call_frame_cfa";
12510     case DW_OP_bit_piece:
12511       return "DW_OP_bit_piece";
12512     /* DWARF 4 extensions.  */
12513     case DW_OP_implicit_value:
12514       return "DW_OP_implicit_value";
12515     case DW_OP_stack_value:
12516       return "DW_OP_stack_value";
12517     /* GNU extensions.  */
12518     case DW_OP_GNU_push_tls_address:
12519       return "DW_OP_GNU_push_tls_address";
12520     case DW_OP_GNU_uninit:
12521       return "DW_OP_GNU_uninit";
12522     default:
12523       return def ? "OP_<unknown>" : NULL;
12524     }
12525 }
12526
12527 static char *
12528 dwarf_bool_name (unsigned mybool)
12529 {
12530   if (mybool)
12531     return "TRUE";
12532   else
12533     return "FALSE";
12534 }
12535
12536 /* Convert a DWARF type code into its string name.  */
12537
12538 static char *
12539 dwarf_type_encoding_name (unsigned enc)
12540 {
12541   switch (enc)
12542     {
12543     case DW_ATE_void:
12544       return "DW_ATE_void";
12545     case DW_ATE_address:
12546       return "DW_ATE_address";
12547     case DW_ATE_boolean:
12548       return "DW_ATE_boolean";
12549     case DW_ATE_complex_float:
12550       return "DW_ATE_complex_float";
12551     case DW_ATE_float:
12552       return "DW_ATE_float";
12553     case DW_ATE_signed:
12554       return "DW_ATE_signed";
12555     case DW_ATE_signed_char:
12556       return "DW_ATE_signed_char";
12557     case DW_ATE_unsigned:
12558       return "DW_ATE_unsigned";
12559     case DW_ATE_unsigned_char:
12560       return "DW_ATE_unsigned_char";
12561     /* DWARF 3.  */
12562     case DW_ATE_imaginary_float:
12563       return "DW_ATE_imaginary_float";
12564     case DW_ATE_packed_decimal:
12565       return "DW_ATE_packed_decimal";
12566     case DW_ATE_numeric_string:
12567       return "DW_ATE_numeric_string";
12568     case DW_ATE_edited:
12569       return "DW_ATE_edited";
12570     case DW_ATE_signed_fixed:
12571       return "DW_ATE_signed_fixed";
12572     case DW_ATE_unsigned_fixed:
12573       return "DW_ATE_unsigned_fixed";
12574     case DW_ATE_decimal_float:
12575       return "DW_ATE_decimal_float";
12576     /* DWARF 4.  */
12577     case DW_ATE_UTF:
12578       return "DW_ATE_UTF";
12579     /* HP extensions.  */
12580     case DW_ATE_HP_float80:
12581       return "DW_ATE_HP_float80";
12582     case DW_ATE_HP_complex_float80:
12583       return "DW_ATE_HP_complex_float80";
12584     case DW_ATE_HP_float128:
12585       return "DW_ATE_HP_float128";
12586     case DW_ATE_HP_complex_float128:
12587       return "DW_ATE_HP_complex_float128";
12588     case DW_ATE_HP_floathpintel:
12589       return "DW_ATE_HP_floathpintel";
12590     case DW_ATE_HP_imaginary_float80:
12591       return "DW_ATE_HP_imaginary_float80";
12592     case DW_ATE_HP_imaginary_float128:
12593       return "DW_ATE_HP_imaginary_float128";
12594     default:
12595       return "DW_ATE_<unknown>";
12596     }
12597 }
12598
12599 /* Convert a DWARF call frame info operation to its string name. */
12600
12601 #if 0
12602 static char *
12603 dwarf_cfi_name (unsigned cfi_opc)
12604 {
12605   switch (cfi_opc)
12606     {
12607     case DW_CFA_advance_loc:
12608       return "DW_CFA_advance_loc";
12609     case DW_CFA_offset:
12610       return "DW_CFA_offset";
12611     case DW_CFA_restore:
12612       return "DW_CFA_restore";
12613     case DW_CFA_nop:
12614       return "DW_CFA_nop";
12615     case DW_CFA_set_loc:
12616       return "DW_CFA_set_loc";
12617     case DW_CFA_advance_loc1:
12618       return "DW_CFA_advance_loc1";
12619     case DW_CFA_advance_loc2:
12620       return "DW_CFA_advance_loc2";
12621     case DW_CFA_advance_loc4:
12622       return "DW_CFA_advance_loc4";
12623     case DW_CFA_offset_extended:
12624       return "DW_CFA_offset_extended";
12625     case DW_CFA_restore_extended:
12626       return "DW_CFA_restore_extended";
12627     case DW_CFA_undefined:
12628       return "DW_CFA_undefined";
12629     case DW_CFA_same_value:
12630       return "DW_CFA_same_value";
12631     case DW_CFA_register:
12632       return "DW_CFA_register";
12633     case DW_CFA_remember_state:
12634       return "DW_CFA_remember_state";
12635     case DW_CFA_restore_state:
12636       return "DW_CFA_restore_state";
12637     case DW_CFA_def_cfa:
12638       return "DW_CFA_def_cfa";
12639     case DW_CFA_def_cfa_register:
12640       return "DW_CFA_def_cfa_register";
12641     case DW_CFA_def_cfa_offset:
12642       return "DW_CFA_def_cfa_offset";
12643     /* DWARF 3.  */
12644     case DW_CFA_def_cfa_expression:
12645       return "DW_CFA_def_cfa_expression";
12646     case DW_CFA_expression:
12647       return "DW_CFA_expression";
12648     case DW_CFA_offset_extended_sf:
12649       return "DW_CFA_offset_extended_sf";
12650     case DW_CFA_def_cfa_sf:
12651       return "DW_CFA_def_cfa_sf";
12652     case DW_CFA_def_cfa_offset_sf:
12653       return "DW_CFA_def_cfa_offset_sf";
12654     case DW_CFA_val_offset:
12655       return "DW_CFA_val_offset";
12656     case DW_CFA_val_offset_sf:
12657       return "DW_CFA_val_offset_sf";
12658     case DW_CFA_val_expression:
12659       return "DW_CFA_val_expression";
12660     /* SGI/MIPS specific.  */
12661     case DW_CFA_MIPS_advance_loc8:
12662       return "DW_CFA_MIPS_advance_loc8";
12663     /* GNU extensions.  */
12664     case DW_CFA_GNU_window_save:
12665       return "DW_CFA_GNU_window_save";
12666     case DW_CFA_GNU_args_size:
12667       return "DW_CFA_GNU_args_size";
12668     case DW_CFA_GNU_negative_offset_extended:
12669       return "DW_CFA_GNU_negative_offset_extended";
12670     default:
12671       return "DW_CFA_<unknown>";
12672     }
12673 }
12674 #endif
12675
12676 static void
12677 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12678 {
12679   unsigned int i;
12680
12681   print_spaces (indent, f);
12682   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12683            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12684
12685   if (die->parent != NULL)
12686     {
12687       print_spaces (indent, f);
12688       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12689                           die->parent->offset);
12690     }
12691
12692   print_spaces (indent, f);
12693   fprintf_unfiltered (f, "  has children: %s\n",
12694            dwarf_bool_name (die->child != NULL));
12695
12696   print_spaces (indent, f);
12697   fprintf_unfiltered (f, "  attributes:\n");
12698
12699   for (i = 0; i < die->num_attrs; ++i)
12700     {
12701       print_spaces (indent, f);
12702       fprintf_unfiltered (f, "    %s (%s) ",
12703                dwarf_attr_name (die->attrs[i].name),
12704                dwarf_form_name (die->attrs[i].form));
12705
12706       switch (die->attrs[i].form)
12707         {
12708         case DW_FORM_ref_addr:
12709         case DW_FORM_addr:
12710           fprintf_unfiltered (f, "address: ");
12711           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12712           break;
12713         case DW_FORM_block2:
12714         case DW_FORM_block4:
12715         case DW_FORM_block:
12716         case DW_FORM_block1:
12717           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12718           break;
12719         case DW_FORM_exprloc:
12720           fprintf_unfiltered (f, "expression: size %u",
12721                               DW_BLOCK (&die->attrs[i])->size);
12722           break;
12723         case DW_FORM_ref1:
12724         case DW_FORM_ref2:
12725         case DW_FORM_ref4:
12726           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12727                               (long) (DW_ADDR (&die->attrs[i])));
12728           break;
12729         case DW_FORM_data1:
12730         case DW_FORM_data2:
12731         case DW_FORM_data4:
12732         case DW_FORM_data8:
12733         case DW_FORM_udata:
12734         case DW_FORM_sdata:
12735           fprintf_unfiltered (f, "constant: %s",
12736                               pulongest (DW_UNSND (&die->attrs[i])));
12737           break;
12738         case DW_FORM_sec_offset:
12739           fprintf_unfiltered (f, "section offset: %s",
12740                               pulongest (DW_UNSND (&die->attrs[i])));
12741           break;
12742         case DW_FORM_sig8:
12743           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12744             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12745                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12746           else
12747             fprintf_unfiltered (f, "signatured type, offset: unknown");
12748           break;
12749         case DW_FORM_string:
12750         case DW_FORM_strp:
12751           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12752                    DW_STRING (&die->attrs[i])
12753                    ? DW_STRING (&die->attrs[i]) : "",
12754                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12755           break;
12756         case DW_FORM_flag:
12757           if (DW_UNSND (&die->attrs[i]))
12758             fprintf_unfiltered (f, "flag: TRUE");
12759           else
12760             fprintf_unfiltered (f, "flag: FALSE");
12761           break;
12762         case DW_FORM_flag_present:
12763           fprintf_unfiltered (f, "flag: TRUE");
12764           break;
12765         case DW_FORM_indirect:
12766           /* the reader will have reduced the indirect form to
12767              the "base form" so this form should not occur */
12768           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12769           break;
12770         default:
12771           fprintf_unfiltered (f, "unsupported attribute form: %d.",
12772                    die->attrs[i].form);
12773           break;
12774         }
12775       fprintf_unfiltered (f, "\n");
12776     }
12777 }
12778
12779 static void
12780 dump_die_for_error (struct die_info *die)
12781 {
12782   dump_die_shallow (gdb_stderr, 0, die);
12783 }
12784
12785 static void
12786 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12787 {
12788   int indent = level * 4;
12789
12790   gdb_assert (die != NULL);
12791
12792   if (level >= max_level)
12793     return;
12794
12795   dump_die_shallow (f, indent, die);
12796
12797   if (die->child != NULL)
12798     {
12799       print_spaces (indent, f);
12800       fprintf_unfiltered (f, "  Children:");
12801       if (level + 1 < max_level)
12802         {
12803           fprintf_unfiltered (f, "\n");
12804           dump_die_1 (f, level + 1, max_level, die->child);
12805         }
12806       else
12807         {
12808           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12809         }
12810     }
12811
12812   if (die->sibling != NULL && level > 0)
12813     {
12814       dump_die_1 (f, level, max_level, die->sibling);
12815     }
12816 }
12817
12818 /* This is called from the pdie macro in gdbinit.in.
12819    It's not static so gcc will keep a copy callable from gdb.  */
12820
12821 void
12822 dump_die (struct die_info *die, int max_level)
12823 {
12824   dump_die_1 (gdb_stdlog, 0, max_level, die);
12825 }
12826
12827 static void
12828 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12829 {
12830   void **slot;
12831
12832   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12833
12834   *slot = die;
12835 }
12836
12837 static int
12838 is_ref_attr (struct attribute *attr)
12839 {
12840   switch (attr->form)
12841     {
12842     case DW_FORM_ref_addr:
12843     case DW_FORM_ref1:
12844     case DW_FORM_ref2:
12845     case DW_FORM_ref4:
12846     case DW_FORM_ref8:
12847     case DW_FORM_ref_udata:
12848       return 1;
12849     default:
12850       return 0;
12851     }
12852 }
12853
12854 static unsigned int
12855 dwarf2_get_ref_die_offset (struct attribute *attr)
12856 {
12857   if (is_ref_attr (attr))
12858     return DW_ADDR (attr);
12859
12860   complaint (&symfile_complaints,
12861              _("unsupported die ref attribute form: '%s'"),
12862              dwarf_form_name (attr->form));
12863   return 0;
12864 }
12865
12866 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
12867  * the value held by the attribute is not constant.  */
12868
12869 static LONGEST
12870 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12871 {
12872   if (attr->form == DW_FORM_sdata)
12873     return DW_SND (attr);
12874   else if (attr->form == DW_FORM_udata
12875            || attr->form == DW_FORM_data1
12876            || attr->form == DW_FORM_data2
12877            || attr->form == DW_FORM_data4
12878            || attr->form == DW_FORM_data8)
12879     return DW_UNSND (attr);
12880   else
12881     {
12882       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12883                  dwarf_form_name (attr->form));
12884       return default_value;
12885     }
12886 }
12887
12888 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
12889    unit and add it to our queue.
12890    The result is non-zero if PER_CU was queued, otherwise the result is zero
12891    meaning either PER_CU is already queued or it is already loaded.  */
12892
12893 static int
12894 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12895                        struct dwarf2_per_cu_data *per_cu)
12896 {
12897   /* We may arrive here during partial symbol reading, if we need full
12898      DIEs to process an unusual case (e.g. template arguments).  Do
12899      not queue PER_CU, just tell our caller to load its DIEs.  */
12900   if (dwarf2_per_objfile->reading_partial_symbols)
12901     {
12902       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12903         return 1;
12904       return 0;
12905     }
12906
12907   /* Mark the dependence relation so that we don't flush PER_CU
12908      too early.  */
12909   dwarf2_add_dependence (this_cu, per_cu);
12910
12911   /* If it's already on the queue, we have nothing to do.  */
12912   if (per_cu->queued)
12913     return 0;
12914
12915   /* If the compilation unit is already loaded, just mark it as
12916      used.  */
12917   if (per_cu->cu != NULL)
12918     {
12919       per_cu->cu->last_used = 0;
12920       return 0;
12921     }
12922
12923   /* Add it to the queue.  */
12924   queue_comp_unit (per_cu, this_cu->objfile);
12925
12926   return 1;
12927 }
12928
12929 /* Follow reference or signature attribute ATTR of SRC_DIE.
12930    On entry *REF_CU is the CU of SRC_DIE.
12931    On exit *REF_CU is the CU of the result.  */
12932
12933 static struct die_info *
12934 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12935                        struct dwarf2_cu **ref_cu)
12936 {
12937   struct die_info *die;
12938
12939   if (is_ref_attr (attr))
12940     die = follow_die_ref (src_die, attr, ref_cu);
12941   else if (attr->form == DW_FORM_sig8)
12942     die = follow_die_sig (src_die, attr, ref_cu);
12943   else
12944     {
12945       dump_die_for_error (src_die);
12946       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12947              (*ref_cu)->objfile->name);
12948     }
12949
12950   return die;
12951 }
12952
12953 /* Follow reference OFFSET.
12954    On entry *REF_CU is the CU of the source die referencing OFFSET.
12955    On exit *REF_CU is the CU of the result.
12956    Returns NULL if OFFSET is invalid.  */
12957
12958 static struct die_info *
12959 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12960 {
12961   struct die_info temp_die;
12962   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12963
12964   gdb_assert (cu->per_cu != NULL);
12965
12966   target_cu = cu;
12967
12968   if (cu->per_cu->from_debug_types)
12969     {
12970       /* .debug_types CUs cannot reference anything outside their CU.
12971          If they need to, they have to reference a signatured type via
12972          DW_FORM_sig8.  */
12973       if (! offset_in_cu_p (&cu->header, offset))
12974         return NULL;
12975     }
12976   else if (! offset_in_cu_p (&cu->header, offset))
12977     {
12978       struct dwarf2_per_cu_data *per_cu;
12979
12980       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12981
12982       /* If necessary, add it to the queue and load its DIEs.  */
12983       if (maybe_queue_comp_unit (cu, per_cu))
12984         load_full_comp_unit (per_cu, cu->objfile);
12985
12986       target_cu = per_cu->cu;
12987     }
12988   else if (cu->dies == NULL)
12989     {
12990       /* We're loading full DIEs during partial symbol reading.  */
12991       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12992       load_full_comp_unit (cu->per_cu, cu->objfile);
12993     }
12994
12995   *ref_cu = target_cu;
12996   temp_die.offset = offset;
12997   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12998 }
12999
13000 /* Follow reference attribute ATTR of SRC_DIE.
13001    On entry *REF_CU is the CU of SRC_DIE.
13002    On exit *REF_CU is the CU of the result.  */
13003
13004 static struct die_info *
13005 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13006                 struct dwarf2_cu **ref_cu)
13007 {
13008   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13009   struct dwarf2_cu *cu = *ref_cu;
13010   struct die_info *die;
13011
13012   die = follow_die_offset (offset, ref_cu);
13013   if (!die)
13014     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13015            "at 0x%x [in module %s]"),
13016            offset, src_die->offset, cu->objfile->name);
13017
13018   return die;
13019 }
13020
13021 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13022    value is intended for DW_OP_call*.  */
13023
13024 struct dwarf2_locexpr_baton
13025 dwarf2_fetch_die_location_block (unsigned int offset,
13026                                  struct dwarf2_per_cu_data *per_cu)
13027 {
13028   struct dwarf2_cu *cu = per_cu->cu;
13029   struct die_info *die;
13030   struct attribute *attr;
13031   struct dwarf2_locexpr_baton retval;
13032
13033   die = follow_die_offset (offset, &cu);
13034   if (!die)
13035     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13036            offset, per_cu->cu->objfile->name);
13037
13038   attr = dwarf2_attr (die, DW_AT_location, cu);
13039   if (!attr)
13040     {
13041       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13042
13043       retval.data = NULL;
13044       retval.size = 0;
13045     }
13046   else
13047     {
13048       if (!attr_form_is_block (attr))
13049         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13050                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13051                offset, per_cu->cu->objfile->name);
13052
13053       retval.data = DW_BLOCK (attr)->data;
13054       retval.size = DW_BLOCK (attr)->size;
13055     }
13056   retval.per_cu = cu->per_cu;
13057   return retval;
13058 }
13059
13060 /* Follow the signature attribute ATTR in SRC_DIE.
13061    On entry *REF_CU is the CU of SRC_DIE.
13062    On exit *REF_CU is the CU of the result.  */
13063
13064 static struct die_info *
13065 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13066                 struct dwarf2_cu **ref_cu)
13067 {
13068   struct objfile *objfile = (*ref_cu)->objfile;
13069   struct die_info temp_die;
13070   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13071   struct dwarf2_cu *sig_cu;
13072   struct die_info *die;
13073
13074   /* sig_type will be NULL if the signatured type is missing from
13075      the debug info.  */
13076   if (sig_type == NULL)
13077     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13078              "at 0x%x [in module %s]"),
13079            src_die->offset, objfile->name);
13080
13081   /* If necessary, add it to the queue and load its DIEs.  */
13082
13083   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13084     read_signatured_type (objfile, sig_type);
13085
13086   gdb_assert (sig_type->per_cu.cu != NULL);
13087
13088   sig_cu = sig_type->per_cu.cu;
13089   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13090   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13091   if (die)
13092     {
13093       *ref_cu = sig_cu;
13094       return die;
13095     }
13096
13097   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13098          "at 0x%x [in module %s]"),
13099          sig_type->type_offset, src_die->offset, objfile->name);
13100 }
13101
13102 /* Given an offset of a signatured type, return its signatured_type.  */
13103
13104 static struct signatured_type *
13105 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13106 {
13107   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13108   unsigned int length, initial_length_size;
13109   unsigned int sig_offset;
13110   struct signatured_type find_entry, *type_sig;
13111
13112   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13113   sig_offset = (initial_length_size
13114                 + 2 /*version*/
13115                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13116                 + 1 /*address_size*/);
13117   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13118   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13119
13120   /* This is only used to lookup previously recorded types.
13121      If we didn't find it, it's our bug.  */
13122   gdb_assert (type_sig != NULL);
13123   gdb_assert (offset == type_sig->offset);
13124
13125   return type_sig;
13126 }
13127
13128 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13129
13130 static void
13131 read_signatured_type_at_offset (struct objfile *objfile,
13132                                 unsigned int offset)
13133 {
13134   struct signatured_type *type_sig;
13135
13136   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13137
13138   /* We have the section offset, but we need the signature to do the
13139      hash table lookup.  */
13140   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13141
13142   gdb_assert (type_sig->per_cu.cu == NULL);
13143
13144   read_signatured_type (objfile, type_sig);
13145
13146   gdb_assert (type_sig->per_cu.cu != NULL);
13147 }
13148
13149 /* Read in a signatured type and build its CU and DIEs.  */
13150
13151 static void
13152 read_signatured_type (struct objfile *objfile,
13153                       struct signatured_type *type_sig)
13154 {
13155   gdb_byte *types_ptr;
13156   struct die_reader_specs reader_specs;
13157   struct dwarf2_cu *cu;
13158   ULONGEST signature;
13159   struct cleanup *back_to, *free_cu_cleanup;
13160   struct attribute *attr;
13161
13162   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13163   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13164
13165   gdb_assert (type_sig->per_cu.cu == NULL);
13166
13167   cu = xmalloc (sizeof (struct dwarf2_cu));
13168   memset (cu, 0, sizeof (struct dwarf2_cu));
13169   obstack_init (&cu->comp_unit_obstack);
13170   cu->objfile = objfile;
13171   type_sig->per_cu.cu = cu;
13172   cu->per_cu = &type_sig->per_cu;
13173
13174   /* If an error occurs while loading, release our storage.  */
13175   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13176
13177   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13178                                         types_ptr, objfile->obfd);
13179   gdb_assert (signature == type_sig->signature);
13180
13181   cu->die_hash
13182     = htab_create_alloc_ex (cu->header.length / 12,
13183                             die_hash,
13184                             die_eq,
13185                             NULL,
13186                             &cu->comp_unit_obstack,
13187                             hashtab_obstack_allocate,
13188                             dummy_obstack_deallocate);
13189
13190   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13191   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13192
13193   init_cu_die_reader (&reader_specs, cu);
13194
13195   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13196                                     NULL /*parent*/);
13197
13198   /* We try not to read any attributes in this function, because not
13199      all objfiles needed for references have been loaded yet, and symbol
13200      table processing isn't initialized.  But we have to set the CU language,
13201      or we won't be able to build types correctly.  */
13202   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13203   if (attr)
13204     set_cu_language (DW_UNSND (attr), cu);
13205   else
13206     set_cu_language (language_minimal, cu);
13207
13208   do_cleanups (back_to);
13209
13210   /* We've successfully allocated this compilation unit.  Let our caller
13211      clean it up when finished with it.  */
13212   discard_cleanups (free_cu_cleanup);
13213
13214   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13215   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13216 }
13217
13218 /* Decode simple location descriptions.
13219    Given a pointer to a dwarf block that defines a location, compute
13220    the location and return the value.
13221
13222    NOTE drow/2003-11-18: This function is called in two situations
13223    now: for the address of static or global variables (partial symbols
13224    only) and for offsets into structures which are expected to be
13225    (more or less) constant.  The partial symbol case should go away,
13226    and only the constant case should remain.  That will let this
13227    function complain more accurately.  A few special modes are allowed
13228    without complaint for global variables (for instance, global
13229    register values and thread-local values).
13230
13231    A location description containing no operations indicates that the
13232    object is optimized out.  The return value is 0 for that case.
13233    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13234    callers will only want a very basic result and this can become a
13235    complaint.
13236
13237    Note that stack[0] is unused except as a default error return.
13238    Note that stack overflow is not yet handled.  */
13239
13240 static CORE_ADDR
13241 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13242 {
13243   struct objfile *objfile = cu->objfile;
13244   int i;
13245   int size = blk->size;
13246   gdb_byte *data = blk->data;
13247   CORE_ADDR stack[64];
13248   int stacki;
13249   unsigned int bytes_read, unsnd;
13250   gdb_byte op;
13251
13252   i = 0;
13253   stacki = 0;
13254   stack[stacki] = 0;
13255
13256   while (i < size)
13257     {
13258       op = data[i++];
13259       switch (op)
13260         {
13261         case DW_OP_lit0:
13262         case DW_OP_lit1:
13263         case DW_OP_lit2:
13264         case DW_OP_lit3:
13265         case DW_OP_lit4:
13266         case DW_OP_lit5:
13267         case DW_OP_lit6:
13268         case DW_OP_lit7:
13269         case DW_OP_lit8:
13270         case DW_OP_lit9:
13271         case DW_OP_lit10:
13272         case DW_OP_lit11:
13273         case DW_OP_lit12:
13274         case DW_OP_lit13:
13275         case DW_OP_lit14:
13276         case DW_OP_lit15:
13277         case DW_OP_lit16:
13278         case DW_OP_lit17:
13279         case DW_OP_lit18:
13280         case DW_OP_lit19:
13281         case DW_OP_lit20:
13282         case DW_OP_lit21:
13283         case DW_OP_lit22:
13284         case DW_OP_lit23:
13285         case DW_OP_lit24:
13286         case DW_OP_lit25:
13287         case DW_OP_lit26:
13288         case DW_OP_lit27:
13289         case DW_OP_lit28:
13290         case DW_OP_lit29:
13291         case DW_OP_lit30:
13292         case DW_OP_lit31:
13293           stack[++stacki] = op - DW_OP_lit0;
13294           break;
13295
13296         case DW_OP_reg0:
13297         case DW_OP_reg1:
13298         case DW_OP_reg2:
13299         case DW_OP_reg3:
13300         case DW_OP_reg4:
13301         case DW_OP_reg5:
13302         case DW_OP_reg6:
13303         case DW_OP_reg7:
13304         case DW_OP_reg8:
13305         case DW_OP_reg9:
13306         case DW_OP_reg10:
13307         case DW_OP_reg11:
13308         case DW_OP_reg12:
13309         case DW_OP_reg13:
13310         case DW_OP_reg14:
13311         case DW_OP_reg15:
13312         case DW_OP_reg16:
13313         case DW_OP_reg17:
13314         case DW_OP_reg18:
13315         case DW_OP_reg19:
13316         case DW_OP_reg20:
13317         case DW_OP_reg21:
13318         case DW_OP_reg22:
13319         case DW_OP_reg23:
13320         case DW_OP_reg24:
13321         case DW_OP_reg25:
13322         case DW_OP_reg26:
13323         case DW_OP_reg27:
13324         case DW_OP_reg28:
13325         case DW_OP_reg29:
13326         case DW_OP_reg30:
13327         case DW_OP_reg31:
13328           stack[++stacki] = op - DW_OP_reg0;
13329           if (i < size)
13330             dwarf2_complex_location_expr_complaint ();
13331           break;
13332
13333         case DW_OP_regx:
13334           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13335           i += bytes_read;
13336           stack[++stacki] = unsnd;
13337           if (i < size)
13338             dwarf2_complex_location_expr_complaint ();
13339           break;
13340
13341         case DW_OP_addr:
13342           stack[++stacki] = read_address (objfile->obfd, &data[i],
13343                                           cu, &bytes_read);
13344           i += bytes_read;
13345           break;
13346
13347         case DW_OP_const1u:
13348           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13349           i += 1;
13350           break;
13351
13352         case DW_OP_const1s:
13353           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13354           i += 1;
13355           break;
13356
13357         case DW_OP_const2u:
13358           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13359           i += 2;
13360           break;
13361
13362         case DW_OP_const2s:
13363           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13364           i += 2;
13365           break;
13366
13367         case DW_OP_const4u:
13368           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13369           i += 4;
13370           break;
13371
13372         case DW_OP_const4s:
13373           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13374           i += 4;
13375           break;
13376
13377         case DW_OP_constu:
13378           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13379                                                   &bytes_read);
13380           i += bytes_read;
13381           break;
13382
13383         case DW_OP_consts:
13384           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13385           i += bytes_read;
13386           break;
13387
13388         case DW_OP_dup:
13389           stack[stacki + 1] = stack[stacki];
13390           stacki++;
13391           break;
13392
13393         case DW_OP_plus:
13394           stack[stacki - 1] += stack[stacki];
13395           stacki--;
13396           break;
13397
13398         case DW_OP_plus_uconst:
13399           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13400           i += bytes_read;
13401           break;
13402
13403         case DW_OP_minus:
13404           stack[stacki - 1] -= stack[stacki];
13405           stacki--;
13406           break;
13407
13408         case DW_OP_deref:
13409           /* If we're not the last op, then we definitely can't encode
13410              this using GDB's address_class enum.  This is valid for partial
13411              global symbols, although the variable's address will be bogus
13412              in the psymtab.  */
13413           if (i < size)
13414             dwarf2_complex_location_expr_complaint ();
13415           break;
13416
13417         case DW_OP_GNU_push_tls_address:
13418           /* The top of the stack has the offset from the beginning
13419              of the thread control block at which the variable is located.  */
13420           /* Nothing should follow this operator, so the top of stack would
13421              be returned.  */
13422           /* This is valid for partial global symbols, but the variable's
13423              address will be bogus in the psymtab.  */
13424           if (i < size)
13425             dwarf2_complex_location_expr_complaint ();
13426           break;
13427
13428         case DW_OP_GNU_uninit:
13429           break;
13430
13431         default:
13432           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13433                      dwarf_stack_op_name (op, 1));
13434           return (stack[stacki]);
13435         }
13436     }
13437   return (stack[stacki]);
13438 }
13439
13440 /* memory allocation interface */
13441
13442 static struct dwarf_block *
13443 dwarf_alloc_block (struct dwarf2_cu *cu)
13444 {
13445   struct dwarf_block *blk;
13446
13447   blk = (struct dwarf_block *)
13448     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13449   return (blk);
13450 }
13451
13452 static struct abbrev_info *
13453 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13454 {
13455   struct abbrev_info *abbrev;
13456
13457   abbrev = (struct abbrev_info *)
13458     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13459   memset (abbrev, 0, sizeof (struct abbrev_info));
13460   return (abbrev);
13461 }
13462
13463 static struct die_info *
13464 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13465 {
13466   struct die_info *die;
13467   size_t size = sizeof (struct die_info);
13468
13469   if (num_attrs > 1)
13470     size += (num_attrs - 1) * sizeof (struct attribute);
13471
13472   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13473   memset (die, 0, sizeof (struct die_info));
13474   return (die);
13475 }
13476
13477 \f
13478 /* Macro support.  */
13479
13480
13481 /* Return the full name of file number I in *LH's file name table.
13482    Use COMP_DIR as the name of the current directory of the
13483    compilation.  The result is allocated using xmalloc; the caller is
13484    responsible for freeing it.  */
13485 static char *
13486 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13487 {
13488   /* Is the file number a valid index into the line header's file name
13489      table?  Remember that file numbers start with one, not zero.  */
13490   if (1 <= file && file <= lh->num_file_names)
13491     {
13492       struct file_entry *fe = &lh->file_names[file - 1];
13493
13494       if (IS_ABSOLUTE_PATH (fe->name))
13495         return xstrdup (fe->name);
13496       else
13497         {
13498           const char *dir;
13499           int dir_len;
13500           char *full_name;
13501
13502           if (fe->dir_index)
13503             dir = lh->include_dirs[fe->dir_index - 1];
13504           else
13505             dir = comp_dir;
13506
13507           if (dir)
13508             {
13509               dir_len = strlen (dir);
13510               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13511               strcpy (full_name, dir);
13512               full_name[dir_len] = '/';
13513               strcpy (full_name + dir_len + 1, fe->name);
13514               return full_name;
13515             }
13516           else
13517             return xstrdup (fe->name);
13518         }
13519     }
13520   else
13521     {
13522       /* The compiler produced a bogus file number.  We can at least
13523          record the macro definitions made in the file, even if we
13524          won't be able to find the file by name.  */
13525       char fake_name[80];
13526
13527       sprintf (fake_name, "<bad macro file number %d>", file);
13528
13529       complaint (&symfile_complaints,
13530                  _("bad file number in macro information (%d)"),
13531                  file);
13532
13533       return xstrdup (fake_name);
13534     }
13535 }
13536
13537
13538 static struct macro_source_file *
13539 macro_start_file (int file, int line,
13540                   struct macro_source_file *current_file,
13541                   const char *comp_dir,
13542                   struct line_header *lh, struct objfile *objfile)
13543 {
13544   /* The full name of this source file.  */
13545   char *full_name = file_full_name (file, lh, comp_dir);
13546
13547   /* We don't create a macro table for this compilation unit
13548      at all until we actually get a filename.  */
13549   if (! pending_macros)
13550     pending_macros = new_macro_table (&objfile->objfile_obstack,
13551                                       objfile->macro_cache);
13552
13553   if (! current_file)
13554     /* If we have no current file, then this must be the start_file
13555        directive for the compilation unit's main source file.  */
13556     current_file = macro_set_main (pending_macros, full_name);
13557   else
13558     current_file = macro_include (current_file, line, full_name);
13559
13560   xfree (full_name);
13561
13562   return current_file;
13563 }
13564
13565
13566 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13567    followed by a null byte.  */
13568 static char *
13569 copy_string (const char *buf, int len)
13570 {
13571   char *s = xmalloc (len + 1);
13572
13573   memcpy (s, buf, len);
13574   s[len] = '\0';
13575   return s;
13576 }
13577
13578
13579 static const char *
13580 consume_improper_spaces (const char *p, const char *body)
13581 {
13582   if (*p == ' ')
13583     {
13584       complaint (&symfile_complaints,
13585                  _("macro definition contains spaces in formal argument list:\n`%s'"),
13586                  body);
13587
13588       while (*p == ' ')
13589         p++;
13590     }
13591
13592   return p;
13593 }
13594
13595
13596 static void
13597 parse_macro_definition (struct macro_source_file *file, int line,
13598                         const char *body)
13599 {
13600   const char *p;
13601
13602   /* The body string takes one of two forms.  For object-like macro
13603      definitions, it should be:
13604
13605         <macro name> " " <definition>
13606
13607      For function-like macro definitions, it should be:
13608
13609         <macro name> "() " <definition>
13610      or
13611         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13612
13613      Spaces may appear only where explicitly indicated, and in the
13614      <definition>.
13615
13616      The Dwarf 2 spec says that an object-like macro's name is always
13617      followed by a space, but versions of GCC around March 2002 omit
13618      the space when the macro's definition is the empty string.
13619
13620      The Dwarf 2 spec says that there should be no spaces between the
13621      formal arguments in a function-like macro's formal argument list,
13622      but versions of GCC around March 2002 include spaces after the
13623      commas.  */
13624
13625
13626   /* Find the extent of the macro name.  The macro name is terminated
13627      by either a space or null character (for an object-like macro) or
13628      an opening paren (for a function-like macro).  */
13629   for (p = body; *p; p++)
13630     if (*p == ' ' || *p == '(')
13631       break;
13632
13633   if (*p == ' ' || *p == '\0')
13634     {
13635       /* It's an object-like macro.  */
13636       int name_len = p - body;
13637       char *name = copy_string (body, name_len);
13638       const char *replacement;
13639
13640       if (*p == ' ')
13641         replacement = body + name_len + 1;
13642       else
13643         {
13644           dwarf2_macro_malformed_definition_complaint (body);
13645           replacement = body + name_len;
13646         }
13647
13648       macro_define_object (file, line, name, replacement);
13649
13650       xfree (name);
13651     }
13652   else if (*p == '(')
13653     {
13654       /* It's a function-like macro.  */
13655       char *name = copy_string (body, p - body);
13656       int argc = 0;
13657       int argv_size = 1;
13658       char **argv = xmalloc (argv_size * sizeof (*argv));
13659
13660       p++;
13661
13662       p = consume_improper_spaces (p, body);
13663
13664       /* Parse the formal argument list.  */
13665       while (*p && *p != ')')
13666         {
13667           /* Find the extent of the current argument name.  */
13668           const char *arg_start = p;
13669
13670           while (*p && *p != ',' && *p != ')' && *p != ' ')
13671             p++;
13672
13673           if (! *p || p == arg_start)
13674             dwarf2_macro_malformed_definition_complaint (body);
13675           else
13676             {
13677               /* Make sure argv has room for the new argument.  */
13678               if (argc >= argv_size)
13679                 {
13680                   argv_size *= 2;
13681                   argv = xrealloc (argv, argv_size * sizeof (*argv));
13682                 }
13683
13684               argv[argc++] = copy_string (arg_start, p - arg_start);
13685             }
13686
13687           p = consume_improper_spaces (p, body);
13688
13689           /* Consume the comma, if present.  */
13690           if (*p == ',')
13691             {
13692               p++;
13693
13694               p = consume_improper_spaces (p, body);
13695             }
13696         }
13697
13698       if (*p == ')')
13699         {
13700           p++;
13701
13702           if (*p == ' ')
13703             /* Perfectly formed definition, no complaints.  */
13704             macro_define_function (file, line, name,
13705                                    argc, (const char **) argv,
13706                                    p + 1);
13707           else if (*p == '\0')
13708             {
13709               /* Complain, but do define it.  */
13710               dwarf2_macro_malformed_definition_complaint (body);
13711               macro_define_function (file, line, name,
13712                                      argc, (const char **) argv,
13713                                      p);
13714             }
13715           else
13716             /* Just complain.  */
13717             dwarf2_macro_malformed_definition_complaint (body);
13718         }
13719       else
13720         /* Just complain.  */
13721         dwarf2_macro_malformed_definition_complaint (body);
13722
13723       xfree (name);
13724       {
13725         int i;
13726
13727         for (i = 0; i < argc; i++)
13728           xfree (argv[i]);
13729       }
13730       xfree (argv);
13731     }
13732   else
13733     dwarf2_macro_malformed_definition_complaint (body);
13734 }
13735
13736
13737 static void
13738 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13739                      char *comp_dir, bfd *abfd,
13740                      struct dwarf2_cu *cu)
13741 {
13742   gdb_byte *mac_ptr, *mac_end;
13743   struct macro_source_file *current_file = 0;
13744   enum dwarf_macinfo_record_type macinfo_type;
13745   int at_commandline;
13746
13747   dwarf2_read_section (dwarf2_per_objfile->objfile,
13748                        &dwarf2_per_objfile->macinfo);
13749   if (dwarf2_per_objfile->macinfo.buffer == NULL)
13750     {
13751       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13752       return;
13753     }
13754
13755   /* First pass: Find the name of the base filename.
13756      This filename is needed in order to process all macros whose definition
13757      (or undefinition) comes from the command line.  These macros are defined
13758      before the first DW_MACINFO_start_file entry, and yet still need to be
13759      associated to the base file.
13760
13761      To determine the base file name, we scan the macro definitions until we
13762      reach the first DW_MACINFO_start_file entry.  We then initialize
13763      CURRENT_FILE accordingly so that any macro definition found before the
13764      first DW_MACINFO_start_file can still be associated to the base file.  */
13765
13766   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13767   mac_end = dwarf2_per_objfile->macinfo.buffer
13768     + dwarf2_per_objfile->macinfo.size;
13769
13770   do
13771     {
13772       /* Do we at least have room for a macinfo type byte?  */
13773       if (mac_ptr >= mac_end)
13774         {
13775           /* Complaint is printed during the second pass as GDB will probably
13776              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
13777           break;
13778         }
13779
13780       macinfo_type = read_1_byte (abfd, mac_ptr);
13781       mac_ptr++;
13782
13783       switch (macinfo_type)
13784         {
13785           /* A zero macinfo type indicates the end of the macro
13786              information.  */
13787         case 0:
13788           break;
13789
13790         case DW_MACINFO_define:
13791         case DW_MACINFO_undef:
13792           /* Only skip the data by MAC_PTR.  */
13793           {
13794             unsigned int bytes_read;
13795
13796             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13797             mac_ptr += bytes_read;
13798             read_direct_string (abfd, mac_ptr, &bytes_read);
13799             mac_ptr += bytes_read;
13800           }
13801           break;
13802
13803         case DW_MACINFO_start_file:
13804           {
13805             unsigned int bytes_read;
13806             int line, file;
13807
13808             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13809             mac_ptr += bytes_read;
13810             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13811             mac_ptr += bytes_read;
13812
13813             current_file = macro_start_file (file, line, current_file, comp_dir,
13814                                              lh, cu->objfile);
13815           }
13816           break;
13817
13818         case DW_MACINFO_end_file:
13819           /* No data to skip by MAC_PTR.  */
13820           break;
13821
13822         case DW_MACINFO_vendor_ext:
13823           /* Only skip the data by MAC_PTR.  */
13824           {
13825             unsigned int bytes_read;
13826
13827             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13828             mac_ptr += bytes_read;
13829             read_direct_string (abfd, mac_ptr, &bytes_read);
13830             mac_ptr += bytes_read;
13831           }
13832           break;
13833
13834         default:
13835           break;
13836         }
13837     } while (macinfo_type != 0 && current_file == NULL);
13838
13839   /* Second pass: Process all entries.
13840
13841      Use the AT_COMMAND_LINE flag to determine whether we are still processing
13842      command-line macro definitions/undefinitions.  This flag is unset when we
13843      reach the first DW_MACINFO_start_file entry.  */
13844
13845   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13846
13847   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
13848      GDB is still reading the definitions from command line.  First
13849      DW_MACINFO_start_file will need to be ignored as it was already executed
13850      to create CURRENT_FILE for the main source holding also the command line
13851      definitions.  On first met DW_MACINFO_start_file this flag is reset to
13852      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
13853
13854   at_commandline = 1;
13855
13856   do
13857     {
13858       /* Do we at least have room for a macinfo type byte?  */
13859       if (mac_ptr >= mac_end)
13860         {
13861           dwarf2_macros_too_long_complaint ();
13862           break;
13863         }
13864
13865       macinfo_type = read_1_byte (abfd, mac_ptr);
13866       mac_ptr++;
13867
13868       switch (macinfo_type)
13869         {
13870           /* A zero macinfo type indicates the end of the macro
13871              information.  */
13872         case 0:
13873           break;
13874
13875         case DW_MACINFO_define:
13876         case DW_MACINFO_undef:
13877           {
13878             unsigned int bytes_read;
13879             int line;
13880             char *body;
13881
13882             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13883             mac_ptr += bytes_read;
13884             body = read_direct_string (abfd, mac_ptr, &bytes_read);
13885             mac_ptr += bytes_read;
13886
13887             if (! current_file)
13888               {
13889                 /* DWARF violation as no main source is present.  */
13890                 complaint (&symfile_complaints,
13891                            _("debug info with no main source gives macro %s "
13892                              "on line %d: %s"),
13893                            macinfo_type == DW_MACINFO_define ?
13894                              _("definition") :
13895                                macinfo_type == DW_MACINFO_undef ?
13896                                  _("undefinition") :
13897                                  _("something-or-other"), line, body);
13898                 break;
13899               }
13900             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13901               complaint (&symfile_complaints,
13902                          _("debug info gives %s macro %s with %s line %d: %s"),
13903                          at_commandline ? _("command-line") : _("in-file"),
13904                          macinfo_type == DW_MACINFO_define ?
13905                            _("definition") :
13906                              macinfo_type == DW_MACINFO_undef ?
13907                                _("undefinition") :
13908                                _("something-or-other"),
13909                          line == 0 ? _("zero") : _("non-zero"), line, body);
13910
13911             if (macinfo_type == DW_MACINFO_define)
13912               parse_macro_definition (current_file, line, body);
13913             else if (macinfo_type == DW_MACINFO_undef)
13914               macro_undef (current_file, line, body);
13915           }
13916           break;
13917
13918         case DW_MACINFO_start_file:
13919           {
13920             unsigned int bytes_read;
13921             int line, file;
13922
13923             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13924             mac_ptr += bytes_read;
13925             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13926             mac_ptr += bytes_read;
13927
13928             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13929               complaint (&symfile_complaints,
13930                          _("debug info gives source %d included "
13931                            "from %s at %s line %d"),
13932                          file, at_commandline ? _("command-line") : _("file"),
13933                          line == 0 ? _("zero") : _("non-zero"), line);
13934
13935             if (at_commandline)
13936               {
13937                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13938                 at_commandline = 0;
13939               }
13940             else
13941               current_file = macro_start_file (file, line,
13942                                                current_file, comp_dir,
13943                                                lh, cu->objfile);
13944           }
13945           break;
13946
13947         case DW_MACINFO_end_file:
13948           if (! current_file)
13949             complaint (&symfile_complaints,
13950                        _("macro debug info has an unmatched `close_file' directive"));
13951           else
13952             {
13953               current_file = current_file->included_by;
13954               if (! current_file)
13955                 {
13956                   enum dwarf_macinfo_record_type next_type;
13957
13958                   /* GCC circa March 2002 doesn't produce the zero
13959                      type byte marking the end of the compilation
13960                      unit.  Complain if it's not there, but exit no
13961                      matter what.  */
13962
13963                   /* Do we at least have room for a macinfo type byte?  */
13964                   if (mac_ptr >= mac_end)
13965                     {
13966                       dwarf2_macros_too_long_complaint ();
13967                       return;
13968                     }
13969
13970                   /* We don't increment mac_ptr here, so this is just
13971                      a look-ahead.  */
13972                   next_type = read_1_byte (abfd, mac_ptr);
13973                   if (next_type != 0)
13974                     complaint (&symfile_complaints,
13975                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13976
13977                   return;
13978                 }
13979             }
13980           break;
13981
13982         case DW_MACINFO_vendor_ext:
13983           {
13984             unsigned int bytes_read;
13985             int constant;
13986             char *string;
13987
13988             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13989             mac_ptr += bytes_read;
13990             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13991             mac_ptr += bytes_read;
13992
13993             /* We don't recognize any vendor extensions.  */
13994           }
13995           break;
13996         }
13997     } while (macinfo_type != 0);
13998 }
13999
14000 /* Check if the attribute's form is a DW_FORM_block*
14001    if so return true else false. */
14002 static int
14003 attr_form_is_block (struct attribute *attr)
14004 {
14005   return (attr == NULL ? 0 :
14006       attr->form == DW_FORM_block1
14007       || attr->form == DW_FORM_block2
14008       || attr->form == DW_FORM_block4
14009       || attr->form == DW_FORM_block
14010       || attr->form == DW_FORM_exprloc);
14011 }
14012
14013 /* Return non-zero if ATTR's value is a section offset --- classes
14014    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14015    You may use DW_UNSND (attr) to retrieve such offsets.
14016
14017    Section 7.5.4, "Attribute Encodings", explains that no attribute
14018    may have a value that belongs to more than one of these classes; it
14019    would be ambiguous if we did, because we use the same forms for all
14020    of them.  */
14021 static int
14022 attr_form_is_section_offset (struct attribute *attr)
14023 {
14024   return (attr->form == DW_FORM_data4
14025           || attr->form == DW_FORM_data8
14026           || attr->form == DW_FORM_sec_offset);
14027 }
14028
14029
14030 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14031    zero otherwise.  When this function returns true, you can apply
14032    dwarf2_get_attr_constant_value to it.
14033
14034    However, note that for some attributes you must check
14035    attr_form_is_section_offset before using this test.  DW_FORM_data4
14036    and DW_FORM_data8 are members of both the constant class, and of
14037    the classes that contain offsets into other debug sections
14038    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14039    that, if an attribute's can be either a constant or one of the
14040    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14041    taken as section offsets, not constants.  */
14042 static int
14043 attr_form_is_constant (struct attribute *attr)
14044 {
14045   switch (attr->form)
14046     {
14047     case DW_FORM_sdata:
14048     case DW_FORM_udata:
14049     case DW_FORM_data1:
14050     case DW_FORM_data2:
14051     case DW_FORM_data4:
14052     case DW_FORM_data8:
14053       return 1;
14054     default:
14055       return 0;
14056     }
14057 }
14058
14059 static void
14060 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14061                              struct dwarf2_cu *cu)
14062 {
14063   if (attr_form_is_section_offset (attr)
14064       /* ".debug_loc" may not exist at all, or the offset may be outside
14065          the section.  If so, fall through to the complaint in the
14066          other branch.  */
14067       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
14068     {
14069       struct dwarf2_loclist_baton *baton;
14070
14071       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14072                              sizeof (struct dwarf2_loclist_baton));
14073       baton->per_cu = cu->per_cu;
14074       gdb_assert (baton->per_cu);
14075
14076       dwarf2_read_section (dwarf2_per_objfile->objfile,
14077                            &dwarf2_per_objfile->loc);
14078
14079       /* We don't know how long the location list is, but make sure we
14080          don't run off the edge of the section.  */
14081       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14082       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14083       baton->base_address = cu->base_address;
14084       if (cu->base_known == 0)
14085         complaint (&symfile_complaints,
14086                    _("Location list used without specifying the CU base address."));
14087
14088       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14089       SYMBOL_LOCATION_BATON (sym) = baton;
14090     }
14091   else
14092     {
14093       struct dwarf2_locexpr_baton *baton;
14094
14095       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14096                              sizeof (struct dwarf2_locexpr_baton));
14097       baton->per_cu = cu->per_cu;
14098       gdb_assert (baton->per_cu);
14099
14100       if (attr_form_is_block (attr))
14101         {
14102           /* Note that we're just copying the block's data pointer
14103              here, not the actual data.  We're still pointing into the
14104              info_buffer for SYM's objfile; right now we never release
14105              that buffer, but when we do clean up properly this may
14106              need to change.  */
14107           baton->size = DW_BLOCK (attr)->size;
14108           baton->data = DW_BLOCK (attr)->data;
14109         }
14110       else
14111         {
14112           dwarf2_invalid_attrib_class_complaint ("location description",
14113                                                  SYMBOL_NATURAL_NAME (sym));
14114           baton->size = 0;
14115           baton->data = NULL;
14116         }
14117
14118       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14119       SYMBOL_LOCATION_BATON (sym) = baton;
14120     }
14121 }
14122
14123 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14124    came from a separate debuginfo file, then the master objfile is
14125    returned.  */
14126
14127 struct objfile *
14128 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14129 {
14130   struct objfile *objfile = per_cu->objfile;
14131
14132   /* Return the master objfile, so that we can report and look up the
14133      correct file containing this variable.  */
14134   if (objfile->separate_debug_objfile_backlink)
14135     objfile = objfile->separate_debug_objfile_backlink;
14136
14137   return objfile;
14138 }
14139
14140 /* Return the address size given in the compilation unit header for CU.  */
14141
14142 CORE_ADDR
14143 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14144 {
14145   if (per_cu->cu)
14146     return per_cu->cu->header.addr_size;
14147   else
14148     {
14149       /* If the CU is not currently read in, we re-read its header.  */
14150       struct objfile *objfile = per_cu->objfile;
14151       struct dwarf2_per_objfile *per_objfile
14152         = objfile_data (objfile, dwarf2_objfile_data_key);
14153       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14154       struct comp_unit_head cu_header;
14155
14156       memset (&cu_header, 0, sizeof cu_header);
14157       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14158       return cu_header.addr_size;
14159     }
14160 }
14161
14162 /* Return the offset size given in the compilation unit header for CU.  */
14163
14164 int
14165 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14166 {
14167   if (per_cu->cu)
14168     return per_cu->cu->header.offset_size;
14169   else
14170     {
14171       /* If the CU is not currently read in, we re-read its header.  */
14172       struct objfile *objfile = per_cu->objfile;
14173       struct dwarf2_per_objfile *per_objfile
14174         = objfile_data (objfile, dwarf2_objfile_data_key);
14175       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14176       struct comp_unit_head cu_header;
14177
14178       memset (&cu_header, 0, sizeof cu_header);
14179       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14180       return cu_header.offset_size;
14181     }
14182 }
14183
14184 /* Return the text offset of the CU.  The returned offset comes from
14185    this CU's objfile.  If this objfile came from a separate debuginfo
14186    file, then the offset may be different from the corresponding
14187    offset in the parent objfile.  */
14188
14189 CORE_ADDR
14190 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14191 {
14192   struct objfile *objfile = per_cu->objfile;
14193
14194   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14195 }
14196
14197 /* Locate the .debug_info compilation unit from CU's objfile which contains
14198    the DIE at OFFSET.  Raises an error on failure.  */
14199
14200 static struct dwarf2_per_cu_data *
14201 dwarf2_find_containing_comp_unit (unsigned int offset,
14202                                   struct objfile *objfile)
14203 {
14204   struct dwarf2_per_cu_data *this_cu;
14205   int low, high;
14206
14207   low = 0;
14208   high = dwarf2_per_objfile->n_comp_units - 1;
14209   while (high > low)
14210     {
14211       int mid = low + (high - low) / 2;
14212
14213       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14214         high = mid;
14215       else
14216         low = mid + 1;
14217     }
14218   gdb_assert (low == high);
14219   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14220     {
14221       if (low == 0)
14222         error (_("Dwarf Error: could not find partial DIE containing "
14223                "offset 0x%lx [in module %s]"),
14224                (long) offset, bfd_get_filename (objfile->obfd));
14225
14226       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14227       return dwarf2_per_objfile->all_comp_units[low-1];
14228     }
14229   else
14230     {
14231       this_cu = dwarf2_per_objfile->all_comp_units[low];
14232       if (low == dwarf2_per_objfile->n_comp_units - 1
14233           && offset >= this_cu->offset + this_cu->length)
14234         error (_("invalid dwarf2 offset %u"), offset);
14235       gdb_assert (offset < this_cu->offset + this_cu->length);
14236       return this_cu;
14237     }
14238 }
14239
14240 /* Locate the compilation unit from OBJFILE which is located at exactly
14241    OFFSET.  Raises an error on failure.  */
14242
14243 static struct dwarf2_per_cu_data *
14244 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14245 {
14246   struct dwarf2_per_cu_data *this_cu;
14247
14248   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14249   if (this_cu->offset != offset)
14250     error (_("no compilation unit with offset %u."), offset);
14251   return this_cu;
14252 }
14253
14254 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
14255
14256 static struct dwarf2_cu *
14257 alloc_one_comp_unit (struct objfile *objfile)
14258 {
14259   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14260   cu->objfile = objfile;
14261   obstack_init (&cu->comp_unit_obstack);
14262   return cu;
14263 }
14264
14265 /* Release one cached compilation unit, CU.  We unlink it from the tree
14266    of compilation units, but we don't remove it from the read_in_chain;
14267    the caller is responsible for that.
14268    NOTE: DATA is a void * because this function is also used as a
14269    cleanup routine.  */
14270
14271 static void
14272 free_one_comp_unit (void *data)
14273 {
14274   struct dwarf2_cu *cu = data;
14275
14276   if (cu->per_cu != NULL)
14277     cu->per_cu->cu = NULL;
14278   cu->per_cu = NULL;
14279
14280   obstack_free (&cu->comp_unit_obstack, NULL);
14281
14282   xfree (cu);
14283 }
14284
14285 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14286    when we're finished with it.  We can't free the pointer itself, but be
14287    sure to unlink it from the cache.  Also release any associated storage
14288    and perform cache maintenance.
14289
14290    Only used during partial symbol parsing.  */
14291
14292 static void
14293 free_stack_comp_unit (void *data)
14294 {
14295   struct dwarf2_cu *cu = data;
14296
14297   obstack_free (&cu->comp_unit_obstack, NULL);
14298   cu->partial_dies = NULL;
14299
14300   if (cu->per_cu != NULL)
14301     {
14302       /* This compilation unit is on the stack in our caller, so we
14303          should not xfree it.  Just unlink it.  */
14304       cu->per_cu->cu = NULL;
14305       cu->per_cu = NULL;
14306
14307       /* If we had a per-cu pointer, then we may have other compilation
14308          units loaded, so age them now.  */
14309       age_cached_comp_units ();
14310     }
14311 }
14312
14313 /* Free all cached compilation units.  */
14314
14315 static void
14316 free_cached_comp_units (void *data)
14317 {
14318   struct dwarf2_per_cu_data *per_cu, **last_chain;
14319
14320   per_cu = dwarf2_per_objfile->read_in_chain;
14321   last_chain = &dwarf2_per_objfile->read_in_chain;
14322   while (per_cu != NULL)
14323     {
14324       struct dwarf2_per_cu_data *next_cu;
14325
14326       next_cu = per_cu->cu->read_in_chain;
14327
14328       free_one_comp_unit (per_cu->cu);
14329       *last_chain = next_cu;
14330
14331       per_cu = next_cu;
14332     }
14333 }
14334
14335 /* Increase the age counter on each cached compilation unit, and free
14336    any that are too old.  */
14337
14338 static void
14339 age_cached_comp_units (void)
14340 {
14341   struct dwarf2_per_cu_data *per_cu, **last_chain;
14342
14343   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14344   per_cu = dwarf2_per_objfile->read_in_chain;
14345   while (per_cu != NULL)
14346     {
14347       per_cu->cu->last_used ++;
14348       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14349         dwarf2_mark (per_cu->cu);
14350       per_cu = per_cu->cu->read_in_chain;
14351     }
14352
14353   per_cu = dwarf2_per_objfile->read_in_chain;
14354   last_chain = &dwarf2_per_objfile->read_in_chain;
14355   while (per_cu != NULL)
14356     {
14357       struct dwarf2_per_cu_data *next_cu;
14358
14359       next_cu = per_cu->cu->read_in_chain;
14360
14361       if (!per_cu->cu->mark)
14362         {
14363           free_one_comp_unit (per_cu->cu);
14364           *last_chain = next_cu;
14365         }
14366       else
14367         last_chain = &per_cu->cu->read_in_chain;
14368
14369       per_cu = next_cu;
14370     }
14371 }
14372
14373 /* Remove a single compilation unit from the cache.  */
14374
14375 static void
14376 free_one_cached_comp_unit (void *target_cu)
14377 {
14378   struct dwarf2_per_cu_data *per_cu, **last_chain;
14379
14380   per_cu = dwarf2_per_objfile->read_in_chain;
14381   last_chain = &dwarf2_per_objfile->read_in_chain;
14382   while (per_cu != NULL)
14383     {
14384       struct dwarf2_per_cu_data *next_cu;
14385
14386       next_cu = per_cu->cu->read_in_chain;
14387
14388       if (per_cu->cu == target_cu)
14389         {
14390           free_one_comp_unit (per_cu->cu);
14391           *last_chain = next_cu;
14392           break;
14393         }
14394       else
14395         last_chain = &per_cu->cu->read_in_chain;
14396
14397       per_cu = next_cu;
14398     }
14399 }
14400
14401 /* Release all extra memory associated with OBJFILE.  */
14402
14403 void
14404 dwarf2_free_objfile (struct objfile *objfile)
14405 {
14406   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14407
14408   if (dwarf2_per_objfile == NULL)
14409     return;
14410
14411   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14412   free_cached_comp_units (NULL);
14413
14414   if (dwarf2_per_objfile->using_index)
14415     {
14416       int i;
14417
14418       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14419         {
14420           int j;
14421           struct dwarf2_per_cu_data *per_cu =
14422             dwarf2_per_objfile->all_comp_units[i];
14423
14424           if (!per_cu->v.quick->lines)
14425             continue;
14426
14427           for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14428             {
14429               if (per_cu->v.quick->file_names)
14430                 xfree ((void *) per_cu->v.quick->file_names[j]);
14431               if (per_cu->v.quick->full_names)
14432                 xfree ((void *) per_cu->v.quick->full_names[j]);
14433             }
14434
14435           free_line_header (per_cu->v.quick->lines);
14436         }
14437     }
14438
14439   /* Everything else should be on the objfile obstack.  */
14440 }
14441
14442 /* A pair of DIE offset and GDB type pointer.  We store these
14443    in a hash table separate from the DIEs, and preserve them
14444    when the DIEs are flushed out of cache.  */
14445
14446 struct dwarf2_offset_and_type
14447 {
14448   unsigned int offset;
14449   struct type *type;
14450 };
14451
14452 /* Hash function for a dwarf2_offset_and_type.  */
14453
14454 static hashval_t
14455 offset_and_type_hash (const void *item)
14456 {
14457   const struct dwarf2_offset_and_type *ofs = item;
14458
14459   return ofs->offset;
14460 }
14461
14462 /* Equality function for a dwarf2_offset_and_type.  */
14463
14464 static int
14465 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14466 {
14467   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14468   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14469
14470   return ofs_lhs->offset == ofs_rhs->offset;
14471 }
14472
14473 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14474    table if necessary.  For convenience, return TYPE.
14475
14476    The DIEs reading must have careful ordering to:
14477     * Not cause infite loops trying to read in DIEs as a prerequisite for
14478       reading current DIE.
14479     * Not trying to dereference contents of still incompletely read in types
14480       while reading in other DIEs.
14481     * Enable referencing still incompletely read in types just by a pointer to
14482       the type without accessing its fields.
14483
14484    Therefore caller should follow these rules:
14485      * Try to fetch any prerequisite types we may need to build this DIE type
14486        before building the type and calling set_die_type.
14487      * After building type call set_die_type for current DIE as soon as
14488        possible before fetching more types to complete the current type.
14489      * Make the type as complete as possible before fetching more types.  */
14490
14491 static struct type *
14492 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14493 {
14494   struct dwarf2_offset_and_type **slot, ofs;
14495   struct objfile *objfile = cu->objfile;
14496   htab_t *type_hash_ptr;
14497
14498   /* For Ada types, make sure that the gnat-specific data is always
14499      initialized (if not already set).  There are a few types where
14500      we should not be doing so, because the type-specific area is
14501      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14502      where the type-specific area is used to store the floatformat).
14503      But this is not a problem, because the gnat-specific information
14504      is actually not needed for these types.  */
14505   if (need_gnat_info (cu)
14506       && TYPE_CODE (type) != TYPE_CODE_FUNC
14507       && TYPE_CODE (type) != TYPE_CODE_FLT
14508       && !HAVE_GNAT_AUX_INFO (type))
14509     INIT_GNAT_SPECIFIC (type);
14510
14511   if (cu->per_cu->from_debug_types)
14512     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14513   else
14514     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14515
14516   if (*type_hash_ptr == NULL)
14517     {
14518       *type_hash_ptr
14519         = htab_create_alloc_ex (127,
14520                                 offset_and_type_hash,
14521                                 offset_and_type_eq,
14522                                 NULL,
14523                                 &objfile->objfile_obstack,
14524                                 hashtab_obstack_allocate,
14525                                 dummy_obstack_deallocate);
14526     }
14527
14528   ofs.offset = die->offset;
14529   ofs.type = type;
14530   slot = (struct dwarf2_offset_and_type **)
14531     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14532   if (*slot)
14533     complaint (&symfile_complaints,
14534                _("A problem internal to GDB: DIE 0x%x has type already set"),
14535                die->offset);
14536   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14537   **slot = ofs;
14538   return type;
14539 }
14540
14541 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14542    table, or return NULL if the die does not have a saved type.  */
14543
14544 static struct type *
14545 get_die_type_at_offset (unsigned int offset,
14546                         struct dwarf2_per_cu_data *per_cu)
14547 {
14548   struct dwarf2_offset_and_type *slot, ofs;
14549   htab_t type_hash;
14550
14551   if (per_cu->from_debug_types)
14552     type_hash = dwarf2_per_objfile->debug_types_type_hash;
14553   else
14554     type_hash = dwarf2_per_objfile->debug_info_type_hash;
14555   if (type_hash == NULL)
14556     return NULL;
14557
14558   ofs.offset = offset;
14559   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14560   if (slot)
14561     return slot->type;
14562   else
14563     return NULL;
14564 }
14565
14566 /* Look up the type for DIE in the appropriate type_hash table,
14567    or return NULL if DIE does not have a saved type.  */
14568
14569 static struct type *
14570 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14571 {
14572   return get_die_type_at_offset (die->offset, cu->per_cu);
14573 }
14574
14575 /* Add a dependence relationship from CU to REF_PER_CU.  */
14576
14577 static void
14578 dwarf2_add_dependence (struct dwarf2_cu *cu,
14579                        struct dwarf2_per_cu_data *ref_per_cu)
14580 {
14581   void **slot;
14582
14583   if (cu->dependencies == NULL)
14584     cu->dependencies
14585       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14586                               NULL, &cu->comp_unit_obstack,
14587                               hashtab_obstack_allocate,
14588                               dummy_obstack_deallocate);
14589
14590   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14591   if (*slot == NULL)
14592     *slot = ref_per_cu;
14593 }
14594
14595 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14596    Set the mark field in every compilation unit in the
14597    cache that we must keep because we are keeping CU.  */
14598
14599 static int
14600 dwarf2_mark_helper (void **slot, void *data)
14601 {
14602   struct dwarf2_per_cu_data *per_cu;
14603
14604   per_cu = (struct dwarf2_per_cu_data *) *slot;
14605   if (per_cu->cu->mark)
14606     return 1;
14607   per_cu->cu->mark = 1;
14608
14609   if (per_cu->cu->dependencies != NULL)
14610     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14611
14612   return 1;
14613 }
14614
14615 /* Set the mark field in CU and in every other compilation unit in the
14616    cache that we must keep because we are keeping CU.  */
14617
14618 static void
14619 dwarf2_mark (struct dwarf2_cu *cu)
14620 {
14621   if (cu->mark)
14622     return;
14623   cu->mark = 1;
14624   if (cu->dependencies != NULL)
14625     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14626 }
14627
14628 static void
14629 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14630 {
14631   while (per_cu)
14632     {
14633       per_cu->cu->mark = 0;
14634       per_cu = per_cu->cu->read_in_chain;
14635     }
14636 }
14637
14638 /* Trivial hash function for partial_die_info: the hash value of a DIE
14639    is its offset in .debug_info for this objfile.  */
14640
14641 static hashval_t
14642 partial_die_hash (const void *item)
14643 {
14644   const struct partial_die_info *part_die = item;
14645
14646   return part_die->offset;
14647 }
14648
14649 /* Trivial comparison function for partial_die_info structures: two DIEs
14650    are equal if they have the same offset.  */
14651
14652 static int
14653 partial_die_eq (const void *item_lhs, const void *item_rhs)
14654 {
14655   const struct partial_die_info *part_die_lhs = item_lhs;
14656   const struct partial_die_info *part_die_rhs = item_rhs;
14657
14658   return part_die_lhs->offset == part_die_rhs->offset;
14659 }
14660
14661 static struct cmd_list_element *set_dwarf2_cmdlist;
14662 static struct cmd_list_element *show_dwarf2_cmdlist;
14663
14664 static void
14665 set_dwarf2_cmd (char *args, int from_tty)
14666 {
14667   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14668 }
14669
14670 static void
14671 show_dwarf2_cmd (char *args, int from_tty)
14672 {
14673   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14674 }
14675
14676 /* If section described by INFO was mmapped, munmap it now.  */
14677
14678 static void
14679 munmap_section_buffer (struct dwarf2_section_info *info)
14680 {
14681   if (info->was_mmapped)
14682     {
14683 #ifdef HAVE_MMAP
14684       intptr_t begin = (intptr_t) info->buffer;
14685       intptr_t map_begin = begin & ~(pagesize - 1);
14686       size_t map_length = info->size + begin - map_begin;
14687
14688       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14689 #else
14690       /* Without HAVE_MMAP, we should never be here to begin with.  */
14691       gdb_assert_not_reached ("no mmap support");
14692 #endif
14693     }
14694 }
14695
14696 /* munmap debug sections for OBJFILE, if necessary.  */
14697
14698 static void
14699 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14700 {
14701   struct dwarf2_per_objfile *data = d;
14702
14703   /* This is sorted according to the order they're defined in to make it easier
14704      to keep in sync.  */
14705   munmap_section_buffer (&data->info);
14706   munmap_section_buffer (&data->abbrev);
14707   munmap_section_buffer (&data->line);
14708   munmap_section_buffer (&data->loc);
14709   munmap_section_buffer (&data->macinfo);
14710   munmap_section_buffer (&data->str);
14711   munmap_section_buffer (&data->ranges);
14712   munmap_section_buffer (&data->types);
14713   munmap_section_buffer (&data->frame);
14714   munmap_section_buffer (&data->eh_frame);
14715   munmap_section_buffer (&data->gdb_index);
14716 }
14717
14718 \f
14719
14720 /* The contents of the hash table we create when building the string
14721    table.  */
14722 struct strtab_entry
14723 {
14724   offset_type offset;
14725   const char *str;
14726 };
14727
14728 /* Hash function for a strtab_entry.  */
14729 static hashval_t
14730 hash_strtab_entry (const void *e)
14731 {
14732   const struct strtab_entry *entry = e;
14733   return mapped_index_string_hash (entry->str);
14734 }
14735
14736 /* Equality function for a strtab_entry.  */
14737 static int
14738 eq_strtab_entry (const void *a, const void *b)
14739 {
14740   const struct strtab_entry *ea = a;
14741   const struct strtab_entry *eb = b;
14742   return !strcmp (ea->str, eb->str);
14743 }
14744
14745 /* Create a strtab_entry hash table.  */
14746 static htab_t
14747 create_strtab (void)
14748 {
14749   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14750                             xfree, xcalloc, xfree);
14751 }
14752
14753 /* Add a string to the constant pool.  Return the string's offset in
14754    host order.  */
14755 static offset_type
14756 add_string (htab_t table, struct obstack *cpool, const char *str)
14757 {
14758   void **slot;
14759   struct strtab_entry entry;
14760   struct strtab_entry *result;
14761
14762   entry.str = str;
14763   slot = htab_find_slot (table, &entry, INSERT);
14764   if (*slot)
14765     result = *slot;
14766   else
14767     {
14768       result = XNEW (struct strtab_entry);
14769       result->offset = obstack_object_size (cpool);
14770       result->str = str;
14771       obstack_grow_str0 (cpool, str);
14772       *slot = result;
14773     }
14774   return result->offset;
14775 }
14776
14777 /* An entry in the symbol table.  */
14778 struct symtab_index_entry
14779 {
14780   /* The name of the symbol.  */
14781   const char *name;
14782   /* The offset of the name in the constant pool.  */
14783   offset_type index_offset;
14784   /* A sorted vector of the indices of all the CUs that hold an object
14785      of this name.  */
14786   VEC (offset_type) *cu_indices;
14787 };
14788
14789 /* The symbol table.  This is a power-of-2-sized hash table.  */
14790 struct mapped_symtab
14791 {
14792   offset_type n_elements;
14793   offset_type size;
14794   struct symtab_index_entry **data;
14795 };
14796
14797 /* Hash function for a symtab_index_entry.  */
14798 static hashval_t
14799 hash_symtab_entry (const void *e)
14800 {
14801   const struct symtab_index_entry *entry = e;
14802   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14803                          sizeof (offset_type) * VEC_length (offset_type,
14804                                                             entry->cu_indices),
14805                          0);
14806 }
14807
14808 /* Equality function for a symtab_index_entry.  */
14809 static int
14810 eq_symtab_entry (const void *a, const void *b)
14811 {
14812   const struct symtab_index_entry *ea = a;
14813   const struct symtab_index_entry *eb = b;
14814   int len = VEC_length (offset_type, ea->cu_indices);
14815   if (len != VEC_length (offset_type, eb->cu_indices))
14816     return 0;
14817   return !memcmp (VEC_address (offset_type, ea->cu_indices),
14818                   VEC_address (offset_type, eb->cu_indices),
14819                   sizeof (offset_type) * len);
14820 }
14821
14822 /* Destroy a symtab_index_entry.  */
14823 static void
14824 delete_symtab_entry (void *p)
14825 {
14826   struct symtab_index_entry *entry = p;
14827   VEC_free (offset_type, entry->cu_indices);
14828   xfree (entry);
14829 }
14830
14831 /* Create a hash table holding symtab_index_entry objects.  */
14832 static htab_t
14833 create_index_table (void)
14834 {
14835   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14836                             delete_symtab_entry, xcalloc, xfree);
14837 }
14838
14839 /* Create a new mapped symtab object.  */
14840 static struct mapped_symtab *
14841 create_mapped_symtab (void)
14842 {
14843   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14844   symtab->n_elements = 0;
14845   symtab->size = 1024;
14846   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14847   return symtab;
14848 }
14849
14850 /* Destroy a mapped_symtab.  */
14851 static void
14852 cleanup_mapped_symtab (void *p)
14853 {
14854   struct mapped_symtab *symtab = p;
14855   /* The contents of the array are freed when the other hash table is
14856      destroyed.  */
14857   xfree (symtab->data);
14858   xfree (symtab);
14859 }
14860
14861 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
14862    the slot.  */
14863 static struct symtab_index_entry **
14864 find_slot (struct mapped_symtab *symtab, const char *name)
14865 {
14866   offset_type index, step, hash = mapped_index_string_hash (name);
14867
14868   index = hash & (symtab->size - 1);
14869   step = ((hash * 17) & (symtab->size - 1)) | 1;
14870
14871   for (;;)
14872     {
14873       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14874         return &symtab->data[index];
14875       index = (index + step) & (symtab->size - 1);
14876     }
14877 }
14878
14879 /* Expand SYMTAB's hash table.  */
14880 static void
14881 hash_expand (struct mapped_symtab *symtab)
14882 {
14883   offset_type old_size = symtab->size;
14884   offset_type i;
14885   struct symtab_index_entry **old_entries = symtab->data;
14886
14887   symtab->size *= 2;
14888   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14889
14890   for (i = 0; i < old_size; ++i)
14891     {
14892       if (old_entries[i])
14893         {
14894           struct symtab_index_entry **slot = find_slot (symtab,
14895                                                         old_entries[i]->name);
14896           *slot = old_entries[i];
14897         }
14898     }
14899
14900   xfree (old_entries);
14901 }
14902
14903 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
14904    is the index of the CU in which the symbol appears.  */
14905 static void
14906 add_index_entry (struct mapped_symtab *symtab, const char *name,
14907                  offset_type cu_index)
14908 {
14909   struct symtab_index_entry **slot;
14910
14911   ++symtab->n_elements;
14912   if (4 * symtab->n_elements / 3 >= symtab->size)
14913     hash_expand (symtab);
14914
14915   slot = find_slot (symtab, name);
14916   if (!*slot)
14917     {
14918       *slot = XNEW (struct symtab_index_entry);
14919       (*slot)->name = name;
14920       (*slot)->cu_indices = NULL;
14921     }
14922   /* Don't push an index twice.  Due to how we add entries we only
14923      have to check the last one.  */ 
14924   if (VEC_empty (offset_type, (*slot)->cu_indices)
14925       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14926     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14927 }
14928
14929 /* Add a vector of indices to the constant pool.  */
14930 static offset_type
14931 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14932                       struct symtab_index_entry *entry)
14933 {
14934   void **slot;
14935
14936   slot = htab_find_slot (index_table, entry, INSERT);
14937   if (!*slot)
14938     {
14939       offset_type len = VEC_length (offset_type, entry->cu_indices);
14940       offset_type val = MAYBE_SWAP (len);
14941       offset_type iter;
14942       int i;
14943
14944       *slot = entry;
14945       entry->index_offset = obstack_object_size (cpool);
14946
14947       obstack_grow (cpool, &val, sizeof (val));
14948       for (i = 0;
14949            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14950            ++i)
14951         {
14952           val = MAYBE_SWAP (iter);
14953           obstack_grow (cpool, &val, sizeof (val));
14954         }
14955     }
14956   else
14957     {
14958       struct symtab_index_entry *old_entry = *slot;
14959       entry->index_offset = old_entry->index_offset;
14960       entry = old_entry;
14961     }
14962   return entry->index_offset;
14963 }
14964
14965 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14966    constant pool entries going into the obstack CPOOL.  */
14967 static void
14968 write_hash_table (struct mapped_symtab *symtab,
14969                   struct obstack *output, struct obstack *cpool)
14970 {
14971   offset_type i;
14972   htab_t index_table;
14973   htab_t str_table;
14974
14975   index_table = create_index_table ();
14976   str_table = create_strtab ();
14977   /* We add all the index vectors to the constant pool first, to
14978      ensure alignment is ok.  */
14979   for (i = 0; i < symtab->size; ++i)
14980     {
14981       if (symtab->data[i])
14982         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14983     }
14984
14985   /* Now write out the hash table.  */
14986   for (i = 0; i < symtab->size; ++i)
14987     {
14988       offset_type str_off, vec_off;
14989
14990       if (symtab->data[i])
14991         {
14992           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14993           vec_off = symtab->data[i]->index_offset;
14994         }
14995       else
14996         {
14997           /* While 0 is a valid constant pool index, it is not valid
14998              to have 0 for both offsets.  */
14999           str_off = 0;
15000           vec_off = 0;
15001         }
15002
15003       str_off = MAYBE_SWAP (str_off);
15004       vec_off = MAYBE_SWAP (vec_off);
15005
15006       obstack_grow (output, &str_off, sizeof (str_off));
15007       obstack_grow (output, &vec_off, sizeof (vec_off));
15008     }
15009
15010   htab_delete (str_table);
15011   htab_delete (index_table);
15012 }
15013
15014 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
15015    from PST; CU_INDEX is the index of the CU in the vector of all
15016    CUs.  */
15017 static void
15018 add_address_entry (struct objfile *objfile,
15019                    struct obstack *addr_obstack, struct partial_symtab *pst,
15020                    unsigned int cu_index)
15021 {
15022   offset_type offset;
15023   char addr[8];
15024   CORE_ADDR baseaddr;
15025
15026   /* Don't bother recording empty ranges.  */
15027   if (pst->textlow == pst->texthigh)
15028     return;
15029
15030   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15031
15032   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15033   obstack_grow (addr_obstack, addr, 8);
15034   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15035   obstack_grow (addr_obstack, addr, 8);
15036   offset = MAYBE_SWAP (cu_index);
15037   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15038 }
15039
15040 /* Add a list of partial symbols to SYMTAB.  */
15041 static void
15042 write_psymbols (struct mapped_symtab *symtab,
15043                 htab_t psyms_seen,
15044                 struct partial_symbol **psymp,
15045                 int count,
15046                 offset_type cu_index,
15047                 int is_static)
15048 {
15049   for (; count-- > 0; ++psymp)
15050     {
15051       void **slot, *lookup;
15052
15053       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15054         error (_("Ada is not currently supported by the index"));
15055
15056       /* We only want to add a given psymbol once.  However, we also
15057          want to account for whether it is global or static.  So, we
15058          may add it twice, using slightly different values.  */
15059       if (is_static)
15060         {
15061           uintptr_t val = 1 | (uintptr_t) *psymp;
15062
15063           lookup = (void *) val;
15064         }
15065       else
15066         lookup = *psymp;
15067
15068       /* Only add a given psymbol once.  */
15069       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15070       if (!*slot)
15071         {
15072           *slot = lookup;
15073           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15074         }
15075     }
15076 }
15077
15078 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15079    exception if there is an error.  */
15080 static void
15081 write_obstack (FILE *file, struct obstack *obstack)
15082 {
15083   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15084               file)
15085       != obstack_object_size (obstack))
15086     error (_("couldn't data write to file"));
15087 }
15088
15089 /* Unlink a file if the argument is not NULL.  */
15090 static void
15091 unlink_if_set (void *p)
15092 {
15093   char **filename = p;
15094   if (*filename)
15095     unlink (*filename);
15096 }
15097
15098 /* A helper struct used when iterating over debug_types.  */
15099 struct signatured_type_index_data
15100 {
15101   struct objfile *objfile;
15102   struct mapped_symtab *symtab;
15103   struct obstack *types_list;
15104   htab_t psyms_seen;
15105   int cu_index;
15106 };
15107
15108 /* A helper function that writes a single signatured_type to an
15109    obstack.  */
15110 static int
15111 write_one_signatured_type (void **slot, void *d)
15112 {
15113   struct signatured_type_index_data *info = d;
15114   struct signatured_type *entry = (struct signatured_type *) *slot;
15115   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15116   struct partial_symtab *psymtab = per_cu->v.psymtab;
15117   gdb_byte val[8];
15118
15119   write_psymbols (info->symtab,
15120                   info->psyms_seen,
15121                   info->objfile->global_psymbols.list + psymtab->globals_offset,
15122                   psymtab->n_global_syms, info->cu_index,
15123                   0);
15124   write_psymbols (info->symtab,
15125                   info->psyms_seen,
15126                   info->objfile->static_psymbols.list + psymtab->statics_offset,
15127                   psymtab->n_static_syms, info->cu_index,
15128                   1);
15129
15130   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15131   obstack_grow (info->types_list, val, 8);
15132   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15133   obstack_grow (info->types_list, val, 8);
15134   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15135   obstack_grow (info->types_list, val, 8);
15136
15137   ++info->cu_index;
15138
15139   return 1;
15140 }
15141
15142 /* A cleanup function for an htab_t.  */
15143
15144 static void
15145 cleanup_htab (void *arg)
15146 {
15147   htab_delete (arg);
15148 }
15149
15150 /* Create an index file for OBJFILE in the directory DIR.  */
15151 static void
15152 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15153 {
15154   struct cleanup *cleanup;
15155   char *filename, *cleanup_filename;
15156   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15157   struct obstack cu_list, types_cu_list;
15158   int i;
15159   FILE *out_file;
15160   struct mapped_symtab *symtab;
15161   offset_type val, size_of_contents, total_len;
15162   struct stat st;
15163   char buf[8];
15164   htab_t psyms_seen;
15165
15166   if (!objfile->psymtabs)
15167     return;
15168   if (dwarf2_per_objfile->using_index)
15169     error (_("Cannot use an index to create the index"));
15170
15171   if (stat (objfile->name, &st) < 0)
15172     perror_with_name (_("Could not stat"));
15173
15174   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15175                      INDEX_SUFFIX, (char *) NULL);
15176   cleanup = make_cleanup (xfree, filename);
15177
15178   out_file = fopen (filename, "wb");
15179   if (!out_file)
15180     error (_("Can't open `%s' for writing"), filename);
15181
15182   cleanup_filename = filename;
15183   make_cleanup (unlink_if_set, &cleanup_filename);
15184
15185   symtab = create_mapped_symtab ();
15186   make_cleanup (cleanup_mapped_symtab, symtab);
15187
15188   obstack_init (&addr_obstack);
15189   make_cleanup_obstack_free (&addr_obstack);
15190
15191   obstack_init (&cu_list);
15192   make_cleanup_obstack_free (&cu_list);
15193
15194   obstack_init (&types_cu_list);
15195   make_cleanup_obstack_free (&types_cu_list);
15196
15197   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15198                                   NULL, xcalloc, xfree);
15199   make_cleanup (cleanup_htab, psyms_seen);
15200
15201   /* The list is already sorted, so we don't need to do additional
15202      work here.  Also, the debug_types entries do not appear in
15203      all_comp_units, but only in their own hash table.  */
15204   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15205     {
15206       struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15207       struct partial_symtab *psymtab = per_cu->v.psymtab;
15208       gdb_byte val[8];
15209
15210       write_psymbols (symtab,
15211                       psyms_seen,
15212                       objfile->global_psymbols.list + psymtab->globals_offset,
15213                       psymtab->n_global_syms, i,
15214                       0);
15215       write_psymbols (symtab,
15216                       psyms_seen,
15217                       objfile->static_psymbols.list + psymtab->statics_offset,
15218                       psymtab->n_static_syms, i,
15219                       1);
15220
15221       add_address_entry (objfile, &addr_obstack, psymtab, i);
15222
15223       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15224       obstack_grow (&cu_list, val, 8);
15225       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15226       obstack_grow (&cu_list, val, 8);
15227     }
15228
15229   /* Write out the .debug_type entries, if any.  */
15230   if (dwarf2_per_objfile->signatured_types)
15231     {
15232       struct signatured_type_index_data sig_data;
15233
15234       sig_data.objfile = objfile;
15235       sig_data.symtab = symtab;
15236       sig_data.types_list = &types_cu_list;
15237       sig_data.psyms_seen = psyms_seen;
15238       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15239       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15240                               write_one_signatured_type, &sig_data);
15241     }
15242
15243   obstack_init (&constant_pool);
15244   make_cleanup_obstack_free (&constant_pool);
15245   obstack_init (&symtab_obstack);
15246   make_cleanup_obstack_free (&symtab_obstack);
15247   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15248
15249   obstack_init (&contents);
15250   make_cleanup_obstack_free (&contents);
15251   size_of_contents = 6 * sizeof (offset_type);
15252   total_len = size_of_contents;
15253
15254   /* The version number.  */
15255   val = MAYBE_SWAP (3);
15256   obstack_grow (&contents, &val, sizeof (val));
15257
15258   /* The offset of the CU list from the start of the file.  */
15259   val = MAYBE_SWAP (total_len);
15260   obstack_grow (&contents, &val, sizeof (val));
15261   total_len += obstack_object_size (&cu_list);
15262
15263   /* The offset of the types CU list from the start of the file.  */
15264   val = MAYBE_SWAP (total_len);
15265   obstack_grow (&contents, &val, sizeof (val));
15266   total_len += obstack_object_size (&types_cu_list);
15267
15268   /* The offset of the address table from the start of the file.  */
15269   val = MAYBE_SWAP (total_len);
15270   obstack_grow (&contents, &val, sizeof (val));
15271   total_len += obstack_object_size (&addr_obstack);
15272
15273   /* The offset of the symbol table from the start of the file.  */
15274   val = MAYBE_SWAP (total_len);
15275   obstack_grow (&contents, &val, sizeof (val));
15276   total_len += obstack_object_size (&symtab_obstack);
15277
15278   /* The offset of the constant pool from the start of the file.  */
15279   val = MAYBE_SWAP (total_len);
15280   obstack_grow (&contents, &val, sizeof (val));
15281   total_len += obstack_object_size (&constant_pool);
15282
15283   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15284
15285   write_obstack (out_file, &contents);
15286   write_obstack (out_file, &cu_list);
15287   write_obstack (out_file, &types_cu_list);
15288   write_obstack (out_file, &addr_obstack);
15289   write_obstack (out_file, &symtab_obstack);
15290   write_obstack (out_file, &constant_pool);
15291
15292   fclose (out_file);
15293
15294   /* We want to keep the file, so we set cleanup_filename to NULL
15295      here.  See unlink_if_set.  */
15296   cleanup_filename = NULL;
15297
15298   do_cleanups (cleanup);
15299 }
15300
15301 /* The mapped index file format is designed to be directly mmap()able
15302    on any architecture.  In most cases, a datum is represented using a
15303    little-endian 32-bit integer value, called an offset_type.  Big
15304    endian machines must byte-swap the values before using them.
15305    Exceptions to this rule are noted.  The data is laid out such that
15306    alignment is always respected.
15307
15308    A mapped index consists of several sections.
15309
15310    1. The file header.  This is a sequence of values, of offset_type
15311    unless otherwise noted:
15312
15313    [0] The version number, currently 3.  Versions 1 and 2 are
15314    obsolete.
15315    [1] The offset, from the start of the file, of the CU list.
15316    [2] The offset, from the start of the file, of the types CU list.
15317    Note that this section can be empty, in which case this offset will
15318    be equal to the next offset.
15319    [3] The offset, from the start of the file, of the address section.
15320    [4] The offset, from the start of the file, of the symbol table.
15321    [5] The offset, from the start of the file, of the constant pool.
15322
15323    2. The CU list.  This is a sequence of pairs of 64-bit
15324    little-endian values, sorted by the CU offset.  The first element
15325    in each pair is the offset of a CU in the .debug_info section.  The
15326    second element in each pair is the length of that CU.  References
15327    to a CU elsewhere in the map are done using a CU index, which is
15328    just the 0-based index into this table.  Note that if there are
15329    type CUs, then conceptually CUs and type CUs form a single list for
15330    the purposes of CU indices.
15331
15332    3. The types CU list.  This is a sequence of triplets of 64-bit
15333    little-endian values.  In a triplet, the first value is the CU
15334    offset, the second value is the type offset in the CU, and the
15335    third value is the type signature.  The types CU list is not
15336    sorted.
15337
15338    4. The address section.  The address section consists of a sequence
15339    of address entries.  Each address entry has three elements.
15340    [0] The low address.  This is a 64-bit little-endian value.
15341    [1] The high address.  This is a 64-bit little-endian value.
15342    [2] The CU index.  This is an offset_type value.
15343
15344    5. The symbol table.  This is a hash table.  The size of the hash
15345    table is always a power of 2.  The initial hash and the step are
15346    currently defined by the `find_slot' function.
15347
15348    Each slot in the hash table consists of a pair of offset_type
15349    values.  The first value is the offset of the symbol's name in the
15350    constant pool.  The second value is the offset of the CU vector in
15351    the constant pool.
15352
15353    If both values are 0, then this slot in the hash table is empty.
15354    This is ok because while 0 is a valid constant pool index, it
15355    cannot be a valid index for both a string and a CU vector.
15356
15357    A string in the constant pool is stored as a \0-terminated string,
15358    as you'd expect.
15359
15360    A CU vector in the constant pool is a sequence of offset_type
15361    values.  The first value is the number of CU indices in the vector.
15362    Each subsequent value is the index of a CU in the CU list.  This
15363    element in the hash table is used to indicate which CUs define the
15364    symbol.
15365
15366    6. The constant pool.  This is simply a bunch of bytes.  It is
15367    organized so that alignment is correct: CU vectors are stored
15368    first, followed by strings.  */
15369 static void
15370 save_gdb_index_command (char *arg, int from_tty)
15371 {
15372   struct objfile *objfile;
15373
15374   if (!arg || !*arg)
15375     error (_("usage: save gdb-index DIRECTORY"));
15376
15377   ALL_OBJFILES (objfile)
15378   {
15379     struct stat st;
15380
15381     /* If the objfile does not correspond to an actual file, skip it.  */
15382     if (stat (objfile->name, &st) < 0)
15383       continue;
15384
15385     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15386     if (dwarf2_per_objfile)
15387       {
15388         volatile struct gdb_exception except;
15389
15390         TRY_CATCH (except, RETURN_MASK_ERROR)
15391           {
15392             write_psymtabs_to_index (objfile, arg);
15393           }
15394         if (except.reason < 0)
15395           exception_fprintf (gdb_stderr, except,
15396                              _("Error while writing index for `%s': "),
15397                              objfile->name);
15398       }
15399   }
15400 }
15401
15402 \f
15403
15404 int dwarf2_always_disassemble;
15405
15406 static void
15407 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15408                                 struct cmd_list_element *c, const char *value)
15409 {
15410   fprintf_filtered (file, _("\
15411 Whether to always disassemble DWARF expressions is %s.\n"),
15412                     value);
15413 }
15414
15415 void _initialize_dwarf2_read (void);
15416
15417 void
15418 _initialize_dwarf2_read (void)
15419 {
15420   struct cmd_list_element *c;
15421
15422   dwarf2_objfile_data_key
15423     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15424
15425   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15426 Set DWARF 2 specific variables.\n\
15427 Configure DWARF 2 variables such as the cache size"),
15428                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15429                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15430
15431   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15432 Show DWARF 2 specific variables\n\
15433 Show DWARF 2 variables such as the cache size"),
15434                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15435                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15436
15437   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15438                             &dwarf2_max_cache_age, _("\
15439 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15440 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15441 A higher limit means that cached compilation units will be stored\n\
15442 in memory longer, and more total memory will be used.  Zero disables\n\
15443 caching, which can slow down startup."),
15444                             NULL,
15445                             show_dwarf2_max_cache_age,
15446                             &set_dwarf2_cmdlist,
15447                             &show_dwarf2_cmdlist);
15448
15449   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15450                            &dwarf2_always_disassemble, _("\
15451 Set whether `info address' always disassembles DWARF expressions."), _("\
15452 Show whether `info address' always disassembles DWARF expressions."), _("\
15453 When enabled, DWARF expressions are always printed in an assembly-like\n\
15454 syntax.  When disabled, expressions will be printed in a more\n\
15455 conversational style, when possible."),
15456                            NULL,
15457                            show_dwarf2_always_disassemble,
15458                            &set_dwarf2_cmdlist,
15459                            &show_dwarf2_cmdlist);
15460
15461   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15462 Set debugging of the dwarf2 DIE reader."), _("\
15463 Show debugging of the dwarf2 DIE reader."), _("\
15464 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15465 The value is the maximum depth to print."),
15466                             NULL,
15467                             NULL,
15468                             &setdebuglist, &showdebuglist);
15469
15470   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15471                _("Save a .gdb-index file"),
15472                &save_cmdlist);
15473   set_cmd_completer (c, filename_completer);
15474 }