OSDN Git Service

* xcoffread.c (xcoff_sym_fns): Update.
[pf3gnuchains/sourceware.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 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
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80    Because of alignment constraints, this structure has padding and cannot
81    be mapped directly onto the beginning of the .debug_info section.  */
82 typedef struct comp_unit_header
83   {
84     unsigned int length;        /* length of the .debug_info
85                                    contribution */
86     unsigned short version;     /* version number -- 2 for DWARF
87                                    version 2 */
88     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89     unsigned char addr_size;    /* byte size of an address -- 4 */
90   }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96    Because of alignment constraints, this structure has padding and cannot
97    be mapped directly onto the beginning of the .debug_info section.  */
98 typedef struct statement_prologue
99   {
100     unsigned int total_length;  /* byte length of the statement
101                                    information */
102     unsigned short version;     /* version number -- 2 for DWARF
103                                    version 2 */
104     unsigned int prologue_length;       /* # bytes between prologue &
105                                            stmt program */
106     unsigned char minimum_instruction_length;   /* byte size of
107                                                    smallest instr */
108     unsigned char default_is_stmt;      /* initial value of is_stmt
109                                            register */
110     char line_base;
111     unsigned char line_range;
112     unsigned char opcode_base;  /* number assigned to first special
113                                    opcode */
114     unsigned char *standard_opcode_lengths;
115   }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in.  */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124    info for C++ namespaces.  GCC 3.3.x did not produce this information,
125    but later versions do.  */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133   asection *asection;
134   gdb_byte *buffer;
135   bfd_size_type size;
136   int was_mmapped;
137   /* True if we have tried to read this section.  */
138   int readin;
139 };
140
141 /* All offsets in the index are of this type.  It must be
142    architecture-independent.  */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index.  The file format is described in
148    a comment by the code that writes the index.  */
149 struct mapped_index
150 {
151   /* The total length of the buffer.  */
152   off_t total_size;
153   /* A pointer to the address table data.  */
154   const gdb_byte *address_table;
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157   /* The symbol table, implemented as a hash table.  */
158   const offset_type *symbol_table;
159   /* Size in slots, each slot is 2 offset_types.  */
160   offset_type symbol_table_slots;
161   /* A pointer to the constant pool.  */
162   const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167   struct dwarf2_section_info info;
168   struct dwarf2_section_info abbrev;
169   struct dwarf2_section_info line;
170   struct dwarf2_section_info loc;
171   struct dwarf2_section_info macinfo;
172   struct dwarf2_section_info str;
173   struct dwarf2_section_info ranges;
174   struct dwarf2_section_info types;
175   struct dwarf2_section_info frame;
176   struct dwarf2_section_info eh_frame;
177   struct dwarf2_section_info gdb_index;
178
179   /* Back link.  */
180   struct objfile *objfile;
181
182   /* A list of all the compilation units.  This is used to locate
183      the target compilation unit of a particular reference.  */
184   struct dwarf2_per_cu_data **all_comp_units;
185
186   /* The number of compilation units in ALL_COMP_UNITS.  */
187   int n_comp_units;
188
189   /* The number of .debug_types-related CUs.  */
190   int n_type_comp_units;
191
192   /* The .debug_types-related CUs.  */
193   struct dwarf2_per_cu_data **type_comp_units;
194
195   /* A chain of compilation units that are currently read in, so that
196      they can be freed later.  */
197   struct dwarf2_per_cu_data *read_in_chain;
198
199   /* A table mapping .debug_types signatures to its signatured_type entry.
200      This is NULL if the .debug_types section hasn't been read in yet.  */
201   htab_t signatured_types;
202
203   /* A flag indicating wether this objfile has a section loaded at a
204      VMA of 0.  */
205   int has_section_at_zero;
206
207   /* True if we are using the mapped index,
208      or we are faking it for OBJF_READNOW's sake.  */
209   unsigned char using_index;
210
211   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
212   struct mapped_index *index_table;
213
214   /* When using index_table, this keeps track of all quick_file_names entries.
215      TUs can share line table entries with CUs or other TUs, and there can be
216      a lot more TUs than unique line tables, so we maintain a separate table
217      of all line table entries to support the sharing.  */
218   htab_t quick_file_names_table;
219
220   /* Set during partial symbol reading, to prevent queueing of full
221      symbols.  */
222   int reading_partial_symbols;
223
224   /* Table mapping type .debug_info DIE offsets to types.
225      This is NULL if not allocated yet.
226      It (currently) makes sense to allocate debug_types_type_hash lazily.
227      To keep things simple we allocate both lazily.  */
228   htab_t debug_info_type_hash;
229
230   /* Table mapping type .debug_types DIE offsets to types.
231      This is NULL if not allocated yet.  */
232   htab_t debug_types_type_hash;
233 };
234
235 static struct dwarf2_per_objfile *dwarf2_per_objfile;
236
237 /* names of the debugging sections */
238
239 /* Note that if the debugging section has been compressed, it might
240    have a name like .zdebug_info.  */
241
242 #define INFO_SECTION     "debug_info"
243 #define ABBREV_SECTION   "debug_abbrev"
244 #define LINE_SECTION     "debug_line"
245 #define LOC_SECTION      "debug_loc"
246 #define MACINFO_SECTION  "debug_macinfo"
247 #define STR_SECTION      "debug_str"
248 #define RANGES_SECTION   "debug_ranges"
249 #define TYPES_SECTION    "debug_types"
250 #define FRAME_SECTION    "debug_frame"
251 #define EH_FRAME_SECTION "eh_frame"
252 #define GDB_INDEX_SECTION "gdb_index"
253
254 /* local data types */
255
256 /* We hold several abbreviation tables in memory at the same time.  */
257 #ifndef ABBREV_HASH_SIZE
258 #define ABBREV_HASH_SIZE 121
259 #endif
260
261 /* The data in a compilation unit header, after target2host
262    translation, looks like this.  */
263 struct comp_unit_head
264 {
265   unsigned int length;
266   short version;
267   unsigned char addr_size;
268   unsigned char signed_addr_p;
269   unsigned int abbrev_offset;
270
271   /* Size of file offsets; either 4 or 8.  */
272   unsigned int offset_size;
273
274   /* Size of the length field; either 4 or 12.  */
275   unsigned int initial_length_size;
276
277   /* Offset to the first byte of this compilation unit header in the
278      .debug_info section, for resolving relative reference dies.  */
279   unsigned int offset;
280
281   /* Offset to first die in this cu from the start of the cu.
282      This will be the first byte following the compilation unit header.  */
283   unsigned int first_die_offset;
284 };
285
286 /* Type used for delaying computation of method physnames.
287    See comments for compute_delayed_physnames.  */
288 struct delayed_method_info
289 {
290   /* The type to which the method is attached, i.e., its parent class.  */
291   struct type *type;
292
293   /* The index of the method in the type's function fieldlists.  */
294   int fnfield_index;
295
296   /* The index of the method in the fieldlist.  */
297   int index;
298
299   /* The name of the DIE.  */
300   const char *name;
301
302   /*  The DIE associated with this method.  */
303   struct die_info *die;
304 };
305
306 typedef struct delayed_method_info delayed_method_info;
307 DEF_VEC_O (delayed_method_info);
308
309 /* Internal state when decoding a particular compilation unit.  */
310 struct dwarf2_cu
311 {
312   /* The objfile containing this compilation unit.  */
313   struct objfile *objfile;
314
315   /* The header of the compilation unit.  */
316   struct comp_unit_head header;
317
318   /* Base address of this compilation unit.  */
319   CORE_ADDR base_address;
320
321   /* Non-zero if base_address has been set.  */
322   int base_known;
323
324   struct function_range *first_fn, *last_fn, *cached_fn;
325
326   /* The language we are debugging.  */
327   enum language language;
328   const struct language_defn *language_defn;
329
330   const char *producer;
331
332   /* The generic symbol table building routines have separate lists for
333      file scope symbols and all all other scopes (local scopes).  So
334      we need to select the right one to pass to add_symbol_to_list().
335      We do it by keeping a pointer to the correct list in list_in_scope.
336
337      FIXME: The original dwarf code just treated the file scope as the
338      first local scope, and all other local scopes as nested local
339      scopes, and worked fine.  Check to see if we really need to
340      distinguish these in buildsym.c.  */
341   struct pending **list_in_scope;
342
343   /* DWARF abbreviation table associated with this compilation unit.  */
344   struct abbrev_info **dwarf2_abbrevs;
345
346   /* Storage for the abbrev table.  */
347   struct obstack abbrev_obstack;
348
349   /* Hash table holding all the loaded partial DIEs.  */
350   htab_t partial_dies;
351
352   /* Storage for things with the same lifetime as this read-in compilation
353      unit, including partial DIEs.  */
354   struct obstack comp_unit_obstack;
355
356   /* When multiple dwarf2_cu structures are living in memory, this field
357      chains them all together, so that they can be released efficiently.
358      We will probably also want a generation counter so that most-recently-used
359      compilation units are cached...  */
360   struct dwarf2_per_cu_data *read_in_chain;
361
362   /* Backchain to our per_cu entry if the tree has been built.  */
363   struct dwarf2_per_cu_data *per_cu;
364
365   /* How many compilation units ago was this CU last referenced?  */
366   int last_used;
367
368   /* A hash table of die offsets for following references.  */
369   htab_t die_hash;
370
371   /* Full DIEs if read in.  */
372   struct die_info *dies;
373
374   /* A set of pointers to dwarf2_per_cu_data objects for compilation
375      units referenced by this one.  Only set during full symbol processing;
376      partial symbol tables do not have dependencies.  */
377   htab_t dependencies;
378
379   /* Header data from the line table, during full symbol processing.  */
380   struct line_header *line_header;
381
382   /* A list of methods which need to have physnames computed
383      after all type information has been read.  */
384   VEC (delayed_method_info) *method_list;
385
386   /* Mark used when releasing cached dies.  */
387   unsigned int mark : 1;
388
389   /* This flag will be set if this compilation unit might include
390      inter-compilation-unit references.  */
391   unsigned int has_form_ref_addr : 1;
392
393   /* This flag will be set if this compilation unit includes any
394      DW_TAG_namespace DIEs.  If we know that there are explicit
395      DIEs for namespaces, we don't need to try to infer them
396      from mangled names.  */
397   unsigned int has_namespace_info : 1;
398 };
399
400 /* Persistent data held for a compilation unit, even when not
401    processing it.  We put a pointer to this structure in the
402    read_symtab_private field of the psymtab.  If we encounter
403    inter-compilation-unit references, we also maintain a sorted
404    list of all compilation units.  */
405
406 struct dwarf2_per_cu_data
407 {
408   /* The start offset and length of this compilation unit.  2**29-1
409      bytes should suffice to store the length of any compilation unit
410      - if it doesn't, GDB will fall over anyway.
411      NOTE: Unlike comp_unit_head.length, this length includes
412      initial_length_size.  */
413   unsigned int offset;
414   unsigned int length : 29;
415
416   /* Flag indicating this compilation unit will be read in before
417      any of the current compilation units are processed.  */
418   unsigned int queued : 1;
419
420   /* This flag will be set if we need to load absolutely all DIEs
421      for this compilation unit, instead of just the ones we think
422      are interesting.  It gets set if we look for a DIE in the
423      hash table and don't find it.  */
424   unsigned int load_all_dies : 1;
425
426   /* Non-zero if this CU is from .debug_types.
427      Otherwise it's from .debug_info.  */
428   unsigned int from_debug_types : 1;
429
430   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
431      of the CU cache it gets reset to NULL again.  */
432   struct dwarf2_cu *cu;
433
434   /* The corresponding objfile.  */
435   struct objfile *objfile;
436
437   /* When using partial symbol tables, the 'psymtab' field is active.
438      Otherwise the 'quick' field is active.  */
439   union
440   {
441     /* The partial symbol table associated with this compilation unit,
442        or NULL for partial units (which do not have an associated
443        symtab).  */
444     struct partial_symtab *psymtab;
445
446     /* Data needed by the "quick" functions.  */
447     struct dwarf2_per_cu_quick_data *quick;
448   } v;
449 };
450
451 /* Entry in the signatured_types hash table.  */
452
453 struct signatured_type
454 {
455   ULONGEST signature;
456
457   /* Offset in .debug_types of the TU (type_unit) for this type.  */
458   unsigned int offset;
459
460   /* Offset in .debug_types of the type defined by this TU.  */
461   unsigned int type_offset;
462
463   /* The CU(/TU) of this type.  */
464   struct dwarf2_per_cu_data per_cu;
465 };
466
467 /* Struct used to pass misc. parameters to read_die_and_children, et
468    al.  which are used for both .debug_info and .debug_types dies.
469    All parameters here are unchanging for the life of the call.  This
470    struct exists to abstract away the constant parameters of die
471    reading.  */
472
473 struct die_reader_specs
474 {
475   /* The bfd of this objfile.  */
476   bfd* abfd;
477
478   /* The CU of the DIE we are parsing.  */
479   struct dwarf2_cu *cu;
480
481   /* Pointer to start of section buffer.
482      This is either the start of .debug_info or .debug_types.  */
483   const gdb_byte *buffer;
484 };
485
486 /* The line number information for a compilation unit (found in the
487    .debug_line section) begins with a "statement program header",
488    which contains the following information.  */
489 struct line_header
490 {
491   unsigned int total_length;
492   unsigned short version;
493   unsigned int header_length;
494   unsigned char minimum_instruction_length;
495   unsigned char maximum_ops_per_instruction;
496   unsigned char default_is_stmt;
497   int line_base;
498   unsigned char line_range;
499   unsigned char opcode_base;
500
501   /* standard_opcode_lengths[i] is the number of operands for the
502      standard opcode whose value is i.  This means that
503      standard_opcode_lengths[0] is unused, and the last meaningful
504      element is standard_opcode_lengths[opcode_base - 1].  */
505   unsigned char *standard_opcode_lengths;
506
507   /* The include_directories table.  NOTE!  These strings are not
508      allocated with xmalloc; instead, they are pointers into
509      debug_line_buffer.  If you try to free them, `free' will get
510      indigestion.  */
511   unsigned int num_include_dirs, include_dirs_size;
512   char **include_dirs;
513
514   /* The file_names table.  NOTE!  These strings are not allocated
515      with xmalloc; instead, they are pointers into debug_line_buffer.
516      Don't try to free them directly.  */
517   unsigned int num_file_names, file_names_size;
518   struct file_entry
519   {
520     char *name;
521     unsigned int dir_index;
522     unsigned int mod_time;
523     unsigned int length;
524     int included_p; /* Non-zero if referenced by the Line Number Program.  */
525     struct symtab *symtab; /* The associated symbol table, if any.  */
526   } *file_names;
527
528   /* The start and end of the statement program following this
529      header.  These point into dwarf2_per_objfile->line_buffer.  */
530   gdb_byte *statement_program_start, *statement_program_end;
531 };
532
533 /* When we construct a partial symbol table entry we only
534    need this much information.  */
535 struct partial_die_info
536   {
537     /* Offset of this DIE.  */
538     unsigned int offset;
539
540     /* DWARF-2 tag for this DIE.  */
541     ENUM_BITFIELD(dwarf_tag) tag : 16;
542
543     /* Assorted flags describing the data found in this DIE.  */
544     unsigned int has_children : 1;
545     unsigned int is_external : 1;
546     unsigned int is_declaration : 1;
547     unsigned int has_type : 1;
548     unsigned int has_specification : 1;
549     unsigned int has_pc_info : 1;
550
551     /* Flag set if the SCOPE field of this structure has been
552        computed.  */
553     unsigned int scope_set : 1;
554
555     /* Flag set if the DIE has a byte_size attribute.  */
556     unsigned int has_byte_size : 1;
557
558     /* Flag set if any of the DIE's children are template arguments.  */
559     unsigned int has_template_arguments : 1;
560
561     /* Flag set if fixup_partial_die has been called on this die.  */
562     unsigned int fixup_called : 1;
563
564     /* The name of this DIE.  Normally the value of DW_AT_name, but
565        sometimes a default name for unnamed DIEs.  */
566     char *name;
567
568     /* The linkage name, if present.  */
569     const char *linkage_name;
570
571     /* The scope to prepend to our children.  This is generally
572        allocated on the comp_unit_obstack, so will disappear
573        when this compilation unit leaves the cache.  */
574     char *scope;
575
576     /* The location description associated with this DIE, if any.  */
577     struct dwarf_block *locdesc;
578
579     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
580     CORE_ADDR lowpc;
581     CORE_ADDR highpc;
582
583     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
584        DW_AT_sibling, if any.  */
585     /* NOTE: This member isn't strictly necessary, read_partial_die could
586        return DW_AT_sibling values to its caller load_partial_dies.  */
587     gdb_byte *sibling;
588
589     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
590        DW_AT_specification (or DW_AT_abstract_origin or
591        DW_AT_extension).  */
592     unsigned int spec_offset;
593
594     /* Pointers to this DIE's parent, first child, and next sibling,
595        if any.  */
596     struct partial_die_info *die_parent, *die_child, *die_sibling;
597   };
598
599 /* This data structure holds the information of an abbrev.  */
600 struct abbrev_info
601   {
602     unsigned int number;        /* number identifying abbrev */
603     enum dwarf_tag tag;         /* dwarf tag */
604     unsigned short has_children;                /* boolean */
605     unsigned short num_attrs;   /* number of attributes */
606     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
607     struct abbrev_info *next;   /* next in chain */
608   };
609
610 struct attr_abbrev
611   {
612     ENUM_BITFIELD(dwarf_attribute) name : 16;
613     ENUM_BITFIELD(dwarf_form) form : 16;
614   };
615
616 /* Attributes have a name and a value.  */
617 struct attribute
618   {
619     ENUM_BITFIELD(dwarf_attribute) name : 16;
620     ENUM_BITFIELD(dwarf_form) form : 15;
621
622     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
623        field should be in u.str (existing only for DW_STRING) but it is kept
624        here for better struct attribute alignment.  */
625     unsigned int string_is_canonical : 1;
626
627     union
628       {
629         char *str;
630         struct dwarf_block *blk;
631         ULONGEST unsnd;
632         LONGEST snd;
633         CORE_ADDR addr;
634         struct signatured_type *signatured_type;
635       }
636     u;
637   };
638
639 /* This data structure holds a complete die structure.  */
640 struct die_info
641   {
642     /* DWARF-2 tag for this DIE.  */
643     ENUM_BITFIELD(dwarf_tag) tag : 16;
644
645     /* Number of attributes */
646     unsigned char num_attrs;
647
648     /* True if we're presently building the full type name for the
649        type derived from this DIE.  */
650     unsigned char building_fullname : 1;
651
652     /* Abbrev number */
653     unsigned int abbrev;
654
655     /* Offset in .debug_info or .debug_types section.  */
656     unsigned int offset;
657
658     /* The dies in a compilation unit form an n-ary tree.  PARENT
659        points to this die's parent; CHILD points to the first child of
660        this node; and all the children of a given node are chained
661        together via their SIBLING fields.  */
662     struct die_info *child;     /* Its first child, if any.  */
663     struct die_info *sibling;   /* Its next sibling, if any.  */
664     struct die_info *parent;    /* Its parent, if any.  */
665
666     /* An array of attributes, with NUM_ATTRS elements.  There may be
667        zero, but it's not common and zero-sized arrays are not
668        sufficiently portable C.  */
669     struct attribute attrs[1];
670   };
671
672 struct function_range
673 {
674   const char *name;
675   CORE_ADDR lowpc, highpc;
676   int seen_line;
677   struct function_range *next;
678 };
679
680 /* Get at parts of an attribute structure.  */
681
682 #define DW_STRING(attr)    ((attr)->u.str)
683 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
684 #define DW_UNSND(attr)     ((attr)->u.unsnd)
685 #define DW_BLOCK(attr)     ((attr)->u.blk)
686 #define DW_SND(attr)       ((attr)->u.snd)
687 #define DW_ADDR(attr)      ((attr)->u.addr)
688 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
689
690 /* Blocks are a bunch of untyped bytes.  */
691 struct dwarf_block
692   {
693     unsigned int size;
694     gdb_byte *data;
695   };
696
697 #ifndef ATTR_ALLOC_CHUNK
698 #define ATTR_ALLOC_CHUNK 4
699 #endif
700
701 /* Allocate fields for structs, unions and enums in this size.  */
702 #ifndef DW_FIELD_ALLOC_CHUNK
703 #define DW_FIELD_ALLOC_CHUNK 4
704 #endif
705
706 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
707    but this would require a corresponding change in unpack_field_as_long
708    and friends.  */
709 static int bits_per_byte = 8;
710
711 /* The routines that read and process dies for a C struct or C++ class
712    pass lists of data member fields and lists of member function fields
713    in an instance of a field_info structure, as defined below.  */
714 struct field_info
715   {
716     /* List of data member and baseclasses fields.  */
717     struct nextfield
718       {
719         struct nextfield *next;
720         int accessibility;
721         int virtuality;
722         struct field field;
723       }
724      *fields, *baseclasses;
725
726     /* Number of fields (including baseclasses).  */
727     int nfields;
728
729     /* Number of baseclasses.  */
730     int nbaseclasses;
731
732     /* Set if the accesibility of one of the fields is not public.  */
733     int non_public_fields;
734
735     /* Member function fields array, entries are allocated in the order they
736        are encountered in the object file.  */
737     struct nextfnfield
738       {
739         struct nextfnfield *next;
740         struct fn_field fnfield;
741       }
742      *fnfields;
743
744     /* Member function fieldlist array, contains name of possibly overloaded
745        member function, number of overloaded member functions and a pointer
746        to the head of the member function field chain.  */
747     struct fnfieldlist
748       {
749         char *name;
750         int length;
751         struct nextfnfield *head;
752       }
753      *fnfieldlists;
754
755     /* Number of entries in the fnfieldlists array.  */
756     int nfnfields;
757
758     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
759        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
760     struct typedef_field_list
761       {
762         struct typedef_field field;
763         struct typedef_field_list *next;
764       }
765     *typedef_field_list;
766     unsigned typedef_field_list_count;
767   };
768
769 /* One item on the queue of compilation units to read in full symbols
770    for.  */
771 struct dwarf2_queue_item
772 {
773   struct dwarf2_per_cu_data *per_cu;
774   struct dwarf2_queue_item *next;
775 };
776
777 /* The current queue.  */
778 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
779
780 /* Loaded secondary compilation units are kept in memory until they
781    have not been referenced for the processing of this many
782    compilation units.  Set this to zero to disable caching.  Cache
783    sizes of up to at least twenty will improve startup time for
784    typical inter-CU-reference binaries, at an obvious memory cost.  */
785 static int dwarf2_max_cache_age = 5;
786 static void
787 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
788                            struct cmd_list_element *c, const char *value)
789 {
790   fprintf_filtered (file, _("The upper bound on the age of cached "
791                             "dwarf2 compilation units is %s.\n"),
792                     value);
793 }
794
795
796 /* Various complaints about symbol reading that don't abort the process.  */
797
798 static void
799 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
800 {
801   complaint (&symfile_complaints,
802              _("statement list doesn't fit in .debug_line section"));
803 }
804
805 static void
806 dwarf2_debug_line_missing_file_complaint (void)
807 {
808   complaint (&symfile_complaints,
809              _(".debug_line section has line data without a file"));
810 }
811
812 static void
813 dwarf2_debug_line_missing_end_sequence_complaint (void)
814 {
815   complaint (&symfile_complaints,
816              _(".debug_line section has line "
817                "program sequence without an end"));
818 }
819
820 static void
821 dwarf2_complex_location_expr_complaint (void)
822 {
823   complaint (&symfile_complaints, _("location expression too complex"));
824 }
825
826 static void
827 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
828                                               int arg3)
829 {
830   complaint (&symfile_complaints,
831              _("const value length mismatch for '%s', got %d, expected %d"),
832              arg1, arg2, arg3);
833 }
834
835 static void
836 dwarf2_macros_too_long_complaint (void)
837 {
838   complaint (&symfile_complaints,
839              _("macro info runs off end of `.debug_macinfo' section"));
840 }
841
842 static void
843 dwarf2_macro_malformed_definition_complaint (const char *arg1)
844 {
845   complaint (&symfile_complaints,
846              _("macro debug info contains a "
847                "malformed macro definition:\n`%s'"),
848              arg1);
849 }
850
851 static void
852 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
853 {
854   complaint (&symfile_complaints,
855              _("invalid attribute class or form for '%s' in '%s'"),
856              arg1, arg2);
857 }
858
859 /* local function prototypes */
860
861 static void dwarf2_locate_sections (bfd *, asection *, void *);
862
863 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
864                                            struct objfile *);
865
866 static void dwarf2_build_psymtabs_hard (struct objfile *);
867
868 static void scan_partial_symbols (struct partial_die_info *,
869                                   CORE_ADDR *, CORE_ADDR *,
870                                   int, struct dwarf2_cu *);
871
872 static void add_partial_symbol (struct partial_die_info *,
873                                 struct dwarf2_cu *);
874
875 static void add_partial_namespace (struct partial_die_info *pdi,
876                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
877                                    int need_pc, struct dwarf2_cu *cu);
878
879 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
880                                 CORE_ADDR *highpc, int need_pc,
881                                 struct dwarf2_cu *cu);
882
883 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
884                                      struct dwarf2_cu *cu);
885
886 static void add_partial_subprogram (struct partial_die_info *pdi,
887                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
888                                     int need_pc, struct dwarf2_cu *cu);
889
890 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
891                                      gdb_byte *buffer, gdb_byte *info_ptr,
892                                      bfd *abfd, struct dwarf2_cu *cu);
893
894 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
895
896 static void psymtab_to_symtab_1 (struct partial_symtab *);
897
898 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
899
900 static void dwarf2_free_abbrev_table (void *);
901
902 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
903                                             struct dwarf2_cu *);
904
905 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
906                                                  struct dwarf2_cu *);
907
908 static struct partial_die_info *load_partial_dies (bfd *,
909                                                    gdb_byte *, gdb_byte *,
910                                                    int, struct dwarf2_cu *);
911
912 static gdb_byte *read_partial_die (struct partial_die_info *,
913                                    struct abbrev_info *abbrev,
914                                    unsigned int, bfd *,
915                                    gdb_byte *, gdb_byte *,
916                                    struct dwarf2_cu *);
917
918 static struct partial_die_info *find_partial_die (unsigned int,
919                                                   struct dwarf2_cu *);
920
921 static void fixup_partial_die (struct partial_die_info *,
922                                struct dwarf2_cu *);
923
924 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
925                                  bfd *, gdb_byte *, struct dwarf2_cu *);
926
927 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
928                                        bfd *, gdb_byte *, struct dwarf2_cu *);
929
930 static unsigned int read_1_byte (bfd *, gdb_byte *);
931
932 static int read_1_signed_byte (bfd *, gdb_byte *);
933
934 static unsigned int read_2_bytes (bfd *, gdb_byte *);
935
936 static unsigned int read_4_bytes (bfd *, gdb_byte *);
937
938 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
939
940 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
941                                unsigned int *);
942
943 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
944
945 static LONGEST read_checked_initial_length_and_offset
946   (bfd *, gdb_byte *, const struct comp_unit_head *,
947    unsigned int *, unsigned int *);
948
949 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
950                             unsigned int *);
951
952 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
953
954 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
955
956 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
957
958 static char *read_indirect_string (bfd *, gdb_byte *,
959                                    const struct comp_unit_head *,
960                                    unsigned int *);
961
962 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
963
964 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
965
966 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
967
968 static void set_cu_language (unsigned int, struct dwarf2_cu *);
969
970 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
971                                       struct dwarf2_cu *);
972
973 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
974                                                 unsigned int,
975                                                 struct dwarf2_cu *);
976
977 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
978                                struct dwarf2_cu *cu);
979
980 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
981
982 static struct die_info *die_specification (struct die_info *die,
983                                            struct dwarf2_cu **);
984
985 static void free_line_header (struct line_header *lh);
986
987 static void add_file_name (struct line_header *, char *, unsigned int,
988                            unsigned int, unsigned int);
989
990 static struct line_header *(dwarf_decode_line_header
991                             (unsigned int offset,
992                              bfd *abfd, struct dwarf2_cu *cu));
993
994 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
995                                 struct dwarf2_cu *, struct partial_symtab *);
996
997 static void dwarf2_start_subfile (char *, const char *, const char *);
998
999 static struct symbol *new_symbol (struct die_info *, struct type *,
1000                                   struct dwarf2_cu *);
1001
1002 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003                                        struct dwarf2_cu *, struct symbol *);
1004
1005 static void dwarf2_const_value (struct attribute *, struct symbol *,
1006                                 struct dwarf2_cu *);
1007
1008 static void dwarf2_const_value_attr (struct attribute *attr,
1009                                      struct type *type,
1010                                      const char *name,
1011                                      struct obstack *obstack,
1012                                      struct dwarf2_cu *cu, long *value,
1013                                      gdb_byte **bytes,
1014                                      struct dwarf2_locexpr_baton **baton);
1015
1016 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1017
1018 static int need_gnat_info (struct dwarf2_cu *);
1019
1020 static struct type *die_descriptive_type (struct die_info *,
1021                                           struct dwarf2_cu *);
1022
1023 static void set_descriptive_type (struct type *, struct die_info *,
1024                                   struct dwarf2_cu *);
1025
1026 static struct type *die_containing_type (struct die_info *,
1027                                          struct dwarf2_cu *);
1028
1029 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030                                      struct dwarf2_cu *);
1031
1032 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1033
1034 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
1036 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1037
1038 static char *typename_concat (struct obstack *obs, const char *prefix,
1039                               const char *suffix, int physname,
1040                               struct dwarf2_cu *cu);
1041
1042 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1043
1044 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
1046 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1047
1048 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1049
1050 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1051                                struct dwarf2_cu *, struct partial_symtab *);
1052
1053 static int dwarf2_get_pc_bounds (struct die_info *,
1054                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1055                                  struct partial_symtab *);
1056
1057 static void get_scope_pc_bounds (struct die_info *,
1058                                  CORE_ADDR *, CORE_ADDR *,
1059                                  struct dwarf2_cu *);
1060
1061 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1062                                         CORE_ADDR, struct dwarf2_cu *);
1063
1064 static void dwarf2_add_field (struct field_info *, struct die_info *,
1065                               struct dwarf2_cu *);
1066
1067 static void dwarf2_attach_fields_to_type (struct field_info *,
1068                                           struct type *, struct dwarf2_cu *);
1069
1070 static void dwarf2_add_member_fn (struct field_info *,
1071                                   struct die_info *, struct type *,
1072                                   struct dwarf2_cu *);
1073
1074 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1075                                              struct type *,
1076                                              struct dwarf2_cu *);
1077
1078 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1079
1080 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1081
1082 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1083
1084 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1085
1086 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1087
1088 static struct type *read_module_type (struct die_info *die,
1089                                       struct dwarf2_cu *cu);
1090
1091 static const char *namespace_name (struct die_info *die,
1092                                    int *is_anonymous, struct dwarf2_cu *);
1093
1094 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1095
1096 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1097
1098 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1099                                                        struct dwarf2_cu *);
1100
1101 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1102
1103 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1104                                                  gdb_byte *info_ptr,
1105                                                  gdb_byte **new_info_ptr,
1106                                                  struct die_info *parent);
1107
1108 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1109                                                gdb_byte *info_ptr,
1110                                                gdb_byte **new_info_ptr,
1111                                                struct die_info *parent);
1112
1113 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1114                                                gdb_byte *info_ptr,
1115                                                gdb_byte **new_info_ptr,
1116                                                struct die_info *parent);
1117
1118 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1119                                 struct die_info **, gdb_byte *,
1120                                 int *);
1121
1122 static void process_die (struct die_info *, struct dwarf2_cu *);
1123
1124 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1125                                        struct obstack *);
1126
1127 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1128
1129 static const char *dwarf2_full_name (char *name,
1130                                      struct die_info *die,
1131                                      struct dwarf2_cu *cu);
1132
1133 static struct die_info *dwarf2_extension (struct die_info *die,
1134                                           struct dwarf2_cu **);
1135
1136 static char *dwarf_tag_name (unsigned int);
1137
1138 static char *dwarf_attr_name (unsigned int);
1139
1140 static char *dwarf_form_name (unsigned int);
1141
1142 static char *dwarf_bool_name (unsigned int);
1143
1144 static char *dwarf_type_encoding_name (unsigned int);
1145
1146 #if 0
1147 static char *dwarf_cfi_name (unsigned int);
1148 #endif
1149
1150 static struct die_info *sibling_die (struct die_info *);
1151
1152 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1153
1154 static void dump_die_for_error (struct die_info *);
1155
1156 static void dump_die_1 (struct ui_file *, int level, int max_level,
1157                         struct die_info *);
1158
1159 /*static*/ void dump_die (struct die_info *, int max_level);
1160
1161 static void store_in_ref_table (struct die_info *,
1162                                 struct dwarf2_cu *);
1163
1164 static int is_ref_attr (struct attribute *);
1165
1166 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1167
1168 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1169
1170 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1171                                                struct attribute *,
1172                                                struct dwarf2_cu **);
1173
1174 static struct die_info *follow_die_ref (struct die_info *,
1175                                         struct attribute *,
1176                                         struct dwarf2_cu **);
1177
1178 static struct die_info *follow_die_sig (struct die_info *,
1179                                         struct attribute *,
1180                                         struct dwarf2_cu **);
1181
1182 static void read_signatured_type_at_offset (struct objfile *objfile,
1183                                             unsigned int offset);
1184
1185 static void read_signatured_type (struct objfile *,
1186                                   struct signatured_type *type_sig);
1187
1188 /* memory allocation interface */
1189
1190 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1191
1192 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1193
1194 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1195
1196 static void initialize_cu_func_list (struct dwarf2_cu *);
1197
1198 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1199                                  struct dwarf2_cu *);
1200
1201 static void dwarf_decode_macros (struct line_header *, unsigned int,
1202                                  char *, bfd *, struct dwarf2_cu *);
1203
1204 static int attr_form_is_block (struct attribute *);
1205
1206 static int attr_form_is_section_offset (struct attribute *);
1207
1208 static int attr_form_is_constant (struct attribute *);
1209
1210 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211                                    struct dwarf2_loclist_baton *baton,
1212                                    struct attribute *attr);
1213
1214 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215                                          struct symbol *sym,
1216                                          struct dwarf2_cu *cu);
1217
1218 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219                                struct abbrev_info *abbrev,
1220                                struct dwarf2_cu *cu);
1221
1222 static void free_stack_comp_unit (void *);
1223
1224 static hashval_t partial_die_hash (const void *item);
1225
1226 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
1228 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1229   (unsigned int offset, struct objfile *objfile);
1230
1231 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1232   (unsigned int offset, struct objfile *objfile);
1233
1234 static void init_one_comp_unit (struct dwarf2_cu *cu,
1235                                 struct objfile *objfile);
1236
1237 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1238                                    struct die_info *comp_unit_die);
1239
1240 static void free_one_comp_unit (void *);
1241
1242 static void free_cached_comp_units (void *);
1243
1244 static void age_cached_comp_units (void);
1245
1246 static void free_one_cached_comp_unit (void *);
1247
1248 static struct type *set_die_type (struct die_info *, struct type *,
1249                                   struct dwarf2_cu *);
1250
1251 static void create_all_comp_units (struct objfile *);
1252
1253 static int create_debug_types_hash_table (struct objfile *objfile);
1254
1255 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1256                                  struct objfile *);
1257
1258 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1259
1260 static void dwarf2_add_dependence (struct dwarf2_cu *,
1261                                    struct dwarf2_per_cu_data *);
1262
1263 static void dwarf2_mark (struct dwarf2_cu *);
1264
1265 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1266
1267 static struct type *get_die_type_at_offset (unsigned int,
1268                                             struct dwarf2_per_cu_data *per_cu);
1269
1270 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1271
1272 static void dwarf2_release_queue (void *dummy);
1273
1274 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1275                              struct objfile *objfile);
1276
1277 static void process_queue (struct objfile *objfile);
1278
1279 static void find_file_and_directory (struct die_info *die,
1280                                      struct dwarf2_cu *cu,
1281                                      char **name, char **comp_dir);
1282
1283 static char *file_full_name (int file, struct line_header *lh,
1284                              const char *comp_dir);
1285
1286 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1287                                               gdb_byte *info_ptr,
1288                                               gdb_byte *buffer,
1289                                               unsigned int buffer_size,
1290                                               bfd *abfd);
1291
1292 static void init_cu_die_reader (struct die_reader_specs *reader,
1293                                 struct dwarf2_cu *cu);
1294
1295 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1296
1297 #if WORDS_BIGENDIAN
1298
1299 /* Convert VALUE between big- and little-endian.  */
1300 static offset_type
1301 byte_swap (offset_type value)
1302 {
1303   offset_type result;
1304
1305   result = (value & 0xff) << 24;
1306   result |= (value & 0xff00) << 8;
1307   result |= (value & 0xff0000) >> 8;
1308   result |= (value & 0xff000000) >> 24;
1309   return result;
1310 }
1311
1312 #define MAYBE_SWAP(V)  byte_swap (V)
1313
1314 #else
1315 #define MAYBE_SWAP(V) (V)
1316 #endif /* WORDS_BIGENDIAN */
1317
1318 /* The suffix for an index file.  */
1319 #define INDEX_SUFFIX ".gdb-index"
1320
1321 static const char *dwarf2_physname (char *name, struct die_info *die,
1322                                     struct dwarf2_cu *cu);
1323
1324 /* Try to locate the sections we need for DWARF 2 debugging
1325    information and return true if we have enough to do something.  */
1326
1327 int
1328 dwarf2_has_info (struct objfile *objfile)
1329 {
1330   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1331   if (!dwarf2_per_objfile)
1332     {
1333       /* Initialize per-objfile state.  */
1334       struct dwarf2_per_objfile *data
1335         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1336
1337       memset (data, 0, sizeof (*data));
1338       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1339       dwarf2_per_objfile = data;
1340
1341       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1342       dwarf2_per_objfile->objfile = objfile;
1343     }
1344   return (dwarf2_per_objfile->info.asection != NULL
1345           && dwarf2_per_objfile->abbrev.asection != NULL);
1346 }
1347
1348 /* When loading sections, we can either look for ".<name>", or for
1349  * ".z<name>", which indicates a compressed section.  */
1350
1351 static int
1352 section_is_p (const char *section_name, const char *name)
1353 {
1354   return (section_name[0] == '.'
1355           && (strcmp (section_name + 1, name) == 0
1356               || (section_name[1] == 'z'
1357                   && strcmp (section_name + 2, name) == 0)));
1358 }
1359
1360 /* This function is mapped across the sections and remembers the
1361    offset and size of each of the debugging sections we are interested
1362    in.  */
1363
1364 static void
1365 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1366 {
1367   if (section_is_p (sectp->name, INFO_SECTION))
1368     {
1369       dwarf2_per_objfile->info.asection = sectp;
1370       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1371     }
1372   else if (section_is_p (sectp->name, ABBREV_SECTION))
1373     {
1374       dwarf2_per_objfile->abbrev.asection = sectp;
1375       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1376     }
1377   else if (section_is_p (sectp->name, LINE_SECTION))
1378     {
1379       dwarf2_per_objfile->line.asection = sectp;
1380       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1381     }
1382   else if (section_is_p (sectp->name, LOC_SECTION))
1383     {
1384       dwarf2_per_objfile->loc.asection = sectp;
1385       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1386     }
1387   else if (section_is_p (sectp->name, MACINFO_SECTION))
1388     {
1389       dwarf2_per_objfile->macinfo.asection = sectp;
1390       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1391     }
1392   else if (section_is_p (sectp->name, STR_SECTION))
1393     {
1394       dwarf2_per_objfile->str.asection = sectp;
1395       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1396     }
1397   else if (section_is_p (sectp->name, FRAME_SECTION))
1398     {
1399       dwarf2_per_objfile->frame.asection = sectp;
1400       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1401     }
1402   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1403     {
1404       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1405
1406       if (aflag & SEC_HAS_CONTENTS)
1407         {
1408           dwarf2_per_objfile->eh_frame.asection = sectp;
1409           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1410         }
1411     }
1412   else if (section_is_p (sectp->name, RANGES_SECTION))
1413     {
1414       dwarf2_per_objfile->ranges.asection = sectp;
1415       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1416     }
1417   else if (section_is_p (sectp->name, TYPES_SECTION))
1418     {
1419       dwarf2_per_objfile->types.asection = sectp;
1420       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1421     }
1422   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1423     {
1424       dwarf2_per_objfile->gdb_index.asection = sectp;
1425       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1426     }
1427
1428   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1429       && bfd_section_vma (abfd, sectp) == 0)
1430     dwarf2_per_objfile->has_section_at_zero = 1;
1431 }
1432
1433 /* Decompress a section that was compressed using zlib.  Store the
1434    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1435
1436 static void
1437 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1438                          gdb_byte **outbuf, bfd_size_type *outsize)
1439 {
1440   bfd *abfd = objfile->obfd;
1441 #ifndef HAVE_ZLIB_H
1442   error (_("Support for zlib-compressed DWARF data (from '%s') "
1443            "is disabled in this copy of GDB"),
1444          bfd_get_filename (abfd));
1445 #else
1446   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1447   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1448   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1449   bfd_size_type uncompressed_size;
1450   gdb_byte *uncompressed_buffer;
1451   z_stream strm;
1452   int rc;
1453   int header_size = 12;
1454
1455   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1456       || bfd_bread (compressed_buffer,
1457                     compressed_size, abfd) != compressed_size)
1458     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1459            bfd_get_filename (abfd));
1460
1461   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1462      by the uncompressed section size, 8 bytes in big-endian order.  */
1463   if (compressed_size < header_size
1464       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1465     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1466            bfd_get_filename (abfd));
1467   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1468   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1469   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1470   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1471   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1472   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1473   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1474   uncompressed_size += compressed_buffer[11];
1475
1476   /* It is possible the section consists of several compressed
1477      buffers concatenated together, so we uncompress in a loop.  */
1478   strm.zalloc = NULL;
1479   strm.zfree = NULL;
1480   strm.opaque = NULL;
1481   strm.avail_in = compressed_size - header_size;
1482   strm.next_in = (Bytef*) compressed_buffer + header_size;
1483   strm.avail_out = uncompressed_size;
1484   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1485                                        uncompressed_size);
1486   rc = inflateInit (&strm);
1487   while (strm.avail_in > 0)
1488     {
1489       if (rc != Z_OK)
1490         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1491                bfd_get_filename (abfd), rc);
1492       strm.next_out = ((Bytef*) uncompressed_buffer
1493                        + (uncompressed_size - strm.avail_out));
1494       rc = inflate (&strm, Z_FINISH);
1495       if (rc != Z_STREAM_END)
1496         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1497                bfd_get_filename (abfd), rc);
1498       rc = inflateReset (&strm);
1499     }
1500   rc = inflateEnd (&strm);
1501   if (rc != Z_OK
1502       || strm.avail_out != 0)
1503     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1504            bfd_get_filename (abfd), rc);
1505
1506   do_cleanups (cleanup);
1507   *outbuf = uncompressed_buffer;
1508   *outsize = uncompressed_size;
1509 #endif
1510 }
1511
1512 /* A helper function that decides whether a section is empty.  */
1513
1514 static int
1515 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1516 {
1517   return info->asection == NULL || info->size == 0;
1518 }
1519
1520 /* Read the contents of the section SECTP from object file specified by
1521    OBJFILE, store info about the section into INFO.
1522    If the section is compressed, uncompress it before returning.  */
1523
1524 static void
1525 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1526 {
1527   bfd *abfd = objfile->obfd;
1528   asection *sectp = info->asection;
1529   gdb_byte *buf, *retbuf;
1530   unsigned char header[4];
1531
1532   if (info->readin)
1533     return;
1534   info->buffer = NULL;
1535   info->was_mmapped = 0;
1536   info->readin = 1;
1537
1538   if (dwarf2_section_empty_p (info))
1539     return;
1540
1541   /* Check if the file has a 4-byte header indicating compression.  */
1542   if (info->size > sizeof (header)
1543       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1544       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1545     {
1546       /* Upon decompression, update the buffer and its size.  */
1547       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1548         {
1549           zlib_decompress_section (objfile, sectp, &info->buffer,
1550                                    &info->size);
1551           return;
1552         }
1553     }
1554
1555 #ifdef HAVE_MMAP
1556   if (pagesize == 0)
1557     pagesize = getpagesize ();
1558
1559   /* Only try to mmap sections which are large enough: we don't want to
1560      waste space due to fragmentation.  Also, only try mmap for sections
1561      without relocations.  */
1562
1563   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1564     {
1565       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1566       size_t map_length = info->size + sectp->filepos - pg_offset;
1567       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1568                                  MAP_PRIVATE, pg_offset);
1569
1570       if (retbuf != MAP_FAILED)
1571         {
1572           info->was_mmapped = 1;
1573           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1574 #if HAVE_POSIX_MADVISE
1575           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1576 #endif
1577           return;
1578         }
1579     }
1580 #endif
1581
1582   /* If we get here, we are a normal, not-compressed section.  */
1583   info->buffer = buf
1584     = obstack_alloc (&objfile->objfile_obstack, info->size);
1585
1586   /* When debugging .o files, we may need to apply relocations; see
1587      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1588      We never compress sections in .o files, so we only need to
1589      try this when the section is not compressed.  */
1590   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1591   if (retbuf != NULL)
1592     {
1593       info->buffer = retbuf;
1594       return;
1595     }
1596
1597   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1598       || bfd_bread (buf, info->size, abfd) != info->size)
1599     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1600            bfd_get_filename (abfd));
1601 }
1602
1603 /* A helper function that returns the size of a section in a safe way.
1604    If you are positive that the section has been read before using the
1605    size, then it is safe to refer to the dwarf2_section_info object's
1606    "size" field directly.  In other cases, you must call this
1607    function, because for compressed sections the size field is not set
1608    correctly until the section has been read.  */
1609
1610 static bfd_size_type
1611 dwarf2_section_size (struct objfile *objfile,
1612                      struct dwarf2_section_info *info)
1613 {
1614   if (!info->readin)
1615     dwarf2_read_section (objfile, info);
1616   return info->size;
1617 }
1618
1619 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1620    SECTION_NAME.  */
1621
1622 void
1623 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1624                          asection **sectp, gdb_byte **bufp,
1625                          bfd_size_type *sizep)
1626 {
1627   struct dwarf2_per_objfile *data
1628     = objfile_data (objfile, dwarf2_objfile_data_key);
1629   struct dwarf2_section_info *info;
1630
1631   /* We may see an objfile without any DWARF, in which case we just
1632      return nothing.  */
1633   if (data == NULL)
1634     {
1635       *sectp = NULL;
1636       *bufp = NULL;
1637       *sizep = 0;
1638       return;
1639     }
1640   if (section_is_p (section_name, EH_FRAME_SECTION))
1641     info = &data->eh_frame;
1642   else if (section_is_p (section_name, FRAME_SECTION))
1643     info = &data->frame;
1644   else
1645     gdb_assert_not_reached ("unexpected section");
1646
1647   dwarf2_read_section (objfile, info);
1648
1649   *sectp = info->asection;
1650   *bufp = info->buffer;
1651   *sizep = info->size;
1652 }
1653
1654 \f
1655 /* DWARF quick_symbols_functions support.  */
1656
1657 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1658    unique line tables, so we maintain a separate table of all .debug_line
1659    derived entries to support the sharing.
1660    All the quick functions need is the list of file names.  We discard the
1661    line_header when we're done and don't need to record it here.  */
1662 struct quick_file_names
1663 {
1664   /* The offset in .debug_line of the line table.  We hash on this.  */
1665   unsigned int offset;
1666
1667   /* The number of entries in file_names, real_names.  */
1668   unsigned int num_file_names;
1669
1670   /* The file names from the line table, after being run through
1671      file_full_name.  */
1672   const char **file_names;
1673
1674   /* The file names from the line table after being run through
1675      gdb_realpath.  These are computed lazily.  */
1676   const char **real_names;
1677 };
1678
1679 /* When using the index (and thus not using psymtabs), each CU has an
1680    object of this type.  This is used to hold information needed by
1681    the various "quick" methods.  */
1682 struct dwarf2_per_cu_quick_data
1683 {
1684   /* The file table.  This can be NULL if there was no file table
1685      or it's currently not read in.
1686      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1687   struct quick_file_names *file_names;
1688
1689   /* The corresponding symbol table.  This is NULL if symbols for this
1690      CU have not yet been read.  */
1691   struct symtab *symtab;
1692
1693   /* A temporary mark bit used when iterating over all CUs in
1694      expand_symtabs_matching.  */
1695   unsigned int mark : 1;
1696
1697   /* True if we've tried to read the file table and found there isn't one.
1698      There will be no point in trying to read it again next time.  */
1699   unsigned int no_file_data : 1;
1700 };
1701
1702 /* Hash function for a quick_file_names.  */
1703
1704 static hashval_t
1705 hash_file_name_entry (const void *e)
1706 {
1707   const struct quick_file_names *file_data = e;
1708
1709   return file_data->offset;
1710 }
1711
1712 /* Equality function for a quick_file_names.  */
1713
1714 static int
1715 eq_file_name_entry (const void *a, const void *b)
1716 {
1717   const struct quick_file_names *ea = a;
1718   const struct quick_file_names *eb = b;
1719
1720   return ea->offset == eb->offset;
1721 }
1722
1723 /* Delete function for a quick_file_names.  */
1724
1725 static void
1726 delete_file_name_entry (void *e)
1727 {
1728   struct quick_file_names *file_data = e;
1729   int i;
1730
1731   for (i = 0; i < file_data->num_file_names; ++i)
1732     {
1733       xfree ((void*) file_data->file_names[i]);
1734       if (file_data->real_names)
1735         xfree ((void*) file_data->real_names[i]);
1736     }
1737
1738   /* The space for the struct itself lives on objfile_obstack,
1739      so we don't free it here.  */
1740 }
1741
1742 /* Create a quick_file_names hash table.  */
1743
1744 static htab_t
1745 create_quick_file_names_table (unsigned int nr_initial_entries)
1746 {
1747   return htab_create_alloc (nr_initial_entries,
1748                             hash_file_name_entry, eq_file_name_entry,
1749                             delete_file_name_entry, xcalloc, xfree);
1750 }
1751
1752 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1753    this CU came.  */
1754
1755 static void
1756 dw2_do_instantiate_symtab (struct objfile *objfile,
1757                            struct dwarf2_per_cu_data *per_cu)
1758 {
1759   struct cleanup *back_to;
1760
1761   back_to = make_cleanup (dwarf2_release_queue, NULL);
1762
1763   queue_comp_unit (per_cu, objfile);
1764
1765   if (per_cu->from_debug_types)
1766     read_signatured_type_at_offset (objfile, per_cu->offset);
1767   else
1768     load_full_comp_unit (per_cu, objfile);
1769
1770   process_queue (objfile);
1771
1772   /* Age the cache, releasing compilation units that have not
1773      been used recently.  */
1774   age_cached_comp_units ();
1775
1776   do_cleanups (back_to);
1777 }
1778
1779 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1780    the objfile from which this CU came.  Returns the resulting symbol
1781    table.  */
1782
1783 static struct symtab *
1784 dw2_instantiate_symtab (struct objfile *objfile,
1785                         struct dwarf2_per_cu_data *per_cu)
1786 {
1787   if (!per_cu->v.quick->symtab)
1788     {
1789       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1790       increment_reading_symtab ();
1791       dw2_do_instantiate_symtab (objfile, per_cu);
1792       do_cleanups (back_to);
1793     }
1794   return per_cu->v.quick->symtab;
1795 }
1796
1797 /* Return the CU given its index.  */
1798
1799 static struct dwarf2_per_cu_data *
1800 dw2_get_cu (int index)
1801 {
1802   if (index >= dwarf2_per_objfile->n_comp_units)
1803     {
1804       index -= dwarf2_per_objfile->n_comp_units;
1805       return dwarf2_per_objfile->type_comp_units[index];
1806     }
1807   return dwarf2_per_objfile->all_comp_units[index];
1808 }
1809
1810 /* A helper function that knows how to read a 64-bit value in a way
1811    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1812    otherwise.  */
1813
1814 static int
1815 extract_cu_value (const char *bytes, ULONGEST *result)
1816 {
1817   if (sizeof (ULONGEST) < 8)
1818     {
1819       int i;
1820
1821       /* Ignore the upper 4 bytes if they are all zero.  */
1822       for (i = 0; i < 4; ++i)
1823         if (bytes[i + 4] != 0)
1824           return 0;
1825
1826       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1827     }
1828   else
1829     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1830   return 1;
1831 }
1832
1833 /* Read the CU list from the mapped index, and use it to create all
1834    the CU objects for this objfile.  Return 0 if something went wrong,
1835    1 if everything went ok.  */
1836
1837 static int
1838 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1839                        offset_type cu_list_elements)
1840 {
1841   offset_type i;
1842
1843   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1844   dwarf2_per_objfile->all_comp_units
1845     = obstack_alloc (&objfile->objfile_obstack,
1846                      dwarf2_per_objfile->n_comp_units
1847                      * sizeof (struct dwarf2_per_cu_data *));
1848
1849   for (i = 0; i < cu_list_elements; i += 2)
1850     {
1851       struct dwarf2_per_cu_data *the_cu;
1852       ULONGEST offset, length;
1853
1854       if (!extract_cu_value (cu_list, &offset)
1855           || !extract_cu_value (cu_list + 8, &length))
1856         return 0;
1857       cu_list += 2 * 8;
1858
1859       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1860                                struct dwarf2_per_cu_data);
1861       the_cu->offset = offset;
1862       the_cu->length = length;
1863       the_cu->objfile = objfile;
1864       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1865                                         struct dwarf2_per_cu_quick_data);
1866       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1867     }
1868
1869   return 1;
1870 }
1871
1872 /* Create the signatured type hash table from the index.  */
1873
1874 static int
1875 create_signatured_type_table_from_index (struct objfile *objfile,
1876                                          const gdb_byte *bytes,
1877                                          offset_type elements)
1878 {
1879   offset_type i;
1880   htab_t sig_types_hash;
1881
1882   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1883   dwarf2_per_objfile->type_comp_units
1884     = obstack_alloc (&objfile->objfile_obstack,
1885                      dwarf2_per_objfile->n_type_comp_units
1886                      * sizeof (struct dwarf2_per_cu_data *));
1887
1888   sig_types_hash = allocate_signatured_type_table (objfile);
1889
1890   for (i = 0; i < elements; i += 3)
1891     {
1892       struct signatured_type *type_sig;
1893       ULONGEST offset, type_offset, signature;
1894       void **slot;
1895
1896       if (!extract_cu_value (bytes, &offset)
1897           || !extract_cu_value (bytes + 8, &type_offset))
1898         return 0;
1899       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1900       bytes += 3 * 8;
1901
1902       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1903                                  struct signatured_type);
1904       type_sig->signature = signature;
1905       type_sig->offset = offset;
1906       type_sig->type_offset = type_offset;
1907       type_sig->per_cu.from_debug_types = 1;
1908       type_sig->per_cu.offset = offset;
1909       type_sig->per_cu.objfile = objfile;
1910       type_sig->per_cu.v.quick
1911         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1912                           struct dwarf2_per_cu_quick_data);
1913
1914       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1915       *slot = type_sig;
1916
1917       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1918     }
1919
1920   dwarf2_per_objfile->signatured_types = sig_types_hash;
1921
1922   return 1;
1923 }
1924
1925 /* Read the address map data from the mapped index, and use it to
1926    populate the objfile's psymtabs_addrmap.  */
1927
1928 static void
1929 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1930 {
1931   const gdb_byte *iter, *end;
1932   struct obstack temp_obstack;
1933   struct addrmap *mutable_map;
1934   struct cleanup *cleanup;
1935   CORE_ADDR baseaddr;
1936
1937   obstack_init (&temp_obstack);
1938   cleanup = make_cleanup_obstack_free (&temp_obstack);
1939   mutable_map = addrmap_create_mutable (&temp_obstack);
1940
1941   iter = index->address_table;
1942   end = iter + index->address_table_size;
1943
1944   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1945
1946   while (iter < end)
1947     {
1948       ULONGEST hi, lo, cu_index;
1949       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1950       iter += 8;
1951       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1952       iter += 8;
1953       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1954       iter += 4;
1955       
1956       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1957                          dw2_get_cu (cu_index));
1958     }
1959
1960   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1961                                                     &objfile->objfile_obstack);
1962   do_cleanups (cleanup);
1963 }
1964
1965 /* The hash function for strings in the mapped index.  This is the
1966    same as the hashtab.c hash function, but we keep a separate copy to
1967    maintain control over the implementation.  This is necessary
1968    because the hash function is tied to the format of the mapped index
1969    file.  */
1970
1971 static hashval_t
1972 mapped_index_string_hash (const void *p)
1973 {
1974   const unsigned char *str = (const unsigned char *) p;
1975   hashval_t r = 0;
1976   unsigned char c;
1977
1978   while ((c = *str++) != 0)
1979     r = r * 67 + c - 113;
1980
1981   return r;
1982 }
1983
1984 /* Find a slot in the mapped index INDEX for the object named NAME.
1985    If NAME is found, set *VEC_OUT to point to the CU vector in the
1986    constant pool and return 1.  If NAME cannot be found, return 0.  */
1987
1988 static int
1989 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1990                           offset_type **vec_out)
1991 {
1992   offset_type hash = mapped_index_string_hash (name);
1993   offset_type slot, step;
1994
1995   slot = hash & (index->symbol_table_slots - 1);
1996   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
1997
1998   for (;;)
1999     {
2000       /* Convert a slot number to an offset into the table.  */
2001       offset_type i = 2 * slot;
2002       const char *str;
2003       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2004         return 0;
2005
2006       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2007       if (!strcmp (name, str))
2008         {
2009           *vec_out = (offset_type *) (index->constant_pool
2010                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2011           return 1;
2012         }
2013
2014       slot = (slot + step) & (index->symbol_table_slots - 1);
2015     }
2016 }
2017
2018 /* Read the index file.  If everything went ok, initialize the "quick"
2019    elements of all the CUs and return 1.  Otherwise, return 0.  */
2020
2021 static int
2022 dwarf2_read_index (struct objfile *objfile)
2023 {
2024   char *addr;
2025   struct mapped_index *map;
2026   offset_type *metadata;
2027   const gdb_byte *cu_list;
2028   const gdb_byte *types_list = NULL;
2029   offset_type version, cu_list_elements;
2030   offset_type types_list_elements = 0;
2031   int i;
2032
2033   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2034     return 0;
2035
2036   /* Older elfutils strip versions could keep the section in the main
2037      executable while splitting it for the separate debug info file.  */
2038   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2039        & SEC_HAS_CONTENTS) == 0)
2040     return 0;
2041
2042   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2043
2044   addr = dwarf2_per_objfile->gdb_index.buffer;
2045   /* Version check.  */
2046   version = MAYBE_SWAP (*(offset_type *) addr);
2047   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2048      causes the index to behave very poorly for certain requests.  Version 4
2049      contained incomplete addrmap.  So, it seems better to just ignore such
2050      indices.  */
2051   if (version < 4)
2052     return 0;
2053   /* Indexes with higher version than the one supported by GDB may be no
2054      longer backward compatible.  */
2055   if (version > 4)
2056     return 0;
2057
2058   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2059   map->total_size = dwarf2_per_objfile->gdb_index.size;
2060
2061   metadata = (offset_type *) (addr + sizeof (offset_type));
2062
2063   i = 0;
2064   cu_list = addr + MAYBE_SWAP (metadata[i]);
2065   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2066                       / 8);
2067   ++i;
2068
2069   types_list = addr + MAYBE_SWAP (metadata[i]);
2070   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2071                           - MAYBE_SWAP (metadata[i]))
2072                          / 8);
2073   ++i;
2074
2075   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2076   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2077                              - MAYBE_SWAP (metadata[i]));
2078   ++i;
2079
2080   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2081   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2082                               - MAYBE_SWAP (metadata[i]))
2083                              / (2 * sizeof (offset_type)));
2084   ++i;
2085
2086   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2087
2088   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2089     return 0;
2090
2091   if (types_list_elements
2092       && !create_signatured_type_table_from_index (objfile, types_list,
2093                                                    types_list_elements))
2094     return 0;
2095
2096   create_addrmap_from_index (objfile, map);
2097
2098   dwarf2_per_objfile->index_table = map;
2099   dwarf2_per_objfile->using_index = 1;
2100   dwarf2_per_objfile->quick_file_names_table =
2101     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2102
2103   return 1;
2104 }
2105
2106 /* A helper for the "quick" functions which sets the global
2107    dwarf2_per_objfile according to OBJFILE.  */
2108
2109 static void
2110 dw2_setup (struct objfile *objfile)
2111 {
2112   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2113   gdb_assert (dwarf2_per_objfile);
2114 }
2115
2116 /* A helper for the "quick" functions which attempts to read the line
2117    table for THIS_CU.  */
2118
2119 static struct quick_file_names *
2120 dw2_get_file_names (struct objfile *objfile,
2121                     struct dwarf2_per_cu_data *this_cu)
2122 {
2123   bfd *abfd = objfile->obfd;
2124   struct line_header *lh;
2125   struct attribute *attr;
2126   struct cleanup *cleanups;
2127   struct die_info *comp_unit_die;
2128   struct dwarf2_section_info* sec;
2129   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2130   int has_children, i;
2131   struct dwarf2_cu cu;
2132   unsigned int bytes_read, buffer_size;
2133   struct die_reader_specs reader_specs;
2134   char *name, *comp_dir;
2135   void **slot;
2136   struct quick_file_names *qfn;
2137   unsigned int line_offset;
2138
2139   if (this_cu->v.quick->file_names != NULL)
2140     return this_cu->v.quick->file_names;
2141   /* If we know there is no line data, no point in looking again.  */
2142   if (this_cu->v.quick->no_file_data)
2143     return NULL;
2144
2145   init_one_comp_unit (&cu, objfile);
2146   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2147
2148   if (this_cu->from_debug_types)
2149     sec = &dwarf2_per_objfile->types;
2150   else
2151     sec = &dwarf2_per_objfile->info;
2152   dwarf2_read_section (objfile, sec);
2153   buffer_size = sec->size;
2154   buffer = sec->buffer;
2155   info_ptr = buffer + this_cu->offset;
2156   beg_of_comp_unit = info_ptr;
2157
2158   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2159                                           buffer, buffer_size,
2160                                           abfd);
2161
2162   /* Complete the cu_header.  */
2163   cu.header.offset = beg_of_comp_unit - buffer;
2164   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2165
2166   this_cu->cu = &cu;
2167   cu.per_cu = this_cu;
2168
2169   dwarf2_read_abbrevs (abfd, &cu);
2170   make_cleanup (dwarf2_free_abbrev_table, &cu);
2171
2172   if (this_cu->from_debug_types)
2173     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2174   init_cu_die_reader (&reader_specs, &cu);
2175   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2176                  &has_children);
2177
2178   lh = NULL;
2179   slot = NULL;
2180   line_offset = 0;
2181   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2182   if (attr)
2183     {
2184       struct quick_file_names find_entry;
2185
2186       line_offset = DW_UNSND (attr);
2187
2188       /* We may have already read in this line header (TU line header sharing).
2189          If we have we're done.  */
2190       find_entry.offset = line_offset;
2191       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2192                              &find_entry, INSERT);
2193       if (*slot != NULL)
2194         {
2195           do_cleanups (cleanups);
2196           this_cu->v.quick->file_names = *slot;
2197           return *slot;
2198         }
2199
2200       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2201     }
2202   if (lh == NULL)
2203     {
2204       do_cleanups (cleanups);
2205       this_cu->v.quick->no_file_data = 1;
2206       return NULL;
2207     }
2208
2209   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2210   qfn->offset = line_offset;
2211   gdb_assert (slot != NULL);
2212   *slot = qfn;
2213
2214   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2215
2216   qfn->num_file_names = lh->num_file_names;
2217   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2218                                    lh->num_file_names * sizeof (char *));
2219   for (i = 0; i < lh->num_file_names; ++i)
2220     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2221   qfn->real_names = NULL;
2222
2223   free_line_header (lh);
2224   do_cleanups (cleanups);
2225
2226   this_cu->v.quick->file_names = qfn;
2227   return qfn;
2228 }
2229
2230 /* A helper for the "quick" functions which computes and caches the
2231    real path for a given file name from the line table.  */
2232
2233 static const char *
2234 dw2_get_real_path (struct objfile *objfile,
2235                    struct quick_file_names *qfn, int index)
2236 {
2237   if (qfn->real_names == NULL)
2238     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2239                                       qfn->num_file_names, sizeof (char *));
2240
2241   if (qfn->real_names[index] == NULL)
2242     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2243
2244   return qfn->real_names[index];
2245 }
2246
2247 static struct symtab *
2248 dw2_find_last_source_symtab (struct objfile *objfile)
2249 {
2250   int index;
2251
2252   dw2_setup (objfile);
2253   index = dwarf2_per_objfile->n_comp_units - 1;
2254   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2255 }
2256
2257 /* Traversal function for dw2_forget_cached_source_info.  */
2258
2259 static int
2260 dw2_free_cached_file_names (void **slot, void *info)
2261 {
2262   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2263
2264   if (file_data->real_names)
2265     {
2266       int i;
2267
2268       for (i = 0; i < file_data->num_file_names; ++i)
2269         {
2270           xfree ((void*) file_data->real_names[i]);
2271           file_data->real_names[i] = NULL;
2272         }
2273     }
2274
2275   return 1;
2276 }
2277
2278 static void
2279 dw2_forget_cached_source_info (struct objfile *objfile)
2280 {
2281   dw2_setup (objfile);
2282
2283   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2284                           dw2_free_cached_file_names, NULL);
2285 }
2286
2287 static int
2288 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2289                    const char *full_path, const char *real_path,
2290                    struct symtab **result)
2291 {
2292   int i;
2293   int check_basename = lbasename (name) == name;
2294   struct dwarf2_per_cu_data *base_cu = NULL;
2295
2296   dw2_setup (objfile);
2297
2298   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2299                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2300     {
2301       int j;
2302       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2303       struct quick_file_names *file_data;
2304
2305       if (per_cu->v.quick->symtab)
2306         continue;
2307
2308       file_data = dw2_get_file_names (objfile, per_cu);
2309       if (file_data == NULL)
2310         continue;
2311
2312       for (j = 0; j < file_data->num_file_names; ++j)
2313         {
2314           const char *this_name = file_data->file_names[j];
2315
2316           if (FILENAME_CMP (name, this_name) == 0)
2317             {
2318               *result = dw2_instantiate_symtab (objfile, per_cu);
2319               return 1;
2320             }
2321
2322           if (check_basename && ! base_cu
2323               && FILENAME_CMP (lbasename (this_name), name) == 0)
2324             base_cu = per_cu;
2325
2326           if (full_path != NULL)
2327             {
2328               const char *this_real_name = dw2_get_real_path (objfile,
2329                                                               file_data, j);
2330
2331               if (this_real_name != NULL
2332                   && FILENAME_CMP (full_path, this_real_name) == 0)
2333                 {
2334                   *result = dw2_instantiate_symtab (objfile, per_cu);
2335                   return 1;
2336                 }
2337             }
2338
2339           if (real_path != NULL)
2340             {
2341               const char *this_real_name = dw2_get_real_path (objfile,
2342                                                               file_data, j);
2343
2344               if (this_real_name != NULL
2345                   && FILENAME_CMP (real_path, this_real_name) == 0)
2346                 {
2347                   *result = dw2_instantiate_symtab (objfile, per_cu);
2348                   return 1;
2349                 }
2350             }
2351         }
2352     }
2353
2354   if (base_cu)
2355     {
2356       *result = dw2_instantiate_symtab (objfile, base_cu);
2357       return 1;
2358     }
2359
2360   return 0;
2361 }
2362
2363 static struct symtab *
2364 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2365                    const char *name, domain_enum domain)
2366 {
2367   /* We do all the work in the pre_expand_symtabs_matching hook
2368      instead.  */
2369   return NULL;
2370 }
2371
2372 /* A helper function that expands all symtabs that hold an object
2373    named NAME.  */
2374
2375 static void
2376 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2377 {
2378   dw2_setup (objfile);
2379
2380   /* index_table is NULL if OBJF_READNOW.  */
2381   if (dwarf2_per_objfile->index_table)
2382     {
2383       offset_type *vec;
2384
2385       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2386                                     name, &vec))
2387         {
2388           offset_type i, len = MAYBE_SWAP (*vec);
2389           for (i = 0; i < len; ++i)
2390             {
2391               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2392               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2393
2394               dw2_instantiate_symtab (objfile, per_cu);
2395             }
2396         }
2397     }
2398 }
2399
2400 static void
2401 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2402                                  int kind, const char *name,
2403                                  domain_enum domain)
2404 {
2405   dw2_do_expand_symtabs_matching (objfile, name);
2406 }
2407
2408 static void
2409 dw2_print_stats (struct objfile *objfile)
2410 {
2411   int i, count;
2412
2413   dw2_setup (objfile);
2414   count = 0;
2415   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2417     {
2418       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2419
2420       if (!per_cu->v.quick->symtab)
2421         ++count;
2422     }
2423   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2424 }
2425
2426 static void
2427 dw2_dump (struct objfile *objfile)
2428 {
2429   /* Nothing worth printing.  */
2430 }
2431
2432 static void
2433 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2434               struct section_offsets *delta)
2435 {
2436   /* There's nothing to relocate here.  */
2437 }
2438
2439 static void
2440 dw2_expand_symtabs_for_function (struct objfile *objfile,
2441                                  const char *func_name)
2442 {
2443   dw2_do_expand_symtabs_matching (objfile, func_name);
2444 }
2445
2446 static void
2447 dw2_expand_all_symtabs (struct objfile *objfile)
2448 {
2449   int i;
2450
2451   dw2_setup (objfile);
2452
2453   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2454                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2455     {
2456       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2457
2458       dw2_instantiate_symtab (objfile, per_cu);
2459     }
2460 }
2461
2462 static void
2463 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2464                                   const char *filename)
2465 {
2466   int i;
2467
2468   dw2_setup (objfile);
2469
2470   /* We don't need to consider type units here.
2471      This is only called for examining code, e.g. expand_line_sal.
2472      There can be an order of magnitude (or more) more type units
2473      than comp units, and we avoid them if we can.  */
2474
2475   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2476     {
2477       int j;
2478       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2479       struct quick_file_names *file_data;
2480
2481       if (per_cu->v.quick->symtab)
2482         continue;
2483
2484       file_data = dw2_get_file_names (objfile, per_cu);
2485       if (file_data == NULL)
2486         continue;
2487
2488       for (j = 0; j < file_data->num_file_names; ++j)
2489         {
2490           const char *this_name = file_data->file_names[j];
2491           if (FILENAME_CMP (this_name, filename) == 0)
2492             {
2493               dw2_instantiate_symtab (objfile, per_cu);
2494               break;
2495             }
2496         }
2497     }
2498 }
2499
2500 static const char *
2501 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2502 {
2503   struct dwarf2_per_cu_data *per_cu;
2504   offset_type *vec;
2505   struct quick_file_names *file_data;
2506
2507   dw2_setup (objfile);
2508
2509   /* index_table is NULL if OBJF_READNOW.  */
2510   if (!dwarf2_per_objfile->index_table)
2511     return NULL;
2512
2513   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2514                                  name, &vec))
2515     return NULL;
2516
2517   /* Note that this just looks at the very first one named NAME -- but
2518      actually we are looking for a function.  find_main_filename
2519      should be rewritten so that it doesn't require a custom hook.  It
2520      could just use the ordinary symbol tables.  */
2521   /* vec[0] is the length, which must always be >0.  */
2522   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2523
2524   file_data = dw2_get_file_names (objfile, per_cu);
2525   if (file_data == NULL)
2526     return NULL;
2527
2528   return file_data->file_names[file_data->num_file_names - 1];
2529 }
2530
2531 static void
2532 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2533                           struct objfile *objfile, int global,
2534                           int (*callback) (struct block *,
2535                                            struct symbol *, void *),
2536                           void *data, symbol_compare_ftype *match,
2537                           symbol_compare_ftype *ordered_compare)
2538 {
2539   /* Currently unimplemented; used for Ada.  The function can be called if the
2540      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2541      does not look for non-Ada symbols this function should just return.  */
2542 }
2543
2544 static void
2545 dw2_expand_symtabs_matching (struct objfile *objfile,
2546                              int (*file_matcher) (const char *, void *),
2547                              int (*name_matcher) (const char *, void *),
2548                              domain_enum kind,
2549                              void *data)
2550 {
2551   int i;
2552   offset_type iter;
2553   struct mapped_index *index;
2554
2555   dw2_setup (objfile);
2556
2557   /* index_table is NULL if OBJF_READNOW.  */
2558   if (!dwarf2_per_objfile->index_table)
2559     return;
2560   index = dwarf2_per_objfile->index_table;
2561
2562   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2563                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2564     {
2565       int j;
2566       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2567       struct quick_file_names *file_data;
2568
2569       per_cu->v.quick->mark = 0;
2570       if (per_cu->v.quick->symtab)
2571         continue;
2572
2573       file_data = dw2_get_file_names (objfile, per_cu);
2574       if (file_data == NULL)
2575         continue;
2576
2577       for (j = 0; j < file_data->num_file_names; ++j)
2578         {
2579           if (file_matcher (file_data->file_names[j], data))
2580             {
2581               per_cu->v.quick->mark = 1;
2582               break;
2583             }
2584         }
2585     }
2586
2587   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2588     {
2589       offset_type idx = 2 * iter;
2590       const char *name;
2591       offset_type *vec, vec_len, vec_idx;
2592
2593       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2594         continue;
2595
2596       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2597
2598       if (! (*name_matcher) (name, data))
2599         continue;
2600
2601       /* The name was matched, now expand corresponding CUs that were
2602          marked.  */
2603       vec = (offset_type *) (index->constant_pool
2604                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2605       vec_len = MAYBE_SWAP (vec[0]);
2606       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2607         {
2608           struct dwarf2_per_cu_data *per_cu;
2609
2610           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2611           if (per_cu->v.quick->mark)
2612             dw2_instantiate_symtab (objfile, per_cu);
2613         }
2614     }
2615 }
2616
2617 static struct symtab *
2618 dw2_find_pc_sect_symtab (struct objfile *objfile,
2619                          struct minimal_symbol *msymbol,
2620                          CORE_ADDR pc,
2621                          struct obj_section *section,
2622                          int warn_if_readin)
2623 {
2624   struct dwarf2_per_cu_data *data;
2625
2626   dw2_setup (objfile);
2627
2628   if (!objfile->psymtabs_addrmap)
2629     return NULL;
2630
2631   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2632   if (!data)
2633     return NULL;
2634
2635   if (warn_if_readin && data->v.quick->symtab)
2636     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2637              paddress (get_objfile_arch (objfile), pc));
2638
2639   return dw2_instantiate_symtab (objfile, data);
2640 }
2641
2642 static void
2643 dw2_map_symbol_names (struct objfile *objfile,
2644                       void (*fun) (const char *, void *),
2645                       void *data)
2646 {
2647   offset_type iter;
2648   struct mapped_index *index;
2649
2650   dw2_setup (objfile);
2651
2652   /* index_table is NULL if OBJF_READNOW.  */
2653   if (!dwarf2_per_objfile->index_table)
2654     return;
2655   index = dwarf2_per_objfile->index_table;
2656
2657   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2658     {
2659       offset_type idx = 2 * iter;
2660       const char *name;
2661       offset_type *vec, vec_len, vec_idx;
2662
2663       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2664         continue;
2665
2666       name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
2667
2668       (*fun) (name, data);
2669     }
2670 }
2671
2672 static void
2673 dw2_map_symbol_filenames (struct objfile *objfile,
2674                           void (*fun) (const char *, const char *, void *),
2675                           void *data)
2676 {
2677   int i;
2678
2679   dw2_setup (objfile);
2680
2681   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2682                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2683     {
2684       int j;
2685       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2686       struct quick_file_names *file_data;
2687
2688       if (per_cu->v.quick->symtab)
2689         continue;
2690
2691       file_data = dw2_get_file_names (objfile, per_cu);
2692       if (file_data == NULL)
2693         continue;
2694
2695       for (j = 0; j < file_data->num_file_names; ++j)
2696         {
2697           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2698                                                           j);
2699           (*fun) (file_data->file_names[j], this_real_name, data);
2700         }
2701     }
2702 }
2703
2704 static int
2705 dw2_has_symbols (struct objfile *objfile)
2706 {
2707   return 1;
2708 }
2709
2710 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2711 {
2712   dw2_has_symbols,
2713   dw2_find_last_source_symtab,
2714   dw2_forget_cached_source_info,
2715   dw2_lookup_symtab,
2716   dw2_lookup_symbol,
2717   dw2_pre_expand_symtabs_matching,
2718   dw2_print_stats,
2719   dw2_dump,
2720   dw2_relocate,
2721   dw2_expand_symtabs_for_function,
2722   dw2_expand_all_symtabs,
2723   dw2_expand_symtabs_with_filename,
2724   dw2_find_symbol_file,
2725   dw2_map_matching_symbols,
2726   dw2_expand_symtabs_matching,
2727   dw2_find_pc_sect_symtab,
2728   dw2_map_symbol_names,
2729   dw2_map_symbol_filenames
2730 };
2731
2732 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2733    file will use psymtabs, or 1 if using the GNU index.  */
2734
2735 int
2736 dwarf2_initialize_objfile (struct objfile *objfile)
2737 {
2738   /* If we're about to read full symbols, don't bother with the
2739      indices.  In this case we also don't care if some other debug
2740      format is making psymtabs, because they are all about to be
2741      expanded anyway.  */
2742   if ((objfile->flags & OBJF_READNOW))
2743     {
2744       int i;
2745
2746       dwarf2_per_objfile->using_index = 1;
2747       create_all_comp_units (objfile);
2748       create_debug_types_hash_table (objfile);
2749       dwarf2_per_objfile->quick_file_names_table =
2750         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2751
2752       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2753                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2754         {
2755           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2756
2757           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2758                                             struct dwarf2_per_cu_quick_data);
2759         }
2760
2761       /* Return 1 so that gdb sees the "quick" functions.  However,
2762          these functions will be no-ops because we will have expanded
2763          all symtabs.  */
2764       return 1;
2765     }
2766
2767   if (dwarf2_read_index (objfile))
2768     return 1;
2769
2770   return 0;
2771 }
2772
2773 \f
2774
2775 /* Build a partial symbol table.  */
2776
2777 void
2778 dwarf2_build_psymtabs (struct objfile *objfile)
2779 {
2780   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2781     {
2782       init_psymbol_list (objfile, 1024);
2783     }
2784
2785   dwarf2_build_psymtabs_hard (objfile);
2786 }
2787
2788 /* Return TRUE if OFFSET is within CU_HEADER.  */
2789
2790 static inline int
2791 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2792 {
2793   unsigned int bottom = cu_header->offset;
2794   unsigned int top = (cu_header->offset
2795                       + cu_header->length
2796                       + cu_header->initial_length_size);
2797
2798   return (offset >= bottom && offset < top);
2799 }
2800
2801 /* Read in the comp unit header information from the debug_info at info_ptr.
2802    NOTE: This leaves members offset, first_die_offset to be filled in
2803    by the caller.  */
2804
2805 static gdb_byte *
2806 read_comp_unit_head (struct comp_unit_head *cu_header,
2807                      gdb_byte *info_ptr, bfd *abfd)
2808 {
2809   int signed_addr;
2810   unsigned int bytes_read;
2811
2812   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2813   cu_header->initial_length_size = bytes_read;
2814   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2815   info_ptr += bytes_read;
2816   cu_header->version = read_2_bytes (abfd, info_ptr);
2817   info_ptr += 2;
2818   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2819                                           &bytes_read);
2820   info_ptr += bytes_read;
2821   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2822   info_ptr += 1;
2823   signed_addr = bfd_get_sign_extend_vma (abfd);
2824   if (signed_addr < 0)
2825     internal_error (__FILE__, __LINE__,
2826                     _("read_comp_unit_head: dwarf from non elf file"));
2827   cu_header->signed_addr_p = signed_addr;
2828
2829   return info_ptr;
2830 }
2831
2832 static gdb_byte *
2833 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2834                              gdb_byte *buffer, unsigned int buffer_size,
2835                              bfd *abfd)
2836 {
2837   gdb_byte *beg_of_comp_unit = info_ptr;
2838
2839   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2840
2841   if (header->version != 2 && header->version != 3 && header->version != 4)
2842     error (_("Dwarf Error: wrong version in compilation unit header "
2843            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2844            bfd_get_filename (abfd));
2845
2846   if (header->abbrev_offset
2847       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2848                               &dwarf2_per_objfile->abbrev))
2849     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2850            "(offset 0x%lx + 6) [in module %s]"),
2851            (long) header->abbrev_offset,
2852            (long) (beg_of_comp_unit - buffer),
2853            bfd_get_filename (abfd));
2854
2855   if (beg_of_comp_unit + header->length + header->initial_length_size
2856       > buffer + buffer_size)
2857     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2858            "(offset 0x%lx + 0) [in module %s]"),
2859            (long) header->length,
2860            (long) (beg_of_comp_unit - buffer),
2861            bfd_get_filename (abfd));
2862
2863   return info_ptr;
2864 }
2865
2866 /* Read in the types comp unit header information from .debug_types entry at
2867    types_ptr.  The result is a pointer to one past the end of the header.  */
2868
2869 static gdb_byte *
2870 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2871                           ULONGEST *signature,
2872                           gdb_byte *types_ptr, bfd *abfd)
2873 {
2874   gdb_byte *initial_types_ptr = types_ptr;
2875
2876   dwarf2_read_section (dwarf2_per_objfile->objfile,
2877                        &dwarf2_per_objfile->types);
2878   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2879
2880   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2881
2882   *signature = read_8_bytes (abfd, types_ptr);
2883   types_ptr += 8;
2884   types_ptr += cu_header->offset_size;
2885   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2886
2887   return types_ptr;
2888 }
2889
2890 /* Allocate a new partial symtab for file named NAME and mark this new
2891    partial symtab as being an include of PST.  */
2892
2893 static void
2894 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2895                                struct objfile *objfile)
2896 {
2897   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2898
2899   subpst->section_offsets = pst->section_offsets;
2900   subpst->textlow = 0;
2901   subpst->texthigh = 0;
2902
2903   subpst->dependencies = (struct partial_symtab **)
2904     obstack_alloc (&objfile->objfile_obstack,
2905                    sizeof (struct partial_symtab *));
2906   subpst->dependencies[0] = pst;
2907   subpst->number_of_dependencies = 1;
2908
2909   subpst->globals_offset = 0;
2910   subpst->n_global_syms = 0;
2911   subpst->statics_offset = 0;
2912   subpst->n_static_syms = 0;
2913   subpst->symtab = NULL;
2914   subpst->read_symtab = pst->read_symtab;
2915   subpst->readin = 0;
2916
2917   /* No private part is necessary for include psymtabs.  This property
2918      can be used to differentiate between such include psymtabs and
2919      the regular ones.  */
2920   subpst->read_symtab_private = NULL;
2921 }
2922
2923 /* Read the Line Number Program data and extract the list of files
2924    included by the source file represented by PST.  Build an include
2925    partial symtab for each of these included files.  */
2926
2927 static void
2928 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2929                                struct die_info *die,
2930                                struct partial_symtab *pst)
2931 {
2932   struct objfile *objfile = cu->objfile;
2933   bfd *abfd = objfile->obfd;
2934   struct line_header *lh = NULL;
2935   struct attribute *attr;
2936
2937   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2938   if (attr)
2939     {
2940       unsigned int line_offset = DW_UNSND (attr);
2941
2942       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2943     }
2944   if (lh == NULL)
2945     return;  /* No linetable, so no includes.  */
2946
2947   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2948   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2949
2950   free_line_header (lh);
2951 }
2952
2953 static hashval_t
2954 hash_type_signature (const void *item)
2955 {
2956   const struct signatured_type *type_sig = item;
2957
2958   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2959   return type_sig->signature;
2960 }
2961
2962 static int
2963 eq_type_signature (const void *item_lhs, const void *item_rhs)
2964 {
2965   const struct signatured_type *lhs = item_lhs;
2966   const struct signatured_type *rhs = item_rhs;
2967
2968   return lhs->signature == rhs->signature;
2969 }
2970
2971 /* Allocate a hash table for signatured types.  */
2972
2973 static htab_t
2974 allocate_signatured_type_table (struct objfile *objfile)
2975 {
2976   return htab_create_alloc_ex (41,
2977                                hash_type_signature,
2978                                eq_type_signature,
2979                                NULL,
2980                                &objfile->objfile_obstack,
2981                                hashtab_obstack_allocate,
2982                                dummy_obstack_deallocate);
2983 }
2984
2985 /* A helper function to add a signatured type CU to a list.  */
2986
2987 static int
2988 add_signatured_type_cu_to_list (void **slot, void *datum)
2989 {
2990   struct signatured_type *sigt = *slot;
2991   struct dwarf2_per_cu_data ***datap = datum;
2992
2993   **datap = &sigt->per_cu;
2994   ++*datap;
2995
2996   return 1;
2997 }
2998
2999 /* Create the hash table of all entries in the .debug_types section.
3000    The result is zero if there is an error (e.g. missing .debug_types section),
3001    otherwise non-zero.  */
3002
3003 static int
3004 create_debug_types_hash_table (struct objfile *objfile)
3005 {
3006   gdb_byte *info_ptr;
3007   htab_t types_htab;
3008   struct dwarf2_per_cu_data **iter;
3009
3010   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3011   info_ptr = dwarf2_per_objfile->types.buffer;
3012
3013   if (info_ptr == NULL)
3014     {
3015       dwarf2_per_objfile->signatured_types = NULL;
3016       return 0;
3017     }
3018
3019   types_htab = allocate_signatured_type_table (objfile);
3020
3021   if (dwarf2_die_debug)
3022     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3023
3024   while (info_ptr < dwarf2_per_objfile->types.buffer
3025          + dwarf2_per_objfile->types.size)
3026     {
3027       unsigned int offset;
3028       unsigned int offset_size;
3029       unsigned int type_offset;
3030       unsigned int length, initial_length_size;
3031       unsigned short version;
3032       ULONGEST signature;
3033       struct signatured_type *type_sig;
3034       void **slot;
3035       gdb_byte *ptr = info_ptr;
3036
3037       offset = ptr - dwarf2_per_objfile->types.buffer;
3038
3039       /* We need to read the type's signature in order to build the hash
3040          table, but we don't need to read anything else just yet.  */
3041
3042       /* Sanity check to ensure entire cu is present.  */
3043       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3044       if (ptr + length + initial_length_size
3045           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3046         {
3047           complaint (&symfile_complaints,
3048                      _("debug type entry runs off end "
3049                        "of `.debug_types' section, ignored"));
3050           break;
3051         }
3052
3053       offset_size = initial_length_size == 4 ? 4 : 8;
3054       ptr += initial_length_size;
3055       version = bfd_get_16 (objfile->obfd, ptr);
3056       ptr += 2;
3057       ptr += offset_size; /* abbrev offset */
3058       ptr += 1; /* address size */
3059       signature = bfd_get_64 (objfile->obfd, ptr);
3060       ptr += 8;
3061       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3062
3063       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3064       memset (type_sig, 0, sizeof (*type_sig));
3065       type_sig->signature = signature;
3066       type_sig->offset = offset;
3067       type_sig->type_offset = type_offset;
3068       type_sig->per_cu.objfile = objfile;
3069       type_sig->per_cu.from_debug_types = 1;
3070
3071       slot = htab_find_slot (types_htab, type_sig, INSERT);
3072       gdb_assert (slot != NULL);
3073       *slot = type_sig;
3074
3075       if (dwarf2_die_debug)
3076         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3077                             offset, phex (signature, sizeof (signature)));
3078
3079       info_ptr = info_ptr + initial_length_size + length;
3080     }
3081
3082   dwarf2_per_objfile->signatured_types = types_htab;
3083
3084   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3085   dwarf2_per_objfile->type_comp_units
3086     = obstack_alloc (&objfile->objfile_obstack,
3087                      dwarf2_per_objfile->n_type_comp_units
3088                      * sizeof (struct dwarf2_per_cu_data *));
3089   iter = &dwarf2_per_objfile->type_comp_units[0];
3090   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3091   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3092               == dwarf2_per_objfile->n_type_comp_units);
3093
3094   return 1;
3095 }
3096
3097 /* Lookup a signature based type.
3098    Returns NULL if SIG is not present in the table.  */
3099
3100 static struct signatured_type *
3101 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3102 {
3103   struct signatured_type find_entry, *entry;
3104
3105   if (dwarf2_per_objfile->signatured_types == NULL)
3106     {
3107       complaint (&symfile_complaints,
3108                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
3109       return 0;
3110     }
3111
3112   find_entry.signature = sig;
3113   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3114   return entry;
3115 }
3116
3117 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3118
3119 static void
3120 init_cu_die_reader (struct die_reader_specs *reader,
3121                     struct dwarf2_cu *cu)
3122 {
3123   reader->abfd = cu->objfile->obfd;
3124   reader->cu = cu;
3125   if (cu->per_cu->from_debug_types)
3126     {
3127       gdb_assert (dwarf2_per_objfile->types.readin);
3128       reader->buffer = dwarf2_per_objfile->types.buffer;
3129     }
3130   else
3131     {
3132       gdb_assert (dwarf2_per_objfile->info.readin);
3133       reader->buffer = dwarf2_per_objfile->info.buffer;
3134     }
3135 }
3136
3137 /* Find the base address of the compilation unit for range lists and
3138    location lists.  It will normally be specified by DW_AT_low_pc.
3139    In DWARF-3 draft 4, the base address could be overridden by
3140    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3141    compilation units with discontinuous ranges.  */
3142
3143 static void
3144 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3145 {
3146   struct attribute *attr;
3147
3148   cu->base_known = 0;
3149   cu->base_address = 0;
3150
3151   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3152   if (attr)
3153     {
3154       cu->base_address = DW_ADDR (attr);
3155       cu->base_known = 1;
3156     }
3157   else
3158     {
3159       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3160       if (attr)
3161         {
3162           cu->base_address = DW_ADDR (attr);
3163           cu->base_known = 1;
3164         }
3165     }
3166 }
3167
3168 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3169    to combine the common parts.
3170    Process a compilation unit for a psymtab.
3171    BUFFER is a pointer to the beginning of the dwarf section buffer,
3172    either .debug_info or debug_types.
3173    INFO_PTR is a pointer to the start of the CU.
3174    Returns a pointer to the next CU.  */
3175
3176 static gdb_byte *
3177 process_psymtab_comp_unit (struct objfile *objfile,
3178                            struct dwarf2_per_cu_data *this_cu,
3179                            gdb_byte *buffer, gdb_byte *info_ptr,
3180                            unsigned int buffer_size)
3181 {
3182   bfd *abfd = objfile->obfd;
3183   gdb_byte *beg_of_comp_unit = info_ptr;
3184   struct die_info *comp_unit_die;
3185   struct partial_symtab *pst;
3186   CORE_ADDR baseaddr;
3187   struct cleanup *back_to_inner;
3188   struct dwarf2_cu cu;
3189   int has_children, has_pc_info;
3190   struct attribute *attr;
3191   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3192   struct die_reader_specs reader_specs;
3193   const char *filename;
3194
3195   init_one_comp_unit (&cu, objfile);
3196   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3197
3198   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3199                                           buffer, buffer_size,
3200                                           abfd);
3201
3202   /* Complete the cu_header.  */
3203   cu.header.offset = beg_of_comp_unit - buffer;
3204   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3205
3206   cu.list_in_scope = &file_symbols;
3207
3208   /* If this compilation unit was already read in, free the
3209      cached copy in order to read it in again.  This is
3210      necessary because we skipped some symbols when we first
3211      read in the compilation unit (see load_partial_dies).
3212      This problem could be avoided, but the benefit is
3213      unclear.  */
3214   if (this_cu->cu != NULL)
3215     free_one_cached_comp_unit (this_cu->cu);
3216
3217   /* Note that this is a pointer to our stack frame, being
3218      added to a global data structure.  It will be cleaned up
3219      in free_stack_comp_unit when we finish with this
3220      compilation unit.  */
3221   this_cu->cu = &cu;
3222   cu.per_cu = this_cu;
3223
3224   /* Read the abbrevs for this compilation unit into a table.  */
3225   dwarf2_read_abbrevs (abfd, &cu);
3226   make_cleanup (dwarf2_free_abbrev_table, &cu);
3227
3228   /* Read the compilation unit die.  */
3229   if (this_cu->from_debug_types)
3230     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3231   init_cu_die_reader (&reader_specs, &cu);
3232   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3233                             &has_children);
3234
3235   if (this_cu->from_debug_types)
3236     {
3237       /* offset,length haven't been set yet for type units.  */
3238       this_cu->offset = cu.header.offset;
3239       this_cu->length = cu.header.length + cu.header.initial_length_size;
3240     }
3241   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3242     {
3243       info_ptr = (beg_of_comp_unit + cu.header.length
3244                   + cu.header.initial_length_size);
3245       do_cleanups (back_to_inner);
3246       return info_ptr;
3247     }
3248
3249   prepare_one_comp_unit (&cu, comp_unit_die);
3250
3251   /* Allocate a new partial symbol table structure.  */
3252   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3253   if (attr == NULL || !DW_STRING (attr))
3254     filename = "";
3255   else
3256     filename = DW_STRING (attr);
3257   pst = start_psymtab_common (objfile, objfile->section_offsets,
3258                               filename,
3259                               /* TEXTLOW and TEXTHIGH are set below.  */
3260                               0,
3261                               objfile->global_psymbols.next,
3262                               objfile->static_psymbols.next);
3263
3264   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3265   if (attr != NULL)
3266     pst->dirname = DW_STRING (attr);
3267
3268   pst->read_symtab_private = this_cu;
3269
3270   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3271
3272   /* Store the function that reads in the rest of the symbol table.  */
3273   pst->read_symtab = dwarf2_psymtab_to_symtab;
3274
3275   this_cu->v.psymtab = pst;
3276
3277   dwarf2_find_base_address (comp_unit_die, &cu);
3278
3279   /* Possibly set the default values of LOWPC and HIGHPC from
3280      `DW_AT_ranges'.  */
3281   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3282                                       &best_highpc, &cu, pst);
3283   if (has_pc_info == 1 && best_lowpc < best_highpc)
3284     /* Store the contiguous range if it is not empty; it can be empty for
3285        CUs with no code.  */
3286     addrmap_set_empty (objfile->psymtabs_addrmap,
3287                        best_lowpc + baseaddr,
3288                        best_highpc + baseaddr - 1, pst);
3289
3290   /* Check if comp unit has_children.
3291      If so, read the rest of the partial symbols from this comp unit.
3292      If not, there's no more debug_info for this comp unit.  */
3293   if (has_children)
3294     {
3295       struct partial_die_info *first_die;
3296       CORE_ADDR lowpc, highpc;
3297
3298       lowpc = ((CORE_ADDR) -1);
3299       highpc = ((CORE_ADDR) 0);
3300
3301       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3302
3303       scan_partial_symbols (first_die, &lowpc, &highpc,
3304                             ! has_pc_info, &cu);
3305
3306       /* If we didn't find a lowpc, set it to highpc to avoid
3307          complaints from `maint check'.  */
3308       if (lowpc == ((CORE_ADDR) -1))
3309         lowpc = highpc;
3310
3311       /* If the compilation unit didn't have an explicit address range,
3312          then use the information extracted from its child dies.  */
3313       if (! has_pc_info)
3314         {
3315           best_lowpc = lowpc;
3316           best_highpc = highpc;
3317         }
3318     }
3319   pst->textlow = best_lowpc + baseaddr;
3320   pst->texthigh = best_highpc + baseaddr;
3321
3322   pst->n_global_syms = objfile->global_psymbols.next -
3323     (objfile->global_psymbols.list + pst->globals_offset);
3324   pst->n_static_syms = objfile->static_psymbols.next -
3325     (objfile->static_psymbols.list + pst->statics_offset);
3326   sort_pst_symbols (pst);
3327
3328   info_ptr = (beg_of_comp_unit + cu.header.length
3329               + cu.header.initial_length_size);
3330
3331   if (this_cu->from_debug_types)
3332     {
3333       /* It's not clear we want to do anything with stmt lists here.
3334          Waiting to see what gcc ultimately does.  */
3335     }
3336   else
3337     {
3338       /* Get the list of files included in the current compilation unit,
3339          and build a psymtab for each of them.  */
3340       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3341     }
3342
3343   do_cleanups (back_to_inner);
3344
3345   return info_ptr;
3346 }
3347
3348 /* Traversal function for htab_traverse_noresize.
3349    Process one .debug_types comp-unit.  */
3350
3351 static int
3352 process_type_comp_unit (void **slot, void *info)
3353 {
3354   struct signatured_type *entry = (struct signatured_type *) *slot;
3355   struct objfile *objfile = (struct objfile *) info;
3356   struct dwarf2_per_cu_data *this_cu;
3357
3358   this_cu = &entry->per_cu;
3359
3360   gdb_assert (dwarf2_per_objfile->types.readin);
3361   process_psymtab_comp_unit (objfile, this_cu,
3362                              dwarf2_per_objfile->types.buffer,
3363                              dwarf2_per_objfile->types.buffer + entry->offset,
3364                              dwarf2_per_objfile->types.size);
3365
3366   return 1;
3367 }
3368
3369 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3370    Build partial symbol tables for the .debug_types comp-units.  */
3371
3372 static void
3373 build_type_psymtabs (struct objfile *objfile)
3374 {
3375   if (! create_debug_types_hash_table (objfile))
3376     return;
3377
3378   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3379                           process_type_comp_unit, objfile);
3380 }
3381
3382 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3383
3384 static void
3385 psymtabs_addrmap_cleanup (void *o)
3386 {
3387   struct objfile *objfile = o;
3388
3389   objfile->psymtabs_addrmap = NULL;
3390 }
3391
3392 /* Build the partial symbol table by doing a quick pass through the
3393    .debug_info and .debug_abbrev sections.  */
3394
3395 static void
3396 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3397 {
3398   gdb_byte *info_ptr;
3399   struct cleanup *back_to, *addrmap_cleanup;
3400   struct obstack temp_obstack;
3401
3402   dwarf2_per_objfile->reading_partial_symbols = 1;
3403
3404   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3405   info_ptr = dwarf2_per_objfile->info.buffer;
3406
3407   /* Any cached compilation units will be linked by the per-objfile
3408      read_in_chain.  Make sure to free them when we're done.  */
3409   back_to = make_cleanup (free_cached_comp_units, NULL);
3410
3411   build_type_psymtabs (objfile);
3412
3413   create_all_comp_units (objfile);
3414
3415   /* Create a temporary address map on a temporary obstack.  We later
3416      copy this to the final obstack.  */
3417   obstack_init (&temp_obstack);
3418   make_cleanup_obstack_free (&temp_obstack);
3419   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3420   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3421
3422   /* Since the objects we're extracting from .debug_info vary in
3423      length, only the individual functions to extract them (like
3424      read_comp_unit_head and load_partial_die) can really know whether
3425      the buffer is large enough to hold another complete object.
3426
3427      At the moment, they don't actually check that.  If .debug_info
3428      holds just one extra byte after the last compilation unit's dies,
3429      then read_comp_unit_head will happily read off the end of the
3430      buffer.  read_partial_die is similarly casual.  Those functions
3431      should be fixed.
3432
3433      For this loop condition, simply checking whether there's any data
3434      left at all should be sufficient.  */
3435
3436   while (info_ptr < (dwarf2_per_objfile->info.buffer
3437                      + dwarf2_per_objfile->info.size))
3438     {
3439       struct dwarf2_per_cu_data *this_cu;
3440
3441       this_cu = dwarf2_find_comp_unit (info_ptr
3442                                        - dwarf2_per_objfile->info.buffer,
3443                                        objfile);
3444
3445       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3446                                             dwarf2_per_objfile->info.buffer,
3447                                             info_ptr,
3448                                             dwarf2_per_objfile->info.size);
3449     }
3450
3451   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3452                                                     &objfile->objfile_obstack);
3453   discard_cleanups (addrmap_cleanup);
3454
3455   do_cleanups (back_to);
3456 }
3457
3458 /* Load the partial DIEs for a secondary CU into memory.  */
3459
3460 static void
3461 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3462                         struct objfile *objfile)
3463 {
3464   bfd *abfd = objfile->obfd;
3465   gdb_byte *info_ptr, *beg_of_comp_unit;
3466   struct die_info *comp_unit_die;
3467   struct dwarf2_cu *cu;
3468   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3469   int has_children;
3470   struct die_reader_specs reader_specs;
3471   int read_cu = 0;
3472
3473   gdb_assert (! this_cu->from_debug_types);
3474
3475   gdb_assert (dwarf2_per_objfile->info.readin);
3476   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3477   beg_of_comp_unit = info_ptr;
3478
3479   if (this_cu->cu == NULL)
3480     {
3481       cu = xmalloc (sizeof (*cu));
3482       init_one_comp_unit (cu, objfile);
3483
3484       read_cu = 1;
3485
3486       /* If an error occurs while loading, release our storage.  */
3487       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3488
3489       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3490                                               dwarf2_per_objfile->info.buffer,
3491                                               dwarf2_per_objfile->info.size,
3492                                               abfd);
3493
3494       /* Complete the cu_header.  */
3495       cu->header.offset = this_cu->offset;
3496       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3497
3498       /* Link this compilation unit into the compilation unit tree.  */
3499       this_cu->cu = cu;
3500       cu->per_cu = this_cu;
3501
3502       /* Link this CU into read_in_chain.  */
3503       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3504       dwarf2_per_objfile->read_in_chain = this_cu;
3505     }
3506   else
3507     {
3508       cu = this_cu->cu;
3509       info_ptr += cu->header.first_die_offset;
3510     }
3511
3512   /* Read the abbrevs for this compilation unit into a table.  */
3513   gdb_assert (cu->dwarf2_abbrevs == NULL);
3514   dwarf2_read_abbrevs (abfd, cu);
3515   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3516
3517   /* Read the compilation unit die.  */
3518   init_cu_die_reader (&reader_specs, cu);
3519   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3520                             &has_children);
3521
3522   prepare_one_comp_unit (cu, comp_unit_die);
3523
3524   /* Check if comp unit has_children.
3525      If so, read the rest of the partial symbols from this comp unit.
3526      If not, there's no more debug_info for this comp unit.  */
3527   if (has_children)
3528     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3529
3530   do_cleanups (free_abbrevs_cleanup);
3531
3532   if (read_cu)
3533     {
3534       /* We've successfully allocated this compilation unit.  Let our
3535          caller clean it up when finished with it.  */
3536       discard_cleanups (free_cu_cleanup);
3537     }
3538 }
3539
3540 /* Create a list of all compilation units in OBJFILE.  We do this only
3541    if an inter-comp-unit reference is found; presumably if there is one,
3542    there will be many, and one will occur early in the .debug_info section.
3543    So there's no point in building this list incrementally.  */
3544
3545 static void
3546 create_all_comp_units (struct objfile *objfile)
3547 {
3548   int n_allocated;
3549   int n_comp_units;
3550   struct dwarf2_per_cu_data **all_comp_units;
3551   gdb_byte *info_ptr;
3552
3553   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3554   info_ptr = dwarf2_per_objfile->info.buffer;
3555
3556   n_comp_units = 0;
3557   n_allocated = 10;
3558   all_comp_units = xmalloc (n_allocated
3559                             * sizeof (struct dwarf2_per_cu_data *));
3560
3561   while (info_ptr < dwarf2_per_objfile->info.buffer
3562          + dwarf2_per_objfile->info.size)
3563     {
3564       unsigned int length, initial_length_size;
3565       struct dwarf2_per_cu_data *this_cu;
3566       unsigned int offset;
3567
3568       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3569
3570       /* Read just enough information to find out where the next
3571          compilation unit is.  */
3572       length = read_initial_length (objfile->obfd, info_ptr,
3573                                     &initial_length_size);
3574
3575       /* Save the compilation unit for later lookup.  */
3576       this_cu = obstack_alloc (&objfile->objfile_obstack,
3577                                sizeof (struct dwarf2_per_cu_data));
3578       memset (this_cu, 0, sizeof (*this_cu));
3579       this_cu->offset = offset;
3580       this_cu->length = length + initial_length_size;
3581       this_cu->objfile = objfile;
3582
3583       if (n_comp_units == n_allocated)
3584         {
3585           n_allocated *= 2;
3586           all_comp_units = xrealloc (all_comp_units,
3587                                      n_allocated
3588                                      * sizeof (struct dwarf2_per_cu_data *));
3589         }
3590       all_comp_units[n_comp_units++] = this_cu;
3591
3592       info_ptr = info_ptr + this_cu->length;
3593     }
3594
3595   dwarf2_per_objfile->all_comp_units
3596     = obstack_alloc (&objfile->objfile_obstack,
3597                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3598   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3599           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3600   xfree (all_comp_units);
3601   dwarf2_per_objfile->n_comp_units = n_comp_units;
3602 }
3603
3604 /* Process all loaded DIEs for compilation unit CU, starting at
3605    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3606    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3607    DW_AT_ranges).  If NEED_PC is set, then this function will set
3608    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3609    and record the covered ranges in the addrmap.  */
3610
3611 static void
3612 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3613                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3614 {
3615   struct partial_die_info *pdi;
3616
3617   /* Now, march along the PDI's, descending into ones which have
3618      interesting children but skipping the children of the other ones,
3619      until we reach the end of the compilation unit.  */
3620
3621   pdi = first_die;
3622
3623   while (pdi != NULL)
3624     {
3625       fixup_partial_die (pdi, cu);
3626
3627       /* Anonymous namespaces or modules have no name but have interesting
3628          children, so we need to look at them.  Ditto for anonymous
3629          enums.  */
3630
3631       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3632           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3633         {
3634           switch (pdi->tag)
3635             {
3636             case DW_TAG_subprogram:
3637               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3638               break;
3639             case DW_TAG_constant:
3640             case DW_TAG_variable:
3641             case DW_TAG_typedef:
3642             case DW_TAG_union_type:
3643               if (!pdi->is_declaration)
3644                 {
3645                   add_partial_symbol (pdi, cu);
3646                 }
3647               break;
3648             case DW_TAG_class_type:
3649             case DW_TAG_interface_type:
3650             case DW_TAG_structure_type:
3651               if (!pdi->is_declaration)
3652                 {
3653                   add_partial_symbol (pdi, cu);
3654                 }
3655               break;
3656             case DW_TAG_enumeration_type:
3657               if (!pdi->is_declaration)
3658                 add_partial_enumeration (pdi, cu);
3659               break;
3660             case DW_TAG_base_type:
3661             case DW_TAG_subrange_type:
3662               /* File scope base type definitions are added to the partial
3663                  symbol table.  */
3664               add_partial_symbol (pdi, cu);
3665               break;
3666             case DW_TAG_namespace:
3667               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3668               break;
3669             case DW_TAG_module:
3670               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3671               break;
3672             default:
3673               break;
3674             }
3675         }
3676
3677       /* If the die has a sibling, skip to the sibling.  */
3678
3679       pdi = pdi->die_sibling;
3680     }
3681 }
3682
3683 /* Functions used to compute the fully scoped name of a partial DIE.
3684
3685    Normally, this is simple.  For C++, the parent DIE's fully scoped
3686    name is concatenated with "::" and the partial DIE's name.  For
3687    Java, the same thing occurs except that "." is used instead of "::".
3688    Enumerators are an exception; they use the scope of their parent
3689    enumeration type, i.e. the name of the enumeration type is not
3690    prepended to the enumerator.
3691
3692    There are two complexities.  One is DW_AT_specification; in this
3693    case "parent" means the parent of the target of the specification,
3694    instead of the direct parent of the DIE.  The other is compilers
3695    which do not emit DW_TAG_namespace; in this case we try to guess
3696    the fully qualified name of structure types from their members'
3697    linkage names.  This must be done using the DIE's children rather
3698    than the children of any DW_AT_specification target.  We only need
3699    to do this for structures at the top level, i.e. if the target of
3700    any DW_AT_specification (if any; otherwise the DIE itself) does not
3701    have a parent.  */
3702
3703 /* Compute the scope prefix associated with PDI's parent, in
3704    compilation unit CU.  The result will be allocated on CU's
3705    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3706    field.  NULL is returned if no prefix is necessary.  */
3707 static char *
3708 partial_die_parent_scope (struct partial_die_info *pdi,
3709                           struct dwarf2_cu *cu)
3710 {
3711   char *grandparent_scope;
3712   struct partial_die_info *parent, *real_pdi;
3713
3714   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3715      then this means the parent of the specification DIE.  */
3716
3717   real_pdi = pdi;
3718   while (real_pdi->has_specification)
3719     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3720
3721   parent = real_pdi->die_parent;
3722   if (parent == NULL)
3723     return NULL;
3724
3725   if (parent->scope_set)
3726     return parent->scope;
3727
3728   fixup_partial_die (parent, cu);
3729
3730   grandparent_scope = partial_die_parent_scope (parent, cu);
3731
3732   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3733      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3734      Work around this problem here.  */
3735   if (cu->language == language_cplus
3736       && parent->tag == DW_TAG_namespace
3737       && strcmp (parent->name, "::") == 0
3738       && grandparent_scope == NULL)
3739     {
3740       parent->scope = NULL;
3741       parent->scope_set = 1;
3742       return NULL;
3743     }
3744
3745   if (parent->tag == DW_TAG_namespace
3746       || parent->tag == DW_TAG_module
3747       || parent->tag == DW_TAG_structure_type
3748       || parent->tag == DW_TAG_class_type
3749       || parent->tag == DW_TAG_interface_type
3750       || parent->tag == DW_TAG_union_type
3751       || parent->tag == DW_TAG_enumeration_type)
3752     {
3753       if (grandparent_scope == NULL)
3754         parent->scope = parent->name;
3755       else
3756         parent->scope = typename_concat (&cu->comp_unit_obstack,
3757                                          grandparent_scope,
3758                                          parent->name, 0, cu);
3759     }
3760   else if (parent->tag == DW_TAG_enumerator)
3761     /* Enumerators should not get the name of the enumeration as a prefix.  */
3762     parent->scope = grandparent_scope;
3763   else
3764     {
3765       /* FIXME drow/2004-04-01: What should we be doing with
3766          function-local names?  For partial symbols, we should probably be
3767          ignoring them.  */
3768       complaint (&symfile_complaints,
3769                  _("unhandled containing DIE tag %d for DIE at %d"),
3770                  parent->tag, pdi->offset);
3771       parent->scope = grandparent_scope;
3772     }
3773
3774   parent->scope_set = 1;
3775   return parent->scope;
3776 }
3777
3778 /* Return the fully scoped name associated with PDI, from compilation unit
3779    CU.  The result will be allocated with malloc.  */
3780 static char *
3781 partial_die_full_name (struct partial_die_info *pdi,
3782                        struct dwarf2_cu *cu)
3783 {
3784   char *parent_scope;
3785
3786   /* If this is a template instantiation, we can not work out the
3787      template arguments from partial DIEs.  So, unfortunately, we have
3788      to go through the full DIEs.  At least any work we do building
3789      types here will be reused if full symbols are loaded later.  */
3790   if (pdi->has_template_arguments)
3791     {
3792       fixup_partial_die (pdi, cu);
3793
3794       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3795         {
3796           struct die_info *die;
3797           struct attribute attr;
3798           struct dwarf2_cu *ref_cu = cu;
3799
3800           attr.name = 0;
3801           attr.form = DW_FORM_ref_addr;
3802           attr.u.addr = pdi->offset;
3803           die = follow_die_ref (NULL, &attr, &ref_cu);
3804
3805           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3806         }
3807     }
3808
3809   parent_scope = partial_die_parent_scope (pdi, cu);
3810   if (parent_scope == NULL)
3811     return NULL;
3812   else
3813     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3814 }
3815
3816 static void
3817 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3818 {
3819   struct objfile *objfile = cu->objfile;
3820   CORE_ADDR addr = 0;
3821   char *actual_name = NULL;
3822   const struct partial_symbol *psym = NULL;
3823   CORE_ADDR baseaddr;
3824   int built_actual_name = 0;
3825
3826   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3827
3828   actual_name = partial_die_full_name (pdi, cu);
3829   if (actual_name)
3830     built_actual_name = 1;
3831
3832   if (actual_name == NULL)
3833     actual_name = pdi->name;
3834
3835   switch (pdi->tag)
3836     {
3837     case DW_TAG_subprogram:
3838       if (pdi->is_external || cu->language == language_ada)
3839         {
3840           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3841              of the global scope.  But in Ada, we want to be able to access
3842              nested procedures globally.  So all Ada subprograms are stored
3843              in the global scope.  */
3844           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3845              mst_text, objfile); */
3846           add_psymbol_to_list (actual_name, strlen (actual_name),
3847                                built_actual_name,
3848                                VAR_DOMAIN, LOC_BLOCK,
3849                                &objfile->global_psymbols,
3850                                0, pdi->lowpc + baseaddr,
3851                                cu->language, objfile);
3852         }
3853       else
3854         {
3855           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3856              mst_file_text, objfile); */
3857           add_psymbol_to_list (actual_name, strlen (actual_name),
3858                                built_actual_name,
3859                                VAR_DOMAIN, LOC_BLOCK,
3860                                &objfile->static_psymbols,
3861                                0, pdi->lowpc + baseaddr,
3862                                cu->language, objfile);
3863         }
3864       break;
3865     case DW_TAG_constant:
3866       {
3867         struct psymbol_allocation_list *list;
3868
3869         if (pdi->is_external)
3870           list = &objfile->global_psymbols;
3871         else
3872           list = &objfile->static_psymbols;
3873         add_psymbol_to_list (actual_name, strlen (actual_name),
3874                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3875                              list, 0, 0, cu->language, objfile);
3876       }
3877       break;
3878     case DW_TAG_variable:
3879       if (pdi->locdesc)
3880         addr = decode_locdesc (pdi->locdesc, cu);
3881
3882       if (pdi->locdesc
3883           && addr == 0
3884           && !dwarf2_per_objfile->has_section_at_zero)
3885         {
3886           /* A global or static variable may also have been stripped
3887              out by the linker if unused, in which case its address
3888              will be nullified; do not add such variables into partial
3889              symbol table then.  */
3890         }
3891       else if (pdi->is_external)
3892         {
3893           /* Global Variable.
3894              Don't enter into the minimal symbol tables as there is
3895              a minimal symbol table entry from the ELF symbols already.
3896              Enter into partial symbol table if it has a location
3897              descriptor or a type.
3898              If the location descriptor is missing, new_symbol will create
3899              a LOC_UNRESOLVED symbol, the address of the variable will then
3900              be determined from the minimal symbol table whenever the variable
3901              is referenced.
3902              The address for the partial symbol table entry is not
3903              used by GDB, but it comes in handy for debugging partial symbol
3904              table building.  */
3905
3906           if (pdi->locdesc || pdi->has_type)
3907             add_psymbol_to_list (actual_name, strlen (actual_name),
3908                                  built_actual_name,
3909                                  VAR_DOMAIN, LOC_STATIC,
3910                                  &objfile->global_psymbols,
3911                                  0, addr + baseaddr,
3912                                  cu->language, objfile);
3913         }
3914       else
3915         {
3916           /* Static Variable.  Skip symbols without location descriptors.  */
3917           if (pdi->locdesc == NULL)
3918             {
3919               if (built_actual_name)
3920                 xfree (actual_name);
3921               return;
3922             }
3923           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3924              mst_file_data, objfile); */
3925           add_psymbol_to_list (actual_name, strlen (actual_name),
3926                                built_actual_name,
3927                                VAR_DOMAIN, LOC_STATIC,
3928                                &objfile->static_psymbols,
3929                                0, addr + baseaddr,
3930                                cu->language, objfile);
3931         }
3932       break;
3933     case DW_TAG_typedef:
3934     case DW_TAG_base_type:
3935     case DW_TAG_subrange_type:
3936       add_psymbol_to_list (actual_name, strlen (actual_name),
3937                            built_actual_name,
3938                            VAR_DOMAIN, LOC_TYPEDEF,
3939                            &objfile->static_psymbols,
3940                            0, (CORE_ADDR) 0, cu->language, objfile);
3941       break;
3942     case DW_TAG_namespace:
3943       add_psymbol_to_list (actual_name, strlen (actual_name),
3944                            built_actual_name,
3945                            VAR_DOMAIN, LOC_TYPEDEF,
3946                            &objfile->global_psymbols,
3947                            0, (CORE_ADDR) 0, cu->language, objfile);
3948       break;
3949     case DW_TAG_class_type:
3950     case DW_TAG_interface_type:
3951     case DW_TAG_structure_type:
3952     case DW_TAG_union_type:
3953     case DW_TAG_enumeration_type:
3954       /* Skip external references.  The DWARF standard says in the section
3955          about "Structure, Union, and Class Type Entries": "An incomplete
3956          structure, union or class type is represented by a structure,
3957          union or class entry that does not have a byte size attribute
3958          and that has a DW_AT_declaration attribute."  */
3959       if (!pdi->has_byte_size && pdi->is_declaration)
3960         {
3961           if (built_actual_name)
3962             xfree (actual_name);
3963           return;
3964         }
3965
3966       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3967          static vs. global.  */
3968       add_psymbol_to_list (actual_name, strlen (actual_name),
3969                            built_actual_name,
3970                            STRUCT_DOMAIN, LOC_TYPEDEF,
3971                            (cu->language == language_cplus
3972                             || cu->language == language_java)
3973                            ? &objfile->global_psymbols
3974                            : &objfile->static_psymbols,
3975                            0, (CORE_ADDR) 0, cu->language, objfile);
3976
3977       break;
3978     case DW_TAG_enumerator:
3979       add_psymbol_to_list (actual_name, strlen (actual_name),
3980                            built_actual_name,
3981                            VAR_DOMAIN, LOC_CONST,
3982                            (cu->language == language_cplus
3983                             || cu->language == language_java)
3984                            ? &objfile->global_psymbols
3985                            : &objfile->static_psymbols,
3986                            0, (CORE_ADDR) 0, cu->language, objfile);
3987       break;
3988     default:
3989       break;
3990     }
3991
3992   if (built_actual_name)
3993     xfree (actual_name);
3994 }
3995
3996 /* Read a partial die corresponding to a namespace; also, add a symbol
3997    corresponding to that namespace to the symbol table.  NAMESPACE is
3998    the name of the enclosing namespace.  */
3999
4000 static void
4001 add_partial_namespace (struct partial_die_info *pdi,
4002                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4003                        int need_pc, struct dwarf2_cu *cu)
4004 {
4005   /* Add a symbol for the namespace.  */
4006
4007   add_partial_symbol (pdi, cu);
4008
4009   /* Now scan partial symbols in that namespace.  */
4010
4011   if (pdi->has_children)
4012     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4013 }
4014
4015 /* Read a partial die corresponding to a Fortran module.  */
4016
4017 static void
4018 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4019                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4020 {
4021   /* Now scan partial symbols in that module.  */
4022
4023   if (pdi->has_children)
4024     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4025 }
4026
4027 /* Read a partial die corresponding to a subprogram and create a partial
4028    symbol for that subprogram.  When the CU language allows it, this
4029    routine also defines a partial symbol for each nested subprogram
4030    that this subprogram contains.
4031
4032    DIE my also be a lexical block, in which case we simply search
4033    recursively for suprograms defined inside that lexical block.
4034    Again, this is only performed when the CU language allows this
4035    type of definitions.  */
4036
4037 static void
4038 add_partial_subprogram (struct partial_die_info *pdi,
4039                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4040                         int need_pc, struct dwarf2_cu *cu)
4041 {
4042   if (pdi->tag == DW_TAG_subprogram)
4043     {
4044       if (pdi->has_pc_info)
4045         {
4046           if (pdi->lowpc < *lowpc)
4047             *lowpc = pdi->lowpc;
4048           if (pdi->highpc > *highpc)
4049             *highpc = pdi->highpc;
4050           if (need_pc)
4051             {
4052               CORE_ADDR baseaddr;
4053               struct objfile *objfile = cu->objfile;
4054
4055               baseaddr = ANOFFSET (objfile->section_offsets,
4056                                    SECT_OFF_TEXT (objfile));
4057               addrmap_set_empty (objfile->psymtabs_addrmap,
4058                                  pdi->lowpc + baseaddr,
4059                                  pdi->highpc - 1 + baseaddr,
4060                                  cu->per_cu->v.psymtab);
4061             }
4062           if (!pdi->is_declaration)
4063             /* Ignore subprogram DIEs that do not have a name, they are
4064                illegal.  Do not emit a complaint at this point, we will
4065                do so when we convert this psymtab into a symtab.  */
4066             if (pdi->name)
4067               add_partial_symbol (pdi, cu);
4068         }
4069     }
4070
4071   if (! pdi->has_children)
4072     return;
4073
4074   if (cu->language == language_ada)
4075     {
4076       pdi = pdi->die_child;
4077       while (pdi != NULL)
4078         {
4079           fixup_partial_die (pdi, cu);
4080           if (pdi->tag == DW_TAG_subprogram
4081               || pdi->tag == DW_TAG_lexical_block)
4082             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4083           pdi = pdi->die_sibling;
4084         }
4085     }
4086 }
4087
4088 /* Read a partial die corresponding to an enumeration type.  */
4089
4090 static void
4091 add_partial_enumeration (struct partial_die_info *enum_pdi,
4092                          struct dwarf2_cu *cu)
4093 {
4094   struct partial_die_info *pdi;
4095
4096   if (enum_pdi->name != NULL)
4097     add_partial_symbol (enum_pdi, cu);
4098
4099   pdi = enum_pdi->die_child;
4100   while (pdi)
4101     {
4102       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4103         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4104       else
4105         add_partial_symbol (pdi, cu);
4106       pdi = pdi->die_sibling;
4107     }
4108 }
4109
4110 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4111    Return the corresponding abbrev, or NULL if the number is zero (indicating
4112    an empty DIE).  In either case *BYTES_READ will be set to the length of
4113    the initial number.  */
4114
4115 static struct abbrev_info *
4116 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4117                  struct dwarf2_cu *cu)
4118 {
4119   bfd *abfd = cu->objfile->obfd;
4120   unsigned int abbrev_number;
4121   struct abbrev_info *abbrev;
4122
4123   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4124
4125   if (abbrev_number == 0)
4126     return NULL;
4127
4128   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4129   if (!abbrev)
4130     {
4131       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4132              abbrev_number, bfd_get_filename (abfd));
4133     }
4134
4135   return abbrev;
4136 }
4137
4138 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4139    Returns a pointer to the end of a series of DIEs, terminated by an empty
4140    DIE.  Any children of the skipped DIEs will also be skipped.  */
4141
4142 static gdb_byte *
4143 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4144 {
4145   struct abbrev_info *abbrev;
4146   unsigned int bytes_read;
4147
4148   while (1)
4149     {
4150       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4151       if (abbrev == NULL)
4152         return info_ptr + bytes_read;
4153       else
4154         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4155     }
4156 }
4157
4158 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4159    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4160    abbrev corresponding to that skipped uleb128 should be passed in
4161    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4162    children.  */
4163
4164 static gdb_byte *
4165 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4166               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4167 {
4168   unsigned int bytes_read;
4169   struct attribute attr;
4170   bfd *abfd = cu->objfile->obfd;
4171   unsigned int form, i;
4172
4173   for (i = 0; i < abbrev->num_attrs; i++)
4174     {
4175       /* The only abbrev we care about is DW_AT_sibling.  */
4176       if (abbrev->attrs[i].name == DW_AT_sibling)
4177         {
4178           read_attribute (&attr, &abbrev->attrs[i],
4179                           abfd, info_ptr, cu);
4180           if (attr.form == DW_FORM_ref_addr)
4181             complaint (&symfile_complaints,
4182                        _("ignoring absolute DW_AT_sibling"));
4183           else
4184             return buffer + dwarf2_get_ref_die_offset (&attr);
4185         }
4186
4187       /* If it isn't DW_AT_sibling, skip this attribute.  */
4188       form = abbrev->attrs[i].form;
4189     skip_attribute:
4190       switch (form)
4191         {
4192         case DW_FORM_ref_addr:
4193           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4194              and later it is offset sized.  */
4195           if (cu->header.version == 2)
4196             info_ptr += cu->header.addr_size;
4197           else
4198             info_ptr += cu->header.offset_size;
4199           break;
4200         case DW_FORM_addr:
4201           info_ptr += cu->header.addr_size;
4202           break;
4203         case DW_FORM_data1:
4204         case DW_FORM_ref1:
4205         case DW_FORM_flag:
4206           info_ptr += 1;
4207           break;
4208         case DW_FORM_flag_present:
4209           break;
4210         case DW_FORM_data2:
4211         case DW_FORM_ref2:
4212           info_ptr += 2;
4213           break;
4214         case DW_FORM_data4:
4215         case DW_FORM_ref4:
4216           info_ptr += 4;
4217           break;
4218         case DW_FORM_data8:
4219         case DW_FORM_ref8:
4220         case DW_FORM_sig8:
4221           info_ptr += 8;
4222           break;
4223         case DW_FORM_string:
4224           read_direct_string (abfd, info_ptr, &bytes_read);
4225           info_ptr += bytes_read;
4226           break;
4227         case DW_FORM_sec_offset:
4228         case DW_FORM_strp:
4229           info_ptr += cu->header.offset_size;
4230           break;
4231         case DW_FORM_exprloc:
4232         case DW_FORM_block:
4233           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4234           info_ptr += bytes_read;
4235           break;
4236         case DW_FORM_block1:
4237           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4238           break;
4239         case DW_FORM_block2:
4240           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4241           break;
4242         case DW_FORM_block4:
4243           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4244           break;
4245         case DW_FORM_sdata:
4246         case DW_FORM_udata:
4247         case DW_FORM_ref_udata:
4248           info_ptr = skip_leb128 (abfd, info_ptr);
4249           break;
4250         case DW_FORM_indirect:
4251           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4252           info_ptr += bytes_read;
4253           /* We need to continue parsing from here, so just go back to
4254              the top.  */
4255           goto skip_attribute;
4256
4257         default:
4258           error (_("Dwarf Error: Cannot handle %s "
4259                    "in DWARF reader [in module %s]"),
4260                  dwarf_form_name (form),
4261                  bfd_get_filename (abfd));
4262         }
4263     }
4264
4265   if (abbrev->has_children)
4266     return skip_children (buffer, info_ptr, cu);
4267   else
4268     return info_ptr;
4269 }
4270
4271 /* Locate ORIG_PDI's sibling.
4272    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4273    in BUFFER.  */
4274
4275 static gdb_byte *
4276 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4277                     gdb_byte *buffer, gdb_byte *info_ptr,
4278                     bfd *abfd, struct dwarf2_cu *cu)
4279 {
4280   /* Do we know the sibling already?  */
4281
4282   if (orig_pdi->sibling)
4283     return orig_pdi->sibling;
4284
4285   /* Are there any children to deal with?  */
4286
4287   if (!orig_pdi->has_children)
4288     return info_ptr;
4289
4290   /* Skip the children the long way.  */
4291
4292   return skip_children (buffer, info_ptr, cu);
4293 }
4294
4295 /* Expand this partial symbol table into a full symbol table.  */
4296
4297 static void
4298 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4299 {
4300   if (pst != NULL)
4301     {
4302       if (pst->readin)
4303         {
4304           warning (_("bug: psymtab for %s is already read in."),
4305                    pst->filename);
4306         }
4307       else
4308         {
4309           if (info_verbose)
4310             {
4311               printf_filtered (_("Reading in symbols for %s..."),
4312                                pst->filename);
4313               gdb_flush (gdb_stdout);
4314             }
4315
4316           /* Restore our global data.  */
4317           dwarf2_per_objfile = objfile_data (pst->objfile,
4318                                              dwarf2_objfile_data_key);
4319
4320           /* If this psymtab is constructed from a debug-only objfile, the
4321              has_section_at_zero flag will not necessarily be correct.  We
4322              can get the correct value for this flag by looking at the data
4323              associated with the (presumably stripped) associated objfile.  */
4324           if (pst->objfile->separate_debug_objfile_backlink)
4325             {
4326               struct dwarf2_per_objfile *dpo_backlink
4327                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4328                                 dwarf2_objfile_data_key);
4329
4330               dwarf2_per_objfile->has_section_at_zero
4331                 = dpo_backlink->has_section_at_zero;
4332             }
4333
4334           dwarf2_per_objfile->reading_partial_symbols = 0;
4335
4336           psymtab_to_symtab_1 (pst);
4337
4338           /* Finish up the debug error message.  */
4339           if (info_verbose)
4340             printf_filtered (_("done.\n"));
4341         }
4342     }
4343 }
4344
4345 /* Add PER_CU to the queue.  */
4346
4347 static void
4348 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4349 {
4350   struct dwarf2_queue_item *item;
4351
4352   per_cu->queued = 1;
4353   item = xmalloc (sizeof (*item));
4354   item->per_cu = per_cu;
4355   item->next = NULL;
4356
4357   if (dwarf2_queue == NULL)
4358     dwarf2_queue = item;
4359   else
4360     dwarf2_queue_tail->next = item;
4361
4362   dwarf2_queue_tail = item;
4363 }
4364
4365 /* Process the queue.  */
4366
4367 static void
4368 process_queue (struct objfile *objfile)
4369 {
4370   struct dwarf2_queue_item *item, *next_item;
4371
4372   /* The queue starts out with one item, but following a DIE reference
4373      may load a new CU, adding it to the end of the queue.  */
4374   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4375     {
4376       if (dwarf2_per_objfile->using_index
4377           ? !item->per_cu->v.quick->symtab
4378           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4379         process_full_comp_unit (item->per_cu);
4380
4381       item->per_cu->queued = 0;
4382       next_item = item->next;
4383       xfree (item);
4384     }
4385
4386   dwarf2_queue_tail = NULL;
4387 }
4388
4389 /* Free all allocated queue entries.  This function only releases anything if
4390    an error was thrown; if the queue was processed then it would have been
4391    freed as we went along.  */
4392
4393 static void
4394 dwarf2_release_queue (void *dummy)
4395 {
4396   struct dwarf2_queue_item *item, *last;
4397
4398   item = dwarf2_queue;
4399   while (item)
4400     {
4401       /* Anything still marked queued is likely to be in an
4402          inconsistent state, so discard it.  */
4403       if (item->per_cu->queued)
4404         {
4405           if (item->per_cu->cu != NULL)
4406             free_one_cached_comp_unit (item->per_cu->cu);
4407           item->per_cu->queued = 0;
4408         }
4409
4410       last = item;
4411       item = item->next;
4412       xfree (last);
4413     }
4414
4415   dwarf2_queue = dwarf2_queue_tail = NULL;
4416 }
4417
4418 /* Read in full symbols for PST, and anything it depends on.  */
4419
4420 static void
4421 psymtab_to_symtab_1 (struct partial_symtab *pst)
4422 {
4423   struct dwarf2_per_cu_data *per_cu;
4424   struct cleanup *back_to;
4425   int i;
4426
4427   for (i = 0; i < pst->number_of_dependencies; i++)
4428     if (!pst->dependencies[i]->readin)
4429       {
4430         /* Inform about additional files that need to be read in.  */
4431         if (info_verbose)
4432           {
4433             /* FIXME: i18n: Need to make this a single string.  */
4434             fputs_filtered (" ", gdb_stdout);
4435             wrap_here ("");
4436             fputs_filtered ("and ", gdb_stdout);
4437             wrap_here ("");
4438             printf_filtered ("%s...", pst->dependencies[i]->filename);
4439             wrap_here ("");     /* Flush output.  */
4440             gdb_flush (gdb_stdout);
4441           }
4442         psymtab_to_symtab_1 (pst->dependencies[i]);
4443       }
4444
4445   per_cu = pst->read_symtab_private;
4446
4447   if (per_cu == NULL)
4448     {
4449       /* It's an include file, no symbols to read for it.
4450          Everything is in the parent symtab.  */
4451       pst->readin = 1;
4452       return;
4453     }
4454
4455   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4456 }
4457
4458 /* Load the DIEs associated with PER_CU into memory.  */
4459
4460 static void
4461 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4462                      struct objfile *objfile)
4463 {
4464   bfd *abfd = objfile->obfd;
4465   struct dwarf2_cu *cu;
4466   unsigned int offset;
4467   gdb_byte *info_ptr, *beg_of_comp_unit;
4468   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4469   struct attribute *attr;
4470   int read_cu = 0;
4471
4472   gdb_assert (! per_cu->from_debug_types);
4473
4474   /* Set local variables from the partial symbol table info.  */
4475   offset = per_cu->offset;
4476
4477   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4478   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4479   beg_of_comp_unit = info_ptr;
4480
4481   if (per_cu->cu == NULL)
4482     {
4483       cu = xmalloc (sizeof (*cu));
4484       init_one_comp_unit (cu, objfile);
4485
4486       read_cu = 1;
4487
4488       /* If an error occurs while loading, release our storage.  */
4489       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4490
4491       /* Read in the comp_unit header.  */
4492       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4493
4494       /* Complete the cu_header.  */
4495       cu->header.offset = offset;
4496       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4497
4498       /* Read the abbrevs for this compilation unit.  */
4499       dwarf2_read_abbrevs (abfd, cu);
4500       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4501
4502       /* Link this compilation unit into the compilation unit tree.  */
4503       per_cu->cu = cu;
4504       cu->per_cu = per_cu;
4505
4506       /* Link this CU into read_in_chain.  */
4507       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4508       dwarf2_per_objfile->read_in_chain = per_cu;
4509     }
4510   else
4511     {
4512       cu = per_cu->cu;
4513       info_ptr += cu->header.first_die_offset;
4514     }
4515
4516   cu->dies = read_comp_unit (info_ptr, cu);
4517
4518   /* We try not to read any attributes in this function, because not
4519      all objfiles needed for references have been loaded yet, and symbol
4520      table processing isn't initialized.  But we have to set the CU language,
4521      or we won't be able to build types correctly.  */
4522   prepare_one_comp_unit (cu, cu->dies);
4523
4524   /* Similarly, if we do not read the producer, we can not apply
4525      producer-specific interpretation.  */
4526   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4527   if (attr)
4528     cu->producer = DW_STRING (attr);
4529
4530   if (read_cu)
4531     {
4532       do_cleanups (free_abbrevs_cleanup);
4533
4534       /* We've successfully allocated this compilation unit.  Let our
4535          caller clean it up when finished with it.  */
4536       discard_cleanups (free_cu_cleanup);
4537     }
4538 }
4539
4540 /* Add a DIE to the delayed physname list.  */
4541
4542 static void
4543 add_to_method_list (struct type *type, int fnfield_index, int index,
4544                     const char *name, struct die_info *die,
4545                     struct dwarf2_cu *cu)
4546 {
4547   struct delayed_method_info mi;
4548   mi.type = type;
4549   mi.fnfield_index = fnfield_index;
4550   mi.index = index;
4551   mi.name = name;
4552   mi.die = die;
4553   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4554 }
4555
4556 /* A cleanup for freeing the delayed method list.  */
4557
4558 static void
4559 free_delayed_list (void *ptr)
4560 {
4561   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4562   if (cu->method_list != NULL)
4563     {
4564       VEC_free (delayed_method_info, cu->method_list);
4565       cu->method_list = NULL;
4566     }
4567 }
4568
4569 /* Compute the physnames of any methods on the CU's method list.
4570
4571    The computation of method physnames is delayed in order to avoid the
4572    (bad) condition that one of the method's formal parameters is of an as yet
4573    incomplete type.  */
4574
4575 static void
4576 compute_delayed_physnames (struct dwarf2_cu *cu)
4577 {
4578   int i;
4579   struct delayed_method_info *mi;
4580   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4581     {
4582       char *physname;
4583       struct fn_fieldlist *fn_flp
4584         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4585       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4586       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4587     }
4588 }
4589
4590 /* Generate full symbol information for PST and CU, whose DIEs have
4591    already been loaded into memory.  */
4592
4593 static void
4594 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4595 {
4596   struct dwarf2_cu *cu = per_cu->cu;
4597   struct objfile *objfile = per_cu->objfile;
4598   CORE_ADDR lowpc, highpc;
4599   struct symtab *symtab;
4600   struct cleanup *back_to, *delayed_list_cleanup;
4601   CORE_ADDR baseaddr;
4602
4603   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4604
4605   buildsym_init ();
4606   back_to = make_cleanup (really_free_pendings, NULL);
4607   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4608
4609   cu->list_in_scope = &file_symbols;
4610
4611   dwarf2_find_base_address (cu->dies, cu);
4612
4613   /* Do line number decoding in read_file_scope () */
4614   process_die (cu->dies, cu);
4615
4616   /* Now that we have processed all the DIEs in the CU, all the types 
4617      should be complete, and it should now be safe to compute all of the
4618      physnames.  */
4619   compute_delayed_physnames (cu);
4620   do_cleanups (delayed_list_cleanup);
4621
4622   /* Some compilers don't define a DW_AT_high_pc attribute for the
4623      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4624      it, by scanning the DIE's below the compilation unit.  */
4625   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4626
4627   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4628
4629   /* Set symtab language to language from DW_AT_language.
4630      If the compilation is from a C file generated by language preprocessors,
4631      do not set the language if it was already deduced by start_subfile.  */
4632   if (symtab != NULL
4633       && !(cu->language == language_c && symtab->language != language_c))
4634     {
4635       symtab->language = cu->language;
4636     }
4637
4638   if (dwarf2_per_objfile->using_index)
4639     per_cu->v.quick->symtab = symtab;
4640   else
4641     {
4642       struct partial_symtab *pst = per_cu->v.psymtab;
4643       pst->symtab = symtab;
4644       pst->readin = 1;
4645     }
4646
4647   do_cleanups (back_to);
4648 }
4649
4650 /* Process a die and its children.  */
4651
4652 static void
4653 process_die (struct die_info *die, struct dwarf2_cu *cu)
4654 {
4655   switch (die->tag)
4656     {
4657     case DW_TAG_padding:
4658       break;
4659     case DW_TAG_compile_unit:
4660       read_file_scope (die, cu);
4661       break;
4662     case DW_TAG_type_unit:
4663       read_type_unit_scope (die, cu);
4664       break;
4665     case DW_TAG_subprogram:
4666     case DW_TAG_inlined_subroutine:
4667       read_func_scope (die, cu);
4668       break;
4669     case DW_TAG_lexical_block:
4670     case DW_TAG_try_block:
4671     case DW_TAG_catch_block:
4672       read_lexical_block_scope (die, cu);
4673       break;
4674     case DW_TAG_class_type:
4675     case DW_TAG_interface_type:
4676     case DW_TAG_structure_type:
4677     case DW_TAG_union_type:
4678       process_structure_scope (die, cu);
4679       break;
4680     case DW_TAG_enumeration_type:
4681       process_enumeration_scope (die, cu);
4682       break;
4683
4684     /* These dies have a type, but processing them does not create
4685        a symbol or recurse to process the children.  Therefore we can
4686        read them on-demand through read_type_die.  */
4687     case DW_TAG_subroutine_type:
4688     case DW_TAG_set_type:
4689     case DW_TAG_array_type:
4690     case DW_TAG_pointer_type:
4691     case DW_TAG_ptr_to_member_type:
4692     case DW_TAG_reference_type:
4693     case DW_TAG_string_type:
4694       break;
4695
4696     case DW_TAG_base_type:
4697     case DW_TAG_subrange_type:
4698     case DW_TAG_typedef:
4699       /* Add a typedef symbol for the type definition, if it has a
4700          DW_AT_name.  */
4701       new_symbol (die, read_type_die (die, cu), cu);
4702       break;
4703     case DW_TAG_common_block:
4704       read_common_block (die, cu);
4705       break;
4706     case DW_TAG_common_inclusion:
4707       break;
4708     case DW_TAG_namespace:
4709       processing_has_namespace_info = 1;
4710       read_namespace (die, cu);
4711       break;
4712     case DW_TAG_module:
4713       processing_has_namespace_info = 1;
4714       read_module (die, cu);
4715       break;
4716     case DW_TAG_imported_declaration:
4717     case DW_TAG_imported_module:
4718       processing_has_namespace_info = 1;
4719       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4720                                  || cu->language != language_fortran))
4721         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4722                    dwarf_tag_name (die->tag));
4723       read_import_statement (die, cu);
4724       break;
4725     default:
4726       new_symbol (die, NULL, cu);
4727       break;
4728     }
4729 }
4730
4731 /* A helper function for dwarf2_compute_name which determines whether DIE
4732    needs to have the name of the scope prepended to the name listed in the
4733    die.  */
4734
4735 static int
4736 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4737 {
4738   struct attribute *attr;
4739
4740   switch (die->tag)
4741     {
4742     case DW_TAG_namespace:
4743     case DW_TAG_typedef:
4744     case DW_TAG_class_type:
4745     case DW_TAG_interface_type:
4746     case DW_TAG_structure_type:
4747     case DW_TAG_union_type:
4748     case DW_TAG_enumeration_type:
4749     case DW_TAG_enumerator:
4750     case DW_TAG_subprogram:
4751     case DW_TAG_member:
4752       return 1;
4753
4754     case DW_TAG_variable:
4755     case DW_TAG_constant:
4756       /* We only need to prefix "globally" visible variables.  These include
4757          any variable marked with DW_AT_external or any variable that
4758          lives in a namespace.  [Variables in anonymous namespaces
4759          require prefixing, but they are not DW_AT_external.]  */
4760
4761       if (dwarf2_attr (die, DW_AT_specification, cu))
4762         {
4763           struct dwarf2_cu *spec_cu = cu;
4764
4765           return die_needs_namespace (die_specification (die, &spec_cu),
4766                                       spec_cu);
4767         }
4768
4769       attr = dwarf2_attr (die, DW_AT_external, cu);
4770       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4771           && die->parent->tag != DW_TAG_module)
4772         return 0;
4773       /* A variable in a lexical block of some kind does not need a
4774          namespace, even though in C++ such variables may be external
4775          and have a mangled name.  */
4776       if (die->parent->tag ==  DW_TAG_lexical_block
4777           || die->parent->tag ==  DW_TAG_try_block
4778           || die->parent->tag ==  DW_TAG_catch_block
4779           || die->parent->tag == DW_TAG_subprogram)
4780         return 0;
4781       return 1;
4782
4783     default:
4784       return 0;
4785     }
4786 }
4787
4788 /* Retrieve the last character from a mem_file.  */
4789
4790 static void
4791 do_ui_file_peek_last (void *object, const char *buffer, long length)
4792 {
4793   char *last_char_p = (char *) object;
4794
4795   if (length > 0)
4796     *last_char_p = buffer[length - 1];
4797 }
4798
4799 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4800    compute the physname for the object, which include a method's
4801    formal parameters (C++/Java) and return type (Java).
4802
4803    For Ada, return the DIE's linkage name rather than the fully qualified
4804    name.  PHYSNAME is ignored..
4805
4806    The result is allocated on the objfile_obstack and canonicalized.  */
4807
4808 static const char *
4809 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4810                      int physname)
4811 {
4812   if (name == NULL)
4813     name = dwarf2_name (die, cu);
4814
4815   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4816      compute it by typename_concat inside GDB.  */
4817   if (cu->language == language_ada
4818       || (cu->language == language_fortran && physname))
4819     {
4820       /* For Ada unit, we prefer the linkage name over the name, as
4821          the former contains the exported name, which the user expects
4822          to be able to reference.  Ideally, we want the user to be able
4823          to reference this entity using either natural or linkage name,
4824          but we haven't started looking at this enhancement yet.  */
4825       struct attribute *attr;
4826
4827       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4828       if (attr == NULL)
4829         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4830       if (attr && DW_STRING (attr))
4831         return DW_STRING (attr);
4832     }
4833
4834   /* These are the only languages we know how to qualify names in.  */
4835   if (name != NULL
4836       && (cu->language == language_cplus || cu->language == language_java
4837           || cu->language == language_fortran))
4838     {
4839       if (die_needs_namespace (die, cu))
4840         {
4841           long length;
4842           char *prefix;
4843           struct ui_file *buf;
4844
4845           prefix = determine_prefix (die, cu);
4846           buf = mem_fileopen ();
4847           if (*prefix != '\0')
4848             {
4849               char *prefixed_name = typename_concat (NULL, prefix, name,
4850                                                      physname, cu);
4851
4852               fputs_unfiltered (prefixed_name, buf);
4853               xfree (prefixed_name);
4854             }
4855           else
4856             fputs_unfiltered (name, buf);
4857
4858           /* Template parameters may be specified in the DIE's DW_AT_name, or
4859              as children with DW_TAG_template_type_param or
4860              DW_TAG_value_type_param.  If the latter, add them to the name
4861              here.  If the name already has template parameters, then
4862              skip this step; some versions of GCC emit both, and
4863              it is more efficient to use the pre-computed name.
4864
4865              Something to keep in mind about this process: it is very
4866              unlikely, or in some cases downright impossible, to produce
4867              something that will match the mangled name of a function.
4868              If the definition of the function has the same debug info,
4869              we should be able to match up with it anyway.  But fallbacks
4870              using the minimal symbol, for instance to find a method
4871              implemented in a stripped copy of libstdc++, will not work.
4872              If we do not have debug info for the definition, we will have to
4873              match them up some other way.
4874
4875              When we do name matching there is a related problem with function
4876              templates; two instantiated function templates are allowed to
4877              differ only by their return types, which we do not add here.  */
4878
4879           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4880             {
4881               struct attribute *attr;
4882               struct die_info *child;
4883               int first = 1;
4884
4885               die->building_fullname = 1;
4886
4887               for (child = die->child; child != NULL; child = child->sibling)
4888                 {
4889                   struct type *type;
4890                   long value;
4891                   gdb_byte *bytes;
4892                   struct dwarf2_locexpr_baton *baton;
4893                   struct value *v;
4894
4895                   if (child->tag != DW_TAG_template_type_param
4896                       && child->tag != DW_TAG_template_value_param)
4897                     continue;
4898
4899                   if (first)
4900                     {
4901                       fputs_unfiltered ("<", buf);
4902                       first = 0;
4903                     }
4904                   else
4905                     fputs_unfiltered (", ", buf);
4906
4907                   attr = dwarf2_attr (child, DW_AT_type, cu);
4908                   if (attr == NULL)
4909                     {
4910                       complaint (&symfile_complaints,
4911                                  _("template parameter missing DW_AT_type"));
4912                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4913                       continue;
4914                     }
4915                   type = die_type (child, cu);
4916
4917                   if (child->tag == DW_TAG_template_type_param)
4918                     {
4919                       c_print_type (type, "", buf, -1, 0);
4920                       continue;
4921                     }
4922
4923                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4924                   if (attr == NULL)
4925                     {
4926                       complaint (&symfile_complaints,
4927                                  _("template parameter missing "
4928                                    "DW_AT_const_value"));
4929                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4930                       continue;
4931                     }
4932
4933                   dwarf2_const_value_attr (attr, type, name,
4934                                            &cu->comp_unit_obstack, cu,
4935                                            &value, &bytes, &baton);
4936
4937                   if (TYPE_NOSIGN (type))
4938                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4939                        changed, this can use value_print instead.  */
4940                     c_printchar (value, type, buf);
4941                   else
4942                     {
4943                       struct value_print_options opts;
4944
4945                       if (baton != NULL)
4946                         v = dwarf2_evaluate_loc_desc (type, NULL,
4947                                                       baton->data,
4948                                                       baton->size,
4949                                                       baton->per_cu);
4950                       else if (bytes != NULL)
4951                         {
4952                           v = allocate_value (type);
4953                           memcpy (value_contents_writeable (v), bytes,
4954                                   TYPE_LENGTH (type));
4955                         }
4956                       else
4957                         v = value_from_longest (type, value);
4958
4959                       /* Specify decimal so that we do not depend on
4960                          the radix.  */
4961                       get_formatted_print_options (&opts, 'd');
4962                       opts.raw = 1;
4963                       value_print (v, buf, &opts);
4964                       release_value (v);
4965                       value_free (v);
4966                     }
4967                 }
4968
4969               die->building_fullname = 0;
4970
4971               if (!first)
4972                 {
4973                   /* Close the argument list, with a space if necessary
4974                      (nested templates).  */
4975                   char last_char = '\0';
4976                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4977                   if (last_char == '>')
4978                     fputs_unfiltered (" >", buf);
4979                   else
4980                     fputs_unfiltered (">", buf);
4981                 }
4982             }
4983
4984           /* For Java and C++ methods, append formal parameter type
4985              information, if PHYSNAME.  */
4986
4987           if (physname && die->tag == DW_TAG_subprogram
4988               && (cu->language == language_cplus
4989                   || cu->language == language_java))
4990             {
4991               struct type *type = read_type_die (die, cu);
4992
4993               c_type_print_args (type, buf, 0, cu->language);
4994
4995               if (cu->language == language_java)
4996                 {
4997                   /* For java, we must append the return type to method
4998                      names.  */
4999                   if (die->tag == DW_TAG_subprogram)
5000                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5001                                      0, 0);
5002                 }
5003               else if (cu->language == language_cplus)
5004                 {
5005                   /* Assume that an artificial first parameter is
5006                      "this", but do not crash if it is not.  RealView
5007                      marks unnamed (and thus unused) parameters as
5008                      artificial; there is no way to differentiate
5009                      the two cases.  */
5010                   if (TYPE_NFIELDS (type) > 0
5011                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5012                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5013                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5014                                                                         0))))
5015                     fputs_unfiltered (" const", buf);
5016                 }
5017             }
5018
5019           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5020                                        &length);
5021           ui_file_delete (buf);
5022
5023           if (cu->language == language_cplus)
5024             {
5025               char *cname
5026                 = dwarf2_canonicalize_name (name, cu,
5027                                             &cu->objfile->objfile_obstack);
5028
5029               if (cname != NULL)
5030                 name = cname;
5031             }
5032         }
5033     }
5034
5035   return name;
5036 }
5037
5038 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5039    If scope qualifiers are appropriate they will be added.  The result
5040    will be allocated on the objfile_obstack, or NULL if the DIE does
5041    not have a name.  NAME may either be from a previous call to
5042    dwarf2_name or NULL.
5043
5044    The output string will be canonicalized (if C++/Java).  */
5045
5046 static const char *
5047 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5048 {
5049   return dwarf2_compute_name (name, die, cu, 0);
5050 }
5051
5052 /* Construct a physname for the given DIE in CU.  NAME may either be
5053    from a previous call to dwarf2_name or NULL.  The result will be
5054    allocated on the objfile_objstack or NULL if the DIE does not have a
5055    name.
5056
5057    The output string will be canonicalized (if C++/Java).  */
5058
5059 static const char *
5060 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5061 {
5062   return dwarf2_compute_name (name, die, cu, 1);
5063 }
5064
5065 /* Read the import statement specified by the given die and record it.  */
5066
5067 static void
5068 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5069 {
5070   struct attribute *import_attr;
5071   struct die_info *imported_die;
5072   struct dwarf2_cu *imported_cu;
5073   const char *imported_name;
5074   const char *imported_name_prefix;
5075   const char *canonical_name;
5076   const char *import_alias;
5077   const char *imported_declaration = NULL;
5078   const char *import_prefix;
5079
5080   char *temp;
5081
5082   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5083   if (import_attr == NULL)
5084     {
5085       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5086                  dwarf_tag_name (die->tag));
5087       return;
5088     }
5089
5090   imported_cu = cu;
5091   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5092   imported_name = dwarf2_name (imported_die, imported_cu);
5093   if (imported_name == NULL)
5094     {
5095       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5096
5097         The import in the following code:
5098         namespace A
5099           {
5100             typedef int B;
5101           }
5102
5103         int main ()
5104           {
5105             using A::B;
5106             B b;
5107             return b;
5108           }
5109
5110         ...
5111          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5112             <52>   DW_AT_decl_file   : 1
5113             <53>   DW_AT_decl_line   : 6
5114             <54>   DW_AT_import      : <0x75>
5115          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5116             <59>   DW_AT_name        : B
5117             <5b>   DW_AT_decl_file   : 1
5118             <5c>   DW_AT_decl_line   : 2
5119             <5d>   DW_AT_type        : <0x6e>
5120         ...
5121          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5122             <76>   DW_AT_byte_size   : 4
5123             <77>   DW_AT_encoding    : 5        (signed)
5124
5125         imports the wrong die ( 0x75 instead of 0x58 ).
5126         This case will be ignored until the gcc bug is fixed.  */
5127       return;
5128     }
5129
5130   /* Figure out the local name after import.  */
5131   import_alias = dwarf2_name (die, cu);
5132
5133   /* Figure out where the statement is being imported to.  */
5134   import_prefix = determine_prefix (die, cu);
5135
5136   /* Figure out what the scope of the imported die is and prepend it
5137      to the name of the imported die.  */
5138   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5139
5140   if (imported_die->tag != DW_TAG_namespace
5141       && imported_die->tag != DW_TAG_module)
5142     {
5143       imported_declaration = imported_name;
5144       canonical_name = imported_name_prefix;
5145     }
5146   else if (strlen (imported_name_prefix) > 0)
5147     {
5148       temp = alloca (strlen (imported_name_prefix)
5149                      + 2 + strlen (imported_name) + 1);
5150       strcpy (temp, imported_name_prefix);
5151       strcat (temp, "::");
5152       strcat (temp, imported_name);
5153       canonical_name = temp;
5154     }
5155   else
5156     canonical_name = imported_name;
5157
5158   cp_add_using_directive (import_prefix,
5159                           canonical_name,
5160                           import_alias,
5161                           imported_declaration,
5162                           &cu->objfile->objfile_obstack);
5163 }
5164
5165 static void
5166 initialize_cu_func_list (struct dwarf2_cu *cu)
5167 {
5168   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5169 }
5170
5171 /* Cleanup function for read_file_scope.  */
5172
5173 static void
5174 free_cu_line_header (void *arg)
5175 {
5176   struct dwarf2_cu *cu = arg;
5177
5178   free_line_header (cu->line_header);
5179   cu->line_header = NULL;
5180 }
5181
5182 static void
5183 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5184                          char **name, char **comp_dir)
5185 {
5186   struct attribute *attr;
5187
5188   *name = NULL;
5189   *comp_dir = NULL;
5190
5191   /* Find the filename.  Do not use dwarf2_name here, since the filename
5192      is not a source language identifier.  */
5193   attr = dwarf2_attr (die, DW_AT_name, cu);
5194   if (attr)
5195     {
5196       *name = DW_STRING (attr);
5197     }
5198
5199   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5200   if (attr)
5201     *comp_dir = DW_STRING (attr);
5202   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5203     {
5204       *comp_dir = ldirname (*name);
5205       if (*comp_dir != NULL)
5206         make_cleanup (xfree, *comp_dir);
5207     }
5208   if (*comp_dir != NULL)
5209     {
5210       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5211          directory, get rid of it.  */
5212       char *cp = strchr (*comp_dir, ':');
5213
5214       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5215         *comp_dir = cp + 1;
5216     }
5217
5218   if (*name == NULL)
5219     *name = "<unknown>";
5220 }
5221
5222 /* Process DW_TAG_compile_unit.  */
5223
5224 static void
5225 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5226 {
5227   struct objfile *objfile = cu->objfile;
5228   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5229   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5230   CORE_ADDR highpc = ((CORE_ADDR) 0);
5231   struct attribute *attr;
5232   char *name = NULL;
5233   char *comp_dir = NULL;
5234   struct die_info *child_die;
5235   bfd *abfd = objfile->obfd;
5236   struct line_header *line_header = 0;
5237   CORE_ADDR baseaddr;
5238
5239   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5240
5241   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5242
5243   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5244      from finish_block.  */
5245   if (lowpc == ((CORE_ADDR) -1))
5246     lowpc = highpc;
5247   lowpc += baseaddr;
5248   highpc += baseaddr;
5249
5250   find_file_and_directory (die, cu, &name, &comp_dir);
5251
5252   attr = dwarf2_attr (die, DW_AT_language, cu);
5253   if (attr)
5254     {
5255       set_cu_language (DW_UNSND (attr), cu);
5256     }
5257
5258   attr = dwarf2_attr (die, DW_AT_producer, cu);
5259   if (attr)
5260     cu->producer = DW_STRING (attr);
5261
5262   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5263      standardised yet.  As a workaround for the language detection we fall
5264      back to the DW_AT_producer string.  */
5265   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5266     cu->language = language_opencl;
5267
5268   /* We assume that we're processing GCC output.  */
5269   processing_gcc_compilation = 2;
5270
5271   processing_has_namespace_info = 0;
5272
5273   start_symtab (name, comp_dir, lowpc);
5274   record_debugformat ("DWARF 2");
5275   record_producer (cu->producer);
5276
5277   initialize_cu_func_list (cu);
5278
5279   /* Decode line number information if present.  We do this before
5280      processing child DIEs, so that the line header table is available
5281      for DW_AT_decl_file.  */
5282   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5283   if (attr)
5284     {
5285       unsigned int line_offset = DW_UNSND (attr);
5286       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5287       if (line_header)
5288         {
5289           cu->line_header = line_header;
5290           make_cleanup (free_cu_line_header, cu);
5291           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5292         }
5293     }
5294
5295   /* Process all dies in compilation unit.  */
5296   if (die->child != NULL)
5297     {
5298       child_die = die->child;
5299       while (child_die && child_die->tag)
5300         {
5301           process_die (child_die, cu);
5302           child_die = sibling_die (child_die);
5303         }
5304     }
5305
5306   /* Decode macro information, if present.  Dwarf 2 macro information
5307      refers to information in the line number info statement program
5308      header, so we can only read it if we've read the header
5309      successfully.  */
5310   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5311   if (attr && line_header)
5312     {
5313       unsigned int macro_offset = DW_UNSND (attr);
5314
5315       dwarf_decode_macros (line_header, macro_offset,
5316                            comp_dir, abfd, cu);
5317     }
5318   do_cleanups (back_to);
5319 }
5320
5321 /* Process DW_TAG_type_unit.
5322    For TUs we want to skip the first top level sibling if it's not the
5323    actual type being defined by this TU.  In this case the first top
5324    level sibling is there to provide context only.  */
5325
5326 static void
5327 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5328 {
5329   struct objfile *objfile = cu->objfile;
5330   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5331   CORE_ADDR lowpc;
5332   struct attribute *attr;
5333   char *name = NULL;
5334   char *comp_dir = NULL;
5335   struct die_info *child_die;
5336   bfd *abfd = objfile->obfd;
5337
5338   /* start_symtab needs a low pc, but we don't really have one.
5339      Do what read_file_scope would do in the absence of such info.  */
5340   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5341
5342   /* Find the filename.  Do not use dwarf2_name here, since the filename
5343      is not a source language identifier.  */
5344   attr = dwarf2_attr (die, DW_AT_name, cu);
5345   if (attr)
5346     name = DW_STRING (attr);
5347
5348   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5349   if (attr)
5350     comp_dir = DW_STRING (attr);
5351   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5352     {
5353       comp_dir = ldirname (name);
5354       if (comp_dir != NULL)
5355         make_cleanup (xfree, comp_dir);
5356     }
5357
5358   if (name == NULL)
5359     name = "<unknown>";
5360
5361   attr = dwarf2_attr (die, DW_AT_language, cu);
5362   if (attr)
5363     set_cu_language (DW_UNSND (attr), cu);
5364
5365   /* This isn't technically needed today.  It is done for symmetry
5366      with read_file_scope.  */
5367   attr = dwarf2_attr (die, DW_AT_producer, cu);
5368   if (attr)
5369     cu->producer = DW_STRING (attr);
5370
5371   /* We assume that we're processing GCC output.  */
5372   processing_gcc_compilation = 2;
5373
5374   processing_has_namespace_info = 0;
5375
5376   start_symtab (name, comp_dir, lowpc);
5377   record_debugformat ("DWARF 2");
5378   record_producer (cu->producer);
5379
5380   /* Process the dies in the type unit.  */
5381   if (die->child == NULL)
5382     {
5383       dump_die_for_error (die);
5384       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5385              bfd_get_filename (abfd));
5386     }
5387
5388   child_die = die->child;
5389
5390   while (child_die && child_die->tag)
5391     {
5392       process_die (child_die, cu);
5393
5394       child_die = sibling_die (child_die);
5395     }
5396
5397   do_cleanups (back_to);
5398 }
5399
5400 static void
5401 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5402                      struct dwarf2_cu *cu)
5403 {
5404   struct function_range *thisfn;
5405
5406   thisfn = (struct function_range *)
5407     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5408   thisfn->name = name;
5409   thisfn->lowpc = lowpc;
5410   thisfn->highpc = highpc;
5411   thisfn->seen_line = 0;
5412   thisfn->next = NULL;
5413
5414   if (cu->last_fn == NULL)
5415       cu->first_fn = thisfn;
5416   else
5417       cu->last_fn->next = thisfn;
5418
5419   cu->last_fn = thisfn;
5420 }
5421
5422 /* qsort helper for inherit_abstract_dies.  */
5423
5424 static int
5425 unsigned_int_compar (const void *ap, const void *bp)
5426 {
5427   unsigned int a = *(unsigned int *) ap;
5428   unsigned int b = *(unsigned int *) bp;
5429
5430   return (a > b) - (b > a);
5431 }
5432
5433 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5434    Inherit only the children of the DW_AT_abstract_origin DIE not being
5435    already referenced by DW_AT_abstract_origin from the children of the
5436    current DIE.  */
5437
5438 static void
5439 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5440 {
5441   struct die_info *child_die;
5442   unsigned die_children_count;
5443   /* CU offsets which were referenced by children of the current DIE.  */
5444   unsigned *offsets;
5445   unsigned *offsets_end, *offsetp;
5446   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5447   struct die_info *origin_die;
5448   /* Iterator of the ORIGIN_DIE children.  */
5449   struct die_info *origin_child_die;
5450   struct cleanup *cleanups;
5451   struct attribute *attr;
5452   struct dwarf2_cu *origin_cu;
5453   struct pending **origin_previous_list_in_scope;
5454
5455   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5456   if (!attr)
5457     return;
5458
5459   /* Note that following die references may follow to a die in a
5460      different cu.  */
5461
5462   origin_cu = cu;
5463   origin_die = follow_die_ref (die, attr, &origin_cu);
5464
5465   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5466      symbols in.  */
5467   origin_previous_list_in_scope = origin_cu->list_in_scope;
5468   origin_cu->list_in_scope = cu->list_in_scope;
5469
5470   if (die->tag != origin_die->tag
5471       && !(die->tag == DW_TAG_inlined_subroutine
5472            && origin_die->tag == DW_TAG_subprogram))
5473     complaint (&symfile_complaints,
5474                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5475                die->offset, origin_die->offset);
5476
5477   child_die = die->child;
5478   die_children_count = 0;
5479   while (child_die && child_die->tag)
5480     {
5481       child_die = sibling_die (child_die);
5482       die_children_count++;
5483     }
5484   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5485   cleanups = make_cleanup (xfree, offsets);
5486
5487   offsets_end = offsets;
5488   child_die = die->child;
5489   while (child_die && child_die->tag)
5490     {
5491       /* For each CHILD_DIE, find the corresponding child of
5492          ORIGIN_DIE.  If there is more than one layer of
5493          DW_AT_abstract_origin, follow them all; there shouldn't be,
5494          but GCC versions at least through 4.4 generate this (GCC PR
5495          40573).  */
5496       struct die_info *child_origin_die = child_die;
5497       struct dwarf2_cu *child_origin_cu = cu;
5498
5499       while (1)
5500         {
5501           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5502                               child_origin_cu);
5503           if (attr == NULL)
5504             break;
5505           child_origin_die = follow_die_ref (child_origin_die, attr,
5506                                              &child_origin_cu);
5507         }
5508
5509       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5510          counterpart may exist.  */
5511       if (child_origin_die != child_die)
5512         {
5513           if (child_die->tag != child_origin_die->tag
5514               && !(child_die->tag == DW_TAG_inlined_subroutine
5515                    && child_origin_die->tag == DW_TAG_subprogram))
5516             complaint (&symfile_complaints,
5517                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5518                          "different tags"), child_die->offset,
5519                        child_origin_die->offset);
5520           if (child_origin_die->parent != origin_die)
5521             complaint (&symfile_complaints,
5522                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5523                          "different parents"), child_die->offset,
5524                        child_origin_die->offset);
5525           else
5526             *offsets_end++ = child_origin_die->offset;
5527         }
5528       child_die = sibling_die (child_die);
5529     }
5530   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5531          unsigned_int_compar);
5532   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5533     if (offsetp[-1] == *offsetp)
5534       complaint (&symfile_complaints,
5535                  _("Multiple children of DIE 0x%x refer "
5536                    "to DIE 0x%x as their abstract origin"),
5537                  die->offset, *offsetp);
5538
5539   offsetp = offsets;
5540   origin_child_die = origin_die->child;
5541   while (origin_child_die && origin_child_die->tag)
5542     {
5543       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5544       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5545         offsetp++;
5546       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5547         {
5548           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5549           process_die (origin_child_die, origin_cu);
5550         }
5551       origin_child_die = sibling_die (origin_child_die);
5552     }
5553   origin_cu->list_in_scope = origin_previous_list_in_scope;
5554
5555   do_cleanups (cleanups);
5556 }
5557
5558 static void
5559 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5560 {
5561   struct objfile *objfile = cu->objfile;
5562   struct context_stack *new;
5563   CORE_ADDR lowpc;
5564   CORE_ADDR highpc;
5565   struct die_info *child_die;
5566   struct attribute *attr, *call_line, *call_file;
5567   char *name;
5568   CORE_ADDR baseaddr;
5569   struct block *block;
5570   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5571   VEC (symbolp) *template_args = NULL;
5572   struct template_symbol *templ_func = NULL;
5573
5574   if (inlined_func)
5575     {
5576       /* If we do not have call site information, we can't show the
5577          caller of this inlined function.  That's too confusing, so
5578          only use the scope for local variables.  */
5579       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5580       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5581       if (call_line == NULL || call_file == NULL)
5582         {
5583           read_lexical_block_scope (die, cu);
5584           return;
5585         }
5586     }
5587
5588   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5589
5590   name = dwarf2_name (die, cu);
5591
5592   /* Ignore functions with missing or empty names.  These are actually
5593      illegal according to the DWARF standard.  */
5594   if (name == NULL)
5595     {
5596       complaint (&symfile_complaints,
5597                  _("missing name for subprogram DIE at %d"), die->offset);
5598       return;
5599     }
5600
5601   /* Ignore functions with missing or invalid low and high pc attributes.  */
5602   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5603     {
5604       attr = dwarf2_attr (die, DW_AT_external, cu);
5605       if (!attr || !DW_UNSND (attr))
5606         complaint (&symfile_complaints,
5607                    _("cannot get low and high bounds "
5608                      "for subprogram DIE at %d"),
5609                    die->offset);
5610       return;
5611     }
5612
5613   lowpc += baseaddr;
5614   highpc += baseaddr;
5615
5616   /* Record the function range for dwarf_decode_lines.  */
5617   add_to_cu_func_list (name, lowpc, highpc, cu);
5618
5619   /* If we have any template arguments, then we must allocate a
5620      different sort of symbol.  */
5621   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5622     {
5623       if (child_die->tag == DW_TAG_template_type_param
5624           || child_die->tag == DW_TAG_template_value_param)
5625         {
5626           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5627                                        struct template_symbol);
5628           templ_func->base.is_cplus_template_function = 1;
5629           break;
5630         }
5631     }
5632
5633   new = push_context (0, lowpc);
5634   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5635                                (struct symbol *) templ_func);
5636
5637   /* If there is a location expression for DW_AT_frame_base, record
5638      it.  */
5639   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5640   if (attr)
5641     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5642        expression is being recorded directly in the function's symbol
5643        and not in a separate frame-base object.  I guess this hack is
5644        to avoid adding some sort of frame-base adjunct/annex to the
5645        function's symbol :-(.  The problem with doing this is that it
5646        results in a function symbol with a location expression that
5647        has nothing to do with the location of the function, ouch!  The
5648        relationship should be: a function's symbol has-a frame base; a
5649        frame-base has-a location expression.  */
5650     dwarf2_symbol_mark_computed (attr, new->name, cu);
5651
5652   cu->list_in_scope = &local_symbols;
5653
5654   if (die->child != NULL)
5655     {
5656       child_die = die->child;
5657       while (child_die && child_die->tag)
5658         {
5659           if (child_die->tag == DW_TAG_template_type_param
5660               || child_die->tag == DW_TAG_template_value_param)
5661             {
5662               struct symbol *arg = new_symbol (child_die, NULL, cu);
5663
5664               if (arg != NULL)
5665                 VEC_safe_push (symbolp, template_args, arg);
5666             }
5667           else
5668             process_die (child_die, cu);
5669           child_die = sibling_die (child_die);
5670         }
5671     }
5672
5673   inherit_abstract_dies (die, cu);
5674
5675   /* If we have a DW_AT_specification, we might need to import using
5676      directives from the context of the specification DIE.  See the
5677      comment in determine_prefix.  */
5678   if (cu->language == language_cplus
5679       && dwarf2_attr (die, DW_AT_specification, cu))
5680     {
5681       struct dwarf2_cu *spec_cu = cu;
5682       struct die_info *spec_die = die_specification (die, &spec_cu);
5683
5684       while (spec_die)
5685         {
5686           child_die = spec_die->child;
5687           while (child_die && child_die->tag)
5688             {
5689               if (child_die->tag == DW_TAG_imported_module)
5690                 process_die (child_die, spec_cu);
5691               child_die = sibling_die (child_die);
5692             }
5693
5694           /* In some cases, GCC generates specification DIEs that
5695              themselves contain DW_AT_specification attributes.  */
5696           spec_die = die_specification (spec_die, &spec_cu);
5697         }
5698     }
5699
5700   new = pop_context ();
5701   /* Make a block for the local symbols within.  */
5702   block = finish_block (new->name, &local_symbols, new->old_blocks,
5703                         lowpc, highpc, objfile);
5704
5705   /* For C++, set the block's scope.  */
5706   if (cu->language == language_cplus || cu->language == language_fortran)
5707     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5708                         determine_prefix (die, cu),
5709                         processing_has_namespace_info);
5710
5711   /* If we have address ranges, record them.  */
5712   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5713
5714   /* Attach template arguments to function.  */
5715   if (! VEC_empty (symbolp, template_args))
5716     {
5717       gdb_assert (templ_func != NULL);
5718
5719       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5720       templ_func->template_arguments
5721         = obstack_alloc (&objfile->objfile_obstack,
5722                          (templ_func->n_template_arguments
5723                           * sizeof (struct symbol *)));
5724       memcpy (templ_func->template_arguments,
5725               VEC_address (symbolp, template_args),
5726               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5727       VEC_free (symbolp, template_args);
5728     }
5729
5730   /* In C++, we can have functions nested inside functions (e.g., when
5731      a function declares a class that has methods).  This means that
5732      when we finish processing a function scope, we may need to go
5733      back to building a containing block's symbol lists.  */
5734   local_symbols = new->locals;
5735   param_symbols = new->params;
5736   using_directives = new->using_directives;
5737
5738   /* If we've finished processing a top-level function, subsequent
5739      symbols go in the file symbol list.  */
5740   if (outermost_context_p ())
5741     cu->list_in_scope = &file_symbols;
5742 }
5743
5744 /* Process all the DIES contained within a lexical block scope.  Start
5745    a new scope, process the dies, and then close the scope.  */
5746
5747 static void
5748 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5749 {
5750   struct objfile *objfile = cu->objfile;
5751   struct context_stack *new;
5752   CORE_ADDR lowpc, highpc;
5753   struct die_info *child_die;
5754   CORE_ADDR baseaddr;
5755
5756   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5757
5758   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5759   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5760      as multiple lexical blocks?  Handling children in a sane way would
5761      be nasty.  Might be easier to properly extend generic blocks to
5762      describe ranges.  */
5763   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5764     return;
5765   lowpc += baseaddr;
5766   highpc += baseaddr;
5767
5768   push_context (0, lowpc);
5769   if (die->child != NULL)
5770     {
5771       child_die = die->child;
5772       while (child_die && child_die->tag)
5773         {
5774           process_die (child_die, cu);
5775           child_die = sibling_die (child_die);
5776         }
5777     }
5778   new = pop_context ();
5779
5780   if (local_symbols != NULL || using_directives != NULL)
5781     {
5782       struct block *block
5783         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5784                         highpc, objfile);
5785
5786       /* Note that recording ranges after traversing children, as we
5787          do here, means that recording a parent's ranges entails
5788          walking across all its children's ranges as they appear in
5789          the address map, which is quadratic behavior.
5790
5791          It would be nicer to record the parent's ranges before
5792          traversing its children, simply overriding whatever you find
5793          there.  But since we don't even decide whether to create a
5794          block until after we've traversed its children, that's hard
5795          to do.  */
5796       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5797     }
5798   local_symbols = new->locals;
5799   using_directives = new->using_directives;
5800 }
5801
5802 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5803    Return 1 if the attributes are present and valid, otherwise, return 0.
5804    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5805
5806 static int
5807 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5808                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5809                     struct partial_symtab *ranges_pst)
5810 {
5811   struct objfile *objfile = cu->objfile;
5812   struct comp_unit_head *cu_header = &cu->header;
5813   bfd *obfd = objfile->obfd;
5814   unsigned int addr_size = cu_header->addr_size;
5815   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5816   /* Base address selection entry.  */
5817   CORE_ADDR base;
5818   int found_base;
5819   unsigned int dummy;
5820   gdb_byte *buffer;
5821   CORE_ADDR marker;
5822   int low_set;
5823   CORE_ADDR low = 0;
5824   CORE_ADDR high = 0;
5825   CORE_ADDR baseaddr;
5826
5827   found_base = cu->base_known;
5828   base = cu->base_address;
5829
5830   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5831   if (offset >= dwarf2_per_objfile->ranges.size)
5832     {
5833       complaint (&symfile_complaints,
5834                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5835                  offset);
5836       return 0;
5837     }
5838   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5839
5840   /* Read in the largest possible address.  */
5841   marker = read_address (obfd, buffer, cu, &dummy);
5842   if ((marker & mask) == mask)
5843     {
5844       /* If we found the largest possible address, then
5845          read the base address.  */
5846       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5847       buffer += 2 * addr_size;
5848       offset += 2 * addr_size;
5849       found_base = 1;
5850     }
5851
5852   low_set = 0;
5853
5854   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5855
5856   while (1)
5857     {
5858       CORE_ADDR range_beginning, range_end;
5859
5860       range_beginning = read_address (obfd, buffer, cu, &dummy);
5861       buffer += addr_size;
5862       range_end = read_address (obfd, buffer, cu, &dummy);
5863       buffer += addr_size;
5864       offset += 2 * addr_size;
5865
5866       /* An end of list marker is a pair of zero addresses.  */
5867       if (range_beginning == 0 && range_end == 0)
5868         /* Found the end of list entry.  */
5869         break;
5870
5871       /* Each base address selection entry is a pair of 2 values.
5872          The first is the largest possible address, the second is
5873          the base address.  Check for a base address here.  */
5874       if ((range_beginning & mask) == mask)
5875         {
5876           /* If we found the largest possible address, then
5877              read the base address.  */
5878           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5879           found_base = 1;
5880           continue;
5881         }
5882
5883       if (!found_base)
5884         {
5885           /* We have no valid base address for the ranges
5886              data.  */
5887           complaint (&symfile_complaints,
5888                      _("Invalid .debug_ranges data (no base address)"));
5889           return 0;
5890         }
5891
5892       if (range_beginning > range_end)
5893         {
5894           /* Inverted range entries are invalid.  */
5895           complaint (&symfile_complaints,
5896                      _("Invalid .debug_ranges data (inverted range)"));
5897           return 0;
5898         }
5899
5900       /* Empty range entries have no effect.  */
5901       if (range_beginning == range_end)
5902         continue;
5903
5904       range_beginning += base;
5905       range_end += base;
5906
5907       if (ranges_pst != NULL)
5908         addrmap_set_empty (objfile->psymtabs_addrmap,
5909                            range_beginning + baseaddr,
5910                            range_end - 1 + baseaddr,
5911                            ranges_pst);
5912
5913       /* FIXME: This is recording everything as a low-high
5914          segment of consecutive addresses.  We should have a
5915          data structure for discontiguous block ranges
5916          instead.  */
5917       if (! low_set)
5918         {
5919           low = range_beginning;
5920           high = range_end;
5921           low_set = 1;
5922         }
5923       else
5924         {
5925           if (range_beginning < low)
5926             low = range_beginning;
5927           if (range_end > high)
5928             high = range_end;
5929         }
5930     }
5931
5932   if (! low_set)
5933     /* If the first entry is an end-of-list marker, the range
5934        describes an empty scope, i.e. no instructions.  */
5935     return 0;
5936
5937   if (low_return)
5938     *low_return = low;
5939   if (high_return)
5940     *high_return = high;
5941   return 1;
5942 }
5943
5944 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5945    are present and valid, otherwise, return 0.  Return -1 if the range is
5946    discontinuous, i.e. derived from DW_AT_ranges information.  */
5947 static int
5948 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5949                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5950                       struct partial_symtab *pst)
5951 {
5952   struct attribute *attr;
5953   CORE_ADDR low = 0;
5954   CORE_ADDR high = 0;
5955   int ret = 0;
5956
5957   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5958   if (attr)
5959     {
5960       high = DW_ADDR (attr);
5961       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5962       if (attr)
5963         low = DW_ADDR (attr);
5964       else
5965         /* Found high w/o low attribute.  */
5966         return 0;
5967
5968       /* Found consecutive range of addresses.  */
5969       ret = 1;
5970     }
5971   else
5972     {
5973       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5974       if (attr != NULL)
5975         {
5976           /* Value of the DW_AT_ranges attribute is the offset in the
5977              .debug_ranges section.  */
5978           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5979             return 0;
5980           /* Found discontinuous range of addresses.  */
5981           ret = -1;
5982         }
5983     }
5984
5985   if (high < low)
5986     return 0;
5987
5988   /* When using the GNU linker, .gnu.linkonce. sections are used to
5989      eliminate duplicate copies of functions and vtables and such.
5990      The linker will arbitrarily choose one and discard the others.
5991      The AT_*_pc values for such functions refer to local labels in
5992      these sections.  If the section from that file was discarded, the
5993      labels are not in the output, so the relocs get a value of 0.
5994      If this is a discarded function, mark the pc bounds as invalid,
5995      so that GDB will ignore it.  */
5996   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5997     return 0;
5998
5999   *lowpc = low;
6000   *highpc = high;
6001   return ret;
6002 }
6003
6004 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6005    its low and high PC addresses.  Do nothing if these addresses could not
6006    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6007    and HIGHPC to the high address if greater than HIGHPC.  */
6008
6009 static void
6010 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6011                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6012                                  struct dwarf2_cu *cu)
6013 {
6014   CORE_ADDR low, high;
6015   struct die_info *child = die->child;
6016
6017   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6018     {
6019       *lowpc = min (*lowpc, low);
6020       *highpc = max (*highpc, high);
6021     }
6022
6023   /* If the language does not allow nested subprograms (either inside
6024      subprograms or lexical blocks), we're done.  */
6025   if (cu->language != language_ada)
6026     return;
6027
6028   /* Check all the children of the given DIE.  If it contains nested
6029      subprograms, then check their pc bounds.  Likewise, we need to
6030      check lexical blocks as well, as they may also contain subprogram
6031      definitions.  */
6032   while (child && child->tag)
6033     {
6034       if (child->tag == DW_TAG_subprogram
6035           || child->tag == DW_TAG_lexical_block)
6036         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6037       child = sibling_die (child);
6038     }
6039 }
6040
6041 /* Get the low and high pc's represented by the scope DIE, and store
6042    them in *LOWPC and *HIGHPC.  If the correct values can't be
6043    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6044
6045 static void
6046 get_scope_pc_bounds (struct die_info *die,
6047                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6048                      struct dwarf2_cu *cu)
6049 {
6050   CORE_ADDR best_low = (CORE_ADDR) -1;
6051   CORE_ADDR best_high = (CORE_ADDR) 0;
6052   CORE_ADDR current_low, current_high;
6053
6054   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6055     {
6056       best_low = current_low;
6057       best_high = current_high;
6058     }
6059   else
6060     {
6061       struct die_info *child = die->child;
6062
6063       while (child && child->tag)
6064         {
6065           switch (child->tag) {
6066           case DW_TAG_subprogram:
6067             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6068             break;
6069           case DW_TAG_namespace:
6070           case DW_TAG_module:
6071             /* FIXME: carlton/2004-01-16: Should we do this for
6072                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6073                that current GCC's always emit the DIEs corresponding
6074                to definitions of methods of classes as children of a
6075                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6076                the DIEs giving the declarations, which could be
6077                anywhere).  But I don't see any reason why the
6078                standards says that they have to be there.  */
6079             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6080
6081             if (current_low != ((CORE_ADDR) -1))
6082               {
6083                 best_low = min (best_low, current_low);
6084                 best_high = max (best_high, current_high);
6085               }
6086             break;
6087           default:
6088             /* Ignore.  */
6089             break;
6090           }
6091
6092           child = sibling_die (child);
6093         }
6094     }
6095
6096   *lowpc = best_low;
6097   *highpc = best_high;
6098 }
6099
6100 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6101    in DIE.  */
6102 static void
6103 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6104                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6105 {
6106   struct attribute *attr;
6107
6108   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6109   if (attr)
6110     {
6111       CORE_ADDR high = DW_ADDR (attr);
6112
6113       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6114       if (attr)
6115         {
6116           CORE_ADDR low = DW_ADDR (attr);
6117
6118           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6119         }
6120     }
6121
6122   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6123   if (attr)
6124     {
6125       bfd *obfd = cu->objfile->obfd;
6126
6127       /* The value of the DW_AT_ranges attribute is the offset of the
6128          address range list in the .debug_ranges section.  */
6129       unsigned long offset = DW_UNSND (attr);
6130       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6131
6132       /* For some target architectures, but not others, the
6133          read_address function sign-extends the addresses it returns.
6134          To recognize base address selection entries, we need a
6135          mask.  */
6136       unsigned int addr_size = cu->header.addr_size;
6137       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6138
6139       /* The base address, to which the next pair is relative.  Note
6140          that this 'base' is a DWARF concept: most entries in a range
6141          list are relative, to reduce the number of relocs against the
6142          debugging information.  This is separate from this function's
6143          'baseaddr' argument, which GDB uses to relocate debugging
6144          information from a shared library based on the address at
6145          which the library was loaded.  */
6146       CORE_ADDR base = cu->base_address;
6147       int base_known = cu->base_known;
6148
6149       gdb_assert (dwarf2_per_objfile->ranges.readin);
6150       if (offset >= dwarf2_per_objfile->ranges.size)
6151         {
6152           complaint (&symfile_complaints,
6153                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6154                      offset);
6155           return;
6156         }
6157
6158       for (;;)
6159         {
6160           unsigned int bytes_read;
6161           CORE_ADDR start, end;
6162
6163           start = read_address (obfd, buffer, cu, &bytes_read);
6164           buffer += bytes_read;
6165           end = read_address (obfd, buffer, cu, &bytes_read);
6166           buffer += bytes_read;
6167
6168           /* Did we find the end of the range list?  */
6169           if (start == 0 && end == 0)
6170             break;
6171
6172           /* Did we find a base address selection entry?  */
6173           else if ((start & base_select_mask) == base_select_mask)
6174             {
6175               base = end;
6176               base_known = 1;
6177             }
6178
6179           /* We found an ordinary address range.  */
6180           else
6181             {
6182               if (!base_known)
6183                 {
6184                   complaint (&symfile_complaints,
6185                              _("Invalid .debug_ranges data "
6186                                "(no base address)"));
6187                   return;
6188                 }
6189
6190               if (start > end)
6191                 {
6192                   /* Inverted range entries are invalid.  */
6193                   complaint (&symfile_complaints,
6194                              _("Invalid .debug_ranges data "
6195                                "(inverted range)"));
6196                   return;
6197                 }
6198
6199               /* Empty range entries have no effect.  */
6200               if (start == end)
6201                 continue;
6202
6203               record_block_range (block,
6204                                   baseaddr + base + start,
6205                                   baseaddr + base + end - 1);
6206             }
6207         }
6208     }
6209 }
6210
6211 /* Add an aggregate field to the field list.  */
6212
6213 static void
6214 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6215                   struct dwarf2_cu *cu)
6216 {
6217   struct objfile *objfile = cu->objfile;
6218   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6219   struct nextfield *new_field;
6220   struct attribute *attr;
6221   struct field *fp;
6222   char *fieldname = "";
6223
6224   /* Allocate a new field list entry and link it in.  */
6225   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6226   make_cleanup (xfree, new_field);
6227   memset (new_field, 0, sizeof (struct nextfield));
6228
6229   if (die->tag == DW_TAG_inheritance)
6230     {
6231       new_field->next = fip->baseclasses;
6232       fip->baseclasses = new_field;
6233     }
6234   else
6235     {
6236       new_field->next = fip->fields;
6237       fip->fields = new_field;
6238     }
6239   fip->nfields++;
6240
6241   /* Handle accessibility and virtuality of field.
6242      The default accessibility for members is public, the default
6243      accessibility for inheritance is private.  */
6244   if (die->tag != DW_TAG_inheritance)
6245     new_field->accessibility = DW_ACCESS_public;
6246   else
6247     new_field->accessibility = DW_ACCESS_private;
6248   new_field->virtuality = DW_VIRTUALITY_none;
6249
6250   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6251   if (attr)
6252     new_field->accessibility = DW_UNSND (attr);
6253   if (new_field->accessibility != DW_ACCESS_public)
6254     fip->non_public_fields = 1;
6255   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6256   if (attr)
6257     new_field->virtuality = DW_UNSND (attr);
6258
6259   fp = &new_field->field;
6260
6261   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6262     {
6263       /* Data member other than a C++ static data member.  */
6264
6265       /* Get type of field.  */
6266       fp->type = die_type (die, cu);
6267
6268       SET_FIELD_BITPOS (*fp, 0);
6269
6270       /* Get bit size of field (zero if none).  */
6271       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6272       if (attr)
6273         {
6274           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6275         }
6276       else
6277         {
6278           FIELD_BITSIZE (*fp) = 0;
6279         }
6280
6281       /* Get bit offset of field.  */
6282       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6283       if (attr)
6284         {
6285           int byte_offset = 0;
6286
6287           if (attr_form_is_section_offset (attr))
6288             dwarf2_complex_location_expr_complaint ();
6289           else if (attr_form_is_constant (attr))
6290             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6291           else if (attr_form_is_block (attr))
6292             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6293           else
6294             dwarf2_complex_location_expr_complaint ();
6295
6296           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6297         }
6298       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6299       if (attr)
6300         {
6301           if (gdbarch_bits_big_endian (gdbarch))
6302             {
6303               /* For big endian bits, the DW_AT_bit_offset gives the
6304                  additional bit offset from the MSB of the containing
6305                  anonymous object to the MSB of the field.  We don't
6306                  have to do anything special since we don't need to
6307                  know the size of the anonymous object.  */
6308               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6309             }
6310           else
6311             {
6312               /* For little endian bits, compute the bit offset to the
6313                  MSB of the anonymous object, subtract off the number of
6314                  bits from the MSB of the field to the MSB of the
6315                  object, and then subtract off the number of bits of
6316                  the field itself.  The result is the bit offset of
6317                  the LSB of the field.  */
6318               int anonymous_size;
6319               int bit_offset = DW_UNSND (attr);
6320
6321               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6322               if (attr)
6323                 {
6324                   /* The size of the anonymous object containing
6325                      the bit field is explicit, so use the
6326                      indicated size (in bytes).  */
6327                   anonymous_size = DW_UNSND (attr);
6328                 }
6329               else
6330                 {
6331                   /* The size of the anonymous object containing
6332                      the bit field must be inferred from the type
6333                      attribute of the data member containing the
6334                      bit field.  */
6335                   anonymous_size = TYPE_LENGTH (fp->type);
6336                 }
6337               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6338                 - bit_offset - FIELD_BITSIZE (*fp);
6339             }
6340         }
6341
6342       /* Get name of field.  */
6343       fieldname = dwarf2_name (die, cu);
6344       if (fieldname == NULL)
6345         fieldname = "";
6346
6347       /* The name is already allocated along with this objfile, so we don't
6348          need to duplicate it for the type.  */
6349       fp->name = fieldname;
6350
6351       /* Change accessibility for artificial fields (e.g. virtual table
6352          pointer or virtual base class pointer) to private.  */
6353       if (dwarf2_attr (die, DW_AT_artificial, cu))
6354         {
6355           FIELD_ARTIFICIAL (*fp) = 1;
6356           new_field->accessibility = DW_ACCESS_private;
6357           fip->non_public_fields = 1;
6358         }
6359     }
6360   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6361     {
6362       /* C++ static member.  */
6363
6364       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6365          is a declaration, but all versions of G++ as of this writing
6366          (so through at least 3.2.1) incorrectly generate
6367          DW_TAG_variable tags.  */
6368
6369       char *physname;
6370
6371       /* Get name of field.  */
6372       fieldname = dwarf2_name (die, cu);
6373       if (fieldname == NULL)
6374         return;
6375
6376       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6377       if (attr
6378           /* Only create a symbol if this is an external value.
6379              new_symbol checks this and puts the value in the global symbol
6380              table, which we want.  If it is not external, new_symbol
6381              will try to put the value in cu->list_in_scope which is wrong.  */
6382           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6383         {
6384           /* A static const member, not much different than an enum as far as
6385              we're concerned, except that we can support more types.  */
6386           new_symbol (die, NULL, cu);
6387         }
6388
6389       /* Get physical name.  */
6390       physname = (char *) dwarf2_physname (fieldname, die, cu);
6391
6392       /* The name is already allocated along with this objfile, so we don't
6393          need to duplicate it for the type.  */
6394       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6395       FIELD_TYPE (*fp) = die_type (die, cu);
6396       FIELD_NAME (*fp) = fieldname;
6397     }
6398   else if (die->tag == DW_TAG_inheritance)
6399     {
6400       /* C++ base class field.  */
6401       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6402       if (attr)
6403         {
6404           int byte_offset = 0;
6405
6406           if (attr_form_is_section_offset (attr))
6407             dwarf2_complex_location_expr_complaint ();
6408           else if (attr_form_is_constant (attr))
6409             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6410           else if (attr_form_is_block (attr))
6411             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6412           else
6413             dwarf2_complex_location_expr_complaint ();
6414
6415           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6416         }
6417       FIELD_BITSIZE (*fp) = 0;
6418       FIELD_TYPE (*fp) = die_type (die, cu);
6419       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6420       fip->nbaseclasses++;
6421     }
6422 }
6423
6424 /* Add a typedef defined in the scope of the FIP's class.  */
6425
6426 static void
6427 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6428                     struct dwarf2_cu *cu)
6429 {
6430   struct objfile *objfile = cu->objfile;
6431   struct typedef_field_list *new_field;
6432   struct attribute *attr;
6433   struct typedef_field *fp;
6434   char *fieldname = "";
6435
6436   /* Allocate a new field list entry and link it in.  */
6437   new_field = xzalloc (sizeof (*new_field));
6438   make_cleanup (xfree, new_field);
6439
6440   gdb_assert (die->tag == DW_TAG_typedef);
6441
6442   fp = &new_field->field;
6443
6444   /* Get name of field.  */
6445   fp->name = dwarf2_name (die, cu);
6446   if (fp->name == NULL)
6447     return;
6448
6449   fp->type = read_type_die (die, cu);
6450
6451   new_field->next = fip->typedef_field_list;
6452   fip->typedef_field_list = new_field;
6453   fip->typedef_field_list_count++;
6454 }
6455
6456 /* Create the vector of fields, and attach it to the type.  */
6457
6458 static void
6459 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6460                               struct dwarf2_cu *cu)
6461 {
6462   int nfields = fip->nfields;
6463
6464   /* Record the field count, allocate space for the array of fields,
6465      and create blank accessibility bitfields if necessary.  */
6466   TYPE_NFIELDS (type) = nfields;
6467   TYPE_FIELDS (type) = (struct field *)
6468     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6469   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6470
6471   if (fip->non_public_fields && cu->language != language_ada)
6472     {
6473       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6474
6475       TYPE_FIELD_PRIVATE_BITS (type) =
6476         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6477       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6478
6479       TYPE_FIELD_PROTECTED_BITS (type) =
6480         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6481       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6482
6483       TYPE_FIELD_IGNORE_BITS (type) =
6484         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6485       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6486     }
6487
6488   /* If the type has baseclasses, allocate and clear a bit vector for
6489      TYPE_FIELD_VIRTUAL_BITS.  */
6490   if (fip->nbaseclasses && cu->language != language_ada)
6491     {
6492       int num_bytes = B_BYTES (fip->nbaseclasses);
6493       unsigned char *pointer;
6494
6495       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6496       pointer = TYPE_ALLOC (type, num_bytes);
6497       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6498       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6499       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6500     }
6501
6502   /* Copy the saved-up fields into the field vector.  Start from the head of
6503      the list, adding to the tail of the field array, so that they end up in
6504      the same order in the array in which they were added to the list.  */
6505   while (nfields-- > 0)
6506     {
6507       struct nextfield *fieldp;
6508
6509       if (fip->fields)
6510         {
6511           fieldp = fip->fields;
6512           fip->fields = fieldp->next;
6513         }
6514       else
6515         {
6516           fieldp = fip->baseclasses;
6517           fip->baseclasses = fieldp->next;
6518         }
6519
6520       TYPE_FIELD (type, nfields) = fieldp->field;
6521       switch (fieldp->accessibility)
6522         {
6523         case DW_ACCESS_private:
6524           if (cu->language != language_ada)
6525             SET_TYPE_FIELD_PRIVATE (type, nfields);
6526           break;
6527
6528         case DW_ACCESS_protected:
6529           if (cu->language != language_ada)
6530             SET_TYPE_FIELD_PROTECTED (type, nfields);
6531           break;
6532
6533         case DW_ACCESS_public:
6534           break;
6535
6536         default:
6537           /* Unknown accessibility.  Complain and treat it as public.  */
6538           {
6539             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6540                        fieldp->accessibility);
6541           }
6542           break;
6543         }
6544       if (nfields < fip->nbaseclasses)
6545         {
6546           switch (fieldp->virtuality)
6547             {
6548             case DW_VIRTUALITY_virtual:
6549             case DW_VIRTUALITY_pure_virtual:
6550               if (cu->language == language_ada)
6551                 error (_("unexpected virtuality in component of Ada type"));
6552               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6553               break;
6554             }
6555         }
6556     }
6557 }
6558
6559 /* Add a member function to the proper fieldlist.  */
6560
6561 static void
6562 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6563                       struct type *type, struct dwarf2_cu *cu)
6564 {
6565   struct objfile *objfile = cu->objfile;
6566   struct attribute *attr;
6567   struct fnfieldlist *flp;
6568   int i;
6569   struct fn_field *fnp;
6570   char *fieldname;
6571   struct nextfnfield *new_fnfield;
6572   struct type *this_type;
6573
6574   if (cu->language == language_ada)
6575     error (_("unexpected member function in Ada type"));
6576
6577   /* Get name of member function.  */
6578   fieldname = dwarf2_name (die, cu);
6579   if (fieldname == NULL)
6580     return;
6581
6582   /* Look up member function name in fieldlist.  */
6583   for (i = 0; i < fip->nfnfields; i++)
6584     {
6585       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6586         break;
6587     }
6588
6589   /* Create new list element if necessary.  */
6590   if (i < fip->nfnfields)
6591     flp = &fip->fnfieldlists[i];
6592   else
6593     {
6594       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6595         {
6596           fip->fnfieldlists = (struct fnfieldlist *)
6597             xrealloc (fip->fnfieldlists,
6598                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6599                       * sizeof (struct fnfieldlist));
6600           if (fip->nfnfields == 0)
6601             make_cleanup (free_current_contents, &fip->fnfieldlists);
6602         }
6603       flp = &fip->fnfieldlists[fip->nfnfields];
6604       flp->name = fieldname;
6605       flp->length = 0;
6606       flp->head = NULL;
6607       i = fip->nfnfields++;
6608     }
6609
6610   /* Create a new member function field and chain it to the field list
6611      entry.  */
6612   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6613   make_cleanup (xfree, new_fnfield);
6614   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6615   new_fnfield->next = flp->head;
6616   flp->head = new_fnfield;
6617   flp->length++;
6618
6619   /* Fill in the member function field info.  */
6620   fnp = &new_fnfield->fnfield;
6621
6622   /* Delay processing of the physname until later.  */
6623   if (cu->language == language_cplus || cu->language == language_java)
6624     {
6625       add_to_method_list (type, i, flp->length - 1, fieldname,
6626                           die, cu);
6627     }
6628   else
6629     {
6630       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6631       fnp->physname = physname ? physname : "";
6632     }
6633
6634   fnp->type = alloc_type (objfile);
6635   this_type = read_type_die (die, cu);
6636   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6637     {
6638       int nparams = TYPE_NFIELDS (this_type);
6639
6640       /* TYPE is the domain of this method, and THIS_TYPE is the type
6641            of the method itself (TYPE_CODE_METHOD).  */
6642       smash_to_method_type (fnp->type, type,
6643                             TYPE_TARGET_TYPE (this_type),
6644                             TYPE_FIELDS (this_type),
6645                             TYPE_NFIELDS (this_type),
6646                             TYPE_VARARGS (this_type));
6647
6648       /* Handle static member functions.
6649          Dwarf2 has no clean way to discern C++ static and non-static
6650          member functions.  G++ helps GDB by marking the first
6651          parameter for non-static member functions (which is the this
6652          pointer) as artificial.  We obtain this information from
6653          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6654       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6655         fnp->voffset = VOFFSET_STATIC;
6656     }
6657   else
6658     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6659                dwarf2_full_name (fieldname, die, cu));
6660
6661   /* Get fcontext from DW_AT_containing_type if present.  */
6662   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6663     fnp->fcontext = die_containing_type (die, cu);
6664
6665   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6666      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6667
6668   /* Get accessibility.  */
6669   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6670   if (attr)
6671     {
6672       switch (DW_UNSND (attr))
6673         {
6674         case DW_ACCESS_private:
6675           fnp->is_private = 1;
6676           break;
6677         case DW_ACCESS_protected:
6678           fnp->is_protected = 1;
6679           break;
6680         }
6681     }
6682
6683   /* Check for artificial methods.  */
6684   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6685   if (attr && DW_UNSND (attr) != 0)
6686     fnp->is_artificial = 1;
6687
6688   /* Get index in virtual function table if it is a virtual member
6689      function.  For older versions of GCC, this is an offset in the
6690      appropriate virtual table, as specified by DW_AT_containing_type.
6691      For everyone else, it is an expression to be evaluated relative
6692      to the object address.  */
6693
6694   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6695   if (attr)
6696     {
6697       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6698         {
6699           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6700             {
6701               /* Old-style GCC.  */
6702               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6703             }
6704           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6705                    || (DW_BLOCK (attr)->size > 1
6706                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6707                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6708             {
6709               struct dwarf_block blk;
6710               int offset;
6711
6712               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6713                         ? 1 : 2);
6714               blk.size = DW_BLOCK (attr)->size - offset;
6715               blk.data = DW_BLOCK (attr)->data + offset;
6716               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6717               if ((fnp->voffset % cu->header.addr_size) != 0)
6718                 dwarf2_complex_location_expr_complaint ();
6719               else
6720                 fnp->voffset /= cu->header.addr_size;
6721               fnp->voffset += 2;
6722             }
6723           else
6724             dwarf2_complex_location_expr_complaint ();
6725
6726           if (!fnp->fcontext)
6727             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6728         }
6729       else if (attr_form_is_section_offset (attr))
6730         {
6731           dwarf2_complex_location_expr_complaint ();
6732         }
6733       else
6734         {
6735           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6736                                                  fieldname);
6737         }
6738     }
6739   else
6740     {
6741       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6742       if (attr && DW_UNSND (attr))
6743         {
6744           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6745           complaint (&symfile_complaints,
6746                      _("Member function \"%s\" (offset %d) is virtual "
6747                        "but the vtable offset is not specified"),
6748                      fieldname, die->offset);
6749           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6750           TYPE_CPLUS_DYNAMIC (type) = 1;
6751         }
6752     }
6753 }
6754
6755 /* Create the vector of member function fields, and attach it to the type.  */
6756
6757 static void
6758 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6759                                  struct dwarf2_cu *cu)
6760 {
6761   struct fnfieldlist *flp;
6762   int total_length = 0;
6763   int i;
6764
6765   if (cu->language == language_ada)
6766     error (_("unexpected member functions in Ada type"));
6767
6768   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6769   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6770     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6771
6772   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6773     {
6774       struct nextfnfield *nfp = flp->head;
6775       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6776       int k;
6777
6778       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6779       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6780       fn_flp->fn_fields = (struct fn_field *)
6781         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6782       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6783         fn_flp->fn_fields[k] = nfp->fnfield;
6784
6785       total_length += flp->length;
6786     }
6787
6788   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6789   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6790 }
6791
6792 /* Returns non-zero if NAME is the name of a vtable member in CU's
6793    language, zero otherwise.  */
6794 static int
6795 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6796 {
6797   static const char vptr[] = "_vptr";
6798   static const char vtable[] = "vtable";
6799
6800   /* Look for the C++ and Java forms of the vtable.  */
6801   if ((cu->language == language_java
6802        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6803        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6804        && is_cplus_marker (name[sizeof (vptr) - 1])))
6805     return 1;
6806
6807   return 0;
6808 }
6809
6810 /* GCC outputs unnamed structures that are really pointers to member
6811    functions, with the ABI-specified layout.  If TYPE describes
6812    such a structure, smash it into a member function type.
6813
6814    GCC shouldn't do this; it should just output pointer to member DIEs.
6815    This is GCC PR debug/28767.  */
6816
6817 static void
6818 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6819 {
6820   struct type *pfn_type, *domain_type, *new_type;
6821
6822   /* Check for a structure with no name and two children.  */
6823   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6824     return;
6825
6826   /* Check for __pfn and __delta members.  */
6827   if (TYPE_FIELD_NAME (type, 0) == NULL
6828       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6829       || TYPE_FIELD_NAME (type, 1) == NULL
6830       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6831     return;
6832
6833   /* Find the type of the method.  */
6834   pfn_type = TYPE_FIELD_TYPE (type, 0);
6835   if (pfn_type == NULL
6836       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6837       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6838     return;
6839
6840   /* Look for the "this" argument.  */
6841   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6842   if (TYPE_NFIELDS (pfn_type) == 0
6843       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6844       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6845     return;
6846
6847   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6848   new_type = alloc_type (objfile);
6849   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6850                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6851                         TYPE_VARARGS (pfn_type));
6852   smash_to_methodptr_type (type, new_type);
6853 }
6854
6855 /* Called when we find the DIE that starts a structure or union scope
6856    (definition) to create a type for the structure or union.  Fill in
6857    the type's name and general properties; the members will not be
6858    processed until process_structure_type.
6859
6860    NOTE: we need to call these functions regardless of whether or not the
6861    DIE has a DW_AT_name attribute, since it might be an anonymous
6862    structure or union.  This gets the type entered into our set of
6863    user defined types.
6864
6865    However, if the structure is incomplete (an opaque struct/union)
6866    then suppress creating a symbol table entry for it since gdb only
6867    wants to find the one with the complete definition.  Note that if
6868    it is complete, we just call new_symbol, which does it's own
6869    checking about whether the struct/union is anonymous or not (and
6870    suppresses creating a symbol table entry itself).  */
6871
6872 static struct type *
6873 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6874 {
6875   struct objfile *objfile = cu->objfile;
6876   struct type *type;
6877   struct attribute *attr;
6878   char *name;
6879
6880   /* If the definition of this type lives in .debug_types, read that type.
6881      Don't follow DW_AT_specification though, that will take us back up
6882      the chain and we want to go down.  */
6883   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6884   if (attr)
6885     {
6886       struct dwarf2_cu *type_cu = cu;
6887       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6888
6889       /* We could just recurse on read_structure_type, but we need to call
6890          get_die_type to ensure only one type for this DIE is created.
6891          This is important, for example, because for c++ classes we need
6892          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6893       type = read_type_die (type_die, type_cu);
6894
6895       /* TYPE_CU may not be the same as CU.
6896          Ensure TYPE is recorded in CU's type_hash table.  */
6897       return set_die_type (die, type, cu);
6898     }
6899
6900   type = alloc_type (objfile);
6901   INIT_CPLUS_SPECIFIC (type);
6902
6903   name = dwarf2_name (die, cu);
6904   if (name != NULL)
6905     {
6906       if (cu->language == language_cplus
6907           || cu->language == language_java)
6908         {
6909           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6910
6911           /* dwarf2_full_name might have already finished building the DIE's
6912              type.  If so, there is no need to continue.  */
6913           if (get_die_type (die, cu) != NULL)
6914             return get_die_type (die, cu);
6915
6916           TYPE_TAG_NAME (type) = full_name;
6917           if (die->tag == DW_TAG_structure_type
6918               || die->tag == DW_TAG_class_type)
6919             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6920         }
6921       else
6922         {
6923           /* The name is already allocated along with this objfile, so
6924              we don't need to duplicate it for the type.  */
6925           TYPE_TAG_NAME (type) = (char *) name;
6926           if (die->tag == DW_TAG_class_type)
6927             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6928         }
6929     }
6930
6931   if (die->tag == DW_TAG_structure_type)
6932     {
6933       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6934     }
6935   else if (die->tag == DW_TAG_union_type)
6936     {
6937       TYPE_CODE (type) = TYPE_CODE_UNION;
6938     }
6939   else
6940     {
6941       TYPE_CODE (type) = TYPE_CODE_CLASS;
6942     }
6943
6944   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6945     TYPE_DECLARED_CLASS (type) = 1;
6946
6947   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6948   if (attr)
6949     {
6950       TYPE_LENGTH (type) = DW_UNSND (attr);
6951     }
6952   else
6953     {
6954       TYPE_LENGTH (type) = 0;
6955     }
6956
6957   TYPE_STUB_SUPPORTED (type) = 1;
6958   if (die_is_declaration (die, cu))
6959     TYPE_STUB (type) = 1;
6960   else if (attr == NULL && die->child == NULL
6961            && producer_is_realview (cu->producer))
6962     /* RealView does not output the required DW_AT_declaration
6963        on incomplete types.  */
6964     TYPE_STUB (type) = 1;
6965
6966   /* We need to add the type field to the die immediately so we don't
6967      infinitely recurse when dealing with pointers to the structure
6968      type within the structure itself.  */
6969   set_die_type (die, type, cu);
6970
6971   /* set_die_type should be already done.  */
6972   set_descriptive_type (type, die, cu);
6973
6974   return type;
6975 }
6976
6977 /* Finish creating a structure or union type, including filling in
6978    its members and creating a symbol for it.  */
6979
6980 static void
6981 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6982 {
6983   struct objfile *objfile = cu->objfile;
6984   struct die_info *child_die = die->child;
6985   struct type *type;
6986
6987   type = get_die_type (die, cu);
6988   if (type == NULL)
6989     type = read_structure_type (die, cu);
6990
6991   if (die->child != NULL && ! die_is_declaration (die, cu))
6992     {
6993       struct field_info fi;
6994       struct die_info *child_die;
6995       VEC (symbolp) *template_args = NULL;
6996       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6997
6998       memset (&fi, 0, sizeof (struct field_info));
6999
7000       child_die = die->child;
7001
7002       while (child_die && child_die->tag)
7003         {
7004           if (child_die->tag == DW_TAG_member
7005               || child_die->tag == DW_TAG_variable)
7006             {
7007               /* NOTE: carlton/2002-11-05: A C++ static data member
7008                  should be a DW_TAG_member that is a declaration, but
7009                  all versions of G++ as of this writing (so through at
7010                  least 3.2.1) incorrectly generate DW_TAG_variable
7011                  tags for them instead.  */
7012               dwarf2_add_field (&fi, child_die, cu);
7013             }
7014           else if (child_die->tag == DW_TAG_subprogram)
7015             {
7016               /* C++ member function.  */
7017               dwarf2_add_member_fn (&fi, child_die, type, cu);
7018             }
7019           else if (child_die->tag == DW_TAG_inheritance)
7020             {
7021               /* C++ base class field.  */
7022               dwarf2_add_field (&fi, child_die, cu);
7023             }
7024           else if (child_die->tag == DW_TAG_typedef)
7025             dwarf2_add_typedef (&fi, child_die, cu);
7026           else if (child_die->tag == DW_TAG_template_type_param
7027                    || child_die->tag == DW_TAG_template_value_param)
7028             {
7029               struct symbol *arg = new_symbol (child_die, NULL, cu);
7030
7031               if (arg != NULL)
7032                 VEC_safe_push (symbolp, template_args, arg);
7033             }
7034
7035           child_die = sibling_die (child_die);
7036         }
7037
7038       /* Attach template arguments to type.  */
7039       if (! VEC_empty (symbolp, template_args))
7040         {
7041           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7042           TYPE_N_TEMPLATE_ARGUMENTS (type)
7043             = VEC_length (symbolp, template_args);
7044           TYPE_TEMPLATE_ARGUMENTS (type)
7045             = obstack_alloc (&objfile->objfile_obstack,
7046                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7047                               * sizeof (struct symbol *)));
7048           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7049                   VEC_address (symbolp, template_args),
7050                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7051                    * sizeof (struct symbol *)));
7052           VEC_free (symbolp, template_args);
7053         }
7054
7055       /* Attach fields and member functions to the type.  */
7056       if (fi.nfields)
7057         dwarf2_attach_fields_to_type (&fi, type, cu);
7058       if (fi.nfnfields)
7059         {
7060           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7061
7062           /* Get the type which refers to the base class (possibly this
7063              class itself) which contains the vtable pointer for the current
7064              class from the DW_AT_containing_type attribute.  This use of
7065              DW_AT_containing_type is a GNU extension.  */
7066
7067           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7068             {
7069               struct type *t = die_containing_type (die, cu);
7070
7071               TYPE_VPTR_BASETYPE (type) = t;
7072               if (type == t)
7073                 {
7074                   int i;
7075
7076                   /* Our own class provides vtbl ptr.  */
7077                   for (i = TYPE_NFIELDS (t) - 1;
7078                        i >= TYPE_N_BASECLASSES (t);
7079                        --i)
7080                     {
7081                       char *fieldname = TYPE_FIELD_NAME (t, i);
7082
7083                       if (is_vtable_name (fieldname, cu))
7084                         {
7085                           TYPE_VPTR_FIELDNO (type) = i;
7086                           break;
7087                         }
7088                     }
7089
7090                   /* Complain if virtual function table field not found.  */
7091                   if (i < TYPE_N_BASECLASSES (t))
7092                     complaint (&symfile_complaints,
7093                                _("virtual function table pointer "
7094                                  "not found when defining class '%s'"),
7095                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7096                                "");
7097                 }
7098               else
7099                 {
7100                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7101                 }
7102             }
7103           else if (cu->producer
7104                    && strncmp (cu->producer,
7105                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7106             {
7107               /* The IBM XLC compiler does not provide direct indication
7108                  of the containing type, but the vtable pointer is
7109                  always named __vfp.  */
7110
7111               int i;
7112
7113               for (i = TYPE_NFIELDS (type) - 1;
7114                    i >= TYPE_N_BASECLASSES (type);
7115                    --i)
7116                 {
7117                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7118                     {
7119                       TYPE_VPTR_FIELDNO (type) = i;
7120                       TYPE_VPTR_BASETYPE (type) = type;
7121                       break;
7122                     }
7123                 }
7124             }
7125         }
7126
7127       /* Copy fi.typedef_field_list linked list elements content into the
7128          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7129       if (fi.typedef_field_list)
7130         {
7131           int i = fi.typedef_field_list_count;
7132
7133           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7134           TYPE_TYPEDEF_FIELD_ARRAY (type)
7135             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7136           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7137
7138           /* Reverse the list order to keep the debug info elements order.  */
7139           while (--i >= 0)
7140             {
7141               struct typedef_field *dest, *src;
7142
7143               dest = &TYPE_TYPEDEF_FIELD (type, i);
7144               src = &fi.typedef_field_list->field;
7145               fi.typedef_field_list = fi.typedef_field_list->next;
7146               *dest = *src;
7147             }
7148         }
7149
7150       do_cleanups (back_to);
7151     }
7152
7153   quirk_gcc_member_function_pointer (type, cu->objfile);
7154
7155   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7156      snapshots) has been known to create a die giving a declaration
7157      for a class that has, as a child, a die giving a definition for a
7158      nested class.  So we have to process our children even if the
7159      current die is a declaration.  Normally, of course, a declaration
7160      won't have any children at all.  */
7161
7162   while (child_die != NULL && child_die->tag)
7163     {
7164       if (child_die->tag == DW_TAG_member
7165           || child_die->tag == DW_TAG_variable
7166           || child_die->tag == DW_TAG_inheritance
7167           || child_die->tag == DW_TAG_template_value_param
7168           || child_die->tag == DW_TAG_template_type_param)
7169         {
7170           /* Do nothing.  */
7171         }
7172       else
7173         process_die (child_die, cu);
7174
7175       child_die = sibling_die (child_die);
7176     }
7177
7178   /* Do not consider external references.  According to the DWARF standard,
7179      these DIEs are identified by the fact that they have no byte_size
7180      attribute, and a declaration attribute.  */
7181   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7182       || !die_is_declaration (die, cu))
7183     new_symbol (die, type, cu);
7184 }
7185
7186 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7187    complete the type's fields yet, or create any symbols.  */
7188
7189 static struct type *
7190 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7191 {
7192   struct objfile *objfile = cu->objfile;
7193   struct type *type;
7194   struct attribute *attr;
7195   const char *name;
7196
7197   /* If the definition of this type lives in .debug_types, read that type.
7198      Don't follow DW_AT_specification though, that will take us back up
7199      the chain and we want to go down.  */
7200   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7201   if (attr)
7202     {
7203       struct dwarf2_cu *type_cu = cu;
7204       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7205
7206       type = read_type_die (type_die, type_cu);
7207
7208       /* TYPE_CU may not be the same as CU.
7209          Ensure TYPE is recorded in CU's type_hash table.  */
7210       return set_die_type (die, type, cu);
7211     }
7212
7213   type = alloc_type (objfile);
7214
7215   TYPE_CODE (type) = TYPE_CODE_ENUM;
7216   name = dwarf2_full_name (NULL, die, cu);
7217   if (name != NULL)
7218     TYPE_TAG_NAME (type) = (char *) name;
7219
7220   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7221   if (attr)
7222     {
7223       TYPE_LENGTH (type) = DW_UNSND (attr);
7224     }
7225   else
7226     {
7227       TYPE_LENGTH (type) = 0;
7228     }
7229
7230   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7231      declared as private in the package spec, and then defined only
7232      inside the package body.  Such types are known as Taft Amendment
7233      Types.  When another package uses such a type, an incomplete DIE
7234      may be generated by the compiler.  */
7235   if (die_is_declaration (die, cu))
7236     TYPE_STUB (type) = 1;
7237
7238   return set_die_type (die, type, cu);
7239 }
7240
7241 /* Given a pointer to a die which begins an enumeration, process all
7242    the dies that define the members of the enumeration, and create the
7243    symbol for the enumeration type.
7244
7245    NOTE: We reverse the order of the element list.  */
7246
7247 static void
7248 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7249 {
7250   struct type *this_type;
7251
7252   this_type = get_die_type (die, cu);
7253   if (this_type == NULL)
7254     this_type = read_enumeration_type (die, cu);
7255
7256   if (die->child != NULL)
7257     {
7258       struct die_info *child_die;
7259       struct symbol *sym;
7260       struct field *fields = NULL;
7261       int num_fields = 0;
7262       int unsigned_enum = 1;
7263       char *name;
7264
7265       child_die = die->child;
7266       while (child_die && child_die->tag)
7267         {
7268           if (child_die->tag != DW_TAG_enumerator)
7269             {
7270               process_die (child_die, cu);
7271             }
7272           else
7273             {
7274               name = dwarf2_name (child_die, cu);
7275               if (name)
7276                 {
7277                   sym = new_symbol (child_die, this_type, cu);
7278                   if (SYMBOL_VALUE (sym) < 0)
7279                     unsigned_enum = 0;
7280
7281                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7282                     {
7283                       fields = (struct field *)
7284                         xrealloc (fields,
7285                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7286                                   * sizeof (struct field));
7287                     }
7288
7289                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7290                   FIELD_TYPE (fields[num_fields]) = NULL;
7291                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7292                   FIELD_BITSIZE (fields[num_fields]) = 0;
7293
7294                   num_fields++;
7295                 }
7296             }
7297
7298           child_die = sibling_die (child_die);
7299         }
7300
7301       if (num_fields)
7302         {
7303           TYPE_NFIELDS (this_type) = num_fields;
7304           TYPE_FIELDS (this_type) = (struct field *)
7305             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7306           memcpy (TYPE_FIELDS (this_type), fields,
7307                   sizeof (struct field) * num_fields);
7308           xfree (fields);
7309         }
7310       if (unsigned_enum)
7311         TYPE_UNSIGNED (this_type) = 1;
7312     }
7313
7314   new_symbol (die, this_type, cu);
7315 }
7316
7317 /* Extract all information from a DW_TAG_array_type DIE and put it in
7318    the DIE's type field.  For now, this only handles one dimensional
7319    arrays.  */
7320
7321 static struct type *
7322 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7323 {
7324   struct objfile *objfile = cu->objfile;
7325   struct die_info *child_die;
7326   struct type *type;
7327   struct type *element_type, *range_type, *index_type;
7328   struct type **range_types = NULL;
7329   struct attribute *attr;
7330   int ndim = 0;
7331   struct cleanup *back_to;
7332   char *name;
7333
7334   element_type = die_type (die, cu);
7335
7336   /* The die_type call above may have already set the type for this DIE.  */
7337   type = get_die_type (die, cu);
7338   if (type)
7339     return type;
7340
7341   /* Irix 6.2 native cc creates array types without children for
7342      arrays with unspecified length.  */
7343   if (die->child == NULL)
7344     {
7345       index_type = objfile_type (objfile)->builtin_int;
7346       range_type = create_range_type (NULL, index_type, 0, -1);
7347       type = create_array_type (NULL, element_type, range_type);
7348       return set_die_type (die, type, cu);
7349     }
7350
7351   back_to = make_cleanup (null_cleanup, NULL);
7352   child_die = die->child;
7353   while (child_die && child_die->tag)
7354     {
7355       if (child_die->tag == DW_TAG_subrange_type)
7356         {
7357           struct type *child_type = read_type_die (child_die, cu);
7358
7359           if (child_type != NULL)
7360             {
7361               /* The range type was succesfully read.  Save it for the
7362                  array type creation.  */
7363               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7364                 {
7365                   range_types = (struct type **)
7366                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7367                               * sizeof (struct type *));
7368                   if (ndim == 0)
7369                     make_cleanup (free_current_contents, &range_types);
7370                 }
7371               range_types[ndim++] = child_type;
7372             }
7373         }
7374       child_die = sibling_die (child_die);
7375     }
7376
7377   /* Dwarf2 dimensions are output from left to right, create the
7378      necessary array types in backwards order.  */
7379
7380   type = element_type;
7381
7382   if (read_array_order (die, cu) == DW_ORD_col_major)
7383     {
7384       int i = 0;
7385
7386       while (i < ndim)
7387         type = create_array_type (NULL, type, range_types[i++]);
7388     }
7389   else
7390     {
7391       while (ndim-- > 0)
7392         type = create_array_type (NULL, type, range_types[ndim]);
7393     }
7394
7395   /* Understand Dwarf2 support for vector types (like they occur on
7396      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7397      array type.  This is not part of the Dwarf2/3 standard yet, but a
7398      custom vendor extension.  The main difference between a regular
7399      array and the vector variant is that vectors are passed by value
7400      to functions.  */
7401   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7402   if (attr)
7403     make_vector_type (type);
7404
7405   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7406      implementation may choose to implement triple vectors using this
7407      attribute.  */
7408   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7409   if (attr)
7410     {
7411       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7412         TYPE_LENGTH (type) = DW_UNSND (attr);
7413       else
7414         complaint (&symfile_complaints,
7415                    _("DW_AT_byte_size for array type smaller "
7416                      "than the total size of elements"));
7417     }
7418
7419   name = dwarf2_name (die, cu);
7420   if (name)
7421     TYPE_NAME (type) = name;
7422
7423   /* Install the type in the die.  */
7424   set_die_type (die, type, cu);
7425
7426   /* set_die_type should be already done.  */
7427   set_descriptive_type (type, die, cu);
7428
7429   do_cleanups (back_to);
7430
7431   return type;
7432 }
7433
7434 static enum dwarf_array_dim_ordering
7435 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7436 {
7437   struct attribute *attr;
7438
7439   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7440
7441   if (attr) return DW_SND (attr);
7442
7443   /* GNU F77 is a special case, as at 08/2004 array type info is the
7444      opposite order to the dwarf2 specification, but data is still
7445      laid out as per normal fortran.
7446
7447      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7448      version checking.  */
7449
7450   if (cu->language == language_fortran
7451       && cu->producer && strstr (cu->producer, "GNU F77"))
7452     {
7453       return DW_ORD_row_major;
7454     }
7455
7456   switch (cu->language_defn->la_array_ordering)
7457     {
7458     case array_column_major:
7459       return DW_ORD_col_major;
7460     case array_row_major:
7461     default:
7462       return DW_ORD_row_major;
7463     };
7464 }
7465
7466 /* Extract all information from a DW_TAG_set_type DIE and put it in
7467    the DIE's type field.  */
7468
7469 static struct type *
7470 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7471 {
7472   struct type *domain_type, *set_type;
7473   struct attribute *attr;
7474
7475   domain_type = die_type (die, cu);
7476
7477   /* The die_type call above may have already set the type for this DIE.  */
7478   set_type = get_die_type (die, cu);
7479   if (set_type)
7480     return set_type;
7481
7482   set_type = create_set_type (NULL, domain_type);
7483
7484   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7485   if (attr)
7486     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7487
7488   return set_die_type (die, set_type, cu);
7489 }
7490
7491 /* First cut: install each common block member as a global variable.  */
7492
7493 static void
7494 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7495 {
7496   struct die_info *child_die;
7497   struct attribute *attr;
7498   struct symbol *sym;
7499   CORE_ADDR base = (CORE_ADDR) 0;
7500
7501   attr = dwarf2_attr (die, DW_AT_location, cu);
7502   if (attr)
7503     {
7504       /* Support the .debug_loc offsets.  */
7505       if (attr_form_is_block (attr))
7506         {
7507           base = decode_locdesc (DW_BLOCK (attr), cu);
7508         }
7509       else if (attr_form_is_section_offset (attr))
7510         {
7511           dwarf2_complex_location_expr_complaint ();
7512         }
7513       else
7514         {
7515           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7516                                                  "common block member");
7517         }
7518     }
7519   if (die->child != NULL)
7520     {
7521       child_die = die->child;
7522       while (child_die && child_die->tag)
7523         {
7524           sym = new_symbol (child_die, NULL, cu);
7525           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7526           if (sym != NULL && attr != NULL)
7527             {
7528               CORE_ADDR byte_offset = 0;
7529
7530               if (attr_form_is_section_offset (attr))
7531                 dwarf2_complex_location_expr_complaint ();
7532               else if (attr_form_is_constant (attr))
7533                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7534               else if (attr_form_is_block (attr))
7535                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7536               else
7537                 dwarf2_complex_location_expr_complaint ();
7538
7539               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7540               add_symbol_to_list (sym, &global_symbols);
7541             }
7542           child_die = sibling_die (child_die);
7543         }
7544     }
7545 }
7546
7547 /* Create a type for a C++ namespace.  */
7548
7549 static struct type *
7550 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7551 {
7552   struct objfile *objfile = cu->objfile;
7553   const char *previous_prefix, *name;
7554   int is_anonymous;
7555   struct type *type;
7556
7557   /* For extensions, reuse the type of the original namespace.  */
7558   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7559     {
7560       struct die_info *ext_die;
7561       struct dwarf2_cu *ext_cu = cu;
7562
7563       ext_die = dwarf2_extension (die, &ext_cu);
7564       type = read_type_die (ext_die, ext_cu);
7565
7566       /* EXT_CU may not be the same as CU.
7567          Ensure TYPE is recorded in CU's type_hash table.  */
7568       return set_die_type (die, type, cu);
7569     }
7570
7571   name = namespace_name (die, &is_anonymous, cu);
7572
7573   /* Now build the name of the current namespace.  */
7574
7575   previous_prefix = determine_prefix (die, cu);
7576   if (previous_prefix[0] != '\0')
7577     name = typename_concat (&objfile->objfile_obstack,
7578                             previous_prefix, name, 0, cu);
7579
7580   /* Create the type.  */
7581   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7582                     objfile);
7583   TYPE_NAME (type) = (char *) name;
7584   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7585
7586   return set_die_type (die, type, cu);
7587 }
7588
7589 /* Read a C++ namespace.  */
7590
7591 static void
7592 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7593 {
7594   struct objfile *objfile = cu->objfile;
7595   int is_anonymous;
7596
7597   /* Add a symbol associated to this if we haven't seen the namespace
7598      before.  Also, add a using directive if it's an anonymous
7599      namespace.  */
7600
7601   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7602     {
7603       struct type *type;
7604
7605       type = read_type_die (die, cu);
7606       new_symbol (die, type, cu);
7607
7608       namespace_name (die, &is_anonymous, cu);
7609       if (is_anonymous)
7610         {
7611           const char *previous_prefix = determine_prefix (die, cu);
7612
7613           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7614                                   NULL, &objfile->objfile_obstack);
7615         }
7616     }
7617
7618   if (die->child != NULL)
7619     {
7620       struct die_info *child_die = die->child;
7621
7622       while (child_die && child_die->tag)
7623         {
7624           process_die (child_die, cu);
7625           child_die = sibling_die (child_die);
7626         }
7627     }
7628 }
7629
7630 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7631    imported module.  Still we need that type as local Fortran "use ... only"
7632    declaration imports depend on the created type in determine_prefix.  */
7633
7634 static struct type *
7635 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7636 {
7637   struct objfile *objfile = cu->objfile;
7638   char *module_name;
7639   struct type *type;
7640
7641   module_name = dwarf2_name (die, cu);
7642   if (!module_name)
7643     complaint (&symfile_complaints,
7644                _("DW_TAG_module has no name, offset 0x%x"),
7645                die->offset);
7646   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7647
7648   /* determine_prefix uses TYPE_TAG_NAME.  */
7649   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7650
7651   return set_die_type (die, type, cu);
7652 }
7653
7654 /* Read a Fortran module.  */
7655
7656 static void
7657 read_module (struct die_info *die, struct dwarf2_cu *cu)
7658 {
7659   struct die_info *child_die = die->child;
7660
7661   while (child_die && child_die->tag)
7662     {
7663       process_die (child_die, cu);
7664       child_die = sibling_die (child_die);
7665     }
7666 }
7667
7668 /* Return the name of the namespace represented by DIE.  Set
7669    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7670    namespace.  */
7671
7672 static const char *
7673 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7674 {
7675   struct die_info *current_die;
7676   const char *name = NULL;
7677
7678   /* Loop through the extensions until we find a name.  */
7679
7680   for (current_die = die;
7681        current_die != NULL;
7682        current_die = dwarf2_extension (die, &cu))
7683     {
7684       name = dwarf2_name (current_die, cu);
7685       if (name != NULL)
7686         break;
7687     }
7688
7689   /* Is it an anonymous namespace?  */
7690
7691   *is_anonymous = (name == NULL);
7692   if (*is_anonymous)
7693     name = "(anonymous namespace)";
7694
7695   return name;
7696 }
7697
7698 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7699    the user defined type vector.  */
7700
7701 static struct type *
7702 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7703 {
7704   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7705   struct comp_unit_head *cu_header = &cu->header;
7706   struct type *type;
7707   struct attribute *attr_byte_size;
7708   struct attribute *attr_address_class;
7709   int byte_size, addr_class;
7710   struct type *target_type;
7711
7712   target_type = die_type (die, cu);
7713
7714   /* The die_type call above may have already set the type for this DIE.  */
7715   type = get_die_type (die, cu);
7716   if (type)
7717     return type;
7718
7719   type = lookup_pointer_type (target_type);
7720
7721   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7722   if (attr_byte_size)
7723     byte_size = DW_UNSND (attr_byte_size);
7724   else
7725     byte_size = cu_header->addr_size;
7726
7727   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7728   if (attr_address_class)
7729     addr_class = DW_UNSND (attr_address_class);
7730   else
7731     addr_class = DW_ADDR_none;
7732
7733   /* If the pointer size or address class is different than the
7734      default, create a type variant marked as such and set the
7735      length accordingly.  */
7736   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7737     {
7738       if (gdbarch_address_class_type_flags_p (gdbarch))
7739         {
7740           int type_flags;
7741
7742           type_flags = gdbarch_address_class_type_flags
7743                          (gdbarch, byte_size, addr_class);
7744           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7745                       == 0);
7746           type = make_type_with_address_space (type, type_flags);
7747         }
7748       else if (TYPE_LENGTH (type) != byte_size)
7749         {
7750           complaint (&symfile_complaints,
7751                      _("invalid pointer size %d"), byte_size);
7752         }
7753       else
7754         {
7755           /* Should we also complain about unhandled address classes?  */
7756         }
7757     }
7758
7759   TYPE_LENGTH (type) = byte_size;
7760   return set_die_type (die, type, cu);
7761 }
7762
7763 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7764    the user defined type vector.  */
7765
7766 static struct type *
7767 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7768 {
7769   struct type *type;
7770   struct type *to_type;
7771   struct type *domain;
7772
7773   to_type = die_type (die, cu);
7774   domain = die_containing_type (die, cu);
7775
7776   /* The calls above may have already set the type for this DIE.  */
7777   type = get_die_type (die, cu);
7778   if (type)
7779     return type;
7780
7781   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7782     type = lookup_methodptr_type (to_type);
7783   else
7784     type = lookup_memberptr_type (to_type, domain);
7785
7786   return set_die_type (die, type, cu);
7787 }
7788
7789 /* Extract all information from a DW_TAG_reference_type DIE and add to
7790    the user defined type vector.  */
7791
7792 static struct type *
7793 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7794 {
7795   struct comp_unit_head *cu_header = &cu->header;
7796   struct type *type, *target_type;
7797   struct attribute *attr;
7798
7799   target_type = die_type (die, cu);
7800
7801   /* The die_type call above may have already set the type for this DIE.  */
7802   type = get_die_type (die, cu);
7803   if (type)
7804     return type;
7805
7806   type = lookup_reference_type (target_type);
7807   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7808   if (attr)
7809     {
7810       TYPE_LENGTH (type) = DW_UNSND (attr);
7811     }
7812   else
7813     {
7814       TYPE_LENGTH (type) = cu_header->addr_size;
7815     }
7816   return set_die_type (die, type, cu);
7817 }
7818
7819 static struct type *
7820 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7821 {
7822   struct type *base_type, *cv_type;
7823
7824   base_type = die_type (die, cu);
7825
7826   /* The die_type call above may have already set the type for this DIE.  */
7827   cv_type = get_die_type (die, cu);
7828   if (cv_type)
7829     return cv_type;
7830
7831   /* In case the const qualifier is applied to an array type, the element type
7832      is so qualified, not the array type (section 6.7.3 of C99).  */
7833   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7834     {
7835       struct type *el_type, *inner_array;
7836
7837       base_type = copy_type (base_type);
7838       inner_array = base_type;
7839
7840       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7841         {
7842           TYPE_TARGET_TYPE (inner_array) =
7843             copy_type (TYPE_TARGET_TYPE (inner_array));
7844           inner_array = TYPE_TARGET_TYPE (inner_array);
7845         }
7846
7847       el_type = TYPE_TARGET_TYPE (inner_array);
7848       TYPE_TARGET_TYPE (inner_array) =
7849         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7850
7851       return set_die_type (die, base_type, cu);
7852     }
7853
7854   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7855   return set_die_type (die, cv_type, cu);
7856 }
7857
7858 static struct type *
7859 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7860 {
7861   struct type *base_type, *cv_type;
7862
7863   base_type = die_type (die, cu);
7864
7865   /* The die_type call above may have already set the type for this DIE.  */
7866   cv_type = get_die_type (die, cu);
7867   if (cv_type)
7868     return cv_type;
7869
7870   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7871   return set_die_type (die, cv_type, cu);
7872 }
7873
7874 /* Extract all information from a DW_TAG_string_type DIE and add to
7875    the user defined type vector.  It isn't really a user defined type,
7876    but it behaves like one, with other DIE's using an AT_user_def_type
7877    attribute to reference it.  */
7878
7879 static struct type *
7880 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7881 {
7882   struct objfile *objfile = cu->objfile;
7883   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7884   struct type *type, *range_type, *index_type, *char_type;
7885   struct attribute *attr;
7886   unsigned int length;
7887
7888   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7889   if (attr)
7890     {
7891       length = DW_UNSND (attr);
7892     }
7893   else
7894     {
7895       /* Check for the DW_AT_byte_size attribute.  */
7896       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7897       if (attr)
7898         {
7899           length = DW_UNSND (attr);
7900         }
7901       else
7902         {
7903           length = 1;
7904         }
7905     }
7906
7907   index_type = objfile_type (objfile)->builtin_int;
7908   range_type = create_range_type (NULL, index_type, 1, length);
7909   char_type = language_string_char_type (cu->language_defn, gdbarch);
7910   type = create_string_type (NULL, char_type, range_type);
7911
7912   return set_die_type (die, type, cu);
7913 }
7914
7915 /* Handle DIES due to C code like:
7916
7917    struct foo
7918    {
7919    int (*funcp)(int a, long l);
7920    int b;
7921    };
7922
7923    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
7924
7925 static struct type *
7926 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7927 {
7928   struct type *type;            /* Type that this function returns.  */
7929   struct type *ftype;           /* Function that returns above type.  */
7930   struct attribute *attr;
7931
7932   type = die_type (die, cu);
7933
7934   /* The die_type call above may have already set the type for this DIE.  */
7935   ftype = get_die_type (die, cu);
7936   if (ftype)
7937     return ftype;
7938
7939   ftype = lookup_function_type (type);
7940
7941   /* All functions in C++, Pascal and Java have prototypes.  */
7942   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7943   if ((attr && (DW_UNSND (attr) != 0))
7944       || cu->language == language_cplus
7945       || cu->language == language_java
7946       || cu->language == language_pascal)
7947     TYPE_PROTOTYPED (ftype) = 1;
7948   else if (producer_is_realview (cu->producer))
7949     /* RealView does not emit DW_AT_prototyped.  We can not
7950        distinguish prototyped and unprototyped functions; default to
7951        prototyped, since that is more common in modern code (and
7952        RealView warns about unprototyped functions).  */
7953     TYPE_PROTOTYPED (ftype) = 1;
7954
7955   /* Store the calling convention in the type if it's available in
7956      the subroutine die.  Otherwise set the calling convention to
7957      the default value DW_CC_normal.  */
7958   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7959   if (attr)
7960     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
7961   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
7962     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
7963   else
7964     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
7965
7966   /* We need to add the subroutine type to the die immediately so
7967      we don't infinitely recurse when dealing with parameters
7968      declared as the same subroutine type.  */
7969   set_die_type (die, ftype, cu);
7970
7971   if (die->child != NULL)
7972     {
7973       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7974       struct die_info *child_die;
7975       int nparams, iparams;
7976
7977       /* Count the number of parameters.
7978          FIXME: GDB currently ignores vararg functions, but knows about
7979          vararg member functions.  */
7980       nparams = 0;
7981       child_die = die->child;
7982       while (child_die && child_die->tag)
7983         {
7984           if (child_die->tag == DW_TAG_formal_parameter)
7985             nparams++;
7986           else if (child_die->tag == DW_TAG_unspecified_parameters)
7987             TYPE_VARARGS (ftype) = 1;
7988           child_die = sibling_die (child_die);
7989         }
7990
7991       /* Allocate storage for parameters and fill them in.  */
7992       TYPE_NFIELDS (ftype) = nparams;
7993       TYPE_FIELDS (ftype) = (struct field *)
7994         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7995
7996       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7997          even if we error out during the parameters reading below.  */
7998       for (iparams = 0; iparams < nparams; iparams++)
7999         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8000
8001       iparams = 0;
8002       child_die = die->child;
8003       while (child_die && child_die->tag)
8004         {
8005           if (child_die->tag == DW_TAG_formal_parameter)
8006             {
8007               struct type *arg_type;
8008
8009               /* DWARF version 2 has no clean way to discern C++
8010                  static and non-static member functions.  G++ helps
8011                  GDB by marking the first parameter for non-static
8012                  member functions (which is the this pointer) as
8013                  artificial.  We pass this information to
8014                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8015
8016                  DWARF version 3 added DW_AT_object_pointer, which GCC
8017                  4.5 does not yet generate.  */
8018               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8019               if (attr)
8020                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8021               else
8022                 {
8023                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8024
8025                   /* GCC/43521: In java, the formal parameter
8026                      "this" is sometimes not marked with DW_AT_artificial.  */
8027                   if (cu->language == language_java)
8028                     {
8029                       const char *name = dwarf2_name (child_die, cu);
8030
8031                       if (name && !strcmp (name, "this"))
8032                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8033                     }
8034                 }
8035               arg_type = die_type (child_die, cu);
8036
8037               /* RealView does not mark THIS as const, which the testsuite
8038                  expects.  GCC marks THIS as const in method definitions,
8039                  but not in the class specifications (GCC PR 43053).  */
8040               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8041                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8042                 {
8043                   int is_this = 0;
8044                   struct dwarf2_cu *arg_cu = cu;
8045                   const char *name = dwarf2_name (child_die, cu);
8046
8047                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8048                   if (attr)
8049                     {
8050                       /* If the compiler emits this, use it.  */
8051                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8052                         is_this = 1;
8053                     }
8054                   else if (name && strcmp (name, "this") == 0)
8055                     /* Function definitions will have the argument names.  */
8056                     is_this = 1;
8057                   else if (name == NULL && iparams == 0)
8058                     /* Declarations may not have the names, so like
8059                        elsewhere in GDB, assume an artificial first
8060                        argument is "this".  */
8061                     is_this = 1;
8062
8063                   if (is_this)
8064                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8065                                              arg_type, 0);
8066                 }
8067
8068               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8069               iparams++;
8070             }
8071           child_die = sibling_die (child_die);
8072         }
8073     }
8074
8075   return ftype;
8076 }
8077
8078 static struct type *
8079 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8080 {
8081   struct objfile *objfile = cu->objfile;
8082   const char *name = NULL;
8083   struct type *this_type;
8084
8085   name = dwarf2_full_name (NULL, die, cu);
8086   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8087                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8088   TYPE_NAME (this_type) = (char *) name;
8089   set_die_type (die, this_type, cu);
8090   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8091   return this_type;
8092 }
8093
8094 /* Find a representation of a given base type and install
8095    it in the TYPE field of the die.  */
8096
8097 static struct type *
8098 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8099 {
8100   struct objfile *objfile = cu->objfile;
8101   struct type *type;
8102   struct attribute *attr;
8103   int encoding = 0, size = 0;
8104   char *name;
8105   enum type_code code = TYPE_CODE_INT;
8106   int type_flags = 0;
8107   struct type *target_type = NULL;
8108
8109   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8110   if (attr)
8111     {
8112       encoding = DW_UNSND (attr);
8113     }
8114   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8115   if (attr)
8116     {
8117       size = DW_UNSND (attr);
8118     }
8119   name = dwarf2_name (die, cu);
8120   if (!name)
8121     {
8122       complaint (&symfile_complaints,
8123                  _("DW_AT_name missing from DW_TAG_base_type"));
8124     }
8125
8126   switch (encoding)
8127     {
8128       case DW_ATE_address:
8129         /* Turn DW_ATE_address into a void * pointer.  */
8130         code = TYPE_CODE_PTR;
8131         type_flags |= TYPE_FLAG_UNSIGNED;
8132         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8133         break;
8134       case DW_ATE_boolean:
8135         code = TYPE_CODE_BOOL;
8136         type_flags |= TYPE_FLAG_UNSIGNED;
8137         break;
8138       case DW_ATE_complex_float:
8139         code = TYPE_CODE_COMPLEX;
8140         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8141         break;
8142       case DW_ATE_decimal_float:
8143         code = TYPE_CODE_DECFLOAT;
8144         break;
8145       case DW_ATE_float:
8146         code = TYPE_CODE_FLT;
8147         break;
8148       case DW_ATE_signed:
8149         break;
8150       case DW_ATE_unsigned:
8151         type_flags |= TYPE_FLAG_UNSIGNED;
8152         break;
8153       case DW_ATE_signed_char:
8154         if (cu->language == language_ada || cu->language == language_m2
8155             || cu->language == language_pascal)
8156           code = TYPE_CODE_CHAR;
8157         break;
8158       case DW_ATE_unsigned_char:
8159         if (cu->language == language_ada || cu->language == language_m2
8160             || cu->language == language_pascal)
8161           code = TYPE_CODE_CHAR;
8162         type_flags |= TYPE_FLAG_UNSIGNED;
8163         break;
8164       case DW_ATE_UTF:
8165         /* We just treat this as an integer and then recognize the
8166            type by name elsewhere.  */
8167         break;
8168
8169       default:
8170         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8171                    dwarf_type_encoding_name (encoding));
8172         break;
8173     }
8174
8175   type = init_type (code, size, type_flags, NULL, objfile);
8176   TYPE_NAME (type) = name;
8177   TYPE_TARGET_TYPE (type) = target_type;
8178
8179   if (name && strcmp (name, "char") == 0)
8180     TYPE_NOSIGN (type) = 1;
8181
8182   return set_die_type (die, type, cu);
8183 }
8184
8185 /* Read the given DW_AT_subrange DIE.  */
8186
8187 static struct type *
8188 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8189 {
8190   struct type *base_type;
8191   struct type *range_type;
8192   struct attribute *attr;
8193   LONGEST low = 0;
8194   LONGEST high = -1;
8195   char *name;
8196   LONGEST negative_mask;
8197
8198   base_type = die_type (die, cu);
8199   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8200   check_typedef (base_type);
8201
8202   /* The die_type call above may have already set the type for this DIE.  */
8203   range_type = get_die_type (die, cu);
8204   if (range_type)
8205     return range_type;
8206
8207   if (cu->language == language_fortran)
8208     {
8209       /* FORTRAN implies a lower bound of 1, if not given.  */
8210       low = 1;
8211     }
8212
8213   /* FIXME: For variable sized arrays either of these could be
8214      a variable rather than a constant value.  We'll allow it,
8215      but we don't know how to handle it.  */
8216   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8217   if (attr)
8218     low = dwarf2_get_attr_constant_value (attr, 0);
8219
8220   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8221   if (attr)
8222     {
8223       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8224         {
8225           /* GCC encodes arrays with unspecified or dynamic length
8226              with a DW_FORM_block1 attribute or a reference attribute.
8227              FIXME: GDB does not yet know how to handle dynamic
8228              arrays properly, treat them as arrays with unspecified
8229              length for now.
8230
8231              FIXME: jimb/2003-09-22: GDB does not really know
8232              how to handle arrays of unspecified length
8233              either; we just represent them as zero-length
8234              arrays.  Choose an appropriate upper bound given
8235              the lower bound we've computed above.  */
8236           high = low - 1;
8237         }
8238       else
8239         high = dwarf2_get_attr_constant_value (attr, 1);
8240     }
8241   else
8242     {
8243       attr = dwarf2_attr (die, DW_AT_count, cu);
8244       if (attr)
8245         {
8246           int count = dwarf2_get_attr_constant_value (attr, 1);
8247           high = low + count - 1;
8248         }
8249       else
8250         {
8251           /* Unspecified array length.  */
8252           high = low - 1;
8253         }
8254     }
8255
8256   /* Dwarf-2 specifications explicitly allows to create subrange types
8257      without specifying a base type.
8258      In that case, the base type must be set to the type of
8259      the lower bound, upper bound or count, in that order, if any of these
8260      three attributes references an object that has a type.
8261      If no base type is found, the Dwarf-2 specifications say that
8262      a signed integer type of size equal to the size of an address should
8263      be used.
8264      For the following C code: `extern char gdb_int [];'
8265      GCC produces an empty range DIE.
8266      FIXME: muller/2010-05-28: Possible references to object for low bound,
8267      high bound or count are not yet handled by this code.  */
8268   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8269     {
8270       struct objfile *objfile = cu->objfile;
8271       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8272       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8273       struct type *int_type = objfile_type (objfile)->builtin_int;
8274
8275       /* Test "int", "long int", and "long long int" objfile types,
8276          and select the first one having a size above or equal to the
8277          architecture address size.  */
8278       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8279         base_type = int_type;
8280       else
8281         {
8282           int_type = objfile_type (objfile)->builtin_long;
8283           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8284             base_type = int_type;
8285           else
8286             {
8287               int_type = objfile_type (objfile)->builtin_long_long;
8288               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8289                 base_type = int_type;
8290             }
8291         }
8292     }
8293
8294   negative_mask =
8295     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8296   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8297     low |= negative_mask;
8298   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8299     high |= negative_mask;
8300
8301   range_type = create_range_type (NULL, base_type, low, high);
8302
8303   /* Mark arrays with dynamic length at least as an array of unspecified
8304      length.  GDB could check the boundary but before it gets implemented at
8305      least allow accessing the array elements.  */
8306   if (attr && attr->form == DW_FORM_block1)
8307     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8308
8309   /* Ada expects an empty array on no boundary attributes.  */
8310   if (attr == NULL && cu->language != language_ada)
8311     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8312
8313   name = dwarf2_name (die, cu);
8314   if (name)
8315     TYPE_NAME (range_type) = name;
8316
8317   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8318   if (attr)
8319     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8320
8321   set_die_type (die, range_type, cu);
8322
8323   /* set_die_type should be already done.  */
8324   set_descriptive_type (range_type, die, cu);
8325
8326   return range_type;
8327 }
8328
8329 static struct type *
8330 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8331 {
8332   struct type *type;
8333
8334   /* For now, we only support the C meaning of an unspecified type: void.  */
8335
8336   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8337   TYPE_NAME (type) = dwarf2_name (die, cu);
8338
8339   return set_die_type (die, type, cu);
8340 }
8341
8342 /* Trivial hash function for die_info: the hash value of a DIE
8343    is its offset in .debug_info for this objfile.  */
8344
8345 static hashval_t
8346 die_hash (const void *item)
8347 {
8348   const struct die_info *die = item;
8349
8350   return die->offset;
8351 }
8352
8353 /* Trivial comparison function for die_info structures: two DIEs
8354    are equal if they have the same offset.  */
8355
8356 static int
8357 die_eq (const void *item_lhs, const void *item_rhs)
8358 {
8359   const struct die_info *die_lhs = item_lhs;
8360   const struct die_info *die_rhs = item_rhs;
8361
8362   return die_lhs->offset == die_rhs->offset;
8363 }
8364
8365 /* Read a whole compilation unit into a linked list of dies.  */
8366
8367 static struct die_info *
8368 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8369 {
8370   struct die_reader_specs reader_specs;
8371   int read_abbrevs = 0;
8372   struct cleanup *back_to = NULL;
8373   struct die_info *die;
8374
8375   if (cu->dwarf2_abbrevs == NULL)
8376     {
8377       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8378       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8379       read_abbrevs = 1;
8380     }
8381
8382   gdb_assert (cu->die_hash == NULL);
8383   cu->die_hash
8384     = htab_create_alloc_ex (cu->header.length / 12,
8385                             die_hash,
8386                             die_eq,
8387                             NULL,
8388                             &cu->comp_unit_obstack,
8389                             hashtab_obstack_allocate,
8390                             dummy_obstack_deallocate);
8391
8392   init_cu_die_reader (&reader_specs, cu);
8393
8394   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8395
8396   if (read_abbrevs)
8397     do_cleanups (back_to);
8398
8399   return die;
8400 }
8401
8402 /* Main entry point for reading a DIE and all children.
8403    Read the DIE and dump it if requested.  */
8404
8405 static struct die_info *
8406 read_die_and_children (const struct die_reader_specs *reader,
8407                        gdb_byte *info_ptr,
8408                        gdb_byte **new_info_ptr,
8409                        struct die_info *parent)
8410 {
8411   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8412                                                      new_info_ptr, parent);
8413
8414   if (dwarf2_die_debug)
8415     {
8416       fprintf_unfiltered (gdb_stdlog,
8417                           "\nRead die from %s of %s:\n",
8418                           reader->buffer == dwarf2_per_objfile->info.buffer
8419                           ? ".debug_info"
8420                           : reader->buffer == dwarf2_per_objfile->types.buffer
8421                           ? ".debug_types"
8422                           : "unknown section",
8423                           reader->abfd->filename);
8424       dump_die (result, dwarf2_die_debug);
8425     }
8426
8427   return result;
8428 }
8429
8430 /* Read a single die and all its descendents.  Set the die's sibling
8431    field to NULL; set other fields in the die correctly, and set all
8432    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8433    location of the info_ptr after reading all of those dies.  PARENT
8434    is the parent of the die in question.  */
8435
8436 static struct die_info *
8437 read_die_and_children_1 (const struct die_reader_specs *reader,
8438                          gdb_byte *info_ptr,
8439                          gdb_byte **new_info_ptr,
8440                          struct die_info *parent)
8441 {
8442   struct die_info *die;
8443   gdb_byte *cur_ptr;
8444   int has_children;
8445
8446   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8447   if (die == NULL)
8448     {
8449       *new_info_ptr = cur_ptr;
8450       return NULL;
8451     }
8452   store_in_ref_table (die, reader->cu);
8453
8454   if (has_children)
8455     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8456   else
8457     {
8458       die->child = NULL;
8459       *new_info_ptr = cur_ptr;
8460     }
8461
8462   die->sibling = NULL;
8463   die->parent = parent;
8464   return die;
8465 }
8466
8467 /* Read a die, all of its descendents, and all of its siblings; set
8468    all of the fields of all of the dies correctly.  Arguments are as
8469    in read_die_and_children.  */
8470
8471 static struct die_info *
8472 read_die_and_siblings (const struct die_reader_specs *reader,
8473                        gdb_byte *info_ptr,
8474                        gdb_byte **new_info_ptr,
8475                        struct die_info *parent)
8476 {
8477   struct die_info *first_die, *last_sibling;
8478   gdb_byte *cur_ptr;
8479
8480   cur_ptr = info_ptr;
8481   first_die = last_sibling = NULL;
8482
8483   while (1)
8484     {
8485       struct die_info *die
8486         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8487
8488       if (die == NULL)
8489         {
8490           *new_info_ptr = cur_ptr;
8491           return first_die;
8492         }
8493
8494       if (!first_die)
8495         first_die = die;
8496       else
8497         last_sibling->sibling = die;
8498
8499       last_sibling = die;
8500     }
8501 }
8502
8503 /* Read the die from the .debug_info section buffer.  Set DIEP to
8504    point to a newly allocated die with its information, except for its
8505    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8506    whether the die has children or not.  */
8507
8508 static gdb_byte *
8509 read_full_die (const struct die_reader_specs *reader,
8510                struct die_info **diep, gdb_byte *info_ptr,
8511                int *has_children)
8512 {
8513   unsigned int abbrev_number, bytes_read, i, offset;
8514   struct abbrev_info *abbrev;
8515   struct die_info *die;
8516   struct dwarf2_cu *cu = reader->cu;
8517   bfd *abfd = reader->abfd;
8518
8519   offset = info_ptr - reader->buffer;
8520   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8521   info_ptr += bytes_read;
8522   if (!abbrev_number)
8523     {
8524       *diep = NULL;
8525       *has_children = 0;
8526       return info_ptr;
8527     }
8528
8529   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8530   if (!abbrev)
8531     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8532            abbrev_number,
8533            bfd_get_filename (abfd));
8534
8535   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8536   die->offset = offset;
8537   die->tag = abbrev->tag;
8538   die->abbrev = abbrev_number;
8539
8540   die->num_attrs = abbrev->num_attrs;
8541
8542   for (i = 0; i < abbrev->num_attrs; ++i)
8543     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8544                                abfd, info_ptr, cu);
8545
8546   *diep = die;
8547   *has_children = abbrev->has_children;
8548   return info_ptr;
8549 }
8550
8551 /* In DWARF version 2, the description of the debugging information is
8552    stored in a separate .debug_abbrev section.  Before we read any
8553    dies from a section we read in all abbreviations and install them
8554    in a hash table.  This function also sets flags in CU describing
8555    the data found in the abbrev table.  */
8556
8557 static void
8558 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8559 {
8560   struct comp_unit_head *cu_header = &cu->header;
8561   gdb_byte *abbrev_ptr;
8562   struct abbrev_info *cur_abbrev;
8563   unsigned int abbrev_number, bytes_read, abbrev_name;
8564   unsigned int abbrev_form, hash_number;
8565   struct attr_abbrev *cur_attrs;
8566   unsigned int allocated_attrs;
8567
8568   /* Initialize dwarf2 abbrevs.  */
8569   obstack_init (&cu->abbrev_obstack);
8570   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8571                                       (ABBREV_HASH_SIZE
8572                                        * sizeof (struct abbrev_info *)));
8573   memset (cu->dwarf2_abbrevs, 0,
8574           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8575
8576   dwarf2_read_section (dwarf2_per_objfile->objfile,
8577                        &dwarf2_per_objfile->abbrev);
8578   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8579   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8580   abbrev_ptr += bytes_read;
8581
8582   allocated_attrs = ATTR_ALLOC_CHUNK;
8583   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8584
8585   /* Loop until we reach an abbrev number of 0.  */
8586   while (abbrev_number)
8587     {
8588       cur_abbrev = dwarf_alloc_abbrev (cu);
8589
8590       /* read in abbrev header */
8591       cur_abbrev->number = abbrev_number;
8592       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8593       abbrev_ptr += bytes_read;
8594       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8595       abbrev_ptr += 1;
8596
8597       if (cur_abbrev->tag == DW_TAG_namespace)
8598         cu->has_namespace_info = 1;
8599
8600       /* now read in declarations */
8601       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8602       abbrev_ptr += bytes_read;
8603       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8604       abbrev_ptr += bytes_read;
8605       while (abbrev_name)
8606         {
8607           if (cur_abbrev->num_attrs == allocated_attrs)
8608             {
8609               allocated_attrs += ATTR_ALLOC_CHUNK;
8610               cur_attrs
8611                 = xrealloc (cur_attrs, (allocated_attrs
8612                                         * sizeof (struct attr_abbrev)));
8613             }
8614
8615           /* Record whether this compilation unit might have
8616              inter-compilation-unit references.  If we don't know what form
8617              this attribute will have, then it might potentially be a
8618              DW_FORM_ref_addr, so we conservatively expect inter-CU
8619              references.  */
8620
8621           if (abbrev_form == DW_FORM_ref_addr
8622               || abbrev_form == DW_FORM_indirect)
8623             cu->has_form_ref_addr = 1;
8624
8625           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8626           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8627           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8628           abbrev_ptr += bytes_read;
8629           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8630           abbrev_ptr += bytes_read;
8631         }
8632
8633       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8634                                          (cur_abbrev->num_attrs
8635                                           * sizeof (struct attr_abbrev)));
8636       memcpy (cur_abbrev->attrs, cur_attrs,
8637               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8638
8639       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8640       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8641       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8642
8643       /* Get next abbreviation.
8644          Under Irix6 the abbreviations for a compilation unit are not
8645          always properly terminated with an abbrev number of 0.
8646          Exit loop if we encounter an abbreviation which we have
8647          already read (which means we are about to read the abbreviations
8648          for the next compile unit) or if the end of the abbreviation
8649          table is reached.  */
8650       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8651           >= dwarf2_per_objfile->abbrev.size)
8652         break;
8653       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8654       abbrev_ptr += bytes_read;
8655       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8656         break;
8657     }
8658
8659   xfree (cur_attrs);
8660 }
8661
8662 /* Release the memory used by the abbrev table for a compilation unit.  */
8663
8664 static void
8665 dwarf2_free_abbrev_table (void *ptr_to_cu)
8666 {
8667   struct dwarf2_cu *cu = ptr_to_cu;
8668
8669   obstack_free (&cu->abbrev_obstack, NULL);
8670   cu->dwarf2_abbrevs = NULL;
8671 }
8672
8673 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8674
8675 static struct abbrev_info *
8676 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8677 {
8678   unsigned int hash_number;
8679   struct abbrev_info *abbrev;
8680
8681   hash_number = number % ABBREV_HASH_SIZE;
8682   abbrev = cu->dwarf2_abbrevs[hash_number];
8683
8684   while (abbrev)
8685     {
8686       if (abbrev->number == number)
8687         return abbrev;
8688       else
8689         abbrev = abbrev->next;
8690     }
8691   return NULL;
8692 }
8693
8694 /* Returns nonzero if TAG represents a type that we might generate a partial
8695    symbol for.  */
8696
8697 static int
8698 is_type_tag_for_partial (int tag)
8699 {
8700   switch (tag)
8701     {
8702 #if 0
8703     /* Some types that would be reasonable to generate partial symbols for,
8704        that we don't at present.  */
8705     case DW_TAG_array_type:
8706     case DW_TAG_file_type:
8707     case DW_TAG_ptr_to_member_type:
8708     case DW_TAG_set_type:
8709     case DW_TAG_string_type:
8710     case DW_TAG_subroutine_type:
8711 #endif
8712     case DW_TAG_base_type:
8713     case DW_TAG_class_type:
8714     case DW_TAG_interface_type:
8715     case DW_TAG_enumeration_type:
8716     case DW_TAG_structure_type:
8717     case DW_TAG_subrange_type:
8718     case DW_TAG_typedef:
8719     case DW_TAG_union_type:
8720       return 1;
8721     default:
8722       return 0;
8723     }
8724 }
8725
8726 /* Load all DIEs that are interesting for partial symbols into memory.  */
8727
8728 static struct partial_die_info *
8729 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8730                    int building_psymtab, struct dwarf2_cu *cu)
8731 {
8732   struct partial_die_info *part_die;
8733   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8734   struct abbrev_info *abbrev;
8735   unsigned int bytes_read;
8736   unsigned int load_all = 0;
8737
8738   int nesting_level = 1;
8739
8740   parent_die = NULL;
8741   last_die = NULL;
8742
8743   if (cu->per_cu && cu->per_cu->load_all_dies)
8744     load_all = 1;
8745
8746   cu->partial_dies
8747     = htab_create_alloc_ex (cu->header.length / 12,
8748                             partial_die_hash,
8749                             partial_die_eq,
8750                             NULL,
8751                             &cu->comp_unit_obstack,
8752                             hashtab_obstack_allocate,
8753                             dummy_obstack_deallocate);
8754
8755   part_die = obstack_alloc (&cu->comp_unit_obstack,
8756                             sizeof (struct partial_die_info));
8757
8758   while (1)
8759     {
8760       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8761
8762       /* A NULL abbrev means the end of a series of children.  */
8763       if (abbrev == NULL)
8764         {
8765           if (--nesting_level == 0)
8766             {
8767               /* PART_DIE was probably the last thing allocated on the
8768                  comp_unit_obstack, so we could call obstack_free
8769                  here.  We don't do that because the waste is small,
8770                  and will be cleaned up when we're done with this
8771                  compilation unit.  This way, we're also more robust
8772                  against other users of the comp_unit_obstack.  */
8773               return first_die;
8774             }
8775           info_ptr += bytes_read;
8776           last_die = parent_die;
8777           parent_die = parent_die->die_parent;
8778           continue;
8779         }
8780
8781       /* Check for template arguments.  We never save these; if
8782          they're seen, we just mark the parent, and go on our way.  */
8783       if (parent_die != NULL
8784           && cu->language == language_cplus
8785           && (abbrev->tag == DW_TAG_template_type_param
8786               || abbrev->tag == DW_TAG_template_value_param))
8787         {
8788           parent_die->has_template_arguments = 1;
8789
8790           if (!load_all)
8791             {
8792               /* We don't need a partial DIE for the template argument.  */
8793               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8794                                        cu);
8795               continue;
8796             }
8797         }
8798
8799       /* We only recurse into subprograms looking for template arguments.
8800          Skip their other children.  */
8801       if (!load_all
8802           && cu->language == language_cplus
8803           && parent_die != NULL
8804           && parent_die->tag == DW_TAG_subprogram)
8805         {
8806           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8807           continue;
8808         }
8809
8810       /* Check whether this DIE is interesting enough to save.  Normally
8811          we would not be interested in members here, but there may be
8812          later variables referencing them via DW_AT_specification (for
8813          static members).  */
8814       if (!load_all
8815           && !is_type_tag_for_partial (abbrev->tag)
8816           && abbrev->tag != DW_TAG_constant
8817           && abbrev->tag != DW_TAG_enumerator
8818           && abbrev->tag != DW_TAG_subprogram
8819           && abbrev->tag != DW_TAG_lexical_block
8820           && abbrev->tag != DW_TAG_variable
8821           && abbrev->tag != DW_TAG_namespace
8822           && abbrev->tag != DW_TAG_module
8823           && abbrev->tag != DW_TAG_member)
8824         {
8825           /* Otherwise we skip to the next sibling, if any.  */
8826           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8827           continue;
8828         }
8829
8830       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8831                                    buffer, info_ptr, cu);
8832
8833       /* This two-pass algorithm for processing partial symbols has a
8834          high cost in cache pressure.  Thus, handle some simple cases
8835          here which cover the majority of C partial symbols.  DIEs
8836          which neither have specification tags in them, nor could have
8837          specification tags elsewhere pointing at them, can simply be
8838          processed and discarded.
8839
8840          This segment is also optional; scan_partial_symbols and
8841          add_partial_symbol will handle these DIEs if we chain
8842          them in normally.  When compilers which do not emit large
8843          quantities of duplicate debug information are more common,
8844          this code can probably be removed.  */
8845
8846       /* Any complete simple types at the top level (pretty much all
8847          of them, for a language without namespaces), can be processed
8848          directly.  */
8849       if (parent_die == NULL
8850           && part_die->has_specification == 0
8851           && part_die->is_declaration == 0
8852           && (part_die->tag == DW_TAG_typedef
8853               || part_die->tag == DW_TAG_base_type
8854               || part_die->tag == DW_TAG_subrange_type))
8855         {
8856           if (building_psymtab && part_die->name != NULL)
8857             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8858                                  VAR_DOMAIN, LOC_TYPEDEF,
8859                                  &cu->objfile->static_psymbols,
8860                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8861           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8862           continue;
8863         }
8864
8865       /* If we're at the second level, and we're an enumerator, and
8866          our parent has no specification (meaning possibly lives in a
8867          namespace elsewhere), then we can add the partial symbol now
8868          instead of queueing it.  */
8869       if (part_die->tag == DW_TAG_enumerator
8870           && parent_die != NULL
8871           && parent_die->die_parent == NULL
8872           && parent_die->tag == DW_TAG_enumeration_type
8873           && parent_die->has_specification == 0)
8874         {
8875           if (part_die->name == NULL)
8876             complaint (&symfile_complaints,
8877                        _("malformed enumerator DIE ignored"));
8878           else if (building_psymtab)
8879             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8880                                  VAR_DOMAIN, LOC_CONST,
8881                                  (cu->language == language_cplus
8882                                   || cu->language == language_java)
8883                                  ? &cu->objfile->global_psymbols
8884                                  : &cu->objfile->static_psymbols,
8885                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8886
8887           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8888           continue;
8889         }
8890
8891       /* We'll save this DIE so link it in.  */
8892       part_die->die_parent = parent_die;
8893       part_die->die_sibling = NULL;
8894       part_die->die_child = NULL;
8895
8896       if (last_die && last_die == parent_die)
8897         last_die->die_child = part_die;
8898       else if (last_die)
8899         last_die->die_sibling = part_die;
8900
8901       last_die = part_die;
8902
8903       if (first_die == NULL)
8904         first_die = part_die;
8905
8906       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8907          find interesting need to be in the hash table, because we
8908          also have the parent/sibling/child chains; only those that we
8909          might refer to by offset later during partial symbol reading.
8910
8911          For now this means things that might have be the target of a
8912          DW_AT_specification, DW_AT_abstract_origin, or
8913          DW_AT_extension.  DW_AT_extension will refer only to
8914          namespaces; DW_AT_abstract_origin refers to functions (and
8915          many things under the function DIE, but we do not recurse
8916          into function DIEs during partial symbol reading) and
8917          possibly variables as well; DW_AT_specification refers to
8918          declarations.  Declarations ought to have the DW_AT_declaration
8919          flag.  It happens that GCC forgets to put it in sometimes, but
8920          only for functions, not for types.
8921
8922          Adding more things than necessary to the hash table is harmless
8923          except for the performance cost.  Adding too few will result in
8924          wasted time in find_partial_die, when we reread the compilation
8925          unit with load_all_dies set.  */
8926
8927       if (load_all
8928           || abbrev->tag == DW_TAG_constant
8929           || abbrev->tag == DW_TAG_subprogram
8930           || abbrev->tag == DW_TAG_variable
8931           || abbrev->tag == DW_TAG_namespace
8932           || part_die->is_declaration)
8933         {
8934           void **slot;
8935
8936           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8937                                            part_die->offset, INSERT);
8938           *slot = part_die;
8939         }
8940
8941       part_die = obstack_alloc (&cu->comp_unit_obstack,
8942                                 sizeof (struct partial_die_info));
8943
8944       /* For some DIEs we want to follow their children (if any).  For C
8945          we have no reason to follow the children of structures; for other
8946          languages we have to, so that we can get at method physnames
8947          to infer fully qualified class names, for DW_AT_specification,
8948          and for C++ template arguments.  For C++, we also look one level
8949          inside functions to find template arguments (if the name of the
8950          function does not already contain the template arguments).
8951
8952          For Ada, we need to scan the children of subprograms and lexical
8953          blocks as well because Ada allows the definition of nested
8954          entities that could be interesting for the debugger, such as
8955          nested subprograms for instance.  */
8956       if (last_die->has_children
8957           && (load_all
8958               || last_die->tag == DW_TAG_namespace
8959               || last_die->tag == DW_TAG_module
8960               || last_die->tag == DW_TAG_enumeration_type
8961               || (cu->language == language_cplus
8962                   && last_die->tag == DW_TAG_subprogram
8963                   && (last_die->name == NULL
8964                       || strchr (last_die->name, '<') == NULL))
8965               || (cu->language != language_c
8966                   && (last_die->tag == DW_TAG_class_type
8967                       || last_die->tag == DW_TAG_interface_type
8968                       || last_die->tag == DW_TAG_structure_type
8969                       || last_die->tag == DW_TAG_union_type))
8970               || (cu->language == language_ada
8971                   && (last_die->tag == DW_TAG_subprogram
8972                       || last_die->tag == DW_TAG_lexical_block))))
8973         {
8974           nesting_level++;
8975           parent_die = last_die;
8976           continue;
8977         }
8978
8979       /* Otherwise we skip to the next sibling, if any.  */
8980       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8981
8982       /* Back to the top, do it again.  */
8983     }
8984 }
8985
8986 /* Read a minimal amount of information into the minimal die structure.  */
8987
8988 static gdb_byte *
8989 read_partial_die (struct partial_die_info *part_die,
8990                   struct abbrev_info *abbrev,
8991                   unsigned int abbrev_len, bfd *abfd,
8992                   gdb_byte *buffer, gdb_byte *info_ptr,
8993                   struct dwarf2_cu *cu)
8994 {
8995   unsigned int i;
8996   struct attribute attr;
8997   int has_low_pc_attr = 0;
8998   int has_high_pc_attr = 0;
8999
9000   memset (part_die, 0, sizeof (struct partial_die_info));
9001
9002   part_die->offset = info_ptr - buffer;
9003
9004   info_ptr += abbrev_len;
9005
9006   if (abbrev == NULL)
9007     return info_ptr;
9008
9009   part_die->tag = abbrev->tag;
9010   part_die->has_children = abbrev->has_children;
9011
9012   for (i = 0; i < abbrev->num_attrs; ++i)
9013     {
9014       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9015
9016       /* Store the data if it is of an attribute we want to keep in a
9017          partial symbol table.  */
9018       switch (attr.name)
9019         {
9020         case DW_AT_name:
9021           switch (part_die->tag)
9022             {
9023             case DW_TAG_compile_unit:
9024             case DW_TAG_type_unit:
9025               /* Compilation units have a DW_AT_name that is a filename, not
9026                  a source language identifier.  */
9027             case DW_TAG_enumeration_type:
9028             case DW_TAG_enumerator:
9029               /* These tags always have simple identifiers already; no need
9030                  to canonicalize them.  */
9031               part_die->name = DW_STRING (&attr);
9032               break;
9033             default:
9034               part_die->name
9035                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9036                                             &cu->objfile->objfile_obstack);
9037               break;
9038             }
9039           break;
9040         case DW_AT_linkage_name:
9041         case DW_AT_MIPS_linkage_name:
9042           /* Note that both forms of linkage name might appear.  We
9043              assume they will be the same, and we only store the last
9044              one we see.  */
9045           if (cu->language == language_ada)
9046             part_die->name = DW_STRING (&attr);
9047           part_die->linkage_name = DW_STRING (&attr);
9048           break;
9049         case DW_AT_low_pc:
9050           has_low_pc_attr = 1;
9051           part_die->lowpc = DW_ADDR (&attr);
9052           break;
9053         case DW_AT_high_pc:
9054           has_high_pc_attr = 1;
9055           part_die->highpc = DW_ADDR (&attr);
9056           break;
9057         case DW_AT_location:
9058           /* Support the .debug_loc offsets.  */
9059           if (attr_form_is_block (&attr))
9060             {
9061                part_die->locdesc = DW_BLOCK (&attr);
9062             }
9063           else if (attr_form_is_section_offset (&attr))
9064             {
9065               dwarf2_complex_location_expr_complaint ();
9066             }
9067           else
9068             {
9069               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9070                                                      "partial symbol information");
9071             }
9072           break;
9073         case DW_AT_external:
9074           part_die->is_external = DW_UNSND (&attr);
9075           break;
9076         case DW_AT_declaration:
9077           part_die->is_declaration = DW_UNSND (&attr);
9078           break;
9079         case DW_AT_type:
9080           part_die->has_type = 1;
9081           break;
9082         case DW_AT_abstract_origin:
9083         case DW_AT_specification:
9084         case DW_AT_extension:
9085           part_die->has_specification = 1;
9086           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9087           break;
9088         case DW_AT_sibling:
9089           /* Ignore absolute siblings, they might point outside of
9090              the current compile unit.  */
9091           if (attr.form == DW_FORM_ref_addr)
9092             complaint (&symfile_complaints,
9093                        _("ignoring absolute DW_AT_sibling"));
9094           else
9095             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9096           break;
9097         case DW_AT_byte_size:
9098           part_die->has_byte_size = 1;
9099           break;
9100         case DW_AT_calling_convention:
9101           /* DWARF doesn't provide a way to identify a program's source-level
9102              entry point.  DW_AT_calling_convention attributes are only meant
9103              to describe functions' calling conventions.
9104
9105              However, because it's a necessary piece of information in
9106              Fortran, and because DW_CC_program is the only piece of debugging
9107              information whose definition refers to a 'main program' at all,
9108              several compilers have begun marking Fortran main programs with
9109              DW_CC_program --- even when those functions use the standard
9110              calling conventions.
9111
9112              So until DWARF specifies a way to provide this information and
9113              compilers pick up the new representation, we'll support this
9114              practice.  */
9115           if (DW_UNSND (&attr) == DW_CC_program
9116               && cu->language == language_fortran)
9117             {
9118               set_main_name (part_die->name);
9119
9120               /* As this DIE has a static linkage the name would be difficult
9121                  to look up later.  */
9122               language_of_main = language_fortran;
9123             }
9124           break;
9125         default:
9126           break;
9127         }
9128     }
9129
9130   /* When using the GNU linker, .gnu.linkonce. sections are used to
9131      eliminate duplicate copies of functions and vtables and such.
9132      The linker will arbitrarily choose one and discard the others.
9133      The AT_*_pc values for such functions refer to local labels in
9134      these sections.  If the section from that file was discarded, the
9135      labels are not in the output, so the relocs get a value of 0.
9136      If this is a discarded function, mark the pc bounds as invalid,
9137      so that GDB will ignore it.  */
9138   if (has_low_pc_attr && has_high_pc_attr
9139       && part_die->lowpc < part_die->highpc
9140       && (part_die->lowpc != 0
9141           || dwarf2_per_objfile->has_section_at_zero))
9142     part_die->has_pc_info = 1;
9143
9144   return info_ptr;
9145 }
9146
9147 /* Find a cached partial DIE at OFFSET in CU.  */
9148
9149 static struct partial_die_info *
9150 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9151 {
9152   struct partial_die_info *lookup_die = NULL;
9153   struct partial_die_info part_die;
9154
9155   part_die.offset = offset;
9156   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9157
9158   return lookup_die;
9159 }
9160
9161 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9162    except in the case of .debug_types DIEs which do not reference
9163    outside their CU (they do however referencing other types via
9164    DW_FORM_sig8).  */
9165
9166 static struct partial_die_info *
9167 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9168 {
9169   struct dwarf2_per_cu_data *per_cu = NULL;
9170   struct partial_die_info *pd = NULL;
9171
9172   if (cu->per_cu->from_debug_types)
9173     {
9174       pd = find_partial_die_in_comp_unit (offset, cu);
9175       if (pd != NULL)
9176         return pd;
9177       goto not_found;
9178     }
9179
9180   if (offset_in_cu_p (&cu->header, offset))
9181     {
9182       pd = find_partial_die_in_comp_unit (offset, cu);
9183       if (pd != NULL)
9184         return pd;
9185     }
9186
9187   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9188
9189   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9190     load_partial_comp_unit (per_cu, cu->objfile);
9191
9192   per_cu->cu->last_used = 0;
9193   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9194
9195   if (pd == NULL && per_cu->load_all_dies == 0)
9196     {
9197       struct cleanup *back_to;
9198       struct partial_die_info comp_unit_die;
9199       struct abbrev_info *abbrev;
9200       unsigned int bytes_read;
9201       char *info_ptr;
9202
9203       per_cu->load_all_dies = 1;
9204
9205       /* Re-read the DIEs.  */
9206       back_to = make_cleanup (null_cleanup, 0);
9207       if (per_cu->cu->dwarf2_abbrevs == NULL)
9208         {
9209           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9210           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9211         }
9212       info_ptr = (dwarf2_per_objfile->info.buffer
9213                   + per_cu->cu->header.offset
9214                   + per_cu->cu->header.first_die_offset);
9215       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9216       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9217                                    per_cu->cu->objfile->obfd,
9218                                    dwarf2_per_objfile->info.buffer, info_ptr,
9219                                    per_cu->cu);
9220       if (comp_unit_die.has_children)
9221         load_partial_dies (per_cu->cu->objfile->obfd,
9222                            dwarf2_per_objfile->info.buffer, info_ptr,
9223                            0, per_cu->cu);
9224       do_cleanups (back_to);
9225
9226       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9227     }
9228
9229  not_found:
9230
9231   if (pd == NULL)
9232     internal_error (__FILE__, __LINE__,
9233                     _("could not find partial DIE 0x%x "
9234                       "in cache [from module %s]\n"),
9235                     offset, bfd_get_filename (cu->objfile->obfd));
9236   return pd;
9237 }
9238
9239 /* See if we can figure out if the class lives in a namespace.  We do
9240    this by looking for a member function; its demangled name will
9241    contain namespace info, if there is any.  */
9242
9243 static void
9244 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9245                                   struct dwarf2_cu *cu)
9246 {
9247   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9248      what template types look like, because the demangler
9249      frequently doesn't give the same name as the debug info.  We
9250      could fix this by only using the demangled name to get the
9251      prefix (but see comment in read_structure_type).  */
9252
9253   struct partial_die_info *real_pdi;
9254   struct partial_die_info *child_pdi;
9255
9256   /* If this DIE (this DIE's specification, if any) has a parent, then
9257      we should not do this.  We'll prepend the parent's fully qualified
9258      name when we create the partial symbol.  */
9259
9260   real_pdi = struct_pdi;
9261   while (real_pdi->has_specification)
9262     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9263
9264   if (real_pdi->die_parent != NULL)
9265     return;
9266
9267   for (child_pdi = struct_pdi->die_child;
9268        child_pdi != NULL;
9269        child_pdi = child_pdi->die_sibling)
9270     {
9271       if (child_pdi->tag == DW_TAG_subprogram
9272           && child_pdi->linkage_name != NULL)
9273         {
9274           char *actual_class_name
9275             = language_class_name_from_physname (cu->language_defn,
9276                                                  child_pdi->linkage_name);
9277           if (actual_class_name != NULL)
9278             {
9279               struct_pdi->name
9280                 = obsavestring (actual_class_name,
9281                                 strlen (actual_class_name),
9282                                 &cu->objfile->objfile_obstack);
9283               xfree (actual_class_name);
9284             }
9285           break;
9286         }
9287     }
9288 }
9289
9290 /* Adjust PART_DIE before generating a symbol for it.  This function
9291    may set the is_external flag or change the DIE's name.  */
9292
9293 static void
9294 fixup_partial_die (struct partial_die_info *part_die,
9295                    struct dwarf2_cu *cu)
9296 {
9297   /* Once we've fixed up a die, there's no point in doing so again.
9298      This also avoids a memory leak if we were to call
9299      guess_partial_die_structure_name multiple times.  */
9300   if (part_die->fixup_called)
9301     return;
9302
9303   /* If we found a reference attribute and the DIE has no name, try
9304      to find a name in the referred to DIE.  */
9305
9306   if (part_die->name == NULL && part_die->has_specification)
9307     {
9308       struct partial_die_info *spec_die;
9309
9310       spec_die = find_partial_die (part_die->spec_offset, cu);
9311
9312       fixup_partial_die (spec_die, cu);
9313
9314       if (spec_die->name)
9315         {
9316           part_die->name = spec_die->name;
9317
9318           /* Copy DW_AT_external attribute if it is set.  */
9319           if (spec_die->is_external)
9320             part_die->is_external = spec_die->is_external;
9321         }
9322     }
9323
9324   /* Set default names for some unnamed DIEs.  */
9325
9326   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9327     part_die->name = "(anonymous namespace)";
9328
9329   /* If there is no parent die to provide a namespace, and there are
9330      children, see if we can determine the namespace from their linkage
9331      name.
9332      NOTE: We need to do this even if cu->has_namespace_info != 0.
9333      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9334   if (cu->language == language_cplus
9335       && dwarf2_per_objfile->types.asection != NULL
9336       && part_die->die_parent == NULL
9337       && part_die->has_children
9338       && (part_die->tag == DW_TAG_class_type
9339           || part_die->tag == DW_TAG_structure_type
9340           || part_die->tag == DW_TAG_union_type))
9341     guess_partial_die_structure_name (part_die, cu);
9342
9343   part_die->fixup_called = 1;
9344 }
9345
9346 /* Read an attribute value described by an attribute form.  */
9347
9348 static gdb_byte *
9349 read_attribute_value (struct attribute *attr, unsigned form,
9350                       bfd *abfd, gdb_byte *info_ptr,
9351                       struct dwarf2_cu *cu)
9352 {
9353   struct comp_unit_head *cu_header = &cu->header;
9354   unsigned int bytes_read;
9355   struct dwarf_block *blk;
9356
9357   attr->form = form;
9358   switch (form)
9359     {
9360     case DW_FORM_ref_addr:
9361       if (cu->header.version == 2)
9362         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9363       else
9364         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9365                                       &cu->header, &bytes_read);
9366       info_ptr += bytes_read;
9367       break;
9368     case DW_FORM_addr:
9369       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9370       info_ptr += bytes_read;
9371       break;
9372     case DW_FORM_block2:
9373       blk = dwarf_alloc_block (cu);
9374       blk->size = read_2_bytes (abfd, info_ptr);
9375       info_ptr += 2;
9376       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9377       info_ptr += blk->size;
9378       DW_BLOCK (attr) = blk;
9379       break;
9380     case DW_FORM_block4:
9381       blk = dwarf_alloc_block (cu);
9382       blk->size = read_4_bytes (abfd, info_ptr);
9383       info_ptr += 4;
9384       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9385       info_ptr += blk->size;
9386       DW_BLOCK (attr) = blk;
9387       break;
9388     case DW_FORM_data2:
9389       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9390       info_ptr += 2;
9391       break;
9392     case DW_FORM_data4:
9393       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9394       info_ptr += 4;
9395       break;
9396     case DW_FORM_data8:
9397       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9398       info_ptr += 8;
9399       break;
9400     case DW_FORM_sec_offset:
9401       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9402       info_ptr += bytes_read;
9403       break;
9404     case DW_FORM_string:
9405       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9406       DW_STRING_IS_CANONICAL (attr) = 0;
9407       info_ptr += bytes_read;
9408       break;
9409     case DW_FORM_strp:
9410       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9411                                                &bytes_read);
9412       DW_STRING_IS_CANONICAL (attr) = 0;
9413       info_ptr += bytes_read;
9414       break;
9415     case DW_FORM_exprloc:
9416     case DW_FORM_block:
9417       blk = dwarf_alloc_block (cu);
9418       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9419       info_ptr += bytes_read;
9420       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9421       info_ptr += blk->size;
9422       DW_BLOCK (attr) = blk;
9423       break;
9424     case DW_FORM_block1:
9425       blk = dwarf_alloc_block (cu);
9426       blk->size = read_1_byte (abfd, info_ptr);
9427       info_ptr += 1;
9428       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9429       info_ptr += blk->size;
9430       DW_BLOCK (attr) = blk;
9431       break;
9432     case DW_FORM_data1:
9433       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9434       info_ptr += 1;
9435       break;
9436     case DW_FORM_flag:
9437       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9438       info_ptr += 1;
9439       break;
9440     case DW_FORM_flag_present:
9441       DW_UNSND (attr) = 1;
9442       break;
9443     case DW_FORM_sdata:
9444       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9445       info_ptr += bytes_read;
9446       break;
9447     case DW_FORM_udata:
9448       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9449       info_ptr += bytes_read;
9450       break;
9451     case DW_FORM_ref1:
9452       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9453       info_ptr += 1;
9454       break;
9455     case DW_FORM_ref2:
9456       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9457       info_ptr += 2;
9458       break;
9459     case DW_FORM_ref4:
9460       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9461       info_ptr += 4;
9462       break;
9463     case DW_FORM_ref8:
9464       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9465       info_ptr += 8;
9466       break;
9467     case DW_FORM_sig8:
9468       /* Convert the signature to something we can record in DW_UNSND
9469          for later lookup.
9470          NOTE: This is NULL if the type wasn't found.  */
9471       DW_SIGNATURED_TYPE (attr) =
9472         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9473       info_ptr += 8;
9474       break;
9475     case DW_FORM_ref_udata:
9476       DW_ADDR (attr) = (cu->header.offset
9477                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9478       info_ptr += bytes_read;
9479       break;
9480     case DW_FORM_indirect:
9481       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9482       info_ptr += bytes_read;
9483       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9484       break;
9485     default:
9486       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9487              dwarf_form_name (form),
9488              bfd_get_filename (abfd));
9489     }
9490
9491   /* We have seen instances where the compiler tried to emit a byte
9492      size attribute of -1 which ended up being encoded as an unsigned
9493      0xffffffff.  Although 0xffffffff is technically a valid size value,
9494      an object of this size seems pretty unlikely so we can relatively
9495      safely treat these cases as if the size attribute was invalid and
9496      treat them as zero by default.  */
9497   if (attr->name == DW_AT_byte_size
9498       && form == DW_FORM_data4
9499       && DW_UNSND (attr) >= 0xffffffff)
9500     {
9501       complaint
9502         (&symfile_complaints,
9503          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9504          hex_string (DW_UNSND (attr)));
9505       DW_UNSND (attr) = 0;
9506     }
9507
9508   return info_ptr;
9509 }
9510
9511 /* Read an attribute described by an abbreviated attribute.  */
9512
9513 static gdb_byte *
9514 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9515                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9516 {
9517   attr->name = abbrev->name;
9518   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9519 }
9520
9521 /* Read dwarf information from a buffer.  */
9522
9523 static unsigned int
9524 read_1_byte (bfd *abfd, gdb_byte *buf)
9525 {
9526   return bfd_get_8 (abfd, buf);
9527 }
9528
9529 static int
9530 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9531 {
9532   return bfd_get_signed_8 (abfd, buf);
9533 }
9534
9535 static unsigned int
9536 read_2_bytes (bfd *abfd, gdb_byte *buf)
9537 {
9538   return bfd_get_16 (abfd, buf);
9539 }
9540
9541 static int
9542 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9543 {
9544   return bfd_get_signed_16 (abfd, buf);
9545 }
9546
9547 static unsigned int
9548 read_4_bytes (bfd *abfd, gdb_byte *buf)
9549 {
9550   return bfd_get_32 (abfd, buf);
9551 }
9552
9553 static int
9554 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9555 {
9556   return bfd_get_signed_32 (abfd, buf);
9557 }
9558
9559 static ULONGEST
9560 read_8_bytes (bfd *abfd, gdb_byte *buf)
9561 {
9562   return bfd_get_64 (abfd, buf);
9563 }
9564
9565 static CORE_ADDR
9566 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9567               unsigned int *bytes_read)
9568 {
9569   struct comp_unit_head *cu_header = &cu->header;
9570   CORE_ADDR retval = 0;
9571
9572   if (cu_header->signed_addr_p)
9573     {
9574       switch (cu_header->addr_size)
9575         {
9576         case 2:
9577           retval = bfd_get_signed_16 (abfd, buf);
9578           break;
9579         case 4:
9580           retval = bfd_get_signed_32 (abfd, buf);
9581           break;
9582         case 8:
9583           retval = bfd_get_signed_64 (abfd, buf);
9584           break;
9585         default:
9586           internal_error (__FILE__, __LINE__,
9587                           _("read_address: bad switch, signed [in module %s]"),
9588                           bfd_get_filename (abfd));
9589         }
9590     }
9591   else
9592     {
9593       switch (cu_header->addr_size)
9594         {
9595         case 2:
9596           retval = bfd_get_16 (abfd, buf);
9597           break;
9598         case 4:
9599           retval = bfd_get_32 (abfd, buf);
9600           break;
9601         case 8:
9602           retval = bfd_get_64 (abfd, buf);
9603           break;
9604         default:
9605           internal_error (__FILE__, __LINE__,
9606                           _("read_address: bad switch, "
9607                             "unsigned [in module %s]"),
9608                           bfd_get_filename (abfd));
9609         }
9610     }
9611
9612   *bytes_read = cu_header->addr_size;
9613   return retval;
9614 }
9615
9616 /* Read the initial length from a section.  The (draft) DWARF 3
9617    specification allows the initial length to take up either 4 bytes
9618    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9619    bytes describe the length and all offsets will be 8 bytes in length
9620    instead of 4.
9621
9622    An older, non-standard 64-bit format is also handled by this
9623    function.  The older format in question stores the initial length
9624    as an 8-byte quantity without an escape value.  Lengths greater
9625    than 2^32 aren't very common which means that the initial 4 bytes
9626    is almost always zero.  Since a length value of zero doesn't make
9627    sense for the 32-bit format, this initial zero can be considered to
9628    be an escape value which indicates the presence of the older 64-bit
9629    format.  As written, the code can't detect (old format) lengths
9630    greater than 4GB.  If it becomes necessary to handle lengths
9631    somewhat larger than 4GB, we could allow other small values (such
9632    as the non-sensical values of 1, 2, and 3) to also be used as
9633    escape values indicating the presence of the old format.
9634
9635    The value returned via bytes_read should be used to increment the
9636    relevant pointer after calling read_initial_length().
9637
9638    [ Note:  read_initial_length() and read_offset() are based on the
9639      document entitled "DWARF Debugging Information Format", revision
9640      3, draft 8, dated November 19, 2001.  This document was obtained
9641      from:
9642
9643         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9644
9645      This document is only a draft and is subject to change.  (So beware.)
9646
9647      Details regarding the older, non-standard 64-bit format were
9648      determined empirically by examining 64-bit ELF files produced by
9649      the SGI toolchain on an IRIX 6.5 machine.
9650
9651      - Kevin, July 16, 2002
9652    ] */
9653
9654 static LONGEST
9655 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9656 {
9657   LONGEST length = bfd_get_32 (abfd, buf);
9658
9659   if (length == 0xffffffff)
9660     {
9661       length = bfd_get_64 (abfd, buf + 4);
9662       *bytes_read = 12;
9663     }
9664   else if (length == 0)
9665     {
9666       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9667       length = bfd_get_64 (abfd, buf);
9668       *bytes_read = 8;
9669     }
9670   else
9671     {
9672       *bytes_read = 4;
9673     }
9674
9675   return length;
9676 }
9677
9678 /* Cover function for read_initial_length.
9679    Returns the length of the object at BUF, and stores the size of the
9680    initial length in *BYTES_READ and stores the size that offsets will be in
9681    *OFFSET_SIZE.
9682    If the initial length size is not equivalent to that specified in
9683    CU_HEADER then issue a complaint.
9684    This is useful when reading non-comp-unit headers.  */
9685
9686 static LONGEST
9687 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9688                                         const struct comp_unit_head *cu_header,
9689                                         unsigned int *bytes_read,
9690                                         unsigned int *offset_size)
9691 {
9692   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9693
9694   gdb_assert (cu_header->initial_length_size == 4
9695               || cu_header->initial_length_size == 8
9696               || cu_header->initial_length_size == 12);
9697
9698   if (cu_header->initial_length_size != *bytes_read)
9699     complaint (&symfile_complaints,
9700                _("intermixed 32-bit and 64-bit DWARF sections"));
9701
9702   *offset_size = (*bytes_read == 4) ? 4 : 8;
9703   return length;
9704 }
9705
9706 /* Read an offset from the data stream.  The size of the offset is
9707    given by cu_header->offset_size.  */
9708
9709 static LONGEST
9710 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9711              unsigned int *bytes_read)
9712 {
9713   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9714
9715   *bytes_read = cu_header->offset_size;
9716   return offset;
9717 }
9718
9719 /* Read an offset from the data stream.  */
9720
9721 static LONGEST
9722 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9723 {
9724   LONGEST retval = 0;
9725
9726   switch (offset_size)
9727     {
9728     case 4:
9729       retval = bfd_get_32 (abfd, buf);
9730       break;
9731     case 8:
9732       retval = bfd_get_64 (abfd, buf);
9733       break;
9734     default:
9735       internal_error (__FILE__, __LINE__,
9736                       _("read_offset_1: bad switch [in module %s]"),
9737                       bfd_get_filename (abfd));
9738     }
9739
9740   return retval;
9741 }
9742
9743 static gdb_byte *
9744 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9745 {
9746   /* If the size of a host char is 8 bits, we can return a pointer
9747      to the buffer, otherwise we have to copy the data to a buffer
9748      allocated on the temporary obstack.  */
9749   gdb_assert (HOST_CHAR_BIT == 8);
9750   return buf;
9751 }
9752
9753 static char *
9754 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9755 {
9756   /* If the size of a host char is 8 bits, we can return a pointer
9757      to the string, otherwise we have to copy the string to a buffer
9758      allocated on the temporary obstack.  */
9759   gdb_assert (HOST_CHAR_BIT == 8);
9760   if (*buf == '\0')
9761     {
9762       *bytes_read_ptr = 1;
9763       return NULL;
9764     }
9765   *bytes_read_ptr = strlen ((char *) buf) + 1;
9766   return (char *) buf;
9767 }
9768
9769 static char *
9770 read_indirect_string (bfd *abfd, gdb_byte *buf,
9771                       const struct comp_unit_head *cu_header,
9772                       unsigned int *bytes_read_ptr)
9773 {
9774   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9775
9776   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9777   if (dwarf2_per_objfile->str.buffer == NULL)
9778     {
9779       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9780                       bfd_get_filename (abfd));
9781       return NULL;
9782     }
9783   if (str_offset >= dwarf2_per_objfile->str.size)
9784     {
9785       error (_("DW_FORM_strp pointing outside of "
9786                ".debug_str section [in module %s]"),
9787              bfd_get_filename (abfd));
9788       return NULL;
9789     }
9790   gdb_assert (HOST_CHAR_BIT == 8);
9791   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9792     return NULL;
9793   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9794 }
9795
9796 static unsigned long
9797 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9798 {
9799   unsigned long result;
9800   unsigned int num_read;
9801   int i, shift;
9802   unsigned char byte;
9803
9804   result = 0;
9805   shift = 0;
9806   num_read = 0;
9807   i = 0;
9808   while (1)
9809     {
9810       byte = bfd_get_8 (abfd, buf);
9811       buf++;
9812       num_read++;
9813       result |= ((unsigned long)(byte & 127) << shift);
9814       if ((byte & 128) == 0)
9815         {
9816           break;
9817         }
9818       shift += 7;
9819     }
9820   *bytes_read_ptr = num_read;
9821   return result;
9822 }
9823
9824 static long
9825 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9826 {
9827   long result;
9828   int i, shift, num_read;
9829   unsigned char byte;
9830
9831   result = 0;
9832   shift = 0;
9833   num_read = 0;
9834   i = 0;
9835   while (1)
9836     {
9837       byte = bfd_get_8 (abfd, buf);
9838       buf++;
9839       num_read++;
9840       result |= ((long)(byte & 127) << shift);
9841       shift += 7;
9842       if ((byte & 128) == 0)
9843         {
9844           break;
9845         }
9846     }
9847   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9848     result |= -(((long)1) << shift);
9849   *bytes_read_ptr = num_read;
9850   return result;
9851 }
9852
9853 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9854
9855 static gdb_byte *
9856 skip_leb128 (bfd *abfd, gdb_byte *buf)
9857 {
9858   int byte;
9859
9860   while (1)
9861     {
9862       byte = bfd_get_8 (abfd, buf);
9863       buf++;
9864       if ((byte & 128) == 0)
9865         return buf;
9866     }
9867 }
9868
9869 static void
9870 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9871 {
9872   switch (lang)
9873     {
9874     case DW_LANG_C89:
9875     case DW_LANG_C99:
9876     case DW_LANG_C:
9877       cu->language = language_c;
9878       break;
9879     case DW_LANG_C_plus_plus:
9880       cu->language = language_cplus;
9881       break;
9882     case DW_LANG_D:
9883       cu->language = language_d;
9884       break;
9885     case DW_LANG_Fortran77:
9886     case DW_LANG_Fortran90:
9887     case DW_LANG_Fortran95:
9888       cu->language = language_fortran;
9889       break;
9890     case DW_LANG_Mips_Assembler:
9891       cu->language = language_asm;
9892       break;
9893     case DW_LANG_Java:
9894       cu->language = language_java;
9895       break;
9896     case DW_LANG_Ada83:
9897     case DW_LANG_Ada95:
9898       cu->language = language_ada;
9899       break;
9900     case DW_LANG_Modula2:
9901       cu->language = language_m2;
9902       break;
9903     case DW_LANG_Pascal83:
9904       cu->language = language_pascal;
9905       break;
9906     case DW_LANG_ObjC:
9907       cu->language = language_objc;
9908       break;
9909     case DW_LANG_Cobol74:
9910     case DW_LANG_Cobol85:
9911     default:
9912       cu->language = language_minimal;
9913       break;
9914     }
9915   cu->language_defn = language_def (cu->language);
9916 }
9917
9918 /* Return the named attribute or NULL if not there.  */
9919
9920 static struct attribute *
9921 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9922 {
9923   unsigned int i;
9924   struct attribute *spec = NULL;
9925
9926   for (i = 0; i < die->num_attrs; ++i)
9927     {
9928       if (die->attrs[i].name == name)
9929         return &die->attrs[i];
9930       if (die->attrs[i].name == DW_AT_specification
9931           || die->attrs[i].name == DW_AT_abstract_origin)
9932         spec = &die->attrs[i];
9933     }
9934
9935   if (spec)
9936     {
9937       die = follow_die_ref (die, spec, &cu);
9938       return dwarf2_attr (die, name, cu);
9939     }
9940
9941   return NULL;
9942 }
9943
9944 /* Return the named attribute or NULL if not there,
9945    but do not follow DW_AT_specification, etc.
9946    This is for use in contexts where we're reading .debug_types dies.
9947    Following DW_AT_specification, DW_AT_abstract_origin will take us
9948    back up the chain, and we want to go down.  */
9949
9950 static struct attribute *
9951 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9952                        struct dwarf2_cu *cu)
9953 {
9954   unsigned int i;
9955
9956   for (i = 0; i < die->num_attrs; ++i)
9957     if (die->attrs[i].name == name)
9958       return &die->attrs[i];
9959
9960   return NULL;
9961 }
9962
9963 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9964    and holds a non-zero value.  This function should only be used for
9965    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9966
9967 static int
9968 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9969 {
9970   struct attribute *attr = dwarf2_attr (die, name, cu);
9971
9972   return (attr && DW_UNSND (attr));
9973 }
9974
9975 static int
9976 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9977 {
9978   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9979      which value is non-zero.  However, we have to be careful with
9980      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9981      (via dwarf2_flag_true_p) follows this attribute.  So we may
9982      end up accidently finding a declaration attribute that belongs
9983      to a different DIE referenced by the specification attribute,
9984      even though the given DIE does not have a declaration attribute.  */
9985   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9986           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9987 }
9988
9989 /* Return the die giving the specification for DIE, if there is
9990    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9991    containing the return value on output.  If there is no
9992    specification, but there is an abstract origin, that is
9993    returned.  */
9994
9995 static struct die_info *
9996 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9997 {
9998   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9999                                              *spec_cu);
10000
10001   if (spec_attr == NULL)
10002     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10003
10004   if (spec_attr == NULL)
10005     return NULL;
10006   else
10007     return follow_die_ref (die, spec_attr, spec_cu);
10008 }
10009
10010 /* Free the line_header structure *LH, and any arrays and strings it
10011    refers to.
10012    NOTE: This is also used as a "cleanup" function.  */
10013
10014 static void
10015 free_line_header (struct line_header *lh)
10016 {
10017   if (lh->standard_opcode_lengths)
10018     xfree (lh->standard_opcode_lengths);
10019
10020   /* Remember that all the lh->file_names[i].name pointers are
10021      pointers into debug_line_buffer, and don't need to be freed.  */
10022   if (lh->file_names)
10023     xfree (lh->file_names);
10024
10025   /* Similarly for the include directory names.  */
10026   if (lh->include_dirs)
10027     xfree (lh->include_dirs);
10028
10029   xfree (lh);
10030 }
10031
10032 /* Add an entry to LH's include directory table.  */
10033
10034 static void
10035 add_include_dir (struct line_header *lh, char *include_dir)
10036 {
10037   /* Grow the array if necessary.  */
10038   if (lh->include_dirs_size == 0)
10039     {
10040       lh->include_dirs_size = 1; /* for testing */
10041       lh->include_dirs = xmalloc (lh->include_dirs_size
10042                                   * sizeof (*lh->include_dirs));
10043     }
10044   else if (lh->num_include_dirs >= lh->include_dirs_size)
10045     {
10046       lh->include_dirs_size *= 2;
10047       lh->include_dirs = xrealloc (lh->include_dirs,
10048                                    (lh->include_dirs_size
10049                                     * sizeof (*lh->include_dirs)));
10050     }
10051
10052   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10053 }
10054
10055 /* Add an entry to LH's file name table.  */
10056
10057 static void
10058 add_file_name (struct line_header *lh,
10059                char *name,
10060                unsigned int dir_index,
10061                unsigned int mod_time,
10062                unsigned int length)
10063 {
10064   struct file_entry *fe;
10065
10066   /* Grow the array if necessary.  */
10067   if (lh->file_names_size == 0)
10068     {
10069       lh->file_names_size = 1; /* for testing */
10070       lh->file_names = xmalloc (lh->file_names_size
10071                                 * sizeof (*lh->file_names));
10072     }
10073   else if (lh->num_file_names >= lh->file_names_size)
10074     {
10075       lh->file_names_size *= 2;
10076       lh->file_names = xrealloc (lh->file_names,
10077                                  (lh->file_names_size
10078                                   * sizeof (*lh->file_names)));
10079     }
10080
10081   fe = &lh->file_names[lh->num_file_names++];
10082   fe->name = name;
10083   fe->dir_index = dir_index;
10084   fe->mod_time = mod_time;
10085   fe->length = length;
10086   fe->included_p = 0;
10087   fe->symtab = NULL;
10088 }
10089
10090 /* Read the statement program header starting at OFFSET in
10091    .debug_line, according to the endianness of ABFD.  Return a pointer
10092    to a struct line_header, allocated using xmalloc.
10093
10094    NOTE: the strings in the include directory and file name tables of
10095    the returned object point into debug_line_buffer, and must not be
10096    freed.  */
10097
10098 static struct line_header *
10099 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10100                           struct dwarf2_cu *cu)
10101 {
10102   struct cleanup *back_to;
10103   struct line_header *lh;
10104   gdb_byte *line_ptr;
10105   unsigned int bytes_read, offset_size;
10106   int i;
10107   char *cur_dir, *cur_file;
10108
10109   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10110   if (dwarf2_per_objfile->line.buffer == NULL)
10111     {
10112       complaint (&symfile_complaints, _("missing .debug_line section"));
10113       return 0;
10114     }
10115
10116   /* Make sure that at least there's room for the total_length field.
10117      That could be 12 bytes long, but we're just going to fudge that.  */
10118   if (offset + 4 >= dwarf2_per_objfile->line.size)
10119     {
10120       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10121       return 0;
10122     }
10123
10124   lh = xmalloc (sizeof (*lh));
10125   memset (lh, 0, sizeof (*lh));
10126   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10127                           (void *) lh);
10128
10129   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10130
10131   /* Read in the header.  */
10132   lh->total_length =
10133     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10134                                             &bytes_read, &offset_size);
10135   line_ptr += bytes_read;
10136   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10137                                      + dwarf2_per_objfile->line.size))
10138     {
10139       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10140       return 0;
10141     }
10142   lh->statement_program_end = line_ptr + lh->total_length;
10143   lh->version = read_2_bytes (abfd, line_ptr);
10144   line_ptr += 2;
10145   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10146   line_ptr += offset_size;
10147   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10148   line_ptr += 1;
10149   if (lh->version >= 4)
10150     {
10151       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10152       line_ptr += 1;
10153     }
10154   else
10155     lh->maximum_ops_per_instruction = 1;
10156
10157   if (lh->maximum_ops_per_instruction == 0)
10158     {
10159       lh->maximum_ops_per_instruction = 1;
10160       complaint (&symfile_complaints,
10161                  _("invalid maximum_ops_per_instruction "
10162                    "in `.debug_line' section"));
10163     }
10164
10165   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10166   line_ptr += 1;
10167   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10168   line_ptr += 1;
10169   lh->line_range = read_1_byte (abfd, line_ptr);
10170   line_ptr += 1;
10171   lh->opcode_base = read_1_byte (abfd, line_ptr);
10172   line_ptr += 1;
10173   lh->standard_opcode_lengths
10174     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10175
10176   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10177   for (i = 1; i < lh->opcode_base; ++i)
10178     {
10179       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10180       line_ptr += 1;
10181     }
10182
10183   /* Read directory table.  */
10184   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10185     {
10186       line_ptr += bytes_read;
10187       add_include_dir (lh, cur_dir);
10188     }
10189   line_ptr += bytes_read;
10190
10191   /* Read file name table.  */
10192   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10193     {
10194       unsigned int dir_index, mod_time, length;
10195
10196       line_ptr += bytes_read;
10197       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10198       line_ptr += bytes_read;
10199       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10200       line_ptr += bytes_read;
10201       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10202       line_ptr += bytes_read;
10203
10204       add_file_name (lh, cur_file, dir_index, mod_time, length);
10205     }
10206   line_ptr += bytes_read;
10207   lh->statement_program_start = line_ptr;
10208
10209   if (line_ptr > (dwarf2_per_objfile->line.buffer
10210                   + dwarf2_per_objfile->line.size))
10211     complaint (&symfile_complaints,
10212                _("line number info header doesn't "
10213                  "fit in `.debug_line' section"));
10214
10215   discard_cleanups (back_to);
10216   return lh;
10217 }
10218
10219 /* This function exists to work around a bug in certain compilers
10220    (particularly GCC 2.95), in which the first line number marker of a
10221    function does not show up until after the prologue, right before
10222    the second line number marker.  This function shifts ADDRESS down
10223    to the beginning of the function if necessary, and is called on
10224    addresses passed to record_line.  */
10225
10226 static CORE_ADDR
10227 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10228 {
10229   struct function_range *fn;
10230
10231   /* Find the function_range containing address.  */
10232   if (!cu->first_fn)
10233     return address;
10234
10235   if (!cu->cached_fn)
10236     cu->cached_fn = cu->first_fn;
10237
10238   fn = cu->cached_fn;
10239   while (fn)
10240     if (fn->lowpc <= address && fn->highpc > address)
10241       goto found;
10242     else
10243       fn = fn->next;
10244
10245   fn = cu->first_fn;
10246   while (fn && fn != cu->cached_fn)
10247     if (fn->lowpc <= address && fn->highpc > address)
10248       goto found;
10249     else
10250       fn = fn->next;
10251
10252   return address;
10253
10254  found:
10255   if (fn->seen_line)
10256     return address;
10257   if (address != fn->lowpc)
10258     complaint (&symfile_complaints,
10259                _("misplaced first line number at 0x%lx for '%s'"),
10260                (unsigned long) address, fn->name);
10261   fn->seen_line = 1;
10262   return fn->lowpc;
10263 }
10264
10265 /* Subroutine of dwarf_decode_lines to simplify it.
10266    Return the file name of the psymtab for included file FILE_INDEX
10267    in line header LH of PST.
10268    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10269    If space for the result is malloc'd, it will be freed by a cleanup.
10270    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10271
10272 static char *
10273 psymtab_include_file_name (const struct line_header *lh, int file_index,
10274                            const struct partial_symtab *pst,
10275                            const char *comp_dir)
10276 {
10277   const struct file_entry fe = lh->file_names [file_index];
10278   char *include_name = fe.name;
10279   char *include_name_to_compare = include_name;
10280   char *dir_name = NULL;
10281   const char *pst_filename;
10282   char *copied_name = NULL;
10283   int file_is_pst;
10284
10285   if (fe.dir_index)
10286     dir_name = lh->include_dirs[fe.dir_index - 1];
10287
10288   if (!IS_ABSOLUTE_PATH (include_name)
10289       && (dir_name != NULL || comp_dir != NULL))
10290     {
10291       /* Avoid creating a duplicate psymtab for PST.
10292          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10293          Before we do the comparison, however, we need to account
10294          for DIR_NAME and COMP_DIR.
10295          First prepend dir_name (if non-NULL).  If we still don't
10296          have an absolute path prepend comp_dir (if non-NULL).
10297          However, the directory we record in the include-file's
10298          psymtab does not contain COMP_DIR (to match the
10299          corresponding symtab(s)).
10300
10301          Example:
10302
10303          bash$ cd /tmp
10304          bash$ gcc -g ./hello.c
10305          include_name = "hello.c"
10306          dir_name = "."
10307          DW_AT_comp_dir = comp_dir = "/tmp"
10308          DW_AT_name = "./hello.c"  */
10309
10310       if (dir_name != NULL)
10311         {
10312           include_name = concat (dir_name, SLASH_STRING,
10313                                  include_name, (char *)NULL);
10314           include_name_to_compare = include_name;
10315           make_cleanup (xfree, include_name);
10316         }
10317       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10318         {
10319           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10320                                             include_name, (char *)NULL);
10321         }
10322     }
10323
10324   pst_filename = pst->filename;
10325   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10326     {
10327       copied_name = concat (pst->dirname, SLASH_STRING,
10328                             pst_filename, (char *)NULL);
10329       pst_filename = copied_name;
10330     }
10331
10332   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10333
10334   if (include_name_to_compare != include_name)
10335     xfree (include_name_to_compare);
10336   if (copied_name != NULL)
10337     xfree (copied_name);
10338
10339   if (file_is_pst)
10340     return NULL;
10341   return include_name;
10342 }
10343
10344 /* Decode the Line Number Program (LNP) for the given line_header
10345    structure and CU.  The actual information extracted and the type
10346    of structures created from the LNP depends on the value of PST.
10347
10348    1. If PST is NULL, then this procedure uses the data from the program
10349       to create all necessary symbol tables, and their linetables.
10350
10351    2. If PST is not NULL, this procedure reads the program to determine
10352       the list of files included by the unit represented by PST, and
10353       builds all the associated partial symbol tables.
10354
10355    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10356    It is used for relative paths in the line table.
10357    NOTE: When processing partial symtabs (pst != NULL),
10358    comp_dir == pst->dirname.
10359
10360    NOTE: It is important that psymtabs have the same file name (via strcmp)
10361    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10362    symtab we don't use it in the name of the psymtabs we create.
10363    E.g. expand_line_sal requires this when finding psymtabs to expand.
10364    A good testcase for this is mb-inline.exp.  */
10365
10366 static void
10367 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10368                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10369 {
10370   gdb_byte *line_ptr, *extended_end;
10371   gdb_byte *line_end;
10372   unsigned int bytes_read, extended_len;
10373   unsigned char op_code, extended_op, adj_opcode;
10374   CORE_ADDR baseaddr;
10375   struct objfile *objfile = cu->objfile;
10376   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10377   const int decode_for_pst_p = (pst != NULL);
10378   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10379
10380   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10381
10382   line_ptr = lh->statement_program_start;
10383   line_end = lh->statement_program_end;
10384
10385   /* Read the statement sequences until there's nothing left.  */
10386   while (line_ptr < line_end)
10387     {
10388       /* state machine registers  */
10389       CORE_ADDR address = 0;
10390       unsigned int file = 1;
10391       unsigned int line = 1;
10392       unsigned int column = 0;
10393       int is_stmt = lh->default_is_stmt;
10394       int basic_block = 0;
10395       int end_sequence = 0;
10396       CORE_ADDR addr;
10397       unsigned char op_index = 0;
10398
10399       if (!decode_for_pst_p && lh->num_file_names >= file)
10400         {
10401           /* Start a subfile for the current file of the state machine.  */
10402           /* lh->include_dirs and lh->file_names are 0-based, but the
10403              directory and file name numbers in the statement program
10404              are 1-based.  */
10405           struct file_entry *fe = &lh->file_names[file - 1];
10406           char *dir = NULL;
10407
10408           if (fe->dir_index)
10409             dir = lh->include_dirs[fe->dir_index - 1];
10410
10411           dwarf2_start_subfile (fe->name, dir, comp_dir);
10412         }
10413
10414       /* Decode the table.  */
10415       while (!end_sequence)
10416         {
10417           op_code = read_1_byte (abfd, line_ptr);
10418           line_ptr += 1;
10419           if (line_ptr > line_end)
10420             {
10421               dwarf2_debug_line_missing_end_sequence_complaint ();
10422               break;
10423             }
10424
10425           if (op_code >= lh->opcode_base)
10426             {
10427               /* Special operand.  */
10428               adj_opcode = op_code - lh->opcode_base;
10429               address += (((op_index + (adj_opcode / lh->line_range))
10430                            / lh->maximum_ops_per_instruction)
10431                           * lh->minimum_instruction_length);
10432               op_index = ((op_index + (adj_opcode / lh->line_range))
10433                           % lh->maximum_ops_per_instruction);
10434               line += lh->line_base + (adj_opcode % lh->line_range);
10435               if (lh->num_file_names < file || file == 0)
10436                 dwarf2_debug_line_missing_file_complaint ();
10437               /* For now we ignore lines not starting on an
10438                  instruction boundary.  */
10439               else if (op_index == 0)
10440                 {
10441                   lh->file_names[file - 1].included_p = 1;
10442                   if (!decode_for_pst_p && is_stmt)
10443                     {
10444                       if (last_subfile != current_subfile)
10445                         {
10446                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10447                           if (last_subfile)
10448                             record_line (last_subfile, 0, addr);
10449                           last_subfile = current_subfile;
10450                         }
10451                       /* Append row to matrix using current values.  */
10452                       addr = check_cu_functions (address, cu);
10453                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10454                       record_line (current_subfile, line, addr);
10455                     }
10456                 }
10457               basic_block = 0;
10458             }
10459           else switch (op_code)
10460             {
10461             case DW_LNS_extended_op:
10462               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10463                                                    &bytes_read);
10464               line_ptr += bytes_read;
10465               extended_end = line_ptr + extended_len;
10466               extended_op = read_1_byte (abfd, line_ptr);
10467               line_ptr += 1;
10468               switch (extended_op)
10469                 {
10470                 case DW_LNE_end_sequence:
10471                   end_sequence = 1;
10472                   break;
10473                 case DW_LNE_set_address:
10474                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10475                   op_index = 0;
10476                   line_ptr += bytes_read;
10477                   address += baseaddr;
10478                   break;
10479                 case DW_LNE_define_file:
10480                   {
10481                     char *cur_file;
10482                     unsigned int dir_index, mod_time, length;
10483
10484                     cur_file = read_direct_string (abfd, line_ptr,
10485                                                    &bytes_read);
10486                     line_ptr += bytes_read;
10487                     dir_index =
10488                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10489                     line_ptr += bytes_read;
10490                     mod_time =
10491                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10492                     line_ptr += bytes_read;
10493                     length =
10494                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10495                     line_ptr += bytes_read;
10496                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10497                   }
10498                   break;
10499                 case DW_LNE_set_discriminator:
10500                   /* The discriminator is not interesting to the debugger;
10501                      just ignore it.  */
10502                   line_ptr = extended_end;
10503                   break;
10504                 default:
10505                   complaint (&symfile_complaints,
10506                              _("mangled .debug_line section"));
10507                   return;
10508                 }
10509               /* Make sure that we parsed the extended op correctly.  If e.g.
10510                  we expected a different address size than the producer used,
10511                  we may have read the wrong number of bytes.  */
10512               if (line_ptr != extended_end)
10513                 {
10514                   complaint (&symfile_complaints,
10515                              _("mangled .debug_line section"));
10516                   return;
10517                 }
10518               break;
10519             case DW_LNS_copy:
10520               if (lh->num_file_names < file || file == 0)
10521                 dwarf2_debug_line_missing_file_complaint ();
10522               else
10523                 {
10524                   lh->file_names[file - 1].included_p = 1;
10525                   if (!decode_for_pst_p && is_stmt)
10526                     {
10527                       if (last_subfile != current_subfile)
10528                         {
10529                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10530                           if (last_subfile)
10531                             record_line (last_subfile, 0, addr);
10532                           last_subfile = current_subfile;
10533                         }
10534                       addr = check_cu_functions (address, cu);
10535                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10536                       record_line (current_subfile, line, addr);
10537                     }
10538                 }
10539               basic_block = 0;
10540               break;
10541             case DW_LNS_advance_pc:
10542               {
10543                 CORE_ADDR adjust
10544                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10545
10546                 address += (((op_index + adjust)
10547                              / lh->maximum_ops_per_instruction)
10548                             * lh->minimum_instruction_length);
10549                 op_index = ((op_index + adjust)
10550                             % lh->maximum_ops_per_instruction);
10551                 line_ptr += bytes_read;
10552               }
10553               break;
10554             case DW_LNS_advance_line:
10555               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10556               line_ptr += bytes_read;
10557               break;
10558             case DW_LNS_set_file:
10559               {
10560                 /* The arrays lh->include_dirs and lh->file_names are
10561                    0-based, but the directory and file name numbers in
10562                    the statement program are 1-based.  */
10563                 struct file_entry *fe;
10564                 char *dir = NULL;
10565
10566                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10567                 line_ptr += bytes_read;
10568                 if (lh->num_file_names < file || file == 0)
10569                   dwarf2_debug_line_missing_file_complaint ();
10570                 else
10571                   {
10572                     fe = &lh->file_names[file - 1];
10573                     if (fe->dir_index)
10574                       dir = lh->include_dirs[fe->dir_index - 1];
10575                     if (!decode_for_pst_p)
10576                       {
10577                         last_subfile = current_subfile;
10578                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10579                       }
10580                   }
10581               }
10582               break;
10583             case DW_LNS_set_column:
10584               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10585               line_ptr += bytes_read;
10586               break;
10587             case DW_LNS_negate_stmt:
10588               is_stmt = (!is_stmt);
10589               break;
10590             case DW_LNS_set_basic_block:
10591               basic_block = 1;
10592               break;
10593             /* Add to the address register of the state machine the
10594                address increment value corresponding to special opcode
10595                255.  I.e., this value is scaled by the minimum
10596                instruction length since special opcode 255 would have
10597                scaled the increment.  */
10598             case DW_LNS_const_add_pc:
10599               {
10600                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10601
10602                 address += (((op_index + adjust)
10603                              / lh->maximum_ops_per_instruction)
10604                             * lh->minimum_instruction_length);
10605                 op_index = ((op_index + adjust)
10606                             % lh->maximum_ops_per_instruction);
10607               }
10608               break;
10609             case DW_LNS_fixed_advance_pc:
10610               address += read_2_bytes (abfd, line_ptr);
10611               op_index = 0;
10612               line_ptr += 2;
10613               break;
10614             default:
10615               {
10616                 /* Unknown standard opcode, ignore it.  */
10617                 int i;
10618
10619                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10620                   {
10621                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10622                     line_ptr += bytes_read;
10623                   }
10624               }
10625             }
10626         }
10627       if (lh->num_file_names < file || file == 0)
10628         dwarf2_debug_line_missing_file_complaint ();
10629       else
10630         {
10631           lh->file_names[file - 1].included_p = 1;
10632           if (!decode_for_pst_p)
10633             {
10634               addr = gdbarch_addr_bits_remove (gdbarch, address);
10635               record_line (current_subfile, 0, addr);
10636             }
10637         }
10638     }
10639
10640   if (decode_for_pst_p)
10641     {
10642       int file_index;
10643
10644       /* Now that we're done scanning the Line Header Program, we can
10645          create the psymtab of each included file.  */
10646       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10647         if (lh->file_names[file_index].included_p == 1)
10648           {
10649             char *include_name =
10650               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10651             if (include_name != NULL)
10652               dwarf2_create_include_psymtab (include_name, pst, objfile);
10653           }
10654     }
10655   else
10656     {
10657       /* Make sure a symtab is created for every file, even files
10658          which contain only variables (i.e. no code with associated
10659          line numbers).  */
10660
10661       int i;
10662       struct file_entry *fe;
10663
10664       for (i = 0; i < lh->num_file_names; i++)
10665         {
10666           char *dir = NULL;
10667
10668           fe = &lh->file_names[i];
10669           if (fe->dir_index)
10670             dir = lh->include_dirs[fe->dir_index - 1];
10671           dwarf2_start_subfile (fe->name, dir, comp_dir);
10672
10673           /* Skip the main file; we don't need it, and it must be
10674              allocated last, so that it will show up before the
10675              non-primary symtabs in the objfile's symtab list.  */
10676           if (current_subfile == first_subfile)
10677             continue;
10678
10679           if (current_subfile->symtab == NULL)
10680             current_subfile->symtab = allocate_symtab (current_subfile->name,
10681                                                        cu->objfile);
10682           fe->symtab = current_subfile->symtab;
10683         }
10684     }
10685 }
10686
10687 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10688    DIRNAME the name of the source directory which contains FILENAME
10689    or NULL if not known.  COMP_DIR is the compilation directory for the
10690    linetable's compilation unit or NULL if not known.
10691    This routine tries to keep line numbers from identical absolute and
10692    relative file names in a common subfile.
10693
10694    Using the `list' example from the GDB testsuite, which resides in
10695    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10696    of /srcdir/list0.c yields the following debugging information for list0.c:
10697
10698    DW_AT_name:          /srcdir/list0.c
10699    DW_AT_comp_dir:              /compdir
10700    files.files[0].name: list0.h
10701    files.files[0].dir:  /srcdir
10702    files.files[1].name: list0.c
10703    files.files[1].dir:  /srcdir
10704
10705    The line number information for list0.c has to end up in a single
10706    subfile, so that `break /srcdir/list0.c:1' works as expected.
10707    start_subfile will ensure that this happens provided that we pass the
10708    concatenation of files.files[1].dir and files.files[1].name as the
10709    subfile's name.  */
10710
10711 static void
10712 dwarf2_start_subfile (char *filename, const char *dirname,
10713                       const char *comp_dir)
10714 {
10715   char *fullname;
10716
10717   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10718      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10719      second argument to start_subfile.  To be consistent, we do the
10720      same here.  In order not to lose the line information directory,
10721      we concatenate it to the filename when it makes sense.
10722      Note that the Dwarf3 standard says (speaking of filenames in line
10723      information): ``The directory index is ignored for file names
10724      that represent full path names''.  Thus ignoring dirname in the
10725      `else' branch below isn't an issue.  */
10726
10727   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10728     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10729   else
10730     fullname = filename;
10731
10732   start_subfile (fullname, comp_dir);
10733
10734   if (fullname != filename)
10735     xfree (fullname);
10736 }
10737
10738 static void
10739 var_decode_location (struct attribute *attr, struct symbol *sym,
10740                      struct dwarf2_cu *cu)
10741 {
10742   struct objfile *objfile = cu->objfile;
10743   struct comp_unit_head *cu_header = &cu->header;
10744
10745   /* NOTE drow/2003-01-30: There used to be a comment and some special
10746      code here to turn a symbol with DW_AT_external and a
10747      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10748      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10749      with some versions of binutils) where shared libraries could have
10750      relocations against symbols in their debug information - the
10751      minimal symbol would have the right address, but the debug info
10752      would not.  It's no longer necessary, because we will explicitly
10753      apply relocations when we read in the debug information now.  */
10754
10755   /* A DW_AT_location attribute with no contents indicates that a
10756      variable has been optimized away.  */
10757   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10758     {
10759       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10760       return;
10761     }
10762
10763   /* Handle one degenerate form of location expression specially, to
10764      preserve GDB's previous behavior when section offsets are
10765      specified.  If this is just a DW_OP_addr then mark this symbol
10766      as LOC_STATIC.  */
10767
10768   if (attr_form_is_block (attr)
10769       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10770       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10771     {
10772       unsigned int dummy;
10773
10774       SYMBOL_VALUE_ADDRESS (sym) =
10775         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10776       SYMBOL_CLASS (sym) = LOC_STATIC;
10777       fixup_symbol_section (sym, objfile);
10778       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10779                                               SYMBOL_SECTION (sym));
10780       return;
10781     }
10782
10783   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10784      expression evaluator, and use LOC_COMPUTED only when necessary
10785      (i.e. when the value of a register or memory location is
10786      referenced, or a thread-local block, etc.).  Then again, it might
10787      not be worthwhile.  I'm assuming that it isn't unless performance
10788      or memory numbers show me otherwise.  */
10789
10790   dwarf2_symbol_mark_computed (attr, sym, cu);
10791   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10792 }
10793
10794 /* Given a pointer to a DWARF information entry, figure out if we need
10795    to make a symbol table entry for it, and if so, create a new entry
10796    and return a pointer to it.
10797    If TYPE is NULL, determine symbol type from the die, otherwise
10798    used the passed type.
10799    If SPACE is not NULL, use it to hold the new symbol.  If it is
10800    NULL, allocate a new symbol on the objfile's obstack.  */
10801
10802 static struct symbol *
10803 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10804                  struct symbol *space)
10805 {
10806   struct objfile *objfile = cu->objfile;
10807   struct symbol *sym = NULL;
10808   char *name;
10809   struct attribute *attr = NULL;
10810   struct attribute *attr2 = NULL;
10811   CORE_ADDR baseaddr;
10812   struct pending **list_to_add = NULL;
10813
10814   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10815
10816   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10817
10818   name = dwarf2_name (die, cu);
10819   if (name)
10820     {
10821       const char *linkagename;
10822       int suppress_add = 0;
10823
10824       if (space)
10825         sym = space;
10826       else
10827         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10828       OBJSTAT (objfile, n_syms++);
10829
10830       /* Cache this symbol's name and the name's demangled form (if any).  */
10831       SYMBOL_SET_LANGUAGE (sym, cu->language);
10832       linkagename = dwarf2_physname (name, die, cu);
10833       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10834
10835       /* Fortran does not have mangling standard and the mangling does differ
10836          between gfortran, iFort etc.  */
10837       if (cu->language == language_fortran
10838           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10839         symbol_set_demangled_name (&(sym->ginfo),
10840                                    (char *) dwarf2_full_name (name, die, cu),
10841                                    NULL);
10842
10843       /* Default assumptions.
10844          Use the passed type or decode it from the die.  */
10845       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10846       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10847       if (type != NULL)
10848         SYMBOL_TYPE (sym) = type;
10849       else
10850         SYMBOL_TYPE (sym) = die_type (die, cu);
10851       attr = dwarf2_attr (die,
10852                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10853                           cu);
10854       if (attr)
10855         {
10856           SYMBOL_LINE (sym) = DW_UNSND (attr);
10857         }
10858
10859       attr = dwarf2_attr (die,
10860                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10861                           cu);
10862       if (attr)
10863         {
10864           int file_index = DW_UNSND (attr);
10865
10866           if (cu->line_header == NULL
10867               || file_index > cu->line_header->num_file_names)
10868             complaint (&symfile_complaints,
10869                        _("file index out of range"));
10870           else if (file_index > 0)
10871             {
10872               struct file_entry *fe;
10873
10874               fe = &cu->line_header->file_names[file_index - 1];
10875               SYMBOL_SYMTAB (sym) = fe->symtab;
10876             }
10877         }
10878
10879       switch (die->tag)
10880         {
10881         case DW_TAG_label:
10882           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10883           if (attr)
10884             {
10885               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10886             }
10887           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10888           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10889           SYMBOL_CLASS (sym) = LOC_LABEL;
10890           add_symbol_to_list (sym, cu->list_in_scope);
10891           break;
10892         case DW_TAG_subprogram:
10893           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10894              finish_block.  */
10895           SYMBOL_CLASS (sym) = LOC_BLOCK;
10896           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10897           if ((attr2 && (DW_UNSND (attr2) != 0))
10898               || cu->language == language_ada)
10899             {
10900               /* Subprograms marked external are stored as a global symbol.
10901                  Ada subprograms, whether marked external or not, are always
10902                  stored as a global symbol, because we want to be able to
10903                  access them globally.  For instance, we want to be able
10904                  to break on a nested subprogram without having to
10905                  specify the context.  */
10906               list_to_add = &global_symbols;
10907             }
10908           else
10909             {
10910               list_to_add = cu->list_in_scope;
10911             }
10912           break;
10913         case DW_TAG_inlined_subroutine:
10914           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10915              finish_block.  */
10916           SYMBOL_CLASS (sym) = LOC_BLOCK;
10917           SYMBOL_INLINED (sym) = 1;
10918           /* Do not add the symbol to any lists.  It will be found via
10919              BLOCK_FUNCTION from the blockvector.  */
10920           break;
10921         case DW_TAG_template_value_param:
10922           suppress_add = 1;
10923           /* Fall through.  */
10924         case DW_TAG_constant:
10925         case DW_TAG_variable:
10926         case DW_TAG_member:
10927           /* Compilation with minimal debug info may result in
10928              variables with missing type entries.  Change the
10929              misleading `void' type to something sensible.  */
10930           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10931             SYMBOL_TYPE (sym)
10932               = objfile_type (objfile)->nodebug_data_symbol;
10933
10934           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10935           /* In the case of DW_TAG_member, we should only be called for
10936              static const members.  */
10937           if (die->tag == DW_TAG_member)
10938             {
10939               /* dwarf2_add_field uses die_is_declaration,
10940                  so we do the same.  */
10941               gdb_assert (die_is_declaration (die, cu));
10942               gdb_assert (attr);
10943             }
10944           if (attr)
10945             {
10946               dwarf2_const_value (attr, sym, cu);
10947               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10948               if (!suppress_add)
10949                 {
10950                   if (attr2 && (DW_UNSND (attr2) != 0))
10951                     list_to_add = &global_symbols;
10952                   else
10953                     list_to_add = cu->list_in_scope;
10954                 }
10955               break;
10956             }
10957           attr = dwarf2_attr (die, DW_AT_location, cu);
10958           if (attr)
10959             {
10960               var_decode_location (attr, sym, cu);
10961               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10962               if (SYMBOL_CLASS (sym) == LOC_STATIC
10963                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10964                   && !dwarf2_per_objfile->has_section_at_zero)
10965                 {
10966                   /* When a static variable is eliminated by the linker,
10967                      the corresponding debug information is not stripped
10968                      out, but the variable address is set to null;
10969                      do not add such variables into symbol table.  */
10970                 }
10971               else if (attr2 && (DW_UNSND (attr2) != 0))
10972                 {
10973                   /* Workaround gfortran PR debug/40040 - it uses
10974                      DW_AT_location for variables in -fPIC libraries which may
10975                      get overriden by other libraries/executable and get
10976                      a different address.  Resolve it by the minimal symbol
10977                      which may come from inferior's executable using copy
10978                      relocation.  Make this workaround only for gfortran as for
10979                      other compilers GDB cannot guess the minimal symbol
10980                      Fortran mangling kind.  */
10981                   if (cu->language == language_fortran && die->parent
10982                       && die->parent->tag == DW_TAG_module
10983                       && cu->producer
10984                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10985                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10986
10987                   /* A variable with DW_AT_external is never static,
10988                      but it may be block-scoped.  */
10989                   list_to_add = (cu->list_in_scope == &file_symbols
10990                                  ? &global_symbols : cu->list_in_scope);
10991                 }
10992               else
10993                 list_to_add = cu->list_in_scope;
10994             }
10995           else
10996             {
10997               /* We do not know the address of this symbol.
10998                  If it is an external symbol and we have type information
10999                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11000                  The address of the variable will then be determined from
11001                  the minimal symbol table whenever the variable is
11002                  referenced.  */
11003               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11004               if (attr2 && (DW_UNSND (attr2) != 0)
11005                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11006                 {
11007                   /* A variable with DW_AT_external is never static, but it
11008                      may be block-scoped.  */
11009                   list_to_add = (cu->list_in_scope == &file_symbols
11010                                  ? &global_symbols : cu->list_in_scope);
11011
11012                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11013                 }
11014               else if (!die_is_declaration (die, cu))
11015                 {
11016                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11017                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11018                   if (!suppress_add)
11019                     list_to_add = cu->list_in_scope;
11020                 }
11021             }
11022           break;
11023         case DW_TAG_formal_parameter:
11024           /* If we are inside a function, mark this as an argument.  If
11025              not, we might be looking at an argument to an inlined function
11026              when we do not have enough information to show inlined frames;
11027              pretend it's a local variable in that case so that the user can
11028              still see it.  */
11029           if (context_stack_depth > 0
11030               && context_stack[context_stack_depth - 1].name != NULL)
11031             SYMBOL_IS_ARGUMENT (sym) = 1;
11032           attr = dwarf2_attr (die, DW_AT_location, cu);
11033           if (attr)
11034             {
11035               var_decode_location (attr, sym, cu);
11036             }
11037           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11038           if (attr)
11039             {
11040               dwarf2_const_value (attr, sym, cu);
11041             }
11042           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11043           if (attr && DW_UNSND (attr))
11044             {
11045               struct type *ref_type;
11046
11047               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11048               SYMBOL_TYPE (sym) = ref_type;
11049             }
11050
11051           list_to_add = cu->list_in_scope;
11052           break;
11053         case DW_TAG_unspecified_parameters:
11054           /* From varargs functions; gdb doesn't seem to have any
11055              interest in this information, so just ignore it for now.
11056              (FIXME?) */
11057           break;
11058         case DW_TAG_template_type_param:
11059           suppress_add = 1;
11060           /* Fall through.  */
11061         case DW_TAG_class_type:
11062         case DW_TAG_interface_type:
11063         case DW_TAG_structure_type:
11064         case DW_TAG_union_type:
11065         case DW_TAG_set_type:
11066         case DW_TAG_enumeration_type:
11067           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11068           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11069
11070           {
11071             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11072                really ever be static objects: otherwise, if you try
11073                to, say, break of a class's method and you're in a file
11074                which doesn't mention that class, it won't work unless
11075                the check for all static symbols in lookup_symbol_aux
11076                saves you.  See the OtherFileClass tests in
11077                gdb.c++/namespace.exp.  */
11078
11079             if (!suppress_add)
11080               {
11081                 list_to_add = (cu->list_in_scope == &file_symbols
11082                                && (cu->language == language_cplus
11083                                    || cu->language == language_java)
11084                                ? &global_symbols : cu->list_in_scope);
11085
11086                 /* The semantics of C++ state that "struct foo {
11087                    ... }" also defines a typedef for "foo".  A Java
11088                    class declaration also defines a typedef for the
11089                    class.  */
11090                 if (cu->language == language_cplus
11091                     || cu->language == language_java
11092                     || cu->language == language_ada)
11093                   {
11094                     /* The symbol's name is already allocated along
11095                        with this objfile, so we don't need to
11096                        duplicate it for the type.  */
11097                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11098                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11099                   }
11100               }
11101           }
11102           break;
11103         case DW_TAG_typedef:
11104           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11105           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11106           list_to_add = cu->list_in_scope;
11107           break;
11108         case DW_TAG_base_type:
11109         case DW_TAG_subrange_type:
11110           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11111           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11112           list_to_add = cu->list_in_scope;
11113           break;
11114         case DW_TAG_enumerator:
11115           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11116           if (attr)
11117             {
11118               dwarf2_const_value (attr, sym, cu);
11119             }
11120           {
11121             /* NOTE: carlton/2003-11-10: See comment above in the
11122                DW_TAG_class_type, etc. block.  */
11123
11124             list_to_add = (cu->list_in_scope == &file_symbols
11125                            && (cu->language == language_cplus
11126                                || cu->language == language_java)
11127                            ? &global_symbols : cu->list_in_scope);
11128           }
11129           break;
11130         case DW_TAG_namespace:
11131           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11132           list_to_add = &global_symbols;
11133           break;
11134         default:
11135           /* Not a tag we recognize.  Hopefully we aren't processing
11136              trash data, but since we must specifically ignore things
11137              we don't recognize, there is nothing else we should do at
11138              this point.  */
11139           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11140                      dwarf_tag_name (die->tag));
11141           break;
11142         }
11143
11144       if (suppress_add)
11145         {
11146           sym->hash_next = objfile->template_symbols;
11147           objfile->template_symbols = sym;
11148           list_to_add = NULL;
11149         }
11150
11151       if (list_to_add != NULL)
11152         add_symbol_to_list (sym, list_to_add);
11153
11154       /* For the benefit of old versions of GCC, check for anonymous
11155          namespaces based on the demangled name.  */
11156       if (!processing_has_namespace_info
11157           && cu->language == language_cplus)
11158         cp_scan_for_anonymous_namespaces (sym);
11159     }
11160   return (sym);
11161 }
11162
11163 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11164
11165 static struct symbol *
11166 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11167 {
11168   return new_symbol_full (die, type, cu, NULL);
11169 }
11170
11171 /* Given an attr with a DW_FORM_dataN value in host byte order,
11172    zero-extend it as appropriate for the symbol's type.  The DWARF
11173    standard (v4) is not entirely clear about the meaning of using
11174    DW_FORM_dataN for a constant with a signed type, where the type is
11175    wider than the data.  The conclusion of a discussion on the DWARF
11176    list was that this is unspecified.  We choose to always zero-extend
11177    because that is the interpretation long in use by GCC.  */
11178
11179 static gdb_byte *
11180 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11181                          const char *name, struct obstack *obstack,
11182                          struct dwarf2_cu *cu, long *value, int bits)
11183 {
11184   struct objfile *objfile = cu->objfile;
11185   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11186                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11187   LONGEST l = DW_UNSND (attr);
11188
11189   if (bits < sizeof (*value) * 8)
11190     {
11191       l &= ((LONGEST) 1 << bits) - 1;
11192       *value = l;
11193     }
11194   else if (bits == sizeof (*value) * 8)
11195     *value = l;
11196   else
11197     {
11198       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11199       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11200       return bytes;
11201     }
11202
11203   return NULL;
11204 }
11205
11206 /* Read a constant value from an attribute.  Either set *VALUE, or if
11207    the value does not fit in *VALUE, set *BYTES - either already
11208    allocated on the objfile obstack, or newly allocated on OBSTACK,
11209    or, set *BATON, if we translated the constant to a location
11210    expression.  */
11211
11212 static void
11213 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11214                          const char *name, struct obstack *obstack,
11215                          struct dwarf2_cu *cu,
11216                          long *value, gdb_byte **bytes,
11217                          struct dwarf2_locexpr_baton **baton)
11218 {
11219   struct objfile *objfile = cu->objfile;
11220   struct comp_unit_head *cu_header = &cu->header;
11221   struct dwarf_block *blk;
11222   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11223                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11224
11225   *value = 0;
11226   *bytes = NULL;
11227   *baton = NULL;
11228
11229   switch (attr->form)
11230     {
11231     case DW_FORM_addr:
11232       {
11233         gdb_byte *data;
11234
11235         if (TYPE_LENGTH (type) != cu_header->addr_size)
11236           dwarf2_const_value_length_mismatch_complaint (name,
11237                                                         cu_header->addr_size,
11238                                                         TYPE_LENGTH (type));
11239         /* Symbols of this form are reasonably rare, so we just
11240            piggyback on the existing location code rather than writing
11241            a new implementation of symbol_computed_ops.  */
11242         *baton = obstack_alloc (&objfile->objfile_obstack,
11243                                 sizeof (struct dwarf2_locexpr_baton));
11244         (*baton)->per_cu = cu->per_cu;
11245         gdb_assert ((*baton)->per_cu);
11246
11247         (*baton)->size = 2 + cu_header->addr_size;
11248         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11249         (*baton)->data = data;
11250
11251         data[0] = DW_OP_addr;
11252         store_unsigned_integer (&data[1], cu_header->addr_size,
11253                                 byte_order, DW_ADDR (attr));
11254         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11255       }
11256       break;
11257     case DW_FORM_string:
11258     case DW_FORM_strp:
11259       /* DW_STRING is already allocated on the objfile obstack, point
11260          directly to it.  */
11261       *bytes = (gdb_byte *) DW_STRING (attr);
11262       break;
11263     case DW_FORM_block1:
11264     case DW_FORM_block2:
11265     case DW_FORM_block4:
11266     case DW_FORM_block:
11267     case DW_FORM_exprloc:
11268       blk = DW_BLOCK (attr);
11269       if (TYPE_LENGTH (type) != blk->size)
11270         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11271                                                       TYPE_LENGTH (type));
11272       *bytes = blk->data;
11273       break;
11274
11275       /* The DW_AT_const_value attributes are supposed to carry the
11276          symbol's value "represented as it would be on the target
11277          architecture."  By the time we get here, it's already been
11278          converted to host endianness, so we just need to sign- or
11279          zero-extend it as appropriate.  */
11280     case DW_FORM_data1:
11281       *bytes = dwarf2_const_value_data (attr, type, name,
11282                                         obstack, cu, value, 8);
11283       break;
11284     case DW_FORM_data2:
11285       *bytes = dwarf2_const_value_data (attr, type, name,
11286                                         obstack, cu, value, 16);
11287       break;
11288     case DW_FORM_data4:
11289       *bytes = dwarf2_const_value_data (attr, type, name,
11290                                         obstack, cu, value, 32);
11291       break;
11292     case DW_FORM_data8:
11293       *bytes = dwarf2_const_value_data (attr, type, name,
11294                                         obstack, cu, value, 64);
11295       break;
11296
11297     case DW_FORM_sdata:
11298       *value = DW_SND (attr);
11299       break;
11300
11301     case DW_FORM_udata:
11302       *value = DW_UNSND (attr);
11303       break;
11304
11305     default:
11306       complaint (&symfile_complaints,
11307                  _("unsupported const value attribute form: '%s'"),
11308                  dwarf_form_name (attr->form));
11309       *value = 0;
11310       break;
11311     }
11312 }
11313
11314
11315 /* Copy constant value from an attribute to a symbol.  */
11316
11317 static void
11318 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11319                     struct dwarf2_cu *cu)
11320 {
11321   struct objfile *objfile = cu->objfile;
11322   struct comp_unit_head *cu_header = &cu->header;
11323   long value;
11324   gdb_byte *bytes;
11325   struct dwarf2_locexpr_baton *baton;
11326
11327   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11328                            SYMBOL_PRINT_NAME (sym),
11329                            &objfile->objfile_obstack, cu,
11330                            &value, &bytes, &baton);
11331
11332   if (baton != NULL)
11333     {
11334       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11335       SYMBOL_LOCATION_BATON (sym) = baton;
11336       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11337     }
11338   else if (bytes != NULL)
11339      {
11340       SYMBOL_VALUE_BYTES (sym) = bytes;
11341       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11342     }
11343   else
11344     {
11345       SYMBOL_VALUE (sym) = value;
11346       SYMBOL_CLASS (sym) = LOC_CONST;
11347     }
11348 }
11349
11350 /* Return the type of the die in question using its DW_AT_type attribute.  */
11351
11352 static struct type *
11353 die_type (struct die_info *die, struct dwarf2_cu *cu)
11354 {
11355   struct attribute *type_attr;
11356
11357   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11358   if (!type_attr)
11359     {
11360       /* A missing DW_AT_type represents a void type.  */
11361       return objfile_type (cu->objfile)->builtin_void;
11362     }
11363
11364   return lookup_die_type (die, type_attr, cu);
11365 }
11366
11367 /* True iff CU's producer generates GNAT Ada auxiliary information
11368    that allows to find parallel types through that information instead
11369    of having to do expensive parallel lookups by type name.  */
11370
11371 static int
11372 need_gnat_info (struct dwarf2_cu *cu)
11373 {
11374   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11375      of GNAT produces this auxiliary information, without any indication
11376      that it is produced.  Part of enhancing the FSF version of GNAT
11377      to produce that information will be to put in place an indicator
11378      that we can use in order to determine whether the descriptive type
11379      info is available or not.  One suggestion that has been made is
11380      to use a new attribute, attached to the CU die.  For now, assume
11381      that the descriptive type info is not available.  */
11382   return 0;
11383 }
11384
11385 /* Return the auxiliary type of the die in question using its
11386    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11387    attribute is not present.  */
11388
11389 static struct type *
11390 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11391 {
11392   struct attribute *type_attr;
11393
11394   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11395   if (!type_attr)
11396     return NULL;
11397
11398   return lookup_die_type (die, type_attr, cu);
11399 }
11400
11401 /* If DIE has a descriptive_type attribute, then set the TYPE's
11402    descriptive type accordingly.  */
11403
11404 static void
11405 set_descriptive_type (struct type *type, struct die_info *die,
11406                       struct dwarf2_cu *cu)
11407 {
11408   struct type *descriptive_type = die_descriptive_type (die, cu);
11409
11410   if (descriptive_type)
11411     {
11412       ALLOCATE_GNAT_AUX_TYPE (type);
11413       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11414     }
11415 }
11416
11417 /* Return the containing type of the die in question using its
11418    DW_AT_containing_type attribute.  */
11419
11420 static struct type *
11421 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11422 {
11423   struct attribute *type_attr;
11424
11425   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11426   if (!type_attr)
11427     error (_("Dwarf Error: Problem turning containing type into gdb type "
11428              "[in module %s]"), cu->objfile->name);
11429
11430   return lookup_die_type (die, type_attr, cu);
11431 }
11432
11433 /* Look up the type of DIE in CU using its type attribute ATTR.
11434    If there is no type substitute an error marker.  */
11435
11436 static struct type *
11437 lookup_die_type (struct die_info *die, struct attribute *attr,
11438                  struct dwarf2_cu *cu)
11439 {
11440   struct type *this_type;
11441
11442   /* First see if we have it cached.  */
11443
11444   if (is_ref_attr (attr))
11445     {
11446       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11447
11448       this_type = get_die_type_at_offset (offset, cu->per_cu);
11449     }
11450   else if (attr->form == DW_FORM_sig8)
11451     {
11452       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11453       struct dwarf2_cu *sig_cu;
11454       unsigned int offset;
11455
11456       /* sig_type will be NULL if the signatured type is missing from
11457          the debug info.  */
11458       if (sig_type == NULL)
11459         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11460                  "at 0x%x [in module %s]"),
11461                die->offset, cu->objfile->name);
11462
11463       gdb_assert (sig_type->per_cu.from_debug_types);
11464       offset = sig_type->offset + sig_type->type_offset;
11465       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11466     }
11467   else
11468     {
11469       dump_die_for_error (die);
11470       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11471              dwarf_attr_name (attr->name), cu->objfile->name);
11472     }
11473
11474   /* If not cached we need to read it in.  */
11475
11476   if (this_type == NULL)
11477     {
11478       struct die_info *type_die;
11479       struct dwarf2_cu *type_cu = cu;
11480
11481       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11482       /* If the type is cached, we should have found it above.  */
11483       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11484       this_type = read_type_die_1 (type_die, type_cu);
11485     }
11486
11487   /* If we still don't have a type use an error marker.  */
11488
11489   if (this_type == NULL)
11490     {
11491       char *message, *saved;
11492
11493       /* read_type_die already issued a complaint.  */
11494       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11495                             cu->objfile->name,
11496                             cu->header.offset,
11497                             die->offset);
11498       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11499                              message, strlen (message));
11500       xfree (message);
11501
11502       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11503     }
11504
11505   return this_type;
11506 }
11507
11508 /* Return the type in DIE, CU.
11509    Returns NULL for invalid types.
11510
11511    This first does a lookup in the appropriate type_hash table,
11512    and only reads the die in if necessary.
11513
11514    NOTE: This can be called when reading in partial or full symbols.  */
11515
11516 static struct type *
11517 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11518 {
11519   struct type *this_type;
11520
11521   this_type = get_die_type (die, cu);
11522   if (this_type)
11523     return this_type;
11524
11525   return read_type_die_1 (die, cu);
11526 }
11527
11528 /* Read the type in DIE, CU.
11529    Returns NULL for invalid types.  */
11530
11531 static struct type *
11532 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11533 {
11534   struct type *this_type = NULL;
11535
11536   switch (die->tag)
11537     {
11538     case DW_TAG_class_type:
11539     case DW_TAG_interface_type:
11540     case DW_TAG_structure_type:
11541     case DW_TAG_union_type:
11542       this_type = read_structure_type (die, cu);
11543       break;
11544     case DW_TAG_enumeration_type:
11545       this_type = read_enumeration_type (die, cu);
11546       break;
11547     case DW_TAG_subprogram:
11548     case DW_TAG_subroutine_type:
11549     case DW_TAG_inlined_subroutine:
11550       this_type = read_subroutine_type (die, cu);
11551       break;
11552     case DW_TAG_array_type:
11553       this_type = read_array_type (die, cu);
11554       break;
11555     case DW_TAG_set_type:
11556       this_type = read_set_type (die, cu);
11557       break;
11558     case DW_TAG_pointer_type:
11559       this_type = read_tag_pointer_type (die, cu);
11560       break;
11561     case DW_TAG_ptr_to_member_type:
11562       this_type = read_tag_ptr_to_member_type (die, cu);
11563       break;
11564     case DW_TAG_reference_type:
11565       this_type = read_tag_reference_type (die, cu);
11566       break;
11567     case DW_TAG_const_type:
11568       this_type = read_tag_const_type (die, cu);
11569       break;
11570     case DW_TAG_volatile_type:
11571       this_type = read_tag_volatile_type (die, cu);
11572       break;
11573     case DW_TAG_string_type:
11574       this_type = read_tag_string_type (die, cu);
11575       break;
11576     case DW_TAG_typedef:
11577       this_type = read_typedef (die, cu);
11578       break;
11579     case DW_TAG_subrange_type:
11580       this_type = read_subrange_type (die, cu);
11581       break;
11582     case DW_TAG_base_type:
11583       this_type = read_base_type (die, cu);
11584       break;
11585     case DW_TAG_unspecified_type:
11586       this_type = read_unspecified_type (die, cu);
11587       break;
11588     case DW_TAG_namespace:
11589       this_type = read_namespace_type (die, cu);
11590       break;
11591     case DW_TAG_module:
11592       this_type = read_module_type (die, cu);
11593       break;
11594     default:
11595       complaint (&symfile_complaints,
11596                  _("unexpected tag in read_type_die: '%s'"),
11597                  dwarf_tag_name (die->tag));
11598       break;
11599     }
11600
11601   return this_type;
11602 }
11603
11604 /* See if we can figure out if the class lives in a namespace.  We do
11605    this by looking for a member function; its demangled name will
11606    contain namespace info, if there is any.
11607    Return the computed name or NULL.
11608    Space for the result is allocated on the objfile's obstack.
11609    This is the full-die version of guess_partial_die_structure_name.
11610    In this case we know DIE has no useful parent.  */
11611
11612 static char *
11613 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11614 {
11615   struct die_info *spec_die;
11616   struct dwarf2_cu *spec_cu;
11617   struct die_info *child;
11618
11619   spec_cu = cu;
11620   spec_die = die_specification (die, &spec_cu);
11621   if (spec_die != NULL)
11622     {
11623       die = spec_die;
11624       cu = spec_cu;
11625     }
11626
11627   for (child = die->child;
11628        child != NULL;
11629        child = child->sibling)
11630     {
11631       if (child->tag == DW_TAG_subprogram)
11632         {
11633           struct attribute *attr;
11634
11635           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11636           if (attr == NULL)
11637             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11638           if (attr != NULL)
11639             {
11640               char *actual_name
11641                 = language_class_name_from_physname (cu->language_defn,
11642                                                      DW_STRING (attr));
11643               char *name = NULL;
11644
11645               if (actual_name != NULL)
11646                 {
11647                   char *die_name = dwarf2_name (die, cu);
11648
11649                   if (die_name != NULL
11650                       && strcmp (die_name, actual_name) != 0)
11651                     {
11652                       /* Strip off the class name from the full name.
11653                          We want the prefix.  */
11654                       int die_name_len = strlen (die_name);
11655                       int actual_name_len = strlen (actual_name);
11656
11657                       /* Test for '::' as a sanity check.  */
11658                       if (actual_name_len > die_name_len + 2
11659                           && actual_name[actual_name_len
11660                                          - die_name_len - 1] == ':')
11661                         name =
11662                           obsavestring (actual_name,
11663                                         actual_name_len - die_name_len - 2,
11664                                         &cu->objfile->objfile_obstack);
11665                     }
11666                 }
11667               xfree (actual_name);
11668               return name;
11669             }
11670         }
11671     }
11672
11673   return NULL;
11674 }
11675
11676 /* Return the name of the namespace/class that DIE is defined within,
11677    or "" if we can't tell.  The caller should not xfree the result.
11678
11679    For example, if we're within the method foo() in the following
11680    code:
11681
11682    namespace N {
11683      class C {
11684        void foo () {
11685        }
11686      };
11687    }
11688
11689    then determine_prefix on foo's die will return "N::C".  */
11690
11691 static char *
11692 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11693 {
11694   struct die_info *parent, *spec_die;
11695   struct dwarf2_cu *spec_cu;
11696   struct type *parent_type;
11697
11698   if (cu->language != language_cplus && cu->language != language_java
11699       && cu->language != language_fortran)
11700     return "";
11701
11702   /* We have to be careful in the presence of DW_AT_specification.
11703      For example, with GCC 3.4, given the code
11704
11705      namespace N {
11706        void foo() {
11707          // Definition of N::foo.
11708        }
11709      }
11710
11711      then we'll have a tree of DIEs like this:
11712
11713      1: DW_TAG_compile_unit
11714        2: DW_TAG_namespace        // N
11715          3: DW_TAG_subprogram     // declaration of N::foo
11716        4: DW_TAG_subprogram       // definition of N::foo
11717             DW_AT_specification   // refers to die #3
11718
11719      Thus, when processing die #4, we have to pretend that we're in
11720      the context of its DW_AT_specification, namely the contex of die
11721      #3.  */
11722   spec_cu = cu;
11723   spec_die = die_specification (die, &spec_cu);
11724   if (spec_die == NULL)
11725     parent = die->parent;
11726   else
11727     {
11728       parent = spec_die->parent;
11729       cu = spec_cu;
11730     }
11731
11732   if (parent == NULL)
11733     return "";
11734   else if (parent->building_fullname)
11735     {
11736       const char *name;
11737       const char *parent_name;
11738
11739       /* It has been seen on RealView 2.2 built binaries,
11740          DW_TAG_template_type_param types actually _defined_ as
11741          children of the parent class:
11742
11743          enum E {};
11744          template class <class Enum> Class{};
11745          Class<enum E> class_e;
11746
11747          1: DW_TAG_class_type (Class)
11748            2: DW_TAG_enumeration_type (E)
11749              3: DW_TAG_enumerator (enum1:0)
11750              3: DW_TAG_enumerator (enum2:1)
11751              ...
11752            2: DW_TAG_template_type_param
11753               DW_AT_type  DW_FORM_ref_udata (E)
11754
11755          Besides being broken debug info, it can put GDB into an
11756          infinite loop.  Consider:
11757
11758          When we're building the full name for Class<E>, we'll start
11759          at Class, and go look over its template type parameters,
11760          finding E.  We'll then try to build the full name of E, and
11761          reach here.  We're now trying to build the full name of E,
11762          and look over the parent DIE for containing scope.  In the
11763          broken case, if we followed the parent DIE of E, we'd again
11764          find Class, and once again go look at its template type
11765          arguments, etc., etc.  Simply don't consider such parent die
11766          as source-level parent of this die (it can't be, the language
11767          doesn't allow it), and break the loop here.  */
11768       name = dwarf2_name (die, cu);
11769       parent_name = dwarf2_name (parent, cu);
11770       complaint (&symfile_complaints,
11771                  _("template param type '%s' defined within parent '%s'"),
11772                  name ? name : "<unknown>",
11773                  parent_name ? parent_name : "<unknown>");
11774       return "";
11775     }
11776   else
11777     switch (parent->tag)
11778       {
11779       case DW_TAG_namespace:
11780         parent_type = read_type_die (parent, cu);
11781         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11782            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11783            Work around this problem here.  */
11784         if (cu->language == language_cplus
11785             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11786           return "";
11787         /* We give a name to even anonymous namespaces.  */
11788         return TYPE_TAG_NAME (parent_type);
11789       case DW_TAG_class_type:
11790       case DW_TAG_interface_type:
11791       case DW_TAG_structure_type:
11792       case DW_TAG_union_type:
11793       case DW_TAG_module:
11794         parent_type = read_type_die (parent, cu);
11795         if (TYPE_TAG_NAME (parent_type) != NULL)
11796           return TYPE_TAG_NAME (parent_type);
11797         else
11798           /* An anonymous structure is only allowed non-static data
11799              members; no typedefs, no member functions, et cetera.
11800              So it does not need a prefix.  */
11801           return "";
11802       case DW_TAG_compile_unit:
11803         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
11804         if (cu->language == language_cplus
11805             && dwarf2_per_objfile->types.asection != NULL
11806             && die->child != NULL
11807             && (die->tag == DW_TAG_class_type
11808                 || die->tag == DW_TAG_structure_type
11809                 || die->tag == DW_TAG_union_type))
11810           {
11811             char *name = guess_full_die_structure_name (die, cu);
11812             if (name != NULL)
11813               return name;
11814           }
11815         return "";
11816       default:
11817         return determine_prefix (parent, cu);
11818       }
11819 }
11820
11821 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11822    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11823    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
11824    an obconcat, otherwise allocate storage for the result.  The CU argument is
11825    used to determine the language and hence, the appropriate separator.  */
11826
11827 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11828
11829 static char *
11830 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11831                  int physname, struct dwarf2_cu *cu)
11832 {
11833   const char *lead = "";
11834   const char *sep;
11835
11836   if (suffix == NULL || suffix[0] == '\0'
11837       || prefix == NULL || prefix[0] == '\0')
11838     sep = "";
11839   else if (cu->language == language_java)
11840     sep = ".";
11841   else if (cu->language == language_fortran && physname)
11842     {
11843       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11844          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11845
11846       lead = "__";
11847       sep = "_MOD_";
11848     }
11849   else
11850     sep = "::";
11851
11852   if (prefix == NULL)
11853     prefix = "";
11854   if (suffix == NULL)
11855     suffix = "";
11856
11857   if (obs == NULL)
11858     {
11859       char *retval
11860         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11861
11862       strcpy (retval, lead);
11863       strcat (retval, prefix);
11864       strcat (retval, sep);
11865       strcat (retval, suffix);
11866       return retval;
11867     }
11868   else
11869     {
11870       /* We have an obstack.  */
11871       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11872     }
11873 }
11874
11875 /* Return sibling of die, NULL if no sibling.  */
11876
11877 static struct die_info *
11878 sibling_die (struct die_info *die)
11879 {
11880   return die->sibling;
11881 }
11882
11883 /* Get name of a die, return NULL if not found.  */
11884
11885 static char *
11886 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11887                           struct obstack *obstack)
11888 {
11889   if (name && cu->language == language_cplus)
11890     {
11891       char *canon_name = cp_canonicalize_string (name);
11892
11893       if (canon_name != NULL)
11894         {
11895           if (strcmp (canon_name, name) != 0)
11896             name = obsavestring (canon_name, strlen (canon_name),
11897                                  obstack);
11898           xfree (canon_name);
11899         }
11900     }
11901
11902   return name;
11903 }
11904
11905 /* Get name of a die, return NULL if not found.  */
11906
11907 static char *
11908 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11909 {
11910   struct attribute *attr;
11911
11912   attr = dwarf2_attr (die, DW_AT_name, cu);
11913   if (!attr || !DW_STRING (attr))
11914     return NULL;
11915
11916   switch (die->tag)
11917     {
11918     case DW_TAG_compile_unit:
11919       /* Compilation units have a DW_AT_name that is a filename, not
11920          a source language identifier.  */
11921     case DW_TAG_enumeration_type:
11922     case DW_TAG_enumerator:
11923       /* These tags always have simple identifiers already; no need
11924          to canonicalize them.  */
11925       return DW_STRING (attr);
11926
11927     case DW_TAG_subprogram:
11928       /* Java constructors will all be named "<init>", so return
11929          the class name when we see this special case.  */
11930       if (cu->language == language_java
11931           && DW_STRING (attr) != NULL
11932           && strcmp (DW_STRING (attr), "<init>") == 0)
11933         {
11934           struct dwarf2_cu *spec_cu = cu;
11935           struct die_info *spec_die;
11936
11937           /* GCJ will output '<init>' for Java constructor names.
11938              For this special case, return the name of the parent class.  */
11939
11940           /* GCJ may output suprogram DIEs with AT_specification set.
11941              If so, use the name of the specified DIE.  */
11942           spec_die = die_specification (die, &spec_cu);
11943           if (spec_die != NULL)
11944             return dwarf2_name (spec_die, spec_cu);
11945
11946           do
11947             {
11948               die = die->parent;
11949               if (die->tag == DW_TAG_class_type)
11950                 return dwarf2_name (die, cu);
11951             }
11952           while (die->tag != DW_TAG_compile_unit);
11953         }
11954       break;
11955
11956     case DW_TAG_class_type:
11957     case DW_TAG_interface_type:
11958     case DW_TAG_structure_type:
11959     case DW_TAG_union_type:
11960       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11961          structures or unions.  These were of the form "._%d" in GCC 4.1,
11962          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11963          and GCC 4.4.  We work around this problem by ignoring these.  */
11964       if (strncmp (DW_STRING (attr), "._", 2) == 0
11965           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11966         return NULL;
11967       break;
11968
11969     default:
11970       break;
11971     }
11972
11973   if (!DW_STRING_IS_CANONICAL (attr))
11974     {
11975       DW_STRING (attr)
11976         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11977                                     &cu->objfile->objfile_obstack);
11978       DW_STRING_IS_CANONICAL (attr) = 1;
11979     }
11980   return DW_STRING (attr);
11981 }
11982
11983 /* Return the die that this die in an extension of, or NULL if there
11984    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11985    containing the return value on output.  */
11986
11987 static struct die_info *
11988 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11989 {
11990   struct attribute *attr;
11991
11992   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11993   if (attr == NULL)
11994     return NULL;
11995
11996   return follow_die_ref (die, attr, ext_cu);
11997 }
11998
11999 /* Convert a DIE tag into its string name.  */
12000
12001 static char *
12002 dwarf_tag_name (unsigned tag)
12003 {
12004   switch (tag)
12005     {
12006     case DW_TAG_padding:
12007       return "DW_TAG_padding";
12008     case DW_TAG_array_type:
12009       return "DW_TAG_array_type";
12010     case DW_TAG_class_type:
12011       return "DW_TAG_class_type";
12012     case DW_TAG_entry_point:
12013       return "DW_TAG_entry_point";
12014     case DW_TAG_enumeration_type:
12015       return "DW_TAG_enumeration_type";
12016     case DW_TAG_formal_parameter:
12017       return "DW_TAG_formal_parameter";
12018     case DW_TAG_imported_declaration:
12019       return "DW_TAG_imported_declaration";
12020     case DW_TAG_label:
12021       return "DW_TAG_label";
12022     case DW_TAG_lexical_block:
12023       return "DW_TAG_lexical_block";
12024     case DW_TAG_member:
12025       return "DW_TAG_member";
12026     case DW_TAG_pointer_type:
12027       return "DW_TAG_pointer_type";
12028     case DW_TAG_reference_type:
12029       return "DW_TAG_reference_type";
12030     case DW_TAG_compile_unit:
12031       return "DW_TAG_compile_unit";
12032     case DW_TAG_string_type:
12033       return "DW_TAG_string_type";
12034     case DW_TAG_structure_type:
12035       return "DW_TAG_structure_type";
12036     case DW_TAG_subroutine_type:
12037       return "DW_TAG_subroutine_type";
12038     case DW_TAG_typedef:
12039       return "DW_TAG_typedef";
12040     case DW_TAG_union_type:
12041       return "DW_TAG_union_type";
12042     case DW_TAG_unspecified_parameters:
12043       return "DW_TAG_unspecified_parameters";
12044     case DW_TAG_variant:
12045       return "DW_TAG_variant";
12046     case DW_TAG_common_block:
12047       return "DW_TAG_common_block";
12048     case DW_TAG_common_inclusion:
12049       return "DW_TAG_common_inclusion";
12050     case DW_TAG_inheritance:
12051       return "DW_TAG_inheritance";
12052     case DW_TAG_inlined_subroutine:
12053       return "DW_TAG_inlined_subroutine";
12054     case DW_TAG_module:
12055       return "DW_TAG_module";
12056     case DW_TAG_ptr_to_member_type:
12057       return "DW_TAG_ptr_to_member_type";
12058     case DW_TAG_set_type:
12059       return "DW_TAG_set_type";
12060     case DW_TAG_subrange_type:
12061       return "DW_TAG_subrange_type";
12062     case DW_TAG_with_stmt:
12063       return "DW_TAG_with_stmt";
12064     case DW_TAG_access_declaration:
12065       return "DW_TAG_access_declaration";
12066     case DW_TAG_base_type:
12067       return "DW_TAG_base_type";
12068     case DW_TAG_catch_block:
12069       return "DW_TAG_catch_block";
12070     case DW_TAG_const_type:
12071       return "DW_TAG_const_type";
12072     case DW_TAG_constant:
12073       return "DW_TAG_constant";
12074     case DW_TAG_enumerator:
12075       return "DW_TAG_enumerator";
12076     case DW_TAG_file_type:
12077       return "DW_TAG_file_type";
12078     case DW_TAG_friend:
12079       return "DW_TAG_friend";
12080     case DW_TAG_namelist:
12081       return "DW_TAG_namelist";
12082     case DW_TAG_namelist_item:
12083       return "DW_TAG_namelist_item";
12084     case DW_TAG_packed_type:
12085       return "DW_TAG_packed_type";
12086     case DW_TAG_subprogram:
12087       return "DW_TAG_subprogram";
12088     case DW_TAG_template_type_param:
12089       return "DW_TAG_template_type_param";
12090     case DW_TAG_template_value_param:
12091       return "DW_TAG_template_value_param";
12092     case DW_TAG_thrown_type:
12093       return "DW_TAG_thrown_type";
12094     case DW_TAG_try_block:
12095       return "DW_TAG_try_block";
12096     case DW_TAG_variant_part:
12097       return "DW_TAG_variant_part";
12098     case DW_TAG_variable:
12099       return "DW_TAG_variable";
12100     case DW_TAG_volatile_type:
12101       return "DW_TAG_volatile_type";
12102     case DW_TAG_dwarf_procedure:
12103       return "DW_TAG_dwarf_procedure";
12104     case DW_TAG_restrict_type:
12105       return "DW_TAG_restrict_type";
12106     case DW_TAG_interface_type:
12107       return "DW_TAG_interface_type";
12108     case DW_TAG_namespace:
12109       return "DW_TAG_namespace";
12110     case DW_TAG_imported_module:
12111       return "DW_TAG_imported_module";
12112     case DW_TAG_unspecified_type:
12113       return "DW_TAG_unspecified_type";
12114     case DW_TAG_partial_unit:
12115       return "DW_TAG_partial_unit";
12116     case DW_TAG_imported_unit:
12117       return "DW_TAG_imported_unit";
12118     case DW_TAG_condition:
12119       return "DW_TAG_condition";
12120     case DW_TAG_shared_type:
12121       return "DW_TAG_shared_type";
12122     case DW_TAG_type_unit:
12123       return "DW_TAG_type_unit";
12124     case DW_TAG_MIPS_loop:
12125       return "DW_TAG_MIPS_loop";
12126     case DW_TAG_HP_array_descriptor:
12127       return "DW_TAG_HP_array_descriptor";
12128     case DW_TAG_format_label:
12129       return "DW_TAG_format_label";
12130     case DW_TAG_function_template:
12131       return "DW_TAG_function_template";
12132     case DW_TAG_class_template:
12133       return "DW_TAG_class_template";
12134     case DW_TAG_GNU_BINCL:
12135       return "DW_TAG_GNU_BINCL";
12136     case DW_TAG_GNU_EINCL:
12137       return "DW_TAG_GNU_EINCL";
12138     case DW_TAG_upc_shared_type:
12139       return "DW_TAG_upc_shared_type";
12140     case DW_TAG_upc_strict_type:
12141       return "DW_TAG_upc_strict_type";
12142     case DW_TAG_upc_relaxed_type:
12143       return "DW_TAG_upc_relaxed_type";
12144     case DW_TAG_PGI_kanji_type:
12145       return "DW_TAG_PGI_kanji_type";
12146     case DW_TAG_PGI_interface_block:
12147       return "DW_TAG_PGI_interface_block";
12148     default:
12149       return "DW_TAG_<unknown>";
12150     }
12151 }
12152
12153 /* Convert a DWARF attribute code into its string name.  */
12154
12155 static char *
12156 dwarf_attr_name (unsigned attr)
12157 {
12158   switch (attr)
12159     {
12160     case DW_AT_sibling:
12161       return "DW_AT_sibling";
12162     case DW_AT_location:
12163       return "DW_AT_location";
12164     case DW_AT_name:
12165       return "DW_AT_name";
12166     case DW_AT_ordering:
12167       return "DW_AT_ordering";
12168     case DW_AT_subscr_data:
12169       return "DW_AT_subscr_data";
12170     case DW_AT_byte_size:
12171       return "DW_AT_byte_size";
12172     case DW_AT_bit_offset:
12173       return "DW_AT_bit_offset";
12174     case DW_AT_bit_size:
12175       return "DW_AT_bit_size";
12176     case DW_AT_element_list:
12177       return "DW_AT_element_list";
12178     case DW_AT_stmt_list:
12179       return "DW_AT_stmt_list";
12180     case DW_AT_low_pc:
12181       return "DW_AT_low_pc";
12182     case DW_AT_high_pc:
12183       return "DW_AT_high_pc";
12184     case DW_AT_language:
12185       return "DW_AT_language";
12186     case DW_AT_member:
12187       return "DW_AT_member";
12188     case DW_AT_discr:
12189       return "DW_AT_discr";
12190     case DW_AT_discr_value:
12191       return "DW_AT_discr_value";
12192     case DW_AT_visibility:
12193       return "DW_AT_visibility";
12194     case DW_AT_import:
12195       return "DW_AT_import";
12196     case DW_AT_string_length:
12197       return "DW_AT_string_length";
12198     case DW_AT_common_reference:
12199       return "DW_AT_common_reference";
12200     case DW_AT_comp_dir:
12201       return "DW_AT_comp_dir";
12202     case DW_AT_const_value:
12203       return "DW_AT_const_value";
12204     case DW_AT_containing_type:
12205       return "DW_AT_containing_type";
12206     case DW_AT_default_value:
12207       return "DW_AT_default_value";
12208     case DW_AT_inline:
12209       return "DW_AT_inline";
12210     case DW_AT_is_optional:
12211       return "DW_AT_is_optional";
12212     case DW_AT_lower_bound:
12213       return "DW_AT_lower_bound";
12214     case DW_AT_producer:
12215       return "DW_AT_producer";
12216     case DW_AT_prototyped:
12217       return "DW_AT_prototyped";
12218     case DW_AT_return_addr:
12219       return "DW_AT_return_addr";
12220     case DW_AT_start_scope:
12221       return "DW_AT_start_scope";
12222     case DW_AT_bit_stride:
12223       return "DW_AT_bit_stride";
12224     case DW_AT_upper_bound:
12225       return "DW_AT_upper_bound";
12226     case DW_AT_abstract_origin:
12227       return "DW_AT_abstract_origin";
12228     case DW_AT_accessibility:
12229       return "DW_AT_accessibility";
12230     case DW_AT_address_class:
12231       return "DW_AT_address_class";
12232     case DW_AT_artificial:
12233       return "DW_AT_artificial";
12234     case DW_AT_base_types:
12235       return "DW_AT_base_types";
12236     case DW_AT_calling_convention:
12237       return "DW_AT_calling_convention";
12238     case DW_AT_count:
12239       return "DW_AT_count";
12240     case DW_AT_data_member_location:
12241       return "DW_AT_data_member_location";
12242     case DW_AT_decl_column:
12243       return "DW_AT_decl_column";
12244     case DW_AT_decl_file:
12245       return "DW_AT_decl_file";
12246     case DW_AT_decl_line:
12247       return "DW_AT_decl_line";
12248     case DW_AT_declaration:
12249       return "DW_AT_declaration";
12250     case DW_AT_discr_list:
12251       return "DW_AT_discr_list";
12252     case DW_AT_encoding:
12253       return "DW_AT_encoding";
12254     case DW_AT_external:
12255       return "DW_AT_external";
12256     case DW_AT_frame_base:
12257       return "DW_AT_frame_base";
12258     case DW_AT_friend:
12259       return "DW_AT_friend";
12260     case DW_AT_identifier_case:
12261       return "DW_AT_identifier_case";
12262     case DW_AT_macro_info:
12263       return "DW_AT_macro_info";
12264     case DW_AT_namelist_items:
12265       return "DW_AT_namelist_items";
12266     case DW_AT_priority:
12267       return "DW_AT_priority";
12268     case DW_AT_segment:
12269       return "DW_AT_segment";
12270     case DW_AT_specification:
12271       return "DW_AT_specification";
12272     case DW_AT_static_link:
12273       return "DW_AT_static_link";
12274     case DW_AT_type:
12275       return "DW_AT_type";
12276     case DW_AT_use_location:
12277       return "DW_AT_use_location";
12278     case DW_AT_variable_parameter:
12279       return "DW_AT_variable_parameter";
12280     case DW_AT_virtuality:
12281       return "DW_AT_virtuality";
12282     case DW_AT_vtable_elem_location:
12283       return "DW_AT_vtable_elem_location";
12284     /* DWARF 3 values.  */
12285     case DW_AT_allocated:
12286       return "DW_AT_allocated";
12287     case DW_AT_associated:
12288       return "DW_AT_associated";
12289     case DW_AT_data_location:
12290       return "DW_AT_data_location";
12291     case DW_AT_byte_stride:
12292       return "DW_AT_byte_stride";
12293     case DW_AT_entry_pc:
12294       return "DW_AT_entry_pc";
12295     case DW_AT_use_UTF8:
12296       return "DW_AT_use_UTF8";
12297     case DW_AT_extension:
12298       return "DW_AT_extension";
12299     case DW_AT_ranges:
12300       return "DW_AT_ranges";
12301     case DW_AT_trampoline:
12302       return "DW_AT_trampoline";
12303     case DW_AT_call_column:
12304       return "DW_AT_call_column";
12305     case DW_AT_call_file:
12306       return "DW_AT_call_file";
12307     case DW_AT_call_line:
12308       return "DW_AT_call_line";
12309     case DW_AT_description:
12310       return "DW_AT_description";
12311     case DW_AT_binary_scale:
12312       return "DW_AT_binary_scale";
12313     case DW_AT_decimal_scale:
12314       return "DW_AT_decimal_scale";
12315     case DW_AT_small:
12316       return "DW_AT_small";
12317     case DW_AT_decimal_sign:
12318       return "DW_AT_decimal_sign";
12319     case DW_AT_digit_count:
12320       return "DW_AT_digit_count";
12321     case DW_AT_picture_string:
12322       return "DW_AT_picture_string";
12323     case DW_AT_mutable:
12324       return "DW_AT_mutable";
12325     case DW_AT_threads_scaled:
12326       return "DW_AT_threads_scaled";
12327     case DW_AT_explicit:
12328       return "DW_AT_explicit";
12329     case DW_AT_object_pointer:
12330       return "DW_AT_object_pointer";
12331     case DW_AT_endianity:
12332       return "DW_AT_endianity";
12333     case DW_AT_elemental:
12334       return "DW_AT_elemental";
12335     case DW_AT_pure:
12336       return "DW_AT_pure";
12337     case DW_AT_recursive:
12338       return "DW_AT_recursive";
12339     /* DWARF 4 values.  */
12340     case DW_AT_signature:
12341       return "DW_AT_signature";
12342     case DW_AT_linkage_name:
12343       return "DW_AT_linkage_name";
12344     /* SGI/MIPS extensions.  */
12345 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12346     case DW_AT_MIPS_fde:
12347       return "DW_AT_MIPS_fde";
12348 #endif
12349     case DW_AT_MIPS_loop_begin:
12350       return "DW_AT_MIPS_loop_begin";
12351     case DW_AT_MIPS_tail_loop_begin:
12352       return "DW_AT_MIPS_tail_loop_begin";
12353     case DW_AT_MIPS_epilog_begin:
12354       return "DW_AT_MIPS_epilog_begin";
12355     case DW_AT_MIPS_loop_unroll_factor:
12356       return "DW_AT_MIPS_loop_unroll_factor";
12357     case DW_AT_MIPS_software_pipeline_depth:
12358       return "DW_AT_MIPS_software_pipeline_depth";
12359     case DW_AT_MIPS_linkage_name:
12360       return "DW_AT_MIPS_linkage_name";
12361     case DW_AT_MIPS_stride:
12362       return "DW_AT_MIPS_stride";
12363     case DW_AT_MIPS_abstract_name:
12364       return "DW_AT_MIPS_abstract_name";
12365     case DW_AT_MIPS_clone_origin:
12366       return "DW_AT_MIPS_clone_origin";
12367     case DW_AT_MIPS_has_inlines:
12368       return "DW_AT_MIPS_has_inlines";
12369     /* HP extensions.  */
12370 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12371     case DW_AT_HP_block_index:
12372       return "DW_AT_HP_block_index";
12373 #endif
12374     case DW_AT_HP_unmodifiable:
12375       return "DW_AT_HP_unmodifiable";
12376     case DW_AT_HP_actuals_stmt_list:
12377       return "DW_AT_HP_actuals_stmt_list";
12378     case DW_AT_HP_proc_per_section:
12379       return "DW_AT_HP_proc_per_section";
12380     case DW_AT_HP_raw_data_ptr:
12381       return "DW_AT_HP_raw_data_ptr";
12382     case DW_AT_HP_pass_by_reference:
12383       return "DW_AT_HP_pass_by_reference";
12384     case DW_AT_HP_opt_level:
12385       return "DW_AT_HP_opt_level";
12386     case DW_AT_HP_prof_version_id:
12387       return "DW_AT_HP_prof_version_id";
12388     case DW_AT_HP_opt_flags:
12389       return "DW_AT_HP_opt_flags";
12390     case DW_AT_HP_cold_region_low_pc:
12391       return "DW_AT_HP_cold_region_low_pc";
12392     case DW_AT_HP_cold_region_high_pc:
12393       return "DW_AT_HP_cold_region_high_pc";
12394     case DW_AT_HP_all_variables_modifiable:
12395       return "DW_AT_HP_all_variables_modifiable";
12396     case DW_AT_HP_linkage_name:
12397       return "DW_AT_HP_linkage_name";
12398     case DW_AT_HP_prof_flags:
12399       return "DW_AT_HP_prof_flags";
12400     /* GNU extensions.  */
12401     case DW_AT_sf_names:
12402       return "DW_AT_sf_names";
12403     case DW_AT_src_info:
12404       return "DW_AT_src_info";
12405     case DW_AT_mac_info:
12406       return "DW_AT_mac_info";
12407     case DW_AT_src_coords:
12408       return "DW_AT_src_coords";
12409     case DW_AT_body_begin:
12410       return "DW_AT_body_begin";
12411     case DW_AT_body_end:
12412       return "DW_AT_body_end";
12413     case DW_AT_GNU_vector:
12414       return "DW_AT_GNU_vector";
12415     case DW_AT_GNU_odr_signature:
12416       return "DW_AT_GNU_odr_signature";
12417     /* VMS extensions.  */
12418     case DW_AT_VMS_rtnbeg_pd_address:
12419       return "DW_AT_VMS_rtnbeg_pd_address";
12420     /* UPC extension.  */
12421     case DW_AT_upc_threads_scaled:
12422       return "DW_AT_upc_threads_scaled";
12423     /* PGI (STMicroelectronics) extensions.  */
12424     case DW_AT_PGI_lbase:
12425       return "DW_AT_PGI_lbase";
12426     case DW_AT_PGI_soffset:
12427       return "DW_AT_PGI_soffset";
12428     case DW_AT_PGI_lstride:
12429       return "DW_AT_PGI_lstride";
12430     default:
12431       return "DW_AT_<unknown>";
12432     }
12433 }
12434
12435 /* Convert a DWARF value form code into its string name.  */
12436
12437 static char *
12438 dwarf_form_name (unsigned form)
12439 {
12440   switch (form)
12441     {
12442     case DW_FORM_addr:
12443       return "DW_FORM_addr";
12444     case DW_FORM_block2:
12445       return "DW_FORM_block2";
12446     case DW_FORM_block4:
12447       return "DW_FORM_block4";
12448     case DW_FORM_data2:
12449       return "DW_FORM_data2";
12450     case DW_FORM_data4:
12451       return "DW_FORM_data4";
12452     case DW_FORM_data8:
12453       return "DW_FORM_data8";
12454     case DW_FORM_string:
12455       return "DW_FORM_string";
12456     case DW_FORM_block:
12457       return "DW_FORM_block";
12458     case DW_FORM_block1:
12459       return "DW_FORM_block1";
12460     case DW_FORM_data1:
12461       return "DW_FORM_data1";
12462     case DW_FORM_flag:
12463       return "DW_FORM_flag";
12464     case DW_FORM_sdata:
12465       return "DW_FORM_sdata";
12466     case DW_FORM_strp:
12467       return "DW_FORM_strp";
12468     case DW_FORM_udata:
12469       return "DW_FORM_udata";
12470     case DW_FORM_ref_addr:
12471       return "DW_FORM_ref_addr";
12472     case DW_FORM_ref1:
12473       return "DW_FORM_ref1";
12474     case DW_FORM_ref2:
12475       return "DW_FORM_ref2";
12476     case DW_FORM_ref4:
12477       return "DW_FORM_ref4";
12478     case DW_FORM_ref8:
12479       return "DW_FORM_ref8";
12480     case DW_FORM_ref_udata:
12481       return "DW_FORM_ref_udata";
12482     case DW_FORM_indirect:
12483       return "DW_FORM_indirect";
12484     case DW_FORM_sec_offset:
12485       return "DW_FORM_sec_offset";
12486     case DW_FORM_exprloc:
12487       return "DW_FORM_exprloc";
12488     case DW_FORM_flag_present:
12489       return "DW_FORM_flag_present";
12490     case DW_FORM_sig8:
12491       return "DW_FORM_sig8";
12492     default:
12493       return "DW_FORM_<unknown>";
12494     }
12495 }
12496
12497 /* Convert a DWARF stack opcode into its string name.  */
12498
12499 const char *
12500 dwarf_stack_op_name (unsigned op)
12501 {
12502   switch (op)
12503     {
12504     case DW_OP_addr:
12505       return "DW_OP_addr";
12506     case DW_OP_deref:
12507       return "DW_OP_deref";
12508     case DW_OP_const1u:
12509       return "DW_OP_const1u";
12510     case DW_OP_const1s:
12511       return "DW_OP_const1s";
12512     case DW_OP_const2u:
12513       return "DW_OP_const2u";
12514     case DW_OP_const2s:
12515       return "DW_OP_const2s";
12516     case DW_OP_const4u:
12517       return "DW_OP_const4u";
12518     case DW_OP_const4s:
12519       return "DW_OP_const4s";
12520     case DW_OP_const8u:
12521       return "DW_OP_const8u";
12522     case DW_OP_const8s:
12523       return "DW_OP_const8s";
12524     case DW_OP_constu:
12525       return "DW_OP_constu";
12526     case DW_OP_consts:
12527       return "DW_OP_consts";
12528     case DW_OP_dup:
12529       return "DW_OP_dup";
12530     case DW_OP_drop:
12531       return "DW_OP_drop";
12532     case DW_OP_over:
12533       return "DW_OP_over";
12534     case DW_OP_pick:
12535       return "DW_OP_pick";
12536     case DW_OP_swap:
12537       return "DW_OP_swap";
12538     case DW_OP_rot:
12539       return "DW_OP_rot";
12540     case DW_OP_xderef:
12541       return "DW_OP_xderef";
12542     case DW_OP_abs:
12543       return "DW_OP_abs";
12544     case DW_OP_and:
12545       return "DW_OP_and";
12546     case DW_OP_div:
12547       return "DW_OP_div";
12548     case DW_OP_minus:
12549       return "DW_OP_minus";
12550     case DW_OP_mod:
12551       return "DW_OP_mod";
12552     case DW_OP_mul:
12553       return "DW_OP_mul";
12554     case DW_OP_neg:
12555       return "DW_OP_neg";
12556     case DW_OP_not:
12557       return "DW_OP_not";
12558     case DW_OP_or:
12559       return "DW_OP_or";
12560     case DW_OP_plus:
12561       return "DW_OP_plus";
12562     case DW_OP_plus_uconst:
12563       return "DW_OP_plus_uconst";
12564     case DW_OP_shl:
12565       return "DW_OP_shl";
12566     case DW_OP_shr:
12567       return "DW_OP_shr";
12568     case DW_OP_shra:
12569       return "DW_OP_shra";
12570     case DW_OP_xor:
12571       return "DW_OP_xor";
12572     case DW_OP_bra:
12573       return "DW_OP_bra";
12574     case DW_OP_eq:
12575       return "DW_OP_eq";
12576     case DW_OP_ge:
12577       return "DW_OP_ge";
12578     case DW_OP_gt:
12579       return "DW_OP_gt";
12580     case DW_OP_le:
12581       return "DW_OP_le";
12582     case DW_OP_lt:
12583       return "DW_OP_lt";
12584     case DW_OP_ne:
12585       return "DW_OP_ne";
12586     case DW_OP_skip:
12587       return "DW_OP_skip";
12588     case DW_OP_lit0:
12589       return "DW_OP_lit0";
12590     case DW_OP_lit1:
12591       return "DW_OP_lit1";
12592     case DW_OP_lit2:
12593       return "DW_OP_lit2";
12594     case DW_OP_lit3:
12595       return "DW_OP_lit3";
12596     case DW_OP_lit4:
12597       return "DW_OP_lit4";
12598     case DW_OP_lit5:
12599       return "DW_OP_lit5";
12600     case DW_OP_lit6:
12601       return "DW_OP_lit6";
12602     case DW_OP_lit7:
12603       return "DW_OP_lit7";
12604     case DW_OP_lit8:
12605       return "DW_OP_lit8";
12606     case DW_OP_lit9:
12607       return "DW_OP_lit9";
12608     case DW_OP_lit10:
12609       return "DW_OP_lit10";
12610     case DW_OP_lit11:
12611       return "DW_OP_lit11";
12612     case DW_OP_lit12:
12613       return "DW_OP_lit12";
12614     case DW_OP_lit13:
12615       return "DW_OP_lit13";
12616     case DW_OP_lit14:
12617       return "DW_OP_lit14";
12618     case DW_OP_lit15:
12619       return "DW_OP_lit15";
12620     case DW_OP_lit16:
12621       return "DW_OP_lit16";
12622     case DW_OP_lit17:
12623       return "DW_OP_lit17";
12624     case DW_OP_lit18:
12625       return "DW_OP_lit18";
12626     case DW_OP_lit19:
12627       return "DW_OP_lit19";
12628     case DW_OP_lit20:
12629       return "DW_OP_lit20";
12630     case DW_OP_lit21:
12631       return "DW_OP_lit21";
12632     case DW_OP_lit22:
12633       return "DW_OP_lit22";
12634     case DW_OP_lit23:
12635       return "DW_OP_lit23";
12636     case DW_OP_lit24:
12637       return "DW_OP_lit24";
12638     case DW_OP_lit25:
12639       return "DW_OP_lit25";
12640     case DW_OP_lit26:
12641       return "DW_OP_lit26";
12642     case DW_OP_lit27:
12643       return "DW_OP_lit27";
12644     case DW_OP_lit28:
12645       return "DW_OP_lit28";
12646     case DW_OP_lit29:
12647       return "DW_OP_lit29";
12648     case DW_OP_lit30:
12649       return "DW_OP_lit30";
12650     case DW_OP_lit31:
12651       return "DW_OP_lit31";
12652     case DW_OP_reg0:
12653       return "DW_OP_reg0";
12654     case DW_OP_reg1:
12655       return "DW_OP_reg1";
12656     case DW_OP_reg2:
12657       return "DW_OP_reg2";
12658     case DW_OP_reg3:
12659       return "DW_OP_reg3";
12660     case DW_OP_reg4:
12661       return "DW_OP_reg4";
12662     case DW_OP_reg5:
12663       return "DW_OP_reg5";
12664     case DW_OP_reg6:
12665       return "DW_OP_reg6";
12666     case DW_OP_reg7:
12667       return "DW_OP_reg7";
12668     case DW_OP_reg8:
12669       return "DW_OP_reg8";
12670     case DW_OP_reg9:
12671       return "DW_OP_reg9";
12672     case DW_OP_reg10:
12673       return "DW_OP_reg10";
12674     case DW_OP_reg11:
12675       return "DW_OP_reg11";
12676     case DW_OP_reg12:
12677       return "DW_OP_reg12";
12678     case DW_OP_reg13:
12679       return "DW_OP_reg13";
12680     case DW_OP_reg14:
12681       return "DW_OP_reg14";
12682     case DW_OP_reg15:
12683       return "DW_OP_reg15";
12684     case DW_OP_reg16:
12685       return "DW_OP_reg16";
12686     case DW_OP_reg17:
12687       return "DW_OP_reg17";
12688     case DW_OP_reg18:
12689       return "DW_OP_reg18";
12690     case DW_OP_reg19:
12691       return "DW_OP_reg19";
12692     case DW_OP_reg20:
12693       return "DW_OP_reg20";
12694     case DW_OP_reg21:
12695       return "DW_OP_reg21";
12696     case DW_OP_reg22:
12697       return "DW_OP_reg22";
12698     case DW_OP_reg23:
12699       return "DW_OP_reg23";
12700     case DW_OP_reg24:
12701       return "DW_OP_reg24";
12702     case DW_OP_reg25:
12703       return "DW_OP_reg25";
12704     case DW_OP_reg26:
12705       return "DW_OP_reg26";
12706     case DW_OP_reg27:
12707       return "DW_OP_reg27";
12708     case DW_OP_reg28:
12709       return "DW_OP_reg28";
12710     case DW_OP_reg29:
12711       return "DW_OP_reg29";
12712     case DW_OP_reg30:
12713       return "DW_OP_reg30";
12714     case DW_OP_reg31:
12715       return "DW_OP_reg31";
12716     case DW_OP_breg0:
12717       return "DW_OP_breg0";
12718     case DW_OP_breg1:
12719       return "DW_OP_breg1";
12720     case DW_OP_breg2:
12721       return "DW_OP_breg2";
12722     case DW_OP_breg3:
12723       return "DW_OP_breg3";
12724     case DW_OP_breg4:
12725       return "DW_OP_breg4";
12726     case DW_OP_breg5:
12727       return "DW_OP_breg5";
12728     case DW_OP_breg6:
12729       return "DW_OP_breg6";
12730     case DW_OP_breg7:
12731       return "DW_OP_breg7";
12732     case DW_OP_breg8:
12733       return "DW_OP_breg8";
12734     case DW_OP_breg9:
12735       return "DW_OP_breg9";
12736     case DW_OP_breg10:
12737       return "DW_OP_breg10";
12738     case DW_OP_breg11:
12739       return "DW_OP_breg11";
12740     case DW_OP_breg12:
12741       return "DW_OP_breg12";
12742     case DW_OP_breg13:
12743       return "DW_OP_breg13";
12744     case DW_OP_breg14:
12745       return "DW_OP_breg14";
12746     case DW_OP_breg15:
12747       return "DW_OP_breg15";
12748     case DW_OP_breg16:
12749       return "DW_OP_breg16";
12750     case DW_OP_breg17:
12751       return "DW_OP_breg17";
12752     case DW_OP_breg18:
12753       return "DW_OP_breg18";
12754     case DW_OP_breg19:
12755       return "DW_OP_breg19";
12756     case DW_OP_breg20:
12757       return "DW_OP_breg20";
12758     case DW_OP_breg21:
12759       return "DW_OP_breg21";
12760     case DW_OP_breg22:
12761       return "DW_OP_breg22";
12762     case DW_OP_breg23:
12763       return "DW_OP_breg23";
12764     case DW_OP_breg24:
12765       return "DW_OP_breg24";
12766     case DW_OP_breg25:
12767       return "DW_OP_breg25";
12768     case DW_OP_breg26:
12769       return "DW_OP_breg26";
12770     case DW_OP_breg27:
12771       return "DW_OP_breg27";
12772     case DW_OP_breg28:
12773       return "DW_OP_breg28";
12774     case DW_OP_breg29:
12775       return "DW_OP_breg29";
12776     case DW_OP_breg30:
12777       return "DW_OP_breg30";
12778     case DW_OP_breg31:
12779       return "DW_OP_breg31";
12780     case DW_OP_regx:
12781       return "DW_OP_regx";
12782     case DW_OP_fbreg:
12783       return "DW_OP_fbreg";
12784     case DW_OP_bregx:
12785       return "DW_OP_bregx";
12786     case DW_OP_piece:
12787       return "DW_OP_piece";
12788     case DW_OP_deref_size:
12789       return "DW_OP_deref_size";
12790     case DW_OP_xderef_size:
12791       return "DW_OP_xderef_size";
12792     case DW_OP_nop:
12793       return "DW_OP_nop";
12794     /* DWARF 3 extensions.  */
12795     case DW_OP_push_object_address:
12796       return "DW_OP_push_object_address";
12797     case DW_OP_call2:
12798       return "DW_OP_call2";
12799     case DW_OP_call4:
12800       return "DW_OP_call4";
12801     case DW_OP_call_ref:
12802       return "DW_OP_call_ref";
12803     case DW_OP_form_tls_address:
12804       return "DW_OP_form_tls_address";
12805     case DW_OP_call_frame_cfa:
12806       return "DW_OP_call_frame_cfa";
12807     case DW_OP_bit_piece:
12808       return "DW_OP_bit_piece";
12809     /* DWARF 4 extensions.  */
12810     case DW_OP_implicit_value:
12811       return "DW_OP_implicit_value";
12812     case DW_OP_stack_value:
12813       return "DW_OP_stack_value";
12814     /* GNU extensions.  */
12815     case DW_OP_GNU_push_tls_address:
12816       return "DW_OP_GNU_push_tls_address";
12817     case DW_OP_GNU_uninit:
12818       return "DW_OP_GNU_uninit";
12819     case DW_OP_GNU_implicit_pointer:
12820       return "DW_OP_GNU_implicit_pointer";
12821     default:
12822       return NULL;
12823     }
12824 }
12825
12826 static char *
12827 dwarf_bool_name (unsigned mybool)
12828 {
12829   if (mybool)
12830     return "TRUE";
12831   else
12832     return "FALSE";
12833 }
12834
12835 /* Convert a DWARF type code into its string name.  */
12836
12837 static char *
12838 dwarf_type_encoding_name (unsigned enc)
12839 {
12840   switch (enc)
12841     {
12842     case DW_ATE_void:
12843       return "DW_ATE_void";
12844     case DW_ATE_address:
12845       return "DW_ATE_address";
12846     case DW_ATE_boolean:
12847       return "DW_ATE_boolean";
12848     case DW_ATE_complex_float:
12849       return "DW_ATE_complex_float";
12850     case DW_ATE_float:
12851       return "DW_ATE_float";
12852     case DW_ATE_signed:
12853       return "DW_ATE_signed";
12854     case DW_ATE_signed_char:
12855       return "DW_ATE_signed_char";
12856     case DW_ATE_unsigned:
12857       return "DW_ATE_unsigned";
12858     case DW_ATE_unsigned_char:
12859       return "DW_ATE_unsigned_char";
12860     /* DWARF 3.  */
12861     case DW_ATE_imaginary_float:
12862       return "DW_ATE_imaginary_float";
12863     case DW_ATE_packed_decimal:
12864       return "DW_ATE_packed_decimal";
12865     case DW_ATE_numeric_string:
12866       return "DW_ATE_numeric_string";
12867     case DW_ATE_edited:
12868       return "DW_ATE_edited";
12869     case DW_ATE_signed_fixed:
12870       return "DW_ATE_signed_fixed";
12871     case DW_ATE_unsigned_fixed:
12872       return "DW_ATE_unsigned_fixed";
12873     case DW_ATE_decimal_float:
12874       return "DW_ATE_decimal_float";
12875     /* DWARF 4.  */
12876     case DW_ATE_UTF:
12877       return "DW_ATE_UTF";
12878     /* HP extensions.  */
12879     case DW_ATE_HP_float80:
12880       return "DW_ATE_HP_float80";
12881     case DW_ATE_HP_complex_float80:
12882       return "DW_ATE_HP_complex_float80";
12883     case DW_ATE_HP_float128:
12884       return "DW_ATE_HP_float128";
12885     case DW_ATE_HP_complex_float128:
12886       return "DW_ATE_HP_complex_float128";
12887     case DW_ATE_HP_floathpintel:
12888       return "DW_ATE_HP_floathpintel";
12889     case DW_ATE_HP_imaginary_float80:
12890       return "DW_ATE_HP_imaginary_float80";
12891     case DW_ATE_HP_imaginary_float128:
12892       return "DW_ATE_HP_imaginary_float128";
12893     default:
12894       return "DW_ATE_<unknown>";
12895     }
12896 }
12897
12898 /* Convert a DWARF call frame info operation to its string name.  */
12899
12900 #if 0
12901 static char *
12902 dwarf_cfi_name (unsigned cfi_opc)
12903 {
12904   switch (cfi_opc)
12905     {
12906     case DW_CFA_advance_loc:
12907       return "DW_CFA_advance_loc";
12908     case DW_CFA_offset:
12909       return "DW_CFA_offset";
12910     case DW_CFA_restore:
12911       return "DW_CFA_restore";
12912     case DW_CFA_nop:
12913       return "DW_CFA_nop";
12914     case DW_CFA_set_loc:
12915       return "DW_CFA_set_loc";
12916     case DW_CFA_advance_loc1:
12917       return "DW_CFA_advance_loc1";
12918     case DW_CFA_advance_loc2:
12919       return "DW_CFA_advance_loc2";
12920     case DW_CFA_advance_loc4:
12921       return "DW_CFA_advance_loc4";
12922     case DW_CFA_offset_extended:
12923       return "DW_CFA_offset_extended";
12924     case DW_CFA_restore_extended:
12925       return "DW_CFA_restore_extended";
12926     case DW_CFA_undefined:
12927       return "DW_CFA_undefined";
12928     case DW_CFA_same_value:
12929       return "DW_CFA_same_value";
12930     case DW_CFA_register:
12931       return "DW_CFA_register";
12932     case DW_CFA_remember_state:
12933       return "DW_CFA_remember_state";
12934     case DW_CFA_restore_state:
12935       return "DW_CFA_restore_state";
12936     case DW_CFA_def_cfa:
12937       return "DW_CFA_def_cfa";
12938     case DW_CFA_def_cfa_register:
12939       return "DW_CFA_def_cfa_register";
12940     case DW_CFA_def_cfa_offset:
12941       return "DW_CFA_def_cfa_offset";
12942     /* DWARF 3.  */
12943     case DW_CFA_def_cfa_expression:
12944       return "DW_CFA_def_cfa_expression";
12945     case DW_CFA_expression:
12946       return "DW_CFA_expression";
12947     case DW_CFA_offset_extended_sf:
12948       return "DW_CFA_offset_extended_sf";
12949     case DW_CFA_def_cfa_sf:
12950       return "DW_CFA_def_cfa_sf";
12951     case DW_CFA_def_cfa_offset_sf:
12952       return "DW_CFA_def_cfa_offset_sf";
12953     case DW_CFA_val_offset:
12954       return "DW_CFA_val_offset";
12955     case DW_CFA_val_offset_sf:
12956       return "DW_CFA_val_offset_sf";
12957     case DW_CFA_val_expression:
12958       return "DW_CFA_val_expression";
12959     /* SGI/MIPS specific.  */
12960     case DW_CFA_MIPS_advance_loc8:
12961       return "DW_CFA_MIPS_advance_loc8";
12962     /* GNU extensions.  */
12963     case DW_CFA_GNU_window_save:
12964       return "DW_CFA_GNU_window_save";
12965     case DW_CFA_GNU_args_size:
12966       return "DW_CFA_GNU_args_size";
12967     case DW_CFA_GNU_negative_offset_extended:
12968       return "DW_CFA_GNU_negative_offset_extended";
12969     default:
12970       return "DW_CFA_<unknown>";
12971     }
12972 }
12973 #endif
12974
12975 static void
12976 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12977 {
12978   unsigned int i;
12979
12980   print_spaces (indent, f);
12981   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12982            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12983
12984   if (die->parent != NULL)
12985     {
12986       print_spaces (indent, f);
12987       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12988                           die->parent->offset);
12989     }
12990
12991   print_spaces (indent, f);
12992   fprintf_unfiltered (f, "  has children: %s\n",
12993            dwarf_bool_name (die->child != NULL));
12994
12995   print_spaces (indent, f);
12996   fprintf_unfiltered (f, "  attributes:\n");
12997
12998   for (i = 0; i < die->num_attrs; ++i)
12999     {
13000       print_spaces (indent, f);
13001       fprintf_unfiltered (f, "    %s (%s) ",
13002                dwarf_attr_name (die->attrs[i].name),
13003                dwarf_form_name (die->attrs[i].form));
13004
13005       switch (die->attrs[i].form)
13006         {
13007         case DW_FORM_ref_addr:
13008         case DW_FORM_addr:
13009           fprintf_unfiltered (f, "address: ");
13010           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13011           break;
13012         case DW_FORM_block2:
13013         case DW_FORM_block4:
13014         case DW_FORM_block:
13015         case DW_FORM_block1:
13016           fprintf_unfiltered (f, "block: size %d",
13017                               DW_BLOCK (&die->attrs[i])->size);
13018           break;
13019         case DW_FORM_exprloc:
13020           fprintf_unfiltered (f, "expression: size %u",
13021                               DW_BLOCK (&die->attrs[i])->size);
13022           break;
13023         case DW_FORM_ref1:
13024         case DW_FORM_ref2:
13025         case DW_FORM_ref4:
13026           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13027                               (long) (DW_ADDR (&die->attrs[i])));
13028           break;
13029         case DW_FORM_data1:
13030         case DW_FORM_data2:
13031         case DW_FORM_data4:
13032         case DW_FORM_data8:
13033         case DW_FORM_udata:
13034         case DW_FORM_sdata:
13035           fprintf_unfiltered (f, "constant: %s",
13036                               pulongest (DW_UNSND (&die->attrs[i])));
13037           break;
13038         case DW_FORM_sec_offset:
13039           fprintf_unfiltered (f, "section offset: %s",
13040                               pulongest (DW_UNSND (&die->attrs[i])));
13041           break;
13042         case DW_FORM_sig8:
13043           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13044             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13045                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13046           else
13047             fprintf_unfiltered (f, "signatured type, offset: unknown");
13048           break;
13049         case DW_FORM_string:
13050         case DW_FORM_strp:
13051           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13052                    DW_STRING (&die->attrs[i])
13053                    ? DW_STRING (&die->attrs[i]) : "",
13054                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13055           break;
13056         case DW_FORM_flag:
13057           if (DW_UNSND (&die->attrs[i]))
13058             fprintf_unfiltered (f, "flag: TRUE");
13059           else
13060             fprintf_unfiltered (f, "flag: FALSE");
13061           break;
13062         case DW_FORM_flag_present:
13063           fprintf_unfiltered (f, "flag: TRUE");
13064           break;
13065         case DW_FORM_indirect:
13066           /* The reader will have reduced the indirect form to
13067              the "base form" so this form should not occur.  */
13068           fprintf_unfiltered (f, 
13069                               "unexpected attribute form: DW_FORM_indirect");
13070           break;
13071         default:
13072           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13073                    die->attrs[i].form);
13074           break;
13075         }
13076       fprintf_unfiltered (f, "\n");
13077     }
13078 }
13079
13080 static void
13081 dump_die_for_error (struct die_info *die)
13082 {
13083   dump_die_shallow (gdb_stderr, 0, die);
13084 }
13085
13086 static void
13087 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13088 {
13089   int indent = level * 4;
13090
13091   gdb_assert (die != NULL);
13092
13093   if (level >= max_level)
13094     return;
13095
13096   dump_die_shallow (f, indent, die);
13097
13098   if (die->child != NULL)
13099     {
13100       print_spaces (indent, f);
13101       fprintf_unfiltered (f, "  Children:");
13102       if (level + 1 < max_level)
13103         {
13104           fprintf_unfiltered (f, "\n");
13105           dump_die_1 (f, level + 1, max_level, die->child);
13106         }
13107       else
13108         {
13109           fprintf_unfiltered (f,
13110                               " [not printed, max nesting level reached]\n");
13111         }
13112     }
13113
13114   if (die->sibling != NULL && level > 0)
13115     {
13116       dump_die_1 (f, level, max_level, die->sibling);
13117     }
13118 }
13119
13120 /* This is called from the pdie macro in gdbinit.in.
13121    It's not static so gcc will keep a copy callable from gdb.  */
13122
13123 void
13124 dump_die (struct die_info *die, int max_level)
13125 {
13126   dump_die_1 (gdb_stdlog, 0, max_level, die);
13127 }
13128
13129 static void
13130 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13131 {
13132   void **slot;
13133
13134   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13135
13136   *slot = die;
13137 }
13138
13139 static int
13140 is_ref_attr (struct attribute *attr)
13141 {
13142   switch (attr->form)
13143     {
13144     case DW_FORM_ref_addr:
13145     case DW_FORM_ref1:
13146     case DW_FORM_ref2:
13147     case DW_FORM_ref4:
13148     case DW_FORM_ref8:
13149     case DW_FORM_ref_udata:
13150       return 1;
13151     default:
13152       return 0;
13153     }
13154 }
13155
13156 static unsigned int
13157 dwarf2_get_ref_die_offset (struct attribute *attr)
13158 {
13159   if (is_ref_attr (attr))
13160     return DW_ADDR (attr);
13161
13162   complaint (&symfile_complaints,
13163              _("unsupported die ref attribute form: '%s'"),
13164              dwarf_form_name (attr->form));
13165   return 0;
13166 }
13167
13168 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13169  * the value held by the attribute is not constant.  */
13170
13171 static LONGEST
13172 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13173 {
13174   if (attr->form == DW_FORM_sdata)
13175     return DW_SND (attr);
13176   else if (attr->form == DW_FORM_udata
13177            || attr->form == DW_FORM_data1
13178            || attr->form == DW_FORM_data2
13179            || attr->form == DW_FORM_data4
13180            || attr->form == DW_FORM_data8)
13181     return DW_UNSND (attr);
13182   else
13183     {
13184       complaint (&symfile_complaints,
13185                  _("Attribute value is not a constant (%s)"),
13186                  dwarf_form_name (attr->form));
13187       return default_value;
13188     }
13189 }
13190
13191 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13192    unit and add it to our queue.
13193    The result is non-zero if PER_CU was queued, otherwise the result is zero
13194    meaning either PER_CU is already queued or it is already loaded.  */
13195
13196 static int
13197 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13198                        struct dwarf2_per_cu_data *per_cu)
13199 {
13200   /* We may arrive here during partial symbol reading, if we need full
13201      DIEs to process an unusual case (e.g. template arguments).  Do
13202      not queue PER_CU, just tell our caller to load its DIEs.  */
13203   if (dwarf2_per_objfile->reading_partial_symbols)
13204     {
13205       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13206         return 1;
13207       return 0;
13208     }
13209
13210   /* Mark the dependence relation so that we don't flush PER_CU
13211      too early.  */
13212   dwarf2_add_dependence (this_cu, per_cu);
13213
13214   /* If it's already on the queue, we have nothing to do.  */
13215   if (per_cu->queued)
13216     return 0;
13217
13218   /* If the compilation unit is already loaded, just mark it as
13219      used.  */
13220   if (per_cu->cu != NULL)
13221     {
13222       per_cu->cu->last_used = 0;
13223       return 0;
13224     }
13225
13226   /* Add it to the queue.  */
13227   queue_comp_unit (per_cu, this_cu->objfile);
13228
13229   return 1;
13230 }
13231
13232 /* Follow reference or signature attribute ATTR of SRC_DIE.
13233    On entry *REF_CU is the CU of SRC_DIE.
13234    On exit *REF_CU is the CU of the result.  */
13235
13236 static struct die_info *
13237 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13238                        struct dwarf2_cu **ref_cu)
13239 {
13240   struct die_info *die;
13241
13242   if (is_ref_attr (attr))
13243     die = follow_die_ref (src_die, attr, ref_cu);
13244   else if (attr->form == DW_FORM_sig8)
13245     die = follow_die_sig (src_die, attr, ref_cu);
13246   else
13247     {
13248       dump_die_for_error (src_die);
13249       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13250              (*ref_cu)->objfile->name);
13251     }
13252
13253   return die;
13254 }
13255
13256 /* Follow reference OFFSET.
13257    On entry *REF_CU is the CU of the source die referencing OFFSET.
13258    On exit *REF_CU is the CU of the result.
13259    Returns NULL if OFFSET is invalid.  */
13260
13261 static struct die_info *
13262 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13263 {
13264   struct die_info temp_die;
13265   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13266
13267   gdb_assert (cu->per_cu != NULL);
13268
13269   target_cu = cu;
13270
13271   if (cu->per_cu->from_debug_types)
13272     {
13273       /* .debug_types CUs cannot reference anything outside their CU.
13274          If they need to, they have to reference a signatured type via
13275          DW_FORM_sig8.  */
13276       if (! offset_in_cu_p (&cu->header, offset))
13277         return NULL;
13278     }
13279   else if (! offset_in_cu_p (&cu->header, offset))
13280     {
13281       struct dwarf2_per_cu_data *per_cu;
13282
13283       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13284
13285       /* If necessary, add it to the queue and load its DIEs.  */
13286       if (maybe_queue_comp_unit (cu, per_cu))
13287         load_full_comp_unit (per_cu, cu->objfile);
13288
13289       target_cu = per_cu->cu;
13290     }
13291   else if (cu->dies == NULL)
13292     {
13293       /* We're loading full DIEs during partial symbol reading.  */
13294       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13295       load_full_comp_unit (cu->per_cu, cu->objfile);
13296     }
13297
13298   *ref_cu = target_cu;
13299   temp_die.offset = offset;
13300   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13301 }
13302
13303 /* Follow reference attribute ATTR of SRC_DIE.
13304    On entry *REF_CU is the CU of SRC_DIE.
13305    On exit *REF_CU is the CU of the result.  */
13306
13307 static struct die_info *
13308 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13309                 struct dwarf2_cu **ref_cu)
13310 {
13311   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13312   struct dwarf2_cu *cu = *ref_cu;
13313   struct die_info *die;
13314
13315   die = follow_die_offset (offset, ref_cu);
13316   if (!die)
13317     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13318            "at 0x%x [in module %s]"),
13319            offset, src_die->offset, cu->objfile->name);
13320
13321   return die;
13322 }
13323
13324 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13325    value is intended for DW_OP_call*.  */
13326
13327 struct dwarf2_locexpr_baton
13328 dwarf2_fetch_die_location_block (unsigned int offset,
13329                                  struct dwarf2_per_cu_data *per_cu,
13330                                  CORE_ADDR (*get_frame_pc) (void *baton),
13331                                  void *baton)
13332 {
13333   struct dwarf2_cu *cu = per_cu->cu;
13334   struct die_info *die;
13335   struct attribute *attr;
13336   struct dwarf2_locexpr_baton retval;
13337
13338   dw2_setup (per_cu->objfile);
13339
13340   die = follow_die_offset (offset, &cu);
13341   if (!die)
13342     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13343            offset, per_cu->cu->objfile->name);
13344
13345   attr = dwarf2_attr (die, DW_AT_location, cu);
13346   if (!attr)
13347     {
13348       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13349
13350       retval.data = NULL;
13351       retval.size = 0;
13352     }
13353   else if (attr_form_is_section_offset (attr))
13354     {
13355       struct dwarf2_loclist_baton loclist_baton;
13356       CORE_ADDR pc = (*get_frame_pc) (baton);
13357       size_t size;
13358
13359       fill_in_loclist_baton (cu, &loclist_baton, attr);
13360
13361       retval.data = dwarf2_find_location_expression (&loclist_baton,
13362                                                      &size, pc);
13363       retval.size = size;
13364     }
13365   else
13366     {
13367       if (!attr_form_is_block (attr))
13368         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13369                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13370                offset, per_cu->cu->objfile->name);
13371
13372       retval.data = DW_BLOCK (attr)->data;
13373       retval.size = DW_BLOCK (attr)->size;
13374     }
13375   retval.per_cu = cu->per_cu;
13376   return retval;
13377 }
13378
13379 /* Follow the signature attribute ATTR in SRC_DIE.
13380    On entry *REF_CU is the CU of SRC_DIE.
13381    On exit *REF_CU is the CU of the result.  */
13382
13383 static struct die_info *
13384 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13385                 struct dwarf2_cu **ref_cu)
13386 {
13387   struct objfile *objfile = (*ref_cu)->objfile;
13388   struct die_info temp_die;
13389   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13390   struct dwarf2_cu *sig_cu;
13391   struct die_info *die;
13392
13393   /* sig_type will be NULL if the signatured type is missing from
13394      the debug info.  */
13395   if (sig_type == NULL)
13396     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13397              "at 0x%x [in module %s]"),
13398            src_die->offset, objfile->name);
13399
13400   /* If necessary, add it to the queue and load its DIEs.  */
13401
13402   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13403     read_signatured_type (objfile, sig_type);
13404
13405   gdb_assert (sig_type->per_cu.cu != NULL);
13406
13407   sig_cu = sig_type->per_cu.cu;
13408   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13409   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13410   if (die)
13411     {
13412       *ref_cu = sig_cu;
13413       return die;
13414     }
13415
13416   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13417          "from DIE at 0x%x [in module %s]"),
13418          sig_type->type_offset, src_die->offset, objfile->name);
13419 }
13420
13421 /* Given an offset of a signatured type, return its signatured_type.  */
13422
13423 static struct signatured_type *
13424 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13425 {
13426   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13427   unsigned int length, initial_length_size;
13428   unsigned int sig_offset;
13429   struct signatured_type find_entry, *type_sig;
13430
13431   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13432   sig_offset = (initial_length_size
13433                 + 2 /*version*/
13434                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13435                 + 1 /*address_size*/);
13436   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13437   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13438
13439   /* This is only used to lookup previously recorded types.
13440      If we didn't find it, it's our bug.  */
13441   gdb_assert (type_sig != NULL);
13442   gdb_assert (offset == type_sig->offset);
13443
13444   return type_sig;
13445 }
13446
13447 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13448
13449 static void
13450 read_signatured_type_at_offset (struct objfile *objfile,
13451                                 unsigned int offset)
13452 {
13453   struct signatured_type *type_sig;
13454
13455   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13456
13457   /* We have the section offset, but we need the signature to do the
13458      hash table lookup.  */
13459   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13460
13461   gdb_assert (type_sig->per_cu.cu == NULL);
13462
13463   read_signatured_type (objfile, type_sig);
13464
13465   gdb_assert (type_sig->per_cu.cu != NULL);
13466 }
13467
13468 /* Read in a signatured type and build its CU and DIEs.  */
13469
13470 static void
13471 read_signatured_type (struct objfile *objfile,
13472                       struct signatured_type *type_sig)
13473 {
13474   gdb_byte *types_ptr;
13475   struct die_reader_specs reader_specs;
13476   struct dwarf2_cu *cu;
13477   ULONGEST signature;
13478   struct cleanup *back_to, *free_cu_cleanup;
13479
13480   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13481   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13482
13483   gdb_assert (type_sig->per_cu.cu == NULL);
13484
13485   cu = xmalloc (sizeof (*cu));
13486   init_one_comp_unit (cu, objfile);
13487
13488   type_sig->per_cu.cu = cu;
13489   cu->per_cu = &type_sig->per_cu;
13490
13491   /* If an error occurs while loading, release our storage.  */
13492   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13493
13494   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13495                                         types_ptr, objfile->obfd);
13496   gdb_assert (signature == type_sig->signature);
13497
13498   cu->die_hash
13499     = htab_create_alloc_ex (cu->header.length / 12,
13500                             die_hash,
13501                             die_eq,
13502                             NULL,
13503                             &cu->comp_unit_obstack,
13504                             hashtab_obstack_allocate,
13505                             dummy_obstack_deallocate);
13506
13507   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13508   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13509
13510   init_cu_die_reader (&reader_specs, cu);
13511
13512   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13513                                     NULL /*parent*/);
13514
13515   /* We try not to read any attributes in this function, because not
13516      all objfiles needed for references have been loaded yet, and symbol
13517      table processing isn't initialized.  But we have to set the CU language,
13518      or we won't be able to build types correctly.  */
13519   prepare_one_comp_unit (cu, cu->dies);
13520
13521   do_cleanups (back_to);
13522
13523   /* We've successfully allocated this compilation unit.  Let our caller
13524      clean it up when finished with it.  */
13525   discard_cleanups (free_cu_cleanup);
13526
13527   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13528   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13529 }
13530
13531 /* Decode simple location descriptions.
13532    Given a pointer to a dwarf block that defines a location, compute
13533    the location and return the value.
13534
13535    NOTE drow/2003-11-18: This function is called in two situations
13536    now: for the address of static or global variables (partial symbols
13537    only) and for offsets into structures which are expected to be
13538    (more or less) constant.  The partial symbol case should go away,
13539    and only the constant case should remain.  That will let this
13540    function complain more accurately.  A few special modes are allowed
13541    without complaint for global variables (for instance, global
13542    register values and thread-local values).
13543
13544    A location description containing no operations indicates that the
13545    object is optimized out.  The return value is 0 for that case.
13546    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13547    callers will only want a very basic result and this can become a
13548    complaint.
13549
13550    Note that stack[0] is unused except as a default error return.  */
13551
13552 static CORE_ADDR
13553 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13554 {
13555   struct objfile *objfile = cu->objfile;
13556   int i;
13557   int size = blk->size;
13558   gdb_byte *data = blk->data;
13559   CORE_ADDR stack[64];
13560   int stacki;
13561   unsigned int bytes_read, unsnd;
13562   gdb_byte op;
13563
13564   i = 0;
13565   stacki = 0;
13566   stack[stacki] = 0;
13567   stack[++stacki] = 0;
13568
13569   while (i < size)
13570     {
13571       op = data[i++];
13572       switch (op)
13573         {
13574         case DW_OP_lit0:
13575         case DW_OP_lit1:
13576         case DW_OP_lit2:
13577         case DW_OP_lit3:
13578         case DW_OP_lit4:
13579         case DW_OP_lit5:
13580         case DW_OP_lit6:
13581         case DW_OP_lit7:
13582         case DW_OP_lit8:
13583         case DW_OP_lit9:
13584         case DW_OP_lit10:
13585         case DW_OP_lit11:
13586         case DW_OP_lit12:
13587         case DW_OP_lit13:
13588         case DW_OP_lit14:
13589         case DW_OP_lit15:
13590         case DW_OP_lit16:
13591         case DW_OP_lit17:
13592         case DW_OP_lit18:
13593         case DW_OP_lit19:
13594         case DW_OP_lit20:
13595         case DW_OP_lit21:
13596         case DW_OP_lit22:
13597         case DW_OP_lit23:
13598         case DW_OP_lit24:
13599         case DW_OP_lit25:
13600         case DW_OP_lit26:
13601         case DW_OP_lit27:
13602         case DW_OP_lit28:
13603         case DW_OP_lit29:
13604         case DW_OP_lit30:
13605         case DW_OP_lit31:
13606           stack[++stacki] = op - DW_OP_lit0;
13607           break;
13608
13609         case DW_OP_reg0:
13610         case DW_OP_reg1:
13611         case DW_OP_reg2:
13612         case DW_OP_reg3:
13613         case DW_OP_reg4:
13614         case DW_OP_reg5:
13615         case DW_OP_reg6:
13616         case DW_OP_reg7:
13617         case DW_OP_reg8:
13618         case DW_OP_reg9:
13619         case DW_OP_reg10:
13620         case DW_OP_reg11:
13621         case DW_OP_reg12:
13622         case DW_OP_reg13:
13623         case DW_OP_reg14:
13624         case DW_OP_reg15:
13625         case DW_OP_reg16:
13626         case DW_OP_reg17:
13627         case DW_OP_reg18:
13628         case DW_OP_reg19:
13629         case DW_OP_reg20:
13630         case DW_OP_reg21:
13631         case DW_OP_reg22:
13632         case DW_OP_reg23:
13633         case DW_OP_reg24:
13634         case DW_OP_reg25:
13635         case DW_OP_reg26:
13636         case DW_OP_reg27:
13637         case DW_OP_reg28:
13638         case DW_OP_reg29:
13639         case DW_OP_reg30:
13640         case DW_OP_reg31:
13641           stack[++stacki] = op - DW_OP_reg0;
13642           if (i < size)
13643             dwarf2_complex_location_expr_complaint ();
13644           break;
13645
13646         case DW_OP_regx:
13647           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13648           i += bytes_read;
13649           stack[++stacki] = unsnd;
13650           if (i < size)
13651             dwarf2_complex_location_expr_complaint ();
13652           break;
13653
13654         case DW_OP_addr:
13655           stack[++stacki] = read_address (objfile->obfd, &data[i],
13656                                           cu, &bytes_read);
13657           i += bytes_read;
13658           break;
13659
13660         case DW_OP_const1u:
13661           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13662           i += 1;
13663           break;
13664
13665         case DW_OP_const1s:
13666           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13667           i += 1;
13668           break;
13669
13670         case DW_OP_const2u:
13671           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13672           i += 2;
13673           break;
13674
13675         case DW_OP_const2s:
13676           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13677           i += 2;
13678           break;
13679
13680         case DW_OP_const4u:
13681           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13682           i += 4;
13683           break;
13684
13685         case DW_OP_const4s:
13686           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13687           i += 4;
13688           break;
13689
13690         case DW_OP_constu:
13691           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13692                                                   &bytes_read);
13693           i += bytes_read;
13694           break;
13695
13696         case DW_OP_consts:
13697           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13698           i += bytes_read;
13699           break;
13700
13701         case DW_OP_dup:
13702           stack[stacki + 1] = stack[stacki];
13703           stacki++;
13704           break;
13705
13706         case DW_OP_plus:
13707           stack[stacki - 1] += stack[stacki];
13708           stacki--;
13709           break;
13710
13711         case DW_OP_plus_uconst:
13712           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13713                                                  &bytes_read);
13714           i += bytes_read;
13715           break;
13716
13717         case DW_OP_minus:
13718           stack[stacki - 1] -= stack[stacki];
13719           stacki--;
13720           break;
13721
13722         case DW_OP_deref:
13723           /* If we're not the last op, then we definitely can't encode
13724              this using GDB's address_class enum.  This is valid for partial
13725              global symbols, although the variable's address will be bogus
13726              in the psymtab.  */
13727           if (i < size)
13728             dwarf2_complex_location_expr_complaint ();
13729           break;
13730
13731         case DW_OP_GNU_push_tls_address:
13732           /* The top of the stack has the offset from the beginning
13733              of the thread control block at which the variable is located.  */
13734           /* Nothing should follow this operator, so the top of stack would
13735              be returned.  */
13736           /* This is valid for partial global symbols, but the variable's
13737              address will be bogus in the psymtab.  */
13738           if (i < size)
13739             dwarf2_complex_location_expr_complaint ();
13740           break;
13741
13742         case DW_OP_GNU_uninit:
13743           break;
13744
13745         default:
13746           {
13747             const char *name = dwarf_stack_op_name (op);
13748
13749             if (name)
13750               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13751                          name);
13752             else
13753               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13754                          op);
13755           }
13756
13757           return (stack[stacki]);
13758         }
13759
13760       /* Enforce maximum stack depth of SIZE-1 to avoid writing
13761          outside of the allocated space.  Also enforce minimum>0.  */
13762       if (stacki >= ARRAY_SIZE (stack) - 1)
13763         {
13764           complaint (&symfile_complaints,
13765                      _("location description stack overflow"));
13766           return 0;
13767         }
13768
13769       if (stacki <= 0)
13770         {
13771           complaint (&symfile_complaints,
13772                      _("location description stack underflow"));
13773           return 0;
13774         }
13775     }
13776   return (stack[stacki]);
13777 }
13778
13779 /* memory allocation interface */
13780
13781 static struct dwarf_block *
13782 dwarf_alloc_block (struct dwarf2_cu *cu)
13783 {
13784   struct dwarf_block *blk;
13785
13786   blk = (struct dwarf_block *)
13787     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13788   return (blk);
13789 }
13790
13791 static struct abbrev_info *
13792 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13793 {
13794   struct abbrev_info *abbrev;
13795
13796   abbrev = (struct abbrev_info *)
13797     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13798   memset (abbrev, 0, sizeof (struct abbrev_info));
13799   return (abbrev);
13800 }
13801
13802 static struct die_info *
13803 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13804 {
13805   struct die_info *die;
13806   size_t size = sizeof (struct die_info);
13807
13808   if (num_attrs > 1)
13809     size += (num_attrs - 1) * sizeof (struct attribute);
13810
13811   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13812   memset (die, 0, sizeof (struct die_info));
13813   return (die);
13814 }
13815
13816 \f
13817 /* Macro support.  */
13818
13819 /* Return the full name of file number I in *LH's file name table.
13820    Use COMP_DIR as the name of the current directory of the
13821    compilation.  The result is allocated using xmalloc; the caller is
13822    responsible for freeing it.  */
13823 static char *
13824 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13825 {
13826   /* Is the file number a valid index into the line header's file name
13827      table?  Remember that file numbers start with one, not zero.  */
13828   if (1 <= file && file <= lh->num_file_names)
13829     {
13830       struct file_entry *fe = &lh->file_names[file - 1];
13831
13832       if (IS_ABSOLUTE_PATH (fe->name))
13833         return xstrdup (fe->name);
13834       else
13835         {
13836           const char *dir;
13837           int dir_len;
13838           char *full_name;
13839
13840           if (fe->dir_index)
13841             dir = lh->include_dirs[fe->dir_index - 1];
13842           else
13843             dir = comp_dir;
13844
13845           if (dir)
13846             {
13847               dir_len = strlen (dir);
13848               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13849               strcpy (full_name, dir);
13850               full_name[dir_len] = '/';
13851               strcpy (full_name + dir_len + 1, fe->name);
13852               return full_name;
13853             }
13854           else
13855             return xstrdup (fe->name);
13856         }
13857     }
13858   else
13859     {
13860       /* The compiler produced a bogus file number.  We can at least
13861          record the macro definitions made in the file, even if we
13862          won't be able to find the file by name.  */
13863       char fake_name[80];
13864
13865       sprintf (fake_name, "<bad macro file number %d>", file);
13866
13867       complaint (&symfile_complaints,
13868                  _("bad file number in macro information (%d)"),
13869                  file);
13870
13871       return xstrdup (fake_name);
13872     }
13873 }
13874
13875
13876 static struct macro_source_file *
13877 macro_start_file (int file, int line,
13878                   struct macro_source_file *current_file,
13879                   const char *comp_dir,
13880                   struct line_header *lh, struct objfile *objfile)
13881 {
13882   /* The full name of this source file.  */
13883   char *full_name = file_full_name (file, lh, comp_dir);
13884
13885   /* We don't create a macro table for this compilation unit
13886      at all until we actually get a filename.  */
13887   if (! pending_macros)
13888     pending_macros = new_macro_table (&objfile->objfile_obstack,
13889                                       objfile->macro_cache);
13890
13891   if (! current_file)
13892     /* If we have no current file, then this must be the start_file
13893        directive for the compilation unit's main source file.  */
13894     current_file = macro_set_main (pending_macros, full_name);
13895   else
13896     current_file = macro_include (current_file, line, full_name);
13897
13898   xfree (full_name);
13899
13900   return current_file;
13901 }
13902
13903
13904 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13905    followed by a null byte.  */
13906 static char *
13907 copy_string (const char *buf, int len)
13908 {
13909   char *s = xmalloc (len + 1);
13910
13911   memcpy (s, buf, len);
13912   s[len] = '\0';
13913   return s;
13914 }
13915
13916
13917 static const char *
13918 consume_improper_spaces (const char *p, const char *body)
13919 {
13920   if (*p == ' ')
13921     {
13922       complaint (&symfile_complaints,
13923                  _("macro definition contains spaces "
13924                    "in formal argument list:\n`%s'"),
13925                  body);
13926
13927       while (*p == ' ')
13928         p++;
13929     }
13930
13931   return p;
13932 }
13933
13934
13935 static void
13936 parse_macro_definition (struct macro_source_file *file, int line,
13937                         const char *body)
13938 {
13939   const char *p;
13940
13941   /* The body string takes one of two forms.  For object-like macro
13942      definitions, it should be:
13943
13944         <macro name> " " <definition>
13945
13946      For function-like macro definitions, it should be:
13947
13948         <macro name> "() " <definition>
13949      or
13950         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13951
13952      Spaces may appear only where explicitly indicated, and in the
13953      <definition>.
13954
13955      The Dwarf 2 spec says that an object-like macro's name is always
13956      followed by a space, but versions of GCC around March 2002 omit
13957      the space when the macro's definition is the empty string.
13958
13959      The Dwarf 2 spec says that there should be no spaces between the
13960      formal arguments in a function-like macro's formal argument list,
13961      but versions of GCC around March 2002 include spaces after the
13962      commas.  */
13963
13964
13965   /* Find the extent of the macro name.  The macro name is terminated
13966      by either a space or null character (for an object-like macro) or
13967      an opening paren (for a function-like macro).  */
13968   for (p = body; *p; p++)
13969     if (*p == ' ' || *p == '(')
13970       break;
13971
13972   if (*p == ' ' || *p == '\0')
13973     {
13974       /* It's an object-like macro.  */
13975       int name_len = p - body;
13976       char *name = copy_string (body, name_len);
13977       const char *replacement;
13978
13979       if (*p == ' ')
13980         replacement = body + name_len + 1;
13981       else
13982         {
13983           dwarf2_macro_malformed_definition_complaint (body);
13984           replacement = body + name_len;
13985         }
13986
13987       macro_define_object (file, line, name, replacement);
13988
13989       xfree (name);
13990     }
13991   else if (*p == '(')
13992     {
13993       /* It's a function-like macro.  */
13994       char *name = copy_string (body, p - body);
13995       int argc = 0;
13996       int argv_size = 1;
13997       char **argv = xmalloc (argv_size * sizeof (*argv));
13998
13999       p++;
14000
14001       p = consume_improper_spaces (p, body);
14002
14003       /* Parse the formal argument list.  */
14004       while (*p && *p != ')')
14005         {
14006           /* Find the extent of the current argument name.  */
14007           const char *arg_start = p;
14008
14009           while (*p && *p != ',' && *p != ')' && *p != ' ')
14010             p++;
14011
14012           if (! *p || p == arg_start)
14013             dwarf2_macro_malformed_definition_complaint (body);
14014           else
14015             {
14016               /* Make sure argv has room for the new argument.  */
14017               if (argc >= argv_size)
14018                 {
14019                   argv_size *= 2;
14020                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14021                 }
14022
14023               argv[argc++] = copy_string (arg_start, p - arg_start);
14024             }
14025
14026           p = consume_improper_spaces (p, body);
14027
14028           /* Consume the comma, if present.  */
14029           if (*p == ',')
14030             {
14031               p++;
14032
14033               p = consume_improper_spaces (p, body);
14034             }
14035         }
14036
14037       if (*p == ')')
14038         {
14039           p++;
14040
14041           if (*p == ' ')
14042             /* Perfectly formed definition, no complaints.  */
14043             macro_define_function (file, line, name,
14044                                    argc, (const char **) argv,
14045                                    p + 1);
14046           else if (*p == '\0')
14047             {
14048               /* Complain, but do define it.  */
14049               dwarf2_macro_malformed_definition_complaint (body);
14050               macro_define_function (file, line, name,
14051                                      argc, (const char **) argv,
14052                                      p);
14053             }
14054           else
14055             /* Just complain.  */
14056             dwarf2_macro_malformed_definition_complaint (body);
14057         }
14058       else
14059         /* Just complain.  */
14060         dwarf2_macro_malformed_definition_complaint (body);
14061
14062       xfree (name);
14063       {
14064         int i;
14065
14066         for (i = 0; i < argc; i++)
14067           xfree (argv[i]);
14068       }
14069       xfree (argv);
14070     }
14071   else
14072     dwarf2_macro_malformed_definition_complaint (body);
14073 }
14074
14075
14076 static void
14077 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14078                      char *comp_dir, bfd *abfd,
14079                      struct dwarf2_cu *cu)
14080 {
14081   gdb_byte *mac_ptr, *mac_end;
14082   struct macro_source_file *current_file = 0;
14083   enum dwarf_macinfo_record_type macinfo_type;
14084   int at_commandline;
14085
14086   dwarf2_read_section (dwarf2_per_objfile->objfile,
14087                        &dwarf2_per_objfile->macinfo);
14088   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14089     {
14090       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14091       return;
14092     }
14093
14094   /* First pass: Find the name of the base filename.
14095      This filename is needed in order to process all macros whose definition
14096      (or undefinition) comes from the command line.  These macros are defined
14097      before the first DW_MACINFO_start_file entry, and yet still need to be
14098      associated to the base file.
14099
14100      To determine the base file name, we scan the macro definitions until we
14101      reach the first DW_MACINFO_start_file entry.  We then initialize
14102      CURRENT_FILE accordingly so that any macro definition found before the
14103      first DW_MACINFO_start_file can still be associated to the base file.  */
14104
14105   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14106   mac_end = dwarf2_per_objfile->macinfo.buffer
14107     + dwarf2_per_objfile->macinfo.size;
14108
14109   do
14110     {
14111       /* Do we at least have room for a macinfo type byte?  */
14112       if (mac_ptr >= mac_end)
14113         {
14114           /* Complaint is printed during the second pass as GDB will probably
14115              stop the first pass earlier upon finding
14116              DW_MACINFO_start_file.  */
14117           break;
14118         }
14119
14120       macinfo_type = read_1_byte (abfd, mac_ptr);
14121       mac_ptr++;
14122
14123       switch (macinfo_type)
14124         {
14125           /* A zero macinfo type indicates the end of the macro
14126              information.  */
14127         case 0:
14128           break;
14129
14130         case DW_MACINFO_define:
14131         case DW_MACINFO_undef:
14132           /* Only skip the data by MAC_PTR.  */
14133           {
14134             unsigned int bytes_read;
14135
14136             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14137             mac_ptr += bytes_read;
14138             read_direct_string (abfd, mac_ptr, &bytes_read);
14139             mac_ptr += bytes_read;
14140           }
14141           break;
14142
14143         case DW_MACINFO_start_file:
14144           {
14145             unsigned int bytes_read;
14146             int line, file;
14147
14148             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14149             mac_ptr += bytes_read;
14150             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14151             mac_ptr += bytes_read;
14152
14153             current_file = macro_start_file (file, line, current_file,
14154                                              comp_dir, lh, cu->objfile);
14155           }
14156           break;
14157
14158         case DW_MACINFO_end_file:
14159           /* No data to skip by MAC_PTR.  */
14160           break;
14161
14162         case DW_MACINFO_vendor_ext:
14163           /* Only skip the data by MAC_PTR.  */
14164           {
14165             unsigned int bytes_read;
14166
14167             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14168             mac_ptr += bytes_read;
14169             read_direct_string (abfd, mac_ptr, &bytes_read);
14170             mac_ptr += bytes_read;
14171           }
14172           break;
14173
14174         default:
14175           break;
14176         }
14177     } while (macinfo_type != 0 && current_file == NULL);
14178
14179   /* Second pass: Process all entries.
14180
14181      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14182      command-line macro definitions/undefinitions.  This flag is unset when we
14183      reach the first DW_MACINFO_start_file entry.  */
14184
14185   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14186
14187   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14188      GDB is still reading the definitions from command line.  First
14189      DW_MACINFO_start_file will need to be ignored as it was already executed
14190      to create CURRENT_FILE for the main source holding also the command line
14191      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14192      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14193
14194   at_commandline = 1;
14195
14196   do
14197     {
14198       /* Do we at least have room for a macinfo type byte?  */
14199       if (mac_ptr >= mac_end)
14200         {
14201           dwarf2_macros_too_long_complaint ();
14202           break;
14203         }
14204
14205       macinfo_type = read_1_byte (abfd, mac_ptr);
14206       mac_ptr++;
14207
14208       switch (macinfo_type)
14209         {
14210           /* A zero macinfo type indicates the end of the macro
14211              information.  */
14212         case 0:
14213           break;
14214
14215         case DW_MACINFO_define:
14216         case DW_MACINFO_undef:
14217           {
14218             unsigned int bytes_read;
14219             int line;
14220             char *body;
14221
14222             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14223             mac_ptr += bytes_read;
14224             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14225             mac_ptr += bytes_read;
14226
14227             if (! current_file)
14228               {
14229                 /* DWARF violation as no main source is present.  */
14230                 complaint (&symfile_complaints,
14231                            _("debug info with no main source gives macro %s "
14232                              "on line %d: %s"),
14233                            macinfo_type == DW_MACINFO_define ?
14234                              _("definition") :
14235                                macinfo_type == DW_MACINFO_undef ?
14236                                  _("undefinition") :
14237                                  _("something-or-other"), line, body);
14238                 break;
14239               }
14240             if ((line == 0 && !at_commandline)
14241                 || (line != 0 && at_commandline))
14242               complaint (&symfile_complaints,
14243                          _("debug info gives %s macro %s with %s line %d: %s"),
14244                          at_commandline ? _("command-line") : _("in-file"),
14245                          macinfo_type == DW_MACINFO_define ?
14246                            _("definition") :
14247                              macinfo_type == DW_MACINFO_undef ?
14248                                _("undefinition") :
14249                                _("something-or-other"),
14250                          line == 0 ? _("zero") : _("non-zero"), line, body);
14251
14252             if (macinfo_type == DW_MACINFO_define)
14253               parse_macro_definition (current_file, line, body);
14254             else if (macinfo_type == DW_MACINFO_undef)
14255               macro_undef (current_file, line, body);
14256           }
14257           break;
14258
14259         case DW_MACINFO_start_file:
14260           {
14261             unsigned int bytes_read;
14262             int line, file;
14263
14264             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14265             mac_ptr += bytes_read;
14266             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14267             mac_ptr += bytes_read;
14268
14269             if ((line == 0 && !at_commandline)
14270                 || (line != 0 && at_commandline))
14271               complaint (&symfile_complaints,
14272                          _("debug info gives source %d included "
14273                            "from %s at %s line %d"),
14274                          file, at_commandline ? _("command-line") : _("file"),
14275                          line == 0 ? _("zero") : _("non-zero"), line);
14276
14277             if (at_commandline)
14278               {
14279                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14280                 at_commandline = 0;
14281               }
14282             else
14283               current_file = macro_start_file (file, line,
14284                                                current_file, comp_dir,
14285                                                lh, cu->objfile);
14286           }
14287           break;
14288
14289         case DW_MACINFO_end_file:
14290           if (! current_file)
14291             complaint (&symfile_complaints,
14292                        _("macro debug info has an unmatched "
14293                          "`close_file' directive"));
14294           else
14295             {
14296               current_file = current_file->included_by;
14297               if (! current_file)
14298                 {
14299                   enum dwarf_macinfo_record_type next_type;
14300
14301                   /* GCC circa March 2002 doesn't produce the zero
14302                      type byte marking the end of the compilation
14303                      unit.  Complain if it's not there, but exit no
14304                      matter what.  */
14305
14306                   /* Do we at least have room for a macinfo type byte?  */
14307                   if (mac_ptr >= mac_end)
14308                     {
14309                       dwarf2_macros_too_long_complaint ();
14310                       return;
14311                     }
14312
14313                   /* We don't increment mac_ptr here, so this is just
14314                      a look-ahead.  */
14315                   next_type = read_1_byte (abfd, mac_ptr);
14316                   if (next_type != 0)
14317                     complaint (&symfile_complaints,
14318                                _("no terminating 0-type entry for "
14319                                  "macros in `.debug_macinfo' section"));
14320
14321                   return;
14322                 }
14323             }
14324           break;
14325
14326         case DW_MACINFO_vendor_ext:
14327           {
14328             unsigned int bytes_read;
14329             int constant;
14330
14331             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14332             mac_ptr += bytes_read;
14333             read_direct_string (abfd, mac_ptr, &bytes_read);
14334             mac_ptr += bytes_read;
14335
14336             /* We don't recognize any vendor extensions.  */
14337           }
14338           break;
14339         }
14340     } while (macinfo_type != 0);
14341 }
14342
14343 /* Check if the attribute's form is a DW_FORM_block*
14344    if so return true else false.  */
14345 static int
14346 attr_form_is_block (struct attribute *attr)
14347 {
14348   return (attr == NULL ? 0 :
14349       attr->form == DW_FORM_block1
14350       || attr->form == DW_FORM_block2
14351       || attr->form == DW_FORM_block4
14352       || attr->form == DW_FORM_block
14353       || attr->form == DW_FORM_exprloc);
14354 }
14355
14356 /* Return non-zero if ATTR's value is a section offset --- classes
14357    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14358    You may use DW_UNSND (attr) to retrieve such offsets.
14359
14360    Section 7.5.4, "Attribute Encodings", explains that no attribute
14361    may have a value that belongs to more than one of these classes; it
14362    would be ambiguous if we did, because we use the same forms for all
14363    of them.  */
14364 static int
14365 attr_form_is_section_offset (struct attribute *attr)
14366 {
14367   return (attr->form == DW_FORM_data4
14368           || attr->form == DW_FORM_data8
14369           || attr->form == DW_FORM_sec_offset);
14370 }
14371
14372
14373 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14374    zero otherwise.  When this function returns true, you can apply
14375    dwarf2_get_attr_constant_value to it.
14376
14377    However, note that for some attributes you must check
14378    attr_form_is_section_offset before using this test.  DW_FORM_data4
14379    and DW_FORM_data8 are members of both the constant class, and of
14380    the classes that contain offsets into other debug sections
14381    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14382    that, if an attribute's can be either a constant or one of the
14383    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14384    taken as section offsets, not constants.  */
14385 static int
14386 attr_form_is_constant (struct attribute *attr)
14387 {
14388   switch (attr->form)
14389     {
14390     case DW_FORM_sdata:
14391     case DW_FORM_udata:
14392     case DW_FORM_data1:
14393     case DW_FORM_data2:
14394     case DW_FORM_data4:
14395     case DW_FORM_data8:
14396       return 1;
14397     default:
14398       return 0;
14399     }
14400 }
14401
14402 /* A helper function that fills in a dwarf2_loclist_baton.  */
14403
14404 static void
14405 fill_in_loclist_baton (struct dwarf2_cu *cu,
14406                        struct dwarf2_loclist_baton *baton,
14407                        struct attribute *attr)
14408 {
14409   dwarf2_read_section (dwarf2_per_objfile->objfile,
14410                        &dwarf2_per_objfile->loc);
14411
14412   baton->per_cu = cu->per_cu;
14413   gdb_assert (baton->per_cu);
14414   /* We don't know how long the location list is, but make sure we
14415      don't run off the edge of the section.  */
14416   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14417   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14418   baton->base_address = cu->base_address;
14419 }
14420
14421 static void
14422 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14423                              struct dwarf2_cu *cu)
14424 {
14425   if (attr_form_is_section_offset (attr)
14426       /* ".debug_loc" may not exist at all, or the offset may be outside
14427          the section.  If so, fall through to the complaint in the
14428          other branch.  */
14429       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14430                                                 &dwarf2_per_objfile->loc))
14431     {
14432       struct dwarf2_loclist_baton *baton;
14433
14434       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14435                              sizeof (struct dwarf2_loclist_baton));
14436
14437       fill_in_loclist_baton (cu, baton, attr);
14438
14439       if (cu->base_known == 0)
14440         complaint (&symfile_complaints,
14441                    _("Location list used without "
14442                      "specifying the CU base address."));
14443
14444       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14445       SYMBOL_LOCATION_BATON (sym) = baton;
14446     }
14447   else
14448     {
14449       struct dwarf2_locexpr_baton *baton;
14450
14451       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14452                              sizeof (struct dwarf2_locexpr_baton));
14453       baton->per_cu = cu->per_cu;
14454       gdb_assert (baton->per_cu);
14455
14456       if (attr_form_is_block (attr))
14457         {
14458           /* Note that we're just copying the block's data pointer
14459              here, not the actual data.  We're still pointing into the
14460              info_buffer for SYM's objfile; right now we never release
14461              that buffer, but when we do clean up properly this may
14462              need to change.  */
14463           baton->size = DW_BLOCK (attr)->size;
14464           baton->data = DW_BLOCK (attr)->data;
14465         }
14466       else
14467         {
14468           dwarf2_invalid_attrib_class_complaint ("location description",
14469                                                  SYMBOL_NATURAL_NAME (sym));
14470           baton->size = 0;
14471           baton->data = NULL;
14472         }
14473
14474       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14475       SYMBOL_LOCATION_BATON (sym) = baton;
14476     }
14477 }
14478
14479 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14480    came from a separate debuginfo file, then the master objfile is
14481    returned.  */
14482
14483 struct objfile *
14484 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14485 {
14486   struct objfile *objfile = per_cu->objfile;
14487
14488   /* Return the master objfile, so that we can report and look up the
14489      correct file containing this variable.  */
14490   if (objfile->separate_debug_objfile_backlink)
14491     objfile = objfile->separate_debug_objfile_backlink;
14492
14493   return objfile;
14494 }
14495
14496 /* Return the address size given in the compilation unit header for CU.  */
14497
14498 CORE_ADDR
14499 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14500 {
14501   if (per_cu->cu)
14502     return per_cu->cu->header.addr_size;
14503   else
14504     {
14505       /* If the CU is not currently read in, we re-read its header.  */
14506       struct objfile *objfile = per_cu->objfile;
14507       struct dwarf2_per_objfile *per_objfile
14508         = objfile_data (objfile, dwarf2_objfile_data_key);
14509       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14510       struct comp_unit_head cu_header;
14511
14512       memset (&cu_header, 0, sizeof cu_header);
14513       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14514       return cu_header.addr_size;
14515     }
14516 }
14517
14518 /* Return the offset size given in the compilation unit header for CU.  */
14519
14520 int
14521 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14522 {
14523   if (per_cu->cu)
14524     return per_cu->cu->header.offset_size;
14525   else
14526     {
14527       /* If the CU is not currently read in, we re-read its header.  */
14528       struct objfile *objfile = per_cu->objfile;
14529       struct dwarf2_per_objfile *per_objfile
14530         = objfile_data (objfile, dwarf2_objfile_data_key);
14531       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14532       struct comp_unit_head cu_header;
14533
14534       memset (&cu_header, 0, sizeof cu_header);
14535       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14536       return cu_header.offset_size;
14537     }
14538 }
14539
14540 /* Return the text offset of the CU.  The returned offset comes from
14541    this CU's objfile.  If this objfile came from a separate debuginfo
14542    file, then the offset may be different from the corresponding
14543    offset in the parent objfile.  */
14544
14545 CORE_ADDR
14546 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14547 {
14548   struct objfile *objfile = per_cu->objfile;
14549
14550   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14551 }
14552
14553 /* Locate the .debug_info compilation unit from CU's objfile which contains
14554    the DIE at OFFSET.  Raises an error on failure.  */
14555
14556 static struct dwarf2_per_cu_data *
14557 dwarf2_find_containing_comp_unit (unsigned int offset,
14558                                   struct objfile *objfile)
14559 {
14560   struct dwarf2_per_cu_data *this_cu;
14561   int low, high;
14562
14563   low = 0;
14564   high = dwarf2_per_objfile->n_comp_units - 1;
14565   while (high > low)
14566     {
14567       int mid = low + (high - low) / 2;
14568
14569       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14570         high = mid;
14571       else
14572         low = mid + 1;
14573     }
14574   gdb_assert (low == high);
14575   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14576     {
14577       if (low == 0)
14578         error (_("Dwarf Error: could not find partial DIE containing "
14579                "offset 0x%lx [in module %s]"),
14580                (long) offset, bfd_get_filename (objfile->obfd));
14581
14582       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14583       return dwarf2_per_objfile->all_comp_units[low-1];
14584     }
14585   else
14586     {
14587       this_cu = dwarf2_per_objfile->all_comp_units[low];
14588       if (low == dwarf2_per_objfile->n_comp_units - 1
14589           && offset >= this_cu->offset + this_cu->length)
14590         error (_("invalid dwarf2 offset %u"), offset);
14591       gdb_assert (offset < this_cu->offset + this_cu->length);
14592       return this_cu;
14593     }
14594 }
14595
14596 /* Locate the compilation unit from OBJFILE which is located at exactly
14597    OFFSET.  Raises an error on failure.  */
14598
14599 static struct dwarf2_per_cu_data *
14600 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14601 {
14602   struct dwarf2_per_cu_data *this_cu;
14603
14604   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14605   if (this_cu->offset != offset)
14606     error (_("no compilation unit with offset %u."), offset);
14607   return this_cu;
14608 }
14609
14610 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14611
14612 static void
14613 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14614 {
14615   memset (cu, 0, sizeof (*cu));
14616   cu->objfile = objfile;
14617   obstack_init (&cu->comp_unit_obstack);
14618 }
14619
14620 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14621
14622 static void
14623 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14624 {
14625   struct attribute *attr;
14626
14627   /* Set the language we're debugging.  */
14628   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14629   if (attr)
14630     set_cu_language (DW_UNSND (attr), cu);
14631   else
14632     set_cu_language (language_minimal, cu);
14633 }
14634
14635 /* Release one cached compilation unit, CU.  We unlink it from the tree
14636    of compilation units, but we don't remove it from the read_in_chain;
14637    the caller is responsible for that.
14638    NOTE: DATA is a void * because this function is also used as a
14639    cleanup routine.  */
14640
14641 static void
14642 free_one_comp_unit (void *data)
14643 {
14644   struct dwarf2_cu *cu = data;
14645
14646   if (cu->per_cu != NULL)
14647     cu->per_cu->cu = NULL;
14648   cu->per_cu = NULL;
14649
14650   obstack_free (&cu->comp_unit_obstack, NULL);
14651
14652   xfree (cu);
14653 }
14654
14655 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14656    when we're finished with it.  We can't free the pointer itself, but be
14657    sure to unlink it from the cache.  Also release any associated storage
14658    and perform cache maintenance.
14659
14660    Only used during partial symbol parsing.  */
14661
14662 static void
14663 free_stack_comp_unit (void *data)
14664 {
14665   struct dwarf2_cu *cu = data;
14666
14667   obstack_free (&cu->comp_unit_obstack, NULL);
14668   cu->partial_dies = NULL;
14669
14670   if (cu->per_cu != NULL)
14671     {
14672       /* This compilation unit is on the stack in our caller, so we
14673          should not xfree it.  Just unlink it.  */
14674       cu->per_cu->cu = NULL;
14675       cu->per_cu = NULL;
14676
14677       /* If we had a per-cu pointer, then we may have other compilation
14678          units loaded, so age them now.  */
14679       age_cached_comp_units ();
14680     }
14681 }
14682
14683 /* Free all cached compilation units.  */
14684
14685 static void
14686 free_cached_comp_units (void *data)
14687 {
14688   struct dwarf2_per_cu_data *per_cu, **last_chain;
14689
14690   per_cu = dwarf2_per_objfile->read_in_chain;
14691   last_chain = &dwarf2_per_objfile->read_in_chain;
14692   while (per_cu != NULL)
14693     {
14694       struct dwarf2_per_cu_data *next_cu;
14695
14696       next_cu = per_cu->cu->read_in_chain;
14697
14698       free_one_comp_unit (per_cu->cu);
14699       *last_chain = next_cu;
14700
14701       per_cu = next_cu;
14702     }
14703 }
14704
14705 /* Increase the age counter on each cached compilation unit, and free
14706    any that are too old.  */
14707
14708 static void
14709 age_cached_comp_units (void)
14710 {
14711   struct dwarf2_per_cu_data *per_cu, **last_chain;
14712
14713   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14714   per_cu = dwarf2_per_objfile->read_in_chain;
14715   while (per_cu != NULL)
14716     {
14717       per_cu->cu->last_used ++;
14718       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14719         dwarf2_mark (per_cu->cu);
14720       per_cu = per_cu->cu->read_in_chain;
14721     }
14722
14723   per_cu = dwarf2_per_objfile->read_in_chain;
14724   last_chain = &dwarf2_per_objfile->read_in_chain;
14725   while (per_cu != NULL)
14726     {
14727       struct dwarf2_per_cu_data *next_cu;
14728
14729       next_cu = per_cu->cu->read_in_chain;
14730
14731       if (!per_cu->cu->mark)
14732         {
14733           free_one_comp_unit (per_cu->cu);
14734           *last_chain = next_cu;
14735         }
14736       else
14737         last_chain = &per_cu->cu->read_in_chain;
14738
14739       per_cu = next_cu;
14740     }
14741 }
14742
14743 /* Remove a single compilation unit from the cache.  */
14744
14745 static void
14746 free_one_cached_comp_unit (void *target_cu)
14747 {
14748   struct dwarf2_per_cu_data *per_cu, **last_chain;
14749
14750   per_cu = dwarf2_per_objfile->read_in_chain;
14751   last_chain = &dwarf2_per_objfile->read_in_chain;
14752   while (per_cu != NULL)
14753     {
14754       struct dwarf2_per_cu_data *next_cu;
14755
14756       next_cu = per_cu->cu->read_in_chain;
14757
14758       if (per_cu->cu == target_cu)
14759         {
14760           free_one_comp_unit (per_cu->cu);
14761           *last_chain = next_cu;
14762           break;
14763         }
14764       else
14765         last_chain = &per_cu->cu->read_in_chain;
14766
14767       per_cu = next_cu;
14768     }
14769 }
14770
14771 /* Release all extra memory associated with OBJFILE.  */
14772
14773 void
14774 dwarf2_free_objfile (struct objfile *objfile)
14775 {
14776   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14777
14778   if (dwarf2_per_objfile == NULL)
14779     return;
14780
14781   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14782   free_cached_comp_units (NULL);
14783
14784   if (dwarf2_per_objfile->quick_file_names_table)
14785     htab_delete (dwarf2_per_objfile->quick_file_names_table);
14786
14787   /* Everything else should be on the objfile obstack.  */
14788 }
14789
14790 /* A pair of DIE offset and GDB type pointer.  We store these
14791    in a hash table separate from the DIEs, and preserve them
14792    when the DIEs are flushed out of cache.  */
14793
14794 struct dwarf2_offset_and_type
14795 {
14796   unsigned int offset;
14797   struct type *type;
14798 };
14799
14800 /* Hash function for a dwarf2_offset_and_type.  */
14801
14802 static hashval_t
14803 offset_and_type_hash (const void *item)
14804 {
14805   const struct dwarf2_offset_and_type *ofs = item;
14806
14807   return ofs->offset;
14808 }
14809
14810 /* Equality function for a dwarf2_offset_and_type.  */
14811
14812 static int
14813 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14814 {
14815   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14816   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14817
14818   return ofs_lhs->offset == ofs_rhs->offset;
14819 }
14820
14821 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14822    table if necessary.  For convenience, return TYPE.
14823
14824    The DIEs reading must have careful ordering to:
14825     * Not cause infite loops trying to read in DIEs as a prerequisite for
14826       reading current DIE.
14827     * Not trying to dereference contents of still incompletely read in types
14828       while reading in other DIEs.
14829     * Enable referencing still incompletely read in types just by a pointer to
14830       the type without accessing its fields.
14831
14832    Therefore caller should follow these rules:
14833      * Try to fetch any prerequisite types we may need to build this DIE type
14834        before building the type and calling set_die_type.
14835      * After building type call set_die_type for current DIE as soon as
14836        possible before fetching more types to complete the current type.
14837      * Make the type as complete as possible before fetching more types.  */
14838
14839 static struct type *
14840 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14841 {
14842   struct dwarf2_offset_and_type **slot, ofs;
14843   struct objfile *objfile = cu->objfile;
14844   htab_t *type_hash_ptr;
14845
14846   /* For Ada types, make sure that the gnat-specific data is always
14847      initialized (if not already set).  There are a few types where
14848      we should not be doing so, because the type-specific area is
14849      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14850      where the type-specific area is used to store the floatformat).
14851      But this is not a problem, because the gnat-specific information
14852      is actually not needed for these types.  */
14853   if (need_gnat_info (cu)
14854       && TYPE_CODE (type) != TYPE_CODE_FUNC
14855       && TYPE_CODE (type) != TYPE_CODE_FLT
14856       && !HAVE_GNAT_AUX_INFO (type))
14857     INIT_GNAT_SPECIFIC (type);
14858
14859   if (cu->per_cu->from_debug_types)
14860     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14861   else
14862     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14863
14864   if (*type_hash_ptr == NULL)
14865     {
14866       *type_hash_ptr
14867         = htab_create_alloc_ex (127,
14868                                 offset_and_type_hash,
14869                                 offset_and_type_eq,
14870                                 NULL,
14871                                 &objfile->objfile_obstack,
14872                                 hashtab_obstack_allocate,
14873                                 dummy_obstack_deallocate);
14874     }
14875
14876   ofs.offset = die->offset;
14877   ofs.type = type;
14878   slot = (struct dwarf2_offset_and_type **)
14879     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14880   if (*slot)
14881     complaint (&symfile_complaints,
14882                _("A problem internal to GDB: DIE 0x%x has type already set"),
14883                die->offset);
14884   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14885   **slot = ofs;
14886   return type;
14887 }
14888
14889 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14890    table, or return NULL if the die does not have a saved type.  */
14891
14892 static struct type *
14893 get_die_type_at_offset (unsigned int offset,
14894                         struct dwarf2_per_cu_data *per_cu)
14895 {
14896   struct dwarf2_offset_and_type *slot, ofs;
14897   htab_t type_hash;
14898
14899   if (per_cu->from_debug_types)
14900     type_hash = dwarf2_per_objfile->debug_types_type_hash;
14901   else
14902     type_hash = dwarf2_per_objfile->debug_info_type_hash;
14903   if (type_hash == NULL)
14904     return NULL;
14905
14906   ofs.offset = offset;
14907   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14908   if (slot)
14909     return slot->type;
14910   else
14911     return NULL;
14912 }
14913
14914 /* Look up the type for DIE in the appropriate type_hash table,
14915    or return NULL if DIE does not have a saved type.  */
14916
14917 static struct type *
14918 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14919 {
14920   return get_die_type_at_offset (die->offset, cu->per_cu);
14921 }
14922
14923 /* Add a dependence relationship from CU to REF_PER_CU.  */
14924
14925 static void
14926 dwarf2_add_dependence (struct dwarf2_cu *cu,
14927                        struct dwarf2_per_cu_data *ref_per_cu)
14928 {
14929   void **slot;
14930
14931   if (cu->dependencies == NULL)
14932     cu->dependencies
14933       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14934                               NULL, &cu->comp_unit_obstack,
14935                               hashtab_obstack_allocate,
14936                               dummy_obstack_deallocate);
14937
14938   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14939   if (*slot == NULL)
14940     *slot = ref_per_cu;
14941 }
14942
14943 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14944    Set the mark field in every compilation unit in the
14945    cache that we must keep because we are keeping CU.  */
14946
14947 static int
14948 dwarf2_mark_helper (void **slot, void *data)
14949 {
14950   struct dwarf2_per_cu_data *per_cu;
14951
14952   per_cu = (struct dwarf2_per_cu_data *) *slot;
14953   if (per_cu->cu->mark)
14954     return 1;
14955   per_cu->cu->mark = 1;
14956
14957   if (per_cu->cu->dependencies != NULL)
14958     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14959
14960   return 1;
14961 }
14962
14963 /* Set the mark field in CU and in every other compilation unit in the
14964    cache that we must keep because we are keeping CU.  */
14965
14966 static void
14967 dwarf2_mark (struct dwarf2_cu *cu)
14968 {
14969   if (cu->mark)
14970     return;
14971   cu->mark = 1;
14972   if (cu->dependencies != NULL)
14973     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14974 }
14975
14976 static void
14977 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14978 {
14979   while (per_cu)
14980     {
14981       per_cu->cu->mark = 0;
14982       per_cu = per_cu->cu->read_in_chain;
14983     }
14984 }
14985
14986 /* Trivial hash function for partial_die_info: the hash value of a DIE
14987    is its offset in .debug_info for this objfile.  */
14988
14989 static hashval_t
14990 partial_die_hash (const void *item)
14991 {
14992   const struct partial_die_info *part_die = item;
14993
14994   return part_die->offset;
14995 }
14996
14997 /* Trivial comparison function for partial_die_info structures: two DIEs
14998    are equal if they have the same offset.  */
14999
15000 static int
15001 partial_die_eq (const void *item_lhs, const void *item_rhs)
15002 {
15003   const struct partial_die_info *part_die_lhs = item_lhs;
15004   const struct partial_die_info *part_die_rhs = item_rhs;
15005
15006   return part_die_lhs->offset == part_die_rhs->offset;
15007 }
15008
15009 static struct cmd_list_element *set_dwarf2_cmdlist;
15010 static struct cmd_list_element *show_dwarf2_cmdlist;
15011
15012 static void
15013 set_dwarf2_cmd (char *args, int from_tty)
15014 {
15015   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15016 }
15017
15018 static void
15019 show_dwarf2_cmd (char *args, int from_tty)
15020 {
15021   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15022 }
15023
15024 /* If section described by INFO was mmapped, munmap it now.  */
15025
15026 static void
15027 munmap_section_buffer (struct dwarf2_section_info *info)
15028 {
15029   if (info->was_mmapped)
15030     {
15031 #ifdef HAVE_MMAP
15032       intptr_t begin = (intptr_t) info->buffer;
15033       intptr_t map_begin = begin & ~(pagesize - 1);
15034       size_t map_length = info->size + begin - map_begin;
15035
15036       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15037 #else
15038       /* Without HAVE_MMAP, we should never be here to begin with.  */
15039       gdb_assert_not_reached ("no mmap support");
15040 #endif
15041     }
15042 }
15043
15044 /* munmap debug sections for OBJFILE, if necessary.  */
15045
15046 static void
15047 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15048 {
15049   struct dwarf2_per_objfile *data = d;
15050
15051   /* This is sorted according to the order they're defined in to make it easier
15052      to keep in sync.  */
15053   munmap_section_buffer (&data->info);
15054   munmap_section_buffer (&data->abbrev);
15055   munmap_section_buffer (&data->line);
15056   munmap_section_buffer (&data->loc);
15057   munmap_section_buffer (&data->macinfo);
15058   munmap_section_buffer (&data->str);
15059   munmap_section_buffer (&data->ranges);
15060   munmap_section_buffer (&data->types);
15061   munmap_section_buffer (&data->frame);
15062   munmap_section_buffer (&data->eh_frame);
15063   munmap_section_buffer (&data->gdb_index);
15064 }
15065
15066 \f
15067 /* The "save gdb-index" command.  */
15068
15069 /* The contents of the hash table we create when building the string
15070    table.  */
15071 struct strtab_entry
15072 {
15073   offset_type offset;
15074   const char *str;
15075 };
15076
15077 /* Hash function for a strtab_entry.  */
15078
15079 static hashval_t
15080 hash_strtab_entry (const void *e)
15081 {
15082   const struct strtab_entry *entry = e;
15083   return mapped_index_string_hash (entry->str);
15084 }
15085
15086 /* Equality function for a strtab_entry.  */
15087
15088 static int
15089 eq_strtab_entry (const void *a, const void *b)
15090 {
15091   const struct strtab_entry *ea = a;
15092   const struct strtab_entry *eb = b;
15093   return !strcmp (ea->str, eb->str);
15094 }
15095
15096 /* Create a strtab_entry hash table.  */
15097
15098 static htab_t
15099 create_strtab (void)
15100 {
15101   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15102                             xfree, xcalloc, xfree);
15103 }
15104
15105 /* Add a string to the constant pool.  Return the string's offset in
15106    host order.  */
15107
15108 static offset_type
15109 add_string (htab_t table, struct obstack *cpool, const char *str)
15110 {
15111   void **slot;
15112   struct strtab_entry entry;
15113   struct strtab_entry *result;
15114
15115   entry.str = str;
15116   slot = htab_find_slot (table, &entry, INSERT);
15117   if (*slot)
15118     result = *slot;
15119   else
15120     {
15121       result = XNEW (struct strtab_entry);
15122       result->offset = obstack_object_size (cpool);
15123       result->str = str;
15124       obstack_grow_str0 (cpool, str);
15125       *slot = result;
15126     }
15127   return result->offset;
15128 }
15129
15130 /* An entry in the symbol table.  */
15131 struct symtab_index_entry
15132 {
15133   /* The name of the symbol.  */
15134   const char *name;
15135   /* The offset of the name in the constant pool.  */
15136   offset_type index_offset;
15137   /* A sorted vector of the indices of all the CUs that hold an object
15138      of this name.  */
15139   VEC (offset_type) *cu_indices;
15140 };
15141
15142 /* The symbol table.  This is a power-of-2-sized hash table.  */
15143 struct mapped_symtab
15144 {
15145   offset_type n_elements;
15146   offset_type size;
15147   struct symtab_index_entry **data;
15148 };
15149
15150 /* Hash function for a symtab_index_entry.  */
15151
15152 static hashval_t
15153 hash_symtab_entry (const void *e)
15154 {
15155   const struct symtab_index_entry *entry = e;
15156   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15157                          sizeof (offset_type) * VEC_length (offset_type,
15158                                                             entry->cu_indices),
15159                          0);
15160 }
15161
15162 /* Equality function for a symtab_index_entry.  */
15163
15164 static int
15165 eq_symtab_entry (const void *a, const void *b)
15166 {
15167   const struct symtab_index_entry *ea = a;
15168   const struct symtab_index_entry *eb = b;
15169   int len = VEC_length (offset_type, ea->cu_indices);
15170   if (len != VEC_length (offset_type, eb->cu_indices))
15171     return 0;
15172   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15173                   VEC_address (offset_type, eb->cu_indices),
15174                   sizeof (offset_type) * len);
15175 }
15176
15177 /* Destroy a symtab_index_entry.  */
15178
15179 static void
15180 delete_symtab_entry (void *p)
15181 {
15182   struct symtab_index_entry *entry = p;
15183   VEC_free (offset_type, entry->cu_indices);
15184   xfree (entry);
15185 }
15186
15187 /* Create a hash table holding symtab_index_entry objects.  */
15188
15189 static htab_t
15190 create_symbol_hash_table (void)
15191 {
15192   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15193                             delete_symtab_entry, xcalloc, xfree);
15194 }
15195
15196 /* Create a new mapped symtab object.  */
15197
15198 static struct mapped_symtab *
15199 create_mapped_symtab (void)
15200 {
15201   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15202   symtab->n_elements = 0;
15203   symtab->size = 1024;
15204   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15205   return symtab;
15206 }
15207
15208 /* Destroy a mapped_symtab.  */
15209
15210 static void
15211 cleanup_mapped_symtab (void *p)
15212 {
15213   struct mapped_symtab *symtab = p;
15214   /* The contents of the array are freed when the other hash table is
15215      destroyed.  */
15216   xfree (symtab->data);
15217   xfree (symtab);
15218 }
15219
15220 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15221    the slot.  */
15222
15223 static struct symtab_index_entry **
15224 find_slot (struct mapped_symtab *symtab, const char *name)
15225 {
15226   offset_type index, step, hash = mapped_index_string_hash (name);
15227
15228   index = hash & (symtab->size - 1);
15229   step = ((hash * 17) & (symtab->size - 1)) | 1;
15230
15231   for (;;)
15232     {
15233       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15234         return &symtab->data[index];
15235       index = (index + step) & (symtab->size - 1);
15236     }
15237 }
15238
15239 /* Expand SYMTAB's hash table.  */
15240
15241 static void
15242 hash_expand (struct mapped_symtab *symtab)
15243 {
15244   offset_type old_size = symtab->size;
15245   offset_type i;
15246   struct symtab_index_entry **old_entries = symtab->data;
15247
15248   symtab->size *= 2;
15249   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15250
15251   for (i = 0; i < old_size; ++i)
15252     {
15253       if (old_entries[i])
15254         {
15255           struct symtab_index_entry **slot = find_slot (symtab,
15256                                                         old_entries[i]->name);
15257           *slot = old_entries[i];
15258         }
15259     }
15260
15261   xfree (old_entries);
15262 }
15263
15264 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15265    is the index of the CU in which the symbol appears.  */
15266
15267 static void
15268 add_index_entry (struct mapped_symtab *symtab, const char *name,
15269                  offset_type cu_index)
15270 {
15271   struct symtab_index_entry **slot;
15272
15273   ++symtab->n_elements;
15274   if (4 * symtab->n_elements / 3 >= symtab->size)
15275     hash_expand (symtab);
15276
15277   slot = find_slot (symtab, name);
15278   if (!*slot)
15279     {
15280       *slot = XNEW (struct symtab_index_entry);
15281       (*slot)->name = name;
15282       (*slot)->cu_indices = NULL;
15283     }
15284   /* Don't push an index twice.  Due to how we add entries we only
15285      have to check the last one.  */ 
15286   if (VEC_empty (offset_type, (*slot)->cu_indices)
15287       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15288     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15289 }
15290
15291 /* Add a vector of indices to the constant pool.  */
15292
15293 static offset_type
15294 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15295                       struct symtab_index_entry *entry)
15296 {
15297   void **slot;
15298
15299   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15300   if (!*slot)
15301     {
15302       offset_type len = VEC_length (offset_type, entry->cu_indices);
15303       offset_type val = MAYBE_SWAP (len);
15304       offset_type iter;
15305       int i;
15306
15307       *slot = entry;
15308       entry->index_offset = obstack_object_size (cpool);
15309
15310       obstack_grow (cpool, &val, sizeof (val));
15311       for (i = 0;
15312            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15313            ++i)
15314         {
15315           val = MAYBE_SWAP (iter);
15316           obstack_grow (cpool, &val, sizeof (val));
15317         }
15318     }
15319   else
15320     {
15321       struct symtab_index_entry *old_entry = *slot;
15322       entry->index_offset = old_entry->index_offset;
15323       entry = old_entry;
15324     }
15325   return entry->index_offset;
15326 }
15327
15328 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15329    constant pool entries going into the obstack CPOOL.  */
15330
15331 static void
15332 write_hash_table (struct mapped_symtab *symtab,
15333                   struct obstack *output, struct obstack *cpool)
15334 {
15335   offset_type i;
15336   htab_t symbol_hash_table;
15337   htab_t str_table;
15338
15339   symbol_hash_table = create_symbol_hash_table ();
15340   str_table = create_strtab ();
15341
15342   /* We add all the index vectors to the constant pool first, to
15343      ensure alignment is ok.  */
15344   for (i = 0; i < symtab->size; ++i)
15345     {
15346       if (symtab->data[i])
15347         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15348     }
15349
15350   /* Now write out the hash table.  */
15351   for (i = 0; i < symtab->size; ++i)
15352     {
15353       offset_type str_off, vec_off;
15354
15355       if (symtab->data[i])
15356         {
15357           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15358           vec_off = symtab->data[i]->index_offset;
15359         }
15360       else
15361         {
15362           /* While 0 is a valid constant pool index, it is not valid
15363              to have 0 for both offsets.  */
15364           str_off = 0;
15365           vec_off = 0;
15366         }
15367
15368       str_off = MAYBE_SWAP (str_off);
15369       vec_off = MAYBE_SWAP (vec_off);
15370
15371       obstack_grow (output, &str_off, sizeof (str_off));
15372       obstack_grow (output, &vec_off, sizeof (vec_off));
15373     }
15374
15375   htab_delete (str_table);
15376   htab_delete (symbol_hash_table);
15377 }
15378
15379 /* Struct to map psymtab to CU index in the index file.  */
15380 struct psymtab_cu_index_map
15381 {
15382   struct partial_symtab *psymtab;
15383   unsigned int cu_index;
15384 };
15385
15386 static hashval_t
15387 hash_psymtab_cu_index (const void *item)
15388 {
15389   const struct psymtab_cu_index_map *map = item;
15390
15391   return htab_hash_pointer (map->psymtab);
15392 }
15393
15394 static int
15395 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15396 {
15397   const struct psymtab_cu_index_map *lhs = item_lhs;
15398   const struct psymtab_cu_index_map *rhs = item_rhs;
15399
15400   return lhs->psymtab == rhs->psymtab;
15401 }
15402
15403 /* Helper struct for building the address table.  */
15404 struct addrmap_index_data
15405 {
15406   struct objfile *objfile;
15407   struct obstack *addr_obstack;
15408   htab_t cu_index_htab;
15409
15410   /* Non-zero if the previous_* fields are valid.
15411      We can't write an entry until we see the next entry (since it is only then
15412      that we know the end of the entry).  */
15413   int previous_valid;
15414   /* Index of the CU in the table of all CUs in the index file.  */
15415   unsigned int previous_cu_index;
15416   /* Start address of the CU.  */
15417   CORE_ADDR previous_cu_start;
15418 };
15419
15420 /* Write an address entry to OBSTACK.  */
15421
15422 static void
15423 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15424                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15425 {
15426   offset_type cu_index_to_write;
15427   char addr[8];
15428   CORE_ADDR baseaddr;
15429
15430   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15431
15432   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15433   obstack_grow (obstack, addr, 8);
15434   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15435   obstack_grow (obstack, addr, 8);
15436   cu_index_to_write = MAYBE_SWAP (cu_index);
15437   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15438 }
15439
15440 /* Worker function for traversing an addrmap to build the address table.  */
15441
15442 static int
15443 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15444 {
15445   struct addrmap_index_data *data = datap;
15446   struct partial_symtab *pst = obj;
15447   offset_type cu_index;
15448   void **slot;
15449
15450   if (data->previous_valid)
15451     add_address_entry (data->objfile, data->addr_obstack,
15452                        data->previous_cu_start, start_addr,
15453                        data->previous_cu_index);
15454
15455   data->previous_cu_start = start_addr;
15456   if (pst != NULL)
15457     {
15458       struct psymtab_cu_index_map find_map, *map;
15459       find_map.psymtab = pst;
15460       map = htab_find (data->cu_index_htab, &find_map);
15461       gdb_assert (map != NULL);
15462       data->previous_cu_index = map->cu_index;
15463       data->previous_valid = 1;
15464     }
15465   else
15466       data->previous_valid = 0;
15467
15468   return 0;
15469 }
15470
15471 /* Write OBJFILE's address map to OBSTACK.
15472    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15473    in the index file.  */
15474
15475 static void
15476 write_address_map (struct objfile *objfile, struct obstack *obstack,
15477                    htab_t cu_index_htab)
15478 {
15479   struct addrmap_index_data addrmap_index_data;
15480
15481   /* When writing the address table, we have to cope with the fact that
15482      the addrmap iterator only provides the start of a region; we have to
15483      wait until the next invocation to get the start of the next region.  */
15484
15485   addrmap_index_data.objfile = objfile;
15486   addrmap_index_data.addr_obstack = obstack;
15487   addrmap_index_data.cu_index_htab = cu_index_htab;
15488   addrmap_index_data.previous_valid = 0;
15489
15490   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15491                    &addrmap_index_data);
15492
15493   /* It's highly unlikely the last entry (end address = 0xff...ff)
15494      is valid, but we should still handle it.
15495      The end address is recorded as the start of the next region, but that
15496      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15497      anyway.  */
15498   if (addrmap_index_data.previous_valid)
15499     add_address_entry (objfile, obstack,
15500                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15501                        addrmap_index_data.previous_cu_index);
15502 }
15503
15504 /* Add a list of partial symbols to SYMTAB.  */
15505
15506 static void
15507 write_psymbols (struct mapped_symtab *symtab,
15508                 htab_t psyms_seen,
15509                 struct partial_symbol **psymp,
15510                 int count,
15511                 offset_type cu_index,
15512                 int is_static)
15513 {
15514   for (; count-- > 0; ++psymp)
15515     {
15516       void **slot, *lookup;
15517
15518       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15519         error (_("Ada is not currently supported by the index"));
15520
15521       /* We only want to add a given psymbol once.  However, we also
15522          want to account for whether it is global or static.  So, we
15523          may add it twice, using slightly different values.  */
15524       if (is_static)
15525         {
15526           uintptr_t val = 1 | (uintptr_t) *psymp;
15527
15528           lookup = (void *) val;
15529         }
15530       else
15531         lookup = *psymp;
15532
15533       /* Only add a given psymbol once.  */
15534       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15535       if (!*slot)
15536         {
15537           *slot = lookup;
15538           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15539         }
15540     }
15541 }
15542
15543 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15544    exception if there is an error.  */
15545
15546 static void
15547 write_obstack (FILE *file, struct obstack *obstack)
15548 {
15549   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15550               file)
15551       != obstack_object_size (obstack))
15552     error (_("couldn't data write to file"));
15553 }
15554
15555 /* Unlink a file if the argument is not NULL.  */
15556
15557 static void
15558 unlink_if_set (void *p)
15559 {
15560   char **filename = p;
15561   if (*filename)
15562     unlink (*filename);
15563 }
15564
15565 /* A helper struct used when iterating over debug_types.  */
15566 struct signatured_type_index_data
15567 {
15568   struct objfile *objfile;
15569   struct mapped_symtab *symtab;
15570   struct obstack *types_list;
15571   htab_t psyms_seen;
15572   int cu_index;
15573 };
15574
15575 /* A helper function that writes a single signatured_type to an
15576    obstack.  */
15577
15578 static int
15579 write_one_signatured_type (void **slot, void *d)
15580 {
15581   struct signatured_type_index_data *info = d;
15582   struct signatured_type *entry = (struct signatured_type *) *slot;
15583   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15584   struct partial_symtab *psymtab = per_cu->v.psymtab;
15585   gdb_byte val[8];
15586
15587   write_psymbols (info->symtab,
15588                   info->psyms_seen,
15589                   info->objfile->global_psymbols.list
15590                   + psymtab->globals_offset,
15591                   psymtab->n_global_syms, info->cu_index,
15592                   0);
15593   write_psymbols (info->symtab,
15594                   info->psyms_seen,
15595                   info->objfile->static_psymbols.list
15596                   + psymtab->statics_offset,
15597                   psymtab->n_static_syms, info->cu_index,
15598                   1);
15599
15600   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15601   obstack_grow (info->types_list, val, 8);
15602   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15603   obstack_grow (info->types_list, val, 8);
15604   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15605   obstack_grow (info->types_list, val, 8);
15606
15607   ++info->cu_index;
15608
15609   return 1;
15610 }
15611
15612 /* A cleanup function for an htab_t.  */
15613
15614 static void
15615 cleanup_htab (void *arg)
15616 {
15617   htab_delete (arg);
15618 }
15619
15620 /* Create an index file for OBJFILE in the directory DIR.  */
15621
15622 static void
15623 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15624 {
15625   struct cleanup *cleanup;
15626   char *filename, *cleanup_filename;
15627   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15628   struct obstack cu_list, types_cu_list;
15629   int i;
15630   FILE *out_file;
15631   struct mapped_symtab *symtab;
15632   offset_type val, size_of_contents, total_len;
15633   struct stat st;
15634   char buf[8];
15635   htab_t psyms_seen;
15636   htab_t cu_index_htab;
15637   struct psymtab_cu_index_map *psymtab_cu_index_map;
15638
15639   if (!objfile->psymtabs)
15640     return;
15641   if (dwarf2_per_objfile->using_index)
15642     error (_("Cannot use an index to create the index"));
15643
15644   if (stat (objfile->name, &st) < 0)
15645     perror_with_name (objfile->name);
15646
15647   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15648                      INDEX_SUFFIX, (char *) NULL);
15649   cleanup = make_cleanup (xfree, filename);
15650
15651   out_file = fopen (filename, "wb");
15652   if (!out_file)
15653     error (_("Can't open `%s' for writing"), filename);
15654
15655   cleanup_filename = filename;
15656   make_cleanup (unlink_if_set, &cleanup_filename);
15657
15658   symtab = create_mapped_symtab ();
15659   make_cleanup (cleanup_mapped_symtab, symtab);
15660
15661   obstack_init (&addr_obstack);
15662   make_cleanup_obstack_free (&addr_obstack);
15663
15664   obstack_init (&cu_list);
15665   make_cleanup_obstack_free (&cu_list);
15666
15667   obstack_init (&types_cu_list);
15668   make_cleanup_obstack_free (&types_cu_list);
15669
15670   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15671                                   NULL, xcalloc, xfree);
15672   make_cleanup (cleanup_htab, psyms_seen);
15673
15674   /* While we're scanning CU's create a table that maps a psymtab pointer
15675      (which is what addrmap records) to its index (which is what is recorded
15676      in the index file).  This will later be needed to write the address
15677      table.  */
15678   cu_index_htab = htab_create_alloc (100,
15679                                      hash_psymtab_cu_index,
15680                                      eq_psymtab_cu_index,
15681                                      NULL, xcalloc, xfree);
15682   make_cleanup (cleanup_htab, cu_index_htab);
15683   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15684     xmalloc (sizeof (struct psymtab_cu_index_map)
15685              * dwarf2_per_objfile->n_comp_units);
15686   make_cleanup (xfree, psymtab_cu_index_map);
15687
15688   /* The CU list is already sorted, so we don't need to do additional
15689      work here.  Also, the debug_types entries do not appear in
15690      all_comp_units, but only in their own hash table.  */
15691   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15692     {
15693       struct dwarf2_per_cu_data *per_cu
15694         = dwarf2_per_objfile->all_comp_units[i];
15695       struct partial_symtab *psymtab = per_cu->v.psymtab;
15696       gdb_byte val[8];
15697       struct psymtab_cu_index_map *map;
15698       void **slot;
15699
15700       write_psymbols (symtab,
15701                       psyms_seen,
15702                       objfile->global_psymbols.list + psymtab->globals_offset,
15703                       psymtab->n_global_syms, i,
15704                       0);
15705       write_psymbols (symtab,
15706                       psyms_seen,
15707                       objfile->static_psymbols.list + psymtab->statics_offset,
15708                       psymtab->n_static_syms, i,
15709                       1);
15710
15711       map = &psymtab_cu_index_map[i];
15712       map->psymtab = psymtab;
15713       map->cu_index = i;
15714       slot = htab_find_slot (cu_index_htab, map, INSERT);
15715       gdb_assert (slot != NULL);
15716       gdb_assert (*slot == NULL);
15717       *slot = map;
15718
15719       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15720       obstack_grow (&cu_list, val, 8);
15721       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15722       obstack_grow (&cu_list, val, 8);
15723     }
15724
15725   /* Dump the address map.  */
15726   write_address_map (objfile, &addr_obstack, cu_index_htab);
15727
15728   /* Write out the .debug_type entries, if any.  */
15729   if (dwarf2_per_objfile->signatured_types)
15730     {
15731       struct signatured_type_index_data sig_data;
15732
15733       sig_data.objfile = objfile;
15734       sig_data.symtab = symtab;
15735       sig_data.types_list = &types_cu_list;
15736       sig_data.psyms_seen = psyms_seen;
15737       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15738       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15739                               write_one_signatured_type, &sig_data);
15740     }
15741
15742   obstack_init (&constant_pool);
15743   make_cleanup_obstack_free (&constant_pool);
15744   obstack_init (&symtab_obstack);
15745   make_cleanup_obstack_free (&symtab_obstack);
15746   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15747
15748   obstack_init (&contents);
15749   make_cleanup_obstack_free (&contents);
15750   size_of_contents = 6 * sizeof (offset_type);
15751   total_len = size_of_contents;
15752
15753   /* The version number.  */
15754   val = MAYBE_SWAP (4);
15755   obstack_grow (&contents, &val, sizeof (val));
15756
15757   /* The offset of the CU list from the start of the file.  */
15758   val = MAYBE_SWAP (total_len);
15759   obstack_grow (&contents, &val, sizeof (val));
15760   total_len += obstack_object_size (&cu_list);
15761
15762   /* The offset of the types CU list from the start of the file.  */
15763   val = MAYBE_SWAP (total_len);
15764   obstack_grow (&contents, &val, sizeof (val));
15765   total_len += obstack_object_size (&types_cu_list);
15766
15767   /* The offset of the address table from the start of the file.  */
15768   val = MAYBE_SWAP (total_len);
15769   obstack_grow (&contents, &val, sizeof (val));
15770   total_len += obstack_object_size (&addr_obstack);
15771
15772   /* The offset of the symbol table from the start of the file.  */
15773   val = MAYBE_SWAP (total_len);
15774   obstack_grow (&contents, &val, sizeof (val));
15775   total_len += obstack_object_size (&symtab_obstack);
15776
15777   /* The offset of the constant pool from the start of the file.  */
15778   val = MAYBE_SWAP (total_len);
15779   obstack_grow (&contents, &val, sizeof (val));
15780   total_len += obstack_object_size (&constant_pool);
15781
15782   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15783
15784   write_obstack (out_file, &contents);
15785   write_obstack (out_file, &cu_list);
15786   write_obstack (out_file, &types_cu_list);
15787   write_obstack (out_file, &addr_obstack);
15788   write_obstack (out_file, &symtab_obstack);
15789   write_obstack (out_file, &constant_pool);
15790
15791   fclose (out_file);
15792
15793   /* We want to keep the file, so we set cleanup_filename to NULL
15794      here.  See unlink_if_set.  */
15795   cleanup_filename = NULL;
15796
15797   do_cleanups (cleanup);
15798 }
15799
15800 /* The mapped index file format is designed to be directly mmap()able
15801    on any architecture.  In most cases, a datum is represented using a
15802    little-endian 32-bit integer value, called an offset_type.  Big
15803    endian machines must byte-swap the values before using them.
15804    Exceptions to this rule are noted.  The data is laid out such that
15805    alignment is always respected.
15806
15807    A mapped index consists of several sections.
15808
15809    1. The file header.  This is a sequence of values, of offset_type
15810    unless otherwise noted:
15811
15812    [0] The version number, currently 4.  Versions 1, 2 and 3 are
15813    obsolete.
15814    [1] The offset, from the start of the file, of the CU list.
15815    [2] The offset, from the start of the file, of the types CU list.
15816    Note that this section can be empty, in which case this offset will
15817    be equal to the next offset.
15818    [3] The offset, from the start of the file, of the address section.
15819    [4] The offset, from the start of the file, of the symbol table.
15820    [5] The offset, from the start of the file, of the constant pool.
15821
15822    2. The CU list.  This is a sequence of pairs of 64-bit
15823    little-endian values, sorted by the CU offset.  The first element
15824    in each pair is the offset of a CU in the .debug_info section.  The
15825    second element in each pair is the length of that CU.  References
15826    to a CU elsewhere in the map are done using a CU index, which is
15827    just the 0-based index into this table.  Note that if there are
15828    type CUs, then conceptually CUs and type CUs form a single list for
15829    the purposes of CU indices.
15830
15831    3. The types CU list.  This is a sequence of triplets of 64-bit
15832    little-endian values.  In a triplet, the first value is the CU
15833    offset, the second value is the type offset in the CU, and the
15834    third value is the type signature.  The types CU list is not
15835    sorted.
15836
15837    4. The address section.  The address section consists of a sequence
15838    of address entries.  Each address entry has three elements.
15839    [0] The low address.  This is a 64-bit little-endian value.
15840    [1] The high address.  This is a 64-bit little-endian value.
15841        Like DW_AT_high_pc, the value is one byte beyond the end.
15842    [2] The CU index.  This is an offset_type value.
15843
15844    5. The symbol table.  This is a hash table.  The size of the hash
15845    table is always a power of 2.  The initial hash and the step are
15846    currently defined by the `find_slot' function.
15847
15848    Each slot in the hash table consists of a pair of offset_type
15849    values.  The first value is the offset of the symbol's name in the
15850    constant pool.  The second value is the offset of the CU vector in
15851    the constant pool.
15852
15853    If both values are 0, then this slot in the hash table is empty.
15854    This is ok because while 0 is a valid constant pool index, it
15855    cannot be a valid index for both a string and a CU vector.
15856
15857    A string in the constant pool is stored as a \0-terminated string,
15858    as you'd expect.
15859
15860    A CU vector in the constant pool is a sequence of offset_type
15861    values.  The first value is the number of CU indices in the vector.
15862    Each subsequent value is the index of a CU in the CU list.  This
15863    element in the hash table is used to indicate which CUs define the
15864    symbol.
15865
15866    6. The constant pool.  This is simply a bunch of bytes.  It is
15867    organized so that alignment is correct: CU vectors are stored
15868    first, followed by strings.  */
15869
15870 static void
15871 save_gdb_index_command (char *arg, int from_tty)
15872 {
15873   struct objfile *objfile;
15874
15875   if (!arg || !*arg)
15876     error (_("usage: save gdb-index DIRECTORY"));
15877
15878   ALL_OBJFILES (objfile)
15879   {
15880     struct stat st;
15881
15882     /* If the objfile does not correspond to an actual file, skip it.  */
15883     if (stat (objfile->name, &st) < 0)
15884       continue;
15885
15886     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15887     if (dwarf2_per_objfile)
15888       {
15889         volatile struct gdb_exception except;
15890
15891         TRY_CATCH (except, RETURN_MASK_ERROR)
15892           {
15893             write_psymtabs_to_index (objfile, arg);
15894           }
15895         if (except.reason < 0)
15896           exception_fprintf (gdb_stderr, except,
15897                              _("Error while writing index for `%s': "),
15898                              objfile->name);
15899       }
15900   }
15901 }
15902
15903 \f
15904
15905 int dwarf2_always_disassemble;
15906
15907 static void
15908 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15909                                 struct cmd_list_element *c, const char *value)
15910 {
15911   fprintf_filtered (file,
15912                     _("Whether to always disassemble "
15913                       "DWARF expressions is %s.\n"),
15914                     value);
15915 }
15916
15917 void _initialize_dwarf2_read (void);
15918
15919 void
15920 _initialize_dwarf2_read (void)
15921 {
15922   struct cmd_list_element *c;
15923
15924   dwarf2_objfile_data_key
15925     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15926
15927   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15928 Set DWARF 2 specific variables.\n\
15929 Configure DWARF 2 variables such as the cache size"),
15930                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15931                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15932
15933   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15934 Show DWARF 2 specific variables\n\
15935 Show DWARF 2 variables such as the cache size"),
15936                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15937                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15938
15939   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15940                             &dwarf2_max_cache_age, _("\
15941 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15942 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15943 A higher limit means that cached compilation units will be stored\n\
15944 in memory longer, and more total memory will be used.  Zero disables\n\
15945 caching, which can slow down startup."),
15946                             NULL,
15947                             show_dwarf2_max_cache_age,
15948                             &set_dwarf2_cmdlist,
15949                             &show_dwarf2_cmdlist);
15950
15951   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15952                            &dwarf2_always_disassemble, _("\
15953 Set whether `info address' always disassembles DWARF expressions."), _("\
15954 Show whether `info address' always disassembles DWARF expressions."), _("\
15955 When enabled, DWARF expressions are always printed in an assembly-like\n\
15956 syntax.  When disabled, expressions will be printed in a more\n\
15957 conversational style, when possible."),
15958                            NULL,
15959                            show_dwarf2_always_disassemble,
15960                            &set_dwarf2_cmdlist,
15961                            &show_dwarf2_cmdlist);
15962
15963   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15964 Set debugging of the dwarf2 DIE reader."), _("\
15965 Show debugging of the dwarf2 DIE reader."), _("\
15966 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15967 The value is the maximum depth to print."),
15968                             NULL,
15969                             NULL,
15970                             &setdebuglist, &showdebuglist);
15971
15972   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15973                _("\
15974 Save a gdb-index file.\n\
15975 Usage: save gdb-index DIRECTORY"),
15976                &save_cmdlist);
15977   set_cmd_completer (c, filename_completer);
15978 }