eb5d9245bdb5eb7c9dc78252f05c60ce146925d1
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81    Because of alignment constraints, this structure has padding and cannot
82    be mapped directly onto the beginning of the .debug_info section.  */
83 typedef struct comp_unit_header
84   {
85     unsigned int length;        /* length of the .debug_info
86                                    contribution */
87     unsigned short version;     /* version number -- 2 for DWARF
88                                    version 2 */
89     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90     unsigned char addr_size;    /* byte size of an address -- 4 */
91   }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97    Because of alignment constraints, this structure has padding and cannot
98    be mapped directly onto the beginning of the .debug_info section.  */
99 typedef struct statement_prologue
100   {
101     unsigned int total_length;  /* byte length of the statement
102                                    information */
103     unsigned short version;     /* version number -- 2 for DWARF
104                                    version 2 */
105     unsigned int prologue_length;       /* # bytes between prologue &
106                                            stmt program */
107     unsigned char minimum_instruction_length;   /* byte size of
108                                                    smallest instr */
109     unsigned char default_is_stmt;      /* initial value of is_stmt
110                                            register */
111     char line_base;
112     unsigned char line_range;
113     unsigned char opcode_base;  /* number assigned to first special
114                                    opcode */
115     unsigned char *standard_opcode_lengths;
116   }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in.  */
120 static int dwarf2_die_debug = 0;
121
122 static int pagesize;
123
124 /* When set, the file that we're processing is known to have debugging
125    info for C++ namespaces.  GCC 3.3.x did not produce this information,
126    but later versions do.  */
127
128 static int processing_has_namespace_info;
129
130 static const struct objfile_data *dwarf2_objfile_data_key;
131
132 struct dwarf2_section_info
133 {
134   asection *asection;
135   gdb_byte *buffer;
136   bfd_size_type size;
137   int was_mmapped;
138   /* True if we have tried to read this section.  */
139   int readin;
140 };
141
142 /* All offsets in the index are of this type.  It must be
143    architecture-independent.  */
144 typedef uint32_t offset_type;
145
146 DEF_VEC_I (offset_type);
147
148 /* A description of the mapped index.  The file format is described in
149    a comment by the code that writes the index.  */
150 struct mapped_index
151 {
152   /* Index data format version.  */
153   int version;
154
155   /* The total length of the buffer.  */
156   off_t total_size;
157
158   /* A pointer to the address table data.  */
159   const gdb_byte *address_table;
160
161   /* Size of the address table data in bytes.  */
162   offset_type address_table_size;
163
164   /* The symbol table, implemented as a hash table.  */
165   const offset_type *symbol_table;
166
167   /* Size in slots, each slot is 2 offset_types.  */
168   offset_type symbol_table_slots;
169
170   /* A pointer to the constant pool.  */
171   const char *constant_pool;
172 };
173
174 struct dwarf2_per_objfile
175 {
176   struct dwarf2_section_info info;
177   struct dwarf2_section_info abbrev;
178   struct dwarf2_section_info line;
179   struct dwarf2_section_info loc;
180   struct dwarf2_section_info macinfo;
181   struct dwarf2_section_info str;
182   struct dwarf2_section_info ranges;
183   struct dwarf2_section_info types;
184   struct dwarf2_section_info frame;
185   struct dwarf2_section_info eh_frame;
186   struct dwarf2_section_info gdb_index;
187
188   /* Back link.  */
189   struct objfile *objfile;
190
191   /* A list of all the compilation units.  This is used to locate
192      the target compilation unit of a particular reference.  */
193   struct dwarf2_per_cu_data **all_comp_units;
194
195   /* The number of compilation units in ALL_COMP_UNITS.  */
196   int n_comp_units;
197
198   /* The number of .debug_types-related CUs.  */
199   int n_type_comp_units;
200
201   /* The .debug_types-related CUs.  */
202   struct dwarf2_per_cu_data **type_comp_units;
203
204   /* A chain of compilation units that are currently read in, so that
205      they can be freed later.  */
206   struct dwarf2_per_cu_data *read_in_chain;
207
208   /* A table mapping .debug_types signatures to its signatured_type entry.
209      This is NULL if the .debug_types section hasn't been read in yet.  */
210   htab_t signatured_types;
211
212   /* A flag indicating wether this objfile has a section loaded at a
213      VMA of 0.  */
214   int has_section_at_zero;
215
216   /* True if we are using the mapped index,
217      or we are faking it for OBJF_READNOW's sake.  */
218   unsigned char using_index;
219
220   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
221   struct mapped_index *index_table;
222
223   /* When using index_table, this keeps track of all quick_file_names entries.
224      TUs can share line table entries with CUs or other TUs, and there can be
225      a lot more TUs than unique line tables, so we maintain a separate table
226      of all line table entries to support the sharing.  */
227   htab_t quick_file_names_table;
228
229   /* Set during partial symbol reading, to prevent queueing of full
230      symbols.  */
231   int reading_partial_symbols;
232
233   /* Table mapping type .debug_info DIE offsets to types.
234      This is NULL if not allocated yet.
235      It (currently) makes sense to allocate debug_types_type_hash lazily.
236      To keep things simple we allocate both lazily.  */
237   htab_t debug_info_type_hash;
238
239   /* Table mapping type .debug_types DIE offsets to types.
240      This is NULL if not allocated yet.  */
241   htab_t debug_types_type_hash;
242 };
243
244 static struct dwarf2_per_objfile *dwarf2_per_objfile;
245
246 /* names of the debugging sections */
247
248 /* Note that if the debugging section has been compressed, it might
249    have a name like .zdebug_info.  */
250
251 #define INFO_SECTION     "debug_info"
252 #define ABBREV_SECTION   "debug_abbrev"
253 #define LINE_SECTION     "debug_line"
254 #define LOC_SECTION      "debug_loc"
255 #define MACINFO_SECTION  "debug_macinfo"
256 #define STR_SECTION      "debug_str"
257 #define RANGES_SECTION   "debug_ranges"
258 #define TYPES_SECTION    "debug_types"
259 #define FRAME_SECTION    "debug_frame"
260 #define EH_FRAME_SECTION "eh_frame"
261 #define GDB_INDEX_SECTION "gdb_index"
262
263 /* local data types */
264
265 /* We hold several abbreviation tables in memory at the same time.  */
266 #ifndef ABBREV_HASH_SIZE
267 #define ABBREV_HASH_SIZE 121
268 #endif
269
270 /* The data in a compilation unit header, after target2host
271    translation, looks like this.  */
272 struct comp_unit_head
273 {
274   unsigned int length;
275   short version;
276   unsigned char addr_size;
277   unsigned char signed_addr_p;
278   unsigned int abbrev_offset;
279
280   /* Size of file offsets; either 4 or 8.  */
281   unsigned int offset_size;
282
283   /* Size of the length field; either 4 or 12.  */
284   unsigned int initial_length_size;
285
286   /* Offset to the first byte of this compilation unit header in the
287      .debug_info section, for resolving relative reference dies.  */
288   unsigned int offset;
289
290   /* Offset to first die in this cu from the start of the cu.
291      This will be the first byte following the compilation unit header.  */
292   unsigned int first_die_offset;
293 };
294
295 /* Type used for delaying computation of method physnames.
296    See comments for compute_delayed_physnames.  */
297 struct delayed_method_info
298 {
299   /* The type to which the method is attached, i.e., its parent class.  */
300   struct type *type;
301
302   /* The index of the method in the type's function fieldlists.  */
303   int fnfield_index;
304
305   /* The index of the method in the fieldlist.  */
306   int index;
307
308   /* The name of the DIE.  */
309   const char *name;
310
311   /*  The DIE associated with this method.  */
312   struct die_info *die;
313 };
314
315 typedef struct delayed_method_info delayed_method_info;
316 DEF_VEC_O (delayed_method_info);
317
318 /* Internal state when decoding a particular compilation unit.  */
319 struct dwarf2_cu
320 {
321   /* The objfile containing this compilation unit.  */
322   struct objfile *objfile;
323
324   /* The header of the compilation unit.  */
325   struct comp_unit_head header;
326
327   /* Base address of this compilation unit.  */
328   CORE_ADDR base_address;
329
330   /* Non-zero if base_address has been set.  */
331   int base_known;
332
333   struct function_range *first_fn, *last_fn, *cached_fn;
334
335   /* The language we are debugging.  */
336   enum language language;
337   const struct language_defn *language_defn;
338
339   const char *producer;
340
341   /* The generic symbol table building routines have separate lists for
342      file scope symbols and all all other scopes (local scopes).  So
343      we need to select the right one to pass to add_symbol_to_list().
344      We do it by keeping a pointer to the correct list in list_in_scope.
345
346      FIXME: The original dwarf code just treated the file scope as the
347      first local scope, and all other local scopes as nested local
348      scopes, and worked fine.  Check to see if we really need to
349      distinguish these in buildsym.c.  */
350   struct pending **list_in_scope;
351
352   /* DWARF abbreviation table associated with this compilation unit.  */
353   struct abbrev_info **dwarf2_abbrevs;
354
355   /* Storage for the abbrev table.  */
356   struct obstack abbrev_obstack;
357
358   /* Hash table holding all the loaded partial DIEs.  */
359   htab_t partial_dies;
360
361   /* Storage for things with the same lifetime as this read-in compilation
362      unit, including partial DIEs.  */
363   struct obstack comp_unit_obstack;
364
365   /* When multiple dwarf2_cu structures are living in memory, this field
366      chains them all together, so that they can be released efficiently.
367      We will probably also want a generation counter so that most-recently-used
368      compilation units are cached...  */
369   struct dwarf2_per_cu_data *read_in_chain;
370
371   /* Backchain to our per_cu entry if the tree has been built.  */
372   struct dwarf2_per_cu_data *per_cu;
373
374   /* How many compilation units ago was this CU last referenced?  */
375   int last_used;
376
377   /* A hash table of die offsets for following references.  */
378   htab_t die_hash;
379
380   /* Full DIEs if read in.  */
381   struct die_info *dies;
382
383   /* A set of pointers to dwarf2_per_cu_data objects for compilation
384      units referenced by this one.  Only set during full symbol processing;
385      partial symbol tables do not have dependencies.  */
386   htab_t dependencies;
387
388   /* Header data from the line table, during full symbol processing.  */
389   struct line_header *line_header;
390
391   /* A list of methods which need to have physnames computed
392      after all type information has been read.  */
393   VEC (delayed_method_info) *method_list;
394
395   /* Mark used when releasing cached dies.  */
396   unsigned int mark : 1;
397
398   /* This flag will be set if this compilation unit might include
399      inter-compilation-unit references.  */
400   unsigned int has_form_ref_addr : 1;
401
402   /* This flag will be set if this compilation unit includes any
403      DW_TAG_namespace DIEs.  If we know that there are explicit
404      DIEs for namespaces, we don't need to try to infer them
405      from mangled names.  */
406   unsigned int has_namespace_info : 1;
407 };
408
409 /* Persistent data held for a compilation unit, even when not
410    processing it.  We put a pointer to this structure in the
411    read_symtab_private field of the psymtab.  If we encounter
412    inter-compilation-unit references, we also maintain a sorted
413    list of all compilation units.  */
414
415 struct dwarf2_per_cu_data
416 {
417   /* The start offset and length of this compilation unit.  2**29-1
418      bytes should suffice to store the length of any compilation unit
419      - if it doesn't, GDB will fall over anyway.
420      NOTE: Unlike comp_unit_head.length, this length includes
421      initial_length_size.  */
422   unsigned int offset;
423   unsigned int length : 29;
424
425   /* Flag indicating this compilation unit will be read in before
426      any of the current compilation units are processed.  */
427   unsigned int queued : 1;
428
429   /* This flag will be set if we need to load absolutely all DIEs
430      for this compilation unit, instead of just the ones we think
431      are interesting.  It gets set if we look for a DIE in the
432      hash table and don't find it.  */
433   unsigned int load_all_dies : 1;
434
435   /* Non-zero if this CU is from .debug_types.
436      Otherwise it's from .debug_info.  */
437   unsigned int from_debug_types : 1;
438
439   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
440      of the CU cache it gets reset to NULL again.  */
441   struct dwarf2_cu *cu;
442
443   /* The corresponding objfile.  */
444   struct objfile *objfile;
445
446   /* When using partial symbol tables, the 'psymtab' field is active.
447      Otherwise the 'quick' field is active.  */
448   union
449   {
450     /* The partial symbol table associated with this compilation unit,
451        or NULL for partial units (which do not have an associated
452        symtab).  */
453     struct partial_symtab *psymtab;
454
455     /* Data needed by the "quick" functions.  */
456     struct dwarf2_per_cu_quick_data *quick;
457   } v;
458 };
459
460 /* Entry in the signatured_types hash table.  */
461
462 struct signatured_type
463 {
464   ULONGEST signature;
465
466   /* Offset in .debug_types of the TU (type_unit) for this type.  */
467   unsigned int offset;
468
469   /* Offset in .debug_types of the type defined by this TU.  */
470   unsigned int type_offset;
471
472   /* The CU(/TU) of this type.  */
473   struct dwarf2_per_cu_data per_cu;
474 };
475
476 /* Struct used to pass misc. parameters to read_die_and_children, et
477    al.  which are used for both .debug_info and .debug_types dies.
478    All parameters here are unchanging for the life of the call.  This
479    struct exists to abstract away the constant parameters of die
480    reading.  */
481
482 struct die_reader_specs
483 {
484   /* The bfd of this objfile.  */
485   bfd* abfd;
486
487   /* The CU of the DIE we are parsing.  */
488   struct dwarf2_cu *cu;
489
490   /* Pointer to start of section buffer.
491      This is either the start of .debug_info or .debug_types.  */
492   const gdb_byte *buffer;
493 };
494
495 /* The line number information for a compilation unit (found in the
496    .debug_line section) begins with a "statement program header",
497    which contains the following information.  */
498 struct line_header
499 {
500   unsigned int total_length;
501   unsigned short version;
502   unsigned int header_length;
503   unsigned char minimum_instruction_length;
504   unsigned char maximum_ops_per_instruction;
505   unsigned char default_is_stmt;
506   int line_base;
507   unsigned char line_range;
508   unsigned char opcode_base;
509
510   /* standard_opcode_lengths[i] is the number of operands for the
511      standard opcode whose value is i.  This means that
512      standard_opcode_lengths[0] is unused, and the last meaningful
513      element is standard_opcode_lengths[opcode_base - 1].  */
514   unsigned char *standard_opcode_lengths;
515
516   /* The include_directories table.  NOTE!  These strings are not
517      allocated with xmalloc; instead, they are pointers into
518      debug_line_buffer.  If you try to free them, `free' will get
519      indigestion.  */
520   unsigned int num_include_dirs, include_dirs_size;
521   char **include_dirs;
522
523   /* The file_names table.  NOTE!  These strings are not allocated
524      with xmalloc; instead, they are pointers into debug_line_buffer.
525      Don't try to free them directly.  */
526   unsigned int num_file_names, file_names_size;
527   struct file_entry
528   {
529     char *name;
530     unsigned int dir_index;
531     unsigned int mod_time;
532     unsigned int length;
533     int included_p; /* Non-zero if referenced by the Line Number Program.  */
534     struct symtab *symtab; /* The associated symbol table, if any.  */
535   } *file_names;
536
537   /* The start and end of the statement program following this
538      header.  These point into dwarf2_per_objfile->line_buffer.  */
539   gdb_byte *statement_program_start, *statement_program_end;
540 };
541
542 /* When we construct a partial symbol table entry we only
543    need this much information.  */
544 struct partial_die_info
545   {
546     /* Offset of this DIE.  */
547     unsigned int offset;
548
549     /* DWARF-2 tag for this DIE.  */
550     ENUM_BITFIELD(dwarf_tag) tag : 16;
551
552     /* Assorted flags describing the data found in this DIE.  */
553     unsigned int has_children : 1;
554     unsigned int is_external : 1;
555     unsigned int is_declaration : 1;
556     unsigned int has_type : 1;
557     unsigned int has_specification : 1;
558     unsigned int has_pc_info : 1;
559
560     /* Flag set if the SCOPE field of this structure has been
561        computed.  */
562     unsigned int scope_set : 1;
563
564     /* Flag set if the DIE has a byte_size attribute.  */
565     unsigned int has_byte_size : 1;
566
567     /* Flag set if any of the DIE's children are template arguments.  */
568     unsigned int has_template_arguments : 1;
569
570     /* Flag set if fixup_partial_die has been called on this die.  */
571     unsigned int fixup_called : 1;
572
573     /* The name of this DIE.  Normally the value of DW_AT_name, but
574        sometimes a default name for unnamed DIEs.  */
575     char *name;
576
577     /* The linkage name, if present.  */
578     const char *linkage_name;
579
580     /* The scope to prepend to our children.  This is generally
581        allocated on the comp_unit_obstack, so will disappear
582        when this compilation unit leaves the cache.  */
583     char *scope;
584
585     /* The location description associated with this DIE, if any.  */
586     struct dwarf_block *locdesc;
587
588     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
589     CORE_ADDR lowpc;
590     CORE_ADDR highpc;
591
592     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
593        DW_AT_sibling, if any.  */
594     /* NOTE: This member isn't strictly necessary, read_partial_die could
595        return DW_AT_sibling values to its caller load_partial_dies.  */
596     gdb_byte *sibling;
597
598     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
599        DW_AT_specification (or DW_AT_abstract_origin or
600        DW_AT_extension).  */
601     unsigned int spec_offset;
602
603     /* Pointers to this DIE's parent, first child, and next sibling,
604        if any.  */
605     struct partial_die_info *die_parent, *die_child, *die_sibling;
606   };
607
608 /* This data structure holds the information of an abbrev.  */
609 struct abbrev_info
610   {
611     unsigned int number;        /* number identifying abbrev */
612     enum dwarf_tag tag;         /* dwarf tag */
613     unsigned short has_children;                /* boolean */
614     unsigned short num_attrs;   /* number of attributes */
615     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
616     struct abbrev_info *next;   /* next in chain */
617   };
618
619 struct attr_abbrev
620   {
621     ENUM_BITFIELD(dwarf_attribute) name : 16;
622     ENUM_BITFIELD(dwarf_form) form : 16;
623   };
624
625 /* Attributes have a name and a value.  */
626 struct attribute
627   {
628     ENUM_BITFIELD(dwarf_attribute) name : 16;
629     ENUM_BITFIELD(dwarf_form) form : 15;
630
631     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
632        field should be in u.str (existing only for DW_STRING) but it is kept
633        here for better struct attribute alignment.  */
634     unsigned int string_is_canonical : 1;
635
636     union
637       {
638         char *str;
639         struct dwarf_block *blk;
640         ULONGEST unsnd;
641         LONGEST snd;
642         CORE_ADDR addr;
643         struct signatured_type *signatured_type;
644       }
645     u;
646   };
647
648 /* This data structure holds a complete die structure.  */
649 struct die_info
650   {
651     /* DWARF-2 tag for this DIE.  */
652     ENUM_BITFIELD(dwarf_tag) tag : 16;
653
654     /* Number of attributes */
655     unsigned char num_attrs;
656
657     /* True if we're presently building the full type name for the
658        type derived from this DIE.  */
659     unsigned char building_fullname : 1;
660
661     /* Abbrev number */
662     unsigned int abbrev;
663
664     /* Offset in .debug_info or .debug_types section.  */
665     unsigned int offset;
666
667     /* The dies in a compilation unit form an n-ary tree.  PARENT
668        points to this die's parent; CHILD points to the first child of
669        this node; and all the children of a given node are chained
670        together via their SIBLING fields.  */
671     struct die_info *child;     /* Its first child, if any.  */
672     struct die_info *sibling;   /* Its next sibling, if any.  */
673     struct die_info *parent;    /* Its parent, if any.  */
674
675     /* An array of attributes, with NUM_ATTRS elements.  There may be
676        zero, but it's not common and zero-sized arrays are not
677        sufficiently portable C.  */
678     struct attribute attrs[1];
679   };
680
681 struct function_range
682 {
683   const char *name;
684   CORE_ADDR lowpc, highpc;
685   int seen_line;
686   struct function_range *next;
687 };
688
689 /* Get at parts of an attribute structure.  */
690
691 #define DW_STRING(attr)    ((attr)->u.str)
692 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
693 #define DW_UNSND(attr)     ((attr)->u.unsnd)
694 #define DW_BLOCK(attr)     ((attr)->u.blk)
695 #define DW_SND(attr)       ((attr)->u.snd)
696 #define DW_ADDR(attr)      ((attr)->u.addr)
697 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
698
699 /* Blocks are a bunch of untyped bytes.  */
700 struct dwarf_block
701   {
702     unsigned int size;
703     gdb_byte *data;
704   };
705
706 #ifndef ATTR_ALLOC_CHUNK
707 #define ATTR_ALLOC_CHUNK 4
708 #endif
709
710 /* Allocate fields for structs, unions and enums in this size.  */
711 #ifndef DW_FIELD_ALLOC_CHUNK
712 #define DW_FIELD_ALLOC_CHUNK 4
713 #endif
714
715 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
716    but this would require a corresponding change in unpack_field_as_long
717    and friends.  */
718 static int bits_per_byte = 8;
719
720 /* The routines that read and process dies for a C struct or C++ class
721    pass lists of data member fields and lists of member function fields
722    in an instance of a field_info structure, as defined below.  */
723 struct field_info
724   {
725     /* List of data member and baseclasses fields.  */
726     struct nextfield
727       {
728         struct nextfield *next;
729         int accessibility;
730         int virtuality;
731         struct field field;
732       }
733      *fields, *baseclasses;
734
735     /* Number of fields (including baseclasses).  */
736     int nfields;
737
738     /* Number of baseclasses.  */
739     int nbaseclasses;
740
741     /* Set if the accesibility of one of the fields is not public.  */
742     int non_public_fields;
743
744     /* Member function fields array, entries are allocated in the order they
745        are encountered in the object file.  */
746     struct nextfnfield
747       {
748         struct nextfnfield *next;
749         struct fn_field fnfield;
750       }
751      *fnfields;
752
753     /* Member function fieldlist array, contains name of possibly overloaded
754        member function, number of overloaded member functions and a pointer
755        to the head of the member function field chain.  */
756     struct fnfieldlist
757       {
758         char *name;
759         int length;
760         struct nextfnfield *head;
761       }
762      *fnfieldlists;
763
764     /* Number of entries in the fnfieldlists array.  */
765     int nfnfields;
766
767     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
768        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
769     struct typedef_field_list
770       {
771         struct typedef_field field;
772         struct typedef_field_list *next;
773       }
774     *typedef_field_list;
775     unsigned typedef_field_list_count;
776   };
777
778 /* One item on the queue of compilation units to read in full symbols
779    for.  */
780 struct dwarf2_queue_item
781 {
782   struct dwarf2_per_cu_data *per_cu;
783   struct dwarf2_queue_item *next;
784 };
785
786 /* The current queue.  */
787 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
788
789 /* Loaded secondary compilation units are kept in memory until they
790    have not been referenced for the processing of this many
791    compilation units.  Set this to zero to disable caching.  Cache
792    sizes of up to at least twenty will improve startup time for
793    typical inter-CU-reference binaries, at an obvious memory cost.  */
794 static int dwarf2_max_cache_age = 5;
795 static void
796 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
797                            struct cmd_list_element *c, const char *value)
798 {
799   fprintf_filtered (file, _("The upper bound on the age of cached "
800                             "dwarf2 compilation units is %s.\n"),
801                     value);
802 }
803
804
805 /* Various complaints about symbol reading that don't abort the process.  */
806
807 static void
808 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
809 {
810   complaint (&symfile_complaints,
811              _("statement list doesn't fit in .debug_line section"));
812 }
813
814 static void
815 dwarf2_debug_line_missing_file_complaint (void)
816 {
817   complaint (&symfile_complaints,
818              _(".debug_line section has line data without a file"));
819 }
820
821 static void
822 dwarf2_debug_line_missing_end_sequence_complaint (void)
823 {
824   complaint (&symfile_complaints,
825              _(".debug_line section has line "
826                "program sequence without an end"));
827 }
828
829 static void
830 dwarf2_complex_location_expr_complaint (void)
831 {
832   complaint (&symfile_complaints, _("location expression too complex"));
833 }
834
835 static void
836 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
837                                               int arg3)
838 {
839   complaint (&symfile_complaints,
840              _("const value length mismatch for '%s', got %d, expected %d"),
841              arg1, arg2, arg3);
842 }
843
844 static void
845 dwarf2_macros_too_long_complaint (void)
846 {
847   complaint (&symfile_complaints,
848              _("macro info runs off end of `.debug_macinfo' section"));
849 }
850
851 static void
852 dwarf2_macro_malformed_definition_complaint (const char *arg1)
853 {
854   complaint (&symfile_complaints,
855              _("macro debug info contains a "
856                "malformed macro definition:\n`%s'"),
857              arg1);
858 }
859
860 static void
861 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
862 {
863   complaint (&symfile_complaints,
864              _("invalid attribute class or form for '%s' in '%s'"),
865              arg1, arg2);
866 }
867
868 /* local function prototypes */
869
870 static void dwarf2_locate_sections (bfd *, asection *, void *);
871
872 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873                                            struct objfile *);
874
875 static void dwarf2_build_psymtabs_hard (struct objfile *);
876
877 static void scan_partial_symbols (struct partial_die_info *,
878                                   CORE_ADDR *, CORE_ADDR *,
879                                   int, struct dwarf2_cu *);
880
881 static void add_partial_symbol (struct partial_die_info *,
882                                 struct dwarf2_cu *);
883
884 static void add_partial_namespace (struct partial_die_info *pdi,
885                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
886                                    int need_pc, struct dwarf2_cu *cu);
887
888 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
889                                 CORE_ADDR *highpc, int need_pc,
890                                 struct dwarf2_cu *cu);
891
892 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
893                                      struct dwarf2_cu *cu);
894
895 static void add_partial_subprogram (struct partial_die_info *pdi,
896                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
897                                     int need_pc, struct dwarf2_cu *cu);
898
899 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
900                                      gdb_byte *buffer, gdb_byte *info_ptr,
901                                      bfd *abfd, struct dwarf2_cu *cu);
902
903 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
904
905 static void psymtab_to_symtab_1 (struct partial_symtab *);
906
907 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
908
909 static void dwarf2_free_abbrev_table (void *);
910
911 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
912                                             struct dwarf2_cu *);
913
914 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
915                                                  struct dwarf2_cu *);
916
917 static struct partial_die_info *load_partial_dies (bfd *,
918                                                    gdb_byte *, gdb_byte *,
919                                                    int, struct dwarf2_cu *);
920
921 static gdb_byte *read_partial_die (struct partial_die_info *,
922                                    struct abbrev_info *abbrev,
923                                    unsigned int, bfd *,
924                                    gdb_byte *, gdb_byte *,
925                                    struct dwarf2_cu *);
926
927 static struct partial_die_info *find_partial_die (unsigned int,
928                                                   struct dwarf2_cu *);
929
930 static void fixup_partial_die (struct partial_die_info *,
931                                struct dwarf2_cu *);
932
933 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
934                                  bfd *, gdb_byte *, struct dwarf2_cu *);
935
936 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
937                                        bfd *, gdb_byte *, struct dwarf2_cu *);
938
939 static unsigned int read_1_byte (bfd *, gdb_byte *);
940
941 static int read_1_signed_byte (bfd *, gdb_byte *);
942
943 static unsigned int read_2_bytes (bfd *, gdb_byte *);
944
945 static unsigned int read_4_bytes (bfd *, gdb_byte *);
946
947 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
948
949 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
950                                unsigned int *);
951
952 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
953
954 static LONGEST read_checked_initial_length_and_offset
955   (bfd *, gdb_byte *, const struct comp_unit_head *,
956    unsigned int *, unsigned int *);
957
958 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
959                             unsigned int *);
960
961 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
962
963 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
964
965 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
966
967 static char *read_indirect_string (bfd *, gdb_byte *,
968                                    const struct comp_unit_head *,
969                                    unsigned int *);
970
971 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
972
973 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
974
975 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
976
977 static void set_cu_language (unsigned int, struct dwarf2_cu *);
978
979 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
980                                       struct dwarf2_cu *);
981
982 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
983                                                 unsigned int,
984                                                 struct dwarf2_cu *);
985
986 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
987                                struct dwarf2_cu *cu);
988
989 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
990
991 static struct die_info *die_specification (struct die_info *die,
992                                            struct dwarf2_cu **);
993
994 static void free_line_header (struct line_header *lh);
995
996 static void add_file_name (struct line_header *, char *, unsigned int,
997                            unsigned int, unsigned int);
998
999 static struct line_header *(dwarf_decode_line_header
1000                             (unsigned int offset,
1001                              bfd *abfd, struct dwarf2_cu *cu));
1002
1003 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1004                                 struct dwarf2_cu *, struct partial_symtab *);
1005
1006 static void dwarf2_start_subfile (char *, const char *, const char *);
1007
1008 static struct symbol *new_symbol (struct die_info *, struct type *,
1009                                   struct dwarf2_cu *);
1010
1011 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1012                                        struct dwarf2_cu *, struct symbol *);
1013
1014 static void dwarf2_const_value (struct attribute *, struct symbol *,
1015                                 struct dwarf2_cu *);
1016
1017 static void dwarf2_const_value_attr (struct attribute *attr,
1018                                      struct type *type,
1019                                      const char *name,
1020                                      struct obstack *obstack,
1021                                      struct dwarf2_cu *cu, long *value,
1022                                      gdb_byte **bytes,
1023                                      struct dwarf2_locexpr_baton **baton);
1024
1025 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1026
1027 static int need_gnat_info (struct dwarf2_cu *);
1028
1029 static struct type *die_descriptive_type (struct die_info *,
1030                                           struct dwarf2_cu *);
1031
1032 static void set_descriptive_type (struct type *, struct die_info *,
1033                                   struct dwarf2_cu *);
1034
1035 static struct type *die_containing_type (struct die_info *,
1036                                          struct dwarf2_cu *);
1037
1038 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1039                                      struct dwarf2_cu *);
1040
1041 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1042
1043 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1044
1045 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1046
1047 static char *typename_concat (struct obstack *obs, const char *prefix,
1048                               const char *suffix, int physname,
1049                               struct dwarf2_cu *cu);
1050
1051 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1052
1053 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1054
1055 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1056
1057 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1060                                struct dwarf2_cu *, struct partial_symtab *);
1061
1062 static int dwarf2_get_pc_bounds (struct die_info *,
1063                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1064                                  struct partial_symtab *);
1065
1066 static void get_scope_pc_bounds (struct die_info *,
1067                                  CORE_ADDR *, CORE_ADDR *,
1068                                  struct dwarf2_cu *);
1069
1070 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1071                                         CORE_ADDR, struct dwarf2_cu *);
1072
1073 static void dwarf2_add_field (struct field_info *, struct die_info *,
1074                               struct dwarf2_cu *);
1075
1076 static void dwarf2_attach_fields_to_type (struct field_info *,
1077                                           struct type *, struct dwarf2_cu *);
1078
1079 static void dwarf2_add_member_fn (struct field_info *,
1080                                   struct die_info *, struct type *,
1081                                   struct dwarf2_cu *);
1082
1083 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1084                                              struct type *,
1085                                              struct dwarf2_cu *);
1086
1087 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1090
1091 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1092
1093 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1094
1095 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1096
1097 static struct type *read_module_type (struct die_info *die,
1098                                       struct dwarf2_cu *cu);
1099
1100 static const char *namespace_name (struct die_info *die,
1101                                    int *is_anonymous, struct dwarf2_cu *);
1102
1103 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1104
1105 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1106
1107 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1108                                                        struct dwarf2_cu *);
1109
1110 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1111
1112 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1113                                                  gdb_byte *info_ptr,
1114                                                  gdb_byte **new_info_ptr,
1115                                                  struct die_info *parent);
1116
1117 static struct die_info *read_die_and_children (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_siblings (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 gdb_byte *read_full_die (const struct die_reader_specs *reader,
1128                                 struct die_info **, gdb_byte *,
1129                                 int *);
1130
1131 static void process_die (struct die_info *, struct dwarf2_cu *);
1132
1133 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1134                                        struct obstack *);
1135
1136 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1137
1138 static const char *dwarf2_full_name (char *name,
1139                                      struct die_info *die,
1140                                      struct dwarf2_cu *cu);
1141
1142 static struct die_info *dwarf2_extension (struct die_info *die,
1143                                           struct dwarf2_cu **);
1144
1145 static char *dwarf_tag_name (unsigned int);
1146
1147 static char *dwarf_attr_name (unsigned int);
1148
1149 static char *dwarf_form_name (unsigned int);
1150
1151 static char *dwarf_bool_name (unsigned int);
1152
1153 static char *dwarf_type_encoding_name (unsigned int);
1154
1155 #if 0
1156 static char *dwarf_cfi_name (unsigned int);
1157 #endif
1158
1159 static struct die_info *sibling_die (struct die_info *);
1160
1161 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1162
1163 static void dump_die_for_error (struct die_info *);
1164
1165 static void dump_die_1 (struct ui_file *, int level, int max_level,
1166                         struct die_info *);
1167
1168 /*static*/ void dump_die (struct die_info *, int max_level);
1169
1170 static void store_in_ref_table (struct die_info *,
1171                                 struct dwarf2_cu *);
1172
1173 static int is_ref_attr (struct attribute *);
1174
1175 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1176
1177 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1178
1179 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1180                                                struct attribute *,
1181                                                struct dwarf2_cu **);
1182
1183 static struct die_info *follow_die_ref (struct die_info *,
1184                                         struct attribute *,
1185                                         struct dwarf2_cu **);
1186
1187 static struct die_info *follow_die_sig (struct die_info *,
1188                                         struct attribute *,
1189                                         struct dwarf2_cu **);
1190
1191 static void read_signatured_type_at_offset (struct objfile *objfile,
1192                                             unsigned int offset);
1193
1194 static void read_signatured_type (struct objfile *,
1195                                   struct signatured_type *type_sig);
1196
1197 /* memory allocation interface */
1198
1199 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1200
1201 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1202
1203 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1204
1205 static void initialize_cu_func_list (struct dwarf2_cu *);
1206
1207 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1208                                  struct dwarf2_cu *);
1209
1210 static void dwarf_decode_macros (struct line_header *, unsigned int,
1211                                  char *, bfd *, struct dwarf2_cu *);
1212
1213 static int attr_form_is_block (struct attribute *);
1214
1215 static int attr_form_is_section_offset (struct attribute *);
1216
1217 static int attr_form_is_constant (struct attribute *);
1218
1219 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1220                                    struct dwarf2_loclist_baton *baton,
1221                                    struct attribute *attr);
1222
1223 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1224                                          struct symbol *sym,
1225                                          struct dwarf2_cu *cu);
1226
1227 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1228                                struct abbrev_info *abbrev,
1229                                struct dwarf2_cu *cu);
1230
1231 static void free_stack_comp_unit (void *);
1232
1233 static hashval_t partial_die_hash (const void *item);
1234
1235 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1236
1237 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1238   (unsigned int offset, struct objfile *objfile);
1239
1240 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1241   (unsigned int offset, struct objfile *objfile);
1242
1243 static void init_one_comp_unit (struct dwarf2_cu *cu,
1244                                 struct objfile *objfile);
1245
1246 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1247                                    struct die_info *comp_unit_die);
1248
1249 static void free_one_comp_unit (void *);
1250
1251 static void free_cached_comp_units (void *);
1252
1253 static void age_cached_comp_units (void);
1254
1255 static void free_one_cached_comp_unit (void *);
1256
1257 static struct type *set_die_type (struct die_info *, struct type *,
1258                                   struct dwarf2_cu *);
1259
1260 static void create_all_comp_units (struct objfile *);
1261
1262 static int create_debug_types_hash_table (struct objfile *objfile);
1263
1264 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1265                                  struct objfile *);
1266
1267 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1268
1269 static void dwarf2_add_dependence (struct dwarf2_cu *,
1270                                    struct dwarf2_per_cu_data *);
1271
1272 static void dwarf2_mark (struct dwarf2_cu *);
1273
1274 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1275
1276 static struct type *get_die_type_at_offset (unsigned int,
1277                                             struct dwarf2_per_cu_data *per_cu);
1278
1279 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1280
1281 static void dwarf2_release_queue (void *dummy);
1282
1283 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1284                              struct objfile *objfile);
1285
1286 static void process_queue (struct objfile *objfile);
1287
1288 static void find_file_and_directory (struct die_info *die,
1289                                      struct dwarf2_cu *cu,
1290                                      char **name, char **comp_dir);
1291
1292 static char *file_full_name (int file, struct line_header *lh,
1293                              const char *comp_dir);
1294
1295 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1296                                               gdb_byte *info_ptr,
1297                                               gdb_byte *buffer,
1298                                               unsigned int buffer_size,
1299                                               bfd *abfd);
1300
1301 static void init_cu_die_reader (struct die_reader_specs *reader,
1302                                 struct dwarf2_cu *cu);
1303
1304 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1305
1306 #if WORDS_BIGENDIAN
1307
1308 /* Convert VALUE between big- and little-endian.  */
1309 static offset_type
1310 byte_swap (offset_type value)
1311 {
1312   offset_type result;
1313
1314   result = (value & 0xff) << 24;
1315   result |= (value & 0xff00) << 8;
1316   result |= (value & 0xff0000) >> 8;
1317   result |= (value & 0xff000000) >> 24;
1318   return result;
1319 }
1320
1321 #define MAYBE_SWAP(V)  byte_swap (V)
1322
1323 #else
1324 #define MAYBE_SWAP(V) (V)
1325 #endif /* WORDS_BIGENDIAN */
1326
1327 /* The suffix for an index file.  */
1328 #define INDEX_SUFFIX ".gdb-index"
1329
1330 static const char *dwarf2_physname (char *name, struct die_info *die,
1331                                     struct dwarf2_cu *cu);
1332
1333 /* Try to locate the sections we need for DWARF 2 debugging
1334    information and return true if we have enough to do something.  */
1335
1336 int
1337 dwarf2_has_info (struct objfile *objfile)
1338 {
1339   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1340   if (!dwarf2_per_objfile)
1341     {
1342       /* Initialize per-objfile state.  */
1343       struct dwarf2_per_objfile *data
1344         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1345
1346       memset (data, 0, sizeof (*data));
1347       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1348       dwarf2_per_objfile = data;
1349
1350       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1351       dwarf2_per_objfile->objfile = objfile;
1352     }
1353   return (dwarf2_per_objfile->info.asection != NULL
1354           && dwarf2_per_objfile->abbrev.asection != NULL);
1355 }
1356
1357 /* When loading sections, we can either look for ".<name>", or for
1358  * ".z<name>", which indicates a compressed section.  */
1359
1360 static int
1361 section_is_p (const char *section_name, const char *name)
1362 {
1363   return (section_name[0] == '.'
1364           && (strcmp (section_name + 1, name) == 0
1365               || (section_name[1] == 'z'
1366                   && strcmp (section_name + 2, name) == 0)));
1367 }
1368
1369 /* This function is mapped across the sections and remembers the
1370    offset and size of each of the debugging sections we are interested
1371    in.  */
1372
1373 static void
1374 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1375 {
1376   if (section_is_p (sectp->name, INFO_SECTION))
1377     {
1378       dwarf2_per_objfile->info.asection = sectp;
1379       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1380     }
1381   else if (section_is_p (sectp->name, ABBREV_SECTION))
1382     {
1383       dwarf2_per_objfile->abbrev.asection = sectp;
1384       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1385     }
1386   else if (section_is_p (sectp->name, LINE_SECTION))
1387     {
1388       dwarf2_per_objfile->line.asection = sectp;
1389       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1390     }
1391   else if (section_is_p (sectp->name, LOC_SECTION))
1392     {
1393       dwarf2_per_objfile->loc.asection = sectp;
1394       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1395     }
1396   else if (section_is_p (sectp->name, MACINFO_SECTION))
1397     {
1398       dwarf2_per_objfile->macinfo.asection = sectp;
1399       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1400     }
1401   else if (section_is_p (sectp->name, STR_SECTION))
1402     {
1403       dwarf2_per_objfile->str.asection = sectp;
1404       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1405     }
1406   else if (section_is_p (sectp->name, FRAME_SECTION))
1407     {
1408       dwarf2_per_objfile->frame.asection = sectp;
1409       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1410     }
1411   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1412     {
1413       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1414
1415       if (aflag & SEC_HAS_CONTENTS)
1416         {
1417           dwarf2_per_objfile->eh_frame.asection = sectp;
1418           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1419         }
1420     }
1421   else if (section_is_p (sectp->name, RANGES_SECTION))
1422     {
1423       dwarf2_per_objfile->ranges.asection = sectp;
1424       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1425     }
1426   else if (section_is_p (sectp->name, TYPES_SECTION))
1427     {
1428       dwarf2_per_objfile->types.asection = sectp;
1429       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1430     }
1431   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1432     {
1433       dwarf2_per_objfile->gdb_index.asection = sectp;
1434       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1435     }
1436
1437   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1438       && bfd_section_vma (abfd, sectp) == 0)
1439     dwarf2_per_objfile->has_section_at_zero = 1;
1440 }
1441
1442 /* Decompress a section that was compressed using zlib.  Store the
1443    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1444
1445 static void
1446 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1447                          gdb_byte **outbuf, bfd_size_type *outsize)
1448 {
1449   bfd *abfd = objfile->obfd;
1450 #ifndef HAVE_ZLIB_H
1451   error (_("Support for zlib-compressed DWARF data (from '%s') "
1452            "is disabled in this copy of GDB"),
1453          bfd_get_filename (abfd));
1454 #else
1455   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1456   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1457   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1458   bfd_size_type uncompressed_size;
1459   gdb_byte *uncompressed_buffer;
1460   z_stream strm;
1461   int rc;
1462   int header_size = 12;
1463
1464   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1465       || bfd_bread (compressed_buffer,
1466                     compressed_size, abfd) != compressed_size)
1467     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1468            bfd_get_filename (abfd));
1469
1470   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1471      by the uncompressed section size, 8 bytes in big-endian order.  */
1472   if (compressed_size < header_size
1473       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1474     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1475            bfd_get_filename (abfd));
1476   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1477   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1478   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1479   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1480   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1481   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1482   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1483   uncompressed_size += compressed_buffer[11];
1484
1485   /* It is possible the section consists of several compressed
1486      buffers concatenated together, so we uncompress in a loop.  */
1487   strm.zalloc = NULL;
1488   strm.zfree = NULL;
1489   strm.opaque = NULL;
1490   strm.avail_in = compressed_size - header_size;
1491   strm.next_in = (Bytef*) compressed_buffer + header_size;
1492   strm.avail_out = uncompressed_size;
1493   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1494                                        uncompressed_size);
1495   rc = inflateInit (&strm);
1496   while (strm.avail_in > 0)
1497     {
1498       if (rc != Z_OK)
1499         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1500                bfd_get_filename (abfd), rc);
1501       strm.next_out = ((Bytef*) uncompressed_buffer
1502                        + (uncompressed_size - strm.avail_out));
1503       rc = inflate (&strm, Z_FINISH);
1504       if (rc != Z_STREAM_END)
1505         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1506                bfd_get_filename (abfd), rc);
1507       rc = inflateReset (&strm);
1508     }
1509   rc = inflateEnd (&strm);
1510   if (rc != Z_OK
1511       || strm.avail_out != 0)
1512     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1513            bfd_get_filename (abfd), rc);
1514
1515   do_cleanups (cleanup);
1516   *outbuf = uncompressed_buffer;
1517   *outsize = uncompressed_size;
1518 #endif
1519 }
1520
1521 /* A helper function that decides whether a section is empty.  */
1522
1523 static int
1524 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1525 {
1526   return info->asection == NULL || info->size == 0;
1527 }
1528
1529 /* Read the contents of the section SECTP from object file specified by
1530    OBJFILE, store info about the section into INFO.
1531    If the section is compressed, uncompress it before returning.  */
1532
1533 static void
1534 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1535 {
1536   bfd *abfd = objfile->obfd;
1537   asection *sectp = info->asection;
1538   gdb_byte *buf, *retbuf;
1539   unsigned char header[4];
1540
1541   if (info->readin)
1542     return;
1543   info->buffer = NULL;
1544   info->was_mmapped = 0;
1545   info->readin = 1;
1546
1547   if (dwarf2_section_empty_p (info))
1548     return;
1549
1550   /* Check if the file has a 4-byte header indicating compression.  */
1551   if (info->size > sizeof (header)
1552       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1553       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1554     {
1555       /* Upon decompression, update the buffer and its size.  */
1556       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1557         {
1558           zlib_decompress_section (objfile, sectp, &info->buffer,
1559                                    &info->size);
1560           return;
1561         }
1562     }
1563
1564 #ifdef HAVE_MMAP
1565   if (pagesize == 0)
1566     pagesize = getpagesize ();
1567
1568   /* Only try to mmap sections which are large enough: we don't want to
1569      waste space due to fragmentation.  Also, only try mmap for sections
1570      without relocations.  */
1571
1572   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1573     {
1574       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1575       size_t map_length = info->size + sectp->filepos - pg_offset;
1576       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1577                                  MAP_PRIVATE, pg_offset);
1578
1579       if (retbuf != MAP_FAILED)
1580         {
1581           info->was_mmapped = 1;
1582           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1583 #if HAVE_POSIX_MADVISE
1584           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1585 #endif
1586           return;
1587         }
1588     }
1589 #endif
1590
1591   /* If we get here, we are a normal, not-compressed section.  */
1592   info->buffer = buf
1593     = obstack_alloc (&objfile->objfile_obstack, info->size);
1594
1595   /* When debugging .o files, we may need to apply relocations; see
1596      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1597      We never compress sections in .o files, so we only need to
1598      try this when the section is not compressed.  */
1599   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1600   if (retbuf != NULL)
1601     {
1602       info->buffer = retbuf;
1603       return;
1604     }
1605
1606   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1607       || bfd_bread (buf, info->size, abfd) != info->size)
1608     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1609            bfd_get_filename (abfd));
1610 }
1611
1612 /* A helper function that returns the size of a section in a safe way.
1613    If you are positive that the section has been read before using the
1614    size, then it is safe to refer to the dwarf2_section_info object's
1615    "size" field directly.  In other cases, you must call this
1616    function, because for compressed sections the size field is not set
1617    correctly until the section has been read.  */
1618
1619 static bfd_size_type
1620 dwarf2_section_size (struct objfile *objfile,
1621                      struct dwarf2_section_info *info)
1622 {
1623   if (!info->readin)
1624     dwarf2_read_section (objfile, info);
1625   return info->size;
1626 }
1627
1628 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1629    SECTION_NAME.  */
1630
1631 void
1632 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1633                          asection **sectp, gdb_byte **bufp,
1634                          bfd_size_type *sizep)
1635 {
1636   struct dwarf2_per_objfile *data
1637     = objfile_data (objfile, dwarf2_objfile_data_key);
1638   struct dwarf2_section_info *info;
1639
1640   /* We may see an objfile without any DWARF, in which case we just
1641      return nothing.  */
1642   if (data == NULL)
1643     {
1644       *sectp = NULL;
1645       *bufp = NULL;
1646       *sizep = 0;
1647       return;
1648     }
1649   if (section_is_p (section_name, EH_FRAME_SECTION))
1650     info = &data->eh_frame;
1651   else if (section_is_p (section_name, FRAME_SECTION))
1652     info = &data->frame;
1653   else
1654     gdb_assert_not_reached ("unexpected section");
1655
1656   dwarf2_read_section (objfile, info);
1657
1658   *sectp = info->asection;
1659   *bufp = info->buffer;
1660   *sizep = info->size;
1661 }
1662
1663 \f
1664 /* DWARF quick_symbols_functions support.  */
1665
1666 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1667    unique line tables, so we maintain a separate table of all .debug_line
1668    derived entries to support the sharing.
1669    All the quick functions need is the list of file names.  We discard the
1670    line_header when we're done and don't need to record it here.  */
1671 struct quick_file_names
1672 {
1673   /* The offset in .debug_line of the line table.  We hash on this.  */
1674   unsigned int offset;
1675
1676   /* The number of entries in file_names, real_names.  */
1677   unsigned int num_file_names;
1678
1679   /* The file names from the line table, after being run through
1680      file_full_name.  */
1681   const char **file_names;
1682
1683   /* The file names from the line table after being run through
1684      gdb_realpath.  These are computed lazily.  */
1685   const char **real_names;
1686 };
1687
1688 /* When using the index (and thus not using psymtabs), each CU has an
1689    object of this type.  This is used to hold information needed by
1690    the various "quick" methods.  */
1691 struct dwarf2_per_cu_quick_data
1692 {
1693   /* The file table.  This can be NULL if there was no file table
1694      or it's currently not read in.
1695      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1696   struct quick_file_names *file_names;
1697
1698   /* The corresponding symbol table.  This is NULL if symbols for this
1699      CU have not yet been read.  */
1700   struct symtab *symtab;
1701
1702   /* A temporary mark bit used when iterating over all CUs in
1703      expand_symtabs_matching.  */
1704   unsigned int mark : 1;
1705
1706   /* True if we've tried to read the file table and found there isn't one.
1707      There will be no point in trying to read it again next time.  */
1708   unsigned int no_file_data : 1;
1709 };
1710
1711 /* Hash function for a quick_file_names.  */
1712
1713 static hashval_t
1714 hash_file_name_entry (const void *e)
1715 {
1716   const struct quick_file_names *file_data = e;
1717
1718   return file_data->offset;
1719 }
1720
1721 /* Equality function for a quick_file_names.  */
1722
1723 static int
1724 eq_file_name_entry (const void *a, const void *b)
1725 {
1726   const struct quick_file_names *ea = a;
1727   const struct quick_file_names *eb = b;
1728
1729   return ea->offset == eb->offset;
1730 }
1731
1732 /* Delete function for a quick_file_names.  */
1733
1734 static void
1735 delete_file_name_entry (void *e)
1736 {
1737   struct quick_file_names *file_data = e;
1738   int i;
1739
1740   for (i = 0; i < file_data->num_file_names; ++i)
1741     {
1742       xfree ((void*) file_data->file_names[i]);
1743       if (file_data->real_names)
1744         xfree ((void*) file_data->real_names[i]);
1745     }
1746
1747   /* The space for the struct itself lives on objfile_obstack,
1748      so we don't free it here.  */
1749 }
1750
1751 /* Create a quick_file_names hash table.  */
1752
1753 static htab_t
1754 create_quick_file_names_table (unsigned int nr_initial_entries)
1755 {
1756   return htab_create_alloc (nr_initial_entries,
1757                             hash_file_name_entry, eq_file_name_entry,
1758                             delete_file_name_entry, xcalloc, xfree);
1759 }
1760
1761 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1762    this CU came.  */
1763
1764 static void
1765 dw2_do_instantiate_symtab (struct objfile *objfile,
1766                            struct dwarf2_per_cu_data *per_cu)
1767 {
1768   struct cleanup *back_to;
1769
1770   back_to = make_cleanup (dwarf2_release_queue, NULL);
1771
1772   queue_comp_unit (per_cu, objfile);
1773
1774   if (per_cu->from_debug_types)
1775     read_signatured_type_at_offset (objfile, per_cu->offset);
1776   else
1777     load_full_comp_unit (per_cu, objfile);
1778
1779   process_queue (objfile);
1780
1781   /* Age the cache, releasing compilation units that have not
1782      been used recently.  */
1783   age_cached_comp_units ();
1784
1785   do_cleanups (back_to);
1786 }
1787
1788 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1789    the objfile from which this CU came.  Returns the resulting symbol
1790    table.  */
1791
1792 static struct symtab *
1793 dw2_instantiate_symtab (struct objfile *objfile,
1794                         struct dwarf2_per_cu_data *per_cu)
1795 {
1796   if (!per_cu->v.quick->symtab)
1797     {
1798       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1799       increment_reading_symtab ();
1800       dw2_do_instantiate_symtab (objfile, per_cu);
1801       do_cleanups (back_to);
1802     }
1803   return per_cu->v.quick->symtab;
1804 }
1805
1806 /* Return the CU given its index.  */
1807
1808 static struct dwarf2_per_cu_data *
1809 dw2_get_cu (int index)
1810 {
1811   if (index >= dwarf2_per_objfile->n_comp_units)
1812     {
1813       index -= dwarf2_per_objfile->n_comp_units;
1814       return dwarf2_per_objfile->type_comp_units[index];
1815     }
1816   return dwarf2_per_objfile->all_comp_units[index];
1817 }
1818
1819 /* A helper function that knows how to read a 64-bit value in a way
1820    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1821    otherwise.  */
1822
1823 static int
1824 extract_cu_value (const char *bytes, ULONGEST *result)
1825 {
1826   if (sizeof (ULONGEST) < 8)
1827     {
1828       int i;
1829
1830       /* Ignore the upper 4 bytes if they are all zero.  */
1831       for (i = 0; i < 4; ++i)
1832         if (bytes[i + 4] != 0)
1833           return 0;
1834
1835       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1836     }
1837   else
1838     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1839   return 1;
1840 }
1841
1842 /* Read the CU list from the mapped index, and use it to create all
1843    the CU objects for this objfile.  Return 0 if something went wrong,
1844    1 if everything went ok.  */
1845
1846 static int
1847 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1848                        offset_type cu_list_elements)
1849 {
1850   offset_type i;
1851
1852   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1853   dwarf2_per_objfile->all_comp_units
1854     = obstack_alloc (&objfile->objfile_obstack,
1855                      dwarf2_per_objfile->n_comp_units
1856                      * sizeof (struct dwarf2_per_cu_data *));
1857
1858   for (i = 0; i < cu_list_elements; i += 2)
1859     {
1860       struct dwarf2_per_cu_data *the_cu;
1861       ULONGEST offset, length;
1862
1863       if (!extract_cu_value (cu_list, &offset)
1864           || !extract_cu_value (cu_list + 8, &length))
1865         return 0;
1866       cu_list += 2 * 8;
1867
1868       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1869                                struct dwarf2_per_cu_data);
1870       the_cu->offset = offset;
1871       the_cu->length = length;
1872       the_cu->objfile = objfile;
1873       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1874                                         struct dwarf2_per_cu_quick_data);
1875       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1876     }
1877
1878   return 1;
1879 }
1880
1881 /* Create the signatured type hash table from the index.  */
1882
1883 static int
1884 create_signatured_type_table_from_index (struct objfile *objfile,
1885                                          const gdb_byte *bytes,
1886                                          offset_type elements)
1887 {
1888   offset_type i;
1889   htab_t sig_types_hash;
1890
1891   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1892   dwarf2_per_objfile->type_comp_units
1893     = obstack_alloc (&objfile->objfile_obstack,
1894                      dwarf2_per_objfile->n_type_comp_units
1895                      * sizeof (struct dwarf2_per_cu_data *));
1896
1897   sig_types_hash = allocate_signatured_type_table (objfile);
1898
1899   for (i = 0; i < elements; i += 3)
1900     {
1901       struct signatured_type *type_sig;
1902       ULONGEST offset, type_offset, signature;
1903       void **slot;
1904
1905       if (!extract_cu_value (bytes, &offset)
1906           || !extract_cu_value (bytes + 8, &type_offset))
1907         return 0;
1908       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1909       bytes += 3 * 8;
1910
1911       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1912                                  struct signatured_type);
1913       type_sig->signature = signature;
1914       type_sig->offset = offset;
1915       type_sig->type_offset = type_offset;
1916       type_sig->per_cu.from_debug_types = 1;
1917       type_sig->per_cu.offset = offset;
1918       type_sig->per_cu.objfile = objfile;
1919       type_sig->per_cu.v.quick
1920         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1921                           struct dwarf2_per_cu_quick_data);
1922
1923       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1924       *slot = type_sig;
1925
1926       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1927     }
1928
1929   dwarf2_per_objfile->signatured_types = sig_types_hash;
1930
1931   return 1;
1932 }
1933
1934 /* Read the address map data from the mapped index, and use it to
1935    populate the objfile's psymtabs_addrmap.  */
1936
1937 static void
1938 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1939 {
1940   const gdb_byte *iter, *end;
1941   struct obstack temp_obstack;
1942   struct addrmap *mutable_map;
1943   struct cleanup *cleanup;
1944   CORE_ADDR baseaddr;
1945
1946   obstack_init (&temp_obstack);
1947   cleanup = make_cleanup_obstack_free (&temp_obstack);
1948   mutable_map = addrmap_create_mutable (&temp_obstack);
1949
1950   iter = index->address_table;
1951   end = iter + index->address_table_size;
1952
1953   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1954
1955   while (iter < end)
1956     {
1957       ULONGEST hi, lo, cu_index;
1958       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1959       iter += 8;
1960       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1961       iter += 8;
1962       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1963       iter += 4;
1964       
1965       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1966                          dw2_get_cu (cu_index));
1967     }
1968
1969   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1970                                                     &objfile->objfile_obstack);
1971   do_cleanups (cleanup);
1972 }
1973
1974 /* The hash function for strings in the mapped index.  This is the same as
1975    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1976    implementation.  This is necessary because the hash function is tied to the
1977    format of the mapped index file.  The hash values do not have to match with
1978    SYMBOL_HASH_NEXT.
1979    
1980    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
1981
1982 static hashval_t
1983 mapped_index_string_hash (int index_version, const void *p)
1984 {
1985   const unsigned char *str = (const unsigned char *) p;
1986   hashval_t r = 0;
1987   unsigned char c;
1988
1989   while ((c = *str++) != 0)
1990     {
1991       if (index_version >= 5)
1992         c = tolower (c);
1993       r = r * 67 + c - 113;
1994     }
1995
1996   return r;
1997 }
1998
1999 /* Find a slot in the mapped index INDEX for the object named NAME.
2000    If NAME is found, set *VEC_OUT to point to the CU vector in the
2001    constant pool and return 1.  If NAME cannot be found, return 0.  */
2002
2003 static int
2004 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2005                           offset_type **vec_out)
2006 {
2007   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2008   offset_type hash;
2009   offset_type slot, step;
2010   int (*cmp) (const char *, const char *);
2011
2012   if (current_language->la_language == language_cplus
2013       || current_language->la_language == language_java
2014       || current_language->la_language == language_fortran)
2015     {
2016       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2017          not contain any.  */
2018       const char *paren = strchr (name, '(');
2019
2020       if (paren)
2021         {
2022           char *dup;
2023
2024           dup = xmalloc (paren - name + 1);
2025           memcpy (dup, name, paren - name);
2026           dup[paren - name] = 0;
2027
2028           make_cleanup (xfree, dup);
2029           name = dup;
2030         }
2031     }
2032
2033   /* Index version 4 did not support case insensitive searches.  But the
2034      indexes for case insensitive languages are built in lowercase, therefore
2035      simulate our NAME being searched is also lowercased.  */
2036   hash = mapped_index_string_hash ((index->version == 4
2037                                     && case_sensitivity == case_sensitive_off
2038                                     ? 5 : index->version),
2039                                    name);
2040
2041   slot = hash & (index->symbol_table_slots - 1);
2042   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2043   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2044
2045   for (;;)
2046     {
2047       /* Convert a slot number to an offset into the table.  */
2048       offset_type i = 2 * slot;
2049       const char *str;
2050       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2051         {
2052           do_cleanups (back_to);
2053           return 0;
2054         }
2055
2056       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2057       if (!cmp (name, str))
2058         {
2059           *vec_out = (offset_type *) (index->constant_pool
2060                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2061           do_cleanups (back_to);
2062           return 1;
2063         }
2064
2065       slot = (slot + step) & (index->symbol_table_slots - 1);
2066     }
2067 }
2068
2069 /* Read the index file.  If everything went ok, initialize the "quick"
2070    elements of all the CUs and return 1.  Otherwise, return 0.  */
2071
2072 static int
2073 dwarf2_read_index (struct objfile *objfile)
2074 {
2075   char *addr;
2076   struct mapped_index *map;
2077   offset_type *metadata;
2078   const gdb_byte *cu_list;
2079   const gdb_byte *types_list = NULL;
2080   offset_type version, cu_list_elements;
2081   offset_type types_list_elements = 0;
2082   int i;
2083
2084   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2085     return 0;
2086
2087   /* Older elfutils strip versions could keep the section in the main
2088      executable while splitting it for the separate debug info file.  */
2089   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2090        & SEC_HAS_CONTENTS) == 0)
2091     return 0;
2092
2093   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2094
2095   addr = dwarf2_per_objfile->gdb_index.buffer;
2096   /* Version check.  */
2097   version = MAYBE_SWAP (*(offset_type *) addr);
2098   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2099      causes the index to behave very poorly for certain requests.  Version 3
2100      contained incomplete addrmap.  So, it seems better to just ignore such
2101      indices.  Index version 4 uses a different hash function than index
2102      version 5 and later.  */
2103   if (version < 4)
2104     return 0;
2105   /* Indexes with higher version than the one supported by GDB may be no
2106      longer backward compatible.  */
2107   if (version > 5)
2108     return 0;
2109
2110   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2111   map->version = version;
2112   map->total_size = dwarf2_per_objfile->gdb_index.size;
2113
2114   metadata = (offset_type *) (addr + sizeof (offset_type));
2115
2116   i = 0;
2117   cu_list = addr + MAYBE_SWAP (metadata[i]);
2118   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2119                       / 8);
2120   ++i;
2121
2122   types_list = addr + MAYBE_SWAP (metadata[i]);
2123   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2124                           - MAYBE_SWAP (metadata[i]))
2125                          / 8);
2126   ++i;
2127
2128   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2129   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2130                              - MAYBE_SWAP (metadata[i]));
2131   ++i;
2132
2133   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2134   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2135                               - MAYBE_SWAP (metadata[i]))
2136                              / (2 * sizeof (offset_type)));
2137   ++i;
2138
2139   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2140
2141   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2142     return 0;
2143
2144   if (types_list_elements
2145       && !create_signatured_type_table_from_index (objfile, types_list,
2146                                                    types_list_elements))
2147     return 0;
2148
2149   create_addrmap_from_index (objfile, map);
2150
2151   dwarf2_per_objfile->index_table = map;
2152   dwarf2_per_objfile->using_index = 1;
2153   dwarf2_per_objfile->quick_file_names_table =
2154     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2155
2156   return 1;
2157 }
2158
2159 /* A helper for the "quick" functions which sets the global
2160    dwarf2_per_objfile according to OBJFILE.  */
2161
2162 static void
2163 dw2_setup (struct objfile *objfile)
2164 {
2165   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2166   gdb_assert (dwarf2_per_objfile);
2167 }
2168
2169 /* A helper for the "quick" functions which attempts to read the line
2170    table for THIS_CU.  */
2171
2172 static struct quick_file_names *
2173 dw2_get_file_names (struct objfile *objfile,
2174                     struct dwarf2_per_cu_data *this_cu)
2175 {
2176   bfd *abfd = objfile->obfd;
2177   struct line_header *lh;
2178   struct attribute *attr;
2179   struct cleanup *cleanups;
2180   struct die_info *comp_unit_die;
2181   struct dwarf2_section_info* sec;
2182   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2183   int has_children, i;
2184   struct dwarf2_cu cu;
2185   unsigned int bytes_read, buffer_size;
2186   struct die_reader_specs reader_specs;
2187   char *name, *comp_dir;
2188   void **slot;
2189   struct quick_file_names *qfn;
2190   unsigned int line_offset;
2191
2192   if (this_cu->v.quick->file_names != NULL)
2193     return this_cu->v.quick->file_names;
2194   /* If we know there is no line data, no point in looking again.  */
2195   if (this_cu->v.quick->no_file_data)
2196     return NULL;
2197
2198   init_one_comp_unit (&cu, objfile);
2199   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2200
2201   if (this_cu->from_debug_types)
2202     sec = &dwarf2_per_objfile->types;
2203   else
2204     sec = &dwarf2_per_objfile->info;
2205   dwarf2_read_section (objfile, sec);
2206   buffer_size = sec->size;
2207   buffer = sec->buffer;
2208   info_ptr = buffer + this_cu->offset;
2209   beg_of_comp_unit = info_ptr;
2210
2211   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2212                                           buffer, buffer_size,
2213                                           abfd);
2214
2215   /* Complete the cu_header.  */
2216   cu.header.offset = beg_of_comp_unit - buffer;
2217   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2218
2219   this_cu->cu = &cu;
2220   cu.per_cu = this_cu;
2221
2222   dwarf2_read_abbrevs (abfd, &cu);
2223   make_cleanup (dwarf2_free_abbrev_table, &cu);
2224
2225   if (this_cu->from_debug_types)
2226     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2227   init_cu_die_reader (&reader_specs, &cu);
2228   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2229                  &has_children);
2230
2231   lh = NULL;
2232   slot = NULL;
2233   line_offset = 0;
2234   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2235   if (attr)
2236     {
2237       struct quick_file_names find_entry;
2238
2239       line_offset = DW_UNSND (attr);
2240
2241       /* We may have already read in this line header (TU line header sharing).
2242          If we have we're done.  */
2243       find_entry.offset = line_offset;
2244       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2245                              &find_entry, INSERT);
2246       if (*slot != NULL)
2247         {
2248           do_cleanups (cleanups);
2249           this_cu->v.quick->file_names = *slot;
2250           return *slot;
2251         }
2252
2253       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2254     }
2255   if (lh == NULL)
2256     {
2257       do_cleanups (cleanups);
2258       this_cu->v.quick->no_file_data = 1;
2259       return NULL;
2260     }
2261
2262   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2263   qfn->offset = line_offset;
2264   gdb_assert (slot != NULL);
2265   *slot = qfn;
2266
2267   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2268
2269   qfn->num_file_names = lh->num_file_names;
2270   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2271                                    lh->num_file_names * sizeof (char *));
2272   for (i = 0; i < lh->num_file_names; ++i)
2273     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2274   qfn->real_names = NULL;
2275
2276   free_line_header (lh);
2277   do_cleanups (cleanups);
2278
2279   this_cu->v.quick->file_names = qfn;
2280   return qfn;
2281 }
2282
2283 /* A helper for the "quick" functions which computes and caches the
2284    real path for a given file name from the line table.  */
2285
2286 static const char *
2287 dw2_get_real_path (struct objfile *objfile,
2288                    struct quick_file_names *qfn, int index)
2289 {
2290   if (qfn->real_names == NULL)
2291     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2292                                       qfn->num_file_names, sizeof (char *));
2293
2294   if (qfn->real_names[index] == NULL)
2295     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2296
2297   return qfn->real_names[index];
2298 }
2299
2300 static struct symtab *
2301 dw2_find_last_source_symtab (struct objfile *objfile)
2302 {
2303   int index;
2304
2305   dw2_setup (objfile);
2306   index = dwarf2_per_objfile->n_comp_units - 1;
2307   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2308 }
2309
2310 /* Traversal function for dw2_forget_cached_source_info.  */
2311
2312 static int
2313 dw2_free_cached_file_names (void **slot, void *info)
2314 {
2315   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2316
2317   if (file_data->real_names)
2318     {
2319       int i;
2320
2321       for (i = 0; i < file_data->num_file_names; ++i)
2322         {
2323           xfree ((void*) file_data->real_names[i]);
2324           file_data->real_names[i] = NULL;
2325         }
2326     }
2327
2328   return 1;
2329 }
2330
2331 static void
2332 dw2_forget_cached_source_info (struct objfile *objfile)
2333 {
2334   dw2_setup (objfile);
2335
2336   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2337                           dw2_free_cached_file_names, NULL);
2338 }
2339
2340 static int
2341 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2342                    const char *full_path, const char *real_path,
2343                    struct symtab **result)
2344 {
2345   int i;
2346   int check_basename = lbasename (name) == name;
2347   struct dwarf2_per_cu_data *base_cu = NULL;
2348
2349   dw2_setup (objfile);
2350
2351   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2352                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2353     {
2354       int j;
2355       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2356       struct quick_file_names *file_data;
2357
2358       if (per_cu->v.quick->symtab)
2359         continue;
2360
2361       file_data = dw2_get_file_names (objfile, per_cu);
2362       if (file_data == NULL)
2363         continue;
2364
2365       for (j = 0; j < file_data->num_file_names; ++j)
2366         {
2367           const char *this_name = file_data->file_names[j];
2368
2369           if (FILENAME_CMP (name, this_name) == 0)
2370             {
2371               *result = dw2_instantiate_symtab (objfile, per_cu);
2372               return 1;
2373             }
2374
2375           if (check_basename && ! base_cu
2376               && FILENAME_CMP (lbasename (this_name), name) == 0)
2377             base_cu = per_cu;
2378
2379           if (full_path != NULL)
2380             {
2381               const char *this_real_name = dw2_get_real_path (objfile,
2382                                                               file_data, j);
2383
2384               if (this_real_name != NULL
2385                   && FILENAME_CMP (full_path, this_real_name) == 0)
2386                 {
2387                   *result = dw2_instantiate_symtab (objfile, per_cu);
2388                   return 1;
2389                 }
2390             }
2391
2392           if (real_path != NULL)
2393             {
2394               const char *this_real_name = dw2_get_real_path (objfile,
2395                                                               file_data, j);
2396
2397               if (this_real_name != NULL
2398                   && FILENAME_CMP (real_path, this_real_name) == 0)
2399                 {
2400                   *result = dw2_instantiate_symtab (objfile, per_cu);
2401                   return 1;
2402                 }
2403             }
2404         }
2405     }
2406
2407   if (base_cu)
2408     {
2409       *result = dw2_instantiate_symtab (objfile, base_cu);
2410       return 1;
2411     }
2412
2413   return 0;
2414 }
2415
2416 static struct symtab *
2417 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2418                    const char *name, domain_enum domain)
2419 {
2420   /* We do all the work in the pre_expand_symtabs_matching hook
2421      instead.  */
2422   return NULL;
2423 }
2424
2425 /* A helper function that expands all symtabs that hold an object
2426    named NAME.  */
2427
2428 static void
2429 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2430 {
2431   dw2_setup (objfile);
2432
2433   /* index_table is NULL if OBJF_READNOW.  */
2434   if (dwarf2_per_objfile->index_table)
2435     {
2436       offset_type *vec;
2437
2438       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2439                                     name, &vec))
2440         {
2441           offset_type i, len = MAYBE_SWAP (*vec);
2442           for (i = 0; i < len; ++i)
2443             {
2444               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2445               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2446
2447               dw2_instantiate_symtab (objfile, per_cu);
2448             }
2449         }
2450     }
2451 }
2452
2453 static void
2454 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2455                                  enum block_enum block_kind, const char *name,
2456                                  domain_enum domain)
2457 {
2458   dw2_do_expand_symtabs_matching (objfile, name);
2459 }
2460
2461 static void
2462 dw2_print_stats (struct objfile *objfile)
2463 {
2464   int i, count;
2465
2466   dw2_setup (objfile);
2467   count = 0;
2468   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2469                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2470     {
2471       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2472
2473       if (!per_cu->v.quick->symtab)
2474         ++count;
2475     }
2476   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2477 }
2478
2479 static void
2480 dw2_dump (struct objfile *objfile)
2481 {
2482   /* Nothing worth printing.  */
2483 }
2484
2485 static void
2486 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2487               struct section_offsets *delta)
2488 {
2489   /* There's nothing to relocate here.  */
2490 }
2491
2492 static void
2493 dw2_expand_symtabs_for_function (struct objfile *objfile,
2494                                  const char *func_name)
2495 {
2496   dw2_do_expand_symtabs_matching (objfile, func_name);
2497 }
2498
2499 static void
2500 dw2_expand_all_symtabs (struct objfile *objfile)
2501 {
2502   int i;
2503
2504   dw2_setup (objfile);
2505
2506   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2507                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2508     {
2509       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2510
2511       dw2_instantiate_symtab (objfile, per_cu);
2512     }
2513 }
2514
2515 static void
2516 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2517                                   const char *filename)
2518 {
2519   int i;
2520
2521   dw2_setup (objfile);
2522
2523   /* We don't need to consider type units here.
2524      This is only called for examining code, e.g. expand_line_sal.
2525      There can be an order of magnitude (or more) more type units
2526      than comp units, and we avoid them if we can.  */
2527
2528   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2529     {
2530       int j;
2531       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2532       struct quick_file_names *file_data;
2533
2534       if (per_cu->v.quick->symtab)
2535         continue;
2536
2537       file_data = dw2_get_file_names (objfile, per_cu);
2538       if (file_data == NULL)
2539         continue;
2540
2541       for (j = 0; j < file_data->num_file_names; ++j)
2542         {
2543           const char *this_name = file_data->file_names[j];
2544           if (FILENAME_CMP (this_name, filename) == 0)
2545             {
2546               dw2_instantiate_symtab (objfile, per_cu);
2547               break;
2548             }
2549         }
2550     }
2551 }
2552
2553 static const char *
2554 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2555 {
2556   struct dwarf2_per_cu_data *per_cu;
2557   offset_type *vec;
2558   struct quick_file_names *file_data;
2559
2560   dw2_setup (objfile);
2561
2562   /* index_table is NULL if OBJF_READNOW.  */
2563   if (!dwarf2_per_objfile->index_table)
2564     return NULL;
2565
2566   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2567                                  name, &vec))
2568     return NULL;
2569
2570   /* Note that this just looks at the very first one named NAME -- but
2571      actually we are looking for a function.  find_main_filename
2572      should be rewritten so that it doesn't require a custom hook.  It
2573      could just use the ordinary symbol tables.  */
2574   /* vec[0] is the length, which must always be >0.  */
2575   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2576
2577   file_data = dw2_get_file_names (objfile, per_cu);
2578   if (file_data == NULL)
2579     return NULL;
2580
2581   return file_data->file_names[file_data->num_file_names - 1];
2582 }
2583
2584 static void
2585 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2586                           struct objfile *objfile, int global,
2587                           int (*callback) (struct block *,
2588                                            struct symbol *, void *),
2589                           void *data, symbol_compare_ftype *match,
2590                           symbol_compare_ftype *ordered_compare)
2591 {
2592   /* Currently unimplemented; used for Ada.  The function can be called if the
2593      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2594      does not look for non-Ada symbols this function should just return.  */
2595 }
2596
2597 static void
2598 dw2_expand_symtabs_matching (struct objfile *objfile,
2599                              int (*file_matcher) (const char *, void *),
2600                              int (*name_matcher) (const char *, void *),
2601                              enum search_domain kind,
2602                              void *data)
2603 {
2604   int i;
2605   offset_type iter;
2606   struct mapped_index *index;
2607
2608   dw2_setup (objfile);
2609
2610   /* index_table is NULL if OBJF_READNOW.  */
2611   if (!dwarf2_per_objfile->index_table)
2612     return;
2613   index = dwarf2_per_objfile->index_table;
2614
2615   if (file_matcher != NULL)
2616     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2617                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2618       {
2619         int j;
2620         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2621         struct quick_file_names *file_data;
2622
2623         per_cu->v.quick->mark = 0;
2624         if (per_cu->v.quick->symtab)
2625           continue;
2626
2627         file_data = dw2_get_file_names (objfile, per_cu);
2628         if (file_data == NULL)
2629           continue;
2630
2631         for (j = 0; j < file_data->num_file_names; ++j)
2632           {
2633             if (file_matcher (file_data->file_names[j], data))
2634               {
2635                 per_cu->v.quick->mark = 1;
2636                 break;
2637               }
2638           }
2639       }
2640
2641   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2642     {
2643       offset_type idx = 2 * iter;
2644       const char *name;
2645       offset_type *vec, vec_len, vec_idx;
2646
2647       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2648         continue;
2649
2650       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2651
2652       if (! (*name_matcher) (name, data))
2653         continue;
2654
2655       /* The name was matched, now expand corresponding CUs that were
2656          marked.  */
2657       vec = (offset_type *) (index->constant_pool
2658                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2659       vec_len = MAYBE_SWAP (vec[0]);
2660       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2661         {
2662           struct dwarf2_per_cu_data *per_cu;
2663
2664           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2665           if (file_matcher == NULL || per_cu->v.quick->mark)
2666             dw2_instantiate_symtab (objfile, per_cu);
2667         }
2668     }
2669 }
2670
2671 static struct symtab *
2672 dw2_find_pc_sect_symtab (struct objfile *objfile,
2673                          struct minimal_symbol *msymbol,
2674                          CORE_ADDR pc,
2675                          struct obj_section *section,
2676                          int warn_if_readin)
2677 {
2678   struct dwarf2_per_cu_data *data;
2679
2680   dw2_setup (objfile);
2681
2682   if (!objfile->psymtabs_addrmap)
2683     return NULL;
2684
2685   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2686   if (!data)
2687     return NULL;
2688
2689   if (warn_if_readin && data->v.quick->symtab)
2690     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2691              paddress (get_objfile_arch (objfile), pc));
2692
2693   return dw2_instantiate_symtab (objfile, data);
2694 }
2695
2696 static void
2697 dw2_map_symbol_filenames (struct objfile *objfile,
2698                           void (*fun) (const char *, const char *, void *),
2699                           void *data)
2700 {
2701   int i;
2702
2703   dw2_setup (objfile);
2704
2705   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2706                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2707     {
2708       int j;
2709       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2710       struct quick_file_names *file_data;
2711
2712       if (per_cu->v.quick->symtab)
2713         continue;
2714
2715       file_data = dw2_get_file_names (objfile, per_cu);
2716       if (file_data == NULL)
2717         continue;
2718
2719       for (j = 0; j < file_data->num_file_names; ++j)
2720         {
2721           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2722                                                           j);
2723           (*fun) (file_data->file_names[j], this_real_name, data);
2724         }
2725     }
2726 }
2727
2728 static int
2729 dw2_has_symbols (struct objfile *objfile)
2730 {
2731   return 1;
2732 }
2733
2734 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2735 {
2736   dw2_has_symbols,
2737   dw2_find_last_source_symtab,
2738   dw2_forget_cached_source_info,
2739   dw2_lookup_symtab,
2740   dw2_lookup_symbol,
2741   dw2_pre_expand_symtabs_matching,
2742   dw2_print_stats,
2743   dw2_dump,
2744   dw2_relocate,
2745   dw2_expand_symtabs_for_function,
2746   dw2_expand_all_symtabs,
2747   dw2_expand_symtabs_with_filename,
2748   dw2_find_symbol_file,
2749   dw2_map_matching_symbols,
2750   dw2_expand_symtabs_matching,
2751   dw2_find_pc_sect_symtab,
2752   dw2_map_symbol_filenames
2753 };
2754
2755 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2756    file will use psymtabs, or 1 if using the GNU index.  */
2757
2758 int
2759 dwarf2_initialize_objfile (struct objfile *objfile)
2760 {
2761   /* If we're about to read full symbols, don't bother with the
2762      indices.  In this case we also don't care if some other debug
2763      format is making psymtabs, because they are all about to be
2764      expanded anyway.  */
2765   if ((objfile->flags & OBJF_READNOW))
2766     {
2767       int i;
2768
2769       dwarf2_per_objfile->using_index = 1;
2770       create_all_comp_units (objfile);
2771       create_debug_types_hash_table (objfile);
2772       dwarf2_per_objfile->quick_file_names_table =
2773         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2774
2775       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2776                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2777         {
2778           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2779
2780           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2781                                             struct dwarf2_per_cu_quick_data);
2782         }
2783
2784       /* Return 1 so that gdb sees the "quick" functions.  However,
2785          these functions will be no-ops because we will have expanded
2786          all symtabs.  */
2787       return 1;
2788     }
2789
2790   if (dwarf2_read_index (objfile))
2791     return 1;
2792
2793   return 0;
2794 }
2795
2796 \f
2797
2798 /* Build a partial symbol table.  */
2799
2800 void
2801 dwarf2_build_psymtabs (struct objfile *objfile)
2802 {
2803   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2804     {
2805       init_psymbol_list (objfile, 1024);
2806     }
2807
2808   dwarf2_build_psymtabs_hard (objfile);
2809 }
2810
2811 /* Return TRUE if OFFSET is within CU_HEADER.  */
2812
2813 static inline int
2814 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2815 {
2816   unsigned int bottom = cu_header->offset;
2817   unsigned int top = (cu_header->offset
2818                       + cu_header->length
2819                       + cu_header->initial_length_size);
2820
2821   return (offset >= bottom && offset < top);
2822 }
2823
2824 /* Read in the comp unit header information from the debug_info at info_ptr.
2825    NOTE: This leaves members offset, first_die_offset to be filled in
2826    by the caller.  */
2827
2828 static gdb_byte *
2829 read_comp_unit_head (struct comp_unit_head *cu_header,
2830                      gdb_byte *info_ptr, bfd *abfd)
2831 {
2832   int signed_addr;
2833   unsigned int bytes_read;
2834
2835   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2836   cu_header->initial_length_size = bytes_read;
2837   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2838   info_ptr += bytes_read;
2839   cu_header->version = read_2_bytes (abfd, info_ptr);
2840   info_ptr += 2;
2841   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2842                                           &bytes_read);
2843   info_ptr += bytes_read;
2844   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2845   info_ptr += 1;
2846   signed_addr = bfd_get_sign_extend_vma (abfd);
2847   if (signed_addr < 0)
2848     internal_error (__FILE__, __LINE__,
2849                     _("read_comp_unit_head: dwarf from non elf file"));
2850   cu_header->signed_addr_p = signed_addr;
2851
2852   return info_ptr;
2853 }
2854
2855 static gdb_byte *
2856 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2857                              gdb_byte *buffer, unsigned int buffer_size,
2858                              bfd *abfd)
2859 {
2860   gdb_byte *beg_of_comp_unit = info_ptr;
2861
2862   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2863
2864   if (header->version != 2 && header->version != 3 && header->version != 4)
2865     error (_("Dwarf Error: wrong version in compilation unit header "
2866            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2867            bfd_get_filename (abfd));
2868
2869   if (header->abbrev_offset
2870       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2871                               &dwarf2_per_objfile->abbrev))
2872     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2873            "(offset 0x%lx + 6) [in module %s]"),
2874            (long) header->abbrev_offset,
2875            (long) (beg_of_comp_unit - buffer),
2876            bfd_get_filename (abfd));
2877
2878   if (beg_of_comp_unit + header->length + header->initial_length_size
2879       > buffer + buffer_size)
2880     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2881            "(offset 0x%lx + 0) [in module %s]"),
2882            (long) header->length,
2883            (long) (beg_of_comp_unit - buffer),
2884            bfd_get_filename (abfd));
2885
2886   return info_ptr;
2887 }
2888
2889 /* Read in the types comp unit header information from .debug_types entry at
2890    types_ptr.  The result is a pointer to one past the end of the header.  */
2891
2892 static gdb_byte *
2893 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2894                           ULONGEST *signature,
2895                           gdb_byte *types_ptr, bfd *abfd)
2896 {
2897   gdb_byte *initial_types_ptr = types_ptr;
2898
2899   dwarf2_read_section (dwarf2_per_objfile->objfile,
2900                        &dwarf2_per_objfile->types);
2901   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2902
2903   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2904
2905   *signature = read_8_bytes (abfd, types_ptr);
2906   types_ptr += 8;
2907   types_ptr += cu_header->offset_size;
2908   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2909
2910   return types_ptr;
2911 }
2912
2913 /* Allocate a new partial symtab for file named NAME and mark this new
2914    partial symtab as being an include of PST.  */
2915
2916 static void
2917 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2918                                struct objfile *objfile)
2919 {
2920   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2921
2922   subpst->section_offsets = pst->section_offsets;
2923   subpst->textlow = 0;
2924   subpst->texthigh = 0;
2925
2926   subpst->dependencies = (struct partial_symtab **)
2927     obstack_alloc (&objfile->objfile_obstack,
2928                    sizeof (struct partial_symtab *));
2929   subpst->dependencies[0] = pst;
2930   subpst->number_of_dependencies = 1;
2931
2932   subpst->globals_offset = 0;
2933   subpst->n_global_syms = 0;
2934   subpst->statics_offset = 0;
2935   subpst->n_static_syms = 0;
2936   subpst->symtab = NULL;
2937   subpst->read_symtab = pst->read_symtab;
2938   subpst->readin = 0;
2939
2940   /* No private part is necessary for include psymtabs.  This property
2941      can be used to differentiate between such include psymtabs and
2942      the regular ones.  */
2943   subpst->read_symtab_private = NULL;
2944 }
2945
2946 /* Read the Line Number Program data and extract the list of files
2947    included by the source file represented by PST.  Build an include
2948    partial symtab for each of these included files.  */
2949
2950 static void
2951 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2952                                struct die_info *die,
2953                                struct partial_symtab *pst)
2954 {
2955   struct objfile *objfile = cu->objfile;
2956   bfd *abfd = objfile->obfd;
2957   struct line_header *lh = NULL;
2958   struct attribute *attr;
2959
2960   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2961   if (attr)
2962     {
2963       unsigned int line_offset = DW_UNSND (attr);
2964
2965       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2966     }
2967   if (lh == NULL)
2968     return;  /* No linetable, so no includes.  */
2969
2970   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2971   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2972
2973   free_line_header (lh);
2974 }
2975
2976 static hashval_t
2977 hash_type_signature (const void *item)
2978 {
2979   const struct signatured_type *type_sig = item;
2980
2981   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2982   return type_sig->signature;
2983 }
2984
2985 static int
2986 eq_type_signature (const void *item_lhs, const void *item_rhs)
2987 {
2988   const struct signatured_type *lhs = item_lhs;
2989   const struct signatured_type *rhs = item_rhs;
2990
2991   return lhs->signature == rhs->signature;
2992 }
2993
2994 /* Allocate a hash table for signatured types.  */
2995
2996 static htab_t
2997 allocate_signatured_type_table (struct objfile *objfile)
2998 {
2999   return htab_create_alloc_ex (41,
3000                                hash_type_signature,
3001                                eq_type_signature,
3002                                NULL,
3003                                &objfile->objfile_obstack,
3004                                hashtab_obstack_allocate,
3005                                dummy_obstack_deallocate);
3006 }
3007
3008 /* A helper function to add a signatured type CU to a list.  */
3009
3010 static int
3011 add_signatured_type_cu_to_list (void **slot, void *datum)
3012 {
3013   struct signatured_type *sigt = *slot;
3014   struct dwarf2_per_cu_data ***datap = datum;
3015
3016   **datap = &sigt->per_cu;
3017   ++*datap;
3018
3019   return 1;
3020 }
3021
3022 /* Create the hash table of all entries in the .debug_types section.
3023    The result is zero if there is an error (e.g. missing .debug_types section),
3024    otherwise non-zero.  */
3025
3026 static int
3027 create_debug_types_hash_table (struct objfile *objfile)
3028 {
3029   gdb_byte *info_ptr;
3030   htab_t types_htab;
3031   struct dwarf2_per_cu_data **iter;
3032
3033   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3034   info_ptr = dwarf2_per_objfile->types.buffer;
3035
3036   if (info_ptr == NULL)
3037     {
3038       dwarf2_per_objfile->signatured_types = NULL;
3039       return 0;
3040     }
3041
3042   types_htab = allocate_signatured_type_table (objfile);
3043
3044   if (dwarf2_die_debug)
3045     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3046
3047   while (info_ptr < dwarf2_per_objfile->types.buffer
3048          + dwarf2_per_objfile->types.size)
3049     {
3050       unsigned int offset;
3051       unsigned int offset_size;
3052       unsigned int type_offset;
3053       unsigned int length, initial_length_size;
3054       unsigned short version;
3055       ULONGEST signature;
3056       struct signatured_type *type_sig;
3057       void **slot;
3058       gdb_byte *ptr = info_ptr;
3059
3060       offset = ptr - dwarf2_per_objfile->types.buffer;
3061
3062       /* We need to read the type's signature in order to build the hash
3063          table, but we don't need to read anything else just yet.  */
3064
3065       /* Sanity check to ensure entire cu is present.  */
3066       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3067       if (ptr + length + initial_length_size
3068           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3069         {
3070           complaint (&symfile_complaints,
3071                      _("debug type entry runs off end "
3072                        "of `.debug_types' section, ignored"));
3073           break;
3074         }
3075
3076       offset_size = initial_length_size == 4 ? 4 : 8;
3077       ptr += initial_length_size;
3078       version = bfd_get_16 (objfile->obfd, ptr);
3079       ptr += 2;
3080       ptr += offset_size; /* abbrev offset */
3081       ptr += 1; /* address size */
3082       signature = bfd_get_64 (objfile->obfd, ptr);
3083       ptr += 8;
3084       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3085
3086       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3087       memset (type_sig, 0, sizeof (*type_sig));
3088       type_sig->signature = signature;
3089       type_sig->offset = offset;
3090       type_sig->type_offset = type_offset;
3091       type_sig->per_cu.objfile = objfile;
3092       type_sig->per_cu.from_debug_types = 1;
3093
3094       slot = htab_find_slot (types_htab, type_sig, INSERT);
3095       gdb_assert (slot != NULL);
3096       *slot = type_sig;
3097
3098       if (dwarf2_die_debug)
3099         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3100                             offset, phex (signature, sizeof (signature)));
3101
3102       info_ptr = info_ptr + initial_length_size + length;
3103     }
3104
3105   dwarf2_per_objfile->signatured_types = types_htab;
3106
3107   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3108   dwarf2_per_objfile->type_comp_units
3109     = obstack_alloc (&objfile->objfile_obstack,
3110                      dwarf2_per_objfile->n_type_comp_units
3111                      * sizeof (struct dwarf2_per_cu_data *));
3112   iter = &dwarf2_per_objfile->type_comp_units[0];
3113   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3114   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3115               == dwarf2_per_objfile->n_type_comp_units);
3116
3117   return 1;
3118 }
3119
3120 /* Lookup a signature based type.
3121    Returns NULL if SIG is not present in the table.  */
3122
3123 static struct signatured_type *
3124 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3125 {
3126   struct signatured_type find_entry, *entry;
3127
3128   if (dwarf2_per_objfile->signatured_types == NULL)
3129     {
3130       complaint (&symfile_complaints,
3131                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3132       return 0;
3133     }
3134
3135   find_entry.signature = sig;
3136   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3137   return entry;
3138 }
3139
3140 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3141
3142 static void
3143 init_cu_die_reader (struct die_reader_specs *reader,
3144                     struct dwarf2_cu *cu)
3145 {
3146   reader->abfd = cu->objfile->obfd;
3147   reader->cu = cu;
3148   if (cu->per_cu->from_debug_types)
3149     {
3150       gdb_assert (dwarf2_per_objfile->types.readin);
3151       reader->buffer = dwarf2_per_objfile->types.buffer;
3152     }
3153   else
3154     {
3155       gdb_assert (dwarf2_per_objfile->info.readin);
3156       reader->buffer = dwarf2_per_objfile->info.buffer;
3157     }
3158 }
3159
3160 /* Find the base address of the compilation unit for range lists and
3161    location lists.  It will normally be specified by DW_AT_low_pc.
3162    In DWARF-3 draft 4, the base address could be overridden by
3163    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3164    compilation units with discontinuous ranges.  */
3165
3166 static void
3167 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3168 {
3169   struct attribute *attr;
3170
3171   cu->base_known = 0;
3172   cu->base_address = 0;
3173
3174   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3175   if (attr)
3176     {
3177       cu->base_address = DW_ADDR (attr);
3178       cu->base_known = 1;
3179     }
3180   else
3181     {
3182       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3183       if (attr)
3184         {
3185           cu->base_address = DW_ADDR (attr);
3186           cu->base_known = 1;
3187         }
3188     }
3189 }
3190
3191 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3192    to combine the common parts.
3193    Process a compilation unit for a psymtab.
3194    BUFFER is a pointer to the beginning of the dwarf section buffer,
3195    either .debug_info or debug_types.
3196    INFO_PTR is a pointer to the start of the CU.
3197    Returns a pointer to the next CU.  */
3198
3199 static gdb_byte *
3200 process_psymtab_comp_unit (struct objfile *objfile,
3201                            struct dwarf2_per_cu_data *this_cu,
3202                            gdb_byte *buffer, gdb_byte *info_ptr,
3203                            unsigned int buffer_size)
3204 {
3205   bfd *abfd = objfile->obfd;
3206   gdb_byte *beg_of_comp_unit = info_ptr;
3207   struct die_info *comp_unit_die;
3208   struct partial_symtab *pst;
3209   CORE_ADDR baseaddr;
3210   struct cleanup *back_to_inner;
3211   struct dwarf2_cu cu;
3212   int has_children, has_pc_info;
3213   struct attribute *attr;
3214   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3215   struct die_reader_specs reader_specs;
3216   const char *filename;
3217
3218   init_one_comp_unit (&cu, objfile);
3219   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3220
3221   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3222                                           buffer, buffer_size,
3223                                           abfd);
3224
3225   /* Complete the cu_header.  */
3226   cu.header.offset = beg_of_comp_unit - buffer;
3227   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3228
3229   cu.list_in_scope = &file_symbols;
3230
3231   /* If this compilation unit was already read in, free the
3232      cached copy in order to read it in again.  This is
3233      necessary because we skipped some symbols when we first
3234      read in the compilation unit (see load_partial_dies).
3235      This problem could be avoided, but the benefit is
3236      unclear.  */
3237   if (this_cu->cu != NULL)
3238     free_one_cached_comp_unit (this_cu->cu);
3239
3240   /* Note that this is a pointer to our stack frame, being
3241      added to a global data structure.  It will be cleaned up
3242      in free_stack_comp_unit when we finish with this
3243      compilation unit.  */
3244   this_cu->cu = &cu;
3245   cu.per_cu = this_cu;
3246
3247   /* Read the abbrevs for this compilation unit into a table.  */
3248   dwarf2_read_abbrevs (abfd, &cu);
3249   make_cleanup (dwarf2_free_abbrev_table, &cu);
3250
3251   /* Read the compilation unit die.  */
3252   if (this_cu->from_debug_types)
3253     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3254   init_cu_die_reader (&reader_specs, &cu);
3255   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3256                             &has_children);
3257
3258   if (this_cu->from_debug_types)
3259     {
3260       /* offset,length haven't been set yet for type units.  */
3261       this_cu->offset = cu.header.offset;
3262       this_cu->length = cu.header.length + cu.header.initial_length_size;
3263     }
3264   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3265     {
3266       info_ptr = (beg_of_comp_unit + cu.header.length
3267                   + cu.header.initial_length_size);
3268       do_cleanups (back_to_inner);
3269       return info_ptr;
3270     }
3271
3272   prepare_one_comp_unit (&cu, comp_unit_die);
3273
3274   /* Allocate a new partial symbol table structure.  */
3275   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3276   if (attr == NULL || !DW_STRING (attr))
3277     filename = "";
3278   else
3279     filename = DW_STRING (attr);
3280   pst = start_psymtab_common (objfile, objfile->section_offsets,
3281                               filename,
3282                               /* TEXTLOW and TEXTHIGH are set below.  */
3283                               0,
3284                               objfile->global_psymbols.next,
3285                               objfile->static_psymbols.next);
3286
3287   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3288   if (attr != NULL)
3289     pst->dirname = DW_STRING (attr);
3290
3291   pst->read_symtab_private = this_cu;
3292
3293   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3294
3295   /* Store the function that reads in the rest of the symbol table.  */
3296   pst->read_symtab = dwarf2_psymtab_to_symtab;
3297
3298   this_cu->v.psymtab = pst;
3299
3300   dwarf2_find_base_address (comp_unit_die, &cu);
3301
3302   /* Possibly set the default values of LOWPC and HIGHPC from
3303      `DW_AT_ranges'.  */
3304   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3305                                       &best_highpc, &cu, pst);
3306   if (has_pc_info == 1 && best_lowpc < best_highpc)
3307     /* Store the contiguous range if it is not empty; it can be empty for
3308        CUs with no code.  */
3309     addrmap_set_empty (objfile->psymtabs_addrmap,
3310                        best_lowpc + baseaddr,
3311                        best_highpc + baseaddr - 1, pst);
3312
3313   /* Check if comp unit has_children.
3314      If so, read the rest of the partial symbols from this comp unit.
3315      If not, there's no more debug_info for this comp unit.  */
3316   if (has_children)
3317     {
3318       struct partial_die_info *first_die;
3319       CORE_ADDR lowpc, highpc;
3320
3321       lowpc = ((CORE_ADDR) -1);
3322       highpc = ((CORE_ADDR) 0);
3323
3324       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3325
3326       scan_partial_symbols (first_die, &lowpc, &highpc,
3327                             ! has_pc_info, &cu);
3328
3329       /* If we didn't find a lowpc, set it to highpc to avoid
3330          complaints from `maint check'.  */
3331       if (lowpc == ((CORE_ADDR) -1))
3332         lowpc = highpc;
3333
3334       /* If the compilation unit didn't have an explicit address range,
3335          then use the information extracted from its child dies.  */
3336       if (! has_pc_info)
3337         {
3338           best_lowpc = lowpc;
3339           best_highpc = highpc;
3340         }
3341     }
3342   pst->textlow = best_lowpc + baseaddr;
3343   pst->texthigh = best_highpc + baseaddr;
3344
3345   pst->n_global_syms = objfile->global_psymbols.next -
3346     (objfile->global_psymbols.list + pst->globals_offset);
3347   pst->n_static_syms = objfile->static_psymbols.next -
3348     (objfile->static_psymbols.list + pst->statics_offset);
3349   sort_pst_symbols (pst);
3350
3351   info_ptr = (beg_of_comp_unit + cu.header.length
3352               + cu.header.initial_length_size);
3353
3354   if (this_cu->from_debug_types)
3355     {
3356       /* It's not clear we want to do anything with stmt lists here.
3357          Waiting to see what gcc ultimately does.  */
3358     }
3359   else
3360     {
3361       /* Get the list of files included in the current compilation unit,
3362          and build a psymtab for each of them.  */
3363       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3364     }
3365
3366   do_cleanups (back_to_inner);
3367
3368   return info_ptr;
3369 }
3370
3371 /* Traversal function for htab_traverse_noresize.
3372    Process one .debug_types comp-unit.  */
3373
3374 static int
3375 process_type_comp_unit (void **slot, void *info)
3376 {
3377   struct signatured_type *entry = (struct signatured_type *) *slot;
3378   struct objfile *objfile = (struct objfile *) info;
3379   struct dwarf2_per_cu_data *this_cu;
3380
3381   this_cu = &entry->per_cu;
3382
3383   gdb_assert (dwarf2_per_objfile->types.readin);
3384   process_psymtab_comp_unit (objfile, this_cu,
3385                              dwarf2_per_objfile->types.buffer,
3386                              dwarf2_per_objfile->types.buffer + entry->offset,
3387                              dwarf2_per_objfile->types.size);
3388
3389   return 1;
3390 }
3391
3392 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3393    Build partial symbol tables for the .debug_types comp-units.  */
3394
3395 static void
3396 build_type_psymtabs (struct objfile *objfile)
3397 {
3398   if (! create_debug_types_hash_table (objfile))
3399     return;
3400
3401   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3402                           process_type_comp_unit, objfile);
3403 }
3404
3405 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3406
3407 static void
3408 psymtabs_addrmap_cleanup (void *o)
3409 {
3410   struct objfile *objfile = o;
3411
3412   objfile->psymtabs_addrmap = NULL;
3413 }
3414
3415 /* Build the partial symbol table by doing a quick pass through the
3416    .debug_info and .debug_abbrev sections.  */
3417
3418 static void
3419 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3420 {
3421   gdb_byte *info_ptr;
3422   struct cleanup *back_to, *addrmap_cleanup;
3423   struct obstack temp_obstack;
3424
3425   dwarf2_per_objfile->reading_partial_symbols = 1;
3426
3427   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3428   info_ptr = dwarf2_per_objfile->info.buffer;
3429
3430   /* Any cached compilation units will be linked by the per-objfile
3431      read_in_chain.  Make sure to free them when we're done.  */
3432   back_to = make_cleanup (free_cached_comp_units, NULL);
3433
3434   build_type_psymtabs (objfile);
3435
3436   create_all_comp_units (objfile);
3437
3438   /* Create a temporary address map on a temporary obstack.  We later
3439      copy this to the final obstack.  */
3440   obstack_init (&temp_obstack);
3441   make_cleanup_obstack_free (&temp_obstack);
3442   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3443   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3444
3445   /* Since the objects we're extracting from .debug_info vary in
3446      length, only the individual functions to extract them (like
3447      read_comp_unit_head and load_partial_die) can really know whether
3448      the buffer is large enough to hold another complete object.
3449
3450      At the moment, they don't actually check that.  If .debug_info
3451      holds just one extra byte after the last compilation unit's dies,
3452      then read_comp_unit_head will happily read off the end of the
3453      buffer.  read_partial_die is similarly casual.  Those functions
3454      should be fixed.
3455
3456      For this loop condition, simply checking whether there's any data
3457      left at all should be sufficient.  */
3458
3459   while (info_ptr < (dwarf2_per_objfile->info.buffer
3460                      + dwarf2_per_objfile->info.size))
3461     {
3462       struct dwarf2_per_cu_data *this_cu;
3463
3464       this_cu = dwarf2_find_comp_unit (info_ptr
3465                                        - dwarf2_per_objfile->info.buffer,
3466                                        objfile);
3467
3468       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3469                                             dwarf2_per_objfile->info.buffer,
3470                                             info_ptr,
3471                                             dwarf2_per_objfile->info.size);
3472     }
3473
3474   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3475                                                     &objfile->objfile_obstack);
3476   discard_cleanups (addrmap_cleanup);
3477
3478   do_cleanups (back_to);
3479 }
3480
3481 /* Load the partial DIEs for a secondary CU into memory.  */
3482
3483 static void
3484 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3485                         struct objfile *objfile)
3486 {
3487   bfd *abfd = objfile->obfd;
3488   gdb_byte *info_ptr, *beg_of_comp_unit;
3489   struct die_info *comp_unit_die;
3490   struct dwarf2_cu *cu;
3491   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3492   int has_children;
3493   struct die_reader_specs reader_specs;
3494   int read_cu = 0;
3495
3496   gdb_assert (! this_cu->from_debug_types);
3497
3498   gdb_assert (dwarf2_per_objfile->info.readin);
3499   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3500   beg_of_comp_unit = info_ptr;
3501
3502   if (this_cu->cu == NULL)
3503     {
3504       cu = xmalloc (sizeof (*cu));
3505       init_one_comp_unit (cu, objfile);
3506
3507       read_cu = 1;
3508
3509       /* If an error occurs while loading, release our storage.  */
3510       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3511
3512       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3513                                               dwarf2_per_objfile->info.buffer,
3514                                               dwarf2_per_objfile->info.size,
3515                                               abfd);
3516
3517       /* Complete the cu_header.  */
3518       cu->header.offset = this_cu->offset;
3519       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3520
3521       /* Link this compilation unit into the compilation unit tree.  */
3522       this_cu->cu = cu;
3523       cu->per_cu = this_cu;
3524
3525       /* Link this CU into read_in_chain.  */
3526       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3527       dwarf2_per_objfile->read_in_chain = this_cu;
3528     }
3529   else
3530     {
3531       cu = this_cu->cu;
3532       info_ptr += cu->header.first_die_offset;
3533     }
3534
3535   /* Read the abbrevs for this compilation unit into a table.  */
3536   gdb_assert (cu->dwarf2_abbrevs == NULL);
3537   dwarf2_read_abbrevs (abfd, cu);
3538   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3539
3540   /* Read the compilation unit die.  */
3541   init_cu_die_reader (&reader_specs, cu);
3542   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3543                             &has_children);
3544
3545   prepare_one_comp_unit (cu, comp_unit_die);
3546
3547   /* Check if comp unit has_children.
3548      If so, read the rest of the partial symbols from this comp unit.
3549      If not, there's no more debug_info for this comp unit.  */
3550   if (has_children)
3551     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3552
3553   do_cleanups (free_abbrevs_cleanup);
3554
3555   if (read_cu)
3556     {
3557       /* We've successfully allocated this compilation unit.  Let our
3558          caller clean it up when finished with it.  */
3559       discard_cleanups (free_cu_cleanup);
3560     }
3561 }
3562
3563 /* Create a list of all compilation units in OBJFILE.  We do this only
3564    if an inter-comp-unit reference is found; presumably if there is one,
3565    there will be many, and one will occur early in the .debug_info section.
3566    So there's no point in building this list incrementally.  */
3567
3568 static void
3569 create_all_comp_units (struct objfile *objfile)
3570 {
3571   int n_allocated;
3572   int n_comp_units;
3573   struct dwarf2_per_cu_data **all_comp_units;
3574   gdb_byte *info_ptr;
3575
3576   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3577   info_ptr = dwarf2_per_objfile->info.buffer;
3578
3579   n_comp_units = 0;
3580   n_allocated = 10;
3581   all_comp_units = xmalloc (n_allocated
3582                             * sizeof (struct dwarf2_per_cu_data *));
3583
3584   while (info_ptr < dwarf2_per_objfile->info.buffer
3585          + dwarf2_per_objfile->info.size)
3586     {
3587       unsigned int length, initial_length_size;
3588       struct dwarf2_per_cu_data *this_cu;
3589       unsigned int offset;
3590
3591       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3592
3593       /* Read just enough information to find out where the next
3594          compilation unit is.  */
3595       length = read_initial_length (objfile->obfd, info_ptr,
3596                                     &initial_length_size);
3597
3598       /* Save the compilation unit for later lookup.  */
3599       this_cu = obstack_alloc (&objfile->objfile_obstack,
3600                                sizeof (struct dwarf2_per_cu_data));
3601       memset (this_cu, 0, sizeof (*this_cu));
3602       this_cu->offset = offset;
3603       this_cu->length = length + initial_length_size;
3604       this_cu->objfile = objfile;
3605
3606       if (n_comp_units == n_allocated)
3607         {
3608           n_allocated *= 2;
3609           all_comp_units = xrealloc (all_comp_units,
3610                                      n_allocated
3611                                      * sizeof (struct dwarf2_per_cu_data *));
3612         }
3613       all_comp_units[n_comp_units++] = this_cu;
3614
3615       info_ptr = info_ptr + this_cu->length;
3616     }
3617
3618   dwarf2_per_objfile->all_comp_units
3619     = obstack_alloc (&objfile->objfile_obstack,
3620                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3621   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3622           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3623   xfree (all_comp_units);
3624   dwarf2_per_objfile->n_comp_units = n_comp_units;
3625 }
3626
3627 /* Process all loaded DIEs for compilation unit CU, starting at
3628    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3629    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3630    DW_AT_ranges).  If NEED_PC is set, then this function will set
3631    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3632    and record the covered ranges in the addrmap.  */
3633
3634 static void
3635 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3636                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3637 {
3638   struct partial_die_info *pdi;
3639
3640   /* Now, march along the PDI's, descending into ones which have
3641      interesting children but skipping the children of the other ones,
3642      until we reach the end of the compilation unit.  */
3643
3644   pdi = first_die;
3645
3646   while (pdi != NULL)
3647     {
3648       fixup_partial_die (pdi, cu);
3649
3650       /* Anonymous namespaces or modules have no name but have interesting
3651          children, so we need to look at them.  Ditto for anonymous
3652          enums.  */
3653
3654       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3655           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3656         {
3657           switch (pdi->tag)
3658             {
3659             case DW_TAG_subprogram:
3660               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3661               break;
3662             case DW_TAG_constant:
3663             case DW_TAG_variable:
3664             case DW_TAG_typedef:
3665             case DW_TAG_union_type:
3666               if (!pdi->is_declaration)
3667                 {
3668                   add_partial_symbol (pdi, cu);
3669                 }
3670               break;
3671             case DW_TAG_class_type:
3672             case DW_TAG_interface_type:
3673             case DW_TAG_structure_type:
3674               if (!pdi->is_declaration)
3675                 {
3676                   add_partial_symbol (pdi, cu);
3677                 }
3678               break;
3679             case DW_TAG_enumeration_type:
3680               if (!pdi->is_declaration)
3681                 add_partial_enumeration (pdi, cu);
3682               break;
3683             case DW_TAG_base_type:
3684             case DW_TAG_subrange_type:
3685               /* File scope base type definitions are added to the partial
3686                  symbol table.  */
3687               add_partial_symbol (pdi, cu);
3688               break;
3689             case DW_TAG_namespace:
3690               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3691               break;
3692             case DW_TAG_module:
3693               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3694               break;
3695             default:
3696               break;
3697             }
3698         }
3699
3700       /* If the die has a sibling, skip to the sibling.  */
3701
3702       pdi = pdi->die_sibling;
3703     }
3704 }
3705
3706 /* Functions used to compute the fully scoped name of a partial DIE.
3707
3708    Normally, this is simple.  For C++, the parent DIE's fully scoped
3709    name is concatenated with "::" and the partial DIE's name.  For
3710    Java, the same thing occurs except that "." is used instead of "::".
3711    Enumerators are an exception; they use the scope of their parent
3712    enumeration type, i.e. the name of the enumeration type is not
3713    prepended to the enumerator.
3714
3715    There are two complexities.  One is DW_AT_specification; in this
3716    case "parent" means the parent of the target of the specification,
3717    instead of the direct parent of the DIE.  The other is compilers
3718    which do not emit DW_TAG_namespace; in this case we try to guess
3719    the fully qualified name of structure types from their members'
3720    linkage names.  This must be done using the DIE's children rather
3721    than the children of any DW_AT_specification target.  We only need
3722    to do this for structures at the top level, i.e. if the target of
3723    any DW_AT_specification (if any; otherwise the DIE itself) does not
3724    have a parent.  */
3725
3726 /* Compute the scope prefix associated with PDI's parent, in
3727    compilation unit CU.  The result will be allocated on CU's
3728    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3729    field.  NULL is returned if no prefix is necessary.  */
3730 static char *
3731 partial_die_parent_scope (struct partial_die_info *pdi,
3732                           struct dwarf2_cu *cu)
3733 {
3734   char *grandparent_scope;
3735   struct partial_die_info *parent, *real_pdi;
3736
3737   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3738      then this means the parent of the specification DIE.  */
3739
3740   real_pdi = pdi;
3741   while (real_pdi->has_specification)
3742     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3743
3744   parent = real_pdi->die_parent;
3745   if (parent == NULL)
3746     return NULL;
3747
3748   if (parent->scope_set)
3749     return parent->scope;
3750
3751   fixup_partial_die (parent, cu);
3752
3753   grandparent_scope = partial_die_parent_scope (parent, cu);
3754
3755   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3756      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3757      Work around this problem here.  */
3758   if (cu->language == language_cplus
3759       && parent->tag == DW_TAG_namespace
3760       && strcmp (parent->name, "::") == 0
3761       && grandparent_scope == NULL)
3762     {
3763       parent->scope = NULL;
3764       parent->scope_set = 1;
3765       return NULL;
3766     }
3767
3768   if (parent->tag == DW_TAG_namespace
3769       || parent->tag == DW_TAG_module
3770       || parent->tag == DW_TAG_structure_type
3771       || parent->tag == DW_TAG_class_type
3772       || parent->tag == DW_TAG_interface_type
3773       || parent->tag == DW_TAG_union_type
3774       || parent->tag == DW_TAG_enumeration_type)
3775     {
3776       if (grandparent_scope == NULL)
3777         parent->scope = parent->name;
3778       else
3779         parent->scope = typename_concat (&cu->comp_unit_obstack,
3780                                          grandparent_scope,
3781                                          parent->name, 0, cu);
3782     }
3783   else if (parent->tag == DW_TAG_enumerator)
3784     /* Enumerators should not get the name of the enumeration as a prefix.  */
3785     parent->scope = grandparent_scope;
3786   else
3787     {
3788       /* FIXME drow/2004-04-01: What should we be doing with
3789          function-local names?  For partial symbols, we should probably be
3790          ignoring them.  */
3791       complaint (&symfile_complaints,
3792                  _("unhandled containing DIE tag %d for DIE at %d"),
3793                  parent->tag, pdi->offset);
3794       parent->scope = grandparent_scope;
3795     }
3796
3797   parent->scope_set = 1;
3798   return parent->scope;
3799 }
3800
3801 /* Return the fully scoped name associated with PDI, from compilation unit
3802    CU.  The result will be allocated with malloc.  */
3803 static char *
3804 partial_die_full_name (struct partial_die_info *pdi,
3805                        struct dwarf2_cu *cu)
3806 {
3807   char *parent_scope;
3808
3809   /* If this is a template instantiation, we can not work out the
3810      template arguments from partial DIEs.  So, unfortunately, we have
3811      to go through the full DIEs.  At least any work we do building
3812      types here will be reused if full symbols are loaded later.  */
3813   if (pdi->has_template_arguments)
3814     {
3815       fixup_partial_die (pdi, cu);
3816
3817       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3818         {
3819           struct die_info *die;
3820           struct attribute attr;
3821           struct dwarf2_cu *ref_cu = cu;
3822
3823           attr.name = 0;
3824           attr.form = DW_FORM_ref_addr;
3825           attr.u.addr = pdi->offset;
3826           die = follow_die_ref (NULL, &attr, &ref_cu);
3827
3828           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3829         }
3830     }
3831
3832   parent_scope = partial_die_parent_scope (pdi, cu);
3833   if (parent_scope == NULL)
3834     return NULL;
3835   else
3836     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3837 }
3838
3839 static void
3840 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3841 {
3842   struct objfile *objfile = cu->objfile;
3843   CORE_ADDR addr = 0;
3844   char *actual_name = NULL;
3845   const struct partial_symbol *psym = NULL;
3846   CORE_ADDR baseaddr;
3847   int built_actual_name = 0;
3848
3849   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3850
3851   actual_name = partial_die_full_name (pdi, cu);
3852   if (actual_name)
3853     built_actual_name = 1;
3854
3855   if (actual_name == NULL)
3856     actual_name = pdi->name;
3857
3858   switch (pdi->tag)
3859     {
3860     case DW_TAG_subprogram:
3861       if (pdi->is_external || cu->language == language_ada)
3862         {
3863           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3864              of the global scope.  But in Ada, we want to be able to access
3865              nested procedures globally.  So all Ada subprograms are stored
3866              in the global scope.  */
3867           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3868              mst_text, objfile); */
3869           add_psymbol_to_list (actual_name, strlen (actual_name),
3870                                built_actual_name,
3871                                VAR_DOMAIN, LOC_BLOCK,
3872                                &objfile->global_psymbols,
3873                                0, pdi->lowpc + baseaddr,
3874                                cu->language, objfile);
3875         }
3876       else
3877         {
3878           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3879              mst_file_text, objfile); */
3880           add_psymbol_to_list (actual_name, strlen (actual_name),
3881                                built_actual_name,
3882                                VAR_DOMAIN, LOC_BLOCK,
3883                                &objfile->static_psymbols,
3884                                0, pdi->lowpc + baseaddr,
3885                                cu->language, objfile);
3886         }
3887       break;
3888     case DW_TAG_constant:
3889       {
3890         struct psymbol_allocation_list *list;
3891
3892         if (pdi->is_external)
3893           list = &objfile->global_psymbols;
3894         else
3895           list = &objfile->static_psymbols;
3896         add_psymbol_to_list (actual_name, strlen (actual_name),
3897                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3898                              list, 0, 0, cu->language, objfile);
3899       }
3900       break;
3901     case DW_TAG_variable:
3902       if (pdi->locdesc)
3903         addr = decode_locdesc (pdi->locdesc, cu);
3904
3905       if (pdi->locdesc
3906           && addr == 0
3907           && !dwarf2_per_objfile->has_section_at_zero)
3908         {
3909           /* A global or static variable may also have been stripped
3910              out by the linker if unused, in which case its address
3911              will be nullified; do not add such variables into partial
3912              symbol table then.  */
3913         }
3914       else if (pdi->is_external)
3915         {
3916           /* Global Variable.
3917              Don't enter into the minimal symbol tables as there is
3918              a minimal symbol table entry from the ELF symbols already.
3919              Enter into partial symbol table if it has a location
3920              descriptor or a type.
3921              If the location descriptor is missing, new_symbol will create
3922              a LOC_UNRESOLVED symbol, the address of the variable will then
3923              be determined from the minimal symbol table whenever the variable
3924              is referenced.
3925              The address for the partial symbol table entry is not
3926              used by GDB, but it comes in handy for debugging partial symbol
3927              table building.  */
3928
3929           if (pdi->locdesc || pdi->has_type)
3930             add_psymbol_to_list (actual_name, strlen (actual_name),
3931                                  built_actual_name,
3932                                  VAR_DOMAIN, LOC_STATIC,
3933                                  &objfile->global_psymbols,
3934                                  0, addr + baseaddr,
3935                                  cu->language, objfile);
3936         }
3937       else
3938         {
3939           /* Static Variable.  Skip symbols without location descriptors.  */
3940           if (pdi->locdesc == NULL)
3941             {
3942               if (built_actual_name)
3943                 xfree (actual_name);
3944               return;
3945             }
3946           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3947              mst_file_data, objfile); */
3948           add_psymbol_to_list (actual_name, strlen (actual_name),
3949                                built_actual_name,
3950                                VAR_DOMAIN, LOC_STATIC,
3951                                &objfile->static_psymbols,
3952                                0, addr + baseaddr,
3953                                cu->language, objfile);
3954         }
3955       break;
3956     case DW_TAG_typedef:
3957     case DW_TAG_base_type:
3958     case DW_TAG_subrange_type:
3959       add_psymbol_to_list (actual_name, strlen (actual_name),
3960                            built_actual_name,
3961                            VAR_DOMAIN, LOC_TYPEDEF,
3962                            &objfile->static_psymbols,
3963                            0, (CORE_ADDR) 0, cu->language, objfile);
3964       break;
3965     case DW_TAG_namespace:
3966       add_psymbol_to_list (actual_name, strlen (actual_name),
3967                            built_actual_name,
3968                            VAR_DOMAIN, LOC_TYPEDEF,
3969                            &objfile->global_psymbols,
3970                            0, (CORE_ADDR) 0, cu->language, objfile);
3971       break;
3972     case DW_TAG_class_type:
3973     case DW_TAG_interface_type:
3974     case DW_TAG_structure_type:
3975     case DW_TAG_union_type:
3976     case DW_TAG_enumeration_type:
3977       /* Skip external references.  The DWARF standard says in the section
3978          about "Structure, Union, and Class Type Entries": "An incomplete
3979          structure, union or class type is represented by a structure,
3980          union or class entry that does not have a byte size attribute
3981          and that has a DW_AT_declaration attribute."  */
3982       if (!pdi->has_byte_size && pdi->is_declaration)
3983         {
3984           if (built_actual_name)
3985             xfree (actual_name);
3986           return;
3987         }
3988
3989       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3990          static vs. global.  */
3991       add_psymbol_to_list (actual_name, strlen (actual_name),
3992                            built_actual_name,
3993                            STRUCT_DOMAIN, LOC_TYPEDEF,
3994                            (cu->language == language_cplus
3995                             || cu->language == language_java)
3996                            ? &objfile->global_psymbols
3997                            : &objfile->static_psymbols,
3998                            0, (CORE_ADDR) 0, cu->language, objfile);
3999
4000       break;
4001     case DW_TAG_enumerator:
4002       add_psymbol_to_list (actual_name, strlen (actual_name),
4003                            built_actual_name,
4004                            VAR_DOMAIN, LOC_CONST,
4005                            (cu->language == language_cplus
4006                             || cu->language == language_java)
4007                            ? &objfile->global_psymbols
4008                            : &objfile->static_psymbols,
4009                            0, (CORE_ADDR) 0, cu->language, objfile);
4010       break;
4011     default:
4012       break;
4013     }
4014
4015   if (built_actual_name)
4016     xfree (actual_name);
4017 }
4018
4019 /* Read a partial die corresponding to a namespace; also, add a symbol
4020    corresponding to that namespace to the symbol table.  NAMESPACE is
4021    the name of the enclosing namespace.  */
4022
4023 static void
4024 add_partial_namespace (struct partial_die_info *pdi,
4025                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4026                        int need_pc, struct dwarf2_cu *cu)
4027 {
4028   /* Add a symbol for the namespace.  */
4029
4030   add_partial_symbol (pdi, cu);
4031
4032   /* Now scan partial symbols in that namespace.  */
4033
4034   if (pdi->has_children)
4035     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4036 }
4037
4038 /* Read a partial die corresponding to a Fortran module.  */
4039
4040 static void
4041 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4042                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4043 {
4044   /* Now scan partial symbols in that module.  */
4045
4046   if (pdi->has_children)
4047     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4048 }
4049
4050 /* Read a partial die corresponding to a subprogram and create a partial
4051    symbol for that subprogram.  When the CU language allows it, this
4052    routine also defines a partial symbol for each nested subprogram
4053    that this subprogram contains.
4054
4055    DIE my also be a lexical block, in which case we simply search
4056    recursively for suprograms defined inside that lexical block.
4057    Again, this is only performed when the CU language allows this
4058    type of definitions.  */
4059
4060 static void
4061 add_partial_subprogram (struct partial_die_info *pdi,
4062                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4063                         int need_pc, struct dwarf2_cu *cu)
4064 {
4065   if (pdi->tag == DW_TAG_subprogram)
4066     {
4067       if (pdi->has_pc_info)
4068         {
4069           if (pdi->lowpc < *lowpc)
4070             *lowpc = pdi->lowpc;
4071           if (pdi->highpc > *highpc)
4072             *highpc = pdi->highpc;
4073           if (need_pc)
4074             {
4075               CORE_ADDR baseaddr;
4076               struct objfile *objfile = cu->objfile;
4077
4078               baseaddr = ANOFFSET (objfile->section_offsets,
4079                                    SECT_OFF_TEXT (objfile));
4080               addrmap_set_empty (objfile->psymtabs_addrmap,
4081                                  pdi->lowpc + baseaddr,
4082                                  pdi->highpc - 1 + baseaddr,
4083                                  cu->per_cu->v.psymtab);
4084             }
4085           if (!pdi->is_declaration)
4086             /* Ignore subprogram DIEs that do not have a name, they are
4087                illegal.  Do not emit a complaint at this point, we will
4088                do so when we convert this psymtab into a symtab.  */
4089             if (pdi->name)
4090               add_partial_symbol (pdi, cu);
4091         }
4092     }
4093
4094   if (! pdi->has_children)
4095     return;
4096
4097   if (cu->language == language_ada)
4098     {
4099       pdi = pdi->die_child;
4100       while (pdi != NULL)
4101         {
4102           fixup_partial_die (pdi, cu);
4103           if (pdi->tag == DW_TAG_subprogram
4104               || pdi->tag == DW_TAG_lexical_block)
4105             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4106           pdi = pdi->die_sibling;
4107         }
4108     }
4109 }
4110
4111 /* Read a partial die corresponding to an enumeration type.  */
4112
4113 static void
4114 add_partial_enumeration (struct partial_die_info *enum_pdi,
4115                          struct dwarf2_cu *cu)
4116 {
4117   struct partial_die_info *pdi;
4118
4119   if (enum_pdi->name != NULL)
4120     add_partial_symbol (enum_pdi, cu);
4121
4122   pdi = enum_pdi->die_child;
4123   while (pdi)
4124     {
4125       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4126         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4127       else
4128         add_partial_symbol (pdi, cu);
4129       pdi = pdi->die_sibling;
4130     }
4131 }
4132
4133 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4134    Return the corresponding abbrev, or NULL if the number is zero (indicating
4135    an empty DIE).  In either case *BYTES_READ will be set to the length of
4136    the initial number.  */
4137
4138 static struct abbrev_info *
4139 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4140                  struct dwarf2_cu *cu)
4141 {
4142   bfd *abfd = cu->objfile->obfd;
4143   unsigned int abbrev_number;
4144   struct abbrev_info *abbrev;
4145
4146   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4147
4148   if (abbrev_number == 0)
4149     return NULL;
4150
4151   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4152   if (!abbrev)
4153     {
4154       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4155              abbrev_number, bfd_get_filename (abfd));
4156     }
4157
4158   return abbrev;
4159 }
4160
4161 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4162    Returns a pointer to the end of a series of DIEs, terminated by an empty
4163    DIE.  Any children of the skipped DIEs will also be skipped.  */
4164
4165 static gdb_byte *
4166 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4167 {
4168   struct abbrev_info *abbrev;
4169   unsigned int bytes_read;
4170
4171   while (1)
4172     {
4173       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4174       if (abbrev == NULL)
4175         return info_ptr + bytes_read;
4176       else
4177         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4178     }
4179 }
4180
4181 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4182    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4183    abbrev corresponding to that skipped uleb128 should be passed in
4184    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4185    children.  */
4186
4187 static gdb_byte *
4188 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4189               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4190 {
4191   unsigned int bytes_read;
4192   struct attribute attr;
4193   bfd *abfd = cu->objfile->obfd;
4194   unsigned int form, i;
4195
4196   for (i = 0; i < abbrev->num_attrs; i++)
4197     {
4198       /* The only abbrev we care about is DW_AT_sibling.  */
4199       if (abbrev->attrs[i].name == DW_AT_sibling)
4200         {
4201           read_attribute (&attr, &abbrev->attrs[i],
4202                           abfd, info_ptr, cu);
4203           if (attr.form == DW_FORM_ref_addr)
4204             complaint (&symfile_complaints,
4205                        _("ignoring absolute DW_AT_sibling"));
4206           else
4207             return buffer + dwarf2_get_ref_die_offset (&attr);
4208         }
4209
4210       /* If it isn't DW_AT_sibling, skip this attribute.  */
4211       form = abbrev->attrs[i].form;
4212     skip_attribute:
4213       switch (form)
4214         {
4215         case DW_FORM_ref_addr:
4216           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4217              and later it is offset sized.  */
4218           if (cu->header.version == 2)
4219             info_ptr += cu->header.addr_size;
4220           else
4221             info_ptr += cu->header.offset_size;
4222           break;
4223         case DW_FORM_addr:
4224           info_ptr += cu->header.addr_size;
4225           break;
4226         case DW_FORM_data1:
4227         case DW_FORM_ref1:
4228         case DW_FORM_flag:
4229           info_ptr += 1;
4230           break;
4231         case DW_FORM_flag_present:
4232           break;
4233         case DW_FORM_data2:
4234         case DW_FORM_ref2:
4235           info_ptr += 2;
4236           break;
4237         case DW_FORM_data4:
4238         case DW_FORM_ref4:
4239           info_ptr += 4;
4240           break;
4241         case DW_FORM_data8:
4242         case DW_FORM_ref8:
4243         case DW_FORM_ref_sig8:
4244           info_ptr += 8;
4245           break;
4246         case DW_FORM_string:
4247           read_direct_string (abfd, info_ptr, &bytes_read);
4248           info_ptr += bytes_read;
4249           break;
4250         case DW_FORM_sec_offset:
4251         case DW_FORM_strp:
4252           info_ptr += cu->header.offset_size;
4253           break;
4254         case DW_FORM_exprloc:
4255         case DW_FORM_block:
4256           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4257           info_ptr += bytes_read;
4258           break;
4259         case DW_FORM_block1:
4260           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4261           break;
4262         case DW_FORM_block2:
4263           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4264           break;
4265         case DW_FORM_block4:
4266           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4267           break;
4268         case DW_FORM_sdata:
4269         case DW_FORM_udata:
4270         case DW_FORM_ref_udata:
4271           info_ptr = skip_leb128 (abfd, info_ptr);
4272           break;
4273         case DW_FORM_indirect:
4274           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4275           info_ptr += bytes_read;
4276           /* We need to continue parsing from here, so just go back to
4277              the top.  */
4278           goto skip_attribute;
4279
4280         default:
4281           error (_("Dwarf Error: Cannot handle %s "
4282                    "in DWARF reader [in module %s]"),
4283                  dwarf_form_name (form),
4284                  bfd_get_filename (abfd));
4285         }
4286     }
4287
4288   if (abbrev->has_children)
4289     return skip_children (buffer, info_ptr, cu);
4290   else
4291     return info_ptr;
4292 }
4293
4294 /* Locate ORIG_PDI's sibling.
4295    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4296    in BUFFER.  */
4297
4298 static gdb_byte *
4299 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4300                     gdb_byte *buffer, gdb_byte *info_ptr,
4301                     bfd *abfd, struct dwarf2_cu *cu)
4302 {
4303   /* Do we know the sibling already?  */
4304
4305   if (orig_pdi->sibling)
4306     return orig_pdi->sibling;
4307
4308   /* Are there any children to deal with?  */
4309
4310   if (!orig_pdi->has_children)
4311     return info_ptr;
4312
4313   /* Skip the children the long way.  */
4314
4315   return skip_children (buffer, info_ptr, cu);
4316 }
4317
4318 /* Expand this partial symbol table into a full symbol table.  */
4319
4320 static void
4321 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4322 {
4323   if (pst != NULL)
4324     {
4325       if (pst->readin)
4326         {
4327           warning (_("bug: psymtab for %s is already read in."),
4328                    pst->filename);
4329         }
4330       else
4331         {
4332           if (info_verbose)
4333             {
4334               printf_filtered (_("Reading in symbols for %s..."),
4335                                pst->filename);
4336               gdb_flush (gdb_stdout);
4337             }
4338
4339           /* Restore our global data.  */
4340           dwarf2_per_objfile = objfile_data (pst->objfile,
4341                                              dwarf2_objfile_data_key);
4342
4343           /* If this psymtab is constructed from a debug-only objfile, the
4344              has_section_at_zero flag will not necessarily be correct.  We
4345              can get the correct value for this flag by looking at the data
4346              associated with the (presumably stripped) associated objfile.  */
4347           if (pst->objfile->separate_debug_objfile_backlink)
4348             {
4349               struct dwarf2_per_objfile *dpo_backlink
4350                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4351                                 dwarf2_objfile_data_key);
4352
4353               dwarf2_per_objfile->has_section_at_zero
4354                 = dpo_backlink->has_section_at_zero;
4355             }
4356
4357           dwarf2_per_objfile->reading_partial_symbols = 0;
4358
4359           psymtab_to_symtab_1 (pst);
4360
4361           /* Finish up the debug error message.  */
4362           if (info_verbose)
4363             printf_filtered (_("done.\n"));
4364         }
4365     }
4366 }
4367
4368 /* Add PER_CU to the queue.  */
4369
4370 static void
4371 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4372 {
4373   struct dwarf2_queue_item *item;
4374
4375   per_cu->queued = 1;
4376   item = xmalloc (sizeof (*item));
4377   item->per_cu = per_cu;
4378   item->next = NULL;
4379
4380   if (dwarf2_queue == NULL)
4381     dwarf2_queue = item;
4382   else
4383     dwarf2_queue_tail->next = item;
4384
4385   dwarf2_queue_tail = item;
4386 }
4387
4388 /* Process the queue.  */
4389
4390 static void
4391 process_queue (struct objfile *objfile)
4392 {
4393   struct dwarf2_queue_item *item, *next_item;
4394
4395   /* The queue starts out with one item, but following a DIE reference
4396      may load a new CU, adding it to the end of the queue.  */
4397   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4398     {
4399       if (dwarf2_per_objfile->using_index
4400           ? !item->per_cu->v.quick->symtab
4401           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4402         process_full_comp_unit (item->per_cu);
4403
4404       item->per_cu->queued = 0;
4405       next_item = item->next;
4406       xfree (item);
4407     }
4408
4409   dwarf2_queue_tail = NULL;
4410 }
4411
4412 /* Free all allocated queue entries.  This function only releases anything if
4413    an error was thrown; if the queue was processed then it would have been
4414    freed as we went along.  */
4415
4416 static void
4417 dwarf2_release_queue (void *dummy)
4418 {
4419   struct dwarf2_queue_item *item, *last;
4420
4421   item = dwarf2_queue;
4422   while (item)
4423     {
4424       /* Anything still marked queued is likely to be in an
4425          inconsistent state, so discard it.  */
4426       if (item->per_cu->queued)
4427         {
4428           if (item->per_cu->cu != NULL)
4429             free_one_cached_comp_unit (item->per_cu->cu);
4430           item->per_cu->queued = 0;
4431         }
4432
4433       last = item;
4434       item = item->next;
4435       xfree (last);
4436     }
4437
4438   dwarf2_queue = dwarf2_queue_tail = NULL;
4439 }
4440
4441 /* Read in full symbols for PST, and anything it depends on.  */
4442
4443 static void
4444 psymtab_to_symtab_1 (struct partial_symtab *pst)
4445 {
4446   struct dwarf2_per_cu_data *per_cu;
4447   struct cleanup *back_to;
4448   int i;
4449
4450   for (i = 0; i < pst->number_of_dependencies; i++)
4451     if (!pst->dependencies[i]->readin)
4452       {
4453         /* Inform about additional files that need to be read in.  */
4454         if (info_verbose)
4455           {
4456             /* FIXME: i18n: Need to make this a single string.  */
4457             fputs_filtered (" ", gdb_stdout);
4458             wrap_here ("");
4459             fputs_filtered ("and ", gdb_stdout);
4460             wrap_here ("");
4461             printf_filtered ("%s...", pst->dependencies[i]->filename);
4462             wrap_here ("");     /* Flush output.  */
4463             gdb_flush (gdb_stdout);
4464           }
4465         psymtab_to_symtab_1 (pst->dependencies[i]);
4466       }
4467
4468   per_cu = pst->read_symtab_private;
4469
4470   if (per_cu == NULL)
4471     {
4472       /* It's an include file, no symbols to read for it.
4473          Everything is in the parent symtab.  */
4474       pst->readin = 1;
4475       return;
4476     }
4477
4478   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4479 }
4480
4481 /* Load the DIEs associated with PER_CU into memory.  */
4482
4483 static void
4484 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4485                      struct objfile *objfile)
4486 {
4487   bfd *abfd = objfile->obfd;
4488   struct dwarf2_cu *cu;
4489   unsigned int offset;
4490   gdb_byte *info_ptr, *beg_of_comp_unit;
4491   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4492   struct attribute *attr;
4493   int read_cu = 0;
4494
4495   gdb_assert (! per_cu->from_debug_types);
4496
4497   /* Set local variables from the partial symbol table info.  */
4498   offset = per_cu->offset;
4499
4500   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4501   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4502   beg_of_comp_unit = info_ptr;
4503
4504   if (per_cu->cu == NULL)
4505     {
4506       cu = xmalloc (sizeof (*cu));
4507       init_one_comp_unit (cu, objfile);
4508
4509       read_cu = 1;
4510
4511       /* If an error occurs while loading, release our storage.  */
4512       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4513
4514       /* Read in the comp_unit header.  */
4515       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4516
4517       /* Complete the cu_header.  */
4518       cu->header.offset = offset;
4519       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4520
4521       /* Read the abbrevs for this compilation unit.  */
4522       dwarf2_read_abbrevs (abfd, cu);
4523       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4524
4525       /* Link this compilation unit into the compilation unit tree.  */
4526       per_cu->cu = cu;
4527       cu->per_cu = per_cu;
4528
4529       /* Link this CU into read_in_chain.  */
4530       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4531       dwarf2_per_objfile->read_in_chain = per_cu;
4532     }
4533   else
4534     {
4535       cu = per_cu->cu;
4536       info_ptr += cu->header.first_die_offset;
4537     }
4538
4539   cu->dies = read_comp_unit (info_ptr, cu);
4540
4541   /* We try not to read any attributes in this function, because not
4542      all objfiles needed for references have been loaded yet, and symbol
4543      table processing isn't initialized.  But we have to set the CU language,
4544      or we won't be able to build types correctly.  */
4545   prepare_one_comp_unit (cu, cu->dies);
4546
4547   /* Similarly, if we do not read the producer, we can not apply
4548      producer-specific interpretation.  */
4549   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4550   if (attr)
4551     cu->producer = DW_STRING (attr);
4552
4553   if (read_cu)
4554     {
4555       do_cleanups (free_abbrevs_cleanup);
4556
4557       /* We've successfully allocated this compilation unit.  Let our
4558          caller clean it up when finished with it.  */
4559       discard_cleanups (free_cu_cleanup);
4560     }
4561 }
4562
4563 /* Add a DIE to the delayed physname list.  */
4564
4565 static void
4566 add_to_method_list (struct type *type, int fnfield_index, int index,
4567                     const char *name, struct die_info *die,
4568                     struct dwarf2_cu *cu)
4569 {
4570   struct delayed_method_info mi;
4571   mi.type = type;
4572   mi.fnfield_index = fnfield_index;
4573   mi.index = index;
4574   mi.name = name;
4575   mi.die = die;
4576   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4577 }
4578
4579 /* A cleanup for freeing the delayed method list.  */
4580
4581 static void
4582 free_delayed_list (void *ptr)
4583 {
4584   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4585   if (cu->method_list != NULL)
4586     {
4587       VEC_free (delayed_method_info, cu->method_list);
4588       cu->method_list = NULL;
4589     }
4590 }
4591
4592 /* Compute the physnames of any methods on the CU's method list.
4593
4594    The computation of method physnames is delayed in order to avoid the
4595    (bad) condition that one of the method's formal parameters is of an as yet
4596    incomplete type.  */
4597
4598 static void
4599 compute_delayed_physnames (struct dwarf2_cu *cu)
4600 {
4601   int i;
4602   struct delayed_method_info *mi;
4603   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4604     {
4605       char *physname;
4606       struct fn_fieldlist *fn_flp
4607         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4608       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4609       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4610     }
4611 }
4612
4613 /* Generate full symbol information for PST and CU, whose DIEs have
4614    already been loaded into memory.  */
4615
4616 static void
4617 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4618 {
4619   struct dwarf2_cu *cu = per_cu->cu;
4620   struct objfile *objfile = per_cu->objfile;
4621   CORE_ADDR lowpc, highpc;
4622   struct symtab *symtab;
4623   struct cleanup *back_to, *delayed_list_cleanup;
4624   CORE_ADDR baseaddr;
4625
4626   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4627
4628   buildsym_init ();
4629   back_to = make_cleanup (really_free_pendings, NULL);
4630   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4631
4632   cu->list_in_scope = &file_symbols;
4633
4634   dwarf2_find_base_address (cu->dies, cu);
4635
4636   /* Do line number decoding in read_file_scope () */
4637   process_die (cu->dies, cu);
4638
4639   /* Now that we have processed all the DIEs in the CU, all the types 
4640      should be complete, and it should now be safe to compute all of the
4641      physnames.  */
4642   compute_delayed_physnames (cu);
4643   do_cleanups (delayed_list_cleanup);
4644
4645   /* Some compilers don't define a DW_AT_high_pc attribute for the
4646      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4647      it, by scanning the DIE's below the compilation unit.  */
4648   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4649
4650   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4651
4652   /* Set symtab language to language from DW_AT_language.
4653      If the compilation is from a C file generated by language preprocessors,
4654      do not set the language if it was already deduced by start_subfile.  */
4655   if (symtab != NULL
4656       && !(cu->language == language_c && symtab->language != language_c))
4657     {
4658       symtab->language = cu->language;
4659     }
4660
4661   if (dwarf2_per_objfile->using_index)
4662     per_cu->v.quick->symtab = symtab;
4663   else
4664     {
4665       struct partial_symtab *pst = per_cu->v.psymtab;
4666       pst->symtab = symtab;
4667       pst->readin = 1;
4668     }
4669
4670   do_cleanups (back_to);
4671 }
4672
4673 /* Process a die and its children.  */
4674
4675 static void
4676 process_die (struct die_info *die, struct dwarf2_cu *cu)
4677 {
4678   switch (die->tag)
4679     {
4680     case DW_TAG_padding:
4681       break;
4682     case DW_TAG_compile_unit:
4683       read_file_scope (die, cu);
4684       break;
4685     case DW_TAG_type_unit:
4686       read_type_unit_scope (die, cu);
4687       break;
4688     case DW_TAG_subprogram:
4689     case DW_TAG_inlined_subroutine:
4690       read_func_scope (die, cu);
4691       break;
4692     case DW_TAG_lexical_block:
4693     case DW_TAG_try_block:
4694     case DW_TAG_catch_block:
4695       read_lexical_block_scope (die, cu);
4696       break;
4697     case DW_TAG_class_type:
4698     case DW_TAG_interface_type:
4699     case DW_TAG_structure_type:
4700     case DW_TAG_union_type:
4701       process_structure_scope (die, cu);
4702       break;
4703     case DW_TAG_enumeration_type:
4704       process_enumeration_scope (die, cu);
4705       break;
4706
4707     /* These dies have a type, but processing them does not create
4708        a symbol or recurse to process the children.  Therefore we can
4709        read them on-demand through read_type_die.  */
4710     case DW_TAG_subroutine_type:
4711     case DW_TAG_set_type:
4712     case DW_TAG_array_type:
4713     case DW_TAG_pointer_type:
4714     case DW_TAG_ptr_to_member_type:
4715     case DW_TAG_reference_type:
4716     case DW_TAG_string_type:
4717       break;
4718
4719     case DW_TAG_base_type:
4720     case DW_TAG_subrange_type:
4721     case DW_TAG_typedef:
4722       /* Add a typedef symbol for the type definition, if it has a
4723          DW_AT_name.  */
4724       new_symbol (die, read_type_die (die, cu), cu);
4725       break;
4726     case DW_TAG_common_block:
4727       read_common_block (die, cu);
4728       break;
4729     case DW_TAG_common_inclusion:
4730       break;
4731     case DW_TAG_namespace:
4732       processing_has_namespace_info = 1;
4733       read_namespace (die, cu);
4734       break;
4735     case DW_TAG_module:
4736       processing_has_namespace_info = 1;
4737       read_module (die, cu);
4738       break;
4739     case DW_TAG_imported_declaration:
4740     case DW_TAG_imported_module:
4741       processing_has_namespace_info = 1;
4742       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4743                                  || cu->language != language_fortran))
4744         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4745                    dwarf_tag_name (die->tag));
4746       read_import_statement (die, cu);
4747       break;
4748     default:
4749       new_symbol (die, NULL, cu);
4750       break;
4751     }
4752 }
4753
4754 /* A helper function for dwarf2_compute_name which determines whether DIE
4755    needs to have the name of the scope prepended to the name listed in the
4756    die.  */
4757
4758 static int
4759 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4760 {
4761   struct attribute *attr;
4762
4763   switch (die->tag)
4764     {
4765     case DW_TAG_namespace:
4766     case DW_TAG_typedef:
4767     case DW_TAG_class_type:
4768     case DW_TAG_interface_type:
4769     case DW_TAG_structure_type:
4770     case DW_TAG_union_type:
4771     case DW_TAG_enumeration_type:
4772     case DW_TAG_enumerator:
4773     case DW_TAG_subprogram:
4774     case DW_TAG_member:
4775       return 1;
4776
4777     case DW_TAG_variable:
4778     case DW_TAG_constant:
4779       /* We only need to prefix "globally" visible variables.  These include
4780          any variable marked with DW_AT_external or any variable that
4781          lives in a namespace.  [Variables in anonymous namespaces
4782          require prefixing, but they are not DW_AT_external.]  */
4783
4784       if (dwarf2_attr (die, DW_AT_specification, cu))
4785         {
4786           struct dwarf2_cu *spec_cu = cu;
4787
4788           return die_needs_namespace (die_specification (die, &spec_cu),
4789                                       spec_cu);
4790         }
4791
4792       attr = dwarf2_attr (die, DW_AT_external, cu);
4793       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4794           && die->parent->tag != DW_TAG_module)
4795         return 0;
4796       /* A variable in a lexical block of some kind does not need a
4797          namespace, even though in C++ such variables may be external
4798          and have a mangled name.  */
4799       if (die->parent->tag ==  DW_TAG_lexical_block
4800           || die->parent->tag ==  DW_TAG_try_block
4801           || die->parent->tag ==  DW_TAG_catch_block
4802           || die->parent->tag == DW_TAG_subprogram)
4803         return 0;
4804       return 1;
4805
4806     default:
4807       return 0;
4808     }
4809 }
4810
4811 /* Retrieve the last character from a mem_file.  */
4812
4813 static void
4814 do_ui_file_peek_last (void *object, const char *buffer, long length)
4815 {
4816   char *last_char_p = (char *) object;
4817
4818   if (length > 0)
4819     *last_char_p = buffer[length - 1];
4820 }
4821
4822 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4823    compute the physname for the object, which include a method's
4824    formal parameters (C++/Java) and return type (Java).
4825
4826    For Ada, return the DIE's linkage name rather than the fully qualified
4827    name.  PHYSNAME is ignored..
4828
4829    The result is allocated on the objfile_obstack and canonicalized.  */
4830
4831 static const char *
4832 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4833                      int physname)
4834 {
4835   if (name == NULL)
4836     name = dwarf2_name (die, cu);
4837
4838   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4839      compute it by typename_concat inside GDB.  */
4840   if (cu->language == language_ada
4841       || (cu->language == language_fortran && physname))
4842     {
4843       /* For Ada unit, we prefer the linkage name over the name, as
4844          the former contains the exported name, which the user expects
4845          to be able to reference.  Ideally, we want the user to be able
4846          to reference this entity using either natural or linkage name,
4847          but we haven't started looking at this enhancement yet.  */
4848       struct attribute *attr;
4849
4850       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4851       if (attr == NULL)
4852         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4853       if (attr && DW_STRING (attr))
4854         return DW_STRING (attr);
4855     }
4856
4857   /* These are the only languages we know how to qualify names in.  */
4858   if (name != NULL
4859       && (cu->language == language_cplus || cu->language == language_java
4860           || cu->language == language_fortran))
4861     {
4862       if (die_needs_namespace (die, cu))
4863         {
4864           long length;
4865           char *prefix;
4866           struct ui_file *buf;
4867
4868           prefix = determine_prefix (die, cu);
4869           buf = mem_fileopen ();
4870           if (*prefix != '\0')
4871             {
4872               char *prefixed_name = typename_concat (NULL, prefix, name,
4873                                                      physname, cu);
4874
4875               fputs_unfiltered (prefixed_name, buf);
4876               xfree (prefixed_name);
4877             }
4878           else
4879             fputs_unfiltered (name, buf);
4880
4881           /* Template parameters may be specified in the DIE's DW_AT_name, or
4882              as children with DW_TAG_template_type_param or
4883              DW_TAG_value_type_param.  If the latter, add them to the name
4884              here.  If the name already has template parameters, then
4885              skip this step; some versions of GCC emit both, and
4886              it is more efficient to use the pre-computed name.
4887
4888              Something to keep in mind about this process: it is very
4889              unlikely, or in some cases downright impossible, to produce
4890              something that will match the mangled name of a function.
4891              If the definition of the function has the same debug info,
4892              we should be able to match up with it anyway.  But fallbacks
4893              using the minimal symbol, for instance to find a method
4894              implemented in a stripped copy of libstdc++, will not work.
4895              If we do not have debug info for the definition, we will have to
4896              match them up some other way.
4897
4898              When we do name matching there is a related problem with function
4899              templates; two instantiated function templates are allowed to
4900              differ only by their return types, which we do not add here.  */
4901
4902           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4903             {
4904               struct attribute *attr;
4905               struct die_info *child;
4906               int first = 1;
4907
4908               die->building_fullname = 1;
4909
4910               for (child = die->child; child != NULL; child = child->sibling)
4911                 {
4912                   struct type *type;
4913                   long value;
4914                   gdb_byte *bytes;
4915                   struct dwarf2_locexpr_baton *baton;
4916                   struct value *v;
4917
4918                   if (child->tag != DW_TAG_template_type_param
4919                       && child->tag != DW_TAG_template_value_param)
4920                     continue;
4921
4922                   if (first)
4923                     {
4924                       fputs_unfiltered ("<", buf);
4925                       first = 0;
4926                     }
4927                   else
4928                     fputs_unfiltered (", ", buf);
4929
4930                   attr = dwarf2_attr (child, DW_AT_type, cu);
4931                   if (attr == NULL)
4932                     {
4933                       complaint (&symfile_complaints,
4934                                  _("template parameter missing DW_AT_type"));
4935                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4936                       continue;
4937                     }
4938                   type = die_type (child, cu);
4939
4940                   if (child->tag == DW_TAG_template_type_param)
4941                     {
4942                       c_print_type (type, "", buf, -1, 0);
4943                       continue;
4944                     }
4945
4946                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4947                   if (attr == NULL)
4948                     {
4949                       complaint (&symfile_complaints,
4950                                  _("template parameter missing "
4951                                    "DW_AT_const_value"));
4952                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4953                       continue;
4954                     }
4955
4956                   dwarf2_const_value_attr (attr, type, name,
4957                                            &cu->comp_unit_obstack, cu,
4958                                            &value, &bytes, &baton);
4959
4960                   if (TYPE_NOSIGN (type))
4961                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4962                        changed, this can use value_print instead.  */
4963                     c_printchar (value, type, buf);
4964                   else
4965                     {
4966                       struct value_print_options opts;
4967
4968                       if (baton != NULL)
4969                         v = dwarf2_evaluate_loc_desc (type, NULL,
4970                                                       baton->data,
4971                                                       baton->size,
4972                                                       baton->per_cu);
4973                       else if (bytes != NULL)
4974                         {
4975                           v = allocate_value (type);
4976                           memcpy (value_contents_writeable (v), bytes,
4977                                   TYPE_LENGTH (type));
4978                         }
4979                       else
4980                         v = value_from_longest (type, value);
4981
4982                       /* Specify decimal so that we do not depend on
4983                          the radix.  */
4984                       get_formatted_print_options (&opts, 'd');
4985                       opts.raw = 1;
4986                       value_print (v, buf, &opts);
4987                       release_value (v);
4988                       value_free (v);
4989                     }
4990                 }
4991
4992               die->building_fullname = 0;
4993
4994               if (!first)
4995                 {
4996                   /* Close the argument list, with a space if necessary
4997                      (nested templates).  */
4998                   char last_char = '\0';
4999                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5000                   if (last_char == '>')
5001                     fputs_unfiltered (" >", buf);
5002                   else
5003                     fputs_unfiltered (">", buf);
5004                 }
5005             }
5006
5007           /* For Java and C++ methods, append formal parameter type
5008              information, if PHYSNAME.  */
5009
5010           if (physname && die->tag == DW_TAG_subprogram
5011               && (cu->language == language_cplus
5012                   || cu->language == language_java))
5013             {
5014               struct type *type = read_type_die (die, cu);
5015
5016               c_type_print_args (type, buf, 1, cu->language);
5017
5018               if (cu->language == language_java)
5019                 {
5020                   /* For java, we must append the return type to method
5021                      names.  */
5022                   if (die->tag == DW_TAG_subprogram)
5023                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5024                                      0, 0);
5025                 }
5026               else if (cu->language == language_cplus)
5027                 {
5028                   /* Assume that an artificial first parameter is
5029                      "this", but do not crash if it is not.  RealView
5030                      marks unnamed (and thus unused) parameters as
5031                      artificial; there is no way to differentiate
5032                      the two cases.  */
5033                   if (TYPE_NFIELDS (type) > 0
5034                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5035                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5036                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5037                                                                         0))))
5038                     fputs_unfiltered (" const", buf);
5039                 }
5040             }
5041
5042           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5043                                        &length);
5044           ui_file_delete (buf);
5045
5046           if (cu->language == language_cplus)
5047             {
5048               char *cname
5049                 = dwarf2_canonicalize_name (name, cu,
5050                                             &cu->objfile->objfile_obstack);
5051
5052               if (cname != NULL)
5053                 name = cname;
5054             }
5055         }
5056     }
5057
5058   return name;
5059 }
5060
5061 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5062    If scope qualifiers are appropriate they will be added.  The result
5063    will be allocated on the objfile_obstack, or NULL if the DIE does
5064    not have a name.  NAME may either be from a previous call to
5065    dwarf2_name or NULL.
5066
5067    The output string will be canonicalized (if C++/Java).  */
5068
5069 static const char *
5070 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5071 {
5072   return dwarf2_compute_name (name, die, cu, 0);
5073 }
5074
5075 /* Construct a physname for the given DIE in CU.  NAME may either be
5076    from a previous call to dwarf2_name or NULL.  The result will be
5077    allocated on the objfile_objstack or NULL if the DIE does not have a
5078    name.
5079
5080    The output string will be canonicalized (if C++/Java).  */
5081
5082 static const char *
5083 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5084 {
5085   return dwarf2_compute_name (name, die, cu, 1);
5086 }
5087
5088 /* Read the import statement specified by the given die and record it.  */
5089
5090 static void
5091 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5092 {
5093   struct attribute *import_attr;
5094   struct die_info *imported_die;
5095   struct dwarf2_cu *imported_cu;
5096   const char *imported_name;
5097   const char *imported_name_prefix;
5098   const char *canonical_name;
5099   const char *import_alias;
5100   const char *imported_declaration = NULL;
5101   const char *import_prefix;
5102
5103   char *temp;
5104
5105   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5106   if (import_attr == NULL)
5107     {
5108       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5109                  dwarf_tag_name (die->tag));
5110       return;
5111     }
5112
5113   imported_cu = cu;
5114   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5115   imported_name = dwarf2_name (imported_die, imported_cu);
5116   if (imported_name == NULL)
5117     {
5118       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5119
5120         The import in the following code:
5121         namespace A
5122           {
5123             typedef int B;
5124           }
5125
5126         int main ()
5127           {
5128             using A::B;
5129             B b;
5130             return b;
5131           }
5132
5133         ...
5134          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5135             <52>   DW_AT_decl_file   : 1
5136             <53>   DW_AT_decl_line   : 6
5137             <54>   DW_AT_import      : <0x75>
5138          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5139             <59>   DW_AT_name        : B
5140             <5b>   DW_AT_decl_file   : 1
5141             <5c>   DW_AT_decl_line   : 2
5142             <5d>   DW_AT_type        : <0x6e>
5143         ...
5144          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5145             <76>   DW_AT_byte_size   : 4
5146             <77>   DW_AT_encoding    : 5        (signed)
5147
5148         imports the wrong die ( 0x75 instead of 0x58 ).
5149         This case will be ignored until the gcc bug is fixed.  */
5150       return;
5151     }
5152
5153   /* Figure out the local name after import.  */
5154   import_alias = dwarf2_name (die, cu);
5155
5156   /* Figure out where the statement is being imported to.  */
5157   import_prefix = determine_prefix (die, cu);
5158
5159   /* Figure out what the scope of the imported die is and prepend it
5160      to the name of the imported die.  */
5161   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5162
5163   if (imported_die->tag != DW_TAG_namespace
5164       && imported_die->tag != DW_TAG_module)
5165     {
5166       imported_declaration = imported_name;
5167       canonical_name = imported_name_prefix;
5168     }
5169   else if (strlen (imported_name_prefix) > 0)
5170     {
5171       temp = alloca (strlen (imported_name_prefix)
5172                      + 2 + strlen (imported_name) + 1);
5173       strcpy (temp, imported_name_prefix);
5174       strcat (temp, "::");
5175       strcat (temp, imported_name);
5176       canonical_name = temp;
5177     }
5178   else
5179     canonical_name = imported_name;
5180
5181   cp_add_using_directive (import_prefix,
5182                           canonical_name,
5183                           import_alias,
5184                           imported_declaration,
5185                           &cu->objfile->objfile_obstack);
5186 }
5187
5188 static void
5189 initialize_cu_func_list (struct dwarf2_cu *cu)
5190 {
5191   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5192 }
5193
5194 /* Cleanup function for read_file_scope.  */
5195
5196 static void
5197 free_cu_line_header (void *arg)
5198 {
5199   struct dwarf2_cu *cu = arg;
5200
5201   free_line_header (cu->line_header);
5202   cu->line_header = NULL;
5203 }
5204
5205 static void
5206 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5207                          char **name, char **comp_dir)
5208 {
5209   struct attribute *attr;
5210
5211   *name = NULL;
5212   *comp_dir = NULL;
5213
5214   /* Find the filename.  Do not use dwarf2_name here, since the filename
5215      is not a source language identifier.  */
5216   attr = dwarf2_attr (die, DW_AT_name, cu);
5217   if (attr)
5218     {
5219       *name = DW_STRING (attr);
5220     }
5221
5222   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5223   if (attr)
5224     *comp_dir = DW_STRING (attr);
5225   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5226     {
5227       *comp_dir = ldirname (*name);
5228       if (*comp_dir != NULL)
5229         make_cleanup (xfree, *comp_dir);
5230     }
5231   if (*comp_dir != NULL)
5232     {
5233       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5234          directory, get rid of it.  */
5235       char *cp = strchr (*comp_dir, ':');
5236
5237       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5238         *comp_dir = cp + 1;
5239     }
5240
5241   if (*name == NULL)
5242     *name = "<unknown>";
5243 }
5244
5245 /* Process DW_TAG_compile_unit.  */
5246
5247 static void
5248 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5249 {
5250   struct objfile *objfile = cu->objfile;
5251   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5252   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5253   CORE_ADDR highpc = ((CORE_ADDR) 0);
5254   struct attribute *attr;
5255   char *name = NULL;
5256   char *comp_dir = NULL;
5257   struct die_info *child_die;
5258   bfd *abfd = objfile->obfd;
5259   struct line_header *line_header = 0;
5260   CORE_ADDR baseaddr;
5261
5262   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5263
5264   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5265
5266   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5267      from finish_block.  */
5268   if (lowpc == ((CORE_ADDR) -1))
5269     lowpc = highpc;
5270   lowpc += baseaddr;
5271   highpc += baseaddr;
5272
5273   find_file_and_directory (die, cu, &name, &comp_dir);
5274
5275   attr = dwarf2_attr (die, DW_AT_language, cu);
5276   if (attr)
5277     {
5278       set_cu_language (DW_UNSND (attr), cu);
5279     }
5280
5281   attr = dwarf2_attr (die, DW_AT_producer, cu);
5282   if (attr)
5283     cu->producer = DW_STRING (attr);
5284
5285   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5286      standardised yet.  As a workaround for the language detection we fall
5287      back to the DW_AT_producer string.  */
5288   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5289     cu->language = language_opencl;
5290
5291   /* We assume that we're processing GCC output.  */
5292   processing_gcc_compilation = 2;
5293
5294   processing_has_namespace_info = 0;
5295
5296   start_symtab (name, comp_dir, lowpc);
5297   record_debugformat ("DWARF 2");
5298   record_producer (cu->producer);
5299
5300   initialize_cu_func_list (cu);
5301
5302   /* Decode line number information if present.  We do this before
5303      processing child DIEs, so that the line header table is available
5304      for DW_AT_decl_file.  */
5305   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5306   if (attr)
5307     {
5308       unsigned int line_offset = DW_UNSND (attr);
5309       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5310       if (line_header)
5311         {
5312           cu->line_header = line_header;
5313           make_cleanup (free_cu_line_header, cu);
5314           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5315         }
5316     }
5317
5318   /* Process all dies in compilation unit.  */
5319   if (die->child != NULL)
5320     {
5321       child_die = die->child;
5322       while (child_die && child_die->tag)
5323         {
5324           process_die (child_die, cu);
5325           child_die = sibling_die (child_die);
5326         }
5327     }
5328
5329   /* Decode macro information, if present.  Dwarf 2 macro information
5330      refers to information in the line number info statement program
5331      header, so we can only read it if we've read the header
5332      successfully.  */
5333   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5334   if (attr && line_header)
5335     {
5336       unsigned int macro_offset = DW_UNSND (attr);
5337
5338       dwarf_decode_macros (line_header, macro_offset,
5339                            comp_dir, abfd, cu);
5340     }
5341   do_cleanups (back_to);
5342 }
5343
5344 /* Process DW_TAG_type_unit.
5345    For TUs we want to skip the first top level sibling if it's not the
5346    actual type being defined by this TU.  In this case the first top
5347    level sibling is there to provide context only.  */
5348
5349 static void
5350 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5351 {
5352   struct objfile *objfile = cu->objfile;
5353   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5354   CORE_ADDR lowpc;
5355   struct attribute *attr;
5356   char *name = NULL;
5357   char *comp_dir = NULL;
5358   struct die_info *child_die;
5359   bfd *abfd = objfile->obfd;
5360
5361   /* start_symtab needs a low pc, but we don't really have one.
5362      Do what read_file_scope would do in the absence of such info.  */
5363   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5364
5365   /* Find the filename.  Do not use dwarf2_name here, since the filename
5366      is not a source language identifier.  */
5367   attr = dwarf2_attr (die, DW_AT_name, cu);
5368   if (attr)
5369     name = DW_STRING (attr);
5370
5371   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5372   if (attr)
5373     comp_dir = DW_STRING (attr);
5374   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5375     {
5376       comp_dir = ldirname (name);
5377       if (comp_dir != NULL)
5378         make_cleanup (xfree, comp_dir);
5379     }
5380
5381   if (name == NULL)
5382     name = "<unknown>";
5383
5384   attr = dwarf2_attr (die, DW_AT_language, cu);
5385   if (attr)
5386     set_cu_language (DW_UNSND (attr), cu);
5387
5388   /* This isn't technically needed today.  It is done for symmetry
5389      with read_file_scope.  */
5390   attr = dwarf2_attr (die, DW_AT_producer, cu);
5391   if (attr)
5392     cu->producer = DW_STRING (attr);
5393
5394   /* We assume that we're processing GCC output.  */
5395   processing_gcc_compilation = 2;
5396
5397   processing_has_namespace_info = 0;
5398
5399   start_symtab (name, comp_dir, lowpc);
5400   record_debugformat ("DWARF 2");
5401   record_producer (cu->producer);
5402
5403   /* Process the dies in the type unit.  */
5404   if (die->child == NULL)
5405     {
5406       dump_die_for_error (die);
5407       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5408              bfd_get_filename (abfd));
5409     }
5410
5411   child_die = die->child;
5412
5413   while (child_die && child_die->tag)
5414     {
5415       process_die (child_die, cu);
5416
5417       child_die = sibling_die (child_die);
5418     }
5419
5420   do_cleanups (back_to);
5421 }
5422
5423 static void
5424 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5425                      struct dwarf2_cu *cu)
5426 {
5427   struct function_range *thisfn;
5428
5429   thisfn = (struct function_range *)
5430     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5431   thisfn->name = name;
5432   thisfn->lowpc = lowpc;
5433   thisfn->highpc = highpc;
5434   thisfn->seen_line = 0;
5435   thisfn->next = NULL;
5436
5437   if (cu->last_fn == NULL)
5438       cu->first_fn = thisfn;
5439   else
5440       cu->last_fn->next = thisfn;
5441
5442   cu->last_fn = thisfn;
5443 }
5444
5445 /* qsort helper for inherit_abstract_dies.  */
5446
5447 static int
5448 unsigned_int_compar (const void *ap, const void *bp)
5449 {
5450   unsigned int a = *(unsigned int *) ap;
5451   unsigned int b = *(unsigned int *) bp;
5452
5453   return (a > b) - (b > a);
5454 }
5455
5456 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5457    Inherit only the children of the DW_AT_abstract_origin DIE not being
5458    already referenced by DW_AT_abstract_origin from the children of the
5459    current DIE.  */
5460
5461 static void
5462 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5463 {
5464   struct die_info *child_die;
5465   unsigned die_children_count;
5466   /* CU offsets which were referenced by children of the current DIE.  */
5467   unsigned *offsets;
5468   unsigned *offsets_end, *offsetp;
5469   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5470   struct die_info *origin_die;
5471   /* Iterator of the ORIGIN_DIE children.  */
5472   struct die_info *origin_child_die;
5473   struct cleanup *cleanups;
5474   struct attribute *attr;
5475   struct dwarf2_cu *origin_cu;
5476   struct pending **origin_previous_list_in_scope;
5477
5478   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5479   if (!attr)
5480     return;
5481
5482   /* Note that following die references may follow to a die in a
5483      different cu.  */
5484
5485   origin_cu = cu;
5486   origin_die = follow_die_ref (die, attr, &origin_cu);
5487
5488   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5489      symbols in.  */
5490   origin_previous_list_in_scope = origin_cu->list_in_scope;
5491   origin_cu->list_in_scope = cu->list_in_scope;
5492
5493   if (die->tag != origin_die->tag
5494       && !(die->tag == DW_TAG_inlined_subroutine
5495            && origin_die->tag == DW_TAG_subprogram))
5496     complaint (&symfile_complaints,
5497                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5498                die->offset, origin_die->offset);
5499
5500   child_die = die->child;
5501   die_children_count = 0;
5502   while (child_die && child_die->tag)
5503     {
5504       child_die = sibling_die (child_die);
5505       die_children_count++;
5506     }
5507   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5508   cleanups = make_cleanup (xfree, offsets);
5509
5510   offsets_end = offsets;
5511   child_die = die->child;
5512   while (child_die && child_die->tag)
5513     {
5514       /* For each CHILD_DIE, find the corresponding child of
5515          ORIGIN_DIE.  If there is more than one layer of
5516          DW_AT_abstract_origin, follow them all; there shouldn't be,
5517          but GCC versions at least through 4.4 generate this (GCC PR
5518          40573).  */
5519       struct die_info *child_origin_die = child_die;
5520       struct dwarf2_cu *child_origin_cu = cu;
5521
5522       while (1)
5523         {
5524           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5525                               child_origin_cu);
5526           if (attr == NULL)
5527             break;
5528           child_origin_die = follow_die_ref (child_origin_die, attr,
5529                                              &child_origin_cu);
5530         }
5531
5532       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5533          counterpart may exist.  */
5534       if (child_origin_die != child_die)
5535         {
5536           if (child_die->tag != child_origin_die->tag
5537               && !(child_die->tag == DW_TAG_inlined_subroutine
5538                    && child_origin_die->tag == DW_TAG_subprogram))
5539             complaint (&symfile_complaints,
5540                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5541                          "different tags"), child_die->offset,
5542                        child_origin_die->offset);
5543           if (child_origin_die->parent != origin_die)
5544             complaint (&symfile_complaints,
5545                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5546                          "different parents"), child_die->offset,
5547                        child_origin_die->offset);
5548           else
5549             *offsets_end++ = child_origin_die->offset;
5550         }
5551       child_die = sibling_die (child_die);
5552     }
5553   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5554          unsigned_int_compar);
5555   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5556     if (offsetp[-1] == *offsetp)
5557       complaint (&symfile_complaints,
5558                  _("Multiple children of DIE 0x%x refer "
5559                    "to DIE 0x%x as their abstract origin"),
5560                  die->offset, *offsetp);
5561
5562   offsetp = offsets;
5563   origin_child_die = origin_die->child;
5564   while (origin_child_die && origin_child_die->tag)
5565     {
5566       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5567       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5568         offsetp++;
5569       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5570         {
5571           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5572           process_die (origin_child_die, origin_cu);
5573         }
5574       origin_child_die = sibling_die (origin_child_die);
5575     }
5576   origin_cu->list_in_scope = origin_previous_list_in_scope;
5577
5578   do_cleanups (cleanups);
5579 }
5580
5581 static void
5582 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5583 {
5584   struct objfile *objfile = cu->objfile;
5585   struct context_stack *new;
5586   CORE_ADDR lowpc;
5587   CORE_ADDR highpc;
5588   struct die_info *child_die;
5589   struct attribute *attr, *call_line, *call_file;
5590   char *name;
5591   CORE_ADDR baseaddr;
5592   struct block *block;
5593   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5594   VEC (symbolp) *template_args = NULL;
5595   struct template_symbol *templ_func = NULL;
5596
5597   if (inlined_func)
5598     {
5599       /* If we do not have call site information, we can't show the
5600          caller of this inlined function.  That's too confusing, so
5601          only use the scope for local variables.  */
5602       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5603       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5604       if (call_line == NULL || call_file == NULL)
5605         {
5606           read_lexical_block_scope (die, cu);
5607           return;
5608         }
5609     }
5610
5611   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5612
5613   name = dwarf2_name (die, cu);
5614
5615   /* Ignore functions with missing or empty names.  These are actually
5616      illegal according to the DWARF standard.  */
5617   if (name == NULL)
5618     {
5619       complaint (&symfile_complaints,
5620                  _("missing name for subprogram DIE at %d"), die->offset);
5621       return;
5622     }
5623
5624   /* Ignore functions with missing or invalid low and high pc attributes.  */
5625   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5626     {
5627       attr = dwarf2_attr (die, DW_AT_external, cu);
5628       if (!attr || !DW_UNSND (attr))
5629         complaint (&symfile_complaints,
5630                    _("cannot get low and high bounds "
5631                      "for subprogram DIE at %d"),
5632                    die->offset);
5633       return;
5634     }
5635
5636   lowpc += baseaddr;
5637   highpc += baseaddr;
5638
5639   /* Record the function range for dwarf_decode_lines.  */
5640   add_to_cu_func_list (name, lowpc, highpc, cu);
5641
5642   /* If we have any template arguments, then we must allocate a
5643      different sort of symbol.  */
5644   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5645     {
5646       if (child_die->tag == DW_TAG_template_type_param
5647           || child_die->tag == DW_TAG_template_value_param)
5648         {
5649           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5650                                        struct template_symbol);
5651           templ_func->base.is_cplus_template_function = 1;
5652           break;
5653         }
5654     }
5655
5656   new = push_context (0, lowpc);
5657   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5658                                (struct symbol *) templ_func);
5659
5660   /* If there is a location expression for DW_AT_frame_base, record
5661      it.  */
5662   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5663   if (attr)
5664     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5665        expression is being recorded directly in the function's symbol
5666        and not in a separate frame-base object.  I guess this hack is
5667        to avoid adding some sort of frame-base adjunct/annex to the
5668        function's symbol :-(.  The problem with doing this is that it
5669        results in a function symbol with a location expression that
5670        has nothing to do with the location of the function, ouch!  The
5671        relationship should be: a function's symbol has-a frame base; a
5672        frame-base has-a location expression.  */
5673     dwarf2_symbol_mark_computed (attr, new->name, cu);
5674
5675   cu->list_in_scope = &local_symbols;
5676
5677   if (die->child != NULL)
5678     {
5679       child_die = die->child;
5680       while (child_die && child_die->tag)
5681         {
5682           if (child_die->tag == DW_TAG_template_type_param
5683               || child_die->tag == DW_TAG_template_value_param)
5684             {
5685               struct symbol *arg = new_symbol (child_die, NULL, cu);
5686
5687               if (arg != NULL)
5688                 VEC_safe_push (symbolp, template_args, arg);
5689             }
5690           else
5691             process_die (child_die, cu);
5692           child_die = sibling_die (child_die);
5693         }
5694     }
5695
5696   inherit_abstract_dies (die, cu);
5697
5698   /* If we have a DW_AT_specification, we might need to import using
5699      directives from the context of the specification DIE.  See the
5700      comment in determine_prefix.  */
5701   if (cu->language == language_cplus
5702       && dwarf2_attr (die, DW_AT_specification, cu))
5703     {
5704       struct dwarf2_cu *spec_cu = cu;
5705       struct die_info *spec_die = die_specification (die, &spec_cu);
5706
5707       while (spec_die)
5708         {
5709           child_die = spec_die->child;
5710           while (child_die && child_die->tag)
5711             {
5712               if (child_die->tag == DW_TAG_imported_module)
5713                 process_die (child_die, spec_cu);
5714               child_die = sibling_die (child_die);
5715             }
5716
5717           /* In some cases, GCC generates specification DIEs that
5718              themselves contain DW_AT_specification attributes.  */
5719           spec_die = die_specification (spec_die, &spec_cu);
5720         }
5721     }
5722
5723   new = pop_context ();
5724   /* Make a block for the local symbols within.  */
5725   block = finish_block (new->name, &local_symbols, new->old_blocks,
5726                         lowpc, highpc, objfile);
5727
5728   /* For C++, set the block's scope.  */
5729   if (cu->language == language_cplus || cu->language == language_fortran)
5730     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5731                         determine_prefix (die, cu),
5732                         processing_has_namespace_info);
5733
5734   /* If we have address ranges, record them.  */
5735   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5736
5737   /* Attach template arguments to function.  */
5738   if (! VEC_empty (symbolp, template_args))
5739     {
5740       gdb_assert (templ_func != NULL);
5741
5742       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5743       templ_func->template_arguments
5744         = obstack_alloc (&objfile->objfile_obstack,
5745                          (templ_func->n_template_arguments
5746                           * sizeof (struct symbol *)));
5747       memcpy (templ_func->template_arguments,
5748               VEC_address (symbolp, template_args),
5749               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5750       VEC_free (symbolp, template_args);
5751     }
5752
5753   /* In C++, we can have functions nested inside functions (e.g., when
5754      a function declares a class that has methods).  This means that
5755      when we finish processing a function scope, we may need to go
5756      back to building a containing block's symbol lists.  */
5757   local_symbols = new->locals;
5758   param_symbols = new->params;
5759   using_directives = new->using_directives;
5760
5761   /* If we've finished processing a top-level function, subsequent
5762      symbols go in the file symbol list.  */
5763   if (outermost_context_p ())
5764     cu->list_in_scope = &file_symbols;
5765 }
5766
5767 /* Process all the DIES contained within a lexical block scope.  Start
5768    a new scope, process the dies, and then close the scope.  */
5769
5770 static void
5771 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5772 {
5773   struct objfile *objfile = cu->objfile;
5774   struct context_stack *new;
5775   CORE_ADDR lowpc, highpc;
5776   struct die_info *child_die;
5777   CORE_ADDR baseaddr;
5778
5779   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5780
5781   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5782   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5783      as multiple lexical blocks?  Handling children in a sane way would
5784      be nasty.  Might be easier to properly extend generic blocks to
5785      describe ranges.  */
5786   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5787     return;
5788   lowpc += baseaddr;
5789   highpc += baseaddr;
5790
5791   push_context (0, lowpc);
5792   if (die->child != NULL)
5793     {
5794       child_die = die->child;
5795       while (child_die && child_die->tag)
5796         {
5797           process_die (child_die, cu);
5798           child_die = sibling_die (child_die);
5799         }
5800     }
5801   new = pop_context ();
5802
5803   if (local_symbols != NULL || using_directives != NULL)
5804     {
5805       struct block *block
5806         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5807                         highpc, objfile);
5808
5809       /* Note that recording ranges after traversing children, as we
5810          do here, means that recording a parent's ranges entails
5811          walking across all its children's ranges as they appear in
5812          the address map, which is quadratic behavior.
5813
5814          It would be nicer to record the parent's ranges before
5815          traversing its children, simply overriding whatever you find
5816          there.  But since we don't even decide whether to create a
5817          block until after we've traversed its children, that's hard
5818          to do.  */
5819       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5820     }
5821   local_symbols = new->locals;
5822   using_directives = new->using_directives;
5823 }
5824
5825 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5826    Return 1 if the attributes are present and valid, otherwise, return 0.
5827    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5828
5829 static int
5830 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5831                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5832                     struct partial_symtab *ranges_pst)
5833 {
5834   struct objfile *objfile = cu->objfile;
5835   struct comp_unit_head *cu_header = &cu->header;
5836   bfd *obfd = objfile->obfd;
5837   unsigned int addr_size = cu_header->addr_size;
5838   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5839   /* Base address selection entry.  */
5840   CORE_ADDR base;
5841   int found_base;
5842   unsigned int dummy;
5843   gdb_byte *buffer;
5844   CORE_ADDR marker;
5845   int low_set;
5846   CORE_ADDR low = 0;
5847   CORE_ADDR high = 0;
5848   CORE_ADDR baseaddr;
5849
5850   found_base = cu->base_known;
5851   base = cu->base_address;
5852
5853   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5854   if (offset >= dwarf2_per_objfile->ranges.size)
5855     {
5856       complaint (&symfile_complaints,
5857                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5858                  offset);
5859       return 0;
5860     }
5861   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5862
5863   /* Read in the largest possible address.  */
5864   marker = read_address (obfd, buffer, cu, &dummy);
5865   if ((marker & mask) == mask)
5866     {
5867       /* If we found the largest possible address, then
5868          read the base address.  */
5869       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5870       buffer += 2 * addr_size;
5871       offset += 2 * addr_size;
5872       found_base = 1;
5873     }
5874
5875   low_set = 0;
5876
5877   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5878
5879   while (1)
5880     {
5881       CORE_ADDR range_beginning, range_end;
5882
5883       range_beginning = read_address (obfd, buffer, cu, &dummy);
5884       buffer += addr_size;
5885       range_end = read_address (obfd, buffer, cu, &dummy);
5886       buffer += addr_size;
5887       offset += 2 * addr_size;
5888
5889       /* An end of list marker is a pair of zero addresses.  */
5890       if (range_beginning == 0 && range_end == 0)
5891         /* Found the end of list entry.  */
5892         break;
5893
5894       /* Each base address selection entry is a pair of 2 values.
5895          The first is the largest possible address, the second is
5896          the base address.  Check for a base address here.  */
5897       if ((range_beginning & mask) == mask)
5898         {
5899           /* If we found the largest possible address, then
5900              read the base address.  */
5901           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5902           found_base = 1;
5903           continue;
5904         }
5905
5906       if (!found_base)
5907         {
5908           /* We have no valid base address for the ranges
5909              data.  */
5910           complaint (&symfile_complaints,
5911                      _("Invalid .debug_ranges data (no base address)"));
5912           return 0;
5913         }
5914
5915       if (range_beginning > range_end)
5916         {
5917           /* Inverted range entries are invalid.  */
5918           complaint (&symfile_complaints,
5919                      _("Invalid .debug_ranges data (inverted range)"));
5920           return 0;
5921         }
5922
5923       /* Empty range entries have no effect.  */
5924       if (range_beginning == range_end)
5925         continue;
5926
5927       range_beginning += base;
5928       range_end += base;
5929
5930       if (ranges_pst != NULL)
5931         addrmap_set_empty (objfile->psymtabs_addrmap,
5932                            range_beginning + baseaddr,
5933                            range_end - 1 + baseaddr,
5934                            ranges_pst);
5935
5936       /* FIXME: This is recording everything as a low-high
5937          segment of consecutive addresses.  We should have a
5938          data structure for discontiguous block ranges
5939          instead.  */
5940       if (! low_set)
5941         {
5942           low = range_beginning;
5943           high = range_end;
5944           low_set = 1;
5945         }
5946       else
5947         {
5948           if (range_beginning < low)
5949             low = range_beginning;
5950           if (range_end > high)
5951             high = range_end;
5952         }
5953     }
5954
5955   if (! low_set)
5956     /* If the first entry is an end-of-list marker, the range
5957        describes an empty scope, i.e. no instructions.  */
5958     return 0;
5959
5960   if (low_return)
5961     *low_return = low;
5962   if (high_return)
5963     *high_return = high;
5964   return 1;
5965 }
5966
5967 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5968    are present and valid, otherwise, return 0.  Return -1 if the range is
5969    discontinuous, i.e. derived from DW_AT_ranges information.  */
5970 static int
5971 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5972                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5973                       struct partial_symtab *pst)
5974 {
5975   struct attribute *attr;
5976   CORE_ADDR low = 0;
5977   CORE_ADDR high = 0;
5978   int ret = 0;
5979
5980   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5981   if (attr)
5982     {
5983       high = DW_ADDR (attr);
5984       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5985       if (attr)
5986         low = DW_ADDR (attr);
5987       else
5988         /* Found high w/o low attribute.  */
5989         return 0;
5990
5991       /* Found consecutive range of addresses.  */
5992       ret = 1;
5993     }
5994   else
5995     {
5996       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5997       if (attr != NULL)
5998         {
5999           /* Value of the DW_AT_ranges attribute is the offset in the
6000              .debug_ranges section.  */
6001           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6002             return 0;
6003           /* Found discontinuous range of addresses.  */
6004           ret = -1;
6005         }
6006     }
6007
6008   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6009   if (high <= low)
6010     return 0;
6011
6012   /* When using the GNU linker, .gnu.linkonce. sections are used to
6013      eliminate duplicate copies of functions and vtables and such.
6014      The linker will arbitrarily choose one and discard the others.
6015      The AT_*_pc values for such functions refer to local labels in
6016      these sections.  If the section from that file was discarded, the
6017      labels are not in the output, so the relocs get a value of 0.
6018      If this is a discarded function, mark the pc bounds as invalid,
6019      so that GDB will ignore it.  */
6020   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6021     return 0;
6022
6023   *lowpc = low;
6024   *highpc = high;
6025   return ret;
6026 }
6027
6028 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6029    its low and high PC addresses.  Do nothing if these addresses could not
6030    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6031    and HIGHPC to the high address if greater than HIGHPC.  */
6032
6033 static void
6034 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6035                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6036                                  struct dwarf2_cu *cu)
6037 {
6038   CORE_ADDR low, high;
6039   struct die_info *child = die->child;
6040
6041   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6042     {
6043       *lowpc = min (*lowpc, low);
6044       *highpc = max (*highpc, high);
6045     }
6046
6047   /* If the language does not allow nested subprograms (either inside
6048      subprograms or lexical blocks), we're done.  */
6049   if (cu->language != language_ada)
6050     return;
6051
6052   /* Check all the children of the given DIE.  If it contains nested
6053      subprograms, then check their pc bounds.  Likewise, we need to
6054      check lexical blocks as well, as they may also contain subprogram
6055      definitions.  */
6056   while (child && child->tag)
6057     {
6058       if (child->tag == DW_TAG_subprogram
6059           || child->tag == DW_TAG_lexical_block)
6060         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6061       child = sibling_die (child);
6062     }
6063 }
6064
6065 /* Get the low and high pc's represented by the scope DIE, and store
6066    them in *LOWPC and *HIGHPC.  If the correct values can't be
6067    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6068
6069 static void
6070 get_scope_pc_bounds (struct die_info *die,
6071                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6072                      struct dwarf2_cu *cu)
6073 {
6074   CORE_ADDR best_low = (CORE_ADDR) -1;
6075   CORE_ADDR best_high = (CORE_ADDR) 0;
6076   CORE_ADDR current_low, current_high;
6077
6078   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6079     {
6080       best_low = current_low;
6081       best_high = current_high;
6082     }
6083   else
6084     {
6085       struct die_info *child = die->child;
6086
6087       while (child && child->tag)
6088         {
6089           switch (child->tag) {
6090           case DW_TAG_subprogram:
6091             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6092             break;
6093           case DW_TAG_namespace:
6094           case DW_TAG_module:
6095             /* FIXME: carlton/2004-01-16: Should we do this for
6096                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6097                that current GCC's always emit the DIEs corresponding
6098                to definitions of methods of classes as children of a
6099                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6100                the DIEs giving the declarations, which could be
6101                anywhere).  But I don't see any reason why the
6102                standards says that they have to be there.  */
6103             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6104
6105             if (current_low != ((CORE_ADDR) -1))
6106               {
6107                 best_low = min (best_low, current_low);
6108                 best_high = max (best_high, current_high);
6109               }
6110             break;
6111           default:
6112             /* Ignore.  */
6113             break;
6114           }
6115
6116           child = sibling_die (child);
6117         }
6118     }
6119
6120   *lowpc = best_low;
6121   *highpc = best_high;
6122 }
6123
6124 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6125    in DIE.  */
6126 static void
6127 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6128                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6129 {
6130   struct attribute *attr;
6131
6132   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6133   if (attr)
6134     {
6135       CORE_ADDR high = DW_ADDR (attr);
6136
6137       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6138       if (attr)
6139         {
6140           CORE_ADDR low = DW_ADDR (attr);
6141
6142           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6143         }
6144     }
6145
6146   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6147   if (attr)
6148     {
6149       bfd *obfd = cu->objfile->obfd;
6150
6151       /* The value of the DW_AT_ranges attribute is the offset of the
6152          address range list in the .debug_ranges section.  */
6153       unsigned long offset = DW_UNSND (attr);
6154       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6155
6156       /* For some target architectures, but not others, the
6157          read_address function sign-extends the addresses it returns.
6158          To recognize base address selection entries, we need a
6159          mask.  */
6160       unsigned int addr_size = cu->header.addr_size;
6161       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6162
6163       /* The base address, to which the next pair is relative.  Note
6164          that this 'base' is a DWARF concept: most entries in a range
6165          list are relative, to reduce the number of relocs against the
6166          debugging information.  This is separate from this function's
6167          'baseaddr' argument, which GDB uses to relocate debugging
6168          information from a shared library based on the address at
6169          which the library was loaded.  */
6170       CORE_ADDR base = cu->base_address;
6171       int base_known = cu->base_known;
6172
6173       gdb_assert (dwarf2_per_objfile->ranges.readin);
6174       if (offset >= dwarf2_per_objfile->ranges.size)
6175         {
6176           complaint (&symfile_complaints,
6177                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6178                      offset);
6179           return;
6180         }
6181
6182       for (;;)
6183         {
6184           unsigned int bytes_read;
6185           CORE_ADDR start, end;
6186
6187           start = read_address (obfd, buffer, cu, &bytes_read);
6188           buffer += bytes_read;
6189           end = read_address (obfd, buffer, cu, &bytes_read);
6190           buffer += bytes_read;
6191
6192           /* Did we find the end of the range list?  */
6193           if (start == 0 && end == 0)
6194             break;
6195
6196           /* Did we find a base address selection entry?  */
6197           else if ((start & base_select_mask) == base_select_mask)
6198             {
6199               base = end;
6200               base_known = 1;
6201             }
6202
6203           /* We found an ordinary address range.  */
6204           else
6205             {
6206               if (!base_known)
6207                 {
6208                   complaint (&symfile_complaints,
6209                              _("Invalid .debug_ranges data "
6210                                "(no base address)"));
6211                   return;
6212                 }
6213
6214               if (start > end)
6215                 {
6216                   /* Inverted range entries are invalid.  */
6217                   complaint (&symfile_complaints,
6218                              _("Invalid .debug_ranges data "
6219                                "(inverted range)"));
6220                   return;
6221                 }
6222
6223               /* Empty range entries have no effect.  */
6224               if (start == end)
6225                 continue;
6226
6227               record_block_range (block,
6228                                   baseaddr + base + start,
6229                                   baseaddr + base + end - 1);
6230             }
6231         }
6232     }
6233 }
6234
6235 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6236    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6237    during 4.6.0 experimental.  */
6238
6239 static int
6240 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6241 {
6242   const char *cs;
6243   int major, minor, release;
6244
6245   if (cu->producer == NULL)
6246     {
6247       /* For unknown compilers expect their behavior is DWARF version
6248          compliant.
6249
6250          GCC started to support .debug_types sections by -gdwarf-4 since
6251          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6252          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6253          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6254          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6255
6256       return 0;
6257     }
6258
6259   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6260
6261   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6262     {
6263       /* For non-GCC compilers expect their behavior is DWARF version
6264          compliant.  */
6265
6266       return 0;
6267     }
6268   cs = &cu->producer[strlen ("GNU ")];
6269   while (*cs && !isdigit (*cs))
6270     cs++;
6271   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6272     {
6273       /* Not recognized as GCC.  */
6274
6275       return 0;
6276     }
6277
6278   return major < 4 || (major == 4 && minor < 6);
6279 }
6280
6281 /* Return the default accessibility type if it is not overriden by
6282    DW_AT_accessibility.  */
6283
6284 static enum dwarf_access_attribute
6285 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6286 {
6287   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6288     {
6289       /* The default DWARF 2 accessibility for members is public, the default
6290          accessibility for inheritance is private.  */
6291
6292       if (die->tag != DW_TAG_inheritance)
6293         return DW_ACCESS_public;
6294       else
6295         return DW_ACCESS_private;
6296     }
6297   else
6298     {
6299       /* DWARF 3+ defines the default accessibility a different way.  The same
6300          rules apply now for DW_TAG_inheritance as for the members and it only
6301          depends on the container kind.  */
6302
6303       if (die->parent->tag == DW_TAG_class_type)
6304         return DW_ACCESS_private;
6305       else
6306         return DW_ACCESS_public;
6307     }
6308 }
6309
6310 /* Add an aggregate field to the field list.  */
6311
6312 static void
6313 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6314                   struct dwarf2_cu *cu)
6315 {
6316   struct objfile *objfile = cu->objfile;
6317   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6318   struct nextfield *new_field;
6319   struct attribute *attr;
6320   struct field *fp;
6321   char *fieldname = "";
6322
6323   /* Allocate a new field list entry and link it in.  */
6324   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6325   make_cleanup (xfree, new_field);
6326   memset (new_field, 0, sizeof (struct nextfield));
6327
6328   if (die->tag == DW_TAG_inheritance)
6329     {
6330       new_field->next = fip->baseclasses;
6331       fip->baseclasses = new_field;
6332     }
6333   else
6334     {
6335       new_field->next = fip->fields;
6336       fip->fields = new_field;
6337     }
6338   fip->nfields++;
6339
6340   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6341   if (attr)
6342     new_field->accessibility = DW_UNSND (attr);
6343   else
6344     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6345   if (new_field->accessibility != DW_ACCESS_public)
6346     fip->non_public_fields = 1;
6347
6348   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6349   if (attr)
6350     new_field->virtuality = DW_UNSND (attr);
6351   else
6352     new_field->virtuality = DW_VIRTUALITY_none;
6353
6354   fp = &new_field->field;
6355
6356   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6357     {
6358       /* Data member other than a C++ static data member.  */
6359
6360       /* Get type of field.  */
6361       fp->type = die_type (die, cu);
6362
6363       SET_FIELD_BITPOS (*fp, 0);
6364
6365       /* Get bit size of field (zero if none).  */
6366       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6367       if (attr)
6368         {
6369           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6370         }
6371       else
6372         {
6373           FIELD_BITSIZE (*fp) = 0;
6374         }
6375
6376       /* Get bit offset of field.  */
6377       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6378       if (attr)
6379         {
6380           int byte_offset = 0;
6381
6382           if (attr_form_is_section_offset (attr))
6383             dwarf2_complex_location_expr_complaint ();
6384           else if (attr_form_is_constant (attr))
6385             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6386           else if (attr_form_is_block (attr))
6387             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6388           else
6389             dwarf2_complex_location_expr_complaint ();
6390
6391           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6392         }
6393       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6394       if (attr)
6395         {
6396           if (gdbarch_bits_big_endian (gdbarch))
6397             {
6398               /* For big endian bits, the DW_AT_bit_offset gives the
6399                  additional bit offset from the MSB of the containing
6400                  anonymous object to the MSB of the field.  We don't
6401                  have to do anything special since we don't need to
6402                  know the size of the anonymous object.  */
6403               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6404             }
6405           else
6406             {
6407               /* For little endian bits, compute the bit offset to the
6408                  MSB of the anonymous object, subtract off the number of
6409                  bits from the MSB of the field to the MSB of the
6410                  object, and then subtract off the number of bits of
6411                  the field itself.  The result is the bit offset of
6412                  the LSB of the field.  */
6413               int anonymous_size;
6414               int bit_offset = DW_UNSND (attr);
6415
6416               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6417               if (attr)
6418                 {
6419                   /* The size of the anonymous object containing
6420                      the bit field is explicit, so use the
6421                      indicated size (in bytes).  */
6422                   anonymous_size = DW_UNSND (attr);
6423                 }
6424               else
6425                 {
6426                   /* The size of the anonymous object containing
6427                      the bit field must be inferred from the type
6428                      attribute of the data member containing the
6429                      bit field.  */
6430                   anonymous_size = TYPE_LENGTH (fp->type);
6431                 }
6432               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6433                 - bit_offset - FIELD_BITSIZE (*fp);
6434             }
6435         }
6436
6437       /* Get name of field.  */
6438       fieldname = dwarf2_name (die, cu);
6439       if (fieldname == NULL)
6440         fieldname = "";
6441
6442       /* The name is already allocated along with this objfile, so we don't
6443          need to duplicate it for the type.  */
6444       fp->name = fieldname;
6445
6446       /* Change accessibility for artificial fields (e.g. virtual table
6447          pointer or virtual base class pointer) to private.  */
6448       if (dwarf2_attr (die, DW_AT_artificial, cu))
6449         {
6450           FIELD_ARTIFICIAL (*fp) = 1;
6451           new_field->accessibility = DW_ACCESS_private;
6452           fip->non_public_fields = 1;
6453         }
6454     }
6455   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6456     {
6457       /* C++ static member.  */
6458
6459       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6460          is a declaration, but all versions of G++ as of this writing
6461          (so through at least 3.2.1) incorrectly generate
6462          DW_TAG_variable tags.  */
6463
6464       char *physname;
6465
6466       /* Get name of field.  */
6467       fieldname = dwarf2_name (die, cu);
6468       if (fieldname == NULL)
6469         return;
6470
6471       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6472       if (attr
6473           /* Only create a symbol if this is an external value.
6474              new_symbol checks this and puts the value in the global symbol
6475              table, which we want.  If it is not external, new_symbol
6476              will try to put the value in cu->list_in_scope which is wrong.  */
6477           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6478         {
6479           /* A static const member, not much different than an enum as far as
6480              we're concerned, except that we can support more types.  */
6481           new_symbol (die, NULL, cu);
6482         }
6483
6484       /* Get physical name.  */
6485       physname = (char *) dwarf2_physname (fieldname, die, cu);
6486
6487       /* The name is already allocated along with this objfile, so we don't
6488          need to duplicate it for the type.  */
6489       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6490       FIELD_TYPE (*fp) = die_type (die, cu);
6491       FIELD_NAME (*fp) = fieldname;
6492     }
6493   else if (die->tag == DW_TAG_inheritance)
6494     {
6495       /* C++ base class field.  */
6496       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6497       if (attr)
6498         {
6499           int byte_offset = 0;
6500
6501           if (attr_form_is_section_offset (attr))
6502             dwarf2_complex_location_expr_complaint ();
6503           else if (attr_form_is_constant (attr))
6504             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6505           else if (attr_form_is_block (attr))
6506             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6507           else
6508             dwarf2_complex_location_expr_complaint ();
6509
6510           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6511         }
6512       FIELD_BITSIZE (*fp) = 0;
6513       FIELD_TYPE (*fp) = die_type (die, cu);
6514       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6515       fip->nbaseclasses++;
6516     }
6517 }
6518
6519 /* Add a typedef defined in the scope of the FIP's class.  */
6520
6521 static void
6522 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6523                     struct dwarf2_cu *cu)
6524 {
6525   struct objfile *objfile = cu->objfile;
6526   struct typedef_field_list *new_field;
6527   struct attribute *attr;
6528   struct typedef_field *fp;
6529   char *fieldname = "";
6530
6531   /* Allocate a new field list entry and link it in.  */
6532   new_field = xzalloc (sizeof (*new_field));
6533   make_cleanup (xfree, new_field);
6534
6535   gdb_assert (die->tag == DW_TAG_typedef);
6536
6537   fp = &new_field->field;
6538
6539   /* Get name of field.  */
6540   fp->name = dwarf2_name (die, cu);
6541   if (fp->name == NULL)
6542     return;
6543
6544   fp->type = read_type_die (die, cu);
6545
6546   new_field->next = fip->typedef_field_list;
6547   fip->typedef_field_list = new_field;
6548   fip->typedef_field_list_count++;
6549 }
6550
6551 /* Create the vector of fields, and attach it to the type.  */
6552
6553 static void
6554 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6555                               struct dwarf2_cu *cu)
6556 {
6557   int nfields = fip->nfields;
6558
6559   /* Record the field count, allocate space for the array of fields,
6560      and create blank accessibility bitfields if necessary.  */
6561   TYPE_NFIELDS (type) = nfields;
6562   TYPE_FIELDS (type) = (struct field *)
6563     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6564   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6565
6566   if (fip->non_public_fields && cu->language != language_ada)
6567     {
6568       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6569
6570       TYPE_FIELD_PRIVATE_BITS (type) =
6571         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6572       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6573
6574       TYPE_FIELD_PROTECTED_BITS (type) =
6575         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6576       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6577
6578       TYPE_FIELD_IGNORE_BITS (type) =
6579         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6580       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6581     }
6582
6583   /* If the type has baseclasses, allocate and clear a bit vector for
6584      TYPE_FIELD_VIRTUAL_BITS.  */
6585   if (fip->nbaseclasses && cu->language != language_ada)
6586     {
6587       int num_bytes = B_BYTES (fip->nbaseclasses);
6588       unsigned char *pointer;
6589
6590       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6591       pointer = TYPE_ALLOC (type, num_bytes);
6592       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6593       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6594       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6595     }
6596
6597   /* Copy the saved-up fields into the field vector.  Start from the head of
6598      the list, adding to the tail of the field array, so that they end up in
6599      the same order in the array in which they were added to the list.  */
6600   while (nfields-- > 0)
6601     {
6602       struct nextfield *fieldp;
6603
6604       if (fip->fields)
6605         {
6606           fieldp = fip->fields;
6607           fip->fields = fieldp->next;
6608         }
6609       else
6610         {
6611           fieldp = fip->baseclasses;
6612           fip->baseclasses = fieldp->next;
6613         }
6614
6615       TYPE_FIELD (type, nfields) = fieldp->field;
6616       switch (fieldp->accessibility)
6617         {
6618         case DW_ACCESS_private:
6619           if (cu->language != language_ada)
6620             SET_TYPE_FIELD_PRIVATE (type, nfields);
6621           break;
6622
6623         case DW_ACCESS_protected:
6624           if (cu->language != language_ada)
6625             SET_TYPE_FIELD_PROTECTED (type, nfields);
6626           break;
6627
6628         case DW_ACCESS_public:
6629           break;
6630
6631         default:
6632           /* Unknown accessibility.  Complain and treat it as public.  */
6633           {
6634             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6635                        fieldp->accessibility);
6636           }
6637           break;
6638         }
6639       if (nfields < fip->nbaseclasses)
6640         {
6641           switch (fieldp->virtuality)
6642             {
6643             case DW_VIRTUALITY_virtual:
6644             case DW_VIRTUALITY_pure_virtual:
6645               if (cu->language == language_ada)
6646                 error (_("unexpected virtuality in component of Ada type"));
6647               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6648               break;
6649             }
6650         }
6651     }
6652 }
6653
6654 /* Add a member function to the proper fieldlist.  */
6655
6656 static void
6657 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6658                       struct type *type, struct dwarf2_cu *cu)
6659 {
6660   struct objfile *objfile = cu->objfile;
6661   struct attribute *attr;
6662   struct fnfieldlist *flp;
6663   int i;
6664   struct fn_field *fnp;
6665   char *fieldname;
6666   struct nextfnfield *new_fnfield;
6667   struct type *this_type;
6668   enum dwarf_access_attribute accessibility;
6669
6670   if (cu->language == language_ada)
6671     error (_("unexpected member function in Ada type"));
6672
6673   /* Get name of member function.  */
6674   fieldname = dwarf2_name (die, cu);
6675   if (fieldname == NULL)
6676     return;
6677
6678   /* Look up member function name in fieldlist.  */
6679   for (i = 0; i < fip->nfnfields; i++)
6680     {
6681       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6682         break;
6683     }
6684
6685   /* Create new list element if necessary.  */
6686   if (i < fip->nfnfields)
6687     flp = &fip->fnfieldlists[i];
6688   else
6689     {
6690       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6691         {
6692           fip->fnfieldlists = (struct fnfieldlist *)
6693             xrealloc (fip->fnfieldlists,
6694                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6695                       * sizeof (struct fnfieldlist));
6696           if (fip->nfnfields == 0)
6697             make_cleanup (free_current_contents, &fip->fnfieldlists);
6698         }
6699       flp = &fip->fnfieldlists[fip->nfnfields];
6700       flp->name = fieldname;
6701       flp->length = 0;
6702       flp->head = NULL;
6703       i = fip->nfnfields++;
6704     }
6705
6706   /* Create a new member function field and chain it to the field list
6707      entry.  */
6708   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6709   make_cleanup (xfree, new_fnfield);
6710   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6711   new_fnfield->next = flp->head;
6712   flp->head = new_fnfield;
6713   flp->length++;
6714
6715   /* Fill in the member function field info.  */
6716   fnp = &new_fnfield->fnfield;
6717
6718   /* Delay processing of the physname until later.  */
6719   if (cu->language == language_cplus || cu->language == language_java)
6720     {
6721       add_to_method_list (type, i, flp->length - 1, fieldname,
6722                           die, cu);
6723     }
6724   else
6725     {
6726       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6727       fnp->physname = physname ? physname : "";
6728     }
6729
6730   fnp->type = alloc_type (objfile);
6731   this_type = read_type_die (die, cu);
6732   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6733     {
6734       int nparams = TYPE_NFIELDS (this_type);
6735
6736       /* TYPE is the domain of this method, and THIS_TYPE is the type
6737            of the method itself (TYPE_CODE_METHOD).  */
6738       smash_to_method_type (fnp->type, type,
6739                             TYPE_TARGET_TYPE (this_type),
6740                             TYPE_FIELDS (this_type),
6741                             TYPE_NFIELDS (this_type),
6742                             TYPE_VARARGS (this_type));
6743
6744       /* Handle static member functions.
6745          Dwarf2 has no clean way to discern C++ static and non-static
6746          member functions.  G++ helps GDB by marking the first
6747          parameter for non-static member functions (which is the this
6748          pointer) as artificial.  We obtain this information from
6749          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6750       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6751         fnp->voffset = VOFFSET_STATIC;
6752     }
6753   else
6754     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6755                dwarf2_full_name (fieldname, die, cu));
6756
6757   /* Get fcontext from DW_AT_containing_type if present.  */
6758   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6759     fnp->fcontext = die_containing_type (die, cu);
6760
6761   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6762      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6763
6764   /* Get accessibility.  */
6765   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6766   if (attr)
6767     accessibility = DW_UNSND (attr);
6768   else
6769     accessibility = dwarf2_default_access_attribute (die, cu);
6770   switch (accessibility)
6771     {
6772     case DW_ACCESS_private:
6773       fnp->is_private = 1;
6774       break;
6775     case DW_ACCESS_protected:
6776       fnp->is_protected = 1;
6777       break;
6778     }
6779
6780   /* Check for artificial methods.  */
6781   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6782   if (attr && DW_UNSND (attr) != 0)
6783     fnp->is_artificial = 1;
6784
6785   /* Get index in virtual function table if it is a virtual member
6786      function.  For older versions of GCC, this is an offset in the
6787      appropriate virtual table, as specified by DW_AT_containing_type.
6788      For everyone else, it is an expression to be evaluated relative
6789      to the object address.  */
6790
6791   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6792   if (attr)
6793     {
6794       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6795         {
6796           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6797             {
6798               /* Old-style GCC.  */
6799               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6800             }
6801           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6802                    || (DW_BLOCK (attr)->size > 1
6803                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6804                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6805             {
6806               struct dwarf_block blk;
6807               int offset;
6808
6809               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6810                         ? 1 : 2);
6811               blk.size = DW_BLOCK (attr)->size - offset;
6812               blk.data = DW_BLOCK (attr)->data + offset;
6813               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6814               if ((fnp->voffset % cu->header.addr_size) != 0)
6815                 dwarf2_complex_location_expr_complaint ();
6816               else
6817                 fnp->voffset /= cu->header.addr_size;
6818               fnp->voffset += 2;
6819             }
6820           else
6821             dwarf2_complex_location_expr_complaint ();
6822
6823           if (!fnp->fcontext)
6824             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6825         }
6826       else if (attr_form_is_section_offset (attr))
6827         {
6828           dwarf2_complex_location_expr_complaint ();
6829         }
6830       else
6831         {
6832           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6833                                                  fieldname);
6834         }
6835     }
6836   else
6837     {
6838       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6839       if (attr && DW_UNSND (attr))
6840         {
6841           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6842           complaint (&symfile_complaints,
6843                      _("Member function \"%s\" (offset %d) is virtual "
6844                        "but the vtable offset is not specified"),
6845                      fieldname, die->offset);
6846           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6847           TYPE_CPLUS_DYNAMIC (type) = 1;
6848         }
6849     }
6850 }
6851
6852 /* Create the vector of member function fields, and attach it to the type.  */
6853
6854 static void
6855 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6856                                  struct dwarf2_cu *cu)
6857 {
6858   struct fnfieldlist *flp;
6859   int total_length = 0;
6860   int i;
6861
6862   if (cu->language == language_ada)
6863     error (_("unexpected member functions in Ada type"));
6864
6865   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6866   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6867     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6868
6869   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6870     {
6871       struct nextfnfield *nfp = flp->head;
6872       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6873       int k;
6874
6875       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6876       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6877       fn_flp->fn_fields = (struct fn_field *)
6878         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6879       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6880         fn_flp->fn_fields[k] = nfp->fnfield;
6881
6882       total_length += flp->length;
6883     }
6884
6885   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6886   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6887 }
6888
6889 /* Returns non-zero if NAME is the name of a vtable member in CU's
6890    language, zero otherwise.  */
6891 static int
6892 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6893 {
6894   static const char vptr[] = "_vptr";
6895   static const char vtable[] = "vtable";
6896
6897   /* Look for the C++ and Java forms of the vtable.  */
6898   if ((cu->language == language_java
6899        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6900        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6901        && is_cplus_marker (name[sizeof (vptr) - 1])))
6902     return 1;
6903
6904   return 0;
6905 }
6906
6907 /* GCC outputs unnamed structures that are really pointers to member
6908    functions, with the ABI-specified layout.  If TYPE describes
6909    such a structure, smash it into a member function type.
6910
6911    GCC shouldn't do this; it should just output pointer to member DIEs.
6912    This is GCC PR debug/28767.  */
6913
6914 static void
6915 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6916 {
6917   struct type *pfn_type, *domain_type, *new_type;
6918
6919   /* Check for a structure with no name and two children.  */
6920   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6921     return;
6922
6923   /* Check for __pfn and __delta members.  */
6924   if (TYPE_FIELD_NAME (type, 0) == NULL
6925       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6926       || TYPE_FIELD_NAME (type, 1) == NULL
6927       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6928     return;
6929
6930   /* Find the type of the method.  */
6931   pfn_type = TYPE_FIELD_TYPE (type, 0);
6932   if (pfn_type == NULL
6933       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6934       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6935     return;
6936
6937   /* Look for the "this" argument.  */
6938   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6939   if (TYPE_NFIELDS (pfn_type) == 0
6940       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6941       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6942     return;
6943
6944   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6945   new_type = alloc_type (objfile);
6946   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6947                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6948                         TYPE_VARARGS (pfn_type));
6949   smash_to_methodptr_type (type, new_type);
6950 }
6951
6952 /* Called when we find the DIE that starts a structure or union scope
6953    (definition) to create a type for the structure or union.  Fill in
6954    the type's name and general properties; the members will not be
6955    processed until process_structure_type.
6956
6957    NOTE: we need to call these functions regardless of whether or not the
6958    DIE has a DW_AT_name attribute, since it might be an anonymous
6959    structure or union.  This gets the type entered into our set of
6960    user defined types.
6961
6962    However, if the structure is incomplete (an opaque struct/union)
6963    then suppress creating a symbol table entry for it since gdb only
6964    wants to find the one with the complete definition.  Note that if
6965    it is complete, we just call new_symbol, which does it's own
6966    checking about whether the struct/union is anonymous or not (and
6967    suppresses creating a symbol table entry itself).  */
6968
6969 static struct type *
6970 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6971 {
6972   struct objfile *objfile = cu->objfile;
6973   struct type *type;
6974   struct attribute *attr;
6975   char *name;
6976
6977   /* If the definition of this type lives in .debug_types, read that type.
6978      Don't follow DW_AT_specification though, that will take us back up
6979      the chain and we want to go down.  */
6980   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6981   if (attr)
6982     {
6983       struct dwarf2_cu *type_cu = cu;
6984       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6985
6986       /* We could just recurse on read_structure_type, but we need to call
6987          get_die_type to ensure only one type for this DIE is created.
6988          This is important, for example, because for c++ classes we need
6989          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6990       type = read_type_die (type_die, type_cu);
6991
6992       /* TYPE_CU may not be the same as CU.
6993          Ensure TYPE is recorded in CU's type_hash table.  */
6994       return set_die_type (die, type, cu);
6995     }
6996
6997   type = alloc_type (objfile);
6998   INIT_CPLUS_SPECIFIC (type);
6999
7000   name = dwarf2_name (die, cu);
7001   if (name != NULL)
7002     {
7003       if (cu->language == language_cplus
7004           || cu->language == language_java)
7005         {
7006           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7007
7008           /* dwarf2_full_name might have already finished building the DIE's
7009              type.  If so, there is no need to continue.  */
7010           if (get_die_type (die, cu) != NULL)
7011             return get_die_type (die, cu);
7012
7013           TYPE_TAG_NAME (type) = full_name;
7014           if (die->tag == DW_TAG_structure_type
7015               || die->tag == DW_TAG_class_type)
7016             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7017         }
7018       else
7019         {
7020           /* The name is already allocated along with this objfile, so
7021              we don't need to duplicate it for the type.  */
7022           TYPE_TAG_NAME (type) = (char *) name;
7023           if (die->tag == DW_TAG_class_type)
7024             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7025         }
7026     }
7027
7028   if (die->tag == DW_TAG_structure_type)
7029     {
7030       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7031     }
7032   else if (die->tag == DW_TAG_union_type)
7033     {
7034       TYPE_CODE (type) = TYPE_CODE_UNION;
7035     }
7036   else
7037     {
7038       TYPE_CODE (type) = TYPE_CODE_CLASS;
7039     }
7040
7041   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7042     TYPE_DECLARED_CLASS (type) = 1;
7043
7044   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7045   if (attr)
7046     {
7047       TYPE_LENGTH (type) = DW_UNSND (attr);
7048     }
7049   else
7050     {
7051       TYPE_LENGTH (type) = 0;
7052     }
7053
7054   TYPE_STUB_SUPPORTED (type) = 1;
7055   if (die_is_declaration (die, cu))
7056     TYPE_STUB (type) = 1;
7057   else if (attr == NULL && die->child == NULL
7058            && producer_is_realview (cu->producer))
7059     /* RealView does not output the required DW_AT_declaration
7060        on incomplete types.  */
7061     TYPE_STUB (type) = 1;
7062
7063   /* We need to add the type field to the die immediately so we don't
7064      infinitely recurse when dealing with pointers to the structure
7065      type within the structure itself.  */
7066   set_die_type (die, type, cu);
7067
7068   /* set_die_type should be already done.  */
7069   set_descriptive_type (type, die, cu);
7070
7071   return type;
7072 }
7073
7074 /* Finish creating a structure or union type, including filling in
7075    its members and creating a symbol for it.  */
7076
7077 static void
7078 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7079 {
7080   struct objfile *objfile = cu->objfile;
7081   struct die_info *child_die = die->child;
7082   struct type *type;
7083
7084   type = get_die_type (die, cu);
7085   if (type == NULL)
7086     type = read_structure_type (die, cu);
7087
7088   if (die->child != NULL && ! die_is_declaration (die, cu))
7089     {
7090       struct field_info fi;
7091       struct die_info *child_die;
7092       VEC (symbolp) *template_args = NULL;
7093       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7094
7095       memset (&fi, 0, sizeof (struct field_info));
7096
7097       child_die = die->child;
7098
7099       while (child_die && child_die->tag)
7100         {
7101           if (child_die->tag == DW_TAG_member
7102               || child_die->tag == DW_TAG_variable)
7103             {
7104               /* NOTE: carlton/2002-11-05: A C++ static data member
7105                  should be a DW_TAG_member that is a declaration, but
7106                  all versions of G++ as of this writing (so through at
7107                  least 3.2.1) incorrectly generate DW_TAG_variable
7108                  tags for them instead.  */
7109               dwarf2_add_field (&fi, child_die, cu);
7110             }
7111           else if (child_die->tag == DW_TAG_subprogram)
7112             {
7113               /* C++ member function.  */
7114               dwarf2_add_member_fn (&fi, child_die, type, cu);
7115             }
7116           else if (child_die->tag == DW_TAG_inheritance)
7117             {
7118               /* C++ base class field.  */
7119               dwarf2_add_field (&fi, child_die, cu);
7120             }
7121           else if (child_die->tag == DW_TAG_typedef)
7122             dwarf2_add_typedef (&fi, child_die, cu);
7123           else if (child_die->tag == DW_TAG_template_type_param
7124                    || child_die->tag == DW_TAG_template_value_param)
7125             {
7126               struct symbol *arg = new_symbol (child_die, NULL, cu);
7127
7128               if (arg != NULL)
7129                 VEC_safe_push (symbolp, template_args, arg);
7130             }
7131
7132           child_die = sibling_die (child_die);
7133         }
7134
7135       /* Attach template arguments to type.  */
7136       if (! VEC_empty (symbolp, template_args))
7137         {
7138           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7139           TYPE_N_TEMPLATE_ARGUMENTS (type)
7140             = VEC_length (symbolp, template_args);
7141           TYPE_TEMPLATE_ARGUMENTS (type)
7142             = obstack_alloc (&objfile->objfile_obstack,
7143                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7144                               * sizeof (struct symbol *)));
7145           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7146                   VEC_address (symbolp, template_args),
7147                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7148                    * sizeof (struct symbol *)));
7149           VEC_free (symbolp, template_args);
7150         }
7151
7152       /* Attach fields and member functions to the type.  */
7153       if (fi.nfields)
7154         dwarf2_attach_fields_to_type (&fi, type, cu);
7155       if (fi.nfnfields)
7156         {
7157           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7158
7159           /* Get the type which refers to the base class (possibly this
7160              class itself) which contains the vtable pointer for the current
7161              class from the DW_AT_containing_type attribute.  This use of
7162              DW_AT_containing_type is a GNU extension.  */
7163
7164           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7165             {
7166               struct type *t = die_containing_type (die, cu);
7167
7168               TYPE_VPTR_BASETYPE (type) = t;
7169               if (type == t)
7170                 {
7171                   int i;
7172
7173                   /* Our own class provides vtbl ptr.  */
7174                   for (i = TYPE_NFIELDS (t) - 1;
7175                        i >= TYPE_N_BASECLASSES (t);
7176                        --i)
7177                     {
7178                       char *fieldname = TYPE_FIELD_NAME (t, i);
7179
7180                       if (is_vtable_name (fieldname, cu))
7181                         {
7182                           TYPE_VPTR_FIELDNO (type) = i;
7183                           break;
7184                         }
7185                     }
7186
7187                   /* Complain if virtual function table field not found.  */
7188                   if (i < TYPE_N_BASECLASSES (t))
7189                     complaint (&symfile_complaints,
7190                                _("virtual function table pointer "
7191                                  "not found when defining class '%s'"),
7192                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7193                                "");
7194                 }
7195               else
7196                 {
7197                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7198                 }
7199             }
7200           else if (cu->producer
7201                    && strncmp (cu->producer,
7202                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7203             {
7204               /* The IBM XLC compiler does not provide direct indication
7205                  of the containing type, but the vtable pointer is
7206                  always named __vfp.  */
7207
7208               int i;
7209
7210               for (i = TYPE_NFIELDS (type) - 1;
7211                    i >= TYPE_N_BASECLASSES (type);
7212                    --i)
7213                 {
7214                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7215                     {
7216                       TYPE_VPTR_FIELDNO (type) = i;
7217                       TYPE_VPTR_BASETYPE (type) = type;
7218                       break;
7219                     }
7220                 }
7221             }
7222         }
7223
7224       /* Copy fi.typedef_field_list linked list elements content into the
7225          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7226       if (fi.typedef_field_list)
7227         {
7228           int i = fi.typedef_field_list_count;
7229
7230           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7231           TYPE_TYPEDEF_FIELD_ARRAY (type)
7232             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7233           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7234
7235           /* Reverse the list order to keep the debug info elements order.  */
7236           while (--i >= 0)
7237             {
7238               struct typedef_field *dest, *src;
7239
7240               dest = &TYPE_TYPEDEF_FIELD (type, i);
7241               src = &fi.typedef_field_list->field;
7242               fi.typedef_field_list = fi.typedef_field_list->next;
7243               *dest = *src;
7244             }
7245         }
7246
7247       do_cleanups (back_to);
7248     }
7249
7250   quirk_gcc_member_function_pointer (type, cu->objfile);
7251
7252   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7253      snapshots) has been known to create a die giving a declaration
7254      for a class that has, as a child, a die giving a definition for a
7255      nested class.  So we have to process our children even if the
7256      current die is a declaration.  Normally, of course, a declaration
7257      won't have any children at all.  */
7258
7259   while (child_die != NULL && child_die->tag)
7260     {
7261       if (child_die->tag == DW_TAG_member
7262           || child_die->tag == DW_TAG_variable
7263           || child_die->tag == DW_TAG_inheritance
7264           || child_die->tag == DW_TAG_template_value_param
7265           || child_die->tag == DW_TAG_template_type_param)
7266         {
7267           /* Do nothing.  */
7268         }
7269       else
7270         process_die (child_die, cu);
7271
7272       child_die = sibling_die (child_die);
7273     }
7274
7275   /* Do not consider external references.  According to the DWARF standard,
7276      these DIEs are identified by the fact that they have no byte_size
7277      attribute, and a declaration attribute.  */
7278   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7279       || !die_is_declaration (die, cu))
7280     new_symbol (die, type, cu);
7281 }
7282
7283 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7284    complete the type's fields yet, or create any symbols.  */
7285
7286 static struct type *
7287 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7288 {
7289   struct objfile *objfile = cu->objfile;
7290   struct type *type;
7291   struct attribute *attr;
7292   const char *name;
7293
7294   /* If the definition of this type lives in .debug_types, read that type.
7295      Don't follow DW_AT_specification though, that will take us back up
7296      the chain and we want to go down.  */
7297   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7298   if (attr)
7299     {
7300       struct dwarf2_cu *type_cu = cu;
7301       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7302
7303       type = read_type_die (type_die, type_cu);
7304
7305       /* TYPE_CU may not be the same as CU.
7306          Ensure TYPE is recorded in CU's type_hash table.  */
7307       return set_die_type (die, type, cu);
7308     }
7309
7310   type = alloc_type (objfile);
7311
7312   TYPE_CODE (type) = TYPE_CODE_ENUM;
7313   name = dwarf2_full_name (NULL, die, cu);
7314   if (name != NULL)
7315     TYPE_TAG_NAME (type) = (char *) name;
7316
7317   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7318   if (attr)
7319     {
7320       TYPE_LENGTH (type) = DW_UNSND (attr);
7321     }
7322   else
7323     {
7324       TYPE_LENGTH (type) = 0;
7325     }
7326
7327   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7328      declared as private in the package spec, and then defined only
7329      inside the package body.  Such types are known as Taft Amendment
7330      Types.  When another package uses such a type, an incomplete DIE
7331      may be generated by the compiler.  */
7332   if (die_is_declaration (die, cu))
7333     TYPE_STUB (type) = 1;
7334
7335   return set_die_type (die, type, cu);
7336 }
7337
7338 /* Given a pointer to a die which begins an enumeration, process all
7339    the dies that define the members of the enumeration, and create the
7340    symbol for the enumeration type.
7341
7342    NOTE: We reverse the order of the element list.  */
7343
7344 static void
7345 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7346 {
7347   struct type *this_type;
7348
7349   this_type = get_die_type (die, cu);
7350   if (this_type == NULL)
7351     this_type = read_enumeration_type (die, cu);
7352
7353   if (die->child != NULL)
7354     {
7355       struct die_info *child_die;
7356       struct symbol *sym;
7357       struct field *fields = NULL;
7358       int num_fields = 0;
7359       int unsigned_enum = 1;
7360       char *name;
7361
7362       child_die = die->child;
7363       while (child_die && child_die->tag)
7364         {
7365           if (child_die->tag != DW_TAG_enumerator)
7366             {
7367               process_die (child_die, cu);
7368             }
7369           else
7370             {
7371               name = dwarf2_name (child_die, cu);
7372               if (name)
7373                 {
7374                   sym = new_symbol (child_die, this_type, cu);
7375                   if (SYMBOL_VALUE (sym) < 0)
7376                     unsigned_enum = 0;
7377
7378                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7379                     {
7380                       fields = (struct field *)
7381                         xrealloc (fields,
7382                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7383                                   * sizeof (struct field));
7384                     }
7385
7386                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7387                   FIELD_TYPE (fields[num_fields]) = NULL;
7388                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7389                   FIELD_BITSIZE (fields[num_fields]) = 0;
7390
7391                   num_fields++;
7392                 }
7393             }
7394
7395           child_die = sibling_die (child_die);
7396         }
7397
7398       if (num_fields)
7399         {
7400           TYPE_NFIELDS (this_type) = num_fields;
7401           TYPE_FIELDS (this_type) = (struct field *)
7402             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7403           memcpy (TYPE_FIELDS (this_type), fields,
7404                   sizeof (struct field) * num_fields);
7405           xfree (fields);
7406         }
7407       if (unsigned_enum)
7408         TYPE_UNSIGNED (this_type) = 1;
7409     }
7410
7411   new_symbol (die, this_type, cu);
7412 }
7413
7414 /* Extract all information from a DW_TAG_array_type DIE and put it in
7415    the DIE's type field.  For now, this only handles one dimensional
7416    arrays.  */
7417
7418 static struct type *
7419 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7420 {
7421   struct objfile *objfile = cu->objfile;
7422   struct die_info *child_die;
7423   struct type *type;
7424   struct type *element_type, *range_type, *index_type;
7425   struct type **range_types = NULL;
7426   struct attribute *attr;
7427   int ndim = 0;
7428   struct cleanup *back_to;
7429   char *name;
7430
7431   element_type = die_type (die, cu);
7432
7433   /* The die_type call above may have already set the type for this DIE.  */
7434   type = get_die_type (die, cu);
7435   if (type)
7436     return type;
7437
7438   /* Irix 6.2 native cc creates array types without children for
7439      arrays with unspecified length.  */
7440   if (die->child == NULL)
7441     {
7442       index_type = objfile_type (objfile)->builtin_int;
7443       range_type = create_range_type (NULL, index_type, 0, -1);
7444       type = create_array_type (NULL, element_type, range_type);
7445       return set_die_type (die, type, cu);
7446     }
7447
7448   back_to = make_cleanup (null_cleanup, NULL);
7449   child_die = die->child;
7450   while (child_die && child_die->tag)
7451     {
7452       if (child_die->tag == DW_TAG_subrange_type)
7453         {
7454           struct type *child_type = read_type_die (child_die, cu);
7455
7456           if (child_type != NULL)
7457             {
7458               /* The range type was succesfully read.  Save it for the
7459                  array type creation.  */
7460               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7461                 {
7462                   range_types = (struct type **)
7463                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7464                               * sizeof (struct type *));
7465                   if (ndim == 0)
7466                     make_cleanup (free_current_contents, &range_types);
7467                 }
7468               range_types[ndim++] = child_type;
7469             }
7470         }
7471       child_die = sibling_die (child_die);
7472     }
7473
7474   /* Dwarf2 dimensions are output from left to right, create the
7475      necessary array types in backwards order.  */
7476
7477   type = element_type;
7478
7479   if (read_array_order (die, cu) == DW_ORD_col_major)
7480     {
7481       int i = 0;
7482
7483       while (i < ndim)
7484         type = create_array_type (NULL, type, range_types[i++]);
7485     }
7486   else
7487     {
7488       while (ndim-- > 0)
7489         type = create_array_type (NULL, type, range_types[ndim]);
7490     }
7491
7492   /* Understand Dwarf2 support for vector types (like they occur on
7493      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7494      array type.  This is not part of the Dwarf2/3 standard yet, but a
7495      custom vendor extension.  The main difference between a regular
7496      array and the vector variant is that vectors are passed by value
7497      to functions.  */
7498   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7499   if (attr)
7500     make_vector_type (type);
7501
7502   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7503      implementation may choose to implement triple vectors using this
7504      attribute.  */
7505   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7506   if (attr)
7507     {
7508       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7509         TYPE_LENGTH (type) = DW_UNSND (attr);
7510       else
7511         complaint (&symfile_complaints,
7512                    _("DW_AT_byte_size for array type smaller "
7513                      "than the total size of elements"));
7514     }
7515
7516   name = dwarf2_name (die, cu);
7517   if (name)
7518     TYPE_NAME (type) = name;
7519
7520   /* Install the type in the die.  */
7521   set_die_type (die, type, cu);
7522
7523   /* set_die_type should be already done.  */
7524   set_descriptive_type (type, die, cu);
7525
7526   do_cleanups (back_to);
7527
7528   return type;
7529 }
7530
7531 static enum dwarf_array_dim_ordering
7532 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7533 {
7534   struct attribute *attr;
7535
7536   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7537
7538   if (attr) return DW_SND (attr);
7539
7540   /* GNU F77 is a special case, as at 08/2004 array type info is the
7541      opposite order to the dwarf2 specification, but data is still
7542      laid out as per normal fortran.
7543
7544      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7545      version checking.  */
7546
7547   if (cu->language == language_fortran
7548       && cu->producer && strstr (cu->producer, "GNU F77"))
7549     {
7550       return DW_ORD_row_major;
7551     }
7552
7553   switch (cu->language_defn->la_array_ordering)
7554     {
7555     case array_column_major:
7556       return DW_ORD_col_major;
7557     case array_row_major:
7558     default:
7559       return DW_ORD_row_major;
7560     };
7561 }
7562
7563 /* Extract all information from a DW_TAG_set_type DIE and put it in
7564    the DIE's type field.  */
7565
7566 static struct type *
7567 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7568 {
7569   struct type *domain_type, *set_type;
7570   struct attribute *attr;
7571
7572   domain_type = die_type (die, cu);
7573
7574   /* The die_type call above may have already set the type for this DIE.  */
7575   set_type = get_die_type (die, cu);
7576   if (set_type)
7577     return set_type;
7578
7579   set_type = create_set_type (NULL, domain_type);
7580
7581   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7582   if (attr)
7583     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7584
7585   return set_die_type (die, set_type, cu);
7586 }
7587
7588 /* First cut: install each common block member as a global variable.  */
7589
7590 static void
7591 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7592 {
7593   struct die_info *child_die;
7594   struct attribute *attr;
7595   struct symbol *sym;
7596   CORE_ADDR base = (CORE_ADDR) 0;
7597
7598   attr = dwarf2_attr (die, DW_AT_location, cu);
7599   if (attr)
7600     {
7601       /* Support the .debug_loc offsets.  */
7602       if (attr_form_is_block (attr))
7603         {
7604           base = decode_locdesc (DW_BLOCK (attr), cu);
7605         }
7606       else if (attr_form_is_section_offset (attr))
7607         {
7608           dwarf2_complex_location_expr_complaint ();
7609         }
7610       else
7611         {
7612           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7613                                                  "common block member");
7614         }
7615     }
7616   if (die->child != NULL)
7617     {
7618       child_die = die->child;
7619       while (child_die && child_die->tag)
7620         {
7621           sym = new_symbol (child_die, NULL, cu);
7622           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7623           if (sym != NULL && attr != NULL)
7624             {
7625               CORE_ADDR byte_offset = 0;
7626
7627               if (attr_form_is_section_offset (attr))
7628                 dwarf2_complex_location_expr_complaint ();
7629               else if (attr_form_is_constant (attr))
7630                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7631               else if (attr_form_is_block (attr))
7632                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7633               else
7634                 dwarf2_complex_location_expr_complaint ();
7635
7636               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7637               add_symbol_to_list (sym, &global_symbols);
7638             }
7639           child_die = sibling_die (child_die);
7640         }
7641     }
7642 }
7643
7644 /* Create a type for a C++ namespace.  */
7645
7646 static struct type *
7647 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7648 {
7649   struct objfile *objfile = cu->objfile;
7650   const char *previous_prefix, *name;
7651   int is_anonymous;
7652   struct type *type;
7653
7654   /* For extensions, reuse the type of the original namespace.  */
7655   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7656     {
7657       struct die_info *ext_die;
7658       struct dwarf2_cu *ext_cu = cu;
7659
7660       ext_die = dwarf2_extension (die, &ext_cu);
7661       type = read_type_die (ext_die, ext_cu);
7662
7663       /* EXT_CU may not be the same as CU.
7664          Ensure TYPE is recorded in CU's type_hash table.  */
7665       return set_die_type (die, type, cu);
7666     }
7667
7668   name = namespace_name (die, &is_anonymous, cu);
7669
7670   /* Now build the name of the current namespace.  */
7671
7672   previous_prefix = determine_prefix (die, cu);
7673   if (previous_prefix[0] != '\0')
7674     name = typename_concat (&objfile->objfile_obstack,
7675                             previous_prefix, name, 0, cu);
7676
7677   /* Create the type.  */
7678   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7679                     objfile);
7680   TYPE_NAME (type) = (char *) name;
7681   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7682
7683   return set_die_type (die, type, cu);
7684 }
7685
7686 /* Read a C++ namespace.  */
7687
7688 static void
7689 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7690 {
7691   struct objfile *objfile = cu->objfile;
7692   int is_anonymous;
7693
7694   /* Add a symbol associated to this if we haven't seen the namespace
7695      before.  Also, add a using directive if it's an anonymous
7696      namespace.  */
7697
7698   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7699     {
7700       struct type *type;
7701
7702       type = read_type_die (die, cu);
7703       new_symbol (die, type, cu);
7704
7705       namespace_name (die, &is_anonymous, cu);
7706       if (is_anonymous)
7707         {
7708           const char *previous_prefix = determine_prefix (die, cu);
7709
7710           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7711                                   NULL, &objfile->objfile_obstack);
7712         }
7713     }
7714
7715   if (die->child != NULL)
7716     {
7717       struct die_info *child_die = die->child;
7718
7719       while (child_die && child_die->tag)
7720         {
7721           process_die (child_die, cu);
7722           child_die = sibling_die (child_die);
7723         }
7724     }
7725 }
7726
7727 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7728    imported module.  Still we need that type as local Fortran "use ... only"
7729    declaration imports depend on the created type in determine_prefix.  */
7730
7731 static struct type *
7732 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7733 {
7734   struct objfile *objfile = cu->objfile;
7735   char *module_name;
7736   struct type *type;
7737
7738   module_name = dwarf2_name (die, cu);
7739   if (!module_name)
7740     complaint (&symfile_complaints,
7741                _("DW_TAG_module has no name, offset 0x%x"),
7742                die->offset);
7743   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7744
7745   /* determine_prefix uses TYPE_TAG_NAME.  */
7746   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7747
7748   return set_die_type (die, type, cu);
7749 }
7750
7751 /* Read a Fortran module.  */
7752
7753 static void
7754 read_module (struct die_info *die, struct dwarf2_cu *cu)
7755 {
7756   struct die_info *child_die = die->child;
7757
7758   while (child_die && child_die->tag)
7759     {
7760       process_die (child_die, cu);
7761       child_die = sibling_die (child_die);
7762     }
7763 }
7764
7765 /* Return the name of the namespace represented by DIE.  Set
7766    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7767    namespace.  */
7768
7769 static const char *
7770 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7771 {
7772   struct die_info *current_die;
7773   const char *name = NULL;
7774
7775   /* Loop through the extensions until we find a name.  */
7776
7777   for (current_die = die;
7778        current_die != NULL;
7779        current_die = dwarf2_extension (die, &cu))
7780     {
7781       name = dwarf2_name (current_die, cu);
7782       if (name != NULL)
7783         break;
7784     }
7785
7786   /* Is it an anonymous namespace?  */
7787
7788   *is_anonymous = (name == NULL);
7789   if (*is_anonymous)
7790     name = "(anonymous namespace)";
7791
7792   return name;
7793 }
7794
7795 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7796    the user defined type vector.  */
7797
7798 static struct type *
7799 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7800 {
7801   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7802   struct comp_unit_head *cu_header = &cu->header;
7803   struct type *type;
7804   struct attribute *attr_byte_size;
7805   struct attribute *attr_address_class;
7806   int byte_size, addr_class;
7807   struct type *target_type;
7808
7809   target_type = die_type (die, cu);
7810
7811   /* The die_type call above may have already set the type for this DIE.  */
7812   type = get_die_type (die, cu);
7813   if (type)
7814     return type;
7815
7816   type = lookup_pointer_type (target_type);
7817
7818   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7819   if (attr_byte_size)
7820     byte_size = DW_UNSND (attr_byte_size);
7821   else
7822     byte_size = cu_header->addr_size;
7823
7824   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7825   if (attr_address_class)
7826     addr_class = DW_UNSND (attr_address_class);
7827   else
7828     addr_class = DW_ADDR_none;
7829
7830   /* If the pointer size or address class is different than the
7831      default, create a type variant marked as such and set the
7832      length accordingly.  */
7833   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7834     {
7835       if (gdbarch_address_class_type_flags_p (gdbarch))
7836         {
7837           int type_flags;
7838
7839           type_flags = gdbarch_address_class_type_flags
7840                          (gdbarch, byte_size, addr_class);
7841           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7842                       == 0);
7843           type = make_type_with_address_space (type, type_flags);
7844         }
7845       else if (TYPE_LENGTH (type) != byte_size)
7846         {
7847           complaint (&symfile_complaints,
7848                      _("invalid pointer size %d"), byte_size);
7849         }
7850       else
7851         {
7852           /* Should we also complain about unhandled address classes?  */
7853         }
7854     }
7855
7856   TYPE_LENGTH (type) = byte_size;
7857   return set_die_type (die, type, cu);
7858 }
7859
7860 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7861    the user defined type vector.  */
7862
7863 static struct type *
7864 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7865 {
7866   struct type *type;
7867   struct type *to_type;
7868   struct type *domain;
7869
7870   to_type = die_type (die, cu);
7871   domain = die_containing_type (die, cu);
7872
7873   /* The calls above may have already set the type for this DIE.  */
7874   type = get_die_type (die, cu);
7875   if (type)
7876     return type;
7877
7878   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7879     type = lookup_methodptr_type (to_type);
7880   else
7881     type = lookup_memberptr_type (to_type, domain);
7882
7883   return set_die_type (die, type, cu);
7884 }
7885
7886 /* Extract all information from a DW_TAG_reference_type DIE and add to
7887    the user defined type vector.  */
7888
7889 static struct type *
7890 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7891 {
7892   struct comp_unit_head *cu_header = &cu->header;
7893   struct type *type, *target_type;
7894   struct attribute *attr;
7895
7896   target_type = die_type (die, cu);
7897
7898   /* The die_type call above may have already set the type for this DIE.  */
7899   type = get_die_type (die, cu);
7900   if (type)
7901     return type;
7902
7903   type = lookup_reference_type (target_type);
7904   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7905   if (attr)
7906     {
7907       TYPE_LENGTH (type) = DW_UNSND (attr);
7908     }
7909   else
7910     {
7911       TYPE_LENGTH (type) = cu_header->addr_size;
7912     }
7913   return set_die_type (die, type, cu);
7914 }
7915
7916 static struct type *
7917 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7918 {
7919   struct type *base_type, *cv_type;
7920
7921   base_type = die_type (die, cu);
7922
7923   /* The die_type call above may have already set the type for this DIE.  */
7924   cv_type = get_die_type (die, cu);
7925   if (cv_type)
7926     return cv_type;
7927
7928   /* In case the const qualifier is applied to an array type, the element type
7929      is so qualified, not the array type (section 6.7.3 of C99).  */
7930   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7931     {
7932       struct type *el_type, *inner_array;
7933
7934       base_type = copy_type (base_type);
7935       inner_array = base_type;
7936
7937       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7938         {
7939           TYPE_TARGET_TYPE (inner_array) =
7940             copy_type (TYPE_TARGET_TYPE (inner_array));
7941           inner_array = TYPE_TARGET_TYPE (inner_array);
7942         }
7943
7944       el_type = TYPE_TARGET_TYPE (inner_array);
7945       TYPE_TARGET_TYPE (inner_array) =
7946         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7947
7948       return set_die_type (die, base_type, cu);
7949     }
7950
7951   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7952   return set_die_type (die, cv_type, cu);
7953 }
7954
7955 static struct type *
7956 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7957 {
7958   struct type *base_type, *cv_type;
7959
7960   base_type = die_type (die, cu);
7961
7962   /* The die_type call above may have already set the type for this DIE.  */
7963   cv_type = get_die_type (die, cu);
7964   if (cv_type)
7965     return cv_type;
7966
7967   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7968   return set_die_type (die, cv_type, cu);
7969 }
7970
7971 /* Extract all information from a DW_TAG_string_type DIE and add to
7972    the user defined type vector.  It isn't really a user defined type,
7973    but it behaves like one, with other DIE's using an AT_user_def_type
7974    attribute to reference it.  */
7975
7976 static struct type *
7977 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7978 {
7979   struct objfile *objfile = cu->objfile;
7980   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7981   struct type *type, *range_type, *index_type, *char_type;
7982   struct attribute *attr;
7983   unsigned int length;
7984
7985   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7986   if (attr)
7987     {
7988       length = DW_UNSND (attr);
7989     }
7990   else
7991     {
7992       /* Check for the DW_AT_byte_size attribute.  */
7993       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7994       if (attr)
7995         {
7996           length = DW_UNSND (attr);
7997         }
7998       else
7999         {
8000           length = 1;
8001         }
8002     }
8003
8004   index_type = objfile_type (objfile)->builtin_int;
8005   range_type = create_range_type (NULL, index_type, 1, length);
8006   char_type = language_string_char_type (cu->language_defn, gdbarch);
8007   type = create_string_type (NULL, char_type, range_type);
8008
8009   return set_die_type (die, type, cu);
8010 }
8011
8012 /* Handle DIES due to C code like:
8013
8014    struct foo
8015    {
8016    int (*funcp)(int a, long l);
8017    int b;
8018    };
8019
8020    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8021
8022 static struct type *
8023 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8024 {
8025   struct type *type;            /* Type that this function returns.  */
8026   struct type *ftype;           /* Function that returns above type.  */
8027   struct attribute *attr;
8028
8029   type = die_type (die, cu);
8030
8031   /* The die_type call above may have already set the type for this DIE.  */
8032   ftype = get_die_type (die, cu);
8033   if (ftype)
8034     return ftype;
8035
8036   ftype = lookup_function_type (type);
8037
8038   /* All functions in C++, Pascal and Java have prototypes.  */
8039   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8040   if ((attr && (DW_UNSND (attr) != 0))
8041       || cu->language == language_cplus
8042       || cu->language == language_java
8043       || cu->language == language_pascal)
8044     TYPE_PROTOTYPED (ftype) = 1;
8045   else if (producer_is_realview (cu->producer))
8046     /* RealView does not emit DW_AT_prototyped.  We can not
8047        distinguish prototyped and unprototyped functions; default to
8048        prototyped, since that is more common in modern code (and
8049        RealView warns about unprototyped functions).  */
8050     TYPE_PROTOTYPED (ftype) = 1;
8051
8052   /* Store the calling convention in the type if it's available in
8053      the subroutine die.  Otherwise set the calling convention to
8054      the default value DW_CC_normal.  */
8055   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8056   if (attr)
8057     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8058   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8059     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8060   else
8061     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8062
8063   /* We need to add the subroutine type to the die immediately so
8064      we don't infinitely recurse when dealing with parameters
8065      declared as the same subroutine type.  */
8066   set_die_type (die, ftype, cu);
8067
8068   if (die->child != NULL)
8069     {
8070       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8071       struct die_info *child_die;
8072       int nparams, iparams;
8073
8074       /* Count the number of parameters.
8075          FIXME: GDB currently ignores vararg functions, but knows about
8076          vararg member functions.  */
8077       nparams = 0;
8078       child_die = die->child;
8079       while (child_die && child_die->tag)
8080         {
8081           if (child_die->tag == DW_TAG_formal_parameter)
8082             nparams++;
8083           else if (child_die->tag == DW_TAG_unspecified_parameters)
8084             TYPE_VARARGS (ftype) = 1;
8085           child_die = sibling_die (child_die);
8086         }
8087
8088       /* Allocate storage for parameters and fill them in.  */
8089       TYPE_NFIELDS (ftype) = nparams;
8090       TYPE_FIELDS (ftype) = (struct field *)
8091         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8092
8093       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8094          even if we error out during the parameters reading below.  */
8095       for (iparams = 0; iparams < nparams; iparams++)
8096         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8097
8098       iparams = 0;
8099       child_die = die->child;
8100       while (child_die && child_die->tag)
8101         {
8102           if (child_die->tag == DW_TAG_formal_parameter)
8103             {
8104               struct type *arg_type;
8105
8106               /* DWARF version 2 has no clean way to discern C++
8107                  static and non-static member functions.  G++ helps
8108                  GDB by marking the first parameter for non-static
8109                  member functions (which is the this pointer) as
8110                  artificial.  We pass this information to
8111                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8112
8113                  DWARF version 3 added DW_AT_object_pointer, which GCC
8114                  4.5 does not yet generate.  */
8115               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8116               if (attr)
8117                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8118               else
8119                 {
8120                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8121
8122                   /* GCC/43521: In java, the formal parameter
8123                      "this" is sometimes not marked with DW_AT_artificial.  */
8124                   if (cu->language == language_java)
8125                     {
8126                       const char *name = dwarf2_name (child_die, cu);
8127
8128                       if (name && !strcmp (name, "this"))
8129                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8130                     }
8131                 }
8132               arg_type = die_type (child_die, cu);
8133
8134               /* RealView does not mark THIS as const, which the testsuite
8135                  expects.  GCC marks THIS as const in method definitions,
8136                  but not in the class specifications (GCC PR 43053).  */
8137               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8138                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8139                 {
8140                   int is_this = 0;
8141                   struct dwarf2_cu *arg_cu = cu;
8142                   const char *name = dwarf2_name (child_die, cu);
8143
8144                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8145                   if (attr)
8146                     {
8147                       /* If the compiler emits this, use it.  */
8148                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8149                         is_this = 1;
8150                     }
8151                   else if (name && strcmp (name, "this") == 0)
8152                     /* Function definitions will have the argument names.  */
8153                     is_this = 1;
8154                   else if (name == NULL && iparams == 0)
8155                     /* Declarations may not have the names, so like
8156                        elsewhere in GDB, assume an artificial first
8157                        argument is "this".  */
8158                     is_this = 1;
8159
8160                   if (is_this)
8161                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8162                                              arg_type, 0);
8163                 }
8164
8165               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8166               iparams++;
8167             }
8168           child_die = sibling_die (child_die);
8169         }
8170     }
8171
8172   return ftype;
8173 }
8174
8175 static struct type *
8176 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8177 {
8178   struct objfile *objfile = cu->objfile;
8179   const char *name = NULL;
8180   struct type *this_type;
8181
8182   name = dwarf2_full_name (NULL, die, cu);
8183   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8184                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8185   TYPE_NAME (this_type) = (char *) name;
8186   set_die_type (die, this_type, cu);
8187   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8188   return this_type;
8189 }
8190
8191 /* Find a representation of a given base type and install
8192    it in the TYPE field of the die.  */
8193
8194 static struct type *
8195 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8196 {
8197   struct objfile *objfile = cu->objfile;
8198   struct type *type;
8199   struct attribute *attr;
8200   int encoding = 0, size = 0;
8201   char *name;
8202   enum type_code code = TYPE_CODE_INT;
8203   int type_flags = 0;
8204   struct type *target_type = NULL;
8205
8206   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8207   if (attr)
8208     {
8209       encoding = DW_UNSND (attr);
8210     }
8211   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8212   if (attr)
8213     {
8214       size = DW_UNSND (attr);
8215     }
8216   name = dwarf2_name (die, cu);
8217   if (!name)
8218     {
8219       complaint (&symfile_complaints,
8220                  _("DW_AT_name missing from DW_TAG_base_type"));
8221     }
8222
8223   switch (encoding)
8224     {
8225       case DW_ATE_address:
8226         /* Turn DW_ATE_address into a void * pointer.  */
8227         code = TYPE_CODE_PTR;
8228         type_flags |= TYPE_FLAG_UNSIGNED;
8229         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8230         break;
8231       case DW_ATE_boolean:
8232         code = TYPE_CODE_BOOL;
8233         type_flags |= TYPE_FLAG_UNSIGNED;
8234         break;
8235       case DW_ATE_complex_float:
8236         code = TYPE_CODE_COMPLEX;
8237         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8238         break;
8239       case DW_ATE_decimal_float:
8240         code = TYPE_CODE_DECFLOAT;
8241         break;
8242       case DW_ATE_float:
8243         code = TYPE_CODE_FLT;
8244         break;
8245       case DW_ATE_signed:
8246         break;
8247       case DW_ATE_unsigned:
8248         type_flags |= TYPE_FLAG_UNSIGNED;
8249         break;
8250       case DW_ATE_signed_char:
8251         if (cu->language == language_ada || cu->language == language_m2
8252             || cu->language == language_pascal)
8253           code = TYPE_CODE_CHAR;
8254         break;
8255       case DW_ATE_unsigned_char:
8256         if (cu->language == language_ada || cu->language == language_m2
8257             || cu->language == language_pascal)
8258           code = TYPE_CODE_CHAR;
8259         type_flags |= TYPE_FLAG_UNSIGNED;
8260         break;
8261       case DW_ATE_UTF:
8262         /* We just treat this as an integer and then recognize the
8263            type by name elsewhere.  */
8264         break;
8265
8266       default:
8267         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8268                    dwarf_type_encoding_name (encoding));
8269         break;
8270     }
8271
8272   type = init_type (code, size, type_flags, NULL, objfile);
8273   TYPE_NAME (type) = name;
8274   TYPE_TARGET_TYPE (type) = target_type;
8275
8276   if (name && strcmp (name, "char") == 0)
8277     TYPE_NOSIGN (type) = 1;
8278
8279   return set_die_type (die, type, cu);
8280 }
8281
8282 /* Read the given DW_AT_subrange DIE.  */
8283
8284 static struct type *
8285 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8286 {
8287   struct type *base_type;
8288   struct type *range_type;
8289   struct attribute *attr;
8290   LONGEST low = 0;
8291   LONGEST high = -1;
8292   char *name;
8293   LONGEST negative_mask;
8294
8295   base_type = die_type (die, cu);
8296   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8297   check_typedef (base_type);
8298
8299   /* The die_type call above may have already set the type for this DIE.  */
8300   range_type = get_die_type (die, cu);
8301   if (range_type)
8302     return range_type;
8303
8304   if (cu->language == language_fortran)
8305     {
8306       /* FORTRAN implies a lower bound of 1, if not given.  */
8307       low = 1;
8308     }
8309
8310   /* FIXME: For variable sized arrays either of these could be
8311      a variable rather than a constant value.  We'll allow it,
8312      but we don't know how to handle it.  */
8313   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8314   if (attr)
8315     low = dwarf2_get_attr_constant_value (attr, 0);
8316
8317   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8318   if (attr)
8319     {
8320       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8321         {
8322           /* GCC encodes arrays with unspecified or dynamic length
8323              with a DW_FORM_block1 attribute or a reference attribute.
8324              FIXME: GDB does not yet know how to handle dynamic
8325              arrays properly, treat them as arrays with unspecified
8326              length for now.
8327
8328              FIXME: jimb/2003-09-22: GDB does not really know
8329              how to handle arrays of unspecified length
8330              either; we just represent them as zero-length
8331              arrays.  Choose an appropriate upper bound given
8332              the lower bound we've computed above.  */
8333           high = low - 1;
8334         }
8335       else
8336         high = dwarf2_get_attr_constant_value (attr, 1);
8337     }
8338   else
8339     {
8340       attr = dwarf2_attr (die, DW_AT_count, cu);
8341       if (attr)
8342         {
8343           int count = dwarf2_get_attr_constant_value (attr, 1);
8344           high = low + count - 1;
8345         }
8346       else
8347         {
8348           /* Unspecified array length.  */
8349           high = low - 1;
8350         }
8351     }
8352
8353   /* Dwarf-2 specifications explicitly allows to create subrange types
8354      without specifying a base type.
8355      In that case, the base type must be set to the type of
8356      the lower bound, upper bound or count, in that order, if any of these
8357      three attributes references an object that has a type.
8358      If no base type is found, the Dwarf-2 specifications say that
8359      a signed integer type of size equal to the size of an address should
8360      be used.
8361      For the following C code: `extern char gdb_int [];'
8362      GCC produces an empty range DIE.
8363      FIXME: muller/2010-05-28: Possible references to object for low bound,
8364      high bound or count are not yet handled by this code.  */
8365   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8366     {
8367       struct objfile *objfile = cu->objfile;
8368       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8369       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8370       struct type *int_type = objfile_type (objfile)->builtin_int;
8371
8372       /* Test "int", "long int", and "long long int" objfile types,
8373          and select the first one having a size above or equal to the
8374          architecture address size.  */
8375       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8376         base_type = int_type;
8377       else
8378         {
8379           int_type = objfile_type (objfile)->builtin_long;
8380           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8381             base_type = int_type;
8382           else
8383             {
8384               int_type = objfile_type (objfile)->builtin_long_long;
8385               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8386                 base_type = int_type;
8387             }
8388         }
8389     }
8390
8391   negative_mask =
8392     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8393   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8394     low |= negative_mask;
8395   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8396     high |= negative_mask;
8397
8398   range_type = create_range_type (NULL, base_type, low, high);
8399
8400   /* Mark arrays with dynamic length at least as an array of unspecified
8401      length.  GDB could check the boundary but before it gets implemented at
8402      least allow accessing the array elements.  */
8403   if (attr && attr->form == DW_FORM_block1)
8404     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8405
8406   /* Ada expects an empty array on no boundary attributes.  */
8407   if (attr == NULL && cu->language != language_ada)
8408     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8409
8410   name = dwarf2_name (die, cu);
8411   if (name)
8412     TYPE_NAME (range_type) = name;
8413
8414   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8415   if (attr)
8416     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8417
8418   set_die_type (die, range_type, cu);
8419
8420   /* set_die_type should be already done.  */
8421   set_descriptive_type (range_type, die, cu);
8422
8423   return range_type;
8424 }
8425
8426 static struct type *
8427 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8428 {
8429   struct type *type;
8430
8431   /* For now, we only support the C meaning of an unspecified type: void.  */
8432
8433   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8434   TYPE_NAME (type) = dwarf2_name (die, cu);
8435
8436   return set_die_type (die, type, cu);
8437 }
8438
8439 /* Trivial hash function for die_info: the hash value of a DIE
8440    is its offset in .debug_info for this objfile.  */
8441
8442 static hashval_t
8443 die_hash (const void *item)
8444 {
8445   const struct die_info *die = item;
8446
8447   return die->offset;
8448 }
8449
8450 /* Trivial comparison function for die_info structures: two DIEs
8451    are equal if they have the same offset.  */
8452
8453 static int
8454 die_eq (const void *item_lhs, const void *item_rhs)
8455 {
8456   const struct die_info *die_lhs = item_lhs;
8457   const struct die_info *die_rhs = item_rhs;
8458
8459   return die_lhs->offset == die_rhs->offset;
8460 }
8461
8462 /* Read a whole compilation unit into a linked list of dies.  */
8463
8464 static struct die_info *
8465 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8466 {
8467   struct die_reader_specs reader_specs;
8468   int read_abbrevs = 0;
8469   struct cleanup *back_to = NULL;
8470   struct die_info *die;
8471
8472   if (cu->dwarf2_abbrevs == NULL)
8473     {
8474       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8475       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8476       read_abbrevs = 1;
8477     }
8478
8479   gdb_assert (cu->die_hash == NULL);
8480   cu->die_hash
8481     = htab_create_alloc_ex (cu->header.length / 12,
8482                             die_hash,
8483                             die_eq,
8484                             NULL,
8485                             &cu->comp_unit_obstack,
8486                             hashtab_obstack_allocate,
8487                             dummy_obstack_deallocate);
8488
8489   init_cu_die_reader (&reader_specs, cu);
8490
8491   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8492
8493   if (read_abbrevs)
8494     do_cleanups (back_to);
8495
8496   return die;
8497 }
8498
8499 /* Main entry point for reading a DIE and all children.
8500    Read the DIE and dump it if requested.  */
8501
8502 static struct die_info *
8503 read_die_and_children (const struct die_reader_specs *reader,
8504                        gdb_byte *info_ptr,
8505                        gdb_byte **new_info_ptr,
8506                        struct die_info *parent)
8507 {
8508   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8509                                                      new_info_ptr, parent);
8510
8511   if (dwarf2_die_debug)
8512     {
8513       fprintf_unfiltered (gdb_stdlog,
8514                           "\nRead die from %s of %s:\n",
8515                           reader->buffer == dwarf2_per_objfile->info.buffer
8516                           ? ".debug_info"
8517                           : reader->buffer == dwarf2_per_objfile->types.buffer
8518                           ? ".debug_types"
8519                           : "unknown section",
8520                           reader->abfd->filename);
8521       dump_die (result, dwarf2_die_debug);
8522     }
8523
8524   return result;
8525 }
8526
8527 /* Read a single die and all its descendents.  Set the die's sibling
8528    field to NULL; set other fields in the die correctly, and set all
8529    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8530    location of the info_ptr after reading all of those dies.  PARENT
8531    is the parent of the die in question.  */
8532
8533 static struct die_info *
8534 read_die_and_children_1 (const struct die_reader_specs *reader,
8535                          gdb_byte *info_ptr,
8536                          gdb_byte **new_info_ptr,
8537                          struct die_info *parent)
8538 {
8539   struct die_info *die;
8540   gdb_byte *cur_ptr;
8541   int has_children;
8542
8543   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8544   if (die == NULL)
8545     {
8546       *new_info_ptr = cur_ptr;
8547       return NULL;
8548     }
8549   store_in_ref_table (die, reader->cu);
8550
8551   if (has_children)
8552     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8553   else
8554     {
8555       die->child = NULL;
8556       *new_info_ptr = cur_ptr;
8557     }
8558
8559   die->sibling = NULL;
8560   die->parent = parent;
8561   return die;
8562 }
8563
8564 /* Read a die, all of its descendents, and all of its siblings; set
8565    all of the fields of all of the dies correctly.  Arguments are as
8566    in read_die_and_children.  */
8567
8568 static struct die_info *
8569 read_die_and_siblings (const struct die_reader_specs *reader,
8570                        gdb_byte *info_ptr,
8571                        gdb_byte **new_info_ptr,
8572                        struct die_info *parent)
8573 {
8574   struct die_info *first_die, *last_sibling;
8575   gdb_byte *cur_ptr;
8576
8577   cur_ptr = info_ptr;
8578   first_die = last_sibling = NULL;
8579
8580   while (1)
8581     {
8582       struct die_info *die
8583         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8584
8585       if (die == NULL)
8586         {
8587           *new_info_ptr = cur_ptr;
8588           return first_die;
8589         }
8590
8591       if (!first_die)
8592         first_die = die;
8593       else
8594         last_sibling->sibling = die;
8595
8596       last_sibling = die;
8597     }
8598 }
8599
8600 /* Read the die from the .debug_info section buffer.  Set DIEP to
8601    point to a newly allocated die with its information, except for its
8602    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8603    whether the die has children or not.  */
8604
8605 static gdb_byte *
8606 read_full_die (const struct die_reader_specs *reader,
8607                struct die_info **diep, gdb_byte *info_ptr,
8608                int *has_children)
8609 {
8610   unsigned int abbrev_number, bytes_read, i, offset;
8611   struct abbrev_info *abbrev;
8612   struct die_info *die;
8613   struct dwarf2_cu *cu = reader->cu;
8614   bfd *abfd = reader->abfd;
8615
8616   offset = info_ptr - reader->buffer;
8617   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8618   info_ptr += bytes_read;
8619   if (!abbrev_number)
8620     {
8621       *diep = NULL;
8622       *has_children = 0;
8623       return info_ptr;
8624     }
8625
8626   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8627   if (!abbrev)
8628     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8629            abbrev_number,
8630            bfd_get_filename (abfd));
8631
8632   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8633   die->offset = offset;
8634   die->tag = abbrev->tag;
8635   die->abbrev = abbrev_number;
8636
8637   die->num_attrs = abbrev->num_attrs;
8638
8639   for (i = 0; i < abbrev->num_attrs; ++i)
8640     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8641                                abfd, info_ptr, cu);
8642
8643   *diep = die;
8644   *has_children = abbrev->has_children;
8645   return info_ptr;
8646 }
8647
8648 /* In DWARF version 2, the description of the debugging information is
8649    stored in a separate .debug_abbrev section.  Before we read any
8650    dies from a section we read in all abbreviations and install them
8651    in a hash table.  This function also sets flags in CU describing
8652    the data found in the abbrev table.  */
8653
8654 static void
8655 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8656 {
8657   struct comp_unit_head *cu_header = &cu->header;
8658   gdb_byte *abbrev_ptr;
8659   struct abbrev_info *cur_abbrev;
8660   unsigned int abbrev_number, bytes_read, abbrev_name;
8661   unsigned int abbrev_form, hash_number;
8662   struct attr_abbrev *cur_attrs;
8663   unsigned int allocated_attrs;
8664
8665   /* Initialize dwarf2 abbrevs.  */
8666   obstack_init (&cu->abbrev_obstack);
8667   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8668                                       (ABBREV_HASH_SIZE
8669                                        * sizeof (struct abbrev_info *)));
8670   memset (cu->dwarf2_abbrevs, 0,
8671           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8672
8673   dwarf2_read_section (dwarf2_per_objfile->objfile,
8674                        &dwarf2_per_objfile->abbrev);
8675   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8676   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8677   abbrev_ptr += bytes_read;
8678
8679   allocated_attrs = ATTR_ALLOC_CHUNK;
8680   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8681
8682   /* Loop until we reach an abbrev number of 0.  */
8683   while (abbrev_number)
8684     {
8685       cur_abbrev = dwarf_alloc_abbrev (cu);
8686
8687       /* read in abbrev header */
8688       cur_abbrev->number = abbrev_number;
8689       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8690       abbrev_ptr += bytes_read;
8691       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8692       abbrev_ptr += 1;
8693
8694       if (cur_abbrev->tag == DW_TAG_namespace)
8695         cu->has_namespace_info = 1;
8696
8697       /* now read in declarations */
8698       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8699       abbrev_ptr += bytes_read;
8700       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8701       abbrev_ptr += bytes_read;
8702       while (abbrev_name)
8703         {
8704           if (cur_abbrev->num_attrs == allocated_attrs)
8705             {
8706               allocated_attrs += ATTR_ALLOC_CHUNK;
8707               cur_attrs
8708                 = xrealloc (cur_attrs, (allocated_attrs
8709                                         * sizeof (struct attr_abbrev)));
8710             }
8711
8712           /* Record whether this compilation unit might have
8713              inter-compilation-unit references.  If we don't know what form
8714              this attribute will have, then it might potentially be a
8715              DW_FORM_ref_addr, so we conservatively expect inter-CU
8716              references.  */
8717
8718           if (abbrev_form == DW_FORM_ref_addr
8719               || abbrev_form == DW_FORM_indirect)
8720             cu->has_form_ref_addr = 1;
8721
8722           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8723           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8724           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8725           abbrev_ptr += bytes_read;
8726           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8727           abbrev_ptr += bytes_read;
8728         }
8729
8730       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8731                                          (cur_abbrev->num_attrs
8732                                           * sizeof (struct attr_abbrev)));
8733       memcpy (cur_abbrev->attrs, cur_attrs,
8734               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8735
8736       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8737       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8738       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8739
8740       /* Get next abbreviation.
8741          Under Irix6 the abbreviations for a compilation unit are not
8742          always properly terminated with an abbrev number of 0.
8743          Exit loop if we encounter an abbreviation which we have
8744          already read (which means we are about to read the abbreviations
8745          for the next compile unit) or if the end of the abbreviation
8746          table is reached.  */
8747       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8748           >= dwarf2_per_objfile->abbrev.size)
8749         break;
8750       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8751       abbrev_ptr += bytes_read;
8752       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8753         break;
8754     }
8755
8756   xfree (cur_attrs);
8757 }
8758
8759 /* Release the memory used by the abbrev table for a compilation unit.  */
8760
8761 static void
8762 dwarf2_free_abbrev_table (void *ptr_to_cu)
8763 {
8764   struct dwarf2_cu *cu = ptr_to_cu;
8765
8766   obstack_free (&cu->abbrev_obstack, NULL);
8767   cu->dwarf2_abbrevs = NULL;
8768 }
8769
8770 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8771
8772 static struct abbrev_info *
8773 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8774 {
8775   unsigned int hash_number;
8776   struct abbrev_info *abbrev;
8777
8778   hash_number = number % ABBREV_HASH_SIZE;
8779   abbrev = cu->dwarf2_abbrevs[hash_number];
8780
8781   while (abbrev)
8782     {
8783       if (abbrev->number == number)
8784         return abbrev;
8785       else
8786         abbrev = abbrev->next;
8787     }
8788   return NULL;
8789 }
8790
8791 /* Returns nonzero if TAG represents a type that we might generate a partial
8792    symbol for.  */
8793
8794 static int
8795 is_type_tag_for_partial (int tag)
8796 {
8797   switch (tag)
8798     {
8799 #if 0
8800     /* Some types that would be reasonable to generate partial symbols for,
8801        that we don't at present.  */
8802     case DW_TAG_array_type:
8803     case DW_TAG_file_type:
8804     case DW_TAG_ptr_to_member_type:
8805     case DW_TAG_set_type:
8806     case DW_TAG_string_type:
8807     case DW_TAG_subroutine_type:
8808 #endif
8809     case DW_TAG_base_type:
8810     case DW_TAG_class_type:
8811     case DW_TAG_interface_type:
8812     case DW_TAG_enumeration_type:
8813     case DW_TAG_structure_type:
8814     case DW_TAG_subrange_type:
8815     case DW_TAG_typedef:
8816     case DW_TAG_union_type:
8817       return 1;
8818     default:
8819       return 0;
8820     }
8821 }
8822
8823 /* Load all DIEs that are interesting for partial symbols into memory.  */
8824
8825 static struct partial_die_info *
8826 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8827                    int building_psymtab, struct dwarf2_cu *cu)
8828 {
8829   struct partial_die_info *part_die;
8830   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8831   struct abbrev_info *abbrev;
8832   unsigned int bytes_read;
8833   unsigned int load_all = 0;
8834
8835   int nesting_level = 1;
8836
8837   parent_die = NULL;
8838   last_die = NULL;
8839
8840   if (cu->per_cu && cu->per_cu->load_all_dies)
8841     load_all = 1;
8842
8843   cu->partial_dies
8844     = htab_create_alloc_ex (cu->header.length / 12,
8845                             partial_die_hash,
8846                             partial_die_eq,
8847                             NULL,
8848                             &cu->comp_unit_obstack,
8849                             hashtab_obstack_allocate,
8850                             dummy_obstack_deallocate);
8851
8852   part_die = obstack_alloc (&cu->comp_unit_obstack,
8853                             sizeof (struct partial_die_info));
8854
8855   while (1)
8856     {
8857       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8858
8859       /* A NULL abbrev means the end of a series of children.  */
8860       if (abbrev == NULL)
8861         {
8862           if (--nesting_level == 0)
8863             {
8864               /* PART_DIE was probably the last thing allocated on the
8865                  comp_unit_obstack, so we could call obstack_free
8866                  here.  We don't do that because the waste is small,
8867                  and will be cleaned up when we're done with this
8868                  compilation unit.  This way, we're also more robust
8869                  against other users of the comp_unit_obstack.  */
8870               return first_die;
8871             }
8872           info_ptr += bytes_read;
8873           last_die = parent_die;
8874           parent_die = parent_die->die_parent;
8875           continue;
8876         }
8877
8878       /* Check for template arguments.  We never save these; if
8879          they're seen, we just mark the parent, and go on our way.  */
8880       if (parent_die != NULL
8881           && cu->language == language_cplus
8882           && (abbrev->tag == DW_TAG_template_type_param
8883               || abbrev->tag == DW_TAG_template_value_param))
8884         {
8885           parent_die->has_template_arguments = 1;
8886
8887           if (!load_all)
8888             {
8889               /* We don't need a partial DIE for the template argument.  */
8890               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8891                                        cu);
8892               continue;
8893             }
8894         }
8895
8896       /* We only recurse into subprograms looking for template arguments.
8897          Skip their other children.  */
8898       if (!load_all
8899           && cu->language == language_cplus
8900           && parent_die != NULL
8901           && parent_die->tag == DW_TAG_subprogram)
8902         {
8903           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8904           continue;
8905         }
8906
8907       /* Check whether this DIE is interesting enough to save.  Normally
8908          we would not be interested in members here, but there may be
8909          later variables referencing them via DW_AT_specification (for
8910          static members).  */
8911       if (!load_all
8912           && !is_type_tag_for_partial (abbrev->tag)
8913           && abbrev->tag != DW_TAG_constant
8914           && abbrev->tag != DW_TAG_enumerator
8915           && abbrev->tag != DW_TAG_subprogram
8916           && abbrev->tag != DW_TAG_lexical_block
8917           && abbrev->tag != DW_TAG_variable
8918           && abbrev->tag != DW_TAG_namespace
8919           && abbrev->tag != DW_TAG_module
8920           && abbrev->tag != DW_TAG_member)
8921         {
8922           /* Otherwise we skip to the next sibling, if any.  */
8923           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8924           continue;
8925         }
8926
8927       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8928                                    buffer, info_ptr, cu);
8929
8930       /* This two-pass algorithm for processing partial symbols has a
8931          high cost in cache pressure.  Thus, handle some simple cases
8932          here which cover the majority of C partial symbols.  DIEs
8933          which neither have specification tags in them, nor could have
8934          specification tags elsewhere pointing at them, can simply be
8935          processed and discarded.
8936
8937          This segment is also optional; scan_partial_symbols and
8938          add_partial_symbol will handle these DIEs if we chain
8939          them in normally.  When compilers which do not emit large
8940          quantities of duplicate debug information are more common,
8941          this code can probably be removed.  */
8942
8943       /* Any complete simple types at the top level (pretty much all
8944          of them, for a language without namespaces), can be processed
8945          directly.  */
8946       if (parent_die == NULL
8947           && part_die->has_specification == 0
8948           && part_die->is_declaration == 0
8949           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
8950               || part_die->tag == DW_TAG_base_type
8951               || part_die->tag == DW_TAG_subrange_type))
8952         {
8953           if (building_psymtab && part_die->name != NULL)
8954             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8955                                  VAR_DOMAIN, LOC_TYPEDEF,
8956                                  &cu->objfile->static_psymbols,
8957                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8958           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8959           continue;
8960         }
8961
8962       /* The exception for DW_TAG_typedef with has_children above is
8963          a workaround of GCC PR debug/47510.  In the case of this complaint
8964          type_name_no_tag_or_error will error on such types later.
8965
8966          GDB skipped children of DW_TAG_typedef by the shortcut above and then
8967          it could not find the child DIEs referenced later, this is checked
8968          above.  In correct DWARF DW_TAG_typedef should have no children.  */
8969
8970       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
8971         complaint (&symfile_complaints,
8972                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
8973                      "- DIE at 0x%x [in module %s]"),
8974                    part_die->offset, cu->objfile->name);
8975
8976       /* If we're at the second level, and we're an enumerator, and
8977          our parent has no specification (meaning possibly lives in a
8978          namespace elsewhere), then we can add the partial symbol now
8979          instead of queueing it.  */
8980       if (part_die->tag == DW_TAG_enumerator
8981           && parent_die != NULL
8982           && parent_die->die_parent == NULL
8983           && parent_die->tag == DW_TAG_enumeration_type
8984           && parent_die->has_specification == 0)
8985         {
8986           if (part_die->name == NULL)
8987             complaint (&symfile_complaints,
8988                        _("malformed enumerator DIE ignored"));
8989           else if (building_psymtab)
8990             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8991                                  VAR_DOMAIN, LOC_CONST,
8992                                  (cu->language == language_cplus
8993                                   || cu->language == language_java)
8994                                  ? &cu->objfile->global_psymbols
8995                                  : &cu->objfile->static_psymbols,
8996                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8997
8998           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8999           continue;
9000         }
9001
9002       /* We'll save this DIE so link it in.  */
9003       part_die->die_parent = parent_die;
9004       part_die->die_sibling = NULL;
9005       part_die->die_child = NULL;
9006
9007       if (last_die && last_die == parent_die)
9008         last_die->die_child = part_die;
9009       else if (last_die)
9010         last_die->die_sibling = part_die;
9011
9012       last_die = part_die;
9013
9014       if (first_die == NULL)
9015         first_die = part_die;
9016
9017       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9018          find interesting need to be in the hash table, because we
9019          also have the parent/sibling/child chains; only those that we
9020          might refer to by offset later during partial symbol reading.
9021
9022          For now this means things that might have be the target of a
9023          DW_AT_specification, DW_AT_abstract_origin, or
9024          DW_AT_extension.  DW_AT_extension will refer only to
9025          namespaces; DW_AT_abstract_origin refers to functions (and
9026          many things under the function DIE, but we do not recurse
9027          into function DIEs during partial symbol reading) and
9028          possibly variables as well; DW_AT_specification refers to
9029          declarations.  Declarations ought to have the DW_AT_declaration
9030          flag.  It happens that GCC forgets to put it in sometimes, but
9031          only for functions, not for types.
9032
9033          Adding more things than necessary to the hash table is harmless
9034          except for the performance cost.  Adding too few will result in
9035          wasted time in find_partial_die, when we reread the compilation
9036          unit with load_all_dies set.  */
9037
9038       if (load_all
9039           || abbrev->tag == DW_TAG_constant
9040           || abbrev->tag == DW_TAG_subprogram
9041           || abbrev->tag == DW_TAG_variable
9042           || abbrev->tag == DW_TAG_namespace
9043           || part_die->is_declaration)
9044         {
9045           void **slot;
9046
9047           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9048                                            part_die->offset, INSERT);
9049           *slot = part_die;
9050         }
9051
9052       part_die = obstack_alloc (&cu->comp_unit_obstack,
9053                                 sizeof (struct partial_die_info));
9054
9055       /* For some DIEs we want to follow their children (if any).  For C
9056          we have no reason to follow the children of structures; for other
9057          languages we have to, so that we can get at method physnames
9058          to infer fully qualified class names, for DW_AT_specification,
9059          and for C++ template arguments.  For C++, we also look one level
9060          inside functions to find template arguments (if the name of the
9061          function does not already contain the template arguments).
9062
9063          For Ada, we need to scan the children of subprograms and lexical
9064          blocks as well because Ada allows the definition of nested
9065          entities that could be interesting for the debugger, such as
9066          nested subprograms for instance.  */
9067       if (last_die->has_children
9068           && (load_all
9069               || last_die->tag == DW_TAG_namespace
9070               || last_die->tag == DW_TAG_module
9071               || last_die->tag == DW_TAG_enumeration_type
9072               || (cu->language == language_cplus
9073                   && last_die->tag == DW_TAG_subprogram
9074                   && (last_die->name == NULL
9075                       || strchr (last_die->name, '<') == NULL))
9076               || (cu->language != language_c
9077                   && (last_die->tag == DW_TAG_class_type
9078                       || last_die->tag == DW_TAG_interface_type
9079                       || last_die->tag == DW_TAG_structure_type
9080                       || last_die->tag == DW_TAG_union_type))
9081               || (cu->language == language_ada
9082                   && (last_die->tag == DW_TAG_subprogram
9083                       || last_die->tag == DW_TAG_lexical_block))))
9084         {
9085           nesting_level++;
9086           parent_die = last_die;
9087           continue;
9088         }
9089
9090       /* Otherwise we skip to the next sibling, if any.  */
9091       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9092
9093       /* Back to the top, do it again.  */
9094     }
9095 }
9096
9097 /* Read a minimal amount of information into the minimal die structure.  */
9098
9099 static gdb_byte *
9100 read_partial_die (struct partial_die_info *part_die,
9101                   struct abbrev_info *abbrev,
9102                   unsigned int abbrev_len, bfd *abfd,
9103                   gdb_byte *buffer, gdb_byte *info_ptr,
9104                   struct dwarf2_cu *cu)
9105 {
9106   unsigned int i;
9107   struct attribute attr;
9108   int has_low_pc_attr = 0;
9109   int has_high_pc_attr = 0;
9110
9111   memset (part_die, 0, sizeof (struct partial_die_info));
9112
9113   part_die->offset = info_ptr - buffer;
9114
9115   info_ptr += abbrev_len;
9116
9117   if (abbrev == NULL)
9118     return info_ptr;
9119
9120   part_die->tag = abbrev->tag;
9121   part_die->has_children = abbrev->has_children;
9122
9123   for (i = 0; i < abbrev->num_attrs; ++i)
9124     {
9125       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9126
9127       /* Store the data if it is of an attribute we want to keep in a
9128          partial symbol table.  */
9129       switch (attr.name)
9130         {
9131         case DW_AT_name:
9132           switch (part_die->tag)
9133             {
9134             case DW_TAG_compile_unit:
9135             case DW_TAG_type_unit:
9136               /* Compilation units have a DW_AT_name that is a filename, not
9137                  a source language identifier.  */
9138             case DW_TAG_enumeration_type:
9139             case DW_TAG_enumerator:
9140               /* These tags always have simple identifiers already; no need
9141                  to canonicalize them.  */
9142               part_die->name = DW_STRING (&attr);
9143               break;
9144             default:
9145               part_die->name
9146                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9147                                             &cu->objfile->objfile_obstack);
9148               break;
9149             }
9150           break;
9151         case DW_AT_linkage_name:
9152         case DW_AT_MIPS_linkage_name:
9153           /* Note that both forms of linkage name might appear.  We
9154              assume they will be the same, and we only store the last
9155              one we see.  */
9156           if (cu->language == language_ada)
9157             part_die->name = DW_STRING (&attr);
9158           part_die->linkage_name = DW_STRING (&attr);
9159           break;
9160         case DW_AT_low_pc:
9161           has_low_pc_attr = 1;
9162           part_die->lowpc = DW_ADDR (&attr);
9163           break;
9164         case DW_AT_high_pc:
9165           has_high_pc_attr = 1;
9166           part_die->highpc = DW_ADDR (&attr);
9167           break;
9168         case DW_AT_location:
9169           /* Support the .debug_loc offsets.  */
9170           if (attr_form_is_block (&attr))
9171             {
9172                part_die->locdesc = DW_BLOCK (&attr);
9173             }
9174           else if (attr_form_is_section_offset (&attr))
9175             {
9176               dwarf2_complex_location_expr_complaint ();
9177             }
9178           else
9179             {
9180               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9181                                                      "partial symbol information");
9182             }
9183           break;
9184         case DW_AT_external:
9185           part_die->is_external = DW_UNSND (&attr);
9186           break;
9187         case DW_AT_declaration:
9188           part_die->is_declaration = DW_UNSND (&attr);
9189           break;
9190         case DW_AT_type:
9191           part_die->has_type = 1;
9192           break;
9193         case DW_AT_abstract_origin:
9194         case DW_AT_specification:
9195         case DW_AT_extension:
9196           part_die->has_specification = 1;
9197           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9198           break;
9199         case DW_AT_sibling:
9200           /* Ignore absolute siblings, they might point outside of
9201              the current compile unit.  */
9202           if (attr.form == DW_FORM_ref_addr)
9203             complaint (&symfile_complaints,
9204                        _("ignoring absolute DW_AT_sibling"));
9205           else
9206             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9207           break;
9208         case DW_AT_byte_size:
9209           part_die->has_byte_size = 1;
9210           break;
9211         case DW_AT_calling_convention:
9212           /* DWARF doesn't provide a way to identify a program's source-level
9213              entry point.  DW_AT_calling_convention attributes are only meant
9214              to describe functions' calling conventions.
9215
9216              However, because it's a necessary piece of information in
9217              Fortran, and because DW_CC_program is the only piece of debugging
9218              information whose definition refers to a 'main program' at all,
9219              several compilers have begun marking Fortran main programs with
9220              DW_CC_program --- even when those functions use the standard
9221              calling conventions.
9222
9223              So until DWARF specifies a way to provide this information and
9224              compilers pick up the new representation, we'll support this
9225              practice.  */
9226           if (DW_UNSND (&attr) == DW_CC_program
9227               && cu->language == language_fortran)
9228             {
9229               set_main_name (part_die->name);
9230
9231               /* As this DIE has a static linkage the name would be difficult
9232                  to look up later.  */
9233               language_of_main = language_fortran;
9234             }
9235           break;
9236         default:
9237           break;
9238         }
9239     }
9240
9241   if (has_low_pc_attr && has_high_pc_attr)
9242     {
9243       /* When using the GNU linker, .gnu.linkonce. sections are used to
9244          eliminate duplicate copies of functions and vtables and such.
9245          The linker will arbitrarily choose one and discard the others.
9246          The AT_*_pc values for such functions refer to local labels in
9247          these sections.  If the section from that file was discarded, the
9248          labels are not in the output, so the relocs get a value of 0.
9249          If this is a discarded function, mark the pc bounds as invalid,
9250          so that GDB will ignore it.  */
9251       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9252         {
9253           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9254
9255           complaint (&symfile_complaints,
9256                      _("DW_AT_low_pc %s is zero "
9257                        "for DIE at 0x%x [in module %s]"),
9258                      paddress (gdbarch, part_die->lowpc),
9259                      part_die->offset, cu->objfile->name);
9260         }
9261       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9262       else if (part_die->lowpc >= part_die->highpc)
9263         {
9264           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9265
9266           complaint (&symfile_complaints,
9267                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9268                        "for DIE at 0x%x [in module %s]"),
9269                      paddress (gdbarch, part_die->lowpc),
9270                      paddress (gdbarch, part_die->highpc),
9271                      part_die->offset, cu->objfile->name);
9272         }
9273       else
9274         part_die->has_pc_info = 1;
9275     }
9276
9277   return info_ptr;
9278 }
9279
9280 /* Find a cached partial DIE at OFFSET in CU.  */
9281
9282 static struct partial_die_info *
9283 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9284 {
9285   struct partial_die_info *lookup_die = NULL;
9286   struct partial_die_info part_die;
9287
9288   part_die.offset = offset;
9289   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9290
9291   return lookup_die;
9292 }
9293
9294 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9295    except in the case of .debug_types DIEs which do not reference
9296    outside their CU (they do however referencing other types via
9297    DW_FORM_ref_sig8).  */
9298
9299 static struct partial_die_info *
9300 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9301 {
9302   struct dwarf2_per_cu_data *per_cu = NULL;
9303   struct partial_die_info *pd = NULL;
9304
9305   if (cu->per_cu->from_debug_types)
9306     {
9307       pd = find_partial_die_in_comp_unit (offset, cu);
9308       if (pd != NULL)
9309         return pd;
9310       goto not_found;
9311     }
9312
9313   if (offset_in_cu_p (&cu->header, offset))
9314     {
9315       pd = find_partial_die_in_comp_unit (offset, cu);
9316       if (pd != NULL)
9317         return pd;
9318     }
9319
9320   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9321
9322   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9323     load_partial_comp_unit (per_cu, cu->objfile);
9324
9325   per_cu->cu->last_used = 0;
9326   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9327
9328   if (pd == NULL && per_cu->load_all_dies == 0)
9329     {
9330       struct cleanup *back_to;
9331       struct partial_die_info comp_unit_die;
9332       struct abbrev_info *abbrev;
9333       unsigned int bytes_read;
9334       char *info_ptr;
9335
9336       per_cu->load_all_dies = 1;
9337
9338       /* Re-read the DIEs.  */
9339       back_to = make_cleanup (null_cleanup, 0);
9340       if (per_cu->cu->dwarf2_abbrevs == NULL)
9341         {
9342           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9343           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9344         }
9345       info_ptr = (dwarf2_per_objfile->info.buffer
9346                   + per_cu->cu->header.offset
9347                   + per_cu->cu->header.first_die_offset);
9348       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9349       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9350                                    per_cu->cu->objfile->obfd,
9351                                    dwarf2_per_objfile->info.buffer, info_ptr,
9352                                    per_cu->cu);
9353       if (comp_unit_die.has_children)
9354         load_partial_dies (per_cu->cu->objfile->obfd,
9355                            dwarf2_per_objfile->info.buffer, info_ptr,
9356                            0, per_cu->cu);
9357       do_cleanups (back_to);
9358
9359       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9360     }
9361
9362  not_found:
9363
9364   if (pd == NULL)
9365     internal_error (__FILE__, __LINE__,
9366                     _("could not find partial DIE 0x%x "
9367                       "in cache [from module %s]\n"),
9368                     offset, bfd_get_filename (cu->objfile->obfd));
9369   return pd;
9370 }
9371
9372 /* See if we can figure out if the class lives in a namespace.  We do
9373    this by looking for a member function; its demangled name will
9374    contain namespace info, if there is any.  */
9375
9376 static void
9377 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9378                                   struct dwarf2_cu *cu)
9379 {
9380   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9381      what template types look like, because the demangler
9382      frequently doesn't give the same name as the debug info.  We
9383      could fix this by only using the demangled name to get the
9384      prefix (but see comment in read_structure_type).  */
9385
9386   struct partial_die_info *real_pdi;
9387   struct partial_die_info *child_pdi;
9388
9389   /* If this DIE (this DIE's specification, if any) has a parent, then
9390      we should not do this.  We'll prepend the parent's fully qualified
9391      name when we create the partial symbol.  */
9392
9393   real_pdi = struct_pdi;
9394   while (real_pdi->has_specification)
9395     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9396
9397   if (real_pdi->die_parent != NULL)
9398     return;
9399
9400   for (child_pdi = struct_pdi->die_child;
9401        child_pdi != NULL;
9402        child_pdi = child_pdi->die_sibling)
9403     {
9404       if (child_pdi->tag == DW_TAG_subprogram
9405           && child_pdi->linkage_name != NULL)
9406         {
9407           char *actual_class_name
9408             = language_class_name_from_physname (cu->language_defn,
9409                                                  child_pdi->linkage_name);
9410           if (actual_class_name != NULL)
9411             {
9412               struct_pdi->name
9413                 = obsavestring (actual_class_name,
9414                                 strlen (actual_class_name),
9415                                 &cu->objfile->objfile_obstack);
9416               xfree (actual_class_name);
9417             }
9418           break;
9419         }
9420     }
9421 }
9422
9423 /* Adjust PART_DIE before generating a symbol for it.  This function
9424    may set the is_external flag or change the DIE's name.  */
9425
9426 static void
9427 fixup_partial_die (struct partial_die_info *part_die,
9428                    struct dwarf2_cu *cu)
9429 {
9430   /* Once we've fixed up a die, there's no point in doing so again.
9431      This also avoids a memory leak if we were to call
9432      guess_partial_die_structure_name multiple times.  */
9433   if (part_die->fixup_called)
9434     return;
9435
9436   /* If we found a reference attribute and the DIE has no name, try
9437      to find a name in the referred to DIE.  */
9438
9439   if (part_die->name == NULL && part_die->has_specification)
9440     {
9441       struct partial_die_info *spec_die;
9442
9443       spec_die = find_partial_die (part_die->spec_offset, cu);
9444
9445       fixup_partial_die (spec_die, cu);
9446
9447       if (spec_die->name)
9448         {
9449           part_die->name = spec_die->name;
9450
9451           /* Copy DW_AT_external attribute if it is set.  */
9452           if (spec_die->is_external)
9453             part_die->is_external = spec_die->is_external;
9454         }
9455     }
9456
9457   /* Set default names for some unnamed DIEs.  */
9458
9459   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9460     part_die->name = "(anonymous namespace)";
9461
9462   /* If there is no parent die to provide a namespace, and there are
9463      children, see if we can determine the namespace from their linkage
9464      name.
9465      NOTE: We need to do this even if cu->has_namespace_info != 0.
9466      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9467   if (cu->language == language_cplus
9468       && dwarf2_per_objfile->types.asection != NULL
9469       && part_die->die_parent == NULL
9470       && part_die->has_children
9471       && (part_die->tag == DW_TAG_class_type
9472           || part_die->tag == DW_TAG_structure_type
9473           || part_die->tag == DW_TAG_union_type))
9474     guess_partial_die_structure_name (part_die, cu);
9475
9476   /* GCC might emit a nameless struct or union that has a linkage
9477      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
9478   if (part_die->name == NULL
9479       && (part_die->tag == DW_TAG_structure_type
9480           || part_die->tag == DW_TAG_union_type
9481           || part_die->tag == DW_TAG_class_type)
9482       && part_die->linkage_name != NULL)
9483     {
9484       char *demangled;
9485
9486       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9487       if (demangled)
9488         {
9489           part_die->name = obsavestring (demangled, strlen (demangled),
9490                                          &cu->objfile->objfile_obstack);
9491           xfree (demangled);
9492         }
9493     }
9494
9495   part_die->fixup_called = 1;
9496 }
9497
9498 /* Read an attribute value described by an attribute form.  */
9499
9500 static gdb_byte *
9501 read_attribute_value (struct attribute *attr, unsigned form,
9502                       bfd *abfd, gdb_byte *info_ptr,
9503                       struct dwarf2_cu *cu)
9504 {
9505   struct comp_unit_head *cu_header = &cu->header;
9506   unsigned int bytes_read;
9507   struct dwarf_block *blk;
9508
9509   attr->form = form;
9510   switch (form)
9511     {
9512     case DW_FORM_ref_addr:
9513       if (cu->header.version == 2)
9514         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9515       else
9516         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9517                                       &cu->header, &bytes_read);
9518       info_ptr += bytes_read;
9519       break;
9520     case DW_FORM_addr:
9521       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9522       info_ptr += bytes_read;
9523       break;
9524     case DW_FORM_block2:
9525       blk = dwarf_alloc_block (cu);
9526       blk->size = read_2_bytes (abfd, info_ptr);
9527       info_ptr += 2;
9528       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9529       info_ptr += blk->size;
9530       DW_BLOCK (attr) = blk;
9531       break;
9532     case DW_FORM_block4:
9533       blk = dwarf_alloc_block (cu);
9534       blk->size = read_4_bytes (abfd, info_ptr);
9535       info_ptr += 4;
9536       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9537       info_ptr += blk->size;
9538       DW_BLOCK (attr) = blk;
9539       break;
9540     case DW_FORM_data2:
9541       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9542       info_ptr += 2;
9543       break;
9544     case DW_FORM_data4:
9545       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9546       info_ptr += 4;
9547       break;
9548     case DW_FORM_data8:
9549       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9550       info_ptr += 8;
9551       break;
9552     case DW_FORM_sec_offset:
9553       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9554       info_ptr += bytes_read;
9555       break;
9556     case DW_FORM_string:
9557       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9558       DW_STRING_IS_CANONICAL (attr) = 0;
9559       info_ptr += bytes_read;
9560       break;
9561     case DW_FORM_strp:
9562       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9563                                                &bytes_read);
9564       DW_STRING_IS_CANONICAL (attr) = 0;
9565       info_ptr += bytes_read;
9566       break;
9567     case DW_FORM_exprloc:
9568     case DW_FORM_block:
9569       blk = dwarf_alloc_block (cu);
9570       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9571       info_ptr += bytes_read;
9572       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9573       info_ptr += blk->size;
9574       DW_BLOCK (attr) = blk;
9575       break;
9576     case DW_FORM_block1:
9577       blk = dwarf_alloc_block (cu);
9578       blk->size = read_1_byte (abfd, info_ptr);
9579       info_ptr += 1;
9580       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9581       info_ptr += blk->size;
9582       DW_BLOCK (attr) = blk;
9583       break;
9584     case DW_FORM_data1:
9585       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9586       info_ptr += 1;
9587       break;
9588     case DW_FORM_flag:
9589       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9590       info_ptr += 1;
9591       break;
9592     case DW_FORM_flag_present:
9593       DW_UNSND (attr) = 1;
9594       break;
9595     case DW_FORM_sdata:
9596       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9597       info_ptr += bytes_read;
9598       break;
9599     case DW_FORM_udata:
9600       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9601       info_ptr += bytes_read;
9602       break;
9603     case DW_FORM_ref1:
9604       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9605       info_ptr += 1;
9606       break;
9607     case DW_FORM_ref2:
9608       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9609       info_ptr += 2;
9610       break;
9611     case DW_FORM_ref4:
9612       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9613       info_ptr += 4;
9614       break;
9615     case DW_FORM_ref8:
9616       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9617       info_ptr += 8;
9618       break;
9619     case DW_FORM_ref_sig8:
9620       /* Convert the signature to something we can record in DW_UNSND
9621          for later lookup.
9622          NOTE: This is NULL if the type wasn't found.  */
9623       DW_SIGNATURED_TYPE (attr) =
9624         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9625       info_ptr += 8;
9626       break;
9627     case DW_FORM_ref_udata:
9628       DW_ADDR (attr) = (cu->header.offset
9629                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9630       info_ptr += bytes_read;
9631       break;
9632     case DW_FORM_indirect:
9633       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9634       info_ptr += bytes_read;
9635       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9636       break;
9637     default:
9638       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9639              dwarf_form_name (form),
9640              bfd_get_filename (abfd));
9641     }
9642
9643   /* We have seen instances where the compiler tried to emit a byte
9644      size attribute of -1 which ended up being encoded as an unsigned
9645      0xffffffff.  Although 0xffffffff is technically a valid size value,
9646      an object of this size seems pretty unlikely so we can relatively
9647      safely treat these cases as if the size attribute was invalid and
9648      treat them as zero by default.  */
9649   if (attr->name == DW_AT_byte_size
9650       && form == DW_FORM_data4
9651       && DW_UNSND (attr) >= 0xffffffff)
9652     {
9653       complaint
9654         (&symfile_complaints,
9655          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9656          hex_string (DW_UNSND (attr)));
9657       DW_UNSND (attr) = 0;
9658     }
9659
9660   return info_ptr;
9661 }
9662
9663 /* Read an attribute described by an abbreviated attribute.  */
9664
9665 static gdb_byte *
9666 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9667                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9668 {
9669   attr->name = abbrev->name;
9670   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9671 }
9672
9673 /* Read dwarf information from a buffer.  */
9674
9675 static unsigned int
9676 read_1_byte (bfd *abfd, gdb_byte *buf)
9677 {
9678   return bfd_get_8 (abfd, buf);
9679 }
9680
9681 static int
9682 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9683 {
9684   return bfd_get_signed_8 (abfd, buf);
9685 }
9686
9687 static unsigned int
9688 read_2_bytes (bfd *abfd, gdb_byte *buf)
9689 {
9690   return bfd_get_16 (abfd, buf);
9691 }
9692
9693 static int
9694 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9695 {
9696   return bfd_get_signed_16 (abfd, buf);
9697 }
9698
9699 static unsigned int
9700 read_4_bytes (bfd *abfd, gdb_byte *buf)
9701 {
9702   return bfd_get_32 (abfd, buf);
9703 }
9704
9705 static int
9706 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9707 {
9708   return bfd_get_signed_32 (abfd, buf);
9709 }
9710
9711 static ULONGEST
9712 read_8_bytes (bfd *abfd, gdb_byte *buf)
9713 {
9714   return bfd_get_64 (abfd, buf);
9715 }
9716
9717 static CORE_ADDR
9718 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9719               unsigned int *bytes_read)
9720 {
9721   struct comp_unit_head *cu_header = &cu->header;
9722   CORE_ADDR retval = 0;
9723
9724   if (cu_header->signed_addr_p)
9725     {
9726       switch (cu_header->addr_size)
9727         {
9728         case 2:
9729           retval = bfd_get_signed_16 (abfd, buf);
9730           break;
9731         case 4:
9732           retval = bfd_get_signed_32 (abfd, buf);
9733           break;
9734         case 8:
9735           retval = bfd_get_signed_64 (abfd, buf);
9736           break;
9737         default:
9738           internal_error (__FILE__, __LINE__,
9739                           _("read_address: bad switch, signed [in module %s]"),
9740                           bfd_get_filename (abfd));
9741         }
9742     }
9743   else
9744     {
9745       switch (cu_header->addr_size)
9746         {
9747         case 2:
9748           retval = bfd_get_16 (abfd, buf);
9749           break;
9750         case 4:
9751           retval = bfd_get_32 (abfd, buf);
9752           break;
9753         case 8:
9754           retval = bfd_get_64 (abfd, buf);
9755           break;
9756         default:
9757           internal_error (__FILE__, __LINE__,
9758                           _("read_address: bad switch, "
9759                             "unsigned [in module %s]"),
9760                           bfd_get_filename (abfd));
9761         }
9762     }
9763
9764   *bytes_read = cu_header->addr_size;
9765   return retval;
9766 }
9767
9768 /* Read the initial length from a section.  The (draft) DWARF 3
9769    specification allows the initial length to take up either 4 bytes
9770    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9771    bytes describe the length and all offsets will be 8 bytes in length
9772    instead of 4.
9773
9774    An older, non-standard 64-bit format is also handled by this
9775    function.  The older format in question stores the initial length
9776    as an 8-byte quantity without an escape value.  Lengths greater
9777    than 2^32 aren't very common which means that the initial 4 bytes
9778    is almost always zero.  Since a length value of zero doesn't make
9779    sense for the 32-bit format, this initial zero can be considered to
9780    be an escape value which indicates the presence of the older 64-bit
9781    format.  As written, the code can't detect (old format) lengths
9782    greater than 4GB.  If it becomes necessary to handle lengths
9783    somewhat larger than 4GB, we could allow other small values (such
9784    as the non-sensical values of 1, 2, and 3) to also be used as
9785    escape values indicating the presence of the old format.
9786
9787    The value returned via bytes_read should be used to increment the
9788    relevant pointer after calling read_initial_length().
9789
9790    [ Note:  read_initial_length() and read_offset() are based on the
9791      document entitled "DWARF Debugging Information Format", revision
9792      3, draft 8, dated November 19, 2001.  This document was obtained
9793      from:
9794
9795         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9796
9797      This document is only a draft and is subject to change.  (So beware.)
9798
9799      Details regarding the older, non-standard 64-bit format were
9800      determined empirically by examining 64-bit ELF files produced by
9801      the SGI toolchain on an IRIX 6.5 machine.
9802
9803      - Kevin, July 16, 2002
9804    ] */
9805
9806 static LONGEST
9807 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9808 {
9809   LONGEST length = bfd_get_32 (abfd, buf);
9810
9811   if (length == 0xffffffff)
9812     {
9813       length = bfd_get_64 (abfd, buf + 4);
9814       *bytes_read = 12;
9815     }
9816   else if (length == 0)
9817     {
9818       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9819       length = bfd_get_64 (abfd, buf);
9820       *bytes_read = 8;
9821     }
9822   else
9823     {
9824       *bytes_read = 4;
9825     }
9826
9827   return length;
9828 }
9829
9830 /* Cover function for read_initial_length.
9831    Returns the length of the object at BUF, and stores the size of the
9832    initial length in *BYTES_READ and stores the size that offsets will be in
9833    *OFFSET_SIZE.
9834    If the initial length size is not equivalent to that specified in
9835    CU_HEADER then issue a complaint.
9836    This is useful when reading non-comp-unit headers.  */
9837
9838 static LONGEST
9839 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9840                                         const struct comp_unit_head *cu_header,
9841                                         unsigned int *bytes_read,
9842                                         unsigned int *offset_size)
9843 {
9844   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9845
9846   gdb_assert (cu_header->initial_length_size == 4
9847               || cu_header->initial_length_size == 8
9848               || cu_header->initial_length_size == 12);
9849
9850   if (cu_header->initial_length_size != *bytes_read)
9851     complaint (&symfile_complaints,
9852                _("intermixed 32-bit and 64-bit DWARF sections"));
9853
9854   *offset_size = (*bytes_read == 4) ? 4 : 8;
9855   return length;
9856 }
9857
9858 /* Read an offset from the data stream.  The size of the offset is
9859    given by cu_header->offset_size.  */
9860
9861 static LONGEST
9862 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9863              unsigned int *bytes_read)
9864 {
9865   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9866
9867   *bytes_read = cu_header->offset_size;
9868   return offset;
9869 }
9870
9871 /* Read an offset from the data stream.  */
9872
9873 static LONGEST
9874 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9875 {
9876   LONGEST retval = 0;
9877
9878   switch (offset_size)
9879     {
9880     case 4:
9881       retval = bfd_get_32 (abfd, buf);
9882       break;
9883     case 8:
9884       retval = bfd_get_64 (abfd, buf);
9885       break;
9886     default:
9887       internal_error (__FILE__, __LINE__,
9888                       _("read_offset_1: bad switch [in module %s]"),
9889                       bfd_get_filename (abfd));
9890     }
9891
9892   return retval;
9893 }
9894
9895 static gdb_byte *
9896 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9897 {
9898   /* If the size of a host char is 8 bits, we can return a pointer
9899      to the buffer, otherwise we have to copy the data to a buffer
9900      allocated on the temporary obstack.  */
9901   gdb_assert (HOST_CHAR_BIT == 8);
9902   return buf;
9903 }
9904
9905 static char *
9906 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9907 {
9908   /* If the size of a host char is 8 bits, we can return a pointer
9909      to the string, otherwise we have to copy the string to a buffer
9910      allocated on the temporary obstack.  */
9911   gdb_assert (HOST_CHAR_BIT == 8);
9912   if (*buf == '\0')
9913     {
9914       *bytes_read_ptr = 1;
9915       return NULL;
9916     }
9917   *bytes_read_ptr = strlen ((char *) buf) + 1;
9918   return (char *) buf;
9919 }
9920
9921 static char *
9922 read_indirect_string (bfd *abfd, gdb_byte *buf,
9923                       const struct comp_unit_head *cu_header,
9924                       unsigned int *bytes_read_ptr)
9925 {
9926   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9927
9928   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9929   if (dwarf2_per_objfile->str.buffer == NULL)
9930     {
9931       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9932                       bfd_get_filename (abfd));
9933       return NULL;
9934     }
9935   if (str_offset >= dwarf2_per_objfile->str.size)
9936     {
9937       error (_("DW_FORM_strp pointing outside of "
9938                ".debug_str section [in module %s]"),
9939              bfd_get_filename (abfd));
9940       return NULL;
9941     }
9942   gdb_assert (HOST_CHAR_BIT == 8);
9943   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9944     return NULL;
9945   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9946 }
9947
9948 static unsigned long
9949 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9950 {
9951   unsigned long result;
9952   unsigned int num_read;
9953   int i, shift;
9954   unsigned char byte;
9955
9956   result = 0;
9957   shift = 0;
9958   num_read = 0;
9959   i = 0;
9960   while (1)
9961     {
9962       byte = bfd_get_8 (abfd, buf);
9963       buf++;
9964       num_read++;
9965       result |= ((unsigned long)(byte & 127) << shift);
9966       if ((byte & 128) == 0)
9967         {
9968           break;
9969         }
9970       shift += 7;
9971     }
9972   *bytes_read_ptr = num_read;
9973   return result;
9974 }
9975
9976 static long
9977 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9978 {
9979   long result;
9980   int i, shift, num_read;
9981   unsigned char byte;
9982
9983   result = 0;
9984   shift = 0;
9985   num_read = 0;
9986   i = 0;
9987   while (1)
9988     {
9989       byte = bfd_get_8 (abfd, buf);
9990       buf++;
9991       num_read++;
9992       result |= ((long)(byte & 127) << shift);
9993       shift += 7;
9994       if ((byte & 128) == 0)
9995         {
9996           break;
9997         }
9998     }
9999   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10000     result |= -(((long)1) << shift);
10001   *bytes_read_ptr = num_read;
10002   return result;
10003 }
10004
10005 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10006
10007 static gdb_byte *
10008 skip_leb128 (bfd *abfd, gdb_byte *buf)
10009 {
10010   int byte;
10011
10012   while (1)
10013     {
10014       byte = bfd_get_8 (abfd, buf);
10015       buf++;
10016       if ((byte & 128) == 0)
10017         return buf;
10018     }
10019 }
10020
10021 static void
10022 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10023 {
10024   switch (lang)
10025     {
10026     case DW_LANG_C89:
10027     case DW_LANG_C99:
10028     case DW_LANG_C:
10029       cu->language = language_c;
10030       break;
10031     case DW_LANG_C_plus_plus:
10032       cu->language = language_cplus;
10033       break;
10034     case DW_LANG_D:
10035       cu->language = language_d;
10036       break;
10037     case DW_LANG_Fortran77:
10038     case DW_LANG_Fortran90:
10039     case DW_LANG_Fortran95:
10040       cu->language = language_fortran;
10041       break;
10042     case DW_LANG_Mips_Assembler:
10043       cu->language = language_asm;
10044       break;
10045     case DW_LANG_Java:
10046       cu->language = language_java;
10047       break;
10048     case DW_LANG_Ada83:
10049     case DW_LANG_Ada95:
10050       cu->language = language_ada;
10051       break;
10052     case DW_LANG_Modula2:
10053       cu->language = language_m2;
10054       break;
10055     case DW_LANG_Pascal83:
10056       cu->language = language_pascal;
10057       break;
10058     case DW_LANG_ObjC:
10059       cu->language = language_objc;
10060       break;
10061     case DW_LANG_Cobol74:
10062     case DW_LANG_Cobol85:
10063     default:
10064       cu->language = language_minimal;
10065       break;
10066     }
10067   cu->language_defn = language_def (cu->language);
10068 }
10069
10070 /* Return the named attribute or NULL if not there.  */
10071
10072 static struct attribute *
10073 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10074 {
10075   unsigned int i;
10076   struct attribute *spec = NULL;
10077
10078   for (i = 0; i < die->num_attrs; ++i)
10079     {
10080       if (die->attrs[i].name == name)
10081         return &die->attrs[i];
10082       if (die->attrs[i].name == DW_AT_specification
10083           || die->attrs[i].name == DW_AT_abstract_origin)
10084         spec = &die->attrs[i];
10085     }
10086
10087   if (spec)
10088     {
10089       die = follow_die_ref (die, spec, &cu);
10090       return dwarf2_attr (die, name, cu);
10091     }
10092
10093   return NULL;
10094 }
10095
10096 /* Return the named attribute or NULL if not there,
10097    but do not follow DW_AT_specification, etc.
10098    This is for use in contexts where we're reading .debug_types dies.
10099    Following DW_AT_specification, DW_AT_abstract_origin will take us
10100    back up the chain, and we want to go down.  */
10101
10102 static struct attribute *
10103 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10104                        struct dwarf2_cu *cu)
10105 {
10106   unsigned int i;
10107
10108   for (i = 0; i < die->num_attrs; ++i)
10109     if (die->attrs[i].name == name)
10110       return &die->attrs[i];
10111
10112   return NULL;
10113 }
10114
10115 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10116    and holds a non-zero value.  This function should only be used for
10117    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10118
10119 static int
10120 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10121 {
10122   struct attribute *attr = dwarf2_attr (die, name, cu);
10123
10124   return (attr && DW_UNSND (attr));
10125 }
10126
10127 static int
10128 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10129 {
10130   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10131      which value is non-zero.  However, we have to be careful with
10132      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10133      (via dwarf2_flag_true_p) follows this attribute.  So we may
10134      end up accidently finding a declaration attribute that belongs
10135      to a different DIE referenced by the specification attribute,
10136      even though the given DIE does not have a declaration attribute.  */
10137   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10138           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10139 }
10140
10141 /* Return the die giving the specification for DIE, if there is
10142    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10143    containing the return value on output.  If there is no
10144    specification, but there is an abstract origin, that is
10145    returned.  */
10146
10147 static struct die_info *
10148 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10149 {
10150   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10151                                              *spec_cu);
10152
10153   if (spec_attr == NULL)
10154     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10155
10156   if (spec_attr == NULL)
10157     return NULL;
10158   else
10159     return follow_die_ref (die, spec_attr, spec_cu);
10160 }
10161
10162 /* Free the line_header structure *LH, and any arrays and strings it
10163    refers to.
10164    NOTE: This is also used as a "cleanup" function.  */
10165
10166 static void
10167 free_line_header (struct line_header *lh)
10168 {
10169   if (lh->standard_opcode_lengths)
10170     xfree (lh->standard_opcode_lengths);
10171
10172   /* Remember that all the lh->file_names[i].name pointers are
10173      pointers into debug_line_buffer, and don't need to be freed.  */
10174   if (lh->file_names)
10175     xfree (lh->file_names);
10176
10177   /* Similarly for the include directory names.  */
10178   if (lh->include_dirs)
10179     xfree (lh->include_dirs);
10180
10181   xfree (lh);
10182 }
10183
10184 /* Add an entry to LH's include directory table.  */
10185
10186 static void
10187 add_include_dir (struct line_header *lh, char *include_dir)
10188 {
10189   /* Grow the array if necessary.  */
10190   if (lh->include_dirs_size == 0)
10191     {
10192       lh->include_dirs_size = 1; /* for testing */
10193       lh->include_dirs = xmalloc (lh->include_dirs_size
10194                                   * sizeof (*lh->include_dirs));
10195     }
10196   else if (lh->num_include_dirs >= lh->include_dirs_size)
10197     {
10198       lh->include_dirs_size *= 2;
10199       lh->include_dirs = xrealloc (lh->include_dirs,
10200                                    (lh->include_dirs_size
10201                                     * sizeof (*lh->include_dirs)));
10202     }
10203
10204   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10205 }
10206
10207 /* Add an entry to LH's file name table.  */
10208
10209 static void
10210 add_file_name (struct line_header *lh,
10211                char *name,
10212                unsigned int dir_index,
10213                unsigned int mod_time,
10214                unsigned int length)
10215 {
10216   struct file_entry *fe;
10217
10218   /* Grow the array if necessary.  */
10219   if (lh->file_names_size == 0)
10220     {
10221       lh->file_names_size = 1; /* for testing */
10222       lh->file_names = xmalloc (lh->file_names_size
10223                                 * sizeof (*lh->file_names));
10224     }
10225   else if (lh->num_file_names >= lh->file_names_size)
10226     {
10227       lh->file_names_size *= 2;
10228       lh->file_names = xrealloc (lh->file_names,
10229                                  (lh->file_names_size
10230                                   * sizeof (*lh->file_names)));
10231     }
10232
10233   fe = &lh->file_names[lh->num_file_names++];
10234   fe->name = name;
10235   fe->dir_index = dir_index;
10236   fe->mod_time = mod_time;
10237   fe->length = length;
10238   fe->included_p = 0;
10239   fe->symtab = NULL;
10240 }
10241
10242 /* Read the statement program header starting at OFFSET in
10243    .debug_line, according to the endianness of ABFD.  Return a pointer
10244    to a struct line_header, allocated using xmalloc.
10245
10246    NOTE: the strings in the include directory and file name tables of
10247    the returned object point into debug_line_buffer, and must not be
10248    freed.  */
10249
10250 static struct line_header *
10251 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10252                           struct dwarf2_cu *cu)
10253 {
10254   struct cleanup *back_to;
10255   struct line_header *lh;
10256   gdb_byte *line_ptr;
10257   unsigned int bytes_read, offset_size;
10258   int i;
10259   char *cur_dir, *cur_file;
10260
10261   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10262   if (dwarf2_per_objfile->line.buffer == NULL)
10263     {
10264       complaint (&symfile_complaints, _("missing .debug_line section"));
10265       return 0;
10266     }
10267
10268   /* Make sure that at least there's room for the total_length field.
10269      That could be 12 bytes long, but we're just going to fudge that.  */
10270   if (offset + 4 >= dwarf2_per_objfile->line.size)
10271     {
10272       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10273       return 0;
10274     }
10275
10276   lh = xmalloc (sizeof (*lh));
10277   memset (lh, 0, sizeof (*lh));
10278   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10279                           (void *) lh);
10280
10281   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10282
10283   /* Read in the header.  */
10284   lh->total_length =
10285     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10286                                             &bytes_read, &offset_size);
10287   line_ptr += bytes_read;
10288   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10289                                      + dwarf2_per_objfile->line.size))
10290     {
10291       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10292       return 0;
10293     }
10294   lh->statement_program_end = line_ptr + lh->total_length;
10295   lh->version = read_2_bytes (abfd, line_ptr);
10296   line_ptr += 2;
10297   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10298   line_ptr += offset_size;
10299   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10300   line_ptr += 1;
10301   if (lh->version >= 4)
10302     {
10303       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10304       line_ptr += 1;
10305     }
10306   else
10307     lh->maximum_ops_per_instruction = 1;
10308
10309   if (lh->maximum_ops_per_instruction == 0)
10310     {
10311       lh->maximum_ops_per_instruction = 1;
10312       complaint (&symfile_complaints,
10313                  _("invalid maximum_ops_per_instruction "
10314                    "in `.debug_line' section"));
10315     }
10316
10317   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10318   line_ptr += 1;
10319   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10320   line_ptr += 1;
10321   lh->line_range = read_1_byte (abfd, line_ptr);
10322   line_ptr += 1;
10323   lh->opcode_base = read_1_byte (abfd, line_ptr);
10324   line_ptr += 1;
10325   lh->standard_opcode_lengths
10326     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10327
10328   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10329   for (i = 1; i < lh->opcode_base; ++i)
10330     {
10331       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10332       line_ptr += 1;
10333     }
10334
10335   /* Read directory table.  */
10336   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10337     {
10338       line_ptr += bytes_read;
10339       add_include_dir (lh, cur_dir);
10340     }
10341   line_ptr += bytes_read;
10342
10343   /* Read file name table.  */
10344   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10345     {
10346       unsigned int dir_index, mod_time, length;
10347
10348       line_ptr += bytes_read;
10349       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10350       line_ptr += bytes_read;
10351       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10352       line_ptr += bytes_read;
10353       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10354       line_ptr += bytes_read;
10355
10356       add_file_name (lh, cur_file, dir_index, mod_time, length);
10357     }
10358   line_ptr += bytes_read;
10359   lh->statement_program_start = line_ptr;
10360
10361   if (line_ptr > (dwarf2_per_objfile->line.buffer
10362                   + dwarf2_per_objfile->line.size))
10363     complaint (&symfile_complaints,
10364                _("line number info header doesn't "
10365                  "fit in `.debug_line' section"));
10366
10367   discard_cleanups (back_to);
10368   return lh;
10369 }
10370
10371 /* This function exists to work around a bug in certain compilers
10372    (particularly GCC 2.95), in which the first line number marker of a
10373    function does not show up until after the prologue, right before
10374    the second line number marker.  This function shifts ADDRESS down
10375    to the beginning of the function if necessary, and is called on
10376    addresses passed to record_line.  */
10377
10378 static CORE_ADDR
10379 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10380 {
10381   struct function_range *fn;
10382
10383   /* Find the function_range containing address.  */
10384   if (!cu->first_fn)
10385     return address;
10386
10387   if (!cu->cached_fn)
10388     cu->cached_fn = cu->first_fn;
10389
10390   fn = cu->cached_fn;
10391   while (fn)
10392     if (fn->lowpc <= address && fn->highpc > address)
10393       goto found;
10394     else
10395       fn = fn->next;
10396
10397   fn = cu->first_fn;
10398   while (fn && fn != cu->cached_fn)
10399     if (fn->lowpc <= address && fn->highpc > address)
10400       goto found;
10401     else
10402       fn = fn->next;
10403
10404   return address;
10405
10406  found:
10407   if (fn->seen_line)
10408     return address;
10409   if (address != fn->lowpc)
10410     complaint (&symfile_complaints,
10411                _("misplaced first line number at 0x%lx for '%s'"),
10412                (unsigned long) address, fn->name);
10413   fn->seen_line = 1;
10414   return fn->lowpc;
10415 }
10416
10417 /* Subroutine of dwarf_decode_lines to simplify it.
10418    Return the file name of the psymtab for included file FILE_INDEX
10419    in line header LH of PST.
10420    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10421    If space for the result is malloc'd, it will be freed by a cleanup.
10422    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10423
10424 static char *
10425 psymtab_include_file_name (const struct line_header *lh, int file_index,
10426                            const struct partial_symtab *pst,
10427                            const char *comp_dir)
10428 {
10429   const struct file_entry fe = lh->file_names [file_index];
10430   char *include_name = fe.name;
10431   char *include_name_to_compare = include_name;
10432   char *dir_name = NULL;
10433   const char *pst_filename;
10434   char *copied_name = NULL;
10435   int file_is_pst;
10436
10437   if (fe.dir_index)
10438     dir_name = lh->include_dirs[fe.dir_index - 1];
10439
10440   if (!IS_ABSOLUTE_PATH (include_name)
10441       && (dir_name != NULL || comp_dir != NULL))
10442     {
10443       /* Avoid creating a duplicate psymtab for PST.
10444          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10445          Before we do the comparison, however, we need to account
10446          for DIR_NAME and COMP_DIR.
10447          First prepend dir_name (if non-NULL).  If we still don't
10448          have an absolute path prepend comp_dir (if non-NULL).
10449          However, the directory we record in the include-file's
10450          psymtab does not contain COMP_DIR (to match the
10451          corresponding symtab(s)).
10452
10453          Example:
10454
10455          bash$ cd /tmp
10456          bash$ gcc -g ./hello.c
10457          include_name = "hello.c"
10458          dir_name = "."
10459          DW_AT_comp_dir = comp_dir = "/tmp"
10460          DW_AT_name = "./hello.c"  */
10461
10462       if (dir_name != NULL)
10463         {
10464           include_name = concat (dir_name, SLASH_STRING,
10465                                  include_name, (char *)NULL);
10466           include_name_to_compare = include_name;
10467           make_cleanup (xfree, include_name);
10468         }
10469       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10470         {
10471           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10472                                             include_name, (char *)NULL);
10473         }
10474     }
10475
10476   pst_filename = pst->filename;
10477   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10478     {
10479       copied_name = concat (pst->dirname, SLASH_STRING,
10480                             pst_filename, (char *)NULL);
10481       pst_filename = copied_name;
10482     }
10483
10484   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10485
10486   if (include_name_to_compare != include_name)
10487     xfree (include_name_to_compare);
10488   if (copied_name != NULL)
10489     xfree (copied_name);
10490
10491   if (file_is_pst)
10492     return NULL;
10493   return include_name;
10494 }
10495
10496 /* Ignore this record_line request.  */
10497
10498 static void
10499 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10500 {
10501   return;
10502 }
10503
10504 /* Decode the Line Number Program (LNP) for the given line_header
10505    structure and CU.  The actual information extracted and the type
10506    of structures created from the LNP depends on the value of PST.
10507
10508    1. If PST is NULL, then this procedure uses the data from the program
10509       to create all necessary symbol tables, and their linetables.
10510
10511    2. If PST is not NULL, this procedure reads the program to determine
10512       the list of files included by the unit represented by PST, and
10513       builds all the associated partial symbol tables.
10514
10515    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10516    It is used for relative paths in the line table.
10517    NOTE: When processing partial symtabs (pst != NULL),
10518    comp_dir == pst->dirname.
10519
10520    NOTE: It is important that psymtabs have the same file name (via strcmp)
10521    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10522    symtab we don't use it in the name of the psymtabs we create.
10523    E.g. expand_line_sal requires this when finding psymtabs to expand.
10524    A good testcase for this is mb-inline.exp.  */
10525
10526 static void
10527 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10528                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10529 {
10530   gdb_byte *line_ptr, *extended_end;
10531   gdb_byte *line_end;
10532   unsigned int bytes_read, extended_len;
10533   unsigned char op_code, extended_op, adj_opcode;
10534   CORE_ADDR baseaddr;
10535   struct objfile *objfile = cu->objfile;
10536   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10537   const int decode_for_pst_p = (pst != NULL);
10538   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10539   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10540     = record_line;
10541
10542   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10543
10544   line_ptr = lh->statement_program_start;
10545   line_end = lh->statement_program_end;
10546
10547   /* Read the statement sequences until there's nothing left.  */
10548   while (line_ptr < line_end)
10549     {
10550       /* state machine registers  */
10551       CORE_ADDR address = 0;
10552       unsigned int file = 1;
10553       unsigned int line = 1;
10554       unsigned int column = 0;
10555       int is_stmt = lh->default_is_stmt;
10556       int basic_block = 0;
10557       int end_sequence = 0;
10558       CORE_ADDR addr;
10559       unsigned char op_index = 0;
10560
10561       if (!decode_for_pst_p && lh->num_file_names >= file)
10562         {
10563           /* Start a subfile for the current file of the state machine.  */
10564           /* lh->include_dirs and lh->file_names are 0-based, but the
10565              directory and file name numbers in the statement program
10566              are 1-based.  */
10567           struct file_entry *fe = &lh->file_names[file - 1];
10568           char *dir = NULL;
10569
10570           if (fe->dir_index)
10571             dir = lh->include_dirs[fe->dir_index - 1];
10572
10573           dwarf2_start_subfile (fe->name, dir, comp_dir);
10574         }
10575
10576       /* Decode the table.  */
10577       while (!end_sequence)
10578         {
10579           op_code = read_1_byte (abfd, line_ptr);
10580           line_ptr += 1;
10581           if (line_ptr > line_end)
10582             {
10583               dwarf2_debug_line_missing_end_sequence_complaint ();
10584               break;
10585             }
10586
10587           if (op_code >= lh->opcode_base)
10588             {
10589               /* Special operand.  */
10590               adj_opcode = op_code - lh->opcode_base;
10591               address += (((op_index + (adj_opcode / lh->line_range))
10592                            / lh->maximum_ops_per_instruction)
10593                           * lh->minimum_instruction_length);
10594               op_index = ((op_index + (adj_opcode / lh->line_range))
10595                           % lh->maximum_ops_per_instruction);
10596               line += lh->line_base + (adj_opcode % lh->line_range);
10597               if (lh->num_file_names < file || file == 0)
10598                 dwarf2_debug_line_missing_file_complaint ();
10599               /* For now we ignore lines not starting on an
10600                  instruction boundary.  */
10601               else if (op_index == 0)
10602                 {
10603                   lh->file_names[file - 1].included_p = 1;
10604                   if (!decode_for_pst_p && is_stmt)
10605                     {
10606                       if (last_subfile != current_subfile)
10607                         {
10608                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10609                           if (last_subfile)
10610                             (*p_record_line) (last_subfile, 0, addr);
10611                           last_subfile = current_subfile;
10612                         }
10613                       /* Append row to matrix using current values.  */
10614                       addr = check_cu_functions (address, cu);
10615                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10616                       (*p_record_line) (current_subfile, line, addr);
10617                     }
10618                 }
10619               basic_block = 0;
10620             }
10621           else switch (op_code)
10622             {
10623             case DW_LNS_extended_op:
10624               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10625                                                    &bytes_read);
10626               line_ptr += bytes_read;
10627               extended_end = line_ptr + extended_len;
10628               extended_op = read_1_byte (abfd, line_ptr);
10629               line_ptr += 1;
10630               switch (extended_op)
10631                 {
10632                 case DW_LNE_end_sequence:
10633                   p_record_line = record_line;
10634                   end_sequence = 1;
10635                   break;
10636                 case DW_LNE_set_address:
10637                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10638
10639                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10640                     {
10641                       /* This line table is for a function which has been
10642                          GCd by the linker.  Ignore it.  PR gdb/12528 */
10643
10644                       long line_offset
10645                         = line_ptr - dwarf2_per_objfile->line.buffer;
10646
10647                       complaint (&symfile_complaints,
10648                                  _(".debug_line address at offset 0x%lx is 0 "
10649                                    "[in module %s]"),
10650                                  line_offset, cu->objfile->name);
10651                       p_record_line = noop_record_line;
10652                     }
10653
10654                   op_index = 0;
10655                   line_ptr += bytes_read;
10656                   address += baseaddr;
10657                   break;
10658                 case DW_LNE_define_file:
10659                   {
10660                     char *cur_file;
10661                     unsigned int dir_index, mod_time, length;
10662
10663                     cur_file = read_direct_string (abfd, line_ptr,
10664                                                    &bytes_read);
10665                     line_ptr += bytes_read;
10666                     dir_index =
10667                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10668                     line_ptr += bytes_read;
10669                     mod_time =
10670                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10671                     line_ptr += bytes_read;
10672                     length =
10673                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10674                     line_ptr += bytes_read;
10675                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10676                   }
10677                   break;
10678                 case DW_LNE_set_discriminator:
10679                   /* The discriminator is not interesting to the debugger;
10680                      just ignore it.  */
10681                   line_ptr = extended_end;
10682                   break;
10683                 default:
10684                   complaint (&symfile_complaints,
10685                              _("mangled .debug_line section"));
10686                   return;
10687                 }
10688               /* Make sure that we parsed the extended op correctly.  If e.g.
10689                  we expected a different address size than the producer used,
10690                  we may have read the wrong number of bytes.  */
10691               if (line_ptr != extended_end)
10692                 {
10693                   complaint (&symfile_complaints,
10694                              _("mangled .debug_line section"));
10695                   return;
10696                 }
10697               break;
10698             case DW_LNS_copy:
10699               if (lh->num_file_names < file || file == 0)
10700                 dwarf2_debug_line_missing_file_complaint ();
10701               else
10702                 {
10703                   lh->file_names[file - 1].included_p = 1;
10704                   if (!decode_for_pst_p && is_stmt)
10705                     {
10706                       if (last_subfile != current_subfile)
10707                         {
10708                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10709                           if (last_subfile)
10710                             (*p_record_line) (last_subfile, 0, addr);
10711                           last_subfile = current_subfile;
10712                         }
10713                       addr = check_cu_functions (address, cu);
10714                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10715                       (*p_record_line) (current_subfile, line, addr);
10716                     }
10717                 }
10718               basic_block = 0;
10719               break;
10720             case DW_LNS_advance_pc:
10721               {
10722                 CORE_ADDR adjust
10723                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10724
10725                 address += (((op_index + adjust)
10726                              / lh->maximum_ops_per_instruction)
10727                             * lh->minimum_instruction_length);
10728                 op_index = ((op_index + adjust)
10729                             % lh->maximum_ops_per_instruction);
10730                 line_ptr += bytes_read;
10731               }
10732               break;
10733             case DW_LNS_advance_line:
10734               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10735               line_ptr += bytes_read;
10736               break;
10737             case DW_LNS_set_file:
10738               {
10739                 /* The arrays lh->include_dirs and lh->file_names are
10740                    0-based, but the directory and file name numbers in
10741                    the statement program are 1-based.  */
10742                 struct file_entry *fe;
10743                 char *dir = NULL;
10744
10745                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10746                 line_ptr += bytes_read;
10747                 if (lh->num_file_names < file || file == 0)
10748                   dwarf2_debug_line_missing_file_complaint ();
10749                 else
10750                   {
10751                     fe = &lh->file_names[file - 1];
10752                     if (fe->dir_index)
10753                       dir = lh->include_dirs[fe->dir_index - 1];
10754                     if (!decode_for_pst_p)
10755                       {
10756                         last_subfile = current_subfile;
10757                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10758                       }
10759                   }
10760               }
10761               break;
10762             case DW_LNS_set_column:
10763               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10764               line_ptr += bytes_read;
10765               break;
10766             case DW_LNS_negate_stmt:
10767               is_stmt = (!is_stmt);
10768               break;
10769             case DW_LNS_set_basic_block:
10770               basic_block = 1;
10771               break;
10772             /* Add to the address register of the state machine the
10773                address increment value corresponding to special opcode
10774                255.  I.e., this value is scaled by the minimum
10775                instruction length since special opcode 255 would have
10776                scaled the increment.  */
10777             case DW_LNS_const_add_pc:
10778               {
10779                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10780
10781                 address += (((op_index + adjust)
10782                              / lh->maximum_ops_per_instruction)
10783                             * lh->minimum_instruction_length);
10784                 op_index = ((op_index + adjust)
10785                             % lh->maximum_ops_per_instruction);
10786               }
10787               break;
10788             case DW_LNS_fixed_advance_pc:
10789               address += read_2_bytes (abfd, line_ptr);
10790               op_index = 0;
10791               line_ptr += 2;
10792               break;
10793             default:
10794               {
10795                 /* Unknown standard opcode, ignore it.  */
10796                 int i;
10797
10798                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10799                   {
10800                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10801                     line_ptr += bytes_read;
10802                   }
10803               }
10804             }
10805         }
10806       if (lh->num_file_names < file || file == 0)
10807         dwarf2_debug_line_missing_file_complaint ();
10808       else
10809         {
10810           lh->file_names[file - 1].included_p = 1;
10811           if (!decode_for_pst_p)
10812             {
10813               addr = gdbarch_addr_bits_remove (gdbarch, address);
10814               (*p_record_line) (current_subfile, 0, addr);
10815             }
10816         }
10817     }
10818
10819   if (decode_for_pst_p)
10820     {
10821       int file_index;
10822
10823       /* Now that we're done scanning the Line Header Program, we can
10824          create the psymtab of each included file.  */
10825       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10826         if (lh->file_names[file_index].included_p == 1)
10827           {
10828             char *include_name =
10829               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10830             if (include_name != NULL)
10831               dwarf2_create_include_psymtab (include_name, pst, objfile);
10832           }
10833     }
10834   else
10835     {
10836       /* Make sure a symtab is created for every file, even files
10837          which contain only variables (i.e. no code with associated
10838          line numbers).  */
10839
10840       int i;
10841       struct file_entry *fe;
10842
10843       for (i = 0; i < lh->num_file_names; i++)
10844         {
10845           char *dir = NULL;
10846
10847           fe = &lh->file_names[i];
10848           if (fe->dir_index)
10849             dir = lh->include_dirs[fe->dir_index - 1];
10850           dwarf2_start_subfile (fe->name, dir, comp_dir);
10851
10852           /* Skip the main file; we don't need it, and it must be
10853              allocated last, so that it will show up before the
10854              non-primary symtabs in the objfile's symtab list.  */
10855           if (current_subfile == first_subfile)
10856             continue;
10857
10858           if (current_subfile->symtab == NULL)
10859             current_subfile->symtab = allocate_symtab (current_subfile->name,
10860                                                        cu->objfile);
10861           fe->symtab = current_subfile->symtab;
10862         }
10863     }
10864 }
10865
10866 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10867    DIRNAME the name of the source directory which contains FILENAME
10868    or NULL if not known.  COMP_DIR is the compilation directory for the
10869    linetable's compilation unit or NULL if not known.
10870    This routine tries to keep line numbers from identical absolute and
10871    relative file names in a common subfile.
10872
10873    Using the `list' example from the GDB testsuite, which resides in
10874    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10875    of /srcdir/list0.c yields the following debugging information for list0.c:
10876
10877    DW_AT_name:          /srcdir/list0.c
10878    DW_AT_comp_dir:              /compdir
10879    files.files[0].name: list0.h
10880    files.files[0].dir:  /srcdir
10881    files.files[1].name: list0.c
10882    files.files[1].dir:  /srcdir
10883
10884    The line number information for list0.c has to end up in a single
10885    subfile, so that `break /srcdir/list0.c:1' works as expected.
10886    start_subfile will ensure that this happens provided that we pass the
10887    concatenation of files.files[1].dir and files.files[1].name as the
10888    subfile's name.  */
10889
10890 static void
10891 dwarf2_start_subfile (char *filename, const char *dirname,
10892                       const char *comp_dir)
10893 {
10894   char *fullname;
10895
10896   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10897      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10898      second argument to start_subfile.  To be consistent, we do the
10899      same here.  In order not to lose the line information directory,
10900      we concatenate it to the filename when it makes sense.
10901      Note that the Dwarf3 standard says (speaking of filenames in line
10902      information): ``The directory index is ignored for file names
10903      that represent full path names''.  Thus ignoring dirname in the
10904      `else' branch below isn't an issue.  */
10905
10906   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10907     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10908   else
10909     fullname = filename;
10910
10911   start_subfile (fullname, comp_dir);
10912
10913   if (fullname != filename)
10914     xfree (fullname);
10915 }
10916
10917 static void
10918 var_decode_location (struct attribute *attr, struct symbol *sym,
10919                      struct dwarf2_cu *cu)
10920 {
10921   struct objfile *objfile = cu->objfile;
10922   struct comp_unit_head *cu_header = &cu->header;
10923
10924   /* NOTE drow/2003-01-30: There used to be a comment and some special
10925      code here to turn a symbol with DW_AT_external and a
10926      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10927      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10928      with some versions of binutils) where shared libraries could have
10929      relocations against symbols in their debug information - the
10930      minimal symbol would have the right address, but the debug info
10931      would not.  It's no longer necessary, because we will explicitly
10932      apply relocations when we read in the debug information now.  */
10933
10934   /* A DW_AT_location attribute with no contents indicates that a
10935      variable has been optimized away.  */
10936   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10937     {
10938       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10939       return;
10940     }
10941
10942   /* Handle one degenerate form of location expression specially, to
10943      preserve GDB's previous behavior when section offsets are
10944      specified.  If this is just a DW_OP_addr then mark this symbol
10945      as LOC_STATIC.  */
10946
10947   if (attr_form_is_block (attr)
10948       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10949       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10950     {
10951       unsigned int dummy;
10952
10953       SYMBOL_VALUE_ADDRESS (sym) =
10954         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10955       SYMBOL_CLASS (sym) = LOC_STATIC;
10956       fixup_symbol_section (sym, objfile);
10957       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10958                                               SYMBOL_SECTION (sym));
10959       return;
10960     }
10961
10962   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10963      expression evaluator, and use LOC_COMPUTED only when necessary
10964      (i.e. when the value of a register or memory location is
10965      referenced, or a thread-local block, etc.).  Then again, it might
10966      not be worthwhile.  I'm assuming that it isn't unless performance
10967      or memory numbers show me otherwise.  */
10968
10969   dwarf2_symbol_mark_computed (attr, sym, cu);
10970   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10971 }
10972
10973 /* Given a pointer to a DWARF information entry, figure out if we need
10974    to make a symbol table entry for it, and if so, create a new entry
10975    and return a pointer to it.
10976    If TYPE is NULL, determine symbol type from the die, otherwise
10977    used the passed type.
10978    If SPACE is not NULL, use it to hold the new symbol.  If it is
10979    NULL, allocate a new symbol on the objfile's obstack.  */
10980
10981 static struct symbol *
10982 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10983                  struct symbol *space)
10984 {
10985   struct objfile *objfile = cu->objfile;
10986   struct symbol *sym = NULL;
10987   char *name;
10988   struct attribute *attr = NULL;
10989   struct attribute *attr2 = NULL;
10990   CORE_ADDR baseaddr;
10991   struct pending **list_to_add = NULL;
10992
10993   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10994
10995   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10996
10997   name = dwarf2_name (die, cu);
10998   if (name)
10999     {
11000       const char *linkagename;
11001       int suppress_add = 0;
11002
11003       if (space)
11004         sym = space;
11005       else
11006         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11007       OBJSTAT (objfile, n_syms++);
11008
11009       /* Cache this symbol's name and the name's demangled form (if any).  */
11010       SYMBOL_SET_LANGUAGE (sym, cu->language);
11011       linkagename = dwarf2_physname (name, die, cu);
11012       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11013
11014       /* Fortran does not have mangling standard and the mangling does differ
11015          between gfortran, iFort etc.  */
11016       if (cu->language == language_fortran
11017           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11018         symbol_set_demangled_name (&(sym->ginfo),
11019                                    (char *) dwarf2_full_name (name, die, cu),
11020                                    NULL);
11021
11022       /* Default assumptions.
11023          Use the passed type or decode it from the die.  */
11024       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11025       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11026       if (type != NULL)
11027         SYMBOL_TYPE (sym) = type;
11028       else
11029         SYMBOL_TYPE (sym) = die_type (die, cu);
11030       attr = dwarf2_attr (die,
11031                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11032                           cu);
11033       if (attr)
11034         {
11035           SYMBOL_LINE (sym) = DW_UNSND (attr);
11036         }
11037
11038       attr = dwarf2_attr (die,
11039                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11040                           cu);
11041       if (attr)
11042         {
11043           int file_index = DW_UNSND (attr);
11044
11045           if (cu->line_header == NULL
11046               || file_index > cu->line_header->num_file_names)
11047             complaint (&symfile_complaints,
11048                        _("file index out of range"));
11049           else if (file_index > 0)
11050             {
11051               struct file_entry *fe;
11052
11053               fe = &cu->line_header->file_names[file_index - 1];
11054               SYMBOL_SYMTAB (sym) = fe->symtab;
11055             }
11056         }
11057
11058       switch (die->tag)
11059         {
11060         case DW_TAG_label:
11061           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11062           if (attr)
11063             {
11064               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11065             }
11066           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11067           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11068           SYMBOL_CLASS (sym) = LOC_LABEL;
11069           add_symbol_to_list (sym, cu->list_in_scope);
11070           break;
11071         case DW_TAG_subprogram:
11072           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11073              finish_block.  */
11074           SYMBOL_CLASS (sym) = LOC_BLOCK;
11075           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11076           if ((attr2 && (DW_UNSND (attr2) != 0))
11077               || cu->language == language_ada)
11078             {
11079               /* Subprograms marked external are stored as a global symbol.
11080                  Ada subprograms, whether marked external or not, are always
11081                  stored as a global symbol, because we want to be able to
11082                  access them globally.  For instance, we want to be able
11083                  to break on a nested subprogram without having to
11084                  specify the context.  */
11085               list_to_add = &global_symbols;
11086             }
11087           else
11088             {
11089               list_to_add = cu->list_in_scope;
11090             }
11091           break;
11092         case DW_TAG_inlined_subroutine:
11093           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11094              finish_block.  */
11095           SYMBOL_CLASS (sym) = LOC_BLOCK;
11096           SYMBOL_INLINED (sym) = 1;
11097           /* Do not add the symbol to any lists.  It will be found via
11098              BLOCK_FUNCTION from the blockvector.  */
11099           break;
11100         case DW_TAG_template_value_param:
11101           suppress_add = 1;
11102           /* Fall through.  */
11103         case DW_TAG_constant:
11104         case DW_TAG_variable:
11105         case DW_TAG_member:
11106           /* Compilation with minimal debug info may result in
11107              variables with missing type entries.  Change the
11108              misleading `void' type to something sensible.  */
11109           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11110             SYMBOL_TYPE (sym)
11111               = objfile_type (objfile)->nodebug_data_symbol;
11112
11113           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11114           /* In the case of DW_TAG_member, we should only be called for
11115              static const members.  */
11116           if (die->tag == DW_TAG_member)
11117             {
11118               /* dwarf2_add_field uses die_is_declaration,
11119                  so we do the same.  */
11120               gdb_assert (die_is_declaration (die, cu));
11121               gdb_assert (attr);
11122             }
11123           if (attr)
11124             {
11125               dwarf2_const_value (attr, sym, cu);
11126               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11127               if (!suppress_add)
11128                 {
11129                   if (attr2 && (DW_UNSND (attr2) != 0))
11130                     list_to_add = &global_symbols;
11131                   else
11132                     list_to_add = cu->list_in_scope;
11133                 }
11134               break;
11135             }
11136           attr = dwarf2_attr (die, DW_AT_location, cu);
11137           if (attr)
11138             {
11139               var_decode_location (attr, sym, cu);
11140               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11141               if (SYMBOL_CLASS (sym) == LOC_STATIC
11142                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11143                   && !dwarf2_per_objfile->has_section_at_zero)
11144                 {
11145                   /* When a static variable is eliminated by the linker,
11146                      the corresponding debug information is not stripped
11147                      out, but the variable address is set to null;
11148                      do not add such variables into symbol table.  */
11149                 }
11150               else if (attr2 && (DW_UNSND (attr2) != 0))
11151                 {
11152                   /* Workaround gfortran PR debug/40040 - it uses
11153                      DW_AT_location for variables in -fPIC libraries which may
11154                      get overriden by other libraries/executable and get
11155                      a different address.  Resolve it by the minimal symbol
11156                      which may come from inferior's executable using copy
11157                      relocation.  Make this workaround only for gfortran as for
11158                      other compilers GDB cannot guess the minimal symbol
11159                      Fortran mangling kind.  */
11160                   if (cu->language == language_fortran && die->parent
11161                       && die->parent->tag == DW_TAG_module
11162                       && cu->producer
11163                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11164                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11165
11166                   /* A variable with DW_AT_external is never static,
11167                      but it may be block-scoped.  */
11168                   list_to_add = (cu->list_in_scope == &file_symbols
11169                                  ? &global_symbols : cu->list_in_scope);
11170                 }
11171               else
11172                 list_to_add = cu->list_in_scope;
11173             }
11174           else
11175             {
11176               /* We do not know the address of this symbol.
11177                  If it is an external symbol and we have type information
11178                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11179                  The address of the variable will then be determined from
11180                  the minimal symbol table whenever the variable is
11181                  referenced.  */
11182               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11183               if (attr2 && (DW_UNSND (attr2) != 0)
11184                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11185                 {
11186                   /* A variable with DW_AT_external is never static, but it
11187                      may be block-scoped.  */
11188                   list_to_add = (cu->list_in_scope == &file_symbols
11189                                  ? &global_symbols : cu->list_in_scope);
11190
11191                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11192                 }
11193               else if (!die_is_declaration (die, cu))
11194                 {
11195                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11196                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11197                   if (!suppress_add)
11198                     list_to_add = cu->list_in_scope;
11199                 }
11200             }
11201           break;
11202         case DW_TAG_formal_parameter:
11203           /* If we are inside a function, mark this as an argument.  If
11204              not, we might be looking at an argument to an inlined function
11205              when we do not have enough information to show inlined frames;
11206              pretend it's a local variable in that case so that the user can
11207              still see it.  */
11208           if (context_stack_depth > 0
11209               && context_stack[context_stack_depth - 1].name != NULL)
11210             SYMBOL_IS_ARGUMENT (sym) = 1;
11211           attr = dwarf2_attr (die, DW_AT_location, cu);
11212           if (attr)
11213             {
11214               var_decode_location (attr, sym, cu);
11215             }
11216           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11217           if (attr)
11218             {
11219               dwarf2_const_value (attr, sym, cu);
11220             }
11221           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11222           if (attr && DW_UNSND (attr))
11223             {
11224               struct type *ref_type;
11225
11226               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11227               SYMBOL_TYPE (sym) = ref_type;
11228             }
11229
11230           list_to_add = cu->list_in_scope;
11231           break;
11232         case DW_TAG_unspecified_parameters:
11233           /* From varargs functions; gdb doesn't seem to have any
11234              interest in this information, so just ignore it for now.
11235              (FIXME?) */
11236           break;
11237         case DW_TAG_template_type_param:
11238           suppress_add = 1;
11239           /* Fall through.  */
11240         case DW_TAG_class_type:
11241         case DW_TAG_interface_type:
11242         case DW_TAG_structure_type:
11243         case DW_TAG_union_type:
11244         case DW_TAG_set_type:
11245         case DW_TAG_enumeration_type:
11246           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11247           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11248
11249           {
11250             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11251                really ever be static objects: otherwise, if you try
11252                to, say, break of a class's method and you're in a file
11253                which doesn't mention that class, it won't work unless
11254                the check for all static symbols in lookup_symbol_aux
11255                saves you.  See the OtherFileClass tests in
11256                gdb.c++/namespace.exp.  */
11257
11258             if (!suppress_add)
11259               {
11260                 list_to_add = (cu->list_in_scope == &file_symbols
11261                                && (cu->language == language_cplus
11262                                    || cu->language == language_java)
11263                                ? &global_symbols : cu->list_in_scope);
11264
11265                 /* The semantics of C++ state that "struct foo {
11266                    ... }" also defines a typedef for "foo".  A Java
11267                    class declaration also defines a typedef for the
11268                    class.  */
11269                 if (cu->language == language_cplus
11270                     || cu->language == language_java
11271                     || cu->language == language_ada)
11272                   {
11273                     /* The symbol's name is already allocated along
11274                        with this objfile, so we don't need to
11275                        duplicate it for the type.  */
11276                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11277                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11278                   }
11279               }
11280           }
11281           break;
11282         case DW_TAG_typedef:
11283           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11284           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11285           list_to_add = cu->list_in_scope;
11286           break;
11287         case DW_TAG_base_type:
11288         case DW_TAG_subrange_type:
11289           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11290           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11291           list_to_add = cu->list_in_scope;
11292           break;
11293         case DW_TAG_enumerator:
11294           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11295           if (attr)
11296             {
11297               dwarf2_const_value (attr, sym, cu);
11298             }
11299           {
11300             /* NOTE: carlton/2003-11-10: See comment above in the
11301                DW_TAG_class_type, etc. block.  */
11302
11303             list_to_add = (cu->list_in_scope == &file_symbols
11304                            && (cu->language == language_cplus
11305                                || cu->language == language_java)
11306                            ? &global_symbols : cu->list_in_scope);
11307           }
11308           break;
11309         case DW_TAG_namespace:
11310           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11311           list_to_add = &global_symbols;
11312           break;
11313         default:
11314           /* Not a tag we recognize.  Hopefully we aren't processing
11315              trash data, but since we must specifically ignore things
11316              we don't recognize, there is nothing else we should do at
11317              this point.  */
11318           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11319                      dwarf_tag_name (die->tag));
11320           break;
11321         }
11322
11323       if (suppress_add)
11324         {
11325           sym->hash_next = objfile->template_symbols;
11326           objfile->template_symbols = sym;
11327           list_to_add = NULL;
11328         }
11329
11330       if (list_to_add != NULL)
11331         add_symbol_to_list (sym, list_to_add);
11332
11333       /* For the benefit of old versions of GCC, check for anonymous
11334          namespaces based on the demangled name.  */
11335       if (!processing_has_namespace_info
11336           && cu->language == language_cplus)
11337         cp_scan_for_anonymous_namespaces (sym);
11338     }
11339   return (sym);
11340 }
11341
11342 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11343
11344 static struct symbol *
11345 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11346 {
11347   return new_symbol_full (die, type, cu, NULL);
11348 }
11349
11350 /* Given an attr with a DW_FORM_dataN value in host byte order,
11351    zero-extend it as appropriate for the symbol's type.  The DWARF
11352    standard (v4) is not entirely clear about the meaning of using
11353    DW_FORM_dataN for a constant with a signed type, where the type is
11354    wider than the data.  The conclusion of a discussion on the DWARF
11355    list was that this is unspecified.  We choose to always zero-extend
11356    because that is the interpretation long in use by GCC.  */
11357
11358 static gdb_byte *
11359 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11360                          const char *name, struct obstack *obstack,
11361                          struct dwarf2_cu *cu, long *value, int bits)
11362 {
11363   struct objfile *objfile = cu->objfile;
11364   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11365                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11366   LONGEST l = DW_UNSND (attr);
11367
11368   if (bits < sizeof (*value) * 8)
11369     {
11370       l &= ((LONGEST) 1 << bits) - 1;
11371       *value = l;
11372     }
11373   else if (bits == sizeof (*value) * 8)
11374     *value = l;
11375   else
11376     {
11377       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11378       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11379       return bytes;
11380     }
11381
11382   return NULL;
11383 }
11384
11385 /* Read a constant value from an attribute.  Either set *VALUE, or if
11386    the value does not fit in *VALUE, set *BYTES - either already
11387    allocated on the objfile obstack, or newly allocated on OBSTACK,
11388    or, set *BATON, if we translated the constant to a location
11389    expression.  */
11390
11391 static void
11392 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11393                          const char *name, struct obstack *obstack,
11394                          struct dwarf2_cu *cu,
11395                          long *value, gdb_byte **bytes,
11396                          struct dwarf2_locexpr_baton **baton)
11397 {
11398   struct objfile *objfile = cu->objfile;
11399   struct comp_unit_head *cu_header = &cu->header;
11400   struct dwarf_block *blk;
11401   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11402                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11403
11404   *value = 0;
11405   *bytes = NULL;
11406   *baton = NULL;
11407
11408   switch (attr->form)
11409     {
11410     case DW_FORM_addr:
11411       {
11412         gdb_byte *data;
11413
11414         if (TYPE_LENGTH (type) != cu_header->addr_size)
11415           dwarf2_const_value_length_mismatch_complaint (name,
11416                                                         cu_header->addr_size,
11417                                                         TYPE_LENGTH (type));
11418         /* Symbols of this form are reasonably rare, so we just
11419            piggyback on the existing location code rather than writing
11420            a new implementation of symbol_computed_ops.  */
11421         *baton = obstack_alloc (&objfile->objfile_obstack,
11422                                 sizeof (struct dwarf2_locexpr_baton));
11423         (*baton)->per_cu = cu->per_cu;
11424         gdb_assert ((*baton)->per_cu);
11425
11426         (*baton)->size = 2 + cu_header->addr_size;
11427         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11428         (*baton)->data = data;
11429
11430         data[0] = DW_OP_addr;
11431         store_unsigned_integer (&data[1], cu_header->addr_size,
11432                                 byte_order, DW_ADDR (attr));
11433         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11434       }
11435       break;
11436     case DW_FORM_string:
11437     case DW_FORM_strp:
11438       /* DW_STRING is already allocated on the objfile obstack, point
11439          directly to it.  */
11440       *bytes = (gdb_byte *) DW_STRING (attr);
11441       break;
11442     case DW_FORM_block1:
11443     case DW_FORM_block2:
11444     case DW_FORM_block4:
11445     case DW_FORM_block:
11446     case DW_FORM_exprloc:
11447       blk = DW_BLOCK (attr);
11448       if (TYPE_LENGTH (type) != blk->size)
11449         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11450                                                       TYPE_LENGTH (type));
11451       *bytes = blk->data;
11452       break;
11453
11454       /* The DW_AT_const_value attributes are supposed to carry the
11455          symbol's value "represented as it would be on the target
11456          architecture."  By the time we get here, it's already been
11457          converted to host endianness, so we just need to sign- or
11458          zero-extend it as appropriate.  */
11459     case DW_FORM_data1:
11460       *bytes = dwarf2_const_value_data (attr, type, name,
11461                                         obstack, cu, value, 8);
11462       break;
11463     case DW_FORM_data2:
11464       *bytes = dwarf2_const_value_data (attr, type, name,
11465                                         obstack, cu, value, 16);
11466       break;
11467     case DW_FORM_data4:
11468       *bytes = dwarf2_const_value_data (attr, type, name,
11469                                         obstack, cu, value, 32);
11470       break;
11471     case DW_FORM_data8:
11472       *bytes = dwarf2_const_value_data (attr, type, name,
11473                                         obstack, cu, value, 64);
11474       break;
11475
11476     case DW_FORM_sdata:
11477       *value = DW_SND (attr);
11478       break;
11479
11480     case DW_FORM_udata:
11481       *value = DW_UNSND (attr);
11482       break;
11483
11484     default:
11485       complaint (&symfile_complaints,
11486                  _("unsupported const value attribute form: '%s'"),
11487                  dwarf_form_name (attr->form));
11488       *value = 0;
11489       break;
11490     }
11491 }
11492
11493
11494 /* Copy constant value from an attribute to a symbol.  */
11495
11496 static void
11497 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11498                     struct dwarf2_cu *cu)
11499 {
11500   struct objfile *objfile = cu->objfile;
11501   struct comp_unit_head *cu_header = &cu->header;
11502   long value;
11503   gdb_byte *bytes;
11504   struct dwarf2_locexpr_baton *baton;
11505
11506   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11507                            SYMBOL_PRINT_NAME (sym),
11508                            &objfile->objfile_obstack, cu,
11509                            &value, &bytes, &baton);
11510
11511   if (baton != NULL)
11512     {
11513       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11514       SYMBOL_LOCATION_BATON (sym) = baton;
11515       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11516     }
11517   else if (bytes != NULL)
11518      {
11519       SYMBOL_VALUE_BYTES (sym) = bytes;
11520       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11521     }
11522   else
11523     {
11524       SYMBOL_VALUE (sym) = value;
11525       SYMBOL_CLASS (sym) = LOC_CONST;
11526     }
11527 }
11528
11529 /* Return the type of the die in question using its DW_AT_type attribute.  */
11530
11531 static struct type *
11532 die_type (struct die_info *die, struct dwarf2_cu *cu)
11533 {
11534   struct attribute *type_attr;
11535
11536   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11537   if (!type_attr)
11538     {
11539       /* A missing DW_AT_type represents a void type.  */
11540       return objfile_type (cu->objfile)->builtin_void;
11541     }
11542
11543   return lookup_die_type (die, type_attr, cu);
11544 }
11545
11546 /* True iff CU's producer generates GNAT Ada auxiliary information
11547    that allows to find parallel types through that information instead
11548    of having to do expensive parallel lookups by type name.  */
11549
11550 static int
11551 need_gnat_info (struct dwarf2_cu *cu)
11552 {
11553   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11554      of GNAT produces this auxiliary information, without any indication
11555      that it is produced.  Part of enhancing the FSF version of GNAT
11556      to produce that information will be to put in place an indicator
11557      that we can use in order to determine whether the descriptive type
11558      info is available or not.  One suggestion that has been made is
11559      to use a new attribute, attached to the CU die.  For now, assume
11560      that the descriptive type info is not available.  */
11561   return 0;
11562 }
11563
11564 /* Return the auxiliary type of the die in question using its
11565    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11566    attribute is not present.  */
11567
11568 static struct type *
11569 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11570 {
11571   struct attribute *type_attr;
11572
11573   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11574   if (!type_attr)
11575     return NULL;
11576
11577   return lookup_die_type (die, type_attr, cu);
11578 }
11579
11580 /* If DIE has a descriptive_type attribute, then set the TYPE's
11581    descriptive type accordingly.  */
11582
11583 static void
11584 set_descriptive_type (struct type *type, struct die_info *die,
11585                       struct dwarf2_cu *cu)
11586 {
11587   struct type *descriptive_type = die_descriptive_type (die, cu);
11588
11589   if (descriptive_type)
11590     {
11591       ALLOCATE_GNAT_AUX_TYPE (type);
11592       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11593     }
11594 }
11595
11596 /* Return the containing type of the die in question using its
11597    DW_AT_containing_type attribute.  */
11598
11599 static struct type *
11600 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11601 {
11602   struct attribute *type_attr;
11603
11604   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11605   if (!type_attr)
11606     error (_("Dwarf Error: Problem turning containing type into gdb type "
11607              "[in module %s]"), cu->objfile->name);
11608
11609   return lookup_die_type (die, type_attr, cu);
11610 }
11611
11612 /* Look up the type of DIE in CU using its type attribute ATTR.
11613    If there is no type substitute an error marker.  */
11614
11615 static struct type *
11616 lookup_die_type (struct die_info *die, struct attribute *attr,
11617                  struct dwarf2_cu *cu)
11618 {
11619   struct type *this_type;
11620
11621   /* First see if we have it cached.  */
11622
11623   if (is_ref_attr (attr))
11624     {
11625       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11626
11627       this_type = get_die_type_at_offset (offset, cu->per_cu);
11628     }
11629   else if (attr->form == DW_FORM_ref_sig8)
11630     {
11631       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11632       struct dwarf2_cu *sig_cu;
11633       unsigned int offset;
11634
11635       /* sig_type will be NULL if the signatured type is missing from
11636          the debug info.  */
11637       if (sig_type == NULL)
11638         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11639                  "at 0x%x [in module %s]"),
11640                die->offset, cu->objfile->name);
11641
11642       gdb_assert (sig_type->per_cu.from_debug_types);
11643       offset = sig_type->offset + sig_type->type_offset;
11644       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11645     }
11646   else
11647     {
11648       dump_die_for_error (die);
11649       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11650              dwarf_attr_name (attr->name), cu->objfile->name);
11651     }
11652
11653   /* If not cached we need to read it in.  */
11654
11655   if (this_type == NULL)
11656     {
11657       struct die_info *type_die;
11658       struct dwarf2_cu *type_cu = cu;
11659
11660       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11661       /* If the type is cached, we should have found it above.  */
11662       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11663       this_type = read_type_die_1 (type_die, type_cu);
11664     }
11665
11666   /* If we still don't have a type use an error marker.  */
11667
11668   if (this_type == NULL)
11669     {
11670       char *message, *saved;
11671
11672       /* read_type_die already issued a complaint.  */
11673       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11674                             cu->objfile->name,
11675                             cu->header.offset,
11676                             die->offset);
11677       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11678                              message, strlen (message));
11679       xfree (message);
11680
11681       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11682     }
11683
11684   return this_type;
11685 }
11686
11687 /* Return the type in DIE, CU.
11688    Returns NULL for invalid types.
11689
11690    This first does a lookup in the appropriate type_hash table,
11691    and only reads the die in if necessary.
11692
11693    NOTE: This can be called when reading in partial or full symbols.  */
11694
11695 static struct type *
11696 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11697 {
11698   struct type *this_type;
11699
11700   this_type = get_die_type (die, cu);
11701   if (this_type)
11702     return this_type;
11703
11704   return read_type_die_1 (die, cu);
11705 }
11706
11707 /* Read the type in DIE, CU.
11708    Returns NULL for invalid types.  */
11709
11710 static struct type *
11711 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11712 {
11713   struct type *this_type = NULL;
11714
11715   switch (die->tag)
11716     {
11717     case DW_TAG_class_type:
11718     case DW_TAG_interface_type:
11719     case DW_TAG_structure_type:
11720     case DW_TAG_union_type:
11721       this_type = read_structure_type (die, cu);
11722       break;
11723     case DW_TAG_enumeration_type:
11724       this_type = read_enumeration_type (die, cu);
11725       break;
11726     case DW_TAG_subprogram:
11727     case DW_TAG_subroutine_type:
11728     case DW_TAG_inlined_subroutine:
11729       this_type = read_subroutine_type (die, cu);
11730       break;
11731     case DW_TAG_array_type:
11732       this_type = read_array_type (die, cu);
11733       break;
11734     case DW_TAG_set_type:
11735       this_type = read_set_type (die, cu);
11736       break;
11737     case DW_TAG_pointer_type:
11738       this_type = read_tag_pointer_type (die, cu);
11739       break;
11740     case DW_TAG_ptr_to_member_type:
11741       this_type = read_tag_ptr_to_member_type (die, cu);
11742       break;
11743     case DW_TAG_reference_type:
11744       this_type = read_tag_reference_type (die, cu);
11745       break;
11746     case DW_TAG_const_type:
11747       this_type = read_tag_const_type (die, cu);
11748       break;
11749     case DW_TAG_volatile_type:
11750       this_type = read_tag_volatile_type (die, cu);
11751       break;
11752     case DW_TAG_string_type:
11753       this_type = read_tag_string_type (die, cu);
11754       break;
11755     case DW_TAG_typedef:
11756       this_type = read_typedef (die, cu);
11757       break;
11758     case DW_TAG_subrange_type:
11759       this_type = read_subrange_type (die, cu);
11760       break;
11761     case DW_TAG_base_type:
11762       this_type = read_base_type (die, cu);
11763       break;
11764     case DW_TAG_unspecified_type:
11765       this_type = read_unspecified_type (die, cu);
11766       break;
11767     case DW_TAG_namespace:
11768       this_type = read_namespace_type (die, cu);
11769       break;
11770     case DW_TAG_module:
11771       this_type = read_module_type (die, cu);
11772       break;
11773     default:
11774       complaint (&symfile_complaints,
11775                  _("unexpected tag in read_type_die: '%s'"),
11776                  dwarf_tag_name (die->tag));
11777       break;
11778     }
11779
11780   return this_type;
11781 }
11782
11783 /* See if we can figure out if the class lives in a namespace.  We do
11784    this by looking for a member function; its demangled name will
11785    contain namespace info, if there is any.
11786    Return the computed name or NULL.
11787    Space for the result is allocated on the objfile's obstack.
11788    This is the full-die version of guess_partial_die_structure_name.
11789    In this case we know DIE has no useful parent.  */
11790
11791 static char *
11792 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11793 {
11794   struct die_info *spec_die;
11795   struct dwarf2_cu *spec_cu;
11796   struct die_info *child;
11797
11798   spec_cu = cu;
11799   spec_die = die_specification (die, &spec_cu);
11800   if (spec_die != NULL)
11801     {
11802       die = spec_die;
11803       cu = spec_cu;
11804     }
11805
11806   for (child = die->child;
11807        child != NULL;
11808        child = child->sibling)
11809     {
11810       if (child->tag == DW_TAG_subprogram)
11811         {
11812           struct attribute *attr;
11813
11814           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11815           if (attr == NULL)
11816             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11817           if (attr != NULL)
11818             {
11819               char *actual_name
11820                 = language_class_name_from_physname (cu->language_defn,
11821                                                      DW_STRING (attr));
11822               char *name = NULL;
11823
11824               if (actual_name != NULL)
11825                 {
11826                   char *die_name = dwarf2_name (die, cu);
11827
11828                   if (die_name != NULL
11829                       && strcmp (die_name, actual_name) != 0)
11830                     {
11831                       /* Strip off the class name from the full name.
11832                          We want the prefix.  */
11833                       int die_name_len = strlen (die_name);
11834                       int actual_name_len = strlen (actual_name);
11835
11836                       /* Test for '::' as a sanity check.  */
11837                       if (actual_name_len > die_name_len + 2
11838                           && actual_name[actual_name_len
11839                                          - die_name_len - 1] == ':')
11840                         name =
11841                           obsavestring (actual_name,
11842                                         actual_name_len - die_name_len - 2,
11843                                         &cu->objfile->objfile_obstack);
11844                     }
11845                 }
11846               xfree (actual_name);
11847               return name;
11848             }
11849         }
11850     }
11851
11852   return NULL;
11853 }
11854
11855 /* Return the name of the namespace/class that DIE is defined within,
11856    or "" if we can't tell.  The caller should not xfree the result.
11857
11858    For example, if we're within the method foo() in the following
11859    code:
11860
11861    namespace N {
11862      class C {
11863        void foo () {
11864        }
11865      };
11866    }
11867
11868    then determine_prefix on foo's die will return "N::C".  */
11869
11870 static char *
11871 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11872 {
11873   struct die_info *parent, *spec_die;
11874   struct dwarf2_cu *spec_cu;
11875   struct type *parent_type;
11876
11877   if (cu->language != language_cplus && cu->language != language_java
11878       && cu->language != language_fortran)
11879     return "";
11880
11881   /* We have to be careful in the presence of DW_AT_specification.
11882      For example, with GCC 3.4, given the code
11883
11884      namespace N {
11885        void foo() {
11886          // Definition of N::foo.
11887        }
11888      }
11889
11890      then we'll have a tree of DIEs like this:
11891
11892      1: DW_TAG_compile_unit
11893        2: DW_TAG_namespace        // N
11894          3: DW_TAG_subprogram     // declaration of N::foo
11895        4: DW_TAG_subprogram       // definition of N::foo
11896             DW_AT_specification   // refers to die #3
11897
11898      Thus, when processing die #4, we have to pretend that we're in
11899      the context of its DW_AT_specification, namely the contex of die
11900      #3.  */
11901   spec_cu = cu;
11902   spec_die = die_specification (die, &spec_cu);
11903   if (spec_die == NULL)
11904     parent = die->parent;
11905   else
11906     {
11907       parent = spec_die->parent;
11908       cu = spec_cu;
11909     }
11910
11911   if (parent == NULL)
11912     return "";
11913   else if (parent->building_fullname)
11914     {
11915       const char *name;
11916       const char *parent_name;
11917
11918       /* It has been seen on RealView 2.2 built binaries,
11919          DW_TAG_template_type_param types actually _defined_ as
11920          children of the parent class:
11921
11922          enum E {};
11923          template class <class Enum> Class{};
11924          Class<enum E> class_e;
11925
11926          1: DW_TAG_class_type (Class)
11927            2: DW_TAG_enumeration_type (E)
11928              3: DW_TAG_enumerator (enum1:0)
11929              3: DW_TAG_enumerator (enum2:1)
11930              ...
11931            2: DW_TAG_template_type_param
11932               DW_AT_type  DW_FORM_ref_udata (E)
11933
11934          Besides being broken debug info, it can put GDB into an
11935          infinite loop.  Consider:
11936
11937          When we're building the full name for Class<E>, we'll start
11938          at Class, and go look over its template type parameters,
11939          finding E.  We'll then try to build the full name of E, and
11940          reach here.  We're now trying to build the full name of E,
11941          and look over the parent DIE for containing scope.  In the
11942          broken case, if we followed the parent DIE of E, we'd again
11943          find Class, and once again go look at its template type
11944          arguments, etc., etc.  Simply don't consider such parent die
11945          as source-level parent of this die (it can't be, the language
11946          doesn't allow it), and break the loop here.  */
11947       name = dwarf2_name (die, cu);
11948       parent_name = dwarf2_name (parent, cu);
11949       complaint (&symfile_complaints,
11950                  _("template param type '%s' defined within parent '%s'"),
11951                  name ? name : "<unknown>",
11952                  parent_name ? parent_name : "<unknown>");
11953       return "";
11954     }
11955   else
11956     switch (parent->tag)
11957       {
11958       case DW_TAG_namespace:
11959         parent_type = read_type_die (parent, cu);
11960         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11961            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11962            Work around this problem here.  */
11963         if (cu->language == language_cplus
11964             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11965           return "";
11966         /* We give a name to even anonymous namespaces.  */
11967         return TYPE_TAG_NAME (parent_type);
11968       case DW_TAG_class_type:
11969       case DW_TAG_interface_type:
11970       case DW_TAG_structure_type:
11971       case DW_TAG_union_type:
11972       case DW_TAG_module:
11973         parent_type = read_type_die (parent, cu);
11974         if (TYPE_TAG_NAME (parent_type) != NULL)
11975           return TYPE_TAG_NAME (parent_type);
11976         else
11977           /* An anonymous structure is only allowed non-static data
11978              members; no typedefs, no member functions, et cetera.
11979              So it does not need a prefix.  */
11980           return "";
11981       case DW_TAG_compile_unit:
11982         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
11983         if (cu->language == language_cplus
11984             && dwarf2_per_objfile->types.asection != NULL
11985             && die->child != NULL
11986             && (die->tag == DW_TAG_class_type
11987                 || die->tag == DW_TAG_structure_type
11988                 || die->tag == DW_TAG_union_type))
11989           {
11990             char *name = guess_full_die_structure_name (die, cu);
11991             if (name != NULL)
11992               return name;
11993           }
11994         return "";
11995       default:
11996         return determine_prefix (parent, cu);
11997       }
11998 }
11999
12000 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12001    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12002    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12003    an obconcat, otherwise allocate storage for the result.  The CU argument is
12004    used to determine the language and hence, the appropriate separator.  */
12005
12006 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12007
12008 static char *
12009 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12010                  int physname, struct dwarf2_cu *cu)
12011 {
12012   const char *lead = "";
12013   const char *sep;
12014
12015   if (suffix == NULL || suffix[0] == '\0'
12016       || prefix == NULL || prefix[0] == '\0')
12017     sep = "";
12018   else if (cu->language == language_java)
12019     sep = ".";
12020   else if (cu->language == language_fortran && physname)
12021     {
12022       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12023          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12024
12025       lead = "__";
12026       sep = "_MOD_";
12027     }
12028   else
12029     sep = "::";
12030
12031   if (prefix == NULL)
12032     prefix = "";
12033   if (suffix == NULL)
12034     suffix = "";
12035
12036   if (obs == NULL)
12037     {
12038       char *retval
12039         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12040
12041       strcpy (retval, lead);
12042       strcat (retval, prefix);
12043       strcat (retval, sep);
12044       strcat (retval, suffix);
12045       return retval;
12046     }
12047   else
12048     {
12049       /* We have an obstack.  */
12050       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12051     }
12052 }
12053
12054 /* Return sibling of die, NULL if no sibling.  */
12055
12056 static struct die_info *
12057 sibling_die (struct die_info *die)
12058 {
12059   return die->sibling;
12060 }
12061
12062 /* Get name of a die, return NULL if not found.  */
12063
12064 static char *
12065 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12066                           struct obstack *obstack)
12067 {
12068   if (name && cu->language == language_cplus)
12069     {
12070       char *canon_name = cp_canonicalize_string (name);
12071
12072       if (canon_name != NULL)
12073         {
12074           if (strcmp (canon_name, name) != 0)
12075             name = obsavestring (canon_name, strlen (canon_name),
12076                                  obstack);
12077           xfree (canon_name);
12078         }
12079     }
12080
12081   return name;
12082 }
12083
12084 /* Get name of a die, return NULL if not found.  */
12085
12086 static char *
12087 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12088 {
12089   struct attribute *attr;
12090
12091   attr = dwarf2_attr (die, DW_AT_name, cu);
12092   if ((!attr || !DW_STRING (attr))
12093       && die->tag != DW_TAG_class_type
12094       && die->tag != DW_TAG_interface_type
12095       && die->tag != DW_TAG_structure_type
12096       && die->tag != DW_TAG_union_type)
12097     return NULL;
12098
12099   switch (die->tag)
12100     {
12101     case DW_TAG_compile_unit:
12102       /* Compilation units have a DW_AT_name that is a filename, not
12103          a source language identifier.  */
12104     case DW_TAG_enumeration_type:
12105     case DW_TAG_enumerator:
12106       /* These tags always have simple identifiers already; no need
12107          to canonicalize them.  */
12108       return DW_STRING (attr);
12109
12110     case DW_TAG_subprogram:
12111       /* Java constructors will all be named "<init>", so return
12112          the class name when we see this special case.  */
12113       if (cu->language == language_java
12114           && DW_STRING (attr) != NULL
12115           && strcmp (DW_STRING (attr), "<init>") == 0)
12116         {
12117           struct dwarf2_cu *spec_cu = cu;
12118           struct die_info *spec_die;
12119
12120           /* GCJ will output '<init>' for Java constructor names.
12121              For this special case, return the name of the parent class.  */
12122
12123           /* GCJ may output suprogram DIEs with AT_specification set.
12124              If so, use the name of the specified DIE.  */
12125           spec_die = die_specification (die, &spec_cu);
12126           if (spec_die != NULL)
12127             return dwarf2_name (spec_die, spec_cu);
12128
12129           do
12130             {
12131               die = die->parent;
12132               if (die->tag == DW_TAG_class_type)
12133                 return dwarf2_name (die, cu);
12134             }
12135           while (die->tag != DW_TAG_compile_unit);
12136         }
12137       break;
12138
12139     case DW_TAG_class_type:
12140     case DW_TAG_interface_type:
12141     case DW_TAG_structure_type:
12142     case DW_TAG_union_type:
12143       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12144          structures or unions.  These were of the form "._%d" in GCC 4.1,
12145          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12146          and GCC 4.4.  We work around this problem by ignoring these.  */
12147       if (attr && DW_STRING (attr)
12148           && (strncmp (DW_STRING (attr), "._", 2) == 0
12149               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12150         return NULL;
12151
12152       /* GCC might emit a nameless typedef that has a linkage name.  See
12153          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12154       if (!attr || DW_STRING (attr) == NULL)
12155         {
12156           char *demangled = NULL;
12157
12158           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12159           if (attr == NULL)
12160             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12161
12162           if (attr == NULL || DW_STRING (attr) == NULL)
12163             return NULL;
12164
12165           /* Avoid demangling DW_STRING (attr) the second time on a second
12166              call for the same DIE.  */
12167           if (!DW_STRING_IS_CANONICAL (attr))
12168             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12169
12170           if (demangled)
12171             {
12172               /* FIXME: we already did this for the partial symbol... */
12173               DW_STRING (attr)
12174                 = obsavestring (demangled, strlen (demangled),
12175                                 &cu->objfile->objfile_obstack);
12176               DW_STRING_IS_CANONICAL (attr) = 1;
12177               xfree (demangled);
12178             }
12179         }
12180       break;
12181
12182     default:
12183       break;
12184     }
12185
12186   if (!DW_STRING_IS_CANONICAL (attr))
12187     {
12188       DW_STRING (attr)
12189         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12190                                     &cu->objfile->objfile_obstack);
12191       DW_STRING_IS_CANONICAL (attr) = 1;
12192     }
12193   return DW_STRING (attr);
12194 }
12195
12196 /* Return the die that this die in an extension of, or NULL if there
12197    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12198    containing the return value on output.  */
12199
12200 static struct die_info *
12201 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12202 {
12203   struct attribute *attr;
12204
12205   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12206   if (attr == NULL)
12207     return NULL;
12208
12209   return follow_die_ref (die, attr, ext_cu);
12210 }
12211
12212 /* Convert a DIE tag into its string name.  */
12213
12214 static char *
12215 dwarf_tag_name (unsigned tag)
12216 {
12217   switch (tag)
12218     {
12219     case DW_TAG_padding:
12220       return "DW_TAG_padding";
12221     case DW_TAG_array_type:
12222       return "DW_TAG_array_type";
12223     case DW_TAG_class_type:
12224       return "DW_TAG_class_type";
12225     case DW_TAG_entry_point:
12226       return "DW_TAG_entry_point";
12227     case DW_TAG_enumeration_type:
12228       return "DW_TAG_enumeration_type";
12229     case DW_TAG_formal_parameter:
12230       return "DW_TAG_formal_parameter";
12231     case DW_TAG_imported_declaration:
12232       return "DW_TAG_imported_declaration";
12233     case DW_TAG_label:
12234       return "DW_TAG_label";
12235     case DW_TAG_lexical_block:
12236       return "DW_TAG_lexical_block";
12237     case DW_TAG_member:
12238       return "DW_TAG_member";
12239     case DW_TAG_pointer_type:
12240       return "DW_TAG_pointer_type";
12241     case DW_TAG_reference_type:
12242       return "DW_TAG_reference_type";
12243     case DW_TAG_compile_unit:
12244       return "DW_TAG_compile_unit";
12245     case DW_TAG_string_type:
12246       return "DW_TAG_string_type";
12247     case DW_TAG_structure_type:
12248       return "DW_TAG_structure_type";
12249     case DW_TAG_subroutine_type:
12250       return "DW_TAG_subroutine_type";
12251     case DW_TAG_typedef:
12252       return "DW_TAG_typedef";
12253     case DW_TAG_union_type:
12254       return "DW_TAG_union_type";
12255     case DW_TAG_unspecified_parameters:
12256       return "DW_TAG_unspecified_parameters";
12257     case DW_TAG_variant:
12258       return "DW_TAG_variant";
12259     case DW_TAG_common_block:
12260       return "DW_TAG_common_block";
12261     case DW_TAG_common_inclusion:
12262       return "DW_TAG_common_inclusion";
12263     case DW_TAG_inheritance:
12264       return "DW_TAG_inheritance";
12265     case DW_TAG_inlined_subroutine:
12266       return "DW_TAG_inlined_subroutine";
12267     case DW_TAG_module:
12268       return "DW_TAG_module";
12269     case DW_TAG_ptr_to_member_type:
12270       return "DW_TAG_ptr_to_member_type";
12271     case DW_TAG_set_type:
12272       return "DW_TAG_set_type";
12273     case DW_TAG_subrange_type:
12274       return "DW_TAG_subrange_type";
12275     case DW_TAG_with_stmt:
12276       return "DW_TAG_with_stmt";
12277     case DW_TAG_access_declaration:
12278       return "DW_TAG_access_declaration";
12279     case DW_TAG_base_type:
12280       return "DW_TAG_base_type";
12281     case DW_TAG_catch_block:
12282       return "DW_TAG_catch_block";
12283     case DW_TAG_const_type:
12284       return "DW_TAG_const_type";
12285     case DW_TAG_constant:
12286       return "DW_TAG_constant";
12287     case DW_TAG_enumerator:
12288       return "DW_TAG_enumerator";
12289     case DW_TAG_file_type:
12290       return "DW_TAG_file_type";
12291     case DW_TAG_friend:
12292       return "DW_TAG_friend";
12293     case DW_TAG_namelist:
12294       return "DW_TAG_namelist";
12295     case DW_TAG_namelist_item:
12296       return "DW_TAG_namelist_item";
12297     case DW_TAG_packed_type:
12298       return "DW_TAG_packed_type";
12299     case DW_TAG_subprogram:
12300       return "DW_TAG_subprogram";
12301     case DW_TAG_template_type_param:
12302       return "DW_TAG_template_type_param";
12303     case DW_TAG_template_value_param:
12304       return "DW_TAG_template_value_param";
12305     case DW_TAG_thrown_type:
12306       return "DW_TAG_thrown_type";
12307     case DW_TAG_try_block:
12308       return "DW_TAG_try_block";
12309     case DW_TAG_variant_part:
12310       return "DW_TAG_variant_part";
12311     case DW_TAG_variable:
12312       return "DW_TAG_variable";
12313     case DW_TAG_volatile_type:
12314       return "DW_TAG_volatile_type";
12315     case DW_TAG_dwarf_procedure:
12316       return "DW_TAG_dwarf_procedure";
12317     case DW_TAG_restrict_type:
12318       return "DW_TAG_restrict_type";
12319     case DW_TAG_interface_type:
12320       return "DW_TAG_interface_type";
12321     case DW_TAG_namespace:
12322       return "DW_TAG_namespace";
12323     case DW_TAG_imported_module:
12324       return "DW_TAG_imported_module";
12325     case DW_TAG_unspecified_type:
12326       return "DW_TAG_unspecified_type";
12327     case DW_TAG_partial_unit:
12328       return "DW_TAG_partial_unit";
12329     case DW_TAG_imported_unit:
12330       return "DW_TAG_imported_unit";
12331     case DW_TAG_condition:
12332       return "DW_TAG_condition";
12333     case DW_TAG_shared_type:
12334       return "DW_TAG_shared_type";
12335     case DW_TAG_type_unit:
12336       return "DW_TAG_type_unit";
12337     case DW_TAG_MIPS_loop:
12338       return "DW_TAG_MIPS_loop";
12339     case DW_TAG_HP_array_descriptor:
12340       return "DW_TAG_HP_array_descriptor";
12341     case DW_TAG_format_label:
12342       return "DW_TAG_format_label";
12343     case DW_TAG_function_template:
12344       return "DW_TAG_function_template";
12345     case DW_TAG_class_template:
12346       return "DW_TAG_class_template";
12347     case DW_TAG_GNU_BINCL:
12348       return "DW_TAG_GNU_BINCL";
12349     case DW_TAG_GNU_EINCL:
12350       return "DW_TAG_GNU_EINCL";
12351     case DW_TAG_upc_shared_type:
12352       return "DW_TAG_upc_shared_type";
12353     case DW_TAG_upc_strict_type:
12354       return "DW_TAG_upc_strict_type";
12355     case DW_TAG_upc_relaxed_type:
12356       return "DW_TAG_upc_relaxed_type";
12357     case DW_TAG_PGI_kanji_type:
12358       return "DW_TAG_PGI_kanji_type";
12359     case DW_TAG_PGI_interface_block:
12360       return "DW_TAG_PGI_interface_block";
12361     default:
12362       return "DW_TAG_<unknown>";
12363     }
12364 }
12365
12366 /* Convert a DWARF attribute code into its string name.  */
12367
12368 static char *
12369 dwarf_attr_name (unsigned attr)
12370 {
12371   switch (attr)
12372     {
12373     case DW_AT_sibling:
12374       return "DW_AT_sibling";
12375     case DW_AT_location:
12376       return "DW_AT_location";
12377     case DW_AT_name:
12378       return "DW_AT_name";
12379     case DW_AT_ordering:
12380       return "DW_AT_ordering";
12381     case DW_AT_subscr_data:
12382       return "DW_AT_subscr_data";
12383     case DW_AT_byte_size:
12384       return "DW_AT_byte_size";
12385     case DW_AT_bit_offset:
12386       return "DW_AT_bit_offset";
12387     case DW_AT_bit_size:
12388       return "DW_AT_bit_size";
12389     case DW_AT_element_list:
12390       return "DW_AT_element_list";
12391     case DW_AT_stmt_list:
12392       return "DW_AT_stmt_list";
12393     case DW_AT_low_pc:
12394       return "DW_AT_low_pc";
12395     case DW_AT_high_pc:
12396       return "DW_AT_high_pc";
12397     case DW_AT_language:
12398       return "DW_AT_language";
12399     case DW_AT_member:
12400       return "DW_AT_member";
12401     case DW_AT_discr:
12402       return "DW_AT_discr";
12403     case DW_AT_discr_value:
12404       return "DW_AT_discr_value";
12405     case DW_AT_visibility:
12406       return "DW_AT_visibility";
12407     case DW_AT_import:
12408       return "DW_AT_import";
12409     case DW_AT_string_length:
12410       return "DW_AT_string_length";
12411     case DW_AT_common_reference:
12412       return "DW_AT_common_reference";
12413     case DW_AT_comp_dir:
12414       return "DW_AT_comp_dir";
12415     case DW_AT_const_value:
12416       return "DW_AT_const_value";
12417     case DW_AT_containing_type:
12418       return "DW_AT_containing_type";
12419     case DW_AT_default_value:
12420       return "DW_AT_default_value";
12421     case DW_AT_inline:
12422       return "DW_AT_inline";
12423     case DW_AT_is_optional:
12424       return "DW_AT_is_optional";
12425     case DW_AT_lower_bound:
12426       return "DW_AT_lower_bound";
12427     case DW_AT_producer:
12428       return "DW_AT_producer";
12429     case DW_AT_prototyped:
12430       return "DW_AT_prototyped";
12431     case DW_AT_return_addr:
12432       return "DW_AT_return_addr";
12433     case DW_AT_start_scope:
12434       return "DW_AT_start_scope";
12435     case DW_AT_bit_stride:
12436       return "DW_AT_bit_stride";
12437     case DW_AT_upper_bound:
12438       return "DW_AT_upper_bound";
12439     case DW_AT_abstract_origin:
12440       return "DW_AT_abstract_origin";
12441     case DW_AT_accessibility:
12442       return "DW_AT_accessibility";
12443     case DW_AT_address_class:
12444       return "DW_AT_address_class";
12445     case DW_AT_artificial:
12446       return "DW_AT_artificial";
12447     case DW_AT_base_types:
12448       return "DW_AT_base_types";
12449     case DW_AT_calling_convention:
12450       return "DW_AT_calling_convention";
12451     case DW_AT_count:
12452       return "DW_AT_count";
12453     case DW_AT_data_member_location:
12454       return "DW_AT_data_member_location";
12455     case DW_AT_decl_column:
12456       return "DW_AT_decl_column";
12457     case DW_AT_decl_file:
12458       return "DW_AT_decl_file";
12459     case DW_AT_decl_line:
12460       return "DW_AT_decl_line";
12461     case DW_AT_declaration:
12462       return "DW_AT_declaration";
12463     case DW_AT_discr_list:
12464       return "DW_AT_discr_list";
12465     case DW_AT_encoding:
12466       return "DW_AT_encoding";
12467     case DW_AT_external:
12468       return "DW_AT_external";
12469     case DW_AT_frame_base:
12470       return "DW_AT_frame_base";
12471     case DW_AT_friend:
12472       return "DW_AT_friend";
12473     case DW_AT_identifier_case:
12474       return "DW_AT_identifier_case";
12475     case DW_AT_macro_info:
12476       return "DW_AT_macro_info";
12477     case DW_AT_namelist_items:
12478       return "DW_AT_namelist_items";
12479     case DW_AT_priority:
12480       return "DW_AT_priority";
12481     case DW_AT_segment:
12482       return "DW_AT_segment";
12483     case DW_AT_specification:
12484       return "DW_AT_specification";
12485     case DW_AT_static_link:
12486       return "DW_AT_static_link";
12487     case DW_AT_type:
12488       return "DW_AT_type";
12489     case DW_AT_use_location:
12490       return "DW_AT_use_location";
12491     case DW_AT_variable_parameter:
12492       return "DW_AT_variable_parameter";
12493     case DW_AT_virtuality:
12494       return "DW_AT_virtuality";
12495     case DW_AT_vtable_elem_location:
12496       return "DW_AT_vtable_elem_location";
12497     /* DWARF 3 values.  */
12498     case DW_AT_allocated:
12499       return "DW_AT_allocated";
12500     case DW_AT_associated:
12501       return "DW_AT_associated";
12502     case DW_AT_data_location:
12503       return "DW_AT_data_location";
12504     case DW_AT_byte_stride:
12505       return "DW_AT_byte_stride";
12506     case DW_AT_entry_pc:
12507       return "DW_AT_entry_pc";
12508     case DW_AT_use_UTF8:
12509       return "DW_AT_use_UTF8";
12510     case DW_AT_extension:
12511       return "DW_AT_extension";
12512     case DW_AT_ranges:
12513       return "DW_AT_ranges";
12514     case DW_AT_trampoline:
12515       return "DW_AT_trampoline";
12516     case DW_AT_call_column:
12517       return "DW_AT_call_column";
12518     case DW_AT_call_file:
12519       return "DW_AT_call_file";
12520     case DW_AT_call_line:
12521       return "DW_AT_call_line";
12522     case DW_AT_description:
12523       return "DW_AT_description";
12524     case DW_AT_binary_scale:
12525       return "DW_AT_binary_scale";
12526     case DW_AT_decimal_scale:
12527       return "DW_AT_decimal_scale";
12528     case DW_AT_small:
12529       return "DW_AT_small";
12530     case DW_AT_decimal_sign:
12531       return "DW_AT_decimal_sign";
12532     case DW_AT_digit_count:
12533       return "DW_AT_digit_count";
12534     case DW_AT_picture_string:
12535       return "DW_AT_picture_string";
12536     case DW_AT_mutable:
12537       return "DW_AT_mutable";
12538     case DW_AT_threads_scaled:
12539       return "DW_AT_threads_scaled";
12540     case DW_AT_explicit:
12541       return "DW_AT_explicit";
12542     case DW_AT_object_pointer:
12543       return "DW_AT_object_pointer";
12544     case DW_AT_endianity:
12545       return "DW_AT_endianity";
12546     case DW_AT_elemental:
12547       return "DW_AT_elemental";
12548     case DW_AT_pure:
12549       return "DW_AT_pure";
12550     case DW_AT_recursive:
12551       return "DW_AT_recursive";
12552     /* DWARF 4 values.  */
12553     case DW_AT_signature:
12554       return "DW_AT_signature";
12555     case DW_AT_linkage_name:
12556       return "DW_AT_linkage_name";
12557     /* SGI/MIPS extensions.  */
12558 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12559     case DW_AT_MIPS_fde:
12560       return "DW_AT_MIPS_fde";
12561 #endif
12562     case DW_AT_MIPS_loop_begin:
12563       return "DW_AT_MIPS_loop_begin";
12564     case DW_AT_MIPS_tail_loop_begin:
12565       return "DW_AT_MIPS_tail_loop_begin";
12566     case DW_AT_MIPS_epilog_begin:
12567       return "DW_AT_MIPS_epilog_begin";
12568     case DW_AT_MIPS_loop_unroll_factor:
12569       return "DW_AT_MIPS_loop_unroll_factor";
12570     case DW_AT_MIPS_software_pipeline_depth:
12571       return "DW_AT_MIPS_software_pipeline_depth";
12572     case DW_AT_MIPS_linkage_name:
12573       return "DW_AT_MIPS_linkage_name";
12574     case DW_AT_MIPS_stride:
12575       return "DW_AT_MIPS_stride";
12576     case DW_AT_MIPS_abstract_name:
12577       return "DW_AT_MIPS_abstract_name";
12578     case DW_AT_MIPS_clone_origin:
12579       return "DW_AT_MIPS_clone_origin";
12580     case DW_AT_MIPS_has_inlines:
12581       return "DW_AT_MIPS_has_inlines";
12582     /* HP extensions.  */
12583 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12584     case DW_AT_HP_block_index:
12585       return "DW_AT_HP_block_index";
12586 #endif
12587     case DW_AT_HP_unmodifiable:
12588       return "DW_AT_HP_unmodifiable";
12589     case DW_AT_HP_actuals_stmt_list:
12590       return "DW_AT_HP_actuals_stmt_list";
12591     case DW_AT_HP_proc_per_section:
12592       return "DW_AT_HP_proc_per_section";
12593     case DW_AT_HP_raw_data_ptr:
12594       return "DW_AT_HP_raw_data_ptr";
12595     case DW_AT_HP_pass_by_reference:
12596       return "DW_AT_HP_pass_by_reference";
12597     case DW_AT_HP_opt_level:
12598       return "DW_AT_HP_opt_level";
12599     case DW_AT_HP_prof_version_id:
12600       return "DW_AT_HP_prof_version_id";
12601     case DW_AT_HP_opt_flags:
12602       return "DW_AT_HP_opt_flags";
12603     case DW_AT_HP_cold_region_low_pc:
12604       return "DW_AT_HP_cold_region_low_pc";
12605     case DW_AT_HP_cold_region_high_pc:
12606       return "DW_AT_HP_cold_region_high_pc";
12607     case DW_AT_HP_all_variables_modifiable:
12608       return "DW_AT_HP_all_variables_modifiable";
12609     case DW_AT_HP_linkage_name:
12610       return "DW_AT_HP_linkage_name";
12611     case DW_AT_HP_prof_flags:
12612       return "DW_AT_HP_prof_flags";
12613     /* GNU extensions.  */
12614     case DW_AT_sf_names:
12615       return "DW_AT_sf_names";
12616     case DW_AT_src_info:
12617       return "DW_AT_src_info";
12618     case DW_AT_mac_info:
12619       return "DW_AT_mac_info";
12620     case DW_AT_src_coords:
12621       return "DW_AT_src_coords";
12622     case DW_AT_body_begin:
12623       return "DW_AT_body_begin";
12624     case DW_AT_body_end:
12625       return "DW_AT_body_end";
12626     case DW_AT_GNU_vector:
12627       return "DW_AT_GNU_vector";
12628     case DW_AT_GNU_odr_signature:
12629       return "DW_AT_GNU_odr_signature";
12630     /* VMS extensions.  */
12631     case DW_AT_VMS_rtnbeg_pd_address:
12632       return "DW_AT_VMS_rtnbeg_pd_address";
12633     /* UPC extension.  */
12634     case DW_AT_upc_threads_scaled:
12635       return "DW_AT_upc_threads_scaled";
12636     /* PGI (STMicroelectronics) extensions.  */
12637     case DW_AT_PGI_lbase:
12638       return "DW_AT_PGI_lbase";
12639     case DW_AT_PGI_soffset:
12640       return "DW_AT_PGI_soffset";
12641     case DW_AT_PGI_lstride:
12642       return "DW_AT_PGI_lstride";
12643     default:
12644       return "DW_AT_<unknown>";
12645     }
12646 }
12647
12648 /* Convert a DWARF value form code into its string name.  */
12649
12650 static char *
12651 dwarf_form_name (unsigned form)
12652 {
12653   switch (form)
12654     {
12655     case DW_FORM_addr:
12656       return "DW_FORM_addr";
12657     case DW_FORM_block2:
12658       return "DW_FORM_block2";
12659     case DW_FORM_block4:
12660       return "DW_FORM_block4";
12661     case DW_FORM_data2:
12662       return "DW_FORM_data2";
12663     case DW_FORM_data4:
12664       return "DW_FORM_data4";
12665     case DW_FORM_data8:
12666       return "DW_FORM_data8";
12667     case DW_FORM_string:
12668       return "DW_FORM_string";
12669     case DW_FORM_block:
12670       return "DW_FORM_block";
12671     case DW_FORM_block1:
12672       return "DW_FORM_block1";
12673     case DW_FORM_data1:
12674       return "DW_FORM_data1";
12675     case DW_FORM_flag:
12676       return "DW_FORM_flag";
12677     case DW_FORM_sdata:
12678       return "DW_FORM_sdata";
12679     case DW_FORM_strp:
12680       return "DW_FORM_strp";
12681     case DW_FORM_udata:
12682       return "DW_FORM_udata";
12683     case DW_FORM_ref_addr:
12684       return "DW_FORM_ref_addr";
12685     case DW_FORM_ref1:
12686       return "DW_FORM_ref1";
12687     case DW_FORM_ref2:
12688       return "DW_FORM_ref2";
12689     case DW_FORM_ref4:
12690       return "DW_FORM_ref4";
12691     case DW_FORM_ref8:
12692       return "DW_FORM_ref8";
12693     case DW_FORM_ref_udata:
12694       return "DW_FORM_ref_udata";
12695     case DW_FORM_indirect:
12696       return "DW_FORM_indirect";
12697     case DW_FORM_sec_offset:
12698       return "DW_FORM_sec_offset";
12699     case DW_FORM_exprloc:
12700       return "DW_FORM_exprloc";
12701     case DW_FORM_flag_present:
12702       return "DW_FORM_flag_present";
12703     case DW_FORM_ref_sig8:
12704       return "DW_FORM_ref_sig8";
12705     default:
12706       return "DW_FORM_<unknown>";
12707     }
12708 }
12709
12710 /* Convert a DWARF stack opcode into its string name.  */
12711
12712 const char *
12713 dwarf_stack_op_name (unsigned op)
12714 {
12715   switch (op)
12716     {
12717     case DW_OP_addr:
12718       return "DW_OP_addr";
12719     case DW_OP_deref:
12720       return "DW_OP_deref";
12721     case DW_OP_const1u:
12722       return "DW_OP_const1u";
12723     case DW_OP_const1s:
12724       return "DW_OP_const1s";
12725     case DW_OP_const2u:
12726       return "DW_OP_const2u";
12727     case DW_OP_const2s:
12728       return "DW_OP_const2s";
12729     case DW_OP_const4u:
12730       return "DW_OP_const4u";
12731     case DW_OP_const4s:
12732       return "DW_OP_const4s";
12733     case DW_OP_const8u:
12734       return "DW_OP_const8u";
12735     case DW_OP_const8s:
12736       return "DW_OP_const8s";
12737     case DW_OP_constu:
12738       return "DW_OP_constu";
12739     case DW_OP_consts:
12740       return "DW_OP_consts";
12741     case DW_OP_dup:
12742       return "DW_OP_dup";
12743     case DW_OP_drop:
12744       return "DW_OP_drop";
12745     case DW_OP_over:
12746       return "DW_OP_over";
12747     case DW_OP_pick:
12748       return "DW_OP_pick";
12749     case DW_OP_swap:
12750       return "DW_OP_swap";
12751     case DW_OP_rot:
12752       return "DW_OP_rot";
12753     case DW_OP_xderef:
12754       return "DW_OP_xderef";
12755     case DW_OP_abs:
12756       return "DW_OP_abs";
12757     case DW_OP_and:
12758       return "DW_OP_and";
12759     case DW_OP_div:
12760       return "DW_OP_div";
12761     case DW_OP_minus:
12762       return "DW_OP_minus";
12763     case DW_OP_mod:
12764       return "DW_OP_mod";
12765     case DW_OP_mul:
12766       return "DW_OP_mul";
12767     case DW_OP_neg:
12768       return "DW_OP_neg";
12769     case DW_OP_not:
12770       return "DW_OP_not";
12771     case DW_OP_or:
12772       return "DW_OP_or";
12773     case DW_OP_plus:
12774       return "DW_OP_plus";
12775     case DW_OP_plus_uconst:
12776       return "DW_OP_plus_uconst";
12777     case DW_OP_shl:
12778       return "DW_OP_shl";
12779     case DW_OP_shr:
12780       return "DW_OP_shr";
12781     case DW_OP_shra:
12782       return "DW_OP_shra";
12783     case DW_OP_xor:
12784       return "DW_OP_xor";
12785     case DW_OP_bra:
12786       return "DW_OP_bra";
12787     case DW_OP_eq:
12788       return "DW_OP_eq";
12789     case DW_OP_ge:
12790       return "DW_OP_ge";
12791     case DW_OP_gt:
12792       return "DW_OP_gt";
12793     case DW_OP_le:
12794       return "DW_OP_le";
12795     case DW_OP_lt:
12796       return "DW_OP_lt";
12797     case DW_OP_ne:
12798       return "DW_OP_ne";
12799     case DW_OP_skip:
12800       return "DW_OP_skip";
12801     case DW_OP_lit0:
12802       return "DW_OP_lit0";
12803     case DW_OP_lit1:
12804       return "DW_OP_lit1";
12805     case DW_OP_lit2:
12806       return "DW_OP_lit2";
12807     case DW_OP_lit3:
12808       return "DW_OP_lit3";
12809     case DW_OP_lit4:
12810       return "DW_OP_lit4";
12811     case DW_OP_lit5:
12812       return "DW_OP_lit5";
12813     case DW_OP_lit6:
12814       return "DW_OP_lit6";
12815     case DW_OP_lit7:
12816       return "DW_OP_lit7";
12817     case DW_OP_lit8:
12818       return "DW_OP_lit8";
12819     case DW_OP_lit9:
12820       return "DW_OP_lit9";
12821     case DW_OP_lit10:
12822       return "DW_OP_lit10";
12823     case DW_OP_lit11:
12824       return "DW_OP_lit11";
12825     case DW_OP_lit12:
12826       return "DW_OP_lit12";
12827     case DW_OP_lit13:
12828       return "DW_OP_lit13";
12829     case DW_OP_lit14:
12830       return "DW_OP_lit14";
12831     case DW_OP_lit15:
12832       return "DW_OP_lit15";
12833     case DW_OP_lit16:
12834       return "DW_OP_lit16";
12835     case DW_OP_lit17:
12836       return "DW_OP_lit17";
12837     case DW_OP_lit18:
12838       return "DW_OP_lit18";
12839     case DW_OP_lit19:
12840       return "DW_OP_lit19";
12841     case DW_OP_lit20:
12842       return "DW_OP_lit20";
12843     case DW_OP_lit21:
12844       return "DW_OP_lit21";
12845     case DW_OP_lit22:
12846       return "DW_OP_lit22";
12847     case DW_OP_lit23:
12848       return "DW_OP_lit23";
12849     case DW_OP_lit24:
12850       return "DW_OP_lit24";
12851     case DW_OP_lit25:
12852       return "DW_OP_lit25";
12853     case DW_OP_lit26:
12854       return "DW_OP_lit26";
12855     case DW_OP_lit27:
12856       return "DW_OP_lit27";
12857     case DW_OP_lit28:
12858       return "DW_OP_lit28";
12859     case DW_OP_lit29:
12860       return "DW_OP_lit29";
12861     case DW_OP_lit30:
12862       return "DW_OP_lit30";
12863     case DW_OP_lit31:
12864       return "DW_OP_lit31";
12865     case DW_OP_reg0:
12866       return "DW_OP_reg0";
12867     case DW_OP_reg1:
12868       return "DW_OP_reg1";
12869     case DW_OP_reg2:
12870       return "DW_OP_reg2";
12871     case DW_OP_reg3:
12872       return "DW_OP_reg3";
12873     case DW_OP_reg4:
12874       return "DW_OP_reg4";
12875     case DW_OP_reg5:
12876       return "DW_OP_reg5";
12877     case DW_OP_reg6:
12878       return "DW_OP_reg6";
12879     case DW_OP_reg7:
12880       return "DW_OP_reg7";
12881     case DW_OP_reg8:
12882       return "DW_OP_reg8";
12883     case DW_OP_reg9:
12884       return "DW_OP_reg9";
12885     case DW_OP_reg10:
12886       return "DW_OP_reg10";
12887     case DW_OP_reg11:
12888       return "DW_OP_reg11";
12889     case DW_OP_reg12:
12890       return "DW_OP_reg12";
12891     case DW_OP_reg13:
12892       return "DW_OP_reg13";
12893     case DW_OP_reg14:
12894       return "DW_OP_reg14";
12895     case DW_OP_reg15:
12896       return "DW_OP_reg15";
12897     case DW_OP_reg16:
12898       return "DW_OP_reg16";
12899     case DW_OP_reg17:
12900       return "DW_OP_reg17";
12901     case DW_OP_reg18:
12902       return "DW_OP_reg18";
12903     case DW_OP_reg19:
12904       return "DW_OP_reg19";
12905     case DW_OP_reg20:
12906       return "DW_OP_reg20";
12907     case DW_OP_reg21:
12908       return "DW_OP_reg21";
12909     case DW_OP_reg22:
12910       return "DW_OP_reg22";
12911     case DW_OP_reg23:
12912       return "DW_OP_reg23";
12913     case DW_OP_reg24:
12914       return "DW_OP_reg24";
12915     case DW_OP_reg25:
12916       return "DW_OP_reg25";
12917     case DW_OP_reg26:
12918       return "DW_OP_reg26";
12919     case DW_OP_reg27:
12920       return "DW_OP_reg27";
12921     case DW_OP_reg28:
12922       return "DW_OP_reg28";
12923     case DW_OP_reg29:
12924       return "DW_OP_reg29";
12925     case DW_OP_reg30:
12926       return "DW_OP_reg30";
12927     case DW_OP_reg31:
12928       return "DW_OP_reg31";
12929     case DW_OP_breg0:
12930       return "DW_OP_breg0";
12931     case DW_OP_breg1:
12932       return "DW_OP_breg1";
12933     case DW_OP_breg2:
12934       return "DW_OP_breg2";
12935     case DW_OP_breg3:
12936       return "DW_OP_breg3";
12937     case DW_OP_breg4:
12938       return "DW_OP_breg4";
12939     case DW_OP_breg5:
12940       return "DW_OP_breg5";
12941     case DW_OP_breg6:
12942       return "DW_OP_breg6";
12943     case DW_OP_breg7:
12944       return "DW_OP_breg7";
12945     case DW_OP_breg8:
12946       return "DW_OP_breg8";
12947     case DW_OP_breg9:
12948       return "DW_OP_breg9";
12949     case DW_OP_breg10:
12950       return "DW_OP_breg10";
12951     case DW_OP_breg11:
12952       return "DW_OP_breg11";
12953     case DW_OP_breg12:
12954       return "DW_OP_breg12";
12955     case DW_OP_breg13:
12956       return "DW_OP_breg13";
12957     case DW_OP_breg14:
12958       return "DW_OP_breg14";
12959     case DW_OP_breg15:
12960       return "DW_OP_breg15";
12961     case DW_OP_breg16:
12962       return "DW_OP_breg16";
12963     case DW_OP_breg17:
12964       return "DW_OP_breg17";
12965     case DW_OP_breg18:
12966       return "DW_OP_breg18";
12967     case DW_OP_breg19:
12968       return "DW_OP_breg19";
12969     case DW_OP_breg20:
12970       return "DW_OP_breg20";
12971     case DW_OP_breg21:
12972       return "DW_OP_breg21";
12973     case DW_OP_breg22:
12974       return "DW_OP_breg22";
12975     case DW_OP_breg23:
12976       return "DW_OP_breg23";
12977     case DW_OP_breg24:
12978       return "DW_OP_breg24";
12979     case DW_OP_breg25:
12980       return "DW_OP_breg25";
12981     case DW_OP_breg26:
12982       return "DW_OP_breg26";
12983     case DW_OP_breg27:
12984       return "DW_OP_breg27";
12985     case DW_OP_breg28:
12986       return "DW_OP_breg28";
12987     case DW_OP_breg29:
12988       return "DW_OP_breg29";
12989     case DW_OP_breg30:
12990       return "DW_OP_breg30";
12991     case DW_OP_breg31:
12992       return "DW_OP_breg31";
12993     case DW_OP_regx:
12994       return "DW_OP_regx";
12995     case DW_OP_fbreg:
12996       return "DW_OP_fbreg";
12997     case DW_OP_bregx:
12998       return "DW_OP_bregx";
12999     case DW_OP_piece:
13000       return "DW_OP_piece";
13001     case DW_OP_deref_size:
13002       return "DW_OP_deref_size";
13003     case DW_OP_xderef_size:
13004       return "DW_OP_xderef_size";
13005     case DW_OP_nop:
13006       return "DW_OP_nop";
13007     /* DWARF 3 extensions.  */
13008     case DW_OP_push_object_address:
13009       return "DW_OP_push_object_address";
13010     case DW_OP_call2:
13011       return "DW_OP_call2";
13012     case DW_OP_call4:
13013       return "DW_OP_call4";
13014     case DW_OP_call_ref:
13015       return "DW_OP_call_ref";
13016     case DW_OP_form_tls_address:
13017       return "DW_OP_form_tls_address";
13018     case DW_OP_call_frame_cfa:
13019       return "DW_OP_call_frame_cfa";
13020     case DW_OP_bit_piece:
13021       return "DW_OP_bit_piece";
13022     /* DWARF 4 extensions.  */
13023     case DW_OP_implicit_value:
13024       return "DW_OP_implicit_value";
13025     case DW_OP_stack_value:
13026       return "DW_OP_stack_value";
13027     /* GNU extensions.  */
13028     case DW_OP_GNU_push_tls_address:
13029       return "DW_OP_GNU_push_tls_address";
13030     case DW_OP_GNU_uninit:
13031       return "DW_OP_GNU_uninit";
13032     case DW_OP_GNU_implicit_pointer:
13033       return "DW_OP_GNU_implicit_pointer";
13034     default:
13035       return NULL;
13036     }
13037 }
13038
13039 static char *
13040 dwarf_bool_name (unsigned mybool)
13041 {
13042   if (mybool)
13043     return "TRUE";
13044   else
13045     return "FALSE";
13046 }
13047
13048 /* Convert a DWARF type code into its string name.  */
13049
13050 static char *
13051 dwarf_type_encoding_name (unsigned enc)
13052 {
13053   switch (enc)
13054     {
13055     case DW_ATE_void:
13056       return "DW_ATE_void";
13057     case DW_ATE_address:
13058       return "DW_ATE_address";
13059     case DW_ATE_boolean:
13060       return "DW_ATE_boolean";
13061     case DW_ATE_complex_float:
13062       return "DW_ATE_complex_float";
13063     case DW_ATE_float:
13064       return "DW_ATE_float";
13065     case DW_ATE_signed:
13066       return "DW_ATE_signed";
13067     case DW_ATE_signed_char:
13068       return "DW_ATE_signed_char";
13069     case DW_ATE_unsigned:
13070       return "DW_ATE_unsigned";
13071     case DW_ATE_unsigned_char:
13072       return "DW_ATE_unsigned_char";
13073     /* DWARF 3.  */
13074     case DW_ATE_imaginary_float:
13075       return "DW_ATE_imaginary_float";
13076     case DW_ATE_packed_decimal:
13077       return "DW_ATE_packed_decimal";
13078     case DW_ATE_numeric_string:
13079       return "DW_ATE_numeric_string";
13080     case DW_ATE_edited:
13081       return "DW_ATE_edited";
13082     case DW_ATE_signed_fixed:
13083       return "DW_ATE_signed_fixed";
13084     case DW_ATE_unsigned_fixed:
13085       return "DW_ATE_unsigned_fixed";
13086     case DW_ATE_decimal_float:
13087       return "DW_ATE_decimal_float";
13088     /* DWARF 4.  */
13089     case DW_ATE_UTF:
13090       return "DW_ATE_UTF";
13091     /* HP extensions.  */
13092     case DW_ATE_HP_float80:
13093       return "DW_ATE_HP_float80";
13094     case DW_ATE_HP_complex_float80:
13095       return "DW_ATE_HP_complex_float80";
13096     case DW_ATE_HP_float128:
13097       return "DW_ATE_HP_float128";
13098     case DW_ATE_HP_complex_float128:
13099       return "DW_ATE_HP_complex_float128";
13100     case DW_ATE_HP_floathpintel:
13101       return "DW_ATE_HP_floathpintel";
13102     case DW_ATE_HP_imaginary_float80:
13103       return "DW_ATE_HP_imaginary_float80";
13104     case DW_ATE_HP_imaginary_float128:
13105       return "DW_ATE_HP_imaginary_float128";
13106     default:
13107       return "DW_ATE_<unknown>";
13108     }
13109 }
13110
13111 /* Convert a DWARF call frame info operation to its string name.  */
13112
13113 #if 0
13114 static char *
13115 dwarf_cfi_name (unsigned cfi_opc)
13116 {
13117   switch (cfi_opc)
13118     {
13119     case DW_CFA_advance_loc:
13120       return "DW_CFA_advance_loc";
13121     case DW_CFA_offset:
13122       return "DW_CFA_offset";
13123     case DW_CFA_restore:
13124       return "DW_CFA_restore";
13125     case DW_CFA_nop:
13126       return "DW_CFA_nop";
13127     case DW_CFA_set_loc:
13128       return "DW_CFA_set_loc";
13129     case DW_CFA_advance_loc1:
13130       return "DW_CFA_advance_loc1";
13131     case DW_CFA_advance_loc2:
13132       return "DW_CFA_advance_loc2";
13133     case DW_CFA_advance_loc4:
13134       return "DW_CFA_advance_loc4";
13135     case DW_CFA_offset_extended:
13136       return "DW_CFA_offset_extended";
13137     case DW_CFA_restore_extended:
13138       return "DW_CFA_restore_extended";
13139     case DW_CFA_undefined:
13140       return "DW_CFA_undefined";
13141     case DW_CFA_same_value:
13142       return "DW_CFA_same_value";
13143     case DW_CFA_register:
13144       return "DW_CFA_register";
13145     case DW_CFA_remember_state:
13146       return "DW_CFA_remember_state";
13147     case DW_CFA_restore_state:
13148       return "DW_CFA_restore_state";
13149     case DW_CFA_def_cfa:
13150       return "DW_CFA_def_cfa";
13151     case DW_CFA_def_cfa_register:
13152       return "DW_CFA_def_cfa_register";
13153     case DW_CFA_def_cfa_offset:
13154       return "DW_CFA_def_cfa_offset";
13155     /* DWARF 3.  */
13156     case DW_CFA_def_cfa_expression:
13157       return "DW_CFA_def_cfa_expression";
13158     case DW_CFA_expression:
13159       return "DW_CFA_expression";
13160     case DW_CFA_offset_extended_sf:
13161       return "DW_CFA_offset_extended_sf";
13162     case DW_CFA_def_cfa_sf:
13163       return "DW_CFA_def_cfa_sf";
13164     case DW_CFA_def_cfa_offset_sf:
13165       return "DW_CFA_def_cfa_offset_sf";
13166     case DW_CFA_val_offset:
13167       return "DW_CFA_val_offset";
13168     case DW_CFA_val_offset_sf:
13169       return "DW_CFA_val_offset_sf";
13170     case DW_CFA_val_expression:
13171       return "DW_CFA_val_expression";
13172     /* SGI/MIPS specific.  */
13173     case DW_CFA_MIPS_advance_loc8:
13174       return "DW_CFA_MIPS_advance_loc8";
13175     /* GNU extensions.  */
13176     case DW_CFA_GNU_window_save:
13177       return "DW_CFA_GNU_window_save";
13178     case DW_CFA_GNU_args_size:
13179       return "DW_CFA_GNU_args_size";
13180     case DW_CFA_GNU_negative_offset_extended:
13181       return "DW_CFA_GNU_negative_offset_extended";
13182     default:
13183       return "DW_CFA_<unknown>";
13184     }
13185 }
13186 #endif
13187
13188 static void
13189 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13190 {
13191   unsigned int i;
13192
13193   print_spaces (indent, f);
13194   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13195            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13196
13197   if (die->parent != NULL)
13198     {
13199       print_spaces (indent, f);
13200       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13201                           die->parent->offset);
13202     }
13203
13204   print_spaces (indent, f);
13205   fprintf_unfiltered (f, "  has children: %s\n",
13206            dwarf_bool_name (die->child != NULL));
13207
13208   print_spaces (indent, f);
13209   fprintf_unfiltered (f, "  attributes:\n");
13210
13211   for (i = 0; i < die->num_attrs; ++i)
13212     {
13213       print_spaces (indent, f);
13214       fprintf_unfiltered (f, "    %s (%s) ",
13215                dwarf_attr_name (die->attrs[i].name),
13216                dwarf_form_name (die->attrs[i].form));
13217
13218       switch (die->attrs[i].form)
13219         {
13220         case DW_FORM_ref_addr:
13221         case DW_FORM_addr:
13222           fprintf_unfiltered (f, "address: ");
13223           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13224           break;
13225         case DW_FORM_block2:
13226         case DW_FORM_block4:
13227         case DW_FORM_block:
13228         case DW_FORM_block1:
13229           fprintf_unfiltered (f, "block: size %d",
13230                               DW_BLOCK (&die->attrs[i])->size);
13231           break;
13232         case DW_FORM_exprloc:
13233           fprintf_unfiltered (f, "expression: size %u",
13234                               DW_BLOCK (&die->attrs[i])->size);
13235           break;
13236         case DW_FORM_ref1:
13237         case DW_FORM_ref2:
13238         case DW_FORM_ref4:
13239           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13240                               (long) (DW_ADDR (&die->attrs[i])));
13241           break;
13242         case DW_FORM_data1:
13243         case DW_FORM_data2:
13244         case DW_FORM_data4:
13245         case DW_FORM_data8:
13246         case DW_FORM_udata:
13247         case DW_FORM_sdata:
13248           fprintf_unfiltered (f, "constant: %s",
13249                               pulongest (DW_UNSND (&die->attrs[i])));
13250           break;
13251         case DW_FORM_sec_offset:
13252           fprintf_unfiltered (f, "section offset: %s",
13253                               pulongest (DW_UNSND (&die->attrs[i])));
13254           break;
13255         case DW_FORM_ref_sig8:
13256           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13257             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13258                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13259           else
13260             fprintf_unfiltered (f, "signatured type, offset: unknown");
13261           break;
13262         case DW_FORM_string:
13263         case DW_FORM_strp:
13264           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13265                    DW_STRING (&die->attrs[i])
13266                    ? DW_STRING (&die->attrs[i]) : "",
13267                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13268           break;
13269         case DW_FORM_flag:
13270           if (DW_UNSND (&die->attrs[i]))
13271             fprintf_unfiltered (f, "flag: TRUE");
13272           else
13273             fprintf_unfiltered (f, "flag: FALSE");
13274           break;
13275         case DW_FORM_flag_present:
13276           fprintf_unfiltered (f, "flag: TRUE");
13277           break;
13278         case DW_FORM_indirect:
13279           /* The reader will have reduced the indirect form to
13280              the "base form" so this form should not occur.  */
13281           fprintf_unfiltered (f, 
13282                               "unexpected attribute form: DW_FORM_indirect");
13283           break;
13284         default:
13285           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13286                    die->attrs[i].form);
13287           break;
13288         }
13289       fprintf_unfiltered (f, "\n");
13290     }
13291 }
13292
13293 static void
13294 dump_die_for_error (struct die_info *die)
13295 {
13296   dump_die_shallow (gdb_stderr, 0, die);
13297 }
13298
13299 static void
13300 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13301 {
13302   int indent = level * 4;
13303
13304   gdb_assert (die != NULL);
13305
13306   if (level >= max_level)
13307     return;
13308
13309   dump_die_shallow (f, indent, die);
13310
13311   if (die->child != NULL)
13312     {
13313       print_spaces (indent, f);
13314       fprintf_unfiltered (f, "  Children:");
13315       if (level + 1 < max_level)
13316         {
13317           fprintf_unfiltered (f, "\n");
13318           dump_die_1 (f, level + 1, max_level, die->child);
13319         }
13320       else
13321         {
13322           fprintf_unfiltered (f,
13323                               " [not printed, max nesting level reached]\n");
13324         }
13325     }
13326
13327   if (die->sibling != NULL && level > 0)
13328     {
13329       dump_die_1 (f, level, max_level, die->sibling);
13330     }
13331 }
13332
13333 /* This is called from the pdie macro in gdbinit.in.
13334    It's not static so gcc will keep a copy callable from gdb.  */
13335
13336 void
13337 dump_die (struct die_info *die, int max_level)
13338 {
13339   dump_die_1 (gdb_stdlog, 0, max_level, die);
13340 }
13341
13342 static void
13343 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13344 {
13345   void **slot;
13346
13347   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13348
13349   *slot = die;
13350 }
13351
13352 static int
13353 is_ref_attr (struct attribute *attr)
13354 {
13355   switch (attr->form)
13356     {
13357     case DW_FORM_ref_addr:
13358     case DW_FORM_ref1:
13359     case DW_FORM_ref2:
13360     case DW_FORM_ref4:
13361     case DW_FORM_ref8:
13362     case DW_FORM_ref_udata:
13363       return 1;
13364     default:
13365       return 0;
13366     }
13367 }
13368
13369 static unsigned int
13370 dwarf2_get_ref_die_offset (struct attribute *attr)
13371 {
13372   if (is_ref_attr (attr))
13373     return DW_ADDR (attr);
13374
13375   complaint (&symfile_complaints,
13376              _("unsupported die ref attribute form: '%s'"),
13377              dwarf_form_name (attr->form));
13378   return 0;
13379 }
13380
13381 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13382  * the value held by the attribute is not constant.  */
13383
13384 static LONGEST
13385 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13386 {
13387   if (attr->form == DW_FORM_sdata)
13388     return DW_SND (attr);
13389   else if (attr->form == DW_FORM_udata
13390            || attr->form == DW_FORM_data1
13391            || attr->form == DW_FORM_data2
13392            || attr->form == DW_FORM_data4
13393            || attr->form == DW_FORM_data8)
13394     return DW_UNSND (attr);
13395   else
13396     {
13397       complaint (&symfile_complaints,
13398                  _("Attribute value is not a constant (%s)"),
13399                  dwarf_form_name (attr->form));
13400       return default_value;
13401     }
13402 }
13403
13404 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13405    unit and add it to our queue.
13406    The result is non-zero if PER_CU was queued, otherwise the result is zero
13407    meaning either PER_CU is already queued or it is already loaded.  */
13408
13409 static int
13410 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13411                        struct dwarf2_per_cu_data *per_cu)
13412 {
13413   /* We may arrive here during partial symbol reading, if we need full
13414      DIEs to process an unusual case (e.g. template arguments).  Do
13415      not queue PER_CU, just tell our caller to load its DIEs.  */
13416   if (dwarf2_per_objfile->reading_partial_symbols)
13417     {
13418       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13419         return 1;
13420       return 0;
13421     }
13422
13423   /* Mark the dependence relation so that we don't flush PER_CU
13424      too early.  */
13425   dwarf2_add_dependence (this_cu, per_cu);
13426
13427   /* If it's already on the queue, we have nothing to do.  */
13428   if (per_cu->queued)
13429     return 0;
13430
13431   /* If the compilation unit is already loaded, just mark it as
13432      used.  */
13433   if (per_cu->cu != NULL)
13434     {
13435       per_cu->cu->last_used = 0;
13436       return 0;
13437     }
13438
13439   /* Add it to the queue.  */
13440   queue_comp_unit (per_cu, this_cu->objfile);
13441
13442   return 1;
13443 }
13444
13445 /* Follow reference or signature attribute ATTR of SRC_DIE.
13446    On entry *REF_CU is the CU of SRC_DIE.
13447    On exit *REF_CU is the CU of the result.  */
13448
13449 static struct die_info *
13450 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13451                        struct dwarf2_cu **ref_cu)
13452 {
13453   struct die_info *die;
13454
13455   if (is_ref_attr (attr))
13456     die = follow_die_ref (src_die, attr, ref_cu);
13457   else if (attr->form == DW_FORM_ref_sig8)
13458     die = follow_die_sig (src_die, attr, ref_cu);
13459   else
13460     {
13461       dump_die_for_error (src_die);
13462       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13463              (*ref_cu)->objfile->name);
13464     }
13465
13466   return die;
13467 }
13468
13469 /* Follow reference OFFSET.
13470    On entry *REF_CU is the CU of the source die referencing OFFSET.
13471    On exit *REF_CU is the CU of the result.
13472    Returns NULL if OFFSET is invalid.  */
13473
13474 static struct die_info *
13475 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13476 {
13477   struct die_info temp_die;
13478   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13479
13480   gdb_assert (cu->per_cu != NULL);
13481
13482   target_cu = cu;
13483
13484   if (cu->per_cu->from_debug_types)
13485     {
13486       /* .debug_types CUs cannot reference anything outside their CU.
13487          If they need to, they have to reference a signatured type via
13488          DW_FORM_ref_sig8.  */
13489       if (! offset_in_cu_p (&cu->header, offset))
13490         return NULL;
13491     }
13492   else if (! offset_in_cu_p (&cu->header, offset))
13493     {
13494       struct dwarf2_per_cu_data *per_cu;
13495
13496       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13497
13498       /* If necessary, add it to the queue and load its DIEs.  */
13499       if (maybe_queue_comp_unit (cu, per_cu))
13500         load_full_comp_unit (per_cu, cu->objfile);
13501
13502       target_cu = per_cu->cu;
13503     }
13504   else if (cu->dies == NULL)
13505     {
13506       /* We're loading full DIEs during partial symbol reading.  */
13507       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13508       load_full_comp_unit (cu->per_cu, cu->objfile);
13509     }
13510
13511   *ref_cu = target_cu;
13512   temp_die.offset = offset;
13513   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13514 }
13515
13516 /* Follow reference attribute ATTR of SRC_DIE.
13517    On entry *REF_CU is the CU of SRC_DIE.
13518    On exit *REF_CU is the CU of the result.  */
13519
13520 static struct die_info *
13521 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13522                 struct dwarf2_cu **ref_cu)
13523 {
13524   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13525   struct dwarf2_cu *cu = *ref_cu;
13526   struct die_info *die;
13527
13528   die = follow_die_offset (offset, ref_cu);
13529   if (!die)
13530     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13531            "at 0x%x [in module %s]"),
13532            offset, src_die->offset, cu->objfile->name);
13533
13534   return die;
13535 }
13536
13537 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13538    value is intended for DW_OP_call*.  */
13539
13540 struct dwarf2_locexpr_baton
13541 dwarf2_fetch_die_location_block (unsigned int offset,
13542                                  struct dwarf2_per_cu_data *per_cu,
13543                                  CORE_ADDR (*get_frame_pc) (void *baton),
13544                                  void *baton)
13545 {
13546   struct dwarf2_cu *cu = per_cu->cu;
13547   struct die_info *die;
13548   struct attribute *attr;
13549   struct dwarf2_locexpr_baton retval;
13550
13551   dw2_setup (per_cu->objfile);
13552
13553   die = follow_die_offset (offset, &cu);
13554   if (!die)
13555     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13556            offset, per_cu->cu->objfile->name);
13557
13558   attr = dwarf2_attr (die, DW_AT_location, cu);
13559   if (!attr)
13560     {
13561       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13562
13563       retval.data = NULL;
13564       retval.size = 0;
13565     }
13566   else if (attr_form_is_section_offset (attr))
13567     {
13568       struct dwarf2_loclist_baton loclist_baton;
13569       CORE_ADDR pc = (*get_frame_pc) (baton);
13570       size_t size;
13571
13572       fill_in_loclist_baton (cu, &loclist_baton, attr);
13573
13574       retval.data = dwarf2_find_location_expression (&loclist_baton,
13575                                                      &size, pc);
13576       retval.size = size;
13577     }
13578   else
13579     {
13580       if (!attr_form_is_block (attr))
13581         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13582                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13583                offset, per_cu->cu->objfile->name);
13584
13585       retval.data = DW_BLOCK (attr)->data;
13586       retval.size = DW_BLOCK (attr)->size;
13587     }
13588   retval.per_cu = cu->per_cu;
13589   return retval;
13590 }
13591
13592 /* Follow the signature attribute ATTR in SRC_DIE.
13593    On entry *REF_CU is the CU of SRC_DIE.
13594    On exit *REF_CU is the CU of the result.  */
13595
13596 static struct die_info *
13597 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13598                 struct dwarf2_cu **ref_cu)
13599 {
13600   struct objfile *objfile = (*ref_cu)->objfile;
13601   struct die_info temp_die;
13602   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13603   struct dwarf2_cu *sig_cu;
13604   struct die_info *die;
13605
13606   /* sig_type will be NULL if the signatured type is missing from
13607      the debug info.  */
13608   if (sig_type == NULL)
13609     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13610              "at 0x%x [in module %s]"),
13611            src_die->offset, objfile->name);
13612
13613   /* If necessary, add it to the queue and load its DIEs.  */
13614
13615   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13616     read_signatured_type (objfile, sig_type);
13617
13618   gdb_assert (sig_type->per_cu.cu != NULL);
13619
13620   sig_cu = sig_type->per_cu.cu;
13621   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13622   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13623   if (die)
13624     {
13625       *ref_cu = sig_cu;
13626       return die;
13627     }
13628
13629   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13630          "from DIE at 0x%x [in module %s]"),
13631          sig_type->type_offset, src_die->offset, objfile->name);
13632 }
13633
13634 /* Given an offset of a signatured type, return its signatured_type.  */
13635
13636 static struct signatured_type *
13637 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13638 {
13639   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13640   unsigned int length, initial_length_size;
13641   unsigned int sig_offset;
13642   struct signatured_type find_entry, *type_sig;
13643
13644   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13645   sig_offset = (initial_length_size
13646                 + 2 /*version*/
13647                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13648                 + 1 /*address_size*/);
13649   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13650   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13651
13652   /* This is only used to lookup previously recorded types.
13653      If we didn't find it, it's our bug.  */
13654   gdb_assert (type_sig != NULL);
13655   gdb_assert (offset == type_sig->offset);
13656
13657   return type_sig;
13658 }
13659
13660 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13661
13662 static void
13663 read_signatured_type_at_offset (struct objfile *objfile,
13664                                 unsigned int offset)
13665 {
13666   struct signatured_type *type_sig;
13667
13668   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13669
13670   /* We have the section offset, but we need the signature to do the
13671      hash table lookup.  */
13672   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13673
13674   gdb_assert (type_sig->per_cu.cu == NULL);
13675
13676   read_signatured_type (objfile, type_sig);
13677
13678   gdb_assert (type_sig->per_cu.cu != NULL);
13679 }
13680
13681 /* Read in a signatured type and build its CU and DIEs.  */
13682
13683 static void
13684 read_signatured_type (struct objfile *objfile,
13685                       struct signatured_type *type_sig)
13686 {
13687   gdb_byte *types_ptr;
13688   struct die_reader_specs reader_specs;
13689   struct dwarf2_cu *cu;
13690   ULONGEST signature;
13691   struct cleanup *back_to, *free_cu_cleanup;
13692
13693   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13694   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13695
13696   gdb_assert (type_sig->per_cu.cu == NULL);
13697
13698   cu = xmalloc (sizeof (*cu));
13699   init_one_comp_unit (cu, objfile);
13700
13701   type_sig->per_cu.cu = cu;
13702   cu->per_cu = &type_sig->per_cu;
13703
13704   /* If an error occurs while loading, release our storage.  */
13705   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13706
13707   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13708                                         types_ptr, objfile->obfd);
13709   gdb_assert (signature == type_sig->signature);
13710
13711   cu->die_hash
13712     = htab_create_alloc_ex (cu->header.length / 12,
13713                             die_hash,
13714                             die_eq,
13715                             NULL,
13716                             &cu->comp_unit_obstack,
13717                             hashtab_obstack_allocate,
13718                             dummy_obstack_deallocate);
13719
13720   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13721   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13722
13723   init_cu_die_reader (&reader_specs, cu);
13724
13725   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13726                                     NULL /*parent*/);
13727
13728   /* We try not to read any attributes in this function, because not
13729      all objfiles needed for references have been loaded yet, and symbol
13730      table processing isn't initialized.  But we have to set the CU language,
13731      or we won't be able to build types correctly.  */
13732   prepare_one_comp_unit (cu, cu->dies);
13733
13734   do_cleanups (back_to);
13735
13736   /* We've successfully allocated this compilation unit.  Let our caller
13737      clean it up when finished with it.  */
13738   discard_cleanups (free_cu_cleanup);
13739
13740   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13741   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13742 }
13743
13744 /* Decode simple location descriptions.
13745    Given a pointer to a dwarf block that defines a location, compute
13746    the location and return the value.
13747
13748    NOTE drow/2003-11-18: This function is called in two situations
13749    now: for the address of static or global variables (partial symbols
13750    only) and for offsets into structures which are expected to be
13751    (more or less) constant.  The partial symbol case should go away,
13752    and only the constant case should remain.  That will let this
13753    function complain more accurately.  A few special modes are allowed
13754    without complaint for global variables (for instance, global
13755    register values and thread-local values).
13756
13757    A location description containing no operations indicates that the
13758    object is optimized out.  The return value is 0 for that case.
13759    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13760    callers will only want a very basic result and this can become a
13761    complaint.
13762
13763    Note that stack[0] is unused except as a default error return.  */
13764
13765 static CORE_ADDR
13766 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13767 {
13768   struct objfile *objfile = cu->objfile;
13769   int i;
13770   int size = blk->size;
13771   gdb_byte *data = blk->data;
13772   CORE_ADDR stack[64];
13773   int stacki;
13774   unsigned int bytes_read, unsnd;
13775   gdb_byte op;
13776
13777   i = 0;
13778   stacki = 0;
13779   stack[stacki] = 0;
13780   stack[++stacki] = 0;
13781
13782   while (i < size)
13783     {
13784       op = data[i++];
13785       switch (op)
13786         {
13787         case DW_OP_lit0:
13788         case DW_OP_lit1:
13789         case DW_OP_lit2:
13790         case DW_OP_lit3:
13791         case DW_OP_lit4:
13792         case DW_OP_lit5:
13793         case DW_OP_lit6:
13794         case DW_OP_lit7:
13795         case DW_OP_lit8:
13796         case DW_OP_lit9:
13797         case DW_OP_lit10:
13798         case DW_OP_lit11:
13799         case DW_OP_lit12:
13800         case DW_OP_lit13:
13801         case DW_OP_lit14:
13802         case DW_OP_lit15:
13803         case DW_OP_lit16:
13804         case DW_OP_lit17:
13805         case DW_OP_lit18:
13806         case DW_OP_lit19:
13807         case DW_OP_lit20:
13808         case DW_OP_lit21:
13809         case DW_OP_lit22:
13810         case DW_OP_lit23:
13811         case DW_OP_lit24:
13812         case DW_OP_lit25:
13813         case DW_OP_lit26:
13814         case DW_OP_lit27:
13815         case DW_OP_lit28:
13816         case DW_OP_lit29:
13817         case DW_OP_lit30:
13818         case DW_OP_lit31:
13819           stack[++stacki] = op - DW_OP_lit0;
13820           break;
13821
13822         case DW_OP_reg0:
13823         case DW_OP_reg1:
13824         case DW_OP_reg2:
13825         case DW_OP_reg3:
13826         case DW_OP_reg4:
13827         case DW_OP_reg5:
13828         case DW_OP_reg6:
13829         case DW_OP_reg7:
13830         case DW_OP_reg8:
13831         case DW_OP_reg9:
13832         case DW_OP_reg10:
13833         case DW_OP_reg11:
13834         case DW_OP_reg12:
13835         case DW_OP_reg13:
13836         case DW_OP_reg14:
13837         case DW_OP_reg15:
13838         case DW_OP_reg16:
13839         case DW_OP_reg17:
13840         case DW_OP_reg18:
13841         case DW_OP_reg19:
13842         case DW_OP_reg20:
13843         case DW_OP_reg21:
13844         case DW_OP_reg22:
13845         case DW_OP_reg23:
13846         case DW_OP_reg24:
13847         case DW_OP_reg25:
13848         case DW_OP_reg26:
13849         case DW_OP_reg27:
13850         case DW_OP_reg28:
13851         case DW_OP_reg29:
13852         case DW_OP_reg30:
13853         case DW_OP_reg31:
13854           stack[++stacki] = op - DW_OP_reg0;
13855           if (i < size)
13856             dwarf2_complex_location_expr_complaint ();
13857           break;
13858
13859         case DW_OP_regx:
13860           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13861           i += bytes_read;
13862           stack[++stacki] = unsnd;
13863           if (i < size)
13864             dwarf2_complex_location_expr_complaint ();
13865           break;
13866
13867         case DW_OP_addr:
13868           stack[++stacki] = read_address (objfile->obfd, &data[i],
13869                                           cu, &bytes_read);
13870           i += bytes_read;
13871           break;
13872
13873         case DW_OP_const1u:
13874           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13875           i += 1;
13876           break;
13877
13878         case DW_OP_const1s:
13879           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13880           i += 1;
13881           break;
13882
13883         case DW_OP_const2u:
13884           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13885           i += 2;
13886           break;
13887
13888         case DW_OP_const2s:
13889           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13890           i += 2;
13891           break;
13892
13893         case DW_OP_const4u:
13894           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13895           i += 4;
13896           break;
13897
13898         case DW_OP_const4s:
13899           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13900           i += 4;
13901           break;
13902
13903         case DW_OP_constu:
13904           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13905                                                   &bytes_read);
13906           i += bytes_read;
13907           break;
13908
13909         case DW_OP_consts:
13910           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13911           i += bytes_read;
13912           break;
13913
13914         case DW_OP_dup:
13915           stack[stacki + 1] = stack[stacki];
13916           stacki++;
13917           break;
13918
13919         case DW_OP_plus:
13920           stack[stacki - 1] += stack[stacki];
13921           stacki--;
13922           break;
13923
13924         case DW_OP_plus_uconst:
13925           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13926                                                  &bytes_read);
13927           i += bytes_read;
13928           break;
13929
13930         case DW_OP_minus:
13931           stack[stacki - 1] -= stack[stacki];
13932           stacki--;
13933           break;
13934
13935         case DW_OP_deref:
13936           /* If we're not the last op, then we definitely can't encode
13937              this using GDB's address_class enum.  This is valid for partial
13938              global symbols, although the variable's address will be bogus
13939              in the psymtab.  */
13940           if (i < size)
13941             dwarf2_complex_location_expr_complaint ();
13942           break;
13943
13944         case DW_OP_GNU_push_tls_address:
13945           /* The top of the stack has the offset from the beginning
13946              of the thread control block at which the variable is located.  */
13947           /* Nothing should follow this operator, so the top of stack would
13948              be returned.  */
13949           /* This is valid for partial global symbols, but the variable's
13950              address will be bogus in the psymtab.  */
13951           if (i < size)
13952             dwarf2_complex_location_expr_complaint ();
13953           break;
13954
13955         case DW_OP_GNU_uninit:
13956           break;
13957
13958         default:
13959           {
13960             const char *name = dwarf_stack_op_name (op);
13961
13962             if (name)
13963               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13964                          name);
13965             else
13966               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13967                          op);
13968           }
13969
13970           return (stack[stacki]);
13971         }
13972
13973       /* Enforce maximum stack depth of SIZE-1 to avoid writing
13974          outside of the allocated space.  Also enforce minimum>0.  */
13975       if (stacki >= ARRAY_SIZE (stack) - 1)
13976         {
13977           complaint (&symfile_complaints,
13978                      _("location description stack overflow"));
13979           return 0;
13980         }
13981
13982       if (stacki <= 0)
13983         {
13984           complaint (&symfile_complaints,
13985                      _("location description stack underflow"));
13986           return 0;
13987         }
13988     }
13989   return (stack[stacki]);
13990 }
13991
13992 /* memory allocation interface */
13993
13994 static struct dwarf_block *
13995 dwarf_alloc_block (struct dwarf2_cu *cu)
13996 {
13997   struct dwarf_block *blk;
13998
13999   blk = (struct dwarf_block *)
14000     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14001   return (blk);
14002 }
14003
14004 static struct abbrev_info *
14005 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14006 {
14007   struct abbrev_info *abbrev;
14008
14009   abbrev = (struct abbrev_info *)
14010     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14011   memset (abbrev, 0, sizeof (struct abbrev_info));
14012   return (abbrev);
14013 }
14014
14015 static struct die_info *
14016 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14017 {
14018   struct die_info *die;
14019   size_t size = sizeof (struct die_info);
14020
14021   if (num_attrs > 1)
14022     size += (num_attrs - 1) * sizeof (struct attribute);
14023
14024   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14025   memset (die, 0, sizeof (struct die_info));
14026   return (die);
14027 }
14028
14029 \f
14030 /* Macro support.  */
14031
14032 /* Return the full name of file number I in *LH's file name table.
14033    Use COMP_DIR as the name of the current directory of the
14034    compilation.  The result is allocated using xmalloc; the caller is
14035    responsible for freeing it.  */
14036 static char *
14037 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14038 {
14039   /* Is the file number a valid index into the line header's file name
14040      table?  Remember that file numbers start with one, not zero.  */
14041   if (1 <= file && file <= lh->num_file_names)
14042     {
14043       struct file_entry *fe = &lh->file_names[file - 1];
14044
14045       if (IS_ABSOLUTE_PATH (fe->name))
14046         return xstrdup (fe->name);
14047       else
14048         {
14049           const char *dir;
14050           int dir_len;
14051           char *full_name;
14052
14053           if (fe->dir_index)
14054             dir = lh->include_dirs[fe->dir_index - 1];
14055           else
14056             dir = comp_dir;
14057
14058           if (dir)
14059             {
14060               dir_len = strlen (dir);
14061               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14062               strcpy (full_name, dir);
14063               full_name[dir_len] = '/';
14064               strcpy (full_name + dir_len + 1, fe->name);
14065               return full_name;
14066             }
14067           else
14068             return xstrdup (fe->name);
14069         }
14070     }
14071   else
14072     {
14073       /* The compiler produced a bogus file number.  We can at least
14074          record the macro definitions made in the file, even if we
14075          won't be able to find the file by name.  */
14076       char fake_name[80];
14077
14078       sprintf (fake_name, "<bad macro file number %d>", file);
14079
14080       complaint (&symfile_complaints,
14081                  _("bad file number in macro information (%d)"),
14082                  file);
14083
14084       return xstrdup (fake_name);
14085     }
14086 }
14087
14088
14089 static struct macro_source_file *
14090 macro_start_file (int file, int line,
14091                   struct macro_source_file *current_file,
14092                   const char *comp_dir,
14093                   struct line_header *lh, struct objfile *objfile)
14094 {
14095   /* The full name of this source file.  */
14096   char *full_name = file_full_name (file, lh, comp_dir);
14097
14098   /* We don't create a macro table for this compilation unit
14099      at all until we actually get a filename.  */
14100   if (! pending_macros)
14101     pending_macros = new_macro_table (&objfile->objfile_obstack,
14102                                       objfile->macro_cache);
14103
14104   if (! current_file)
14105     /* If we have no current file, then this must be the start_file
14106        directive for the compilation unit's main source file.  */
14107     current_file = macro_set_main (pending_macros, full_name);
14108   else
14109     current_file = macro_include (current_file, line, full_name);
14110
14111   xfree (full_name);
14112
14113   return current_file;
14114 }
14115
14116
14117 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14118    followed by a null byte.  */
14119 static char *
14120 copy_string (const char *buf, int len)
14121 {
14122   char *s = xmalloc (len + 1);
14123
14124   memcpy (s, buf, len);
14125   s[len] = '\0';
14126   return s;
14127 }
14128
14129
14130 static const char *
14131 consume_improper_spaces (const char *p, const char *body)
14132 {
14133   if (*p == ' ')
14134     {
14135       complaint (&symfile_complaints,
14136                  _("macro definition contains spaces "
14137                    "in formal argument list:\n`%s'"),
14138                  body);
14139
14140       while (*p == ' ')
14141         p++;
14142     }
14143
14144   return p;
14145 }
14146
14147
14148 static void
14149 parse_macro_definition (struct macro_source_file *file, int line,
14150                         const char *body)
14151 {
14152   const char *p;
14153
14154   /* The body string takes one of two forms.  For object-like macro
14155      definitions, it should be:
14156
14157         <macro name> " " <definition>
14158
14159      For function-like macro definitions, it should be:
14160
14161         <macro name> "() " <definition>
14162      or
14163         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14164
14165      Spaces may appear only where explicitly indicated, and in the
14166      <definition>.
14167
14168      The Dwarf 2 spec says that an object-like macro's name is always
14169      followed by a space, but versions of GCC around March 2002 omit
14170      the space when the macro's definition is the empty string.
14171
14172      The Dwarf 2 spec says that there should be no spaces between the
14173      formal arguments in a function-like macro's formal argument list,
14174      but versions of GCC around March 2002 include spaces after the
14175      commas.  */
14176
14177
14178   /* Find the extent of the macro name.  The macro name is terminated
14179      by either a space or null character (for an object-like macro) or
14180      an opening paren (for a function-like macro).  */
14181   for (p = body; *p; p++)
14182     if (*p == ' ' || *p == '(')
14183       break;
14184
14185   if (*p == ' ' || *p == '\0')
14186     {
14187       /* It's an object-like macro.  */
14188       int name_len = p - body;
14189       char *name = copy_string (body, name_len);
14190       const char *replacement;
14191
14192       if (*p == ' ')
14193         replacement = body + name_len + 1;
14194       else
14195         {
14196           dwarf2_macro_malformed_definition_complaint (body);
14197           replacement = body + name_len;
14198         }
14199
14200       macro_define_object (file, line, name, replacement);
14201
14202       xfree (name);
14203     }
14204   else if (*p == '(')
14205     {
14206       /* It's a function-like macro.  */
14207       char *name = copy_string (body, p - body);
14208       int argc = 0;
14209       int argv_size = 1;
14210       char **argv = xmalloc (argv_size * sizeof (*argv));
14211
14212       p++;
14213
14214       p = consume_improper_spaces (p, body);
14215
14216       /* Parse the formal argument list.  */
14217       while (*p && *p != ')')
14218         {
14219           /* Find the extent of the current argument name.  */
14220           const char *arg_start = p;
14221
14222           while (*p && *p != ',' && *p != ')' && *p != ' ')
14223             p++;
14224
14225           if (! *p || p == arg_start)
14226             dwarf2_macro_malformed_definition_complaint (body);
14227           else
14228             {
14229               /* Make sure argv has room for the new argument.  */
14230               if (argc >= argv_size)
14231                 {
14232                   argv_size *= 2;
14233                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14234                 }
14235
14236               argv[argc++] = copy_string (arg_start, p - arg_start);
14237             }
14238
14239           p = consume_improper_spaces (p, body);
14240
14241           /* Consume the comma, if present.  */
14242           if (*p == ',')
14243             {
14244               p++;
14245
14246               p = consume_improper_spaces (p, body);
14247             }
14248         }
14249
14250       if (*p == ')')
14251         {
14252           p++;
14253
14254           if (*p == ' ')
14255             /* Perfectly formed definition, no complaints.  */
14256             macro_define_function (file, line, name,
14257                                    argc, (const char **) argv,
14258                                    p + 1);
14259           else if (*p == '\0')
14260             {
14261               /* Complain, but do define it.  */
14262               dwarf2_macro_malformed_definition_complaint (body);
14263               macro_define_function (file, line, name,
14264                                      argc, (const char **) argv,
14265                                      p);
14266             }
14267           else
14268             /* Just complain.  */
14269             dwarf2_macro_malformed_definition_complaint (body);
14270         }
14271       else
14272         /* Just complain.  */
14273         dwarf2_macro_malformed_definition_complaint (body);
14274
14275       xfree (name);
14276       {
14277         int i;
14278
14279         for (i = 0; i < argc; i++)
14280           xfree (argv[i]);
14281       }
14282       xfree (argv);
14283     }
14284   else
14285     dwarf2_macro_malformed_definition_complaint (body);
14286 }
14287
14288
14289 static void
14290 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14291                      char *comp_dir, bfd *abfd,
14292                      struct dwarf2_cu *cu)
14293 {
14294   gdb_byte *mac_ptr, *mac_end;
14295   struct macro_source_file *current_file = 0;
14296   enum dwarf_macinfo_record_type macinfo_type;
14297   int at_commandline;
14298
14299   dwarf2_read_section (dwarf2_per_objfile->objfile,
14300                        &dwarf2_per_objfile->macinfo);
14301   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14302     {
14303       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14304       return;
14305     }
14306
14307   /* First pass: Find the name of the base filename.
14308      This filename is needed in order to process all macros whose definition
14309      (or undefinition) comes from the command line.  These macros are defined
14310      before the first DW_MACINFO_start_file entry, and yet still need to be
14311      associated to the base file.
14312
14313      To determine the base file name, we scan the macro definitions until we
14314      reach the first DW_MACINFO_start_file entry.  We then initialize
14315      CURRENT_FILE accordingly so that any macro definition found before the
14316      first DW_MACINFO_start_file can still be associated to the base file.  */
14317
14318   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14319   mac_end = dwarf2_per_objfile->macinfo.buffer
14320     + dwarf2_per_objfile->macinfo.size;
14321
14322   do
14323     {
14324       /* Do we at least have room for a macinfo type byte?  */
14325       if (mac_ptr >= mac_end)
14326         {
14327           /* Complaint is printed during the second pass as GDB will probably
14328              stop the first pass earlier upon finding
14329              DW_MACINFO_start_file.  */
14330           break;
14331         }
14332
14333       macinfo_type = read_1_byte (abfd, mac_ptr);
14334       mac_ptr++;
14335
14336       switch (macinfo_type)
14337         {
14338           /* A zero macinfo type indicates the end of the macro
14339              information.  */
14340         case 0:
14341           break;
14342
14343         case DW_MACINFO_define:
14344         case DW_MACINFO_undef:
14345           /* Only skip the data by MAC_PTR.  */
14346           {
14347             unsigned int bytes_read;
14348
14349             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14350             mac_ptr += bytes_read;
14351             read_direct_string (abfd, mac_ptr, &bytes_read);
14352             mac_ptr += bytes_read;
14353           }
14354           break;
14355
14356         case DW_MACINFO_start_file:
14357           {
14358             unsigned int bytes_read;
14359             int line, file;
14360
14361             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14362             mac_ptr += bytes_read;
14363             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14364             mac_ptr += bytes_read;
14365
14366             current_file = macro_start_file (file, line, current_file,
14367                                              comp_dir, lh, cu->objfile);
14368           }
14369           break;
14370
14371         case DW_MACINFO_end_file:
14372           /* No data to skip by MAC_PTR.  */
14373           break;
14374
14375         case DW_MACINFO_vendor_ext:
14376           /* Only skip the data by MAC_PTR.  */
14377           {
14378             unsigned int bytes_read;
14379
14380             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14381             mac_ptr += bytes_read;
14382             read_direct_string (abfd, mac_ptr, &bytes_read);
14383             mac_ptr += bytes_read;
14384           }
14385           break;
14386
14387         default:
14388           break;
14389         }
14390     } while (macinfo_type != 0 && current_file == NULL);
14391
14392   /* Second pass: Process all entries.
14393
14394      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14395      command-line macro definitions/undefinitions.  This flag is unset when we
14396      reach the first DW_MACINFO_start_file entry.  */
14397
14398   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14399
14400   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14401      GDB is still reading the definitions from command line.  First
14402      DW_MACINFO_start_file will need to be ignored as it was already executed
14403      to create CURRENT_FILE for the main source holding also the command line
14404      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14405      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14406
14407   at_commandline = 1;
14408
14409   do
14410     {
14411       /* Do we at least have room for a macinfo type byte?  */
14412       if (mac_ptr >= mac_end)
14413         {
14414           dwarf2_macros_too_long_complaint ();
14415           break;
14416         }
14417
14418       macinfo_type = read_1_byte (abfd, mac_ptr);
14419       mac_ptr++;
14420
14421       switch (macinfo_type)
14422         {
14423           /* A zero macinfo type indicates the end of the macro
14424              information.  */
14425         case 0:
14426           break;
14427
14428         case DW_MACINFO_define:
14429         case DW_MACINFO_undef:
14430           {
14431             unsigned int bytes_read;
14432             int line;
14433             char *body;
14434
14435             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14436             mac_ptr += bytes_read;
14437             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14438             mac_ptr += bytes_read;
14439
14440             if (! current_file)
14441               {
14442                 /* DWARF violation as no main source is present.  */
14443                 complaint (&symfile_complaints,
14444                            _("debug info with no main source gives macro %s "
14445                              "on line %d: %s"),
14446                            macinfo_type == DW_MACINFO_define ?
14447                              _("definition") :
14448                                macinfo_type == DW_MACINFO_undef ?
14449                                  _("undefinition") :
14450                                  _("something-or-other"), line, body);
14451                 break;
14452               }
14453             if ((line == 0 && !at_commandline)
14454                 || (line != 0 && at_commandline))
14455               complaint (&symfile_complaints,
14456                          _("debug info gives %s macro %s with %s line %d: %s"),
14457                          at_commandline ? _("command-line") : _("in-file"),
14458                          macinfo_type == DW_MACINFO_define ?
14459                            _("definition") :
14460                              macinfo_type == DW_MACINFO_undef ?
14461                                _("undefinition") :
14462                                _("something-or-other"),
14463                          line == 0 ? _("zero") : _("non-zero"), line, body);
14464
14465             if (macinfo_type == DW_MACINFO_define)
14466               parse_macro_definition (current_file, line, body);
14467             else if (macinfo_type == DW_MACINFO_undef)
14468               macro_undef (current_file, line, body);
14469           }
14470           break;
14471
14472         case DW_MACINFO_start_file:
14473           {
14474             unsigned int bytes_read;
14475             int line, file;
14476
14477             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14478             mac_ptr += bytes_read;
14479             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14480             mac_ptr += bytes_read;
14481
14482             if ((line == 0 && !at_commandline)
14483                 || (line != 0 && at_commandline))
14484               complaint (&symfile_complaints,
14485                          _("debug info gives source %d included "
14486                            "from %s at %s line %d"),
14487                          file, at_commandline ? _("command-line") : _("file"),
14488                          line == 0 ? _("zero") : _("non-zero"), line);
14489
14490             if (at_commandline)
14491               {
14492                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14493                 at_commandline = 0;
14494               }
14495             else
14496               current_file = macro_start_file (file, line,
14497                                                current_file, comp_dir,
14498                                                lh, cu->objfile);
14499           }
14500           break;
14501
14502         case DW_MACINFO_end_file:
14503           if (! current_file)
14504             complaint (&symfile_complaints,
14505                        _("macro debug info has an unmatched "
14506                          "`close_file' directive"));
14507           else
14508             {
14509               current_file = current_file->included_by;
14510               if (! current_file)
14511                 {
14512                   enum dwarf_macinfo_record_type next_type;
14513
14514                   /* GCC circa March 2002 doesn't produce the zero
14515                      type byte marking the end of the compilation
14516                      unit.  Complain if it's not there, but exit no
14517                      matter what.  */
14518
14519                   /* Do we at least have room for a macinfo type byte?  */
14520                   if (mac_ptr >= mac_end)
14521                     {
14522                       dwarf2_macros_too_long_complaint ();
14523                       return;
14524                     }
14525
14526                   /* We don't increment mac_ptr here, so this is just
14527                      a look-ahead.  */
14528                   next_type = read_1_byte (abfd, mac_ptr);
14529                   if (next_type != 0)
14530                     complaint (&symfile_complaints,
14531                                _("no terminating 0-type entry for "
14532                                  "macros in `.debug_macinfo' section"));
14533
14534                   return;
14535                 }
14536             }
14537           break;
14538
14539         case DW_MACINFO_vendor_ext:
14540           {
14541             unsigned int bytes_read;
14542             int constant;
14543
14544             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14545             mac_ptr += bytes_read;
14546             read_direct_string (abfd, mac_ptr, &bytes_read);
14547             mac_ptr += bytes_read;
14548
14549             /* We don't recognize any vendor extensions.  */
14550           }
14551           break;
14552         }
14553     } while (macinfo_type != 0);
14554 }
14555
14556 /* Check if the attribute's form is a DW_FORM_block*
14557    if so return true else false.  */
14558 static int
14559 attr_form_is_block (struct attribute *attr)
14560 {
14561   return (attr == NULL ? 0 :
14562       attr->form == DW_FORM_block1
14563       || attr->form == DW_FORM_block2
14564       || attr->form == DW_FORM_block4
14565       || attr->form == DW_FORM_block
14566       || attr->form == DW_FORM_exprloc);
14567 }
14568
14569 /* Return non-zero if ATTR's value is a section offset --- classes
14570    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14571    You may use DW_UNSND (attr) to retrieve such offsets.
14572
14573    Section 7.5.4, "Attribute Encodings", explains that no attribute
14574    may have a value that belongs to more than one of these classes; it
14575    would be ambiguous if we did, because we use the same forms for all
14576    of them.  */
14577 static int
14578 attr_form_is_section_offset (struct attribute *attr)
14579 {
14580   return (attr->form == DW_FORM_data4
14581           || attr->form == DW_FORM_data8
14582           || attr->form == DW_FORM_sec_offset);
14583 }
14584
14585
14586 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14587    zero otherwise.  When this function returns true, you can apply
14588    dwarf2_get_attr_constant_value to it.
14589
14590    However, note that for some attributes you must check
14591    attr_form_is_section_offset before using this test.  DW_FORM_data4
14592    and DW_FORM_data8 are members of both the constant class, and of
14593    the classes that contain offsets into other debug sections
14594    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14595    that, if an attribute's can be either a constant or one of the
14596    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14597    taken as section offsets, not constants.  */
14598 static int
14599 attr_form_is_constant (struct attribute *attr)
14600 {
14601   switch (attr->form)
14602     {
14603     case DW_FORM_sdata:
14604     case DW_FORM_udata:
14605     case DW_FORM_data1:
14606     case DW_FORM_data2:
14607     case DW_FORM_data4:
14608     case DW_FORM_data8:
14609       return 1;
14610     default:
14611       return 0;
14612     }
14613 }
14614
14615 /* A helper function that fills in a dwarf2_loclist_baton.  */
14616
14617 static void
14618 fill_in_loclist_baton (struct dwarf2_cu *cu,
14619                        struct dwarf2_loclist_baton *baton,
14620                        struct attribute *attr)
14621 {
14622   dwarf2_read_section (dwarf2_per_objfile->objfile,
14623                        &dwarf2_per_objfile->loc);
14624
14625   baton->per_cu = cu->per_cu;
14626   gdb_assert (baton->per_cu);
14627   /* We don't know how long the location list is, but make sure we
14628      don't run off the edge of the section.  */
14629   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14630   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14631   baton->base_address = cu->base_address;
14632 }
14633
14634 static void
14635 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14636                              struct dwarf2_cu *cu)
14637 {
14638   if (attr_form_is_section_offset (attr)
14639       /* ".debug_loc" may not exist at all, or the offset may be outside
14640          the section.  If so, fall through to the complaint in the
14641          other branch.  */
14642       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14643                                                 &dwarf2_per_objfile->loc))
14644     {
14645       struct dwarf2_loclist_baton *baton;
14646
14647       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14648                              sizeof (struct dwarf2_loclist_baton));
14649
14650       fill_in_loclist_baton (cu, baton, attr);
14651
14652       if (cu->base_known == 0)
14653         complaint (&symfile_complaints,
14654                    _("Location list used without "
14655                      "specifying the CU base address."));
14656
14657       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14658       SYMBOL_LOCATION_BATON (sym) = baton;
14659     }
14660   else
14661     {
14662       struct dwarf2_locexpr_baton *baton;
14663
14664       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14665                              sizeof (struct dwarf2_locexpr_baton));
14666       baton->per_cu = cu->per_cu;
14667       gdb_assert (baton->per_cu);
14668
14669       if (attr_form_is_block (attr))
14670         {
14671           /* Note that we're just copying the block's data pointer
14672              here, not the actual data.  We're still pointing into the
14673              info_buffer for SYM's objfile; right now we never release
14674              that buffer, but when we do clean up properly this may
14675              need to change.  */
14676           baton->size = DW_BLOCK (attr)->size;
14677           baton->data = DW_BLOCK (attr)->data;
14678         }
14679       else
14680         {
14681           dwarf2_invalid_attrib_class_complaint ("location description",
14682                                                  SYMBOL_NATURAL_NAME (sym));
14683           baton->size = 0;
14684           baton->data = NULL;
14685         }
14686
14687       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14688       SYMBOL_LOCATION_BATON (sym) = baton;
14689     }
14690 }
14691
14692 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14693    came from a separate debuginfo file, then the master objfile is
14694    returned.  */
14695
14696 struct objfile *
14697 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14698 {
14699   struct objfile *objfile = per_cu->objfile;
14700
14701   /* Return the master objfile, so that we can report and look up the
14702      correct file containing this variable.  */
14703   if (objfile->separate_debug_objfile_backlink)
14704     objfile = objfile->separate_debug_objfile_backlink;
14705
14706   return objfile;
14707 }
14708
14709 /* Return the address size given in the compilation unit header for CU.  */
14710
14711 CORE_ADDR
14712 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14713 {
14714   if (per_cu->cu)
14715     return per_cu->cu->header.addr_size;
14716   else
14717     {
14718       /* If the CU is not currently read in, we re-read its header.  */
14719       struct objfile *objfile = per_cu->objfile;
14720       struct dwarf2_per_objfile *per_objfile
14721         = objfile_data (objfile, dwarf2_objfile_data_key);
14722       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14723       struct comp_unit_head cu_header;
14724
14725       memset (&cu_header, 0, sizeof cu_header);
14726       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14727       return cu_header.addr_size;
14728     }
14729 }
14730
14731 /* Return the offset size given in the compilation unit header for CU.  */
14732
14733 int
14734 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14735 {
14736   if (per_cu->cu)
14737     return per_cu->cu->header.offset_size;
14738   else
14739     {
14740       /* If the CU is not currently read in, we re-read its header.  */
14741       struct objfile *objfile = per_cu->objfile;
14742       struct dwarf2_per_objfile *per_objfile
14743         = objfile_data (objfile, dwarf2_objfile_data_key);
14744       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14745       struct comp_unit_head cu_header;
14746
14747       memset (&cu_header, 0, sizeof cu_header);
14748       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14749       return cu_header.offset_size;
14750     }
14751 }
14752
14753 /* Return the text offset of the CU.  The returned offset comes from
14754    this CU's objfile.  If this objfile came from a separate debuginfo
14755    file, then the offset may be different from the corresponding
14756    offset in the parent objfile.  */
14757
14758 CORE_ADDR
14759 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14760 {
14761   struct objfile *objfile = per_cu->objfile;
14762
14763   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14764 }
14765
14766 /* Locate the .debug_info compilation unit from CU's objfile which contains
14767    the DIE at OFFSET.  Raises an error on failure.  */
14768
14769 static struct dwarf2_per_cu_data *
14770 dwarf2_find_containing_comp_unit (unsigned int offset,
14771                                   struct objfile *objfile)
14772 {
14773   struct dwarf2_per_cu_data *this_cu;
14774   int low, high;
14775
14776   low = 0;
14777   high = dwarf2_per_objfile->n_comp_units - 1;
14778   while (high > low)
14779     {
14780       int mid = low + (high - low) / 2;
14781
14782       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14783         high = mid;
14784       else
14785         low = mid + 1;
14786     }
14787   gdb_assert (low == high);
14788   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14789     {
14790       if (low == 0)
14791         error (_("Dwarf Error: could not find partial DIE containing "
14792                "offset 0x%lx [in module %s]"),
14793                (long) offset, bfd_get_filename (objfile->obfd));
14794
14795       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14796       return dwarf2_per_objfile->all_comp_units[low-1];
14797     }
14798   else
14799     {
14800       this_cu = dwarf2_per_objfile->all_comp_units[low];
14801       if (low == dwarf2_per_objfile->n_comp_units - 1
14802           && offset >= this_cu->offset + this_cu->length)
14803         error (_("invalid dwarf2 offset %u"), offset);
14804       gdb_assert (offset < this_cu->offset + this_cu->length);
14805       return this_cu;
14806     }
14807 }
14808
14809 /* Locate the compilation unit from OBJFILE which is located at exactly
14810    OFFSET.  Raises an error on failure.  */
14811
14812 static struct dwarf2_per_cu_data *
14813 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14814 {
14815   struct dwarf2_per_cu_data *this_cu;
14816
14817   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14818   if (this_cu->offset != offset)
14819     error (_("no compilation unit with offset %u."), offset);
14820   return this_cu;
14821 }
14822
14823 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14824
14825 static void
14826 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14827 {
14828   memset (cu, 0, sizeof (*cu));
14829   cu->objfile = objfile;
14830   obstack_init (&cu->comp_unit_obstack);
14831 }
14832
14833 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14834
14835 static void
14836 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14837 {
14838   struct attribute *attr;
14839
14840   /* Set the language we're debugging.  */
14841   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14842   if (attr)
14843     set_cu_language (DW_UNSND (attr), cu);
14844   else
14845     {
14846       cu->language = language_minimal;
14847       cu->language_defn = language_def (cu->language);
14848     }
14849 }
14850
14851 /* Release one cached compilation unit, CU.  We unlink it from the tree
14852    of compilation units, but we don't remove it from the read_in_chain;
14853    the caller is responsible for that.
14854    NOTE: DATA is a void * because this function is also used as a
14855    cleanup routine.  */
14856
14857 static void
14858 free_one_comp_unit (void *data)
14859 {
14860   struct dwarf2_cu *cu = data;
14861
14862   if (cu->per_cu != NULL)
14863     cu->per_cu->cu = NULL;
14864   cu->per_cu = NULL;
14865
14866   obstack_free (&cu->comp_unit_obstack, NULL);
14867
14868   xfree (cu);
14869 }
14870
14871 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14872    when we're finished with it.  We can't free the pointer itself, but be
14873    sure to unlink it from the cache.  Also release any associated storage
14874    and perform cache maintenance.
14875
14876    Only used during partial symbol parsing.  */
14877
14878 static void
14879 free_stack_comp_unit (void *data)
14880 {
14881   struct dwarf2_cu *cu = data;
14882
14883   obstack_free (&cu->comp_unit_obstack, NULL);
14884   cu->partial_dies = NULL;
14885
14886   if (cu->per_cu != NULL)
14887     {
14888       /* This compilation unit is on the stack in our caller, so we
14889          should not xfree it.  Just unlink it.  */
14890       cu->per_cu->cu = NULL;
14891       cu->per_cu = NULL;
14892
14893       /* If we had a per-cu pointer, then we may have other compilation
14894          units loaded, so age them now.  */
14895       age_cached_comp_units ();
14896     }
14897 }
14898
14899 /* Free all cached compilation units.  */
14900
14901 static void
14902 free_cached_comp_units (void *data)
14903 {
14904   struct dwarf2_per_cu_data *per_cu, **last_chain;
14905
14906   per_cu = dwarf2_per_objfile->read_in_chain;
14907   last_chain = &dwarf2_per_objfile->read_in_chain;
14908   while (per_cu != NULL)
14909     {
14910       struct dwarf2_per_cu_data *next_cu;
14911
14912       next_cu = per_cu->cu->read_in_chain;
14913
14914       free_one_comp_unit (per_cu->cu);
14915       *last_chain = next_cu;
14916
14917       per_cu = next_cu;
14918     }
14919 }
14920
14921 /* Increase the age counter on each cached compilation unit, and free
14922    any that are too old.  */
14923
14924 static void
14925 age_cached_comp_units (void)
14926 {
14927   struct dwarf2_per_cu_data *per_cu, **last_chain;
14928
14929   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14930   per_cu = dwarf2_per_objfile->read_in_chain;
14931   while (per_cu != NULL)
14932     {
14933       per_cu->cu->last_used ++;
14934       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14935         dwarf2_mark (per_cu->cu);
14936       per_cu = per_cu->cu->read_in_chain;
14937     }
14938
14939   per_cu = dwarf2_per_objfile->read_in_chain;
14940   last_chain = &dwarf2_per_objfile->read_in_chain;
14941   while (per_cu != NULL)
14942     {
14943       struct dwarf2_per_cu_data *next_cu;
14944
14945       next_cu = per_cu->cu->read_in_chain;
14946
14947       if (!per_cu->cu->mark)
14948         {
14949           free_one_comp_unit (per_cu->cu);
14950           *last_chain = next_cu;
14951         }
14952       else
14953         last_chain = &per_cu->cu->read_in_chain;
14954
14955       per_cu = next_cu;
14956     }
14957 }
14958
14959 /* Remove a single compilation unit from the cache.  */
14960
14961 static void
14962 free_one_cached_comp_unit (void *target_cu)
14963 {
14964   struct dwarf2_per_cu_data *per_cu, **last_chain;
14965
14966   per_cu = dwarf2_per_objfile->read_in_chain;
14967   last_chain = &dwarf2_per_objfile->read_in_chain;
14968   while (per_cu != NULL)
14969     {
14970       struct dwarf2_per_cu_data *next_cu;
14971
14972       next_cu = per_cu->cu->read_in_chain;
14973
14974       if (per_cu->cu == target_cu)
14975         {
14976           free_one_comp_unit (per_cu->cu);
14977           *last_chain = next_cu;
14978           break;
14979         }
14980       else
14981         last_chain = &per_cu->cu->read_in_chain;
14982
14983       per_cu = next_cu;
14984     }
14985 }
14986
14987 /* Release all extra memory associated with OBJFILE.  */
14988
14989 void
14990 dwarf2_free_objfile (struct objfile *objfile)
14991 {
14992   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14993
14994   if (dwarf2_per_objfile == NULL)
14995     return;
14996
14997   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14998   free_cached_comp_units (NULL);
14999
15000   if (dwarf2_per_objfile->quick_file_names_table)
15001     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15002
15003   /* Everything else should be on the objfile obstack.  */
15004 }
15005
15006 /* A pair of DIE offset and GDB type pointer.  We store these
15007    in a hash table separate from the DIEs, and preserve them
15008    when the DIEs are flushed out of cache.  */
15009
15010 struct dwarf2_offset_and_type
15011 {
15012   unsigned int offset;
15013   struct type *type;
15014 };
15015
15016 /* Hash function for a dwarf2_offset_and_type.  */
15017
15018 static hashval_t
15019 offset_and_type_hash (const void *item)
15020 {
15021   const struct dwarf2_offset_and_type *ofs = item;
15022
15023   return ofs->offset;
15024 }
15025
15026 /* Equality function for a dwarf2_offset_and_type.  */
15027
15028 static int
15029 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15030 {
15031   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15032   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15033
15034   return ofs_lhs->offset == ofs_rhs->offset;
15035 }
15036
15037 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15038    table if necessary.  For convenience, return TYPE.
15039
15040    The DIEs reading must have careful ordering to:
15041     * Not cause infite loops trying to read in DIEs as a prerequisite for
15042       reading current DIE.
15043     * Not trying to dereference contents of still incompletely read in types
15044       while reading in other DIEs.
15045     * Enable referencing still incompletely read in types just by a pointer to
15046       the type without accessing its fields.
15047
15048    Therefore caller should follow these rules:
15049      * Try to fetch any prerequisite types we may need to build this DIE type
15050        before building the type and calling set_die_type.
15051      * After building type call set_die_type for current DIE as soon as
15052        possible before fetching more types to complete the current type.
15053      * Make the type as complete as possible before fetching more types.  */
15054
15055 static struct type *
15056 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15057 {
15058   struct dwarf2_offset_and_type **slot, ofs;
15059   struct objfile *objfile = cu->objfile;
15060   htab_t *type_hash_ptr;
15061
15062   /* For Ada types, make sure that the gnat-specific data is always
15063      initialized (if not already set).  There are a few types where
15064      we should not be doing so, because the type-specific area is
15065      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15066      where the type-specific area is used to store the floatformat).
15067      But this is not a problem, because the gnat-specific information
15068      is actually not needed for these types.  */
15069   if (need_gnat_info (cu)
15070       && TYPE_CODE (type) != TYPE_CODE_FUNC
15071       && TYPE_CODE (type) != TYPE_CODE_FLT
15072       && !HAVE_GNAT_AUX_INFO (type))
15073     INIT_GNAT_SPECIFIC (type);
15074
15075   if (cu->per_cu->from_debug_types)
15076     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15077   else
15078     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15079
15080   if (*type_hash_ptr == NULL)
15081     {
15082       *type_hash_ptr
15083         = htab_create_alloc_ex (127,
15084                                 offset_and_type_hash,
15085                                 offset_and_type_eq,
15086                                 NULL,
15087                                 &objfile->objfile_obstack,
15088                                 hashtab_obstack_allocate,
15089                                 dummy_obstack_deallocate);
15090     }
15091
15092   ofs.offset = die->offset;
15093   ofs.type = type;
15094   slot = (struct dwarf2_offset_and_type **)
15095     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15096   if (*slot)
15097     complaint (&symfile_complaints,
15098                _("A problem internal to GDB: DIE 0x%x has type already set"),
15099                die->offset);
15100   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15101   **slot = ofs;
15102   return type;
15103 }
15104
15105 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15106    table, or return NULL if the die does not have a saved type.  */
15107
15108 static struct type *
15109 get_die_type_at_offset (unsigned int offset,
15110                         struct dwarf2_per_cu_data *per_cu)
15111 {
15112   struct dwarf2_offset_and_type *slot, ofs;
15113   htab_t type_hash;
15114
15115   if (per_cu->from_debug_types)
15116     type_hash = dwarf2_per_objfile->debug_types_type_hash;
15117   else
15118     type_hash = dwarf2_per_objfile->debug_info_type_hash;
15119   if (type_hash == NULL)
15120     return NULL;
15121
15122   ofs.offset = offset;
15123   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15124   if (slot)
15125     return slot->type;
15126   else
15127     return NULL;
15128 }
15129
15130 /* Look up the type for DIE in the appropriate type_hash table,
15131    or return NULL if DIE does not have a saved type.  */
15132
15133 static struct type *
15134 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15135 {
15136   return get_die_type_at_offset (die->offset, cu->per_cu);
15137 }
15138
15139 /* Add a dependence relationship from CU to REF_PER_CU.  */
15140
15141 static void
15142 dwarf2_add_dependence (struct dwarf2_cu *cu,
15143                        struct dwarf2_per_cu_data *ref_per_cu)
15144 {
15145   void **slot;
15146
15147   if (cu->dependencies == NULL)
15148     cu->dependencies
15149       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15150                               NULL, &cu->comp_unit_obstack,
15151                               hashtab_obstack_allocate,
15152                               dummy_obstack_deallocate);
15153
15154   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15155   if (*slot == NULL)
15156     *slot = ref_per_cu;
15157 }
15158
15159 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15160    Set the mark field in every compilation unit in the
15161    cache that we must keep because we are keeping CU.  */
15162
15163 static int
15164 dwarf2_mark_helper (void **slot, void *data)
15165 {
15166   struct dwarf2_per_cu_data *per_cu;
15167
15168   per_cu = (struct dwarf2_per_cu_data *) *slot;
15169   if (per_cu->cu->mark)
15170     return 1;
15171   per_cu->cu->mark = 1;
15172
15173   if (per_cu->cu->dependencies != NULL)
15174     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15175
15176   return 1;
15177 }
15178
15179 /* Set the mark field in CU and in every other compilation unit in the
15180    cache that we must keep because we are keeping CU.  */
15181
15182 static void
15183 dwarf2_mark (struct dwarf2_cu *cu)
15184 {
15185   if (cu->mark)
15186     return;
15187   cu->mark = 1;
15188   if (cu->dependencies != NULL)
15189     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15190 }
15191
15192 static void
15193 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15194 {
15195   while (per_cu)
15196     {
15197       per_cu->cu->mark = 0;
15198       per_cu = per_cu->cu->read_in_chain;
15199     }
15200 }
15201
15202 /* Trivial hash function for partial_die_info: the hash value of a DIE
15203    is its offset in .debug_info for this objfile.  */
15204
15205 static hashval_t
15206 partial_die_hash (const void *item)
15207 {
15208   const struct partial_die_info *part_die = item;
15209
15210   return part_die->offset;
15211 }
15212
15213 /* Trivial comparison function for partial_die_info structures: two DIEs
15214    are equal if they have the same offset.  */
15215
15216 static int
15217 partial_die_eq (const void *item_lhs, const void *item_rhs)
15218 {
15219   const struct partial_die_info *part_die_lhs = item_lhs;
15220   const struct partial_die_info *part_die_rhs = item_rhs;
15221
15222   return part_die_lhs->offset == part_die_rhs->offset;
15223 }
15224
15225 static struct cmd_list_element *set_dwarf2_cmdlist;
15226 static struct cmd_list_element *show_dwarf2_cmdlist;
15227
15228 static void
15229 set_dwarf2_cmd (char *args, int from_tty)
15230 {
15231   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15232 }
15233
15234 static void
15235 show_dwarf2_cmd (char *args, int from_tty)
15236 {
15237   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15238 }
15239
15240 /* If section described by INFO was mmapped, munmap it now.  */
15241
15242 static void
15243 munmap_section_buffer (struct dwarf2_section_info *info)
15244 {
15245   if (info->was_mmapped)
15246     {
15247 #ifdef HAVE_MMAP
15248       intptr_t begin = (intptr_t) info->buffer;
15249       intptr_t map_begin = begin & ~(pagesize - 1);
15250       size_t map_length = info->size + begin - map_begin;
15251
15252       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15253 #else
15254       /* Without HAVE_MMAP, we should never be here to begin with.  */
15255       gdb_assert_not_reached ("no mmap support");
15256 #endif
15257     }
15258 }
15259
15260 /* munmap debug sections for OBJFILE, if necessary.  */
15261
15262 static void
15263 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15264 {
15265   struct dwarf2_per_objfile *data = d;
15266
15267   /* This is sorted according to the order they're defined in to make it easier
15268      to keep in sync.  */
15269   munmap_section_buffer (&data->info);
15270   munmap_section_buffer (&data->abbrev);
15271   munmap_section_buffer (&data->line);
15272   munmap_section_buffer (&data->loc);
15273   munmap_section_buffer (&data->macinfo);
15274   munmap_section_buffer (&data->str);
15275   munmap_section_buffer (&data->ranges);
15276   munmap_section_buffer (&data->types);
15277   munmap_section_buffer (&data->frame);
15278   munmap_section_buffer (&data->eh_frame);
15279   munmap_section_buffer (&data->gdb_index);
15280 }
15281
15282 \f
15283 /* The "save gdb-index" command.  */
15284
15285 /* The contents of the hash table we create when building the string
15286    table.  */
15287 struct strtab_entry
15288 {
15289   offset_type offset;
15290   const char *str;
15291 };
15292
15293 /* Hash function for a strtab_entry.
15294
15295    Function is used only during write_hash_table so no index format backward
15296    compatibility is needed.  */
15297
15298 static hashval_t
15299 hash_strtab_entry (const void *e)
15300 {
15301   const struct strtab_entry *entry = e;
15302   return mapped_index_string_hash (INT_MAX, entry->str);
15303 }
15304
15305 /* Equality function for a strtab_entry.  */
15306
15307 static int
15308 eq_strtab_entry (const void *a, const void *b)
15309 {
15310   const struct strtab_entry *ea = a;
15311   const struct strtab_entry *eb = b;
15312   return !strcmp (ea->str, eb->str);
15313 }
15314
15315 /* Create a strtab_entry hash table.  */
15316
15317 static htab_t
15318 create_strtab (void)
15319 {
15320   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15321                             xfree, xcalloc, xfree);
15322 }
15323
15324 /* Add a string to the constant pool.  Return the string's offset in
15325    host order.  */
15326
15327 static offset_type
15328 add_string (htab_t table, struct obstack *cpool, const char *str)
15329 {
15330   void **slot;
15331   struct strtab_entry entry;
15332   struct strtab_entry *result;
15333
15334   entry.str = str;
15335   slot = htab_find_slot (table, &entry, INSERT);
15336   if (*slot)
15337     result = *slot;
15338   else
15339     {
15340       result = XNEW (struct strtab_entry);
15341       result->offset = obstack_object_size (cpool);
15342       result->str = str;
15343       obstack_grow_str0 (cpool, str);
15344       *slot = result;
15345     }
15346   return result->offset;
15347 }
15348
15349 /* An entry in the symbol table.  */
15350 struct symtab_index_entry
15351 {
15352   /* The name of the symbol.  */
15353   const char *name;
15354   /* The offset of the name in the constant pool.  */
15355   offset_type index_offset;
15356   /* A sorted vector of the indices of all the CUs that hold an object
15357      of this name.  */
15358   VEC (offset_type) *cu_indices;
15359 };
15360
15361 /* The symbol table.  This is a power-of-2-sized hash table.  */
15362 struct mapped_symtab
15363 {
15364   offset_type n_elements;
15365   offset_type size;
15366   struct symtab_index_entry **data;
15367 };
15368
15369 /* Hash function for a symtab_index_entry.  */
15370
15371 static hashval_t
15372 hash_symtab_entry (const void *e)
15373 {
15374   const struct symtab_index_entry *entry = e;
15375   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15376                          sizeof (offset_type) * VEC_length (offset_type,
15377                                                             entry->cu_indices),
15378                          0);
15379 }
15380
15381 /* Equality function for a symtab_index_entry.  */
15382
15383 static int
15384 eq_symtab_entry (const void *a, const void *b)
15385 {
15386   const struct symtab_index_entry *ea = a;
15387   const struct symtab_index_entry *eb = b;
15388   int len = VEC_length (offset_type, ea->cu_indices);
15389   if (len != VEC_length (offset_type, eb->cu_indices))
15390     return 0;
15391   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15392                   VEC_address (offset_type, eb->cu_indices),
15393                   sizeof (offset_type) * len);
15394 }
15395
15396 /* Destroy a symtab_index_entry.  */
15397
15398 static void
15399 delete_symtab_entry (void *p)
15400 {
15401   struct symtab_index_entry *entry = p;
15402   VEC_free (offset_type, entry->cu_indices);
15403   xfree (entry);
15404 }
15405
15406 /* Create a hash table holding symtab_index_entry objects.  */
15407
15408 static htab_t
15409 create_symbol_hash_table (void)
15410 {
15411   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15412                             delete_symtab_entry, xcalloc, xfree);
15413 }
15414
15415 /* Create a new mapped symtab object.  */
15416
15417 static struct mapped_symtab *
15418 create_mapped_symtab (void)
15419 {
15420   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15421   symtab->n_elements = 0;
15422   symtab->size = 1024;
15423   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15424   return symtab;
15425 }
15426
15427 /* Destroy a mapped_symtab.  */
15428
15429 static void
15430 cleanup_mapped_symtab (void *p)
15431 {
15432   struct mapped_symtab *symtab = p;
15433   /* The contents of the array are freed when the other hash table is
15434      destroyed.  */
15435   xfree (symtab->data);
15436   xfree (symtab);
15437 }
15438
15439 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15440    the slot.
15441    
15442    Function is used only during write_hash_table so no index format backward
15443    compatibility is needed.  */
15444
15445 static struct symtab_index_entry **
15446 find_slot (struct mapped_symtab *symtab, const char *name)
15447 {
15448   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15449
15450   index = hash & (symtab->size - 1);
15451   step = ((hash * 17) & (symtab->size - 1)) | 1;
15452
15453   for (;;)
15454     {
15455       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15456         return &symtab->data[index];
15457       index = (index + step) & (symtab->size - 1);
15458     }
15459 }
15460
15461 /* Expand SYMTAB's hash table.  */
15462
15463 static void
15464 hash_expand (struct mapped_symtab *symtab)
15465 {
15466   offset_type old_size = symtab->size;
15467   offset_type i;
15468   struct symtab_index_entry **old_entries = symtab->data;
15469
15470   symtab->size *= 2;
15471   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15472
15473   for (i = 0; i < old_size; ++i)
15474     {
15475       if (old_entries[i])
15476         {
15477           struct symtab_index_entry **slot = find_slot (symtab,
15478                                                         old_entries[i]->name);
15479           *slot = old_entries[i];
15480         }
15481     }
15482
15483   xfree (old_entries);
15484 }
15485
15486 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15487    is the index of the CU in which the symbol appears.  */
15488
15489 static void
15490 add_index_entry (struct mapped_symtab *symtab, const char *name,
15491                  offset_type cu_index)
15492 {
15493   struct symtab_index_entry **slot;
15494
15495   ++symtab->n_elements;
15496   if (4 * symtab->n_elements / 3 >= symtab->size)
15497     hash_expand (symtab);
15498
15499   slot = find_slot (symtab, name);
15500   if (!*slot)
15501     {
15502       *slot = XNEW (struct symtab_index_entry);
15503       (*slot)->name = name;
15504       (*slot)->cu_indices = NULL;
15505     }
15506   /* Don't push an index twice.  Due to how we add entries we only
15507      have to check the last one.  */ 
15508   if (VEC_empty (offset_type, (*slot)->cu_indices)
15509       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15510     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15511 }
15512
15513 /* Add a vector of indices to the constant pool.  */
15514
15515 static offset_type
15516 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15517                       struct symtab_index_entry *entry)
15518 {
15519   void **slot;
15520
15521   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15522   if (!*slot)
15523     {
15524       offset_type len = VEC_length (offset_type, entry->cu_indices);
15525       offset_type val = MAYBE_SWAP (len);
15526       offset_type iter;
15527       int i;
15528
15529       *slot = entry;
15530       entry->index_offset = obstack_object_size (cpool);
15531
15532       obstack_grow (cpool, &val, sizeof (val));
15533       for (i = 0;
15534            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15535            ++i)
15536         {
15537           val = MAYBE_SWAP (iter);
15538           obstack_grow (cpool, &val, sizeof (val));
15539         }
15540     }
15541   else
15542     {
15543       struct symtab_index_entry *old_entry = *slot;
15544       entry->index_offset = old_entry->index_offset;
15545       entry = old_entry;
15546     }
15547   return entry->index_offset;
15548 }
15549
15550 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15551    constant pool entries going into the obstack CPOOL.  */
15552
15553 static void
15554 write_hash_table (struct mapped_symtab *symtab,
15555                   struct obstack *output, struct obstack *cpool)
15556 {
15557   offset_type i;
15558   htab_t symbol_hash_table;
15559   htab_t str_table;
15560
15561   symbol_hash_table = create_symbol_hash_table ();
15562   str_table = create_strtab ();
15563
15564   /* We add all the index vectors to the constant pool first, to
15565      ensure alignment is ok.  */
15566   for (i = 0; i < symtab->size; ++i)
15567     {
15568       if (symtab->data[i])
15569         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15570     }
15571
15572   /* Now write out the hash table.  */
15573   for (i = 0; i < symtab->size; ++i)
15574     {
15575       offset_type str_off, vec_off;
15576
15577       if (symtab->data[i])
15578         {
15579           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15580           vec_off = symtab->data[i]->index_offset;
15581         }
15582       else
15583         {
15584           /* While 0 is a valid constant pool index, it is not valid
15585              to have 0 for both offsets.  */
15586           str_off = 0;
15587           vec_off = 0;
15588         }
15589
15590       str_off = MAYBE_SWAP (str_off);
15591       vec_off = MAYBE_SWAP (vec_off);
15592
15593       obstack_grow (output, &str_off, sizeof (str_off));
15594       obstack_grow (output, &vec_off, sizeof (vec_off));
15595     }
15596
15597   htab_delete (str_table);
15598   htab_delete (symbol_hash_table);
15599 }
15600
15601 /* Struct to map psymtab to CU index in the index file.  */
15602 struct psymtab_cu_index_map
15603 {
15604   struct partial_symtab *psymtab;
15605   unsigned int cu_index;
15606 };
15607
15608 static hashval_t
15609 hash_psymtab_cu_index (const void *item)
15610 {
15611   const struct psymtab_cu_index_map *map = item;
15612
15613   return htab_hash_pointer (map->psymtab);
15614 }
15615
15616 static int
15617 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15618 {
15619   const struct psymtab_cu_index_map *lhs = item_lhs;
15620   const struct psymtab_cu_index_map *rhs = item_rhs;
15621
15622   return lhs->psymtab == rhs->psymtab;
15623 }
15624
15625 /* Helper struct for building the address table.  */
15626 struct addrmap_index_data
15627 {
15628   struct objfile *objfile;
15629   struct obstack *addr_obstack;
15630   htab_t cu_index_htab;
15631
15632   /* Non-zero if the previous_* fields are valid.
15633      We can't write an entry until we see the next entry (since it is only then
15634      that we know the end of the entry).  */
15635   int previous_valid;
15636   /* Index of the CU in the table of all CUs in the index file.  */
15637   unsigned int previous_cu_index;
15638   /* Start address of the CU.  */
15639   CORE_ADDR previous_cu_start;
15640 };
15641
15642 /* Write an address entry to OBSTACK.  */
15643
15644 static void
15645 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15646                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15647 {
15648   offset_type cu_index_to_write;
15649   char addr[8];
15650   CORE_ADDR baseaddr;
15651
15652   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15653
15654   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15655   obstack_grow (obstack, addr, 8);
15656   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15657   obstack_grow (obstack, addr, 8);
15658   cu_index_to_write = MAYBE_SWAP (cu_index);
15659   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15660 }
15661
15662 /* Worker function for traversing an addrmap to build the address table.  */
15663
15664 static int
15665 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15666 {
15667   struct addrmap_index_data *data = datap;
15668   struct partial_symtab *pst = obj;
15669   offset_type cu_index;
15670   void **slot;
15671
15672   if (data->previous_valid)
15673     add_address_entry (data->objfile, data->addr_obstack,
15674                        data->previous_cu_start, start_addr,
15675                        data->previous_cu_index);
15676
15677   data->previous_cu_start = start_addr;
15678   if (pst != NULL)
15679     {
15680       struct psymtab_cu_index_map find_map, *map;
15681       find_map.psymtab = pst;
15682       map = htab_find (data->cu_index_htab, &find_map);
15683       gdb_assert (map != NULL);
15684       data->previous_cu_index = map->cu_index;
15685       data->previous_valid = 1;
15686     }
15687   else
15688       data->previous_valid = 0;
15689
15690   return 0;
15691 }
15692
15693 /* Write OBJFILE's address map to OBSTACK.
15694    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15695    in the index file.  */
15696
15697 static void
15698 write_address_map (struct objfile *objfile, struct obstack *obstack,
15699                    htab_t cu_index_htab)
15700 {
15701   struct addrmap_index_data addrmap_index_data;
15702
15703   /* When writing the address table, we have to cope with the fact that
15704      the addrmap iterator only provides the start of a region; we have to
15705      wait until the next invocation to get the start of the next region.  */
15706
15707   addrmap_index_data.objfile = objfile;
15708   addrmap_index_data.addr_obstack = obstack;
15709   addrmap_index_data.cu_index_htab = cu_index_htab;
15710   addrmap_index_data.previous_valid = 0;
15711
15712   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15713                    &addrmap_index_data);
15714
15715   /* It's highly unlikely the last entry (end address = 0xff...ff)
15716      is valid, but we should still handle it.
15717      The end address is recorded as the start of the next region, but that
15718      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15719      anyway.  */
15720   if (addrmap_index_data.previous_valid)
15721     add_address_entry (objfile, obstack,
15722                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15723                        addrmap_index_data.previous_cu_index);
15724 }
15725
15726 /* Add a list of partial symbols to SYMTAB.  */
15727
15728 static void
15729 write_psymbols (struct mapped_symtab *symtab,
15730                 htab_t psyms_seen,
15731                 struct partial_symbol **psymp,
15732                 int count,
15733                 offset_type cu_index,
15734                 int is_static)
15735 {
15736   for (; count-- > 0; ++psymp)
15737     {
15738       void **slot, *lookup;
15739
15740       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15741         error (_("Ada is not currently supported by the index"));
15742
15743       /* We only want to add a given psymbol once.  However, we also
15744          want to account for whether it is global or static.  So, we
15745          may add it twice, using slightly different values.  */
15746       if (is_static)
15747         {
15748           uintptr_t val = 1 | (uintptr_t) *psymp;
15749
15750           lookup = (void *) val;
15751         }
15752       else
15753         lookup = *psymp;
15754
15755       /* Only add a given psymbol once.  */
15756       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15757       if (!*slot)
15758         {
15759           *slot = lookup;
15760           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15761         }
15762     }
15763 }
15764
15765 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15766    exception if there is an error.  */
15767
15768 static void
15769 write_obstack (FILE *file, struct obstack *obstack)
15770 {
15771   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15772               file)
15773       != obstack_object_size (obstack))
15774     error (_("couldn't data write to file"));
15775 }
15776
15777 /* Unlink a file if the argument is not NULL.  */
15778
15779 static void
15780 unlink_if_set (void *p)
15781 {
15782   char **filename = p;
15783   if (*filename)
15784     unlink (*filename);
15785 }
15786
15787 /* A helper struct used when iterating over debug_types.  */
15788 struct signatured_type_index_data
15789 {
15790   struct objfile *objfile;
15791   struct mapped_symtab *symtab;
15792   struct obstack *types_list;
15793   htab_t psyms_seen;
15794   int cu_index;
15795 };
15796
15797 /* A helper function that writes a single signatured_type to an
15798    obstack.  */
15799
15800 static int
15801 write_one_signatured_type (void **slot, void *d)
15802 {
15803   struct signatured_type_index_data *info = d;
15804   struct signatured_type *entry = (struct signatured_type *) *slot;
15805   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15806   struct partial_symtab *psymtab = per_cu->v.psymtab;
15807   gdb_byte val[8];
15808
15809   write_psymbols (info->symtab,
15810                   info->psyms_seen,
15811                   info->objfile->global_psymbols.list
15812                   + psymtab->globals_offset,
15813                   psymtab->n_global_syms, info->cu_index,
15814                   0);
15815   write_psymbols (info->symtab,
15816                   info->psyms_seen,
15817                   info->objfile->static_psymbols.list
15818                   + psymtab->statics_offset,
15819                   psymtab->n_static_syms, info->cu_index,
15820                   1);
15821
15822   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15823   obstack_grow (info->types_list, val, 8);
15824   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15825   obstack_grow (info->types_list, val, 8);
15826   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15827   obstack_grow (info->types_list, val, 8);
15828
15829   ++info->cu_index;
15830
15831   return 1;
15832 }
15833
15834 /* A cleanup function for an htab_t.  */
15835
15836 static void
15837 cleanup_htab (void *arg)
15838 {
15839   htab_delete (arg);
15840 }
15841
15842 /* Create an index file for OBJFILE in the directory DIR.  */
15843
15844 static void
15845 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15846 {
15847   struct cleanup *cleanup;
15848   char *filename, *cleanup_filename;
15849   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15850   struct obstack cu_list, types_cu_list;
15851   int i;
15852   FILE *out_file;
15853   struct mapped_symtab *symtab;
15854   offset_type val, size_of_contents, total_len;
15855   struct stat st;
15856   char buf[8];
15857   htab_t psyms_seen;
15858   htab_t cu_index_htab;
15859   struct psymtab_cu_index_map *psymtab_cu_index_map;
15860
15861   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15862     return;
15863
15864   if (dwarf2_per_objfile->using_index)
15865     error (_("Cannot use an index to create the index"));
15866
15867   if (stat (objfile->name, &st) < 0)
15868     perror_with_name (objfile->name);
15869
15870   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15871                      INDEX_SUFFIX, (char *) NULL);
15872   cleanup = make_cleanup (xfree, filename);
15873
15874   out_file = fopen (filename, "wb");
15875   if (!out_file)
15876     error (_("Can't open `%s' for writing"), filename);
15877
15878   cleanup_filename = filename;
15879   make_cleanup (unlink_if_set, &cleanup_filename);
15880
15881   symtab = create_mapped_symtab ();
15882   make_cleanup (cleanup_mapped_symtab, symtab);
15883
15884   obstack_init (&addr_obstack);
15885   make_cleanup_obstack_free (&addr_obstack);
15886
15887   obstack_init (&cu_list);
15888   make_cleanup_obstack_free (&cu_list);
15889
15890   obstack_init (&types_cu_list);
15891   make_cleanup_obstack_free (&types_cu_list);
15892
15893   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15894                                   NULL, xcalloc, xfree);
15895   make_cleanup (cleanup_htab, psyms_seen);
15896
15897   /* While we're scanning CU's create a table that maps a psymtab pointer
15898      (which is what addrmap records) to its index (which is what is recorded
15899      in the index file).  This will later be needed to write the address
15900      table.  */
15901   cu_index_htab = htab_create_alloc (100,
15902                                      hash_psymtab_cu_index,
15903                                      eq_psymtab_cu_index,
15904                                      NULL, xcalloc, xfree);
15905   make_cleanup (cleanup_htab, cu_index_htab);
15906   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15907     xmalloc (sizeof (struct psymtab_cu_index_map)
15908              * dwarf2_per_objfile->n_comp_units);
15909   make_cleanup (xfree, psymtab_cu_index_map);
15910
15911   /* The CU list is already sorted, so we don't need to do additional
15912      work here.  Also, the debug_types entries do not appear in
15913      all_comp_units, but only in their own hash table.  */
15914   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15915     {
15916       struct dwarf2_per_cu_data *per_cu
15917         = dwarf2_per_objfile->all_comp_units[i];
15918       struct partial_symtab *psymtab = per_cu->v.psymtab;
15919       gdb_byte val[8];
15920       struct psymtab_cu_index_map *map;
15921       void **slot;
15922
15923       write_psymbols (symtab,
15924                       psyms_seen,
15925                       objfile->global_psymbols.list + psymtab->globals_offset,
15926                       psymtab->n_global_syms, i,
15927                       0);
15928       write_psymbols (symtab,
15929                       psyms_seen,
15930                       objfile->static_psymbols.list + psymtab->statics_offset,
15931                       psymtab->n_static_syms, i,
15932                       1);
15933
15934       map = &psymtab_cu_index_map[i];
15935       map->psymtab = psymtab;
15936       map->cu_index = i;
15937       slot = htab_find_slot (cu_index_htab, map, INSERT);
15938       gdb_assert (slot != NULL);
15939       gdb_assert (*slot == NULL);
15940       *slot = map;
15941
15942       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15943       obstack_grow (&cu_list, val, 8);
15944       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15945       obstack_grow (&cu_list, val, 8);
15946     }
15947
15948   /* Dump the address map.  */
15949   write_address_map (objfile, &addr_obstack, cu_index_htab);
15950
15951   /* Write out the .debug_type entries, if any.  */
15952   if (dwarf2_per_objfile->signatured_types)
15953     {
15954       struct signatured_type_index_data sig_data;
15955
15956       sig_data.objfile = objfile;
15957       sig_data.symtab = symtab;
15958       sig_data.types_list = &types_cu_list;
15959       sig_data.psyms_seen = psyms_seen;
15960       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15961       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15962                               write_one_signatured_type, &sig_data);
15963     }
15964
15965   obstack_init (&constant_pool);
15966   make_cleanup_obstack_free (&constant_pool);
15967   obstack_init (&symtab_obstack);
15968   make_cleanup_obstack_free (&symtab_obstack);
15969   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15970
15971   obstack_init (&contents);
15972   make_cleanup_obstack_free (&contents);
15973   size_of_contents = 6 * sizeof (offset_type);
15974   total_len = size_of_contents;
15975
15976   /* The version number.  */
15977   val = MAYBE_SWAP (5);
15978   obstack_grow (&contents, &val, sizeof (val));
15979
15980   /* The offset of the CU list from the start of the file.  */
15981   val = MAYBE_SWAP (total_len);
15982   obstack_grow (&contents, &val, sizeof (val));
15983   total_len += obstack_object_size (&cu_list);
15984
15985   /* The offset of the types CU list from the start of the file.  */
15986   val = MAYBE_SWAP (total_len);
15987   obstack_grow (&contents, &val, sizeof (val));
15988   total_len += obstack_object_size (&types_cu_list);
15989
15990   /* The offset of the address table from the start of the file.  */
15991   val = MAYBE_SWAP (total_len);
15992   obstack_grow (&contents, &val, sizeof (val));
15993   total_len += obstack_object_size (&addr_obstack);
15994
15995   /* The offset of the symbol table from the start of the file.  */
15996   val = MAYBE_SWAP (total_len);
15997   obstack_grow (&contents, &val, sizeof (val));
15998   total_len += obstack_object_size (&symtab_obstack);
15999
16000   /* The offset of the constant pool from the start of the file.  */
16001   val = MAYBE_SWAP (total_len);
16002   obstack_grow (&contents, &val, sizeof (val));
16003   total_len += obstack_object_size (&constant_pool);
16004
16005   gdb_assert (obstack_object_size (&contents) == size_of_contents);
16006
16007   write_obstack (out_file, &contents);
16008   write_obstack (out_file, &cu_list);
16009   write_obstack (out_file, &types_cu_list);
16010   write_obstack (out_file, &addr_obstack);
16011   write_obstack (out_file, &symtab_obstack);
16012   write_obstack (out_file, &constant_pool);
16013
16014   fclose (out_file);
16015
16016   /* We want to keep the file, so we set cleanup_filename to NULL
16017      here.  See unlink_if_set.  */
16018   cleanup_filename = NULL;
16019
16020   do_cleanups (cleanup);
16021 }
16022
16023 /* Implementation of the `save gdb-index' command.
16024    
16025    Note that the file format used by this command is documented in the
16026    GDB manual.  Any changes here must be documented there.  */
16027
16028 static void
16029 save_gdb_index_command (char *arg, int from_tty)
16030 {
16031   struct objfile *objfile;
16032
16033   if (!arg || !*arg)
16034     error (_("usage: save gdb-index DIRECTORY"));
16035
16036   ALL_OBJFILES (objfile)
16037   {
16038     struct stat st;
16039
16040     /* If the objfile does not correspond to an actual file, skip it.  */
16041     if (stat (objfile->name, &st) < 0)
16042       continue;
16043
16044     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16045     if (dwarf2_per_objfile)
16046       {
16047         volatile struct gdb_exception except;
16048
16049         TRY_CATCH (except, RETURN_MASK_ERROR)
16050           {
16051             write_psymtabs_to_index (objfile, arg);
16052           }
16053         if (except.reason < 0)
16054           exception_fprintf (gdb_stderr, except,
16055                              _("Error while writing index for `%s': "),
16056                              objfile->name);
16057       }
16058   }
16059 }
16060
16061 \f
16062
16063 int dwarf2_always_disassemble;
16064
16065 static void
16066 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16067                                 struct cmd_list_element *c, const char *value)
16068 {
16069   fprintf_filtered (file,
16070                     _("Whether to always disassemble "
16071                       "DWARF expressions is %s.\n"),
16072                     value);
16073 }
16074
16075 void _initialize_dwarf2_read (void);
16076
16077 void
16078 _initialize_dwarf2_read (void)
16079 {
16080   struct cmd_list_element *c;
16081
16082   dwarf2_objfile_data_key
16083     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16084
16085   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16086 Set DWARF 2 specific variables.\n\
16087 Configure DWARF 2 variables such as the cache size"),
16088                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16089                   0/*allow-unknown*/, &maintenance_set_cmdlist);
16090
16091   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16092 Show DWARF 2 specific variables\n\
16093 Show DWARF 2 variables such as the cache size"),
16094                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16095                   0/*allow-unknown*/, &maintenance_show_cmdlist);
16096
16097   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16098                             &dwarf2_max_cache_age, _("\
16099 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16100 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16101 A higher limit means that cached compilation units will be stored\n\
16102 in memory longer, and more total memory will be used.  Zero disables\n\
16103 caching, which can slow down startup."),
16104                             NULL,
16105                             show_dwarf2_max_cache_age,
16106                             &set_dwarf2_cmdlist,
16107                             &show_dwarf2_cmdlist);
16108
16109   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16110                            &dwarf2_always_disassemble, _("\
16111 Set whether `info address' always disassembles DWARF expressions."), _("\
16112 Show whether `info address' always disassembles DWARF expressions."), _("\
16113 When enabled, DWARF expressions are always printed in an assembly-like\n\
16114 syntax.  When disabled, expressions will be printed in a more\n\
16115 conversational style, when possible."),
16116                            NULL,
16117                            show_dwarf2_always_disassemble,
16118                            &set_dwarf2_cmdlist,
16119                            &show_dwarf2_cmdlist);
16120
16121   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16122 Set debugging of the dwarf2 DIE reader."), _("\
16123 Show debugging of the dwarf2 DIE reader."), _("\
16124 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16125 The value is the maximum depth to print."),
16126                             NULL,
16127                             NULL,
16128                             &setdebuglist, &showdebuglist);
16129
16130   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16131                _("\
16132 Save a gdb-index file.\n\
16133 Usage: save gdb-index DIRECTORY"),
16134                &save_cmdlist);
16135   set_cmd_completer (c, filename_completer);
16136 }