]> Git Repo - binutils.git/blob - gdb/dwarf2read.c
* dwarf2-frame.c (decode_frame_entry_1): Handle CIE version 4.
[binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck ([email protected]), 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
55 #include <fcntl.h>
56 #include "gdb_string.h"
57 #include "gdb_assert.h"
58 #include <sys/types.h>
59 #ifdef HAVE_ZLIB_H
60 #include <zlib.h>
61 #endif
62 #ifdef HAVE_MMAP
63 #include <sys/mman.h>
64 #ifndef MAP_FAILED
65 #define MAP_FAILED ((void *) -1)
66 #endif
67 #endif
68
69 #if 0
70 /* .debug_info header for a compilation unit
71    Because of alignment constraints, this structure has padding and cannot
72    be mapped directly onto the beginning of the .debug_info section.  */
73 typedef struct comp_unit_header
74   {
75     unsigned int length;        /* length of the .debug_info
76                                    contribution */
77     unsigned short version;     /* version number -- 2 for DWARF
78                                    version 2 */
79     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
80     unsigned char addr_size;    /* byte size of an address -- 4 */
81   }
82 _COMP_UNIT_HEADER;
83 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
84 #endif
85
86 /* .debug_line statement program prologue
87    Because of alignment constraints, this structure has padding and cannot
88    be mapped directly onto the beginning of the .debug_info section.  */
89 typedef struct statement_prologue
90   {
91     unsigned int total_length;  /* byte length of the statement
92                                    information */
93     unsigned short version;     /* version number -- 2 for DWARF
94                                    version 2 */
95     unsigned int prologue_length;       /* # bytes between prologue &
96                                            stmt program */
97     unsigned char minimum_instruction_length;   /* byte size of
98                                                    smallest instr */
99     unsigned char default_is_stmt;      /* initial value of is_stmt
100                                            register */
101     char line_base;
102     unsigned char line_range;
103     unsigned char opcode_base;  /* number assigned to first special
104                                    opcode */
105     unsigned char *standard_opcode_lengths;
106   }
107 _STATEMENT_PROLOGUE;
108
109 /* When non-zero, dump DIEs after they are read in.  */
110 static int dwarf2_die_debug = 0;
111
112 static int pagesize;
113
114 /* When set, the file that we're processing is known to have debugging
115    info for C++ namespaces.  GCC 3.3.x did not produce this information,
116    but later versions do.  */
117
118 static int processing_has_namespace_info;
119
120 static const struct objfile_data *dwarf2_objfile_data_key;
121
122 struct dwarf2_section_info
123 {
124   asection *asection;
125   gdb_byte *buffer;
126   bfd_size_type size;
127   int was_mmapped;
128   /* True if we have tried to read this section.  */
129   int readin;
130 };
131
132 struct dwarf2_per_objfile
133 {
134   struct dwarf2_section_info info;
135   struct dwarf2_section_info abbrev;
136   struct dwarf2_section_info line;
137   struct dwarf2_section_info loc;
138   struct dwarf2_section_info macinfo;
139   struct dwarf2_section_info str;
140   struct dwarf2_section_info ranges;
141   struct dwarf2_section_info types;
142   struct dwarf2_section_info frame;
143   struct dwarf2_section_info eh_frame;
144
145   /* Back link.  */
146   struct objfile *objfile;
147
148   /* A list of all the compilation units.  This is used to locate
149      the target compilation unit of a particular reference.  */
150   struct dwarf2_per_cu_data **all_comp_units;
151
152   /* The number of compilation units in ALL_COMP_UNITS.  */
153   int n_comp_units;
154
155   /* A chain of compilation units that are currently read in, so that
156      they can be freed later.  */
157   struct dwarf2_per_cu_data *read_in_chain;
158
159   /* A table mapping .debug_types signatures to its signatured_type entry.
160      This is NULL if the .debug_types section hasn't been read in yet.  */
161   htab_t signatured_types;
162
163   /* A flag indicating wether this objfile has a section loaded at a
164      VMA of 0.  */
165   int has_section_at_zero;
166 };
167
168 static struct dwarf2_per_objfile *dwarf2_per_objfile;
169
170 /* names of the debugging sections */
171
172 /* Note that if the debugging section has been compressed, it might
173    have a name like .zdebug_info.  */
174
175 #define INFO_SECTION     "debug_info"
176 #define ABBREV_SECTION   "debug_abbrev"
177 #define LINE_SECTION     "debug_line"
178 #define LOC_SECTION      "debug_loc"
179 #define MACINFO_SECTION  "debug_macinfo"
180 #define STR_SECTION      "debug_str"
181 #define RANGES_SECTION   "debug_ranges"
182 #define TYPES_SECTION    "debug_types"
183 #define FRAME_SECTION    "debug_frame"
184 #define EH_FRAME_SECTION "eh_frame"
185
186 /* local data types */
187
188 /* We hold several abbreviation tables in memory at the same time. */
189 #ifndef ABBREV_HASH_SIZE
190 #define ABBREV_HASH_SIZE 121
191 #endif
192
193 /* The data in a compilation unit header, after target2host
194    translation, looks like this.  */
195 struct comp_unit_head
196 {
197   unsigned int length;
198   short version;
199   unsigned char addr_size;
200   unsigned char signed_addr_p;
201   unsigned int abbrev_offset;
202
203   /* Size of file offsets; either 4 or 8.  */
204   unsigned int offset_size;
205
206   /* Size of the length field; either 4 or 12.  */
207   unsigned int initial_length_size;
208
209   /* Offset to the first byte of this compilation unit header in the
210      .debug_info section, for resolving relative reference dies.  */
211   unsigned int offset;
212
213   /* Offset to first die in this cu from the start of the cu.
214      This will be the first byte following the compilation unit header.  */
215   unsigned int first_die_offset;
216 };
217
218 /* Internal state when decoding a particular compilation unit.  */
219 struct dwarf2_cu
220 {
221   /* The objfile containing this compilation unit.  */
222   struct objfile *objfile;
223
224   /* The header of the compilation unit.  */
225   struct comp_unit_head header;
226
227   /* Base address of this compilation unit.  */
228   CORE_ADDR base_address;
229
230   /* Non-zero if base_address has been set.  */
231   int base_known;
232
233   struct function_range *first_fn, *last_fn, *cached_fn;
234
235   /* The language we are debugging.  */
236   enum language language;
237   const struct language_defn *language_defn;
238
239   const char *producer;
240
241   /* The generic symbol table building routines have separate lists for
242      file scope symbols and all all other scopes (local scopes).  So
243      we need to select the right one to pass to add_symbol_to_list().
244      We do it by keeping a pointer to the correct list in list_in_scope.
245
246      FIXME: The original dwarf code just treated the file scope as the
247      first local scope, and all other local scopes as nested local
248      scopes, and worked fine.  Check to see if we really need to
249      distinguish these in buildsym.c.  */
250   struct pending **list_in_scope;
251
252   /* DWARF abbreviation table associated with this compilation unit.  */
253   struct abbrev_info **dwarf2_abbrevs;
254
255   /* Storage for the abbrev table.  */
256   struct obstack abbrev_obstack;
257
258   /* Hash table holding all the loaded partial DIEs.  */
259   htab_t partial_dies;
260
261   /* Storage for things with the same lifetime as this read-in compilation
262      unit, including partial DIEs.  */
263   struct obstack comp_unit_obstack;
264
265   /* When multiple dwarf2_cu structures are living in memory, this field
266      chains them all together, so that they can be released efficiently.
267      We will probably also want a generation counter so that most-recently-used
268      compilation units are cached...  */
269   struct dwarf2_per_cu_data *read_in_chain;
270
271   /* Backchain to our per_cu entry if the tree has been built.  */
272   struct dwarf2_per_cu_data *per_cu;
273
274   /* Pointer to the die -> type map.  Although it is stored
275      permanently in per_cu, we copy it here to avoid double
276      indirection.  */
277   htab_t type_hash;
278
279   /* How many compilation units ago was this CU last referenced?  */
280   int last_used;
281
282   /* A hash table of die offsets for following references.  */
283   htab_t die_hash;
284
285   /* Full DIEs if read in.  */
286   struct die_info *dies;
287
288   /* A set of pointers to dwarf2_per_cu_data objects for compilation
289      units referenced by this one.  Only set during full symbol processing;
290      partial symbol tables do not have dependencies.  */
291   htab_t dependencies;
292
293   /* Header data from the line table, during full symbol processing.  */
294   struct line_header *line_header;
295
296   /* Mark used when releasing cached dies.  */
297   unsigned int mark : 1;
298
299   /* This flag will be set if this compilation unit might include
300      inter-compilation-unit references.  */
301   unsigned int has_form_ref_addr : 1;
302
303   /* This flag will be set if this compilation unit includes any
304      DW_TAG_namespace DIEs.  If we know that there are explicit
305      DIEs for namespaces, we don't need to try to infer them
306      from mangled names.  */
307   unsigned int has_namespace_info : 1;
308 };
309
310 /* Persistent data held for a compilation unit, even when not
311    processing it.  We put a pointer to this structure in the
312    read_symtab_private field of the psymtab.  If we encounter
313    inter-compilation-unit references, we also maintain a sorted
314    list of all compilation units.  */
315
316 struct dwarf2_per_cu_data
317 {
318   /* The start offset and length of this compilation unit.  2**29-1
319      bytes should suffice to store the length of any compilation unit
320      - if it doesn't, GDB will fall over anyway.
321      NOTE: Unlike comp_unit_head.length, this length includes
322      initial_length_size.  */
323   unsigned int offset;
324   unsigned int length : 29;
325
326   /* Flag indicating this compilation unit will be read in before
327      any of the current compilation units are processed.  */
328   unsigned int queued : 1;
329
330   /* This flag will be set if we need to load absolutely all DIEs
331      for this compilation unit, instead of just the ones we think
332      are interesting.  It gets set if we look for a DIE in the
333      hash table and don't find it.  */
334   unsigned int load_all_dies : 1;
335
336   /* Non-zero if this CU is from .debug_types.
337      Otherwise it's from .debug_info.  */
338   unsigned int from_debug_types : 1;
339
340   /* Set iff currently read in.  */
341   struct dwarf2_cu *cu;
342
343   /* If full symbols for this CU have been read in, then this field
344      holds a map of DIE offsets to types.  It isn't always possible
345      to reconstruct this information later, so we have to preserve
346      it.  */
347   htab_t type_hash;
348
349   /* The partial symbol table associated with this compilation unit,
350      or NULL for partial units (which do not have an associated
351      symtab).  */
352   struct partial_symtab *psymtab;
353 };
354
355 /* Entry in the signatured_types hash table.  */
356
357 struct signatured_type
358 {
359   ULONGEST signature;
360
361   /* Offset in .debug_types of the TU (type_unit) for this type.  */
362   unsigned int offset;
363
364   /* Offset in .debug_types of the type defined by this TU.  */
365   unsigned int type_offset;
366
367   /* The CU(/TU) of this type.  */
368   struct dwarf2_per_cu_data per_cu;
369 };
370
371 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
372    which are used for both .debug_info and .debug_types dies.
373    All parameters here are unchanging for the life of the call.
374    This struct exists to abstract away the constant parameters of
375    die reading.  */
376
377 struct die_reader_specs
378 {
379   /* The bfd of this objfile.  */
380   bfd* abfd;
381
382   /* The CU of the DIE we are parsing.  */
383   struct dwarf2_cu *cu;
384
385   /* Pointer to start of section buffer.
386      This is either the start of .debug_info or .debug_types.  */
387   const gdb_byte *buffer;
388 };
389
390 /* The line number information for a compilation unit (found in the
391    .debug_line section) begins with a "statement program header",
392    which contains the following information.  */
393 struct line_header
394 {
395   unsigned int total_length;
396   unsigned short version;
397   unsigned int header_length;
398   unsigned char minimum_instruction_length;
399   unsigned char maximum_ops_per_instruction;
400   unsigned char default_is_stmt;
401   int line_base;
402   unsigned char line_range;
403   unsigned char opcode_base;
404
405   /* standard_opcode_lengths[i] is the number of operands for the
406      standard opcode whose value is i.  This means that
407      standard_opcode_lengths[0] is unused, and the last meaningful
408      element is standard_opcode_lengths[opcode_base - 1].  */
409   unsigned char *standard_opcode_lengths;
410
411   /* The include_directories table.  NOTE!  These strings are not
412      allocated with xmalloc; instead, they are pointers into
413      debug_line_buffer.  If you try to free them, `free' will get
414      indigestion.  */
415   unsigned int num_include_dirs, include_dirs_size;
416   char **include_dirs;
417
418   /* The file_names table.  NOTE!  These strings are not allocated
419      with xmalloc; instead, they are pointers into debug_line_buffer.
420      Don't try to free them directly.  */
421   unsigned int num_file_names, file_names_size;
422   struct file_entry
423   {
424     char *name;
425     unsigned int dir_index;
426     unsigned int mod_time;
427     unsigned int length;
428     int included_p; /* Non-zero if referenced by the Line Number Program.  */
429     struct symtab *symtab; /* The associated symbol table, if any.  */
430   } *file_names;
431
432   /* The start and end of the statement program following this
433      header.  These point into dwarf2_per_objfile->line_buffer.  */
434   gdb_byte *statement_program_start, *statement_program_end;
435 };
436
437 /* When we construct a partial symbol table entry we only
438    need this much information. */
439 struct partial_die_info
440   {
441     /* Offset of this DIE.  */
442     unsigned int offset;
443
444     /* DWARF-2 tag for this DIE.  */
445     ENUM_BITFIELD(dwarf_tag) tag : 16;
446
447     /* Assorted flags describing the data found in this DIE.  */
448     unsigned int has_children : 1;
449     unsigned int is_external : 1;
450     unsigned int is_declaration : 1;
451     unsigned int has_type : 1;
452     unsigned int has_specification : 1;
453     unsigned int has_pc_info : 1;
454
455     /* Flag set if the SCOPE field of this structure has been
456        computed.  */
457     unsigned int scope_set : 1;
458
459     /* Flag set if the DIE has a byte_size attribute.  */
460     unsigned int has_byte_size : 1;
461
462     /* The name of this DIE.  Normally the value of DW_AT_name, but
463        sometimes a default name for unnamed DIEs.  */
464     char *name;
465
466     /* The scope to prepend to our children.  This is generally
467        allocated on the comp_unit_obstack, so will disappear
468        when this compilation unit leaves the cache.  */
469     char *scope;
470
471     /* The location description associated with this DIE, if any.  */
472     struct dwarf_block *locdesc;
473
474     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
475     CORE_ADDR lowpc;
476     CORE_ADDR highpc;
477
478     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
479        DW_AT_sibling, if any.  */
480     gdb_byte *sibling;
481
482     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
483        DW_AT_specification (or DW_AT_abstract_origin or
484        DW_AT_extension).  */
485     unsigned int spec_offset;
486
487     /* Pointers to this DIE's parent, first child, and next sibling,
488        if any.  */
489     struct partial_die_info *die_parent, *die_child, *die_sibling;
490   };
491
492 /* This data structure holds the information of an abbrev. */
493 struct abbrev_info
494   {
495     unsigned int number;        /* number identifying abbrev */
496     enum dwarf_tag tag;         /* dwarf tag */
497     unsigned short has_children;                /* boolean */
498     unsigned short num_attrs;   /* number of attributes */
499     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
500     struct abbrev_info *next;   /* next in chain */
501   };
502
503 struct attr_abbrev
504   {
505     ENUM_BITFIELD(dwarf_attribute) name : 16;
506     ENUM_BITFIELD(dwarf_form) form : 16;
507   };
508
509 /* Attributes have a name and a value */
510 struct attribute
511   {
512     ENUM_BITFIELD(dwarf_attribute) name : 16;
513     ENUM_BITFIELD(dwarf_form) form : 15;
514
515     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
516        field should be in u.str (existing only for DW_STRING) but it is kept
517        here for better struct attribute alignment.  */
518     unsigned int string_is_canonical : 1;
519
520     union
521       {
522         char *str;
523         struct dwarf_block *blk;
524         ULONGEST unsnd;
525         LONGEST snd;
526         CORE_ADDR addr;
527         struct signatured_type *signatured_type;
528       }
529     u;
530   };
531
532 /* This data structure holds a complete die structure. */
533 struct die_info
534   {
535     /* DWARF-2 tag for this DIE.  */
536     ENUM_BITFIELD(dwarf_tag) tag : 16;
537
538     /* Number of attributes */
539     unsigned short num_attrs;
540
541     /* Abbrev number */
542     unsigned int abbrev;
543
544     /* Offset in .debug_info or .debug_types section.  */
545     unsigned int offset;
546
547     /* The dies in a compilation unit form an n-ary tree.  PARENT
548        points to this die's parent; CHILD points to the first child of
549        this node; and all the children of a given node are chained
550        together via their SIBLING fields, terminated by a die whose
551        tag is zero.  */
552     struct die_info *child;     /* Its first child, if any.  */
553     struct die_info *sibling;   /* Its next sibling, if any.  */
554     struct die_info *parent;    /* Its parent, if any.  */
555
556     /* An array of attributes, with NUM_ATTRS elements.  There may be
557        zero, but it's not common and zero-sized arrays are not
558        sufficiently portable C.  */
559     struct attribute attrs[1];
560   };
561
562 struct function_range
563 {
564   const char *name;
565   CORE_ADDR lowpc, highpc;
566   int seen_line;
567   struct function_range *next;
568 };
569
570 /* Get at parts of an attribute structure */
571
572 #define DW_STRING(attr)    ((attr)->u.str)
573 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
574 #define DW_UNSND(attr)     ((attr)->u.unsnd)
575 #define DW_BLOCK(attr)     ((attr)->u.blk)
576 #define DW_SND(attr)       ((attr)->u.snd)
577 #define DW_ADDR(attr)      ((attr)->u.addr)
578 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
579
580 /* Blocks are a bunch of untyped bytes. */
581 struct dwarf_block
582   {
583     unsigned int size;
584     gdb_byte *data;
585   };
586
587 #ifndef ATTR_ALLOC_CHUNK
588 #define ATTR_ALLOC_CHUNK 4
589 #endif
590
591 /* Allocate fields for structs, unions and enums in this size.  */
592 #ifndef DW_FIELD_ALLOC_CHUNK
593 #define DW_FIELD_ALLOC_CHUNK 4
594 #endif
595
596 /* A zeroed version of a partial die for initialization purposes.  */
597 static struct partial_die_info zeroed_partial_die;
598
599 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
600    but this would require a corresponding change in unpack_field_as_long
601    and friends.  */
602 static int bits_per_byte = 8;
603
604 /* The routines that read and process dies for a C struct or C++ class
605    pass lists of data member fields and lists of member function fields
606    in an instance of a field_info structure, as defined below.  */
607 struct field_info
608   {
609     /* List of data member and baseclasses fields. */
610     struct nextfield
611       {
612         struct nextfield *next;
613         int accessibility;
614         int virtuality;
615         struct field field;
616       }
617      *fields, *baseclasses;
618
619     /* Number of fields (including baseclasses).  */
620     int nfields;
621
622     /* Number of baseclasses.  */
623     int nbaseclasses;
624
625     /* Set if the accesibility of one of the fields is not public.  */
626     int non_public_fields;
627
628     /* Member function fields array, entries are allocated in the order they
629        are encountered in the object file.  */
630     struct nextfnfield
631       {
632         struct nextfnfield *next;
633         struct fn_field fnfield;
634       }
635      *fnfields;
636
637     /* Member function fieldlist array, contains name of possibly overloaded
638        member function, number of overloaded member functions and a pointer
639        to the head of the member function field chain.  */
640     struct fnfieldlist
641       {
642         char *name;
643         int length;
644         struct nextfnfield *head;
645       }
646      *fnfieldlists;
647
648     /* Number of entries in the fnfieldlists array.  */
649     int nfnfields;
650   };
651
652 /* One item on the queue of compilation units to read in full symbols
653    for.  */
654 struct dwarf2_queue_item
655 {
656   struct dwarf2_per_cu_data *per_cu;
657   struct dwarf2_queue_item *next;
658 };
659
660 /* The current queue.  */
661 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
662
663 /* Loaded secondary compilation units are kept in memory until they
664    have not been referenced for the processing of this many
665    compilation units.  Set this to zero to disable caching.  Cache
666    sizes of up to at least twenty will improve startup time for
667    typical inter-CU-reference binaries, at an obvious memory cost.  */
668 static int dwarf2_max_cache_age = 5;
669 static void
670 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
671                            struct cmd_list_element *c, const char *value)
672 {
673   fprintf_filtered (file, _("\
674 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
675                     value);
676 }
677
678
679 /* Various complaints about symbol reading that don't abort the process */
680
681 static void
682 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
683 {
684   complaint (&symfile_complaints,
685              _("statement list doesn't fit in .debug_line section"));
686 }
687
688 static void
689 dwarf2_debug_line_missing_file_complaint (void)
690 {
691   complaint (&symfile_complaints,
692              _(".debug_line section has line data without a file"));
693 }
694
695 static void
696 dwarf2_debug_line_missing_end_sequence_complaint (void)
697 {
698   complaint (&symfile_complaints,
699              _(".debug_line section has line program sequence without an end"));
700 }
701
702 static void
703 dwarf2_complex_location_expr_complaint (void)
704 {
705   complaint (&symfile_complaints, _("location expression too complex"));
706 }
707
708 static void
709 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
710                                               int arg3)
711 {
712   complaint (&symfile_complaints,
713              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
714              arg2, arg3);
715 }
716
717 static void
718 dwarf2_macros_too_long_complaint (void)
719 {
720   complaint (&symfile_complaints,
721              _("macro info runs off end of `.debug_macinfo' section"));
722 }
723
724 static void
725 dwarf2_macro_malformed_definition_complaint (const char *arg1)
726 {
727   complaint (&symfile_complaints,
728              _("macro debug info contains a malformed macro definition:\n`%s'"),
729              arg1);
730 }
731
732 static void
733 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
734 {
735   complaint (&symfile_complaints,
736              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
737 }
738
739 /* local function prototypes */
740
741 static void dwarf2_locate_sections (bfd *, asection *, void *);
742
743 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
744                                            struct objfile *);
745
746 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
747                                            struct die_info *,
748                                            struct partial_symtab *);
749
750 static void dwarf2_build_psymtabs_hard (struct objfile *);
751
752 static void scan_partial_symbols (struct partial_die_info *,
753                                   CORE_ADDR *, CORE_ADDR *,
754                                   int, struct dwarf2_cu *);
755
756 static void add_partial_symbol (struct partial_die_info *,
757                                 struct dwarf2_cu *);
758
759 static void add_partial_namespace (struct partial_die_info *pdi,
760                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
761                                    int need_pc, struct dwarf2_cu *cu);
762
763 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
764                                 CORE_ADDR *highpc, int need_pc,
765                                 struct dwarf2_cu *cu);
766
767 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
768                                      struct dwarf2_cu *cu);
769
770 static void add_partial_subprogram (struct partial_die_info *pdi,
771                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
772                                     int need_pc, struct dwarf2_cu *cu);
773
774 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
775                                      gdb_byte *buffer, gdb_byte *info_ptr,
776                                      bfd *abfd, struct dwarf2_cu *cu);
777
778 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
779
780 static void psymtab_to_symtab_1 (struct partial_symtab *);
781
782 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
783
784 static void dwarf2_free_abbrev_table (void *);
785
786 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
787                                             struct dwarf2_cu *);
788
789 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
790                                                  struct dwarf2_cu *);
791
792 static struct partial_die_info *load_partial_dies (bfd *,
793                                                    gdb_byte *, gdb_byte *,
794                                                    int, struct dwarf2_cu *);
795
796 static gdb_byte *read_partial_die (struct partial_die_info *,
797                                    struct abbrev_info *abbrev,
798                                    unsigned int, bfd *,
799                                    gdb_byte *, gdb_byte *,
800                                    struct dwarf2_cu *);
801
802 static struct partial_die_info *find_partial_die (unsigned int,
803                                                   struct dwarf2_cu *);
804
805 static void fixup_partial_die (struct partial_die_info *,
806                                struct dwarf2_cu *);
807
808 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
809                                  bfd *, gdb_byte *, struct dwarf2_cu *);
810
811 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
812                                        bfd *, gdb_byte *, struct dwarf2_cu *);
813
814 static unsigned int read_1_byte (bfd *, gdb_byte *);
815
816 static int read_1_signed_byte (bfd *, gdb_byte *);
817
818 static unsigned int read_2_bytes (bfd *, gdb_byte *);
819
820 static unsigned int read_4_bytes (bfd *, gdb_byte *);
821
822 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
823
824 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
825                                unsigned int *);
826
827 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
828
829 static LONGEST read_checked_initial_length_and_offset
830   (bfd *, gdb_byte *, const struct comp_unit_head *,
831    unsigned int *, unsigned int *);
832
833 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
834                             unsigned int *);
835
836 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
837
838 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
839
840 static char *read_string (bfd *, gdb_byte *, unsigned int *);
841
842 static char *read_indirect_string (bfd *, gdb_byte *,
843                                    const struct comp_unit_head *,
844                                    unsigned int *);
845
846 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
847
848 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
849
850 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
851
852 static void set_cu_language (unsigned int, struct dwarf2_cu *);
853
854 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
855                                       struct dwarf2_cu *);
856
857 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
858                                                 unsigned int,
859                                                 struct dwarf2_cu *);
860
861 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
862                                struct dwarf2_cu *cu);
863
864 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
865
866 static struct die_info *die_specification (struct die_info *die,
867                                            struct dwarf2_cu **);
868
869 static void free_line_header (struct line_header *lh);
870
871 static void add_file_name (struct line_header *, char *, unsigned int,
872                            unsigned int, unsigned int);
873
874 static struct line_header *(dwarf_decode_line_header
875                             (unsigned int offset,
876                              bfd *abfd, struct dwarf2_cu *cu));
877
878 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
879                                 struct dwarf2_cu *, struct partial_symtab *);
880
881 static void dwarf2_start_subfile (char *, char *, char *);
882
883 static struct symbol *new_symbol (struct die_info *, struct type *,
884                                   struct dwarf2_cu *);
885
886 static void dwarf2_const_value (struct attribute *, struct symbol *,
887                                 struct dwarf2_cu *);
888
889 static void dwarf2_const_value_data (struct attribute *attr,
890                                      struct symbol *sym,
891                                      int bits);
892
893 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
894
895 static int need_gnat_info (struct dwarf2_cu *);
896
897 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
898
899 static void set_descriptive_type (struct type *, struct die_info *,
900                                   struct dwarf2_cu *);
901
902 static struct type *die_containing_type (struct die_info *,
903                                          struct dwarf2_cu *);
904
905 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
906
907 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
908
909 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
910
911 static char *typename_concat (struct obstack *,
912                               const char *prefix, 
913                               const char *suffix,
914                               struct dwarf2_cu *);
915
916 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
917
918 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
919
920 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
921
922 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
923
924 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
925                                struct dwarf2_cu *, struct partial_symtab *);
926
927 static int dwarf2_get_pc_bounds (struct die_info *,
928                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
929                                  struct partial_symtab *);
930
931 static void get_scope_pc_bounds (struct die_info *,
932                                  CORE_ADDR *, CORE_ADDR *,
933                                  struct dwarf2_cu *);
934
935 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
936                                         CORE_ADDR, struct dwarf2_cu *);
937
938 static void dwarf2_add_field (struct field_info *, struct die_info *,
939                               struct dwarf2_cu *);
940
941 static void dwarf2_attach_fields_to_type (struct field_info *,
942                                           struct type *, struct dwarf2_cu *);
943
944 static void dwarf2_add_member_fn (struct field_info *,
945                                   struct die_info *, struct type *,
946                                   struct dwarf2_cu *);
947
948 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
949                                              struct type *, struct dwarf2_cu *);
950
951 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
952
953 static void read_common_block (struct die_info *, struct dwarf2_cu *);
954
955 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
956
957 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
958
959 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
960
961 static const char *namespace_name (struct die_info *die,
962                                    int *is_anonymous, struct dwarf2_cu *);
963
964 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
965
966 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
967
968 static enum dwarf_array_dim_ordering read_array_order (struct die_info *, 
969                                                        struct dwarf2_cu *);
970
971 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
972
973 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
974                                                  gdb_byte *info_ptr,
975                                                  gdb_byte **new_info_ptr,
976                                                  struct die_info *parent);
977
978 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
979                                                gdb_byte *info_ptr,
980                                                gdb_byte **new_info_ptr,
981                                                struct die_info *parent);
982
983 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
984                                                gdb_byte *info_ptr,
985                                                gdb_byte **new_info_ptr,
986                                                struct die_info *parent);
987
988 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
989                                 struct die_info **, gdb_byte *,
990                                 int *);
991
992 static void process_die (struct die_info *, struct dwarf2_cu *);
993
994 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
995                                        struct obstack *);
996
997 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
998
999 static struct die_info *dwarf2_extension (struct die_info *die,
1000                                           struct dwarf2_cu **);
1001
1002 static char *dwarf_tag_name (unsigned int);
1003
1004 static char *dwarf_attr_name (unsigned int);
1005
1006 static char *dwarf_form_name (unsigned int);
1007
1008 static char *dwarf_stack_op_name (unsigned int);
1009
1010 static char *dwarf_bool_name (unsigned int);
1011
1012 static char *dwarf_type_encoding_name (unsigned int);
1013
1014 #if 0
1015 static char *dwarf_cfi_name (unsigned int);
1016 #endif
1017
1018 static struct die_info *sibling_die (struct die_info *);
1019
1020 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1021
1022 static void dump_die_for_error (struct die_info *);
1023
1024 static void dump_die_1 (struct ui_file *, int level, int max_level,
1025                         struct die_info *);
1026
1027 /*static*/ void dump_die (struct die_info *, int max_level);
1028
1029 static void store_in_ref_table (struct die_info *,
1030                                 struct dwarf2_cu *);
1031
1032 static int is_ref_attr (struct attribute *);
1033
1034 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1035
1036 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1037
1038 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1039                                                struct attribute *,
1040                                                struct dwarf2_cu **);
1041
1042 static struct die_info *follow_die_ref (struct die_info *,
1043                                         struct attribute *,
1044                                         struct dwarf2_cu **);
1045
1046 static struct die_info *follow_die_sig (struct die_info *,
1047                                         struct attribute *,
1048                                         struct dwarf2_cu **);
1049
1050 static void read_signatured_type_at_offset (struct objfile *objfile,
1051                                             unsigned int offset);
1052
1053 static void read_signatured_type (struct objfile *,
1054                                   struct signatured_type *type_sig);
1055
1056 /* memory allocation interface */
1057
1058 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1059
1060 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1061
1062 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1063
1064 static void initialize_cu_func_list (struct dwarf2_cu *);
1065
1066 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1067                                  struct dwarf2_cu *);
1068
1069 static void dwarf_decode_macros (struct line_header *, unsigned int,
1070                                  char *, bfd *, struct dwarf2_cu *);
1071
1072 static int attr_form_is_block (struct attribute *);
1073
1074 static int attr_form_is_section_offset (struct attribute *);
1075
1076 static int attr_form_is_constant (struct attribute *);
1077
1078 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1079                                          struct symbol *sym,
1080                                          struct dwarf2_cu *cu);
1081
1082 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1083                                struct abbrev_info *abbrev,
1084                                struct dwarf2_cu *cu);
1085
1086 static void free_stack_comp_unit (void *);
1087
1088 static hashval_t partial_die_hash (const void *item);
1089
1090 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1091
1092 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1093   (unsigned int offset, struct objfile *objfile);
1094
1095 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1096   (unsigned int offset, struct objfile *objfile);
1097
1098 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1099
1100 static void free_one_comp_unit (void *);
1101
1102 static void free_cached_comp_units (void *);
1103
1104 static void age_cached_comp_units (void);
1105
1106 static void free_one_cached_comp_unit (void *);
1107
1108 static struct type *set_die_type (struct die_info *, struct type *,
1109                                   struct dwarf2_cu *);
1110
1111 static void create_all_comp_units (struct objfile *);
1112
1113 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1114                                  struct objfile *);
1115
1116 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1117
1118 static void dwarf2_add_dependence (struct dwarf2_cu *,
1119                                    struct dwarf2_per_cu_data *);
1120
1121 static void dwarf2_mark (struct dwarf2_cu *);
1122
1123 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1124
1125 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1126
1127 /* Try to locate the sections we need for DWARF 2 debugging
1128    information and return true if we have enough to do something.  */
1129
1130 int
1131 dwarf2_has_info (struct objfile *objfile)
1132 {
1133   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1134   if (!dwarf2_per_objfile)
1135     {
1136       /* Initialize per-objfile state.  */
1137       struct dwarf2_per_objfile *data
1138         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1139       memset (data, 0, sizeof (*data));
1140       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1141       dwarf2_per_objfile = data;
1142
1143       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1144       dwarf2_per_objfile->objfile = objfile;
1145     }
1146   return (dwarf2_per_objfile->info.asection != NULL
1147           && dwarf2_per_objfile->abbrev.asection != NULL);
1148 }
1149
1150 /* When loading sections, we can either look for ".<name>", or for
1151  * ".z<name>", which indicates a compressed section.  */
1152
1153 static int
1154 section_is_p (const char *section_name, const char *name)
1155 {
1156   return (section_name[0] == '.'
1157           && (strcmp (section_name + 1, name) == 0
1158               || (section_name[1] == 'z'
1159                   && strcmp (section_name + 2, name) == 0)));
1160 }
1161
1162 /* This function is mapped across the sections and remembers the
1163    offset and size of each of the debugging sections we are interested
1164    in.  */
1165
1166 static void
1167 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1168 {
1169   if (section_is_p (sectp->name, INFO_SECTION))
1170     {
1171       dwarf2_per_objfile->info.asection = sectp;
1172       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1173     }
1174   else if (section_is_p (sectp->name, ABBREV_SECTION))
1175     {
1176       dwarf2_per_objfile->abbrev.asection = sectp;
1177       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1178     }
1179   else if (section_is_p (sectp->name, LINE_SECTION))
1180     {
1181       dwarf2_per_objfile->line.asection = sectp;
1182       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1183     }
1184   else if (section_is_p (sectp->name, LOC_SECTION))
1185     {
1186       dwarf2_per_objfile->loc.asection = sectp;
1187       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1188     }
1189   else if (section_is_p (sectp->name, MACINFO_SECTION))
1190     {
1191       dwarf2_per_objfile->macinfo.asection = sectp;
1192       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1193     }
1194   else if (section_is_p (sectp->name, STR_SECTION))
1195     {
1196       dwarf2_per_objfile->str.asection = sectp;
1197       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1198     }
1199   else if (section_is_p (sectp->name, FRAME_SECTION))
1200     {
1201       dwarf2_per_objfile->frame.asection = sectp;
1202       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1203     }
1204   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1205     {
1206       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1207       if (aflag & SEC_HAS_CONTENTS)
1208         {
1209           dwarf2_per_objfile->eh_frame.asection = sectp;
1210           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1211         }
1212     }
1213   else if (section_is_p (sectp->name, RANGES_SECTION))
1214     {
1215       dwarf2_per_objfile->ranges.asection = sectp;
1216       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1217     }
1218   else if (section_is_p (sectp->name, TYPES_SECTION))
1219     {
1220       dwarf2_per_objfile->types.asection = sectp;
1221       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1222     }
1223
1224   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1225       && bfd_section_vma (abfd, sectp) == 0)
1226     dwarf2_per_objfile->has_section_at_zero = 1;
1227 }
1228
1229 /* Decompress a section that was compressed using zlib.  Store the
1230    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1231
1232 static void
1233 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1234                          gdb_byte **outbuf, bfd_size_type *outsize)
1235 {
1236   bfd *abfd = objfile->obfd;
1237 #ifndef HAVE_ZLIB_H
1238   error (_("Support for zlib-compressed DWARF data (from '%s') "
1239            "is disabled in this copy of GDB"),
1240          bfd_get_filename (abfd));
1241 #else
1242   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1243   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1244   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1245   bfd_size_type uncompressed_size;
1246   gdb_byte *uncompressed_buffer;
1247   z_stream strm;
1248   int rc;
1249   int header_size = 12;
1250
1251   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1252       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1253     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1254            bfd_get_filename (abfd));
1255
1256   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1257      by the uncompressed section size, 8 bytes in big-endian order.  */
1258   if (compressed_size < header_size
1259       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1260     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1261            bfd_get_filename (abfd));
1262   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1263   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1264   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1265   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1266   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1267   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1268   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1269   uncompressed_size += compressed_buffer[11];
1270
1271   /* It is possible the section consists of several compressed
1272      buffers concatenated together, so we uncompress in a loop.  */
1273   strm.zalloc = NULL;
1274   strm.zfree = NULL;
1275   strm.opaque = NULL;
1276   strm.avail_in = compressed_size - header_size;
1277   strm.next_in = (Bytef*) compressed_buffer + header_size;
1278   strm.avail_out = uncompressed_size;
1279   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1280                                        uncompressed_size);
1281   rc = inflateInit (&strm);
1282   while (strm.avail_in > 0)
1283     {
1284       if (rc != Z_OK)
1285         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1286                bfd_get_filename (abfd), rc);
1287       strm.next_out = ((Bytef*) uncompressed_buffer
1288                        + (uncompressed_size - strm.avail_out));
1289       rc = inflate (&strm, Z_FINISH);
1290       if (rc != Z_STREAM_END)
1291         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1292                bfd_get_filename (abfd), rc);
1293       rc = inflateReset (&strm);
1294     }
1295   rc = inflateEnd (&strm);
1296   if (rc != Z_OK
1297       || strm.avail_out != 0)
1298     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1299            bfd_get_filename (abfd), rc);
1300
1301   do_cleanups (cleanup);
1302   *outbuf = uncompressed_buffer;
1303   *outsize = uncompressed_size;
1304 #endif
1305 }
1306
1307 /* Read the contents of the section SECTP from object file specified by
1308    OBJFILE, store info about the section into INFO.
1309    If the section is compressed, uncompress it before returning.  */
1310
1311 static void
1312 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1313 {
1314   bfd *abfd = objfile->obfd;
1315   asection *sectp = info->asection;
1316   gdb_byte *buf, *retbuf;
1317   unsigned char header[4];
1318
1319   if (info->readin)
1320     return;
1321   info->buffer = NULL;
1322   info->was_mmapped = 0;
1323   info->readin = 1;
1324
1325   if (info->asection == NULL || info->size == 0)
1326     return;
1327
1328   /* Check if the file has a 4-byte header indicating compression.  */
1329   if (info->size > sizeof (header)
1330       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1331       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1332     {
1333       /* Upon decompression, update the buffer and its size.  */
1334       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1335         {
1336           zlib_decompress_section (objfile, sectp, &info->buffer,
1337                                    &info->size);
1338           return;
1339         }
1340     }
1341
1342 #ifdef HAVE_MMAP
1343   if (pagesize == 0)
1344     pagesize = getpagesize ();
1345
1346   /* Only try to mmap sections which are large enough: we don't want to
1347      waste space due to fragmentation.  Also, only try mmap for sections
1348      without relocations.  */
1349
1350   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1351     {
1352       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1353       size_t map_length = info->size + sectp->filepos - pg_offset;
1354       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1355                                  MAP_PRIVATE, pg_offset);
1356
1357       if (retbuf != MAP_FAILED)
1358         {
1359           info->was_mmapped = 1;
1360           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1361 #if HAVE_POSIX_MADVISE
1362           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1363 #endif
1364           return;
1365         }
1366     }
1367 #endif
1368
1369   /* If we get here, we are a normal, not-compressed section.  */
1370   info->buffer = buf
1371     = obstack_alloc (&objfile->objfile_obstack, info->size);
1372
1373   /* When debugging .o files, we may need to apply relocations; see
1374      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1375      We never compress sections in .o files, so we only need to
1376      try this when the section is not compressed.  */
1377   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1378   if (retbuf != NULL)
1379     {
1380       info->buffer = retbuf;
1381       return;
1382     }
1383
1384   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1385       || bfd_bread (buf, info->size, abfd) != info->size)
1386     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1387            bfd_get_filename (abfd));
1388 }
1389
1390 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1391    SECTION_NAME. */
1392
1393 void
1394 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1395                          asection **sectp, gdb_byte **bufp,
1396                          bfd_size_type *sizep)
1397 {
1398   struct dwarf2_per_objfile *data
1399     = objfile_data (objfile, dwarf2_objfile_data_key);
1400   struct dwarf2_section_info *info;
1401
1402   /* We may see an objfile without any DWARF, in which case we just
1403      return nothing.  */
1404   if (data == NULL)
1405     {
1406       *sectp = NULL;
1407       *bufp = NULL;
1408       *sizep = 0;
1409       return;
1410     }
1411   if (section_is_p (section_name, EH_FRAME_SECTION))
1412     info = &data->eh_frame;
1413   else if (section_is_p (section_name, FRAME_SECTION))
1414     info = &data->frame;
1415   else
1416     gdb_assert (0);
1417
1418   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1419     /* We haven't read this section in yet.  Do it now.  */
1420     dwarf2_read_section (objfile, info);
1421
1422   *sectp = info->asection;
1423   *bufp = info->buffer;
1424   *sizep = info->size;
1425 }
1426
1427 /* Build a partial symbol table.  */
1428
1429 void
1430 dwarf2_build_psymtabs (struct objfile *objfile)
1431 {
1432   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
1433     {
1434       init_psymbol_list (objfile, 1024);
1435     }
1436
1437   dwarf2_build_psymtabs_hard (objfile);
1438 }
1439
1440 /* Return TRUE if OFFSET is within CU_HEADER.  */
1441
1442 static inline int
1443 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
1444 {
1445   unsigned int bottom = cu_header->offset;
1446   unsigned int top = (cu_header->offset
1447                       + cu_header->length
1448                       + cu_header->initial_length_size);
1449   return (offset >= bottom && offset < top);
1450 }
1451
1452 /* Read in the comp unit header information from the debug_info at info_ptr.
1453    NOTE: This leaves members offset, first_die_offset to be filled in
1454    by the caller.  */
1455
1456 static gdb_byte *
1457 read_comp_unit_head (struct comp_unit_head *cu_header,
1458                      gdb_byte *info_ptr, bfd *abfd)
1459 {
1460   int signed_addr;
1461   unsigned int bytes_read;
1462
1463   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
1464   cu_header->initial_length_size = bytes_read;
1465   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
1466   info_ptr += bytes_read;
1467   cu_header->version = read_2_bytes (abfd, info_ptr);
1468   info_ptr += 2;
1469   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1470                                           &bytes_read);
1471   info_ptr += bytes_read;
1472   cu_header->addr_size = read_1_byte (abfd, info_ptr);
1473   info_ptr += 1;
1474   signed_addr = bfd_get_sign_extend_vma (abfd);
1475   if (signed_addr < 0)
1476     internal_error (__FILE__, __LINE__,
1477                     _("read_comp_unit_head: dwarf from non elf file"));
1478   cu_header->signed_addr_p = signed_addr;
1479
1480   return info_ptr;
1481 }
1482
1483 static gdb_byte *
1484 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
1485                              gdb_byte *buffer, unsigned int buffer_size,
1486                              bfd *abfd)
1487 {
1488   gdb_byte *beg_of_comp_unit = info_ptr;
1489
1490   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1491
1492   if (header->version != 2 && header->version != 3 && header->version != 4)
1493     error (_("Dwarf Error: wrong version in compilation unit header "
1494            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
1495            bfd_get_filename (abfd));
1496
1497   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
1498     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1499            "(offset 0x%lx + 6) [in module %s]"),
1500            (long) header->abbrev_offset,
1501            (long) (beg_of_comp_unit - buffer),
1502            bfd_get_filename (abfd));
1503
1504   if (beg_of_comp_unit + header->length + header->initial_length_size
1505       > buffer + buffer_size)
1506     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
1507            "(offset 0x%lx + 0) [in module %s]"),
1508            (long) header->length,
1509            (long) (beg_of_comp_unit - buffer),
1510            bfd_get_filename (abfd));
1511
1512   return info_ptr;
1513 }
1514
1515 /* Read in the types comp unit header information from .debug_types entry at
1516    types_ptr.  The result is a pointer to one past the end of the header.  */
1517
1518 static gdb_byte *
1519 read_type_comp_unit_head (struct comp_unit_head *cu_header,
1520                           ULONGEST *signature,
1521                           gdb_byte *types_ptr, bfd *abfd)
1522 {
1523   unsigned int bytes_read;
1524   gdb_byte *initial_types_ptr = types_ptr;
1525
1526   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->types);
1527   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
1528
1529   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
1530
1531   *signature = read_8_bytes (abfd, types_ptr);
1532   types_ptr += 8;
1533   types_ptr += cu_header->offset_size;
1534   cu_header->first_die_offset = types_ptr - initial_types_ptr;
1535
1536   return types_ptr;
1537 }
1538
1539 /* Allocate a new partial symtab for file named NAME and mark this new
1540    partial symtab as being an include of PST.  */
1541
1542 static void
1543 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1544                                struct objfile *objfile)
1545 {
1546   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1547
1548   subpst->section_offsets = pst->section_offsets;
1549   subpst->textlow = 0;
1550   subpst->texthigh = 0;
1551
1552   subpst->dependencies = (struct partial_symtab **)
1553     obstack_alloc (&objfile->objfile_obstack,
1554                    sizeof (struct partial_symtab *));
1555   subpst->dependencies[0] = pst;
1556   subpst->number_of_dependencies = 1;
1557
1558   subpst->globals_offset = 0;
1559   subpst->n_global_syms = 0;
1560   subpst->statics_offset = 0;
1561   subpst->n_static_syms = 0;
1562   subpst->symtab = NULL;
1563   subpst->read_symtab = pst->read_symtab;
1564   subpst->readin = 0;
1565
1566   /* No private part is necessary for include psymtabs.  This property
1567      can be used to differentiate between such include psymtabs and
1568      the regular ones.  */
1569   subpst->read_symtab_private = NULL;
1570 }
1571
1572 /* Read the Line Number Program data and extract the list of files
1573    included by the source file represented by PST.  Build an include
1574    partial symtab for each of these included files.  */
1575
1576 static void
1577 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1578                                struct die_info *die,
1579                                struct partial_symtab *pst)
1580 {
1581   struct objfile *objfile = cu->objfile;
1582   bfd *abfd = objfile->obfd;
1583   struct line_header *lh = NULL;
1584   struct attribute *attr;
1585
1586   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
1587   if (attr)
1588     {
1589       unsigned int line_offset = DW_UNSND (attr);
1590       lh = dwarf_decode_line_header (line_offset, abfd, cu);
1591     }
1592   if (lh == NULL)
1593     return;  /* No linetable, so no includes.  */
1594
1595   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1596
1597   free_line_header (lh);
1598 }
1599
1600 static hashval_t
1601 hash_type_signature (const void *item)
1602 {
1603   const struct signatured_type *type_sig = item;
1604   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
1605   return type_sig->signature;
1606 }
1607
1608 static int
1609 eq_type_signature (const void *item_lhs, const void *item_rhs)
1610 {
1611   const struct signatured_type *lhs = item_lhs;
1612   const struct signatured_type *rhs = item_rhs;
1613   return lhs->signature == rhs->signature;
1614 }
1615
1616 /* Create the hash table of all entries in the .debug_types section.
1617    The result is zero if there is an error (e.g. missing .debug_types section),
1618    otherwise non-zero.  */
1619
1620 static int
1621 create_debug_types_hash_table (struct objfile *objfile)
1622 {
1623   gdb_byte *info_ptr;
1624   htab_t types_htab;
1625
1626   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
1627   info_ptr = dwarf2_per_objfile->types.buffer;
1628
1629   if (info_ptr == NULL)
1630     {
1631       dwarf2_per_objfile->signatured_types = NULL;
1632       return 0;
1633     }
1634
1635   types_htab = htab_create_alloc_ex (41,
1636                                      hash_type_signature,
1637                                      eq_type_signature,
1638                                      NULL,
1639                                      &objfile->objfile_obstack,
1640                                      hashtab_obstack_allocate,
1641                                      dummy_obstack_deallocate);
1642
1643   if (dwarf2_die_debug)
1644     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
1645
1646   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
1647     {
1648       unsigned int offset;
1649       unsigned int offset_size;
1650       unsigned int type_offset;
1651       unsigned int length, initial_length_size;
1652       unsigned short version;
1653       ULONGEST signature;
1654       struct signatured_type *type_sig;
1655       void **slot;
1656       gdb_byte *ptr = info_ptr;
1657
1658       offset = ptr - dwarf2_per_objfile->types.buffer;
1659
1660       /* We need to read the type's signature in order to build the hash
1661          table, but we don't need to read anything else just yet.  */
1662
1663       /* Sanity check to ensure entire cu is present.  */
1664       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
1665       if (ptr + length + initial_length_size
1666           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
1667         {
1668           complaint (&symfile_complaints,
1669                      _("debug type entry runs off end of `.debug_types' section, ignored"));
1670           break;
1671         }
1672
1673       offset_size = initial_length_size == 4 ? 4 : 8;
1674       ptr += initial_length_size;
1675       version = bfd_get_16 (objfile->obfd, ptr);
1676       ptr += 2;
1677       ptr += offset_size; /* abbrev offset */
1678       ptr += 1; /* address size */
1679       signature = bfd_get_64 (objfile->obfd, ptr);
1680       ptr += 8;
1681       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
1682
1683       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
1684       memset (type_sig, 0, sizeof (*type_sig));
1685       type_sig->signature = signature;
1686       type_sig->offset = offset;
1687       type_sig->type_offset = type_offset;
1688
1689       slot = htab_find_slot (types_htab, type_sig, INSERT);
1690       gdb_assert (slot != NULL);
1691       *slot = type_sig;
1692
1693       if (dwarf2_die_debug)
1694         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
1695                             offset, phex (signature, sizeof (signature)));
1696
1697       info_ptr = info_ptr + initial_length_size + length;
1698     }
1699
1700   dwarf2_per_objfile->signatured_types = types_htab;
1701
1702   return 1;
1703 }
1704
1705 /* Lookup a signature based type.
1706    Returns NULL if SIG is not present in the table.  */
1707
1708 static struct signatured_type *
1709 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
1710 {
1711   struct signatured_type find_entry, *entry;
1712
1713   if (dwarf2_per_objfile->signatured_types == NULL)
1714     {
1715       complaint (&symfile_complaints,
1716                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
1717       return 0;
1718     }
1719
1720   find_entry.signature = sig;
1721   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
1722   return entry;
1723 }
1724
1725 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
1726
1727 static void
1728 init_cu_die_reader (struct die_reader_specs *reader,
1729                     struct dwarf2_cu *cu)
1730 {
1731   reader->abfd = cu->objfile->obfd;
1732   reader->cu = cu;
1733   if (cu->per_cu->from_debug_types)
1734     {
1735       gdb_assert (dwarf2_per_objfile->types.readin);
1736       reader->buffer = dwarf2_per_objfile->types.buffer;
1737     }
1738   else
1739     {
1740       gdb_assert (dwarf2_per_objfile->info.readin);
1741       reader->buffer = dwarf2_per_objfile->info.buffer;
1742     }
1743 }
1744
1745 /* Find the base address of the compilation unit for range lists and
1746    location lists.  It will normally be specified by DW_AT_low_pc.
1747    In DWARF-3 draft 4, the base address could be overridden by
1748    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
1749    compilation units with discontinuous ranges.  */
1750
1751 static void
1752 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
1753 {
1754   struct attribute *attr;
1755
1756   cu->base_known = 0;
1757   cu->base_address = 0;
1758
1759   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
1760   if (attr)
1761     {
1762       cu->base_address = DW_ADDR (attr);
1763       cu->base_known = 1;
1764     }
1765   else
1766     {
1767       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
1768       if (attr)
1769         {
1770           cu->base_address = DW_ADDR (attr);
1771           cu->base_known = 1;
1772         }
1773     }
1774 }
1775
1776 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
1777    to combine the common parts.
1778    Process a compilation unit for a psymtab.
1779    BUFFER is a pointer to the beginning of the dwarf section buffer,
1780    either .debug_info or debug_types.
1781    INFO_PTR is a pointer to the start of the CU.
1782    Returns a pointer to the next CU.  */
1783
1784 static gdb_byte *
1785 process_psymtab_comp_unit (struct objfile *objfile,
1786                            struct dwarf2_per_cu_data *this_cu,
1787                            gdb_byte *buffer, gdb_byte *info_ptr,
1788                            unsigned int buffer_size)
1789 {
1790   bfd *abfd = objfile->obfd;
1791   gdb_byte *beg_of_comp_unit = info_ptr;
1792   struct die_info *comp_unit_die;
1793   struct partial_symtab *pst;
1794   CORE_ADDR baseaddr;
1795   struct cleanup *back_to_inner;
1796   struct dwarf2_cu cu;
1797   unsigned int bytes_read;
1798   int has_children, has_pc_info;
1799   struct attribute *attr;
1800   const char *name;
1801   CORE_ADDR best_lowpc = 0, best_highpc = 0;
1802   struct die_reader_specs reader_specs;
1803
1804   memset (&cu, 0, sizeof (cu));
1805   cu.objfile = objfile;
1806   obstack_init (&cu.comp_unit_obstack);
1807
1808   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
1809
1810   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1811                                           buffer, buffer_size,
1812                                           abfd);
1813
1814   /* Complete the cu_header.  */
1815   cu.header.offset = beg_of_comp_unit - buffer;
1816   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1817
1818   cu.list_in_scope = &file_symbols;
1819
1820   /* If this compilation unit was already read in, free the
1821      cached copy in order to read it in again.  This is
1822      necessary because we skipped some symbols when we first
1823      read in the compilation unit (see load_partial_dies).
1824      This problem could be avoided, but the benefit is
1825      unclear.  */
1826   if (this_cu->cu != NULL)
1827     free_one_cached_comp_unit (this_cu->cu);
1828
1829   /* Note that this is a pointer to our stack frame, being
1830      added to a global data structure.  It will be cleaned up
1831      in free_stack_comp_unit when we finish with this
1832      compilation unit.  */
1833   this_cu->cu = &cu;
1834   cu.per_cu = this_cu;
1835
1836   /* Read the abbrevs for this compilation unit into a table.  */
1837   dwarf2_read_abbrevs (abfd, &cu);
1838   make_cleanup (dwarf2_free_abbrev_table, &cu);
1839
1840   /* Read the compilation unit die.  */
1841   if (this_cu->from_debug_types)
1842     info_ptr += 8 /*signature*/ + cu.header.offset_size;
1843   init_cu_die_reader (&reader_specs, &cu);
1844   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1845                             &has_children);
1846
1847   if (this_cu->from_debug_types)
1848     {
1849       /* offset,length haven't been set yet for type units.  */
1850       this_cu->offset = cu.header.offset;
1851       this_cu->length = cu.header.length + cu.header.initial_length_size;
1852     }
1853   else if (comp_unit_die->tag == DW_TAG_partial_unit)
1854     {
1855       info_ptr = (beg_of_comp_unit + cu.header.length
1856                   + cu.header.initial_length_size);
1857       do_cleanups (back_to_inner);
1858       return info_ptr;
1859     }
1860
1861   /* Set the language we're debugging.  */
1862   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
1863   if (attr)
1864     set_cu_language (DW_UNSND (attr), &cu);
1865   else
1866     set_cu_language (language_minimal, &cu);
1867
1868   /* Allocate a new partial symbol table structure.  */
1869   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
1870   pst = start_psymtab_common (objfile, objfile->section_offsets,
1871                               (attr != NULL) ? DW_STRING (attr) : "",
1872                               /* TEXTLOW and TEXTHIGH are set below.  */
1873                               0,
1874                               objfile->global_psymbols.next,
1875                               objfile->static_psymbols.next);
1876
1877   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
1878   if (attr != NULL)
1879     pst->dirname = DW_STRING (attr);
1880
1881   pst->read_symtab_private = this_cu;
1882
1883   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1884
1885   /* Store the function that reads in the rest of the symbol table */
1886   pst->read_symtab = dwarf2_psymtab_to_symtab;
1887
1888   this_cu->psymtab = pst;
1889
1890   dwarf2_find_base_address (comp_unit_die, &cu);
1891
1892   /* Possibly set the default values of LOWPC and HIGHPC from
1893      `DW_AT_ranges'.  */
1894   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
1895                                       &best_highpc, &cu, pst);
1896   if (has_pc_info == 1 && best_lowpc < best_highpc)
1897     /* Store the contiguous range if it is not empty; it can be empty for
1898        CUs with no code.  */
1899     addrmap_set_empty (objfile->psymtabs_addrmap,
1900                        best_lowpc + baseaddr,
1901                        best_highpc + baseaddr - 1, pst);
1902
1903   /* Check if comp unit has_children.
1904      If so, read the rest of the partial symbols from this comp unit.
1905      If not, there's no more debug_info for this comp unit. */
1906   if (has_children)
1907     {
1908       struct partial_die_info *first_die;
1909       CORE_ADDR lowpc, highpc;
1910
1911       lowpc = ((CORE_ADDR) -1);
1912       highpc = ((CORE_ADDR) 0);
1913
1914       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
1915
1916       scan_partial_symbols (first_die, &lowpc, &highpc,
1917                             ! has_pc_info, &cu);
1918
1919       /* If we didn't find a lowpc, set it to highpc to avoid
1920          complaints from `maint check'.  */
1921       if (lowpc == ((CORE_ADDR) -1))
1922         lowpc = highpc;
1923
1924       /* If the compilation unit didn't have an explicit address range,
1925          then use the information extracted from its child dies.  */
1926       if (! has_pc_info)
1927         {
1928           best_lowpc = lowpc;
1929           best_highpc = highpc;
1930         }
1931     }
1932   pst->textlow = best_lowpc + baseaddr;
1933   pst->texthigh = best_highpc + baseaddr;
1934
1935   pst->n_global_syms = objfile->global_psymbols.next -
1936     (objfile->global_psymbols.list + pst->globals_offset);
1937   pst->n_static_syms = objfile->static_psymbols.next -
1938     (objfile->static_psymbols.list + pst->statics_offset);
1939   sort_pst_symbols (pst);
1940
1941   info_ptr = (beg_of_comp_unit + cu.header.length
1942               + cu.header.initial_length_size);
1943
1944   if (this_cu->from_debug_types)
1945     {
1946       /* It's not clear we want to do anything with stmt lists here.
1947          Waiting to see what gcc ultimately does.  */
1948     }
1949   else
1950     {
1951       /* Get the list of files included in the current compilation unit,
1952          and build a psymtab for each of them.  */
1953       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
1954     }
1955
1956   do_cleanups (back_to_inner);
1957
1958   return info_ptr;
1959 }
1960
1961 /* Traversal function for htab_traverse_noresize.
1962    Process one .debug_types comp-unit.  */
1963
1964 static int
1965 process_type_comp_unit (void **slot, void *info)
1966 {
1967   struct signatured_type *entry = (struct signatured_type *) *slot;
1968   struct objfile *objfile = (struct objfile *) info;
1969   struct dwarf2_per_cu_data *this_cu;
1970
1971   this_cu = &entry->per_cu;
1972   this_cu->from_debug_types = 1;
1973
1974   gdb_assert (dwarf2_per_objfile->types.readin);
1975   process_psymtab_comp_unit (objfile, this_cu,
1976                              dwarf2_per_objfile->types.buffer,
1977                              dwarf2_per_objfile->types.buffer + entry->offset,
1978                              dwarf2_per_objfile->types.size);
1979
1980   return 1;
1981 }
1982
1983 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
1984    Build partial symbol tables for the .debug_types comp-units.  */
1985
1986 static void
1987 build_type_psymtabs (struct objfile *objfile)
1988 {
1989   if (! create_debug_types_hash_table (objfile))
1990     return;
1991
1992   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
1993                           process_type_comp_unit, objfile);
1994 }
1995
1996 /* Build the partial symbol table by doing a quick pass through the
1997    .debug_info and .debug_abbrev sections.  */
1998
1999 static void
2000 dwarf2_build_psymtabs_hard (struct objfile *objfile)
2001 {
2002   bfd *abfd = objfile->obfd;
2003   gdb_byte *info_ptr;
2004   struct cleanup *back_to;
2005
2006   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2007   info_ptr = dwarf2_per_objfile->info.buffer;
2008
2009   /* Any cached compilation units will be linked by the per-objfile
2010      read_in_chain.  Make sure to free them when we're done.  */
2011   back_to = make_cleanup (free_cached_comp_units, NULL);
2012
2013   build_type_psymtabs (objfile);
2014
2015   create_all_comp_units (objfile);
2016
2017   objfile->psymtabs_addrmap =
2018     addrmap_create_mutable (&objfile->objfile_obstack);
2019
2020   /* Since the objects we're extracting from .debug_info vary in
2021      length, only the individual functions to extract them (like
2022      read_comp_unit_head and load_partial_die) can really know whether
2023      the buffer is large enough to hold another complete object.
2024
2025      At the moment, they don't actually check that.  If .debug_info
2026      holds just one extra byte after the last compilation unit's dies,
2027      then read_comp_unit_head will happily read off the end of the
2028      buffer.  read_partial_die is similarly casual.  Those functions
2029      should be fixed.
2030
2031      For this loop condition, simply checking whether there's any data
2032      left at all should be sufficient.  */
2033
2034   while (info_ptr < (dwarf2_per_objfile->info.buffer
2035                      + dwarf2_per_objfile->info.size))
2036     {
2037       struct dwarf2_per_cu_data *this_cu;
2038
2039       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
2040                                        objfile);
2041
2042       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
2043                                             dwarf2_per_objfile->info.buffer,
2044                                             info_ptr,
2045                                             dwarf2_per_objfile->info.size);
2046     }
2047
2048   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
2049                                                     &objfile->objfile_obstack);
2050
2051   do_cleanups (back_to);
2052 }
2053
2054 /* Load the partial DIEs for a secondary CU into memory.  */
2055
2056 static void
2057 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
2058                         struct objfile *objfile)
2059 {
2060   bfd *abfd = objfile->obfd;
2061   gdb_byte *info_ptr, *beg_of_comp_unit;
2062   struct die_info *comp_unit_die;
2063   struct dwarf2_cu *cu;
2064   unsigned int bytes_read;
2065   struct cleanup *back_to;
2066   struct attribute *attr;
2067   int has_children;
2068   struct die_reader_specs reader_specs;
2069
2070   gdb_assert (! this_cu->from_debug_types);
2071
2072   gdb_assert (dwarf2_per_objfile->info.readin);
2073   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
2074   beg_of_comp_unit = info_ptr;
2075
2076   cu = alloc_one_comp_unit (objfile);
2077
2078   /* ??? Missing cleanup for CU?  */
2079
2080   /* Link this compilation unit into the compilation unit tree.  */
2081   this_cu->cu = cu;
2082   cu->per_cu = this_cu;
2083   cu->type_hash = this_cu->type_hash;
2084
2085   info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
2086                                           dwarf2_per_objfile->info.buffer,
2087                                           dwarf2_per_objfile->info.size,
2088                                           abfd);
2089
2090   /* Complete the cu_header.  */
2091   cu->header.offset = this_cu->offset;
2092   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
2093
2094   /* Read the abbrevs for this compilation unit into a table.  */
2095   dwarf2_read_abbrevs (abfd, cu);
2096   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
2097
2098   /* Read the compilation unit die.  */
2099   init_cu_die_reader (&reader_specs, cu);
2100   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2101                             &has_children);
2102
2103   /* Set the language we're debugging.  */
2104   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
2105   if (attr)
2106     set_cu_language (DW_UNSND (attr), cu);
2107   else
2108     set_cu_language (language_minimal, cu);
2109
2110   /* Check if comp unit has_children.
2111      If so, read the rest of the partial symbols from this comp unit.
2112      If not, there's no more debug_info for this comp unit. */
2113   if (has_children)
2114     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
2115
2116   do_cleanups (back_to);
2117 }
2118
2119 /* Create a list of all compilation units in OBJFILE.  We do this only
2120    if an inter-comp-unit reference is found; presumably if there is one,
2121    there will be many, and one will occur early in the .debug_info section.
2122    So there's no point in building this list incrementally.  */
2123
2124 static void
2125 create_all_comp_units (struct objfile *objfile)
2126 {
2127   int n_allocated;
2128   int n_comp_units;
2129   struct dwarf2_per_cu_data **all_comp_units;
2130   gdb_byte *info_ptr;
2131
2132   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2133   info_ptr = dwarf2_per_objfile->info.buffer;
2134
2135   n_comp_units = 0;
2136   n_allocated = 10;
2137   all_comp_units = xmalloc (n_allocated
2138                             * sizeof (struct dwarf2_per_cu_data *));
2139   
2140   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
2141     {
2142       unsigned int length, initial_length_size;
2143       gdb_byte *beg_of_comp_unit;
2144       struct dwarf2_per_cu_data *this_cu;
2145       unsigned int offset;
2146
2147       offset = info_ptr - dwarf2_per_objfile->info.buffer;
2148
2149       /* Read just enough information to find out where the next
2150          compilation unit is.  */
2151       length = read_initial_length (objfile->obfd, info_ptr,
2152                                     &initial_length_size);
2153
2154       /* Save the compilation unit for later lookup.  */
2155       this_cu = obstack_alloc (&objfile->objfile_obstack,
2156                                sizeof (struct dwarf2_per_cu_data));
2157       memset (this_cu, 0, sizeof (*this_cu));
2158       this_cu->offset = offset;
2159       this_cu->length = length + initial_length_size;
2160
2161       if (n_comp_units == n_allocated)
2162         {
2163           n_allocated *= 2;
2164           all_comp_units = xrealloc (all_comp_units,
2165                                      n_allocated
2166                                      * sizeof (struct dwarf2_per_cu_data *));
2167         }
2168       all_comp_units[n_comp_units++] = this_cu;
2169
2170       info_ptr = info_ptr + this_cu->length;
2171     }
2172
2173   dwarf2_per_objfile->all_comp_units
2174     = obstack_alloc (&objfile->objfile_obstack,
2175                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
2176   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
2177           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
2178   xfree (all_comp_units);
2179   dwarf2_per_objfile->n_comp_units = n_comp_units;
2180 }
2181
2182 /* Process all loaded DIEs for compilation unit CU, starting at
2183    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
2184    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
2185    DW_AT_ranges).  If NEED_PC is set, then this function will set
2186    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
2187    and record the covered ranges in the addrmap.  */
2188
2189 static void
2190 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
2191                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
2192 {
2193   struct objfile *objfile = cu->objfile;
2194   bfd *abfd = objfile->obfd;
2195   struct partial_die_info *pdi;
2196
2197   /* Now, march along the PDI's, descending into ones which have
2198      interesting children but skipping the children of the other ones,
2199      until we reach the end of the compilation unit.  */
2200
2201   pdi = first_die;
2202
2203   while (pdi != NULL)
2204     {
2205       fixup_partial_die (pdi, cu);
2206
2207       /* Anonymous namespaces have no name but have interesting
2208          children, so we need to look at them.  Ditto for anonymous
2209          enums.  */
2210
2211       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
2212           || pdi->tag == DW_TAG_enumeration_type)
2213         {
2214           switch (pdi->tag)
2215             {
2216             case DW_TAG_subprogram:
2217               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
2218               break;
2219             case DW_TAG_variable:
2220             case DW_TAG_typedef:
2221             case DW_TAG_union_type:
2222               if (!pdi->is_declaration)
2223                 {
2224                   add_partial_symbol (pdi, cu);
2225                 }
2226               break;
2227             case DW_TAG_class_type:
2228             case DW_TAG_interface_type:
2229             case DW_TAG_structure_type:
2230               if (!pdi->is_declaration)
2231                 {
2232                   add_partial_symbol (pdi, cu);
2233                 }
2234               break;
2235             case DW_TAG_enumeration_type:
2236               if (!pdi->is_declaration)
2237                 add_partial_enumeration (pdi, cu);
2238               break;
2239             case DW_TAG_base_type:
2240             case DW_TAG_subrange_type:
2241               /* File scope base type definitions are added to the partial
2242                  symbol table.  */
2243               add_partial_symbol (pdi, cu);
2244               break;
2245             case DW_TAG_namespace:
2246               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
2247               break;
2248             case DW_TAG_module:
2249               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
2250               break;
2251             default:
2252               break;
2253             }
2254         }
2255
2256       /* If the die has a sibling, skip to the sibling.  */
2257
2258       pdi = pdi->die_sibling;
2259     }
2260 }
2261
2262 /* Functions used to compute the fully scoped name of a partial DIE.
2263
2264    Normally, this is simple.  For C++, the parent DIE's fully scoped
2265    name is concatenated with "::" and the partial DIE's name.  For
2266    Java, the same thing occurs except that "." is used instead of "::".
2267    Enumerators are an exception; they use the scope of their parent
2268    enumeration type, i.e. the name of the enumeration type is not
2269    prepended to the enumerator.
2270
2271    There are two complexities.  One is DW_AT_specification; in this
2272    case "parent" means the parent of the target of the specification,
2273    instead of the direct parent of the DIE.  The other is compilers
2274    which do not emit DW_TAG_namespace; in this case we try to guess
2275    the fully qualified name of structure types from their members'
2276    linkage names.  This must be done using the DIE's children rather
2277    than the children of any DW_AT_specification target.  We only need
2278    to do this for structures at the top level, i.e. if the target of
2279    any DW_AT_specification (if any; otherwise the DIE itself) does not
2280    have a parent.  */
2281
2282 /* Compute the scope prefix associated with PDI's parent, in
2283    compilation unit CU.  The result will be allocated on CU's
2284    comp_unit_obstack, or a copy of the already allocated PDI->NAME
2285    field.  NULL is returned if no prefix is necessary.  */
2286 static char *
2287 partial_die_parent_scope (struct partial_die_info *pdi,
2288                           struct dwarf2_cu *cu)
2289 {
2290   char *grandparent_scope;
2291   struct partial_die_info *parent, *real_pdi;
2292
2293   /* We need to look at our parent DIE; if we have a DW_AT_specification,
2294      then this means the parent of the specification DIE.  */
2295
2296   real_pdi = pdi;
2297   while (real_pdi->has_specification)
2298     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
2299
2300   parent = real_pdi->die_parent;
2301   if (parent == NULL)
2302     return NULL;
2303
2304   if (parent->scope_set)
2305     return parent->scope;
2306
2307   fixup_partial_die (parent, cu);
2308
2309   grandparent_scope = partial_die_parent_scope (parent, cu);
2310
2311   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
2312      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
2313      Work around this problem here.  */
2314   if (cu->language == language_cplus
2315       && parent->tag == DW_TAG_namespace 
2316       && strcmp (parent->name, "::") == 0
2317       && grandparent_scope == NULL)
2318     {
2319       parent->scope = NULL;
2320       parent->scope_set = 1;
2321       return NULL;
2322     }
2323
2324   if (parent->tag == DW_TAG_namespace
2325       || parent->tag == DW_TAG_structure_type
2326       || parent->tag == DW_TAG_class_type
2327       || parent->tag == DW_TAG_interface_type
2328       || parent->tag == DW_TAG_union_type
2329       || parent->tag == DW_TAG_enumeration_type)
2330     {
2331       if (grandparent_scope == NULL)
2332         parent->scope = parent->name;
2333       else
2334         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
2335                                          parent->name, cu);
2336     }
2337   else if (parent->tag == DW_TAG_enumerator)
2338     /* Enumerators should not get the name of the enumeration as a prefix.  */
2339     parent->scope = grandparent_scope;
2340   else
2341     {
2342       /* FIXME drow/2004-04-01: What should we be doing with
2343          function-local names?  For partial symbols, we should probably be
2344          ignoring them.  */
2345       complaint (&symfile_complaints,
2346                  _("unhandled containing DIE tag %d for DIE at %d"),
2347                  parent->tag, pdi->offset);
2348       parent->scope = grandparent_scope;
2349     }
2350
2351   parent->scope_set = 1;
2352   return parent->scope;
2353 }
2354
2355 /* Return the fully scoped name associated with PDI, from compilation unit
2356    CU.  The result will be allocated with malloc.  */
2357 static char *
2358 partial_die_full_name (struct partial_die_info *pdi,
2359                        struct dwarf2_cu *cu)
2360 {
2361   char *parent_scope;
2362
2363   parent_scope = partial_die_parent_scope (pdi, cu);
2364   if (parent_scope == NULL)
2365     return NULL;
2366   else
2367     return typename_concat (NULL, parent_scope, pdi->name, cu);
2368 }
2369
2370 static void
2371 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
2372 {
2373   struct objfile *objfile = cu->objfile;
2374   CORE_ADDR addr = 0;
2375   char *actual_name = NULL;
2376   const char *my_prefix;
2377   const struct partial_symbol *psym = NULL;
2378   CORE_ADDR baseaddr;
2379   int built_actual_name = 0;
2380
2381   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2382
2383   actual_name = partial_die_full_name (pdi, cu);
2384   if (actual_name)
2385     built_actual_name = 1;
2386
2387   if (actual_name == NULL)
2388     actual_name = pdi->name;
2389
2390   switch (pdi->tag)
2391     {
2392     case DW_TAG_subprogram:
2393       if (pdi->is_external || cu->language == language_ada)
2394         {
2395           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
2396              of the global scope.  But in Ada, we want to be able to access
2397              nested procedures globally.  So all Ada subprograms are stored
2398              in the global scope.  */
2399           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
2400              mst_text, objfile); */
2401           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2402                                       built_actual_name,
2403                                       VAR_DOMAIN, LOC_BLOCK,
2404                                       &objfile->global_psymbols,
2405                                       0, pdi->lowpc + baseaddr,
2406                                       cu->language, objfile);
2407         }
2408       else
2409         {
2410           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
2411              mst_file_text, objfile); */
2412           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2413                                       built_actual_name,
2414                                       VAR_DOMAIN, LOC_BLOCK,
2415                                       &objfile->static_psymbols,
2416                                       0, pdi->lowpc + baseaddr,
2417                                       cu->language, objfile);
2418         }
2419       break;
2420     case DW_TAG_variable:
2421       if (pdi->is_external)
2422         {
2423           /* Global Variable.
2424              Don't enter into the minimal symbol tables as there is
2425              a minimal symbol table entry from the ELF symbols already.
2426              Enter into partial symbol table if it has a location
2427              descriptor or a type.
2428              If the location descriptor is missing, new_symbol will create
2429              a LOC_UNRESOLVED symbol, the address of the variable will then
2430              be determined from the minimal symbol table whenever the variable
2431              is referenced.
2432              The address for the partial symbol table entry is not
2433              used by GDB, but it comes in handy for debugging partial symbol
2434              table building.  */
2435
2436           if (pdi->locdesc)
2437             addr = decode_locdesc (pdi->locdesc, cu);
2438           if (pdi->locdesc || pdi->has_type)
2439             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2440                                         built_actual_name,
2441                                         VAR_DOMAIN, LOC_STATIC,
2442                                         &objfile->global_psymbols,
2443                                         0, addr + baseaddr,
2444                                         cu->language, objfile);
2445         }
2446       else
2447         {
2448           /* Static Variable. Skip symbols without location descriptors.  */
2449           if (pdi->locdesc == NULL)
2450             {
2451               if (built_actual_name)
2452                 xfree (actual_name);
2453               return;
2454             }
2455           addr = decode_locdesc (pdi->locdesc, cu);
2456           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
2457              mst_file_data, objfile); */
2458           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2459                                       built_actual_name,
2460                                       VAR_DOMAIN, LOC_STATIC,
2461                                       &objfile->static_psymbols,
2462                                       0, addr + baseaddr,
2463                                       cu->language, objfile);
2464         }
2465       break;
2466     case DW_TAG_typedef:
2467     case DW_TAG_base_type:
2468     case DW_TAG_subrange_type:
2469       add_psymbol_to_list (actual_name, strlen (actual_name),
2470                            built_actual_name,
2471                            VAR_DOMAIN, LOC_TYPEDEF,
2472                            &objfile->static_psymbols,
2473                            0, (CORE_ADDR) 0, cu->language, objfile);
2474       break;
2475     case DW_TAG_namespace:
2476       add_psymbol_to_list (actual_name, strlen (actual_name),
2477                            built_actual_name,
2478                            VAR_DOMAIN, LOC_TYPEDEF,
2479                            &objfile->global_psymbols,
2480                            0, (CORE_ADDR) 0, cu->language, objfile);
2481       break;
2482     case DW_TAG_class_type:
2483     case DW_TAG_interface_type:
2484     case DW_TAG_structure_type:
2485     case DW_TAG_union_type:
2486     case DW_TAG_enumeration_type:
2487       /* Skip external references.  The DWARF standard says in the section
2488          about "Structure, Union, and Class Type Entries": "An incomplete
2489          structure, union or class type is represented by a structure,
2490          union or class entry that does not have a byte size attribute
2491          and that has a DW_AT_declaration attribute."  */
2492       if (!pdi->has_byte_size && pdi->is_declaration)
2493         {
2494           if (built_actual_name)
2495             xfree (actual_name);
2496           return;
2497         }
2498
2499       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
2500          static vs. global.  */
2501       add_psymbol_to_list (actual_name, strlen (actual_name),
2502                            built_actual_name,
2503                            STRUCT_DOMAIN, LOC_TYPEDEF,
2504                            (cu->language == language_cplus
2505                             || cu->language == language_java)
2506                            ? &objfile->global_psymbols
2507                            : &objfile->static_psymbols,
2508                            0, (CORE_ADDR) 0, cu->language, objfile);
2509
2510       break;
2511     case DW_TAG_enumerator:
2512       add_psymbol_to_list (actual_name, strlen (actual_name),
2513                            built_actual_name,
2514                            VAR_DOMAIN, LOC_CONST,
2515                            (cu->language == language_cplus
2516                             || cu->language == language_java)
2517                            ? &objfile->global_psymbols
2518                            : &objfile->static_psymbols,
2519                            0, (CORE_ADDR) 0, cu->language, objfile);
2520       break;
2521     default:
2522       break;
2523     }
2524
2525   if (built_actual_name)
2526     xfree (actual_name);
2527 }
2528
2529 /* Read a partial die corresponding to a namespace; also, add a symbol
2530    corresponding to that namespace to the symbol table.  NAMESPACE is
2531    the name of the enclosing namespace.  */
2532
2533 static void
2534 add_partial_namespace (struct partial_die_info *pdi,
2535                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
2536                        int need_pc, struct dwarf2_cu *cu)
2537 {
2538   struct objfile *objfile = cu->objfile;
2539
2540   /* Add a symbol for the namespace.  */
2541
2542   add_partial_symbol (pdi, cu);
2543
2544   /* Now scan partial symbols in that namespace.  */
2545
2546   if (pdi->has_children)
2547     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
2548 }
2549
2550 /* Read a partial die corresponding to a Fortran module.  */
2551
2552 static void
2553 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
2554                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
2555 {
2556   /* Now scan partial symbols in that module.
2557
2558      FIXME: Support the separate Fortran module namespaces.  */
2559
2560   if (pdi->has_children)
2561     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
2562 }
2563
2564 /* Read a partial die corresponding to a subprogram and create a partial
2565    symbol for that subprogram.  When the CU language allows it, this
2566    routine also defines a partial symbol for each nested subprogram
2567    that this subprogram contains.
2568    
2569    DIE my also be a lexical block, in which case we simply search
2570    recursively for suprograms defined inside that lexical block.
2571    Again, this is only performed when the CU language allows this
2572    type of definitions.  */
2573
2574 static void
2575 add_partial_subprogram (struct partial_die_info *pdi,
2576                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
2577                         int need_pc, struct dwarf2_cu *cu)
2578 {
2579   if (pdi->tag == DW_TAG_subprogram)
2580     {
2581       if (pdi->has_pc_info)
2582         {
2583           if (pdi->lowpc < *lowpc)
2584             *lowpc = pdi->lowpc;
2585           if (pdi->highpc > *highpc)
2586             *highpc = pdi->highpc;
2587           if (need_pc)
2588             {
2589               CORE_ADDR baseaddr;
2590               struct objfile *objfile = cu->objfile;
2591
2592               baseaddr = ANOFFSET (objfile->section_offsets,
2593                                    SECT_OFF_TEXT (objfile));
2594               addrmap_set_empty (objfile->psymtabs_addrmap,
2595                                  pdi->lowpc + baseaddr,
2596                                  pdi->highpc - 1 + baseaddr,
2597                                  cu->per_cu->psymtab);
2598             }
2599           if (!pdi->is_declaration)
2600             /* Ignore subprogram DIEs that do not have a name, they are
2601                illegal.  Do not emit a complaint at this point, we will
2602                do so when we convert this psymtab into a symtab.  */
2603             if (pdi->name)
2604               add_partial_symbol (pdi, cu);
2605         }
2606     }
2607   
2608   if (! pdi->has_children)
2609     return;
2610
2611   if (cu->language == language_ada)
2612     {
2613       pdi = pdi->die_child;
2614       while (pdi != NULL)
2615         {
2616           fixup_partial_die (pdi, cu);
2617           if (pdi->tag == DW_TAG_subprogram
2618               || pdi->tag == DW_TAG_lexical_block)
2619             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
2620           pdi = pdi->die_sibling;
2621         }
2622     }
2623 }
2624
2625 /* See if we can figure out if the class lives in a namespace.  We do
2626    this by looking for a member function; its demangled name will
2627    contain namespace info, if there is any.  */
2628
2629 static void
2630 guess_structure_name (struct partial_die_info *struct_pdi,
2631                       struct dwarf2_cu *cu)
2632 {
2633   if ((cu->language == language_cplus
2634        || cu->language == language_java)
2635       && cu->has_namespace_info == 0
2636       && struct_pdi->has_children)
2637     {
2638       /* NOTE: carlton/2003-10-07: Getting the info this way changes
2639          what template types look like, because the demangler
2640          frequently doesn't give the same name as the debug info.  We
2641          could fix this by only using the demangled name to get the
2642          prefix (but see comment in read_structure_type).  */
2643
2644       struct partial_die_info *real_pdi;
2645
2646       /* If this DIE (this DIE's specification, if any) has a parent, then
2647          we should not do this.  We'll prepend the parent's fully qualified
2648          name when we create the partial symbol.  */
2649
2650       real_pdi = struct_pdi;
2651       while (real_pdi->has_specification)
2652         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
2653
2654       if (real_pdi->die_parent != NULL)
2655         return;
2656     }
2657 }
2658
2659 /* Read a partial die corresponding to an enumeration type.  */
2660
2661 static void
2662 add_partial_enumeration (struct partial_die_info *enum_pdi,
2663                          struct dwarf2_cu *cu)
2664 {
2665   struct objfile *objfile = cu->objfile;
2666   bfd *abfd = objfile->obfd;
2667   struct partial_die_info *pdi;
2668
2669   if (enum_pdi->name != NULL)
2670     add_partial_symbol (enum_pdi, cu);
2671
2672   pdi = enum_pdi->die_child;
2673   while (pdi)
2674     {
2675       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
2676         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
2677       else
2678         add_partial_symbol (pdi, cu);
2679       pdi = pdi->die_sibling;
2680     }
2681 }
2682
2683 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2684    Return the corresponding abbrev, or NULL if the number is zero (indicating
2685    an empty DIE).  In either case *BYTES_READ will be set to the length of
2686    the initial number.  */
2687
2688 static struct abbrev_info *
2689 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
2690                  struct dwarf2_cu *cu)
2691 {
2692   bfd *abfd = cu->objfile->obfd;
2693   unsigned int abbrev_number;
2694   struct abbrev_info *abbrev;
2695
2696   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2697
2698   if (abbrev_number == 0)
2699     return NULL;
2700
2701   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2702   if (!abbrev)
2703     {
2704       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
2705                       bfd_get_filename (abfd));
2706     }
2707
2708   return abbrev;
2709 }
2710
2711 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
2712    Returns a pointer to the end of a series of DIEs, terminated by an empty
2713    DIE.  Any children of the skipped DIEs will also be skipped.  */
2714
2715 static gdb_byte *
2716 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
2717 {
2718   struct abbrev_info *abbrev;
2719   unsigned int bytes_read;
2720
2721   while (1)
2722     {
2723       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2724       if (abbrev == NULL)
2725         return info_ptr + bytes_read;
2726       else
2727         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
2728     }
2729 }
2730
2731 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
2732    INFO_PTR should point just after the initial uleb128 of a DIE, and the
2733    abbrev corresponding to that skipped uleb128 should be passed in
2734    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
2735    children.  */
2736
2737 static gdb_byte *
2738 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
2739               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
2740 {
2741   unsigned int bytes_read;
2742   struct attribute attr;
2743   bfd *abfd = cu->objfile->obfd;
2744   unsigned int form, i;
2745
2746   for (i = 0; i < abbrev->num_attrs; i++)
2747     {
2748       /* The only abbrev we care about is DW_AT_sibling.  */
2749       if (abbrev->attrs[i].name == DW_AT_sibling)
2750         {
2751           read_attribute (&attr, &abbrev->attrs[i],
2752                           abfd, info_ptr, cu);
2753           if (attr.form == DW_FORM_ref_addr)
2754             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
2755           else
2756             return buffer + dwarf2_get_ref_die_offset (&attr);
2757         }
2758
2759       /* If it isn't DW_AT_sibling, skip this attribute.  */
2760       form = abbrev->attrs[i].form;
2761     skip_attribute:
2762       switch (form)
2763         {
2764         case DW_FORM_ref_addr:
2765           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
2766              and later it is offset sized.  */
2767           if (cu->header.version == 2)
2768             info_ptr += cu->header.addr_size;
2769           else
2770             info_ptr += cu->header.offset_size;
2771           break;
2772         case DW_FORM_addr:
2773           info_ptr += cu->header.addr_size;
2774           break;
2775         case DW_FORM_data1:
2776         case DW_FORM_ref1:
2777         case DW_FORM_flag:
2778           info_ptr += 1;
2779           break;
2780         case DW_FORM_flag_present:
2781           break;
2782         case DW_FORM_data2:
2783         case DW_FORM_ref2:
2784           info_ptr += 2;
2785           break;
2786         case DW_FORM_data4:
2787         case DW_FORM_ref4:
2788           info_ptr += 4;
2789           break;
2790         case DW_FORM_data8:
2791         case DW_FORM_ref8:
2792         case DW_FORM_sig8:
2793           info_ptr += 8;
2794           break;
2795         case DW_FORM_string:
2796           read_string (abfd, info_ptr, &bytes_read);
2797           info_ptr += bytes_read;
2798           break;
2799         case DW_FORM_sec_offset:
2800         case DW_FORM_strp:
2801           info_ptr += cu->header.offset_size;
2802           break;
2803         case DW_FORM_exprloc:
2804         case DW_FORM_block:
2805           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2806           info_ptr += bytes_read;
2807           break;
2808         case DW_FORM_block1:
2809           info_ptr += 1 + read_1_byte (abfd, info_ptr);
2810           break;
2811         case DW_FORM_block2:
2812           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2813           break;
2814         case DW_FORM_block4:
2815           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2816           break;
2817         case DW_FORM_sdata:
2818         case DW_FORM_udata:
2819         case DW_FORM_ref_udata:
2820           info_ptr = skip_leb128 (abfd, info_ptr);
2821           break;
2822         case DW_FORM_indirect:
2823           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2824           info_ptr += bytes_read;
2825           /* We need to continue parsing from here, so just go back to
2826              the top.  */
2827           goto skip_attribute;
2828
2829         default:
2830           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
2831                  dwarf_form_name (form),
2832                  bfd_get_filename (abfd));
2833         }
2834     }
2835
2836   if (abbrev->has_children)
2837     return skip_children (buffer, info_ptr, cu);
2838   else
2839     return info_ptr;
2840 }
2841
2842 /* Locate ORIG_PDI's sibling.
2843    INFO_PTR should point to the start of the next DIE after ORIG_PDI
2844    in BUFFER.  */
2845
2846 static gdb_byte *
2847 locate_pdi_sibling (struct partial_die_info *orig_pdi,
2848                     gdb_byte *buffer, gdb_byte *info_ptr,
2849                     bfd *abfd, struct dwarf2_cu *cu)
2850 {
2851   /* Do we know the sibling already?  */
2852
2853   if (orig_pdi->sibling)
2854     return orig_pdi->sibling;
2855
2856   /* Are there any children to deal with?  */
2857
2858   if (!orig_pdi->has_children)
2859     return info_ptr;
2860
2861   /* Skip the children the long way.  */
2862
2863   return skip_children (buffer, info_ptr, cu);
2864 }
2865
2866 /* Expand this partial symbol table into a full symbol table.  */
2867
2868 static void
2869 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
2870 {
2871   /* FIXME: This is barely more than a stub.  */
2872   if (pst != NULL)
2873     {
2874       if (pst->readin)
2875         {
2876           warning (_("bug: psymtab for %s is already read in."), pst->filename);
2877         }
2878       else
2879         {
2880           if (info_verbose)
2881             {
2882               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
2883               gdb_flush (gdb_stdout);
2884             }
2885
2886           /* Restore our global data.  */
2887           dwarf2_per_objfile = objfile_data (pst->objfile,
2888                                              dwarf2_objfile_data_key);
2889
2890           /* If this psymtab is constructed from a debug-only objfile, the
2891              has_section_at_zero flag will not necessarily be correct.  We
2892              can get the correct value for this flag by looking at the data
2893              associated with the (presumably stripped) associated objfile.  */
2894           if (pst->objfile->separate_debug_objfile_backlink)
2895             {
2896               struct dwarf2_per_objfile *dpo_backlink
2897                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
2898                                 dwarf2_objfile_data_key);
2899               dwarf2_per_objfile->has_section_at_zero
2900                 = dpo_backlink->has_section_at_zero;
2901             }
2902
2903           psymtab_to_symtab_1 (pst);
2904
2905           /* Finish up the debug error message.  */
2906           if (info_verbose)
2907             printf_filtered (_("done.\n"));
2908         }
2909     }
2910 }
2911
2912 /* Add PER_CU to the queue.  */
2913
2914 static void
2915 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
2916 {
2917   struct dwarf2_queue_item *item;
2918
2919   per_cu->queued = 1;
2920   item = xmalloc (sizeof (*item));
2921   item->per_cu = per_cu;
2922   item->next = NULL;
2923
2924   if (dwarf2_queue == NULL)
2925     dwarf2_queue = item;
2926   else
2927     dwarf2_queue_tail->next = item;
2928
2929   dwarf2_queue_tail = item;
2930 }
2931
2932 /* Process the queue.  */
2933
2934 static void
2935 process_queue (struct objfile *objfile)
2936 {
2937   struct dwarf2_queue_item *item, *next_item;
2938
2939   /* The queue starts out with one item, but following a DIE reference
2940      may load a new CU, adding it to the end of the queue.  */
2941   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
2942     {
2943       if (item->per_cu->psymtab && !item->per_cu->psymtab->readin)
2944         process_full_comp_unit (item->per_cu);
2945
2946       item->per_cu->queued = 0;
2947       next_item = item->next;
2948       xfree (item);
2949     }
2950
2951   dwarf2_queue_tail = NULL;
2952 }
2953
2954 /* Free all allocated queue entries.  This function only releases anything if
2955    an error was thrown; if the queue was processed then it would have been
2956    freed as we went along.  */
2957
2958 static void
2959 dwarf2_release_queue (void *dummy)
2960 {
2961   struct dwarf2_queue_item *item, *last;
2962
2963   item = dwarf2_queue;
2964   while (item)
2965     {
2966       /* Anything still marked queued is likely to be in an
2967          inconsistent state, so discard it.  */
2968       if (item->per_cu->queued)
2969         {
2970           if (item->per_cu->cu != NULL)
2971             free_one_cached_comp_unit (item->per_cu->cu);
2972           item->per_cu->queued = 0;
2973         }
2974
2975       last = item;
2976       item = item->next;
2977       xfree (last);
2978     }
2979
2980   dwarf2_queue = dwarf2_queue_tail = NULL;
2981 }
2982
2983 /* Read in full symbols for PST, and anything it depends on.  */
2984
2985 static void
2986 psymtab_to_symtab_1 (struct partial_symtab *pst)
2987 {
2988   struct dwarf2_per_cu_data *per_cu;
2989   struct cleanup *back_to;
2990   int i;
2991
2992   for (i = 0; i < pst->number_of_dependencies; i++)
2993     if (!pst->dependencies[i]->readin)
2994       {
2995         /* Inform about additional files that need to be read in.  */
2996         if (info_verbose)
2997           {
2998             /* FIXME: i18n: Need to make this a single string.  */
2999             fputs_filtered (" ", gdb_stdout);
3000             wrap_here ("");
3001             fputs_filtered ("and ", gdb_stdout);
3002             wrap_here ("");
3003             printf_filtered ("%s...", pst->dependencies[i]->filename);
3004             wrap_here ("");     /* Flush output */
3005             gdb_flush (gdb_stdout);
3006           }
3007         psymtab_to_symtab_1 (pst->dependencies[i]);
3008       }
3009
3010   per_cu = pst->read_symtab_private;
3011
3012   if (per_cu == NULL)
3013     {
3014       /* It's an include file, no symbols to read for it.
3015          Everything is in the parent symtab.  */
3016       pst->readin = 1;
3017       return;
3018     }
3019
3020   back_to = make_cleanup (dwarf2_release_queue, NULL);
3021
3022   queue_comp_unit (per_cu, pst->objfile);
3023
3024   if (per_cu->from_debug_types)
3025     read_signatured_type_at_offset (pst->objfile, per_cu->offset);
3026   else
3027     load_full_comp_unit (per_cu, pst->objfile);
3028
3029   process_queue (pst->objfile);
3030
3031   /* Age the cache, releasing compilation units that have not
3032      been used recently.  */
3033   age_cached_comp_units ();
3034
3035   do_cleanups (back_to);
3036 }
3037
3038 /* Load the DIEs associated with PER_CU into memory.  */
3039
3040 static void
3041 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
3042 {
3043   bfd *abfd = objfile->obfd;
3044   struct dwarf2_cu *cu;
3045   unsigned int offset;
3046   gdb_byte *info_ptr, *beg_of_comp_unit;
3047   struct cleanup *back_to, *free_cu_cleanup;
3048   struct attribute *attr;
3049   CORE_ADDR baseaddr;
3050
3051   gdb_assert (! per_cu->from_debug_types);
3052
3053   /* Set local variables from the partial symbol table info.  */
3054   offset = per_cu->offset;
3055
3056   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3057   info_ptr = dwarf2_per_objfile->info.buffer + offset;
3058   beg_of_comp_unit = info_ptr;
3059
3060   cu = alloc_one_comp_unit (objfile);
3061
3062   /* If an error occurs while loading, release our storage.  */
3063   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3064
3065   /* Read in the comp_unit header.  */
3066   info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
3067
3068   /* Complete the cu_header.  */
3069   cu->header.offset = offset;
3070   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3071
3072   /* Read the abbrevs for this compilation unit.  */
3073   dwarf2_read_abbrevs (abfd, cu);
3074   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3075
3076   /* Link this compilation unit into the compilation unit tree.  */
3077   per_cu->cu = cu;
3078   cu->per_cu = per_cu;
3079   cu->type_hash = per_cu->type_hash;
3080
3081   cu->dies = read_comp_unit (info_ptr, cu);
3082
3083   /* We try not to read any attributes in this function, because not
3084      all objfiles needed for references have been loaded yet, and symbol
3085      table processing isn't initialized.  But we have to set the CU language,
3086      or we won't be able to build types correctly.  */
3087   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
3088   if (attr)
3089     set_cu_language (DW_UNSND (attr), cu);
3090   else
3091     set_cu_language (language_minimal, cu);
3092
3093   /* Similarly, if we do not read the producer, we can not apply
3094      producer-specific interpretation.  */
3095   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
3096   if (attr)
3097     cu->producer = DW_STRING (attr);
3098
3099   /* Link this CU into read_in_chain.  */
3100   per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3101   dwarf2_per_objfile->read_in_chain = per_cu;
3102
3103   do_cleanups (back_to);
3104
3105   /* We've successfully allocated this compilation unit.  Let our caller
3106      clean it up when finished with it.  */
3107   discard_cleanups (free_cu_cleanup);
3108 }
3109
3110 /* Generate full symbol information for PST and CU, whose DIEs have
3111    already been loaded into memory.  */
3112
3113 static void
3114 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
3115 {
3116   struct partial_symtab *pst = per_cu->psymtab;
3117   struct dwarf2_cu *cu = per_cu->cu;
3118   struct objfile *objfile = pst->objfile;
3119   bfd *abfd = objfile->obfd;
3120   CORE_ADDR lowpc, highpc;
3121   struct symtab *symtab;
3122   struct cleanup *back_to;
3123   CORE_ADDR baseaddr;
3124
3125   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3126
3127   buildsym_init ();
3128   back_to = make_cleanup (really_free_pendings, NULL);
3129
3130   cu->list_in_scope = &file_symbols;
3131
3132   dwarf2_find_base_address (cu->dies, cu);
3133
3134   /* Do line number decoding in read_file_scope () */
3135   process_die (cu->dies, cu);
3136
3137   /* Some compilers don't define a DW_AT_high_pc attribute for the
3138      compilation unit.  If the DW_AT_high_pc is missing, synthesize
3139      it, by scanning the DIE's below the compilation unit.  */
3140   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
3141
3142   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
3143
3144   /* Set symtab language to language from DW_AT_language.
3145      If the compilation is from a C file generated by language preprocessors,
3146      do not set the language if it was already deduced by start_subfile.  */
3147   if (symtab != NULL
3148       && !(cu->language == language_c && symtab->language != language_c))
3149     {
3150       symtab->language = cu->language;
3151     }
3152   pst->symtab = symtab;
3153   pst->readin = 1;
3154
3155   do_cleanups (back_to);
3156 }
3157
3158 /* Process a die and its children.  */
3159
3160 static void
3161 process_die (struct die_info *die, struct dwarf2_cu *cu)
3162 {
3163   switch (die->tag)
3164     {
3165     case DW_TAG_padding:
3166       break;
3167     case DW_TAG_compile_unit:
3168       read_file_scope (die, cu);
3169       break;
3170     case DW_TAG_type_unit:
3171       read_type_unit_scope (die, cu);
3172       break;
3173     case DW_TAG_subprogram:
3174     case DW_TAG_inlined_subroutine:
3175       read_func_scope (die, cu);
3176       break;
3177     case DW_TAG_lexical_block:
3178     case DW_TAG_try_block:
3179     case DW_TAG_catch_block:
3180       read_lexical_block_scope (die, cu);
3181       break;
3182     case DW_TAG_class_type:
3183     case DW_TAG_interface_type:
3184     case DW_TAG_structure_type:
3185     case DW_TAG_union_type:
3186       process_structure_scope (die, cu);
3187       break;
3188     case DW_TAG_enumeration_type:
3189       process_enumeration_scope (die, cu);
3190       break;
3191
3192     /* These dies have a type, but processing them does not create
3193        a symbol or recurse to process the children.  Therefore we can
3194        read them on-demand through read_type_die.  */
3195     case DW_TAG_subroutine_type:
3196     case DW_TAG_set_type:
3197     case DW_TAG_array_type:
3198     case DW_TAG_pointer_type:
3199     case DW_TAG_ptr_to_member_type:
3200     case DW_TAG_reference_type:
3201     case DW_TAG_string_type:
3202       break;
3203
3204     case DW_TAG_base_type:
3205     case DW_TAG_subrange_type:
3206     case DW_TAG_typedef:
3207       /* Add a typedef symbol for the type definition, if it has a
3208          DW_AT_name.  */
3209       new_symbol (die, read_type_die (die, cu), cu);
3210       break;
3211     case DW_TAG_common_block:
3212       read_common_block (die, cu);
3213       break;
3214     case DW_TAG_common_inclusion:
3215       break;
3216     case DW_TAG_namespace:
3217       processing_has_namespace_info = 1;
3218       read_namespace (die, cu);
3219       break;
3220     case DW_TAG_module:
3221       read_module (die, cu);
3222       break;
3223     case DW_TAG_imported_declaration:
3224     case DW_TAG_imported_module:
3225       processing_has_namespace_info = 1;
3226       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3227                                  || cu->language != language_fortran))
3228         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
3229                    dwarf_tag_name (die->tag));
3230       read_import_statement (die, cu);
3231       break;
3232     default:
3233       new_symbol (die, NULL, cu);
3234       break;
3235     }
3236 }
3237
3238 /* A helper function for dwarf2_compute_name which determines whether DIE
3239    needs to have the name of the scope prepended to the name listed in the
3240    die.  */
3241
3242 static int
3243 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
3244 {
3245   struct attribute *attr;
3246
3247   switch (die->tag)
3248     {
3249     case DW_TAG_namespace:
3250     case DW_TAG_typedef:
3251     case DW_TAG_class_type:
3252     case DW_TAG_interface_type:
3253     case DW_TAG_structure_type:
3254     case DW_TAG_union_type:
3255     case DW_TAG_enumeration_type:
3256     case DW_TAG_enumerator:
3257     case DW_TAG_subprogram:
3258     case DW_TAG_member:
3259       return 1;
3260
3261     case DW_TAG_variable:
3262       /* We only need to prefix "globally" visible variables.  These include
3263          any variable marked with DW_AT_external or any variable that
3264          lives in a namespace.  [Variables in anonymous namespaces
3265          require prefixing, but they are not DW_AT_external.]  */
3266
3267       if (dwarf2_attr (die, DW_AT_specification, cu))
3268         {
3269           struct dwarf2_cu *spec_cu = cu;
3270           return die_needs_namespace (die_specification (die, &spec_cu),
3271                                       spec_cu);
3272         }
3273
3274       attr = dwarf2_attr (die, DW_AT_external, cu);
3275       if (attr == NULL && die->parent->tag != DW_TAG_namespace)
3276         return 0;
3277       /* A variable in a lexical block of some kind does not need a
3278          namespace, even though in C++ such variables may be external
3279          and have a mangled name.  */
3280       if (die->parent->tag ==  DW_TAG_lexical_block
3281           || die->parent->tag ==  DW_TAG_try_block
3282           || die->parent->tag ==  DW_TAG_catch_block
3283           || die->parent->tag == DW_TAG_subprogram)
3284         return 0;
3285       return 1;
3286
3287     default:
3288       return 0;
3289     }
3290 }
3291
3292 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
3293    compute the physname for the object, which include a method's
3294    formal parameters (C++/Java) and return type (Java).
3295
3296    For Ada, return the DIE's linkage name rather than the fully qualified
3297    name.  PHYSNAME is ignored..
3298
3299    The result is allocated on the objfile_obstack and canonicalized.  */
3300
3301 static const char *
3302 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
3303                      int physname)
3304 {
3305   if (name == NULL)
3306     name = dwarf2_name (die, cu);
3307
3308   /* These are the only languages we know how to qualify names in.  */
3309   if (name != NULL
3310       && (cu->language == language_cplus || cu->language == language_java))
3311     {
3312       if (die_needs_namespace (die, cu))
3313         {
3314           long length;
3315           char *prefix;
3316           struct ui_file *buf;
3317
3318           prefix = determine_prefix (die, cu);
3319           buf = mem_fileopen ();
3320           if (*prefix != '\0')
3321             {
3322               char *prefixed_name = typename_concat (NULL, prefix, name, cu);
3323               fputs_unfiltered (prefixed_name, buf);
3324               xfree (prefixed_name);
3325             }
3326           else
3327             fputs_unfiltered (name ? name : "", buf);
3328
3329           /* For Java and C++ methods, append formal parameter type
3330              information, if PHYSNAME.  */
3331           
3332           if (physname && die->tag == DW_TAG_subprogram
3333               && (cu->language == language_cplus
3334                   || cu->language == language_java))
3335             {
3336               struct type *type = read_type_die (die, cu);
3337
3338               c_type_print_args (type, buf, 0, cu->language);
3339
3340               if (cu->language == language_java)
3341                 {
3342                   /* For java, we must append the return type to method
3343                      names. */
3344                   if (die->tag == DW_TAG_subprogram)
3345                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
3346                                      0, 0);
3347                 }
3348               else if (cu->language == language_cplus)
3349                 {
3350                   if (TYPE_NFIELDS (type) > 0
3351                       && TYPE_FIELD_ARTIFICIAL (type, 0)
3352                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
3353                     fputs_unfiltered (" const", buf);
3354                 }
3355             }
3356
3357           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
3358                                        &length);
3359           ui_file_delete (buf);
3360
3361           if (cu->language == language_cplus)
3362             {
3363               char *cname
3364                 = dwarf2_canonicalize_name (name, cu,
3365                                             &cu->objfile->objfile_obstack);
3366               if (cname != NULL)
3367                 name = cname;
3368             }
3369         }
3370     }
3371   else if (cu->language == language_ada)
3372     {
3373       /* For Ada unit, we prefer the linkage name over the name, as
3374          the former contains the exported name, which the user expects
3375          to be able to reference.  Ideally, we want the user to be able
3376          to reference this entity using either natural or linkage name,
3377          but we haven't started looking at this enhancement yet.  */
3378       struct attribute *attr;
3379
3380       attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
3381       if (attr && DW_STRING (attr))
3382         name = DW_STRING (attr);
3383     }
3384
3385   return name;
3386 }
3387
3388 /* Return the fully qualified name of DIE, based on its DW_AT_name.
3389    If scope qualifiers are appropriate they will be added.  The result
3390    will be allocated on the objfile_obstack, or NULL if the DIE does
3391    not have a name.  NAME may either be from a previous call to
3392    dwarf2_name or NULL.
3393
3394    The output string will be canonicalized (if C++/Java). */
3395
3396 static const char *
3397 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
3398 {
3399   return dwarf2_compute_name (name, die, cu, 0);
3400 }
3401
3402 /* Construct a physname for the given DIE in CU.  NAME may either be
3403    from a previous call to dwarf2_name or NULL.  The result will be
3404    allocated on the objfile_objstack or NULL if the DIE does not have a
3405    name.
3406
3407    The output string will be canonicalized (if C++/Java).  */
3408
3409 static const char *
3410 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
3411 {
3412   return dwarf2_compute_name (name, die, cu, 1);
3413 }
3414
3415 /* Read the import statement specified by the given die and record it.  */
3416
3417 static void
3418 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
3419 {
3420   struct attribute *import_attr;
3421   struct die_info *imported_die;
3422   struct dwarf2_cu *imported_cu;
3423   const char *imported_name;
3424   const char *imported_name_prefix;
3425   const char *canonical_name;
3426   const char *import_alias;
3427   const char *imported_declaration = NULL;
3428   const char *import_prefix;
3429
3430   char *temp;
3431
3432   import_attr = dwarf2_attr (die, DW_AT_import, cu);
3433   if (import_attr == NULL)
3434     {
3435       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
3436                  dwarf_tag_name (die->tag));
3437       return;
3438     }
3439
3440   imported_cu = cu;
3441   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
3442   imported_name = dwarf2_name (imported_die, imported_cu);
3443   if (imported_name == NULL)
3444     {
3445       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
3446
3447         The import in the following code:
3448         namespace A
3449           {
3450             typedef int B;
3451           }
3452
3453         int main ()
3454           {
3455             using A::B;
3456             B b;
3457             return b;
3458           }
3459
3460         ...
3461          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
3462             <52>   DW_AT_decl_file   : 1
3463             <53>   DW_AT_decl_line   : 6
3464             <54>   DW_AT_import      : <0x75>
3465          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
3466             <59>   DW_AT_name        : B
3467             <5b>   DW_AT_decl_file   : 1
3468             <5c>   DW_AT_decl_line   : 2
3469             <5d>   DW_AT_type        : <0x6e>
3470         ...
3471          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
3472             <76>   DW_AT_byte_size   : 4
3473             <77>   DW_AT_encoding    : 5        (signed)
3474
3475         imports the wrong die ( 0x75 instead of 0x58 ).
3476         This case will be ignored until the gcc bug is fixed.  */
3477       return;
3478     }
3479
3480   /* Figure out the local name after import.  */
3481   import_alias = dwarf2_name (die, cu);
3482
3483   /* Figure out where the statement is being imported to.  */
3484   import_prefix = determine_prefix (die, cu);
3485
3486   /* Figure out what the scope of the imported die is and prepend it
3487      to the name of the imported die.  */
3488   imported_name_prefix = determine_prefix (imported_die, imported_cu);
3489
3490   if (imported_die->tag != DW_TAG_namespace)
3491     {
3492       imported_declaration = imported_name;
3493       canonical_name = imported_name_prefix;
3494     }
3495   else if (strlen (imported_name_prefix) > 0)
3496     {
3497       temp = alloca (strlen (imported_name_prefix)
3498                      + 2 + strlen (imported_name) + 1);
3499       strcpy (temp, imported_name_prefix);
3500       strcat (temp, "::");
3501       strcat (temp, imported_name);
3502       canonical_name = temp;
3503     }
3504   else
3505     canonical_name = imported_name;
3506
3507   cp_add_using_directive (import_prefix,
3508                           canonical_name,
3509                           import_alias,
3510                           imported_declaration,
3511                           &cu->objfile->objfile_obstack);
3512 }
3513
3514 static void
3515 initialize_cu_func_list (struct dwarf2_cu *cu)
3516 {
3517   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
3518 }
3519
3520 static void
3521 free_cu_line_header (void *arg)
3522 {
3523   struct dwarf2_cu *cu = arg;
3524
3525   free_line_header (cu->line_header);
3526   cu->line_header = NULL;
3527 }
3528
3529 static void
3530 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
3531 {
3532   struct objfile *objfile = cu->objfile;
3533   struct comp_unit_head *cu_header = &cu->header;
3534   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3535   CORE_ADDR lowpc = ((CORE_ADDR) -1);
3536   CORE_ADDR highpc = ((CORE_ADDR) 0);
3537   struct attribute *attr;
3538   char *name = NULL;
3539   char *comp_dir = NULL;
3540   struct die_info *child_die;
3541   bfd *abfd = objfile->obfd;
3542   struct line_header *line_header = 0;
3543   CORE_ADDR baseaddr;
3544   
3545   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3546
3547   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
3548
3549   /* If we didn't find a lowpc, set it to highpc to avoid complaints
3550      from finish_block.  */
3551   if (lowpc == ((CORE_ADDR) -1))
3552     lowpc = highpc;
3553   lowpc += baseaddr;
3554   highpc += baseaddr;
3555
3556   /* Find the filename.  Do not use dwarf2_name here, since the filename
3557      is not a source language identifier.  */
3558   attr = dwarf2_attr (die, DW_AT_name, cu);
3559   if (attr)
3560     {
3561       name = DW_STRING (attr);
3562     }
3563
3564   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3565   if (attr)
3566     comp_dir = DW_STRING (attr);
3567   else if (name != NULL && IS_ABSOLUTE_PATH (name))
3568     {
3569       comp_dir = ldirname (name);
3570       if (comp_dir != NULL)
3571         make_cleanup (xfree, comp_dir);
3572     }
3573   if (comp_dir != NULL)
3574     {
3575       /* Irix 6.2 native cc prepends <machine>.: to the compilation
3576          directory, get rid of it.  */
3577       char *cp = strchr (comp_dir, ':');
3578
3579       if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3580         comp_dir = cp + 1;
3581     }
3582
3583   if (name == NULL)
3584     name = "<unknown>";
3585
3586   attr = dwarf2_attr (die, DW_AT_language, cu);
3587   if (attr)
3588     {
3589       set_cu_language (DW_UNSND (attr), cu);
3590     }
3591
3592   attr = dwarf2_attr (die, DW_AT_producer, cu);
3593   if (attr) 
3594     cu->producer = DW_STRING (attr);
3595
3596   /* We assume that we're processing GCC output. */
3597   processing_gcc_compilation = 2;
3598
3599   processing_has_namespace_info = 0;
3600
3601   start_symtab (name, comp_dir, lowpc);
3602   record_debugformat ("DWARF 2");
3603   record_producer (cu->producer);
3604
3605   initialize_cu_func_list (cu);
3606
3607   /* Decode line number information if present.  We do this before
3608      processing child DIEs, so that the line header table is available
3609      for DW_AT_decl_file.  */
3610   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3611   if (attr)
3612     {
3613       unsigned int line_offset = DW_UNSND (attr);
3614       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
3615       if (line_header)
3616         {
3617           cu->line_header = line_header;
3618           make_cleanup (free_cu_line_header, cu);
3619           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
3620         }
3621     }
3622
3623   /* Process all dies in compilation unit.  */
3624   if (die->child != NULL)
3625     {
3626       child_die = die->child;
3627       while (child_die && child_die->tag)
3628         {
3629           process_die (child_die, cu);
3630           child_die = sibling_die (child_die);
3631         }
3632     }
3633
3634   /* Decode macro information, if present.  Dwarf 2 macro information
3635      refers to information in the line number info statement program
3636      header, so we can only read it if we've read the header
3637      successfully.  */
3638   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
3639   if (attr && line_header)
3640     {
3641       unsigned int macro_offset = DW_UNSND (attr);
3642       dwarf_decode_macros (line_header, macro_offset,
3643                            comp_dir, abfd, cu);
3644     }
3645   do_cleanups (back_to);
3646 }
3647
3648 /* For TUs we want to skip the first top level sibling if it's not the
3649    actual type being defined by this TU.  In this case the first top
3650    level sibling is there to provide context only.  */
3651
3652 static void
3653 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
3654 {
3655   struct objfile *objfile = cu->objfile;
3656   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3657   CORE_ADDR lowpc;
3658   struct attribute *attr;
3659   char *name = NULL;
3660   char *comp_dir = NULL;
3661   struct die_info *child_die;
3662   bfd *abfd = objfile->obfd;
3663   struct line_header *line_header = 0;
3664
3665   /* start_symtab needs a low pc, but we don't really have one.
3666      Do what read_file_scope would do in the absence of such info.  */
3667   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3668
3669   /* Find the filename.  Do not use dwarf2_name here, since the filename
3670      is not a source language identifier.  */
3671   attr = dwarf2_attr (die, DW_AT_name, cu);
3672   if (attr)
3673     name = DW_STRING (attr);
3674
3675   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3676   if (attr)
3677     comp_dir = DW_STRING (attr);
3678   else if (name != NULL && IS_ABSOLUTE_PATH (name))
3679     {
3680       comp_dir = ldirname (name);
3681       if (comp_dir != NULL)
3682         make_cleanup (xfree, comp_dir);
3683     }
3684
3685   if (name == NULL)
3686     name = "<unknown>";
3687
3688   attr = dwarf2_attr (die, DW_AT_language, cu);
3689   if (attr)
3690     set_cu_language (DW_UNSND (attr), cu);
3691
3692   /* This isn't technically needed today.  It is done for symmetry
3693      with read_file_scope.  */
3694   attr = dwarf2_attr (die, DW_AT_producer, cu);
3695   if (attr) 
3696     cu->producer = DW_STRING (attr);
3697
3698   /* We assume that we're processing GCC output. */
3699   processing_gcc_compilation = 2;
3700
3701   processing_has_namespace_info = 0;
3702
3703   start_symtab (name, comp_dir, lowpc);
3704   record_debugformat ("DWARF 2");
3705   record_producer (cu->producer);
3706
3707   /* Process the dies in the type unit.  */
3708   if (die->child == NULL)
3709     {
3710       dump_die_for_error (die);
3711       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
3712              bfd_get_filename (abfd));
3713     }
3714
3715   child_die = die->child;
3716
3717   while (child_die && child_die->tag)
3718     {
3719       process_die (child_die, cu);
3720
3721       child_die = sibling_die (child_die);
3722     }
3723
3724   do_cleanups (back_to);
3725 }
3726
3727 static void
3728 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3729                      struct dwarf2_cu *cu)
3730 {
3731   struct function_range *thisfn;
3732
3733   thisfn = (struct function_range *)
3734     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
3735   thisfn->name = name;
3736   thisfn->lowpc = lowpc;
3737   thisfn->highpc = highpc;
3738   thisfn->seen_line = 0;
3739   thisfn->next = NULL;
3740
3741   if (cu->last_fn == NULL)
3742       cu->first_fn = thisfn;
3743   else
3744       cu->last_fn->next = thisfn;
3745
3746   cu->last_fn = thisfn;
3747 }
3748
3749 /* qsort helper for inherit_abstract_dies.  */
3750
3751 static int
3752 unsigned_int_compar (const void *ap, const void *bp)
3753 {
3754   unsigned int a = *(unsigned int *) ap;
3755   unsigned int b = *(unsigned int *) bp;
3756
3757   return (a > b) - (b > a);
3758 }
3759
3760 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3761    Inherit only the children of the DW_AT_abstract_origin DIE not being already
3762    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
3763
3764 static void
3765 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
3766 {
3767   struct die_info *child_die;
3768   unsigned die_children_count;
3769   /* CU offsets which were referenced by children of the current DIE.  */
3770   unsigned *offsets;
3771   unsigned *offsets_end, *offsetp;
3772   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
3773   struct die_info *origin_die;
3774   /* Iterator of the ORIGIN_DIE children.  */
3775   struct die_info *origin_child_die;
3776   struct cleanup *cleanups;
3777   struct attribute *attr;
3778
3779   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
3780   if (!attr)
3781     return;
3782
3783   origin_die = follow_die_ref (die, attr, &cu);
3784   if (die->tag != origin_die->tag
3785       && !(die->tag == DW_TAG_inlined_subroutine
3786            && origin_die->tag == DW_TAG_subprogram))
3787     complaint (&symfile_complaints,
3788                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
3789                die->offset, origin_die->offset);
3790
3791   child_die = die->child;
3792   die_children_count = 0;
3793   while (child_die && child_die->tag)
3794     {
3795       child_die = sibling_die (child_die);
3796       die_children_count++;
3797     }
3798   offsets = xmalloc (sizeof (*offsets) * die_children_count);
3799   cleanups = make_cleanup (xfree, offsets);
3800
3801   offsets_end = offsets;
3802   child_die = die->child;
3803   while (child_die && child_die->tag)
3804     {
3805       /* For each CHILD_DIE, find the corresponding child of
3806          ORIGIN_DIE.  If there is more than one layer of
3807          DW_AT_abstract_origin, follow them all; there shouldn't be,
3808          but GCC versions at least through 4.4 generate this (GCC PR
3809          40573).  */
3810       struct die_info *child_origin_die = child_die;
3811       while (1)
3812         {
3813           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
3814           if (attr == NULL)
3815             break;
3816           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
3817         }
3818
3819       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
3820          counterpart may exist.  */
3821       if (child_origin_die != child_die)
3822         {
3823           if (child_die->tag != child_origin_die->tag
3824               && !(child_die->tag == DW_TAG_inlined_subroutine
3825                    && child_origin_die->tag == DW_TAG_subprogram))
3826             complaint (&symfile_complaints,
3827                        _("Child DIE 0x%x and its abstract origin 0x%x have "
3828                          "different tags"), child_die->offset,
3829                        child_origin_die->offset);
3830           if (child_origin_die->parent != origin_die)
3831             complaint (&symfile_complaints,
3832                        _("Child DIE 0x%x and its abstract origin 0x%x have "
3833                          "different parents"), child_die->offset,
3834                        child_origin_die->offset);
3835           else
3836             *offsets_end++ = child_origin_die->offset;
3837         }
3838       child_die = sibling_die (child_die);
3839     }
3840   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
3841          unsigned_int_compar);
3842   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
3843     if (offsetp[-1] == *offsetp)
3844       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
3845                                         "to DIE 0x%x as their abstract origin"),
3846                  die->offset, *offsetp);
3847
3848   offsetp = offsets;
3849   origin_child_die = origin_die->child;
3850   while (origin_child_die && origin_child_die->tag)
3851     {
3852       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
3853       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
3854         offsetp++;
3855       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
3856         {
3857           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
3858           process_die (origin_child_die, cu);
3859         }
3860       origin_child_die = sibling_die (origin_child_die);
3861     }
3862
3863   do_cleanups (cleanups);
3864 }
3865
3866 static void
3867 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
3868 {
3869   struct objfile *objfile = cu->objfile;
3870   struct context_stack *new;
3871   CORE_ADDR lowpc;
3872   CORE_ADDR highpc;
3873   struct die_info *child_die;
3874   struct attribute *attr, *call_line, *call_file;
3875   char *name;
3876   CORE_ADDR baseaddr;
3877   struct block *block;
3878   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
3879
3880   if (inlined_func)
3881     {
3882       /* If we do not have call site information, we can't show the
3883          caller of this inlined function.  That's too confusing, so
3884          only use the scope for local variables.  */
3885       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
3886       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
3887       if (call_line == NULL || call_file == NULL)
3888         {
3889           read_lexical_block_scope (die, cu);
3890           return;
3891         }
3892     }
3893
3894   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3895
3896   name = dwarf2_name (die, cu);
3897
3898   /* Ignore functions with missing or empty names.  These are actually
3899      illegal according to the DWARF standard.  */
3900   if (name == NULL)
3901     {
3902       complaint (&symfile_complaints,
3903                  _("missing name for subprogram DIE at %d"), die->offset);
3904       return;
3905     }
3906
3907   /* Ignore functions with missing or invalid low and high pc attributes.  */
3908   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
3909     {
3910       complaint (&symfile_complaints,
3911                  _("cannot get low and high bounds for subprogram DIE at %d"),
3912                  die->offset);
3913       return;
3914     }
3915
3916   lowpc += baseaddr;
3917   highpc += baseaddr;
3918
3919   /* Record the function range for dwarf_decode_lines.  */
3920   add_to_cu_func_list (name, lowpc, highpc, cu);
3921
3922   new = push_context (0, lowpc);
3923   new->name = new_symbol (die, read_type_die (die, cu), cu);
3924
3925   /* If there is a location expression for DW_AT_frame_base, record
3926      it.  */
3927   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
3928   if (attr)
3929     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3930        expression is being recorded directly in the function's symbol
3931        and not in a separate frame-base object.  I guess this hack is
3932        to avoid adding some sort of frame-base adjunct/annex to the
3933        function's symbol :-(.  The problem with doing this is that it
3934        results in a function symbol with a location expression that
3935        has nothing to do with the location of the function, ouch!  The
3936        relationship should be: a function's symbol has-a frame base; a
3937        frame-base has-a location expression.  */
3938     dwarf2_symbol_mark_computed (attr, new->name, cu);
3939
3940   cu->list_in_scope = &local_symbols;
3941
3942   if (die->child != NULL)
3943     {
3944       child_die = die->child;
3945       while (child_die && child_die->tag)
3946         {
3947           process_die (child_die, cu);
3948           child_die = sibling_die (child_die);
3949         }
3950     }
3951
3952   inherit_abstract_dies (die, cu);
3953
3954   /* If we have a DW_AT_specification, we might need to import using
3955      directives from the context of the specification DIE.  See the
3956      comment in determine_prefix.  */
3957   if (cu->language == language_cplus
3958       && dwarf2_attr (die, DW_AT_specification, cu))
3959     {
3960       struct dwarf2_cu *spec_cu = cu;
3961       struct die_info *spec_die = die_specification (die, &spec_cu);
3962
3963       while (spec_die)
3964         {
3965           child_die = spec_die->child;
3966           while (child_die && child_die->tag)
3967             {
3968               if (child_die->tag == DW_TAG_imported_module)
3969                 process_die (child_die, spec_cu);
3970               child_die = sibling_die (child_die);
3971             }
3972
3973           /* In some cases, GCC generates specification DIEs that
3974              themselves contain DW_AT_specification attributes.  */
3975           spec_die = die_specification (spec_die, &spec_cu);
3976         }
3977     }
3978
3979   new = pop_context ();
3980   /* Make a block for the local symbols within.  */
3981   block = finish_block (new->name, &local_symbols, new->old_blocks,
3982                         lowpc, highpc, objfile);
3983
3984   /* For C++, set the block's scope.  */
3985   if (cu->language == language_cplus)
3986     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
3987                         determine_prefix (die, cu),
3988                         processing_has_namespace_info);
3989
3990   /* If we have address ranges, record them.  */
3991   dwarf2_record_block_ranges (die, block, baseaddr, cu);
3992   
3993   /* In C++, we can have functions nested inside functions (e.g., when
3994      a function declares a class that has methods).  This means that
3995      when we finish processing a function scope, we may need to go
3996      back to building a containing block's symbol lists.  */
3997   local_symbols = new->locals;
3998   param_symbols = new->params;
3999   using_directives = new->using_directives;
4000
4001   /* If we've finished processing a top-level function, subsequent
4002      symbols go in the file symbol list.  */
4003   if (outermost_context_p ())
4004     cu->list_in_scope = &file_symbols;
4005 }
4006
4007 /* Process all the DIES contained within a lexical block scope.  Start
4008    a new scope, process the dies, and then close the scope.  */
4009
4010 static void
4011 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
4012 {
4013   struct objfile *objfile = cu->objfile;
4014   struct context_stack *new;
4015   CORE_ADDR lowpc, highpc;
4016   struct die_info *child_die;
4017   CORE_ADDR baseaddr;
4018
4019   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4020
4021   /* Ignore blocks with missing or invalid low and high pc attributes.  */
4022   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
4023      as multiple lexical blocks?  Handling children in a sane way would
4024      be nasty.  Might be easier to properly extend generic blocks to 
4025      describe ranges.  */
4026   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
4027     return;
4028   lowpc += baseaddr;
4029   highpc += baseaddr;
4030
4031   push_context (0, lowpc);
4032   if (die->child != NULL)
4033     {
4034       child_die = die->child;
4035       while (child_die && child_die->tag)
4036         {
4037           process_die (child_die, cu);
4038           child_die = sibling_die (child_die);
4039         }
4040     }
4041   new = pop_context ();
4042
4043   if (local_symbols != NULL || using_directives != NULL)
4044     {
4045       struct block *block
4046         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
4047                         highpc, objfile);
4048
4049       /* Note that recording ranges after traversing children, as we
4050          do here, means that recording a parent's ranges entails
4051          walking across all its children's ranges as they appear in
4052          the address map, which is quadratic behavior.
4053
4054          It would be nicer to record the parent's ranges before
4055          traversing its children, simply overriding whatever you find
4056          there.  But since we don't even decide whether to create a
4057          block until after we've traversed its children, that's hard
4058          to do.  */
4059       dwarf2_record_block_ranges (die, block, baseaddr, cu);
4060     }
4061   local_symbols = new->locals;
4062   using_directives = new->using_directives;
4063 }
4064
4065 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
4066    Return 1 if the attributes are present and valid, otherwise, return 0.
4067    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
4068
4069 static int
4070 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
4071                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
4072                     struct partial_symtab *ranges_pst)
4073 {
4074   struct objfile *objfile = cu->objfile;
4075   struct comp_unit_head *cu_header = &cu->header;
4076   bfd *obfd = objfile->obfd;
4077   unsigned int addr_size = cu_header->addr_size;
4078   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4079   /* Base address selection entry.  */
4080   CORE_ADDR base;
4081   int found_base;
4082   unsigned int dummy;
4083   gdb_byte *buffer;
4084   CORE_ADDR marker;
4085   int low_set;
4086   CORE_ADDR low = 0;
4087   CORE_ADDR high = 0;
4088   CORE_ADDR baseaddr;
4089
4090   found_base = cu->base_known;
4091   base = cu->base_address;
4092
4093   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
4094   if (offset >= dwarf2_per_objfile->ranges.size)
4095     {
4096       complaint (&symfile_complaints,
4097                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
4098                  offset);
4099       return 0;
4100     }
4101   buffer = dwarf2_per_objfile->ranges.buffer + offset;
4102
4103   /* Read in the largest possible address.  */
4104   marker = read_address (obfd, buffer, cu, &dummy);
4105   if ((marker & mask) == mask)
4106     {
4107       /* If we found the largest possible address, then
4108          read the base address.  */
4109       base = read_address (obfd, buffer + addr_size, cu, &dummy);
4110       buffer += 2 * addr_size;
4111       offset += 2 * addr_size;
4112       found_base = 1;
4113     }
4114
4115   low_set = 0;
4116
4117   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4118
4119   while (1)
4120     {
4121       CORE_ADDR range_beginning, range_end;
4122
4123       range_beginning = read_address (obfd, buffer, cu, &dummy);
4124       buffer += addr_size;
4125       range_end = read_address (obfd, buffer, cu, &dummy);
4126       buffer += addr_size;
4127       offset += 2 * addr_size;
4128
4129       /* An end of list marker is a pair of zero addresses.  */
4130       if (range_beginning == 0 && range_end == 0)
4131         /* Found the end of list entry.  */
4132         break;
4133
4134       /* Each base address selection entry is a pair of 2 values.
4135          The first is the largest possible address, the second is
4136          the base address.  Check for a base address here.  */
4137       if ((range_beginning & mask) == mask)
4138         {
4139           /* If we found the largest possible address, then
4140              read the base address.  */
4141           base = read_address (obfd, buffer + addr_size, cu, &dummy);
4142           found_base = 1;
4143           continue;
4144         }
4145
4146       if (!found_base)
4147         {
4148           /* We have no valid base address for the ranges
4149              data.  */
4150           complaint (&symfile_complaints,
4151                      _("Invalid .debug_ranges data (no base address)"));
4152           return 0;
4153         }
4154
4155       range_beginning += base;
4156       range_end += base;
4157
4158       if (ranges_pst != NULL && range_beginning < range_end)
4159         addrmap_set_empty (objfile->psymtabs_addrmap,
4160                            range_beginning + baseaddr, range_end - 1 + baseaddr,
4161                            ranges_pst);
4162
4163       /* FIXME: This is recording everything as a low-high
4164          segment of consecutive addresses.  We should have a
4165          data structure for discontiguous block ranges
4166          instead.  */
4167       if (! low_set)
4168         {
4169           low = range_beginning;
4170           high = range_end;
4171           low_set = 1;
4172         }
4173       else
4174         {
4175           if (range_beginning < low)
4176             low = range_beginning;
4177           if (range_end > high)
4178             high = range_end;
4179         }
4180     }
4181
4182   if (! low_set)
4183     /* If the first entry is an end-of-list marker, the range
4184        describes an empty scope, i.e. no instructions.  */
4185     return 0;
4186
4187   if (low_return)
4188     *low_return = low;
4189   if (high_return)
4190     *high_return = high;
4191   return 1;
4192 }
4193
4194 /* Get low and high pc attributes from a die.  Return 1 if the attributes
4195    are present and valid, otherwise, return 0.  Return -1 if the range is
4196    discontinuous, i.e. derived from DW_AT_ranges information.  */
4197 static int
4198 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
4199                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
4200                       struct partial_symtab *pst)
4201 {
4202   struct attribute *attr;
4203   CORE_ADDR low = 0;
4204   CORE_ADDR high = 0;
4205   int ret = 0;
4206
4207   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4208   if (attr)
4209     {
4210       high = DW_ADDR (attr);
4211       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4212       if (attr)
4213         low = DW_ADDR (attr);
4214       else
4215         /* Found high w/o low attribute.  */
4216         return 0;
4217
4218       /* Found consecutive range of addresses.  */
4219       ret = 1;
4220     }
4221   else
4222     {
4223       attr = dwarf2_attr (die, DW_AT_ranges, cu);
4224       if (attr != NULL)
4225         {
4226           /* Value of the DW_AT_ranges attribute is the offset in the
4227              .debug_ranges section.  */
4228           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
4229             return 0;
4230           /* Found discontinuous range of addresses.  */
4231           ret = -1;
4232         }
4233     }
4234
4235   if (high < low)
4236     return 0;
4237
4238   /* When using the GNU linker, .gnu.linkonce. sections are used to
4239      eliminate duplicate copies of functions and vtables and such.
4240      The linker will arbitrarily choose one and discard the others.
4241      The AT_*_pc values for such functions refer to local labels in
4242      these sections.  If the section from that file was discarded, the
4243      labels are not in the output, so the relocs get a value of 0.
4244      If this is a discarded function, mark the pc bounds as invalid,
4245      so that GDB will ignore it.  */
4246   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
4247     return 0;
4248
4249   *lowpc = low;
4250   *highpc = high;
4251   return ret;
4252 }
4253
4254 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
4255    its low and high PC addresses.  Do nothing if these addresses could not
4256    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
4257    and HIGHPC to the high address if greater than HIGHPC.  */
4258
4259 static void
4260 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
4261                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
4262                                  struct dwarf2_cu *cu)
4263 {
4264   CORE_ADDR low, high;
4265   struct die_info *child = die->child;
4266
4267   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
4268     {
4269       *lowpc = min (*lowpc, low);
4270       *highpc = max (*highpc, high);
4271     }
4272
4273   /* If the language does not allow nested subprograms (either inside
4274      subprograms or lexical blocks), we're done.  */
4275   if (cu->language != language_ada)
4276     return;
4277      
4278   /* Check all the children of the given DIE.  If it contains nested
4279      subprograms, then check their pc bounds.  Likewise, we need to
4280      check lexical blocks as well, as they may also contain subprogram
4281      definitions.  */
4282   while (child && child->tag)
4283     {
4284       if (child->tag == DW_TAG_subprogram
4285           || child->tag == DW_TAG_lexical_block)
4286         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
4287       child = sibling_die (child);
4288     }
4289 }
4290
4291 /* Get the low and high pc's represented by the scope DIE, and store
4292    them in *LOWPC and *HIGHPC.  If the correct values can't be
4293    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
4294
4295 static void
4296 get_scope_pc_bounds (struct die_info *die,
4297                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
4298                      struct dwarf2_cu *cu)
4299 {
4300   CORE_ADDR best_low = (CORE_ADDR) -1;
4301   CORE_ADDR best_high = (CORE_ADDR) 0;
4302   CORE_ADDR current_low, current_high;
4303
4304   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
4305     {
4306       best_low = current_low;
4307       best_high = current_high;
4308     }
4309   else
4310     {
4311       struct die_info *child = die->child;
4312
4313       while (child && child->tag)
4314         {
4315           switch (child->tag) {
4316           case DW_TAG_subprogram:
4317             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
4318             break;
4319           case DW_TAG_namespace:
4320             /* FIXME: carlton/2004-01-16: Should we do this for
4321                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
4322                that current GCC's always emit the DIEs corresponding
4323                to definitions of methods of classes as children of a
4324                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
4325                the DIEs giving the declarations, which could be
4326                anywhere).  But I don't see any reason why the
4327                standards says that they have to be there.  */
4328             get_scope_pc_bounds (child, &current_low, &current_high, cu);
4329
4330             if (current_low != ((CORE_ADDR) -1))
4331               {
4332                 best_low = min (best_low, current_low);
4333                 best_high = max (best_high, current_high);
4334               }
4335             break;
4336           default:
4337             /* Ignore. */
4338             break;
4339           }
4340
4341           child = sibling_die (child);
4342         }
4343     }
4344
4345   *lowpc = best_low;
4346   *highpc = best_high;
4347 }
4348
4349 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
4350    in DIE.  */
4351 static void
4352 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
4353                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
4354 {
4355   struct attribute *attr;
4356
4357   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4358   if (attr)
4359     {
4360       CORE_ADDR high = DW_ADDR (attr);
4361       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4362       if (attr)
4363         {
4364           CORE_ADDR low = DW_ADDR (attr);
4365           record_block_range (block, baseaddr + low, baseaddr + high - 1);
4366         }
4367     }
4368
4369   attr = dwarf2_attr (die, DW_AT_ranges, cu);
4370   if (attr)
4371     {
4372       bfd *obfd = cu->objfile->obfd;
4373
4374       /* The value of the DW_AT_ranges attribute is the offset of the
4375          address range list in the .debug_ranges section.  */
4376       unsigned long offset = DW_UNSND (attr);
4377       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
4378
4379       /* For some target architectures, but not others, the
4380          read_address function sign-extends the addresses it returns.
4381          To recognize base address selection entries, we need a
4382          mask.  */
4383       unsigned int addr_size = cu->header.addr_size;
4384       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4385
4386       /* The base address, to which the next pair is relative.  Note
4387          that this 'base' is a DWARF concept: most entries in a range
4388          list are relative, to reduce the number of relocs against the
4389          debugging information.  This is separate from this function's
4390          'baseaddr' argument, which GDB uses to relocate debugging
4391          information from a shared library based on the address at
4392          which the library was loaded.  */
4393       CORE_ADDR base = cu->base_address;
4394       int base_known = cu->base_known;
4395
4396       gdb_assert (dwarf2_per_objfile->ranges.readin);
4397       if (offset >= dwarf2_per_objfile->ranges.size)
4398         {
4399           complaint (&symfile_complaints,
4400                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
4401                      offset);
4402           return;
4403         }
4404
4405       for (;;)
4406         {
4407           unsigned int bytes_read;
4408           CORE_ADDR start, end;
4409
4410           start = read_address (obfd, buffer, cu, &bytes_read);
4411           buffer += bytes_read;
4412           end = read_address (obfd, buffer, cu, &bytes_read);
4413           buffer += bytes_read;
4414
4415           /* Did we find the end of the range list?  */
4416           if (start == 0 && end == 0)
4417             break;
4418
4419           /* Did we find a base address selection entry?  */
4420           else if ((start & base_select_mask) == base_select_mask)
4421             {
4422               base = end;
4423               base_known = 1;
4424             }
4425
4426           /* We found an ordinary address range.  */
4427           else
4428             {
4429               if (!base_known)
4430                 {
4431                   complaint (&symfile_complaints,
4432                              _("Invalid .debug_ranges data (no base address)"));
4433                   return;
4434                 }
4435
4436               record_block_range (block, 
4437                                   baseaddr + base + start, 
4438                                   baseaddr + base + end - 1);
4439             }
4440         }
4441     }
4442 }
4443
4444 /* Add an aggregate field to the field list.  */
4445
4446 static void
4447 dwarf2_add_field (struct field_info *fip, struct die_info *die,
4448                   struct dwarf2_cu *cu)
4449
4450   struct objfile *objfile = cu->objfile;
4451   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4452   struct nextfield *new_field;
4453   struct attribute *attr;
4454   struct field *fp;
4455   char *fieldname = "";
4456
4457   /* Allocate a new field list entry and link it in.  */
4458   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
4459   make_cleanup (xfree, new_field);
4460   memset (new_field, 0, sizeof (struct nextfield));
4461
4462   if (die->tag == DW_TAG_inheritance)
4463     {
4464       new_field->next = fip->baseclasses;
4465       fip->baseclasses = new_field;
4466     }
4467   else
4468     {
4469       new_field->next = fip->fields;
4470       fip->fields = new_field;
4471     }
4472   fip->nfields++;
4473
4474   /* Handle accessibility and virtuality of field.
4475      The default accessibility for members is public, the default
4476      accessibility for inheritance is private.  */
4477   if (die->tag != DW_TAG_inheritance)
4478     new_field->accessibility = DW_ACCESS_public;
4479   else
4480     new_field->accessibility = DW_ACCESS_private;
4481   new_field->virtuality = DW_VIRTUALITY_none;
4482
4483   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
4484   if (attr)
4485     new_field->accessibility = DW_UNSND (attr);
4486   if (new_field->accessibility != DW_ACCESS_public)
4487     fip->non_public_fields = 1;
4488   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4489   if (attr)
4490     new_field->virtuality = DW_UNSND (attr);
4491
4492   fp = &new_field->field;
4493
4494   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
4495     {
4496       /* Data member other than a C++ static data member.  */
4497       
4498       /* Get type of field.  */
4499       fp->type = die_type (die, cu);
4500
4501       SET_FIELD_BITPOS (*fp, 0);
4502
4503       /* Get bit size of field (zero if none).  */
4504       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
4505       if (attr)
4506         {
4507           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
4508         }
4509       else
4510         {
4511           FIELD_BITSIZE (*fp) = 0;
4512         }
4513
4514       /* Get bit offset of field.  */
4515       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
4516       if (attr)
4517         {
4518           int byte_offset = 0;
4519
4520           if (attr_form_is_section_offset (attr))
4521             dwarf2_complex_location_expr_complaint ();
4522           else if (attr_form_is_constant (attr))
4523             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4524           else if (attr_form_is_block (attr))
4525             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4526           else
4527             dwarf2_complex_location_expr_complaint ();
4528
4529           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4530         }
4531       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
4532       if (attr)
4533         {
4534           if (gdbarch_bits_big_endian (gdbarch))
4535             {
4536               /* For big endian bits, the DW_AT_bit_offset gives the
4537                  additional bit offset from the MSB of the containing
4538                  anonymous object to the MSB of the field.  We don't
4539                  have to do anything special since we don't need to
4540                  know the size of the anonymous object.  */
4541               FIELD_BITPOS (*fp) += DW_UNSND (attr);
4542             }
4543           else
4544             {
4545               /* For little endian bits, compute the bit offset to the
4546                  MSB of the anonymous object, subtract off the number of
4547                  bits from the MSB of the field to the MSB of the
4548                  object, and then subtract off the number of bits of
4549                  the field itself.  The result is the bit offset of
4550                  the LSB of the field.  */
4551               int anonymous_size;
4552               int bit_offset = DW_UNSND (attr);
4553
4554               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4555               if (attr)
4556                 {
4557                   /* The size of the anonymous object containing
4558                      the bit field is explicit, so use the
4559                      indicated size (in bytes).  */
4560                   anonymous_size = DW_UNSND (attr);
4561                 }
4562               else
4563                 {
4564                   /* The size of the anonymous object containing
4565                      the bit field must be inferred from the type
4566                      attribute of the data member containing the
4567                      bit field.  */
4568                   anonymous_size = TYPE_LENGTH (fp->type);
4569                 }
4570               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
4571                 - bit_offset - FIELD_BITSIZE (*fp);
4572             }
4573         }
4574
4575       /* Get name of field.  */
4576       fieldname = dwarf2_name (die, cu);
4577       if (fieldname == NULL)
4578         fieldname = "";
4579
4580       /* The name is already allocated along with this objfile, so we don't
4581          need to duplicate it for the type.  */
4582       fp->name = fieldname;
4583
4584       /* Change accessibility for artificial fields (e.g. virtual table
4585          pointer or virtual base class pointer) to private.  */
4586       if (dwarf2_attr (die, DW_AT_artificial, cu))
4587         {
4588           FIELD_ARTIFICIAL (*fp) = 1;
4589           new_field->accessibility = DW_ACCESS_private;
4590           fip->non_public_fields = 1;
4591         }
4592     }
4593   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
4594     {
4595       /* C++ static member.  */
4596
4597       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
4598          is a declaration, but all versions of G++ as of this writing
4599          (so through at least 3.2.1) incorrectly generate
4600          DW_TAG_variable tags.  */
4601       
4602       char *physname;
4603
4604       /* Get name of field.  */
4605       fieldname = dwarf2_name (die, cu);
4606       if (fieldname == NULL)
4607         return;
4608
4609       /* Get physical name.  */
4610       physname = (char *) dwarf2_physname (fieldname, die, cu);
4611
4612       /* The name is already allocated along with this objfile, so we don't
4613          need to duplicate it for the type.  */
4614       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
4615       FIELD_TYPE (*fp) = die_type (die, cu);
4616       FIELD_NAME (*fp) = fieldname;
4617     }
4618   else if (die->tag == DW_TAG_inheritance)
4619     {
4620       /* C++ base class field.  */
4621       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
4622       if (attr)
4623         {
4624           int byte_offset = 0;
4625
4626           if (attr_form_is_section_offset (attr))
4627             dwarf2_complex_location_expr_complaint ();
4628           else if (attr_form_is_constant (attr))
4629             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4630           else if (attr_form_is_block (attr))
4631             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4632           else
4633             dwarf2_complex_location_expr_complaint ();
4634
4635           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4636         }
4637       FIELD_BITSIZE (*fp) = 0;
4638       FIELD_TYPE (*fp) = die_type (die, cu);
4639       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
4640       fip->nbaseclasses++;
4641     }
4642 }
4643
4644 /* Create the vector of fields, and attach it to the type.  */
4645
4646 static void
4647 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
4648                               struct dwarf2_cu *cu)
4649 {
4650   int nfields = fip->nfields;
4651
4652   /* Record the field count, allocate space for the array of fields,
4653      and create blank accessibility bitfields if necessary.  */
4654   TYPE_NFIELDS (type) = nfields;
4655   TYPE_FIELDS (type) = (struct field *)
4656     TYPE_ALLOC (type, sizeof (struct field) * nfields);
4657   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4658
4659   if (fip->non_public_fields && cu->language != language_ada)
4660     {
4661       ALLOCATE_CPLUS_STRUCT_TYPE (type);
4662
4663       TYPE_FIELD_PRIVATE_BITS (type) =
4664         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4665       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4666
4667       TYPE_FIELD_PROTECTED_BITS (type) =
4668         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4669       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4670
4671       TYPE_FIELD_IGNORE_BITS (type) =
4672         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4673       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4674     }
4675
4676   /* If the type has baseclasses, allocate and clear a bit vector for
4677      TYPE_FIELD_VIRTUAL_BITS.  */
4678   if (fip->nbaseclasses && cu->language != language_ada)
4679     {
4680       int num_bytes = B_BYTES (fip->nbaseclasses);
4681       unsigned char *pointer;
4682
4683       ALLOCATE_CPLUS_STRUCT_TYPE (type);
4684       pointer = TYPE_ALLOC (type, num_bytes);
4685       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
4686       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
4687       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
4688     }
4689
4690   /* Copy the saved-up fields into the field vector.  Start from the head
4691      of the list, adding to the tail of the field array, so that they end
4692      up in the same order in the array in which they were added to the list.  */
4693   while (nfields-- > 0)
4694     {
4695       struct nextfield *fieldp;
4696
4697       if (fip->fields)
4698         {
4699           fieldp = fip->fields;
4700           fip->fields = fieldp->next;
4701         }
4702       else
4703         {
4704           fieldp = fip->baseclasses;
4705           fip->baseclasses = fieldp->next;
4706         }
4707
4708       TYPE_FIELD (type, nfields) = fieldp->field;
4709       switch (fieldp->accessibility)
4710         {
4711         case DW_ACCESS_private:
4712           if (cu->language != language_ada)
4713             SET_TYPE_FIELD_PRIVATE (type, nfields);
4714           break;
4715
4716         case DW_ACCESS_protected:
4717           if (cu->language != language_ada)
4718             SET_TYPE_FIELD_PROTECTED (type, nfields);
4719           break;
4720
4721         case DW_ACCESS_public:
4722           break;
4723
4724         default:
4725           /* Unknown accessibility.  Complain and treat it as public.  */
4726           {
4727             complaint (&symfile_complaints, _("unsupported accessibility %d"),
4728                        fieldp->accessibility);
4729           }
4730           break;
4731         }
4732       if (nfields < fip->nbaseclasses)
4733         {
4734           switch (fieldp->virtuality)
4735             {
4736             case DW_VIRTUALITY_virtual:
4737             case DW_VIRTUALITY_pure_virtual:
4738               if (cu->language == language_ada)
4739                 error ("unexpected virtuality in component of Ada type");
4740               SET_TYPE_FIELD_VIRTUAL (type, nfields);
4741               break;
4742             }
4743         }
4744     }
4745 }
4746
4747 /* Add a member function to the proper fieldlist.  */
4748
4749 static void
4750 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
4751                       struct type *type, struct dwarf2_cu *cu)
4752 {
4753   struct objfile *objfile = cu->objfile;
4754   struct attribute *attr;
4755   struct fnfieldlist *flp;
4756   int i;
4757   struct fn_field *fnp;
4758   char *fieldname;
4759   char *physname;
4760   struct nextfnfield *new_fnfield;
4761   struct type *this_type;
4762
4763   if (cu->language == language_ada)
4764     error ("unexpected member function in Ada type");
4765
4766   /* Get name of member function.  */
4767   fieldname = dwarf2_name (die, cu);
4768   if (fieldname == NULL)
4769     return;
4770
4771   /* Get the mangled name.  */
4772   physname = (char *) dwarf2_physname (fieldname, die, cu);
4773
4774   /* Look up member function name in fieldlist.  */
4775   for (i = 0; i < fip->nfnfields; i++)
4776     {
4777       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
4778         break;
4779     }
4780
4781   /* Create new list element if necessary.  */
4782   if (i < fip->nfnfields)
4783     flp = &fip->fnfieldlists[i];
4784   else
4785     {
4786       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
4787         {
4788           fip->fnfieldlists = (struct fnfieldlist *)
4789             xrealloc (fip->fnfieldlists,
4790                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
4791                       * sizeof (struct fnfieldlist));
4792           if (fip->nfnfields == 0)
4793             make_cleanup (free_current_contents, &fip->fnfieldlists);
4794         }
4795       flp = &fip->fnfieldlists[fip->nfnfields];
4796       flp->name = fieldname;
4797       flp->length = 0;
4798       flp->head = NULL;
4799       fip->nfnfields++;
4800     }
4801
4802   /* Create a new member function field and chain it to the field list
4803      entry. */
4804   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
4805   make_cleanup (xfree, new_fnfield);
4806   memset (new_fnfield, 0, sizeof (struct nextfnfield));
4807   new_fnfield->next = flp->head;
4808   flp->head = new_fnfield;
4809   flp->length++;
4810
4811   /* Fill in the member function field info.  */
4812   fnp = &new_fnfield->fnfield;
4813   /* The name is already allocated along with this objfile, so we don't
4814      need to duplicate it for the type.  */
4815   fnp->physname = physname ? physname : "";
4816   fnp->type = alloc_type (objfile);
4817   this_type = read_type_die (die, cu);
4818   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
4819     {
4820       int nparams = TYPE_NFIELDS (this_type);
4821
4822       /* TYPE is the domain of this method, and THIS_TYPE is the type
4823            of the method itself (TYPE_CODE_METHOD).  */
4824       smash_to_method_type (fnp->type, type,
4825                             TYPE_TARGET_TYPE (this_type),
4826                             TYPE_FIELDS (this_type),
4827                             TYPE_NFIELDS (this_type),
4828                             TYPE_VARARGS (this_type));
4829
4830       /* Handle static member functions.
4831          Dwarf2 has no clean way to discern C++ static and non-static
4832          member functions. G++ helps GDB by marking the first
4833          parameter for non-static member functions (which is the
4834          this pointer) as artificial. We obtain this information
4835          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
4836       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
4837         fnp->voffset = VOFFSET_STATIC;
4838     }
4839   else
4840     complaint (&symfile_complaints, _("member function type missing for '%s'"),
4841                physname);
4842
4843   /* Get fcontext from DW_AT_containing_type if present.  */
4844   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
4845     fnp->fcontext = die_containing_type (die, cu);
4846
4847   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
4848      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
4849
4850   /* Get accessibility.  */
4851   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
4852   if (attr)
4853     {
4854       switch (DW_UNSND (attr))
4855         {
4856         case DW_ACCESS_private:
4857           fnp->is_private = 1;
4858           break;
4859         case DW_ACCESS_protected:
4860           fnp->is_protected = 1;
4861           break;
4862         }
4863     }
4864
4865   /* Check for artificial methods.  */
4866   attr = dwarf2_attr (die, DW_AT_artificial, cu);
4867   if (attr && DW_UNSND (attr) != 0)
4868     fnp->is_artificial = 1;
4869
4870   /* Get index in virtual function table if it is a virtual member
4871      function.  For GCC, this is an offset in the appropriate
4872      virtual table, as specified by DW_AT_containing_type.  For
4873      everyone else, it is an expression to be evaluated relative
4874      to the object address.  */
4875
4876   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
4877   if (attr && fnp->fcontext)
4878     {
4879       /* Support the .debug_loc offsets */
4880       if (attr_form_is_block (attr))
4881         {
4882           fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
4883         }
4884       else if (attr_form_is_section_offset (attr))
4885         {
4886           dwarf2_complex_location_expr_complaint ();
4887         }
4888       else
4889         {
4890           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4891                                                  fieldname);
4892         }
4893     }
4894   else if (attr)
4895     {
4896       /* We only support trivial expressions here.  This hack will work
4897          for v3 classes, which always start with the vtable pointer.  */
4898       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0
4899           && DW_BLOCK (attr)->data[0] == DW_OP_deref)
4900         {
4901           struct dwarf_block blk;
4902           blk.size = DW_BLOCK (attr)->size - 1;
4903           blk.data = DW_BLOCK (attr)->data + 1;
4904           fnp->voffset = decode_locdesc (&blk, cu);
4905           if ((fnp->voffset % cu->header.addr_size) != 0)
4906             dwarf2_complex_location_expr_complaint ();
4907           else
4908             fnp->voffset /= cu->header.addr_size;
4909           fnp->voffset += 2;
4910           fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
4911         }
4912       else
4913         dwarf2_complex_location_expr_complaint ();
4914     }
4915   else
4916     {
4917       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4918       if (attr && DW_UNSND (attr))
4919         {
4920           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
4921           complaint (&symfile_complaints,
4922                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
4923                      fieldname, die->offset);
4924           TYPE_CPLUS_DYNAMIC (type) = 1;
4925         }
4926     }
4927 }
4928
4929 /* Create the vector of member function fields, and attach it to the type.  */
4930
4931 static void
4932 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
4933                                  struct dwarf2_cu *cu)
4934 {
4935   struct fnfieldlist *flp;
4936   int total_length = 0;
4937   int i;
4938
4939   if (cu->language == language_ada)
4940     error ("unexpected member functions in Ada type");
4941
4942   ALLOCATE_CPLUS_STRUCT_TYPE (type);
4943   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4944     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4945
4946   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4947     {
4948       struct nextfnfield *nfp = flp->head;
4949       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4950       int k;
4951
4952       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4953       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4954       fn_flp->fn_fields = (struct fn_field *)
4955         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4956       for (k = flp->length; (k--, nfp); nfp = nfp->next)
4957         fn_flp->fn_fields[k] = nfp->fnfield;
4958
4959       total_length += flp->length;
4960     }
4961
4962   TYPE_NFN_FIELDS (type) = fip->nfnfields;
4963   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4964 }
4965
4966 /* Returns non-zero if NAME is the name of a vtable member in CU's
4967    language, zero otherwise.  */
4968 static int
4969 is_vtable_name (const char *name, struct dwarf2_cu *cu)
4970 {
4971   static const char vptr[] = "_vptr";
4972   static const char vtable[] = "vtable";
4973
4974   /* Look for the C++ and Java forms of the vtable.  */
4975   if ((cu->language == language_java
4976        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4977        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4978        && is_cplus_marker (name[sizeof (vptr) - 1])))
4979     return 1;
4980
4981   return 0;
4982 }
4983
4984 /* GCC outputs unnamed structures that are really pointers to member
4985    functions, with the ABI-specified layout.  If TYPE describes
4986    such a structure, smash it into a member function type.
4987
4988    GCC shouldn't do this; it should just output pointer to member DIEs.
4989    This is GCC PR debug/28767.  */
4990
4991 static void
4992 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
4993 {
4994   struct type *pfn_type, *domain_type, *new_type;
4995
4996   /* Check for a structure with no name and two children.  */
4997   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
4998     return;
4999
5000   /* Check for __pfn and __delta members.  */
5001   if (TYPE_FIELD_NAME (type, 0) == NULL
5002       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
5003       || TYPE_FIELD_NAME (type, 1) == NULL
5004       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
5005     return;
5006
5007   /* Find the type of the method.  */
5008   pfn_type = TYPE_FIELD_TYPE (type, 0);
5009   if (pfn_type == NULL
5010       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
5011       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
5012     return;
5013
5014   /* Look for the "this" argument.  */
5015   pfn_type = TYPE_TARGET_TYPE (pfn_type);
5016   if (TYPE_NFIELDS (pfn_type) == 0
5017       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
5018       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
5019     return;
5020
5021   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
5022   new_type = alloc_type (objfile);
5023   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
5024                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
5025                         TYPE_VARARGS (pfn_type));
5026   smash_to_methodptr_type (type, new_type);
5027 }
5028
5029 /* Called when we find the DIE that starts a structure or union scope
5030    (definition) to process all dies that define the members of the
5031    structure or union.
5032
5033    NOTE: we need to call struct_type regardless of whether or not the
5034    DIE has an at_name attribute, since it might be an anonymous
5035    structure or union.  This gets the type entered into our set of
5036    user defined types.
5037
5038    However, if the structure is incomplete (an opaque struct/union)
5039    then suppress creating a symbol table entry for it since gdb only
5040    wants to find the one with the complete definition.  Note that if
5041    it is complete, we just call new_symbol, which does it's own
5042    checking about whether the struct/union is anonymous or not (and
5043    suppresses creating a symbol table entry itself).  */
5044
5045 static struct type *
5046 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
5047 {
5048   struct objfile *objfile = cu->objfile;
5049   struct type *type;
5050   struct attribute *attr;
5051   char *name;
5052   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5053
5054   /* If the definition of this type lives in .debug_types, read that type.
5055      Don't follow DW_AT_specification though, that will take us back up
5056      the chain and we want to go down.  */
5057   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5058   if (attr)
5059     {
5060       struct dwarf2_cu *type_cu = cu;
5061       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5062       /* We could just recurse on read_structure_type, but we need to call
5063          get_die_type to ensure only one type for this DIE is created.
5064          This is important, for example, because for c++ classes we need
5065          TYPE_NAME set which is only done by new_symbol.  Blech.  */
5066       type = read_type_die (type_die, type_cu);
5067       return set_die_type (die, type, cu);
5068     }
5069
5070   type = alloc_type (objfile);
5071   INIT_CPLUS_SPECIFIC (type);
5072
5073   name = dwarf2_name (die, cu);
5074   if (name != NULL)
5075     {
5076       if (cu->language == language_cplus
5077           || cu->language == language_java)
5078         {
5079           TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
5080           if (die->tag == DW_TAG_structure_type
5081               || die->tag == DW_TAG_class_type)
5082             TYPE_NAME (type) = TYPE_TAG_NAME (type);
5083         }
5084       else
5085         {
5086           /* The name is already allocated along with this objfile, so
5087              we don't need to duplicate it for the type.  */
5088           TYPE_TAG_NAME (type) = (char *) name;
5089           if (die->tag == DW_TAG_class_type)
5090             TYPE_NAME (type) = TYPE_TAG_NAME (type);
5091         }
5092     }
5093
5094   if (die->tag == DW_TAG_structure_type)
5095     {
5096       TYPE_CODE (type) = TYPE_CODE_STRUCT;
5097     }
5098   else if (die->tag == DW_TAG_union_type)
5099     {
5100       TYPE_CODE (type) = TYPE_CODE_UNION;
5101     }
5102   else
5103     {
5104       TYPE_CODE (type) = TYPE_CODE_CLASS;
5105     }
5106
5107   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
5108     TYPE_DECLARED_CLASS (type) = 1;
5109
5110   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5111   if (attr)
5112     {
5113       TYPE_LENGTH (type) = DW_UNSND (attr);
5114     }
5115   else
5116     {
5117       TYPE_LENGTH (type) = 0;
5118     }
5119
5120   TYPE_STUB_SUPPORTED (type) = 1;
5121   if (die_is_declaration (die, cu))
5122     TYPE_STUB (type) = 1;
5123   else if (attr == NULL && die->child == NULL
5124            && producer_is_realview (cu->producer))
5125     /* RealView does not output the required DW_AT_declaration
5126        on incomplete types.  */
5127     TYPE_STUB (type) = 1;
5128
5129   set_descriptive_type (type, die, cu);
5130
5131   /* We need to add the type field to the die immediately so we don't
5132      infinitely recurse when dealing with pointers to the structure
5133      type within the structure itself. */
5134   set_die_type (die, type, cu);
5135
5136   if (die->child != NULL && ! die_is_declaration (die, cu))
5137     {
5138       struct field_info fi;
5139       struct die_info *child_die;
5140
5141       memset (&fi, 0, sizeof (struct field_info));
5142
5143       child_die = die->child;
5144
5145       while (child_die && child_die->tag)
5146         {
5147           if (child_die->tag == DW_TAG_member
5148               || child_die->tag == DW_TAG_variable)
5149             {
5150               /* NOTE: carlton/2002-11-05: A C++ static data member
5151                  should be a DW_TAG_member that is a declaration, but
5152                  all versions of G++ as of this writing (so through at
5153                  least 3.2.1) incorrectly generate DW_TAG_variable
5154                  tags for them instead.  */
5155               dwarf2_add_field (&fi, child_die, cu);
5156             }
5157           else if (child_die->tag == DW_TAG_subprogram)
5158             {
5159               /* C++ member function. */
5160               dwarf2_add_member_fn (&fi, child_die, type, cu);
5161             }
5162           else if (child_die->tag == DW_TAG_inheritance)
5163             {
5164               /* C++ base class field.  */
5165               dwarf2_add_field (&fi, child_die, cu);
5166             }
5167           child_die = sibling_die (child_die);
5168         }
5169
5170       /* Attach fields and member functions to the type.  */
5171       if (fi.nfields)
5172         dwarf2_attach_fields_to_type (&fi, type, cu);
5173       if (fi.nfnfields)
5174         {
5175           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
5176
5177           /* Get the type which refers to the base class (possibly this
5178              class itself) which contains the vtable pointer for the current
5179              class from the DW_AT_containing_type attribute.  This use of
5180              DW_AT_containing_type is a GNU extension.  */
5181
5182           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
5183             {
5184               struct type *t = die_containing_type (die, cu);
5185
5186               TYPE_VPTR_BASETYPE (type) = t;
5187               if (type == t)
5188                 {
5189                   int i;
5190
5191                   /* Our own class provides vtbl ptr.  */
5192                   for (i = TYPE_NFIELDS (t) - 1;
5193                        i >= TYPE_N_BASECLASSES (t);
5194                        --i)
5195                     {
5196                       char *fieldname = TYPE_FIELD_NAME (t, i);
5197
5198                       if (is_vtable_name (fieldname, cu))
5199                         {
5200                           TYPE_VPTR_FIELDNO (type) = i;
5201                           break;
5202                         }
5203                     }
5204
5205                   /* Complain if virtual function table field not found.  */
5206                   if (i < TYPE_N_BASECLASSES (t))
5207                     complaint (&symfile_complaints,
5208                                _("virtual function table pointer not found when defining class '%s'"),
5209                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
5210                                "");
5211                 }
5212               else
5213                 {
5214                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
5215                 }
5216             }
5217           else if (cu->producer
5218                    && strncmp (cu->producer,
5219                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
5220             {
5221               /* The IBM XLC compiler does not provide direct indication
5222                  of the containing type, but the vtable pointer is
5223                  always named __vfp.  */
5224
5225               int i;
5226
5227               for (i = TYPE_NFIELDS (type) - 1;
5228                    i >= TYPE_N_BASECLASSES (type);
5229                    --i)
5230                 {
5231                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
5232                     {
5233                       TYPE_VPTR_FIELDNO (type) = i;
5234                       TYPE_VPTR_BASETYPE (type) = type;
5235                       break;
5236                     }
5237                 }
5238             }
5239         }
5240     }
5241
5242   quirk_gcc_member_function_pointer (type, cu->objfile);
5243
5244   do_cleanups (back_to);
5245   return type;
5246 }
5247
5248 static void
5249 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
5250 {
5251   struct objfile *objfile = cu->objfile;
5252   struct die_info *child_die = die->child;
5253   struct type *this_type;
5254
5255   this_type = get_die_type (die, cu);
5256   if (this_type == NULL)
5257     this_type = read_structure_type (die, cu);
5258
5259   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
5260      snapshots) has been known to create a die giving a declaration
5261      for a class that has, as a child, a die giving a definition for a
5262      nested class.  So we have to process our children even if the
5263      current die is a declaration.  Normally, of course, a declaration
5264      won't have any children at all.  */
5265
5266   while (child_die != NULL && child_die->tag)
5267     {
5268       if (child_die->tag == DW_TAG_member
5269           || child_die->tag == DW_TAG_variable
5270           || child_die->tag == DW_TAG_inheritance)
5271         {
5272           /* Do nothing.  */
5273         }
5274       else
5275         process_die (child_die, cu);
5276
5277       child_die = sibling_die (child_die);
5278     }
5279
5280   /* Do not consider external references.  According to the DWARF standard,
5281      these DIEs are identified by the fact that they have no byte_size
5282      attribute, and a declaration attribute.  */
5283   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
5284       || !die_is_declaration (die, cu))
5285     new_symbol (die, this_type, cu);
5286 }
5287
5288 /* Given a DW_AT_enumeration_type die, set its type.  We do not
5289    complete the type's fields yet, or create any symbols.  */
5290
5291 static struct type *
5292 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
5293 {
5294   struct objfile *objfile = cu->objfile;
5295   struct type *type;
5296   struct attribute *attr;
5297   const char *name;
5298
5299   /* If the definition of this type lives in .debug_types, read that type.
5300      Don't follow DW_AT_specification though, that will take us back up
5301      the chain and we want to go down.  */
5302   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5303   if (attr)
5304     {
5305       struct dwarf2_cu *type_cu = cu;
5306       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5307       type = read_type_die (type_die, type_cu);
5308       return set_die_type (die, type, cu);
5309     }
5310
5311   type = alloc_type (objfile);
5312
5313   TYPE_CODE (type) = TYPE_CODE_ENUM;
5314   name = dwarf2_full_name (NULL, die, cu);
5315   if (name != NULL)
5316     TYPE_TAG_NAME (type) = (char *) name;
5317
5318   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5319   if (attr)
5320     {
5321       TYPE_LENGTH (type) = DW_UNSND (attr);
5322     }
5323   else
5324     {
5325       TYPE_LENGTH (type) = 0;
5326     }
5327
5328   /* The enumeration DIE can be incomplete.  In Ada, any type can be
5329      declared as private in the package spec, and then defined only
5330      inside the package body.  Such types are known as Taft Amendment
5331      Types.  When another package uses such a type, an incomplete DIE
5332      may be generated by the compiler.  */
5333   if (die_is_declaration (die, cu))
5334     TYPE_STUB (type) = 1;
5335
5336   return set_die_type (die, type, cu);
5337 }
5338
5339 /* Given a pointer to a die which begins an enumeration, process all
5340    the dies that define the members of the enumeration, and create the
5341    symbol for the enumeration type.
5342
5343    NOTE: We reverse the order of the element list.  */
5344
5345 static void
5346 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
5347 {
5348   struct objfile *objfile = cu->objfile;
5349   struct die_info *child_die;
5350   struct field *fields;
5351   struct symbol *sym;
5352   int num_fields;
5353   int unsigned_enum = 1;
5354   char *name;
5355   struct type *this_type;
5356
5357   num_fields = 0;
5358   fields = NULL;
5359   this_type = get_die_type (die, cu);
5360   if (this_type == NULL)
5361     this_type = read_enumeration_type (die, cu);
5362   if (die->child != NULL)
5363     {
5364       child_die = die->child;
5365       while (child_die && child_die->tag)
5366         {
5367           if (child_die->tag != DW_TAG_enumerator)
5368             {
5369               process_die (child_die, cu);
5370             }
5371           else
5372             {
5373               name = dwarf2_name (child_die, cu);
5374               if (name)
5375                 {
5376                   sym = new_symbol (child_die, this_type, cu);
5377                   if (SYMBOL_VALUE (sym) < 0)
5378                     unsigned_enum = 0;
5379
5380                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
5381                     {
5382                       fields = (struct field *)
5383                         xrealloc (fields,
5384                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
5385                                   * sizeof (struct field));
5386                     }
5387
5388                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
5389                   FIELD_TYPE (fields[num_fields]) = NULL;
5390                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
5391                   FIELD_BITSIZE (fields[num_fields]) = 0;
5392
5393                   num_fields++;
5394                 }
5395             }
5396
5397           child_die = sibling_die (child_die);
5398         }
5399
5400       if (num_fields)
5401         {
5402           TYPE_NFIELDS (this_type) = num_fields;
5403           TYPE_FIELDS (this_type) = (struct field *)
5404             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
5405           memcpy (TYPE_FIELDS (this_type), fields,
5406                   sizeof (struct field) * num_fields);
5407           xfree (fields);
5408         }
5409       if (unsigned_enum)
5410         TYPE_UNSIGNED (this_type) = 1;
5411     }
5412
5413   new_symbol (die, this_type, cu);
5414 }
5415
5416 /* Extract all information from a DW_TAG_array_type DIE and put it in
5417    the DIE's type field.  For now, this only handles one dimensional
5418    arrays.  */
5419
5420 static struct type *
5421 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
5422 {
5423   struct objfile *objfile = cu->objfile;
5424   struct die_info *child_die;
5425   struct type *type = NULL;
5426   struct type *element_type, *range_type, *index_type;
5427   struct type **range_types = NULL;
5428   struct attribute *attr;
5429   int ndim = 0;
5430   struct cleanup *back_to;
5431   char *name;
5432
5433   element_type = die_type (die, cu);
5434
5435   /* Irix 6.2 native cc creates array types without children for
5436      arrays with unspecified length.  */
5437   if (die->child == NULL)
5438     {
5439       index_type = objfile_type (objfile)->builtin_int;
5440       range_type = create_range_type (NULL, index_type, 0, -1);
5441       type = create_array_type (NULL, element_type, range_type);
5442       return set_die_type (die, type, cu);
5443     }
5444
5445   back_to = make_cleanup (null_cleanup, NULL);
5446   child_die = die->child;
5447   while (child_die && child_die->tag)
5448     {
5449       if (child_die->tag == DW_TAG_subrange_type)
5450         {
5451           struct type *child_type = read_type_die (child_die, cu);
5452           if (child_type != NULL)
5453             {
5454               /* The range type was succesfully read. Save it for
5455                  the array type creation.  */
5456               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
5457                 {
5458                   range_types = (struct type **)
5459                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
5460                               * sizeof (struct type *));
5461                   if (ndim == 0)
5462                     make_cleanup (free_current_contents, &range_types);
5463                 }
5464               range_types[ndim++] = child_type;
5465             }
5466         }
5467       child_die = sibling_die (child_die);
5468     }
5469
5470   /* Dwarf2 dimensions are output from left to right, create the
5471      necessary array types in backwards order.  */
5472
5473   type = element_type;
5474
5475   if (read_array_order (die, cu) == DW_ORD_col_major)
5476     {
5477       int i = 0;
5478       while (i < ndim)
5479         type = create_array_type (NULL, type, range_types[i++]);
5480     }
5481   else
5482     {
5483       while (ndim-- > 0)
5484         type = create_array_type (NULL, type, range_types[ndim]);
5485     }
5486
5487   /* Understand Dwarf2 support for vector types (like they occur on
5488      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
5489      array type.  This is not part of the Dwarf2/3 standard yet, but a
5490      custom vendor extension.  The main difference between a regular
5491      array and the vector variant is that vectors are passed by value
5492      to functions.  */
5493   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
5494   if (attr)
5495     make_vector_type (type);
5496
5497   name = dwarf2_name (die, cu);
5498   if (name)
5499     TYPE_NAME (type) = name;
5500   
5501   set_descriptive_type (type, die, cu);
5502
5503   do_cleanups (back_to);
5504
5505   /* Install the type in the die. */
5506   return set_die_type (die, type, cu);
5507 }
5508
5509 static enum dwarf_array_dim_ordering
5510 read_array_order (struct die_info *die, struct dwarf2_cu *cu) 
5511 {
5512   struct attribute *attr;
5513
5514   attr = dwarf2_attr (die, DW_AT_ordering, cu);
5515
5516   if (attr) return DW_SND (attr);
5517
5518   /*
5519     GNU F77 is a special case, as at 08/2004 array type info is the
5520     opposite order to the dwarf2 specification, but data is still 
5521     laid out as per normal fortran.
5522
5523     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need 
5524     version checking.
5525   */
5526
5527   if (cu->language == language_fortran
5528       && cu->producer && strstr (cu->producer, "GNU F77"))
5529     {
5530       return DW_ORD_row_major;
5531     }
5532
5533   switch (cu->language_defn->la_array_ordering) 
5534     {
5535     case array_column_major:
5536       return DW_ORD_col_major;
5537     case array_row_major:
5538     default:
5539       return DW_ORD_row_major;
5540     };
5541 }
5542
5543 /* Extract all information from a DW_TAG_set_type DIE and put it in
5544    the DIE's type field. */
5545
5546 static struct type *
5547 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
5548 {
5549   struct type *set_type = create_set_type (NULL, die_type (die, cu));
5550
5551   return set_die_type (die, set_type, cu);
5552 }
5553
5554 /* First cut: install each common block member as a global variable.  */
5555
5556 static void
5557 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
5558 {
5559   struct die_info *child_die;
5560   struct attribute *attr;
5561   struct symbol *sym;
5562   CORE_ADDR base = (CORE_ADDR) 0;
5563
5564   attr = dwarf2_attr (die, DW_AT_location, cu);
5565   if (attr)
5566     {
5567       /* Support the .debug_loc offsets */
5568       if (attr_form_is_block (attr))
5569         {
5570           base = decode_locdesc (DW_BLOCK (attr), cu);
5571         }
5572       else if (attr_form_is_section_offset (attr))
5573         {
5574           dwarf2_complex_location_expr_complaint ();
5575         }
5576       else
5577         {
5578           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5579                                                  "common block member");
5580         }
5581     }
5582   if (die->child != NULL)
5583     {
5584       child_die = die->child;
5585       while (child_die && child_die->tag)
5586         {
5587           sym = new_symbol (child_die, NULL, cu);
5588           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
5589           if (attr)
5590             {
5591               CORE_ADDR byte_offset = 0;
5592
5593               if (attr_form_is_section_offset (attr))
5594                 dwarf2_complex_location_expr_complaint ();
5595               else if (attr_form_is_constant (attr))
5596                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5597               else if (attr_form_is_block (attr))
5598                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5599               else
5600                 dwarf2_complex_location_expr_complaint ();
5601
5602               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
5603               add_symbol_to_list (sym, &global_symbols);
5604             }
5605           child_die = sibling_die (child_die);
5606         }
5607     }
5608 }
5609
5610 /* Create a type for a C++ namespace.  */
5611
5612 static struct type *
5613 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
5614 {
5615   struct objfile *objfile = cu->objfile;
5616   const char *previous_prefix, *name;
5617   int is_anonymous;
5618   struct type *type;
5619
5620   /* For extensions, reuse the type of the original namespace.  */
5621   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
5622     {
5623       struct die_info *ext_die;
5624       struct dwarf2_cu *ext_cu = cu;
5625       ext_die = dwarf2_extension (die, &ext_cu);
5626       type = read_type_die (ext_die, ext_cu);
5627       return set_die_type (die, type, cu);
5628     }
5629
5630   name = namespace_name (die, &is_anonymous, cu);
5631
5632   /* Now build the name of the current namespace.  */
5633
5634   previous_prefix = determine_prefix (die, cu);
5635   if (previous_prefix[0] != '\0')
5636     name = typename_concat (&objfile->objfile_obstack,
5637                             previous_prefix, name, cu);
5638
5639   /* Create the type.  */
5640   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
5641                     objfile);
5642   TYPE_NAME (type) = (char *) name;
5643   TYPE_TAG_NAME (type) = TYPE_NAME (type);
5644
5645   return set_die_type (die, type, cu);
5646 }
5647
5648 /* Read a C++ namespace.  */
5649
5650 static void
5651 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
5652 {
5653   struct objfile *objfile = cu->objfile;
5654   const char *name;
5655   int is_anonymous;
5656
5657   /* Add a symbol associated to this if we haven't seen the namespace
5658      before.  Also, add a using directive if it's an anonymous
5659      namespace.  */
5660
5661   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5662     {
5663       struct type *type;
5664
5665       type = read_type_die (die, cu);
5666       new_symbol (die, type, cu);
5667
5668       name = namespace_name (die, &is_anonymous, cu);
5669       if (is_anonymous)
5670         {
5671           const char *previous_prefix = determine_prefix (die, cu);
5672           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
5673                                   NULL, &objfile->objfile_obstack);
5674         }
5675     }
5676
5677   if (die->child != NULL)
5678     {
5679       struct die_info *child_die = die->child;
5680       
5681       while (child_die && child_die->tag)
5682         {
5683           process_die (child_die, cu);
5684           child_die = sibling_die (child_die);
5685         }
5686     }
5687 }
5688
5689 /* Read a Fortran module.  */
5690
5691 static void
5692 read_module (struct die_info *die, struct dwarf2_cu *cu)
5693 {
5694   struct die_info *child_die = die->child;
5695
5696   /* FIXME: Support the separate Fortran module namespaces.  */
5697
5698   while (child_die && child_die->tag)
5699     {
5700       process_die (child_die, cu);
5701       child_die = sibling_die (child_die);
5702     }
5703 }
5704
5705 /* Return the name of the namespace represented by DIE.  Set
5706    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
5707    namespace.  */
5708
5709 static const char *
5710 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
5711 {
5712   struct die_info *current_die;
5713   const char *name = NULL;
5714
5715   /* Loop through the extensions until we find a name.  */
5716
5717   for (current_die = die;
5718        current_die != NULL;
5719        current_die = dwarf2_extension (die, &cu))
5720     {
5721       name = dwarf2_name (current_die, cu);
5722       if (name != NULL)
5723         break;
5724     }
5725
5726   /* Is it an anonymous namespace?  */
5727
5728   *is_anonymous = (name == NULL);
5729   if (*is_anonymous)
5730     name = "(anonymous namespace)";
5731
5732   return name;
5733 }
5734
5735 /* Extract all information from a DW_TAG_pointer_type DIE and add to
5736    the user defined type vector.  */
5737
5738 static struct type *
5739 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
5740 {
5741   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
5742   struct comp_unit_head *cu_header = &cu->header;
5743   struct type *type;
5744   struct attribute *attr_byte_size;
5745   struct attribute *attr_address_class;
5746   int byte_size, addr_class;
5747
5748   type = lookup_pointer_type (die_type (die, cu));
5749
5750   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
5751   if (attr_byte_size)
5752     byte_size = DW_UNSND (attr_byte_size);
5753   else
5754     byte_size = cu_header->addr_size;
5755
5756   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
5757   if (attr_address_class)
5758     addr_class = DW_UNSND (attr_address_class);
5759   else
5760     addr_class = DW_ADDR_none;
5761
5762   /* If the pointer size or address class is different than the
5763      default, create a type variant marked as such and set the
5764      length accordingly.  */
5765   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
5766     {
5767       if (gdbarch_address_class_type_flags_p (gdbarch))
5768         {
5769           int type_flags;
5770
5771           type_flags = gdbarch_address_class_type_flags
5772                          (gdbarch, byte_size, addr_class);
5773           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5774                       == 0);
5775           type = make_type_with_address_space (type, type_flags);
5776         }
5777       else if (TYPE_LENGTH (type) != byte_size)
5778         {
5779           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
5780         }
5781       else {
5782         /* Should we also complain about unhandled address classes?  */
5783       }
5784     }
5785
5786   TYPE_LENGTH (type) = byte_size;
5787   return set_die_type (die, type, cu);
5788 }
5789
5790 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
5791    the user defined type vector.  */
5792
5793 static struct type *
5794 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
5795 {
5796   struct objfile *objfile = cu->objfile;
5797   struct type *type;
5798   struct type *to_type;
5799   struct type *domain;
5800
5801   to_type = die_type (die, cu);
5802   domain = die_containing_type (die, cu);
5803
5804   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
5805     type = lookup_methodptr_type (to_type);
5806   else
5807     type = lookup_memberptr_type (to_type, domain);
5808
5809   return set_die_type (die, type, cu);
5810 }
5811
5812 /* Extract all information from a DW_TAG_reference_type DIE and add to
5813    the user defined type vector.  */
5814
5815 static struct type *
5816 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
5817 {
5818   struct comp_unit_head *cu_header = &cu->header;
5819   struct type *type;
5820   struct attribute *attr;
5821
5822   type = lookup_reference_type (die_type (die, cu));
5823   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5824   if (attr)
5825     {
5826       TYPE_LENGTH (type) = DW_UNSND (attr);
5827     }
5828   else
5829     {
5830       TYPE_LENGTH (type) = cu_header->addr_size;
5831     }
5832   return set_die_type (die, type, cu);
5833 }
5834
5835 static struct type *
5836 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
5837 {
5838   struct type *base_type, *cv_type;
5839
5840   base_type = die_type (die, cu);
5841   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
5842   return set_die_type (die, cv_type, cu);
5843 }
5844
5845 static struct type *
5846 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
5847 {
5848   struct type *base_type, *cv_type;
5849
5850   base_type = die_type (die, cu);
5851   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
5852   return set_die_type (die, cv_type, cu);
5853 }
5854
5855 /* Extract all information from a DW_TAG_string_type DIE and add to
5856    the user defined type vector.  It isn't really a user defined type,
5857    but it behaves like one, with other DIE's using an AT_user_def_type
5858    attribute to reference it.  */
5859
5860 static struct type *
5861 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
5862 {
5863   struct objfile *objfile = cu->objfile;
5864   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5865   struct type *type, *range_type, *index_type, *char_type;
5866   struct attribute *attr;
5867   unsigned int length;
5868
5869   attr = dwarf2_attr (die, DW_AT_string_length, cu);
5870   if (attr)
5871     {
5872       length = DW_UNSND (attr);
5873     }
5874   else
5875     {
5876       /* check for the DW_AT_byte_size attribute */
5877       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5878       if (attr)
5879         {
5880           length = DW_UNSND (attr);
5881         }
5882       else
5883         {
5884           length = 1;
5885         }
5886     }
5887
5888   index_type = objfile_type (objfile)->builtin_int;
5889   range_type = create_range_type (NULL, index_type, 1, length);
5890   char_type = language_string_char_type (cu->language_defn, gdbarch);
5891   type = create_string_type (NULL, char_type, range_type);
5892
5893   return set_die_type (die, type, cu);
5894 }
5895
5896 /* Handle DIES due to C code like:
5897
5898    struct foo
5899    {
5900    int (*funcp)(int a, long l);
5901    int b;
5902    };
5903
5904    ('funcp' generates a DW_TAG_subroutine_type DIE)
5905  */
5906
5907 static struct type *
5908 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
5909 {
5910   struct type *type;            /* Type that this function returns */
5911   struct type *ftype;           /* Function that returns above type */
5912   struct attribute *attr;
5913
5914   type = die_type (die, cu);
5915   ftype = lookup_function_type (type);
5916
5917   /* All functions in C++, Pascal and Java have prototypes.  */
5918   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
5919   if ((attr && (DW_UNSND (attr) != 0))
5920       || cu->language == language_cplus
5921       || cu->language == language_java
5922       || cu->language == language_pascal)
5923     TYPE_PROTOTYPED (ftype) = 1;
5924   else if (producer_is_realview (cu->producer))
5925     /* RealView does not emit DW_AT_prototyped.  We can not
5926        distinguish prototyped and unprototyped functions; default to
5927        prototyped, since that is more common in modern code (and
5928        RealView warns about unprototyped functions).  */
5929     TYPE_PROTOTYPED (ftype) = 1;
5930
5931   /* Store the calling convention in the type if it's available in
5932      the subroutine die.  Otherwise set the calling convention to
5933      the default value DW_CC_normal.  */
5934   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5935   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
5936
5937   /* We need to add the subroutine type to the die immediately so
5938      we don't infinitely recurse when dealing with parameters
5939      declared as the same subroutine type. */
5940   set_die_type (die, ftype, cu);
5941   
5942   if (die->child != NULL)
5943     {
5944       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
5945       struct die_info *child_die;
5946       int nparams, iparams;
5947
5948       /* Count the number of parameters.
5949          FIXME: GDB currently ignores vararg functions, but knows about
5950          vararg member functions.  */
5951       nparams = 0;
5952       child_die = die->child;
5953       while (child_die && child_die->tag)
5954         {
5955           if (child_die->tag == DW_TAG_formal_parameter)
5956             nparams++;
5957           else if (child_die->tag == DW_TAG_unspecified_parameters)
5958             TYPE_VARARGS (ftype) = 1;
5959           child_die = sibling_die (child_die);
5960         }
5961
5962       /* Allocate storage for parameters and fill them in.  */
5963       TYPE_NFIELDS (ftype) = nparams;
5964       TYPE_FIELDS (ftype) = (struct field *)
5965         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
5966
5967       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
5968          even if we error out during the parameters reading below.  */
5969       for (iparams = 0; iparams < nparams; iparams++)
5970         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
5971
5972       iparams = 0;
5973       child_die = die->child;
5974       while (child_die && child_die->tag)
5975         {
5976           if (child_die->tag == DW_TAG_formal_parameter)
5977             {
5978               /* Dwarf2 has no clean way to discern C++ static and non-static
5979                  member functions. G++ helps GDB by marking the first
5980                  parameter for non-static member functions (which is the
5981                  this pointer) as artificial. We pass this information
5982                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
5983               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
5984               if (attr)
5985                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5986               else
5987                 {
5988                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
5989
5990                   /* GCC/43521: In java, the formal parameter
5991                      "this" is sometimes not marked with DW_AT_artificial.  */
5992                   if (cu->language == language_java)
5993                     {
5994                       const char *name = dwarf2_name (child_die, cu);
5995                       if (name && !strcmp (name, "this"))
5996                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
5997                     }
5998                 }
5999               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
6000               iparams++;
6001             }
6002           child_die = sibling_die (child_die);
6003         }
6004     }
6005
6006   return ftype;
6007 }
6008
6009 static struct type *
6010 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
6011 {
6012   struct objfile *objfile = cu->objfile;
6013   struct attribute *attr;
6014   const char *name = NULL;
6015   struct type *this_type;
6016
6017   name = dwarf2_full_name (NULL, die, cu);
6018   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
6019                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
6020   TYPE_NAME (this_type) = (char *) name;
6021   set_die_type (die, this_type, cu);
6022   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
6023   return this_type;
6024 }
6025
6026 /* Find a representation of a given base type and install
6027    it in the TYPE field of the die.  */
6028
6029 static struct type *
6030 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
6031 {
6032   struct objfile *objfile = cu->objfile;
6033   struct type *type;
6034   struct attribute *attr;
6035   int encoding = 0, size = 0;
6036   char *name;
6037   enum type_code code = TYPE_CODE_INT;
6038   int type_flags = 0;
6039   struct type *target_type = NULL;
6040
6041   attr = dwarf2_attr (die, DW_AT_encoding, cu);
6042   if (attr)
6043     {
6044       encoding = DW_UNSND (attr);
6045     }
6046   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6047   if (attr)
6048     {
6049       size = DW_UNSND (attr);
6050     }
6051   name = dwarf2_name (die, cu);
6052   if (!name)
6053     {
6054       complaint (&symfile_complaints,
6055                  _("DW_AT_name missing from DW_TAG_base_type"));
6056     }
6057
6058   switch (encoding)
6059     {
6060       case DW_ATE_address:
6061         /* Turn DW_ATE_address into a void * pointer.  */
6062         code = TYPE_CODE_PTR;
6063         type_flags |= TYPE_FLAG_UNSIGNED;
6064         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
6065         break;
6066       case DW_ATE_boolean:
6067         code = TYPE_CODE_BOOL;
6068         type_flags |= TYPE_FLAG_UNSIGNED;
6069         break;
6070       case DW_ATE_complex_float:
6071         code = TYPE_CODE_COMPLEX;
6072         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
6073         break;
6074       case DW_ATE_decimal_float:
6075         code = TYPE_CODE_DECFLOAT;
6076         break;
6077       case DW_ATE_float:
6078         code = TYPE_CODE_FLT;
6079         break;
6080       case DW_ATE_signed:
6081         break;
6082       case DW_ATE_unsigned:
6083         type_flags |= TYPE_FLAG_UNSIGNED;
6084         break;
6085       case DW_ATE_signed_char:
6086         if (cu->language == language_ada || cu->language == language_m2 
6087             || cu->language == language_pascal)
6088           code = TYPE_CODE_CHAR;
6089         break;
6090       case DW_ATE_unsigned_char:
6091         if (cu->language == language_ada || cu->language == language_m2
6092             || cu->language == language_pascal)
6093           code = TYPE_CODE_CHAR;
6094         type_flags |= TYPE_FLAG_UNSIGNED;
6095         break;
6096       default:
6097         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
6098                    dwarf_type_encoding_name (encoding));
6099         break;
6100     }
6101
6102   type = init_type (code, size, type_flags, NULL, objfile);
6103   TYPE_NAME (type) = name;
6104   TYPE_TARGET_TYPE (type) = target_type;
6105
6106   if (name && strcmp (name, "char") == 0)
6107     TYPE_NOSIGN (type) = 1;
6108
6109   return set_die_type (die, type, cu);
6110 }
6111
6112 /* Read the given DW_AT_subrange DIE.  */
6113
6114 static struct type *
6115 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
6116 {
6117   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
6118   struct type *base_type;
6119   struct type *range_type;
6120   struct attribute *attr;
6121   LONGEST low = 0;
6122   LONGEST high = -1;
6123   char *name;
6124   LONGEST negative_mask;
6125   
6126   base_type = die_type (die, cu);
6127   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
6128     {
6129       complaint (&symfile_complaints,
6130                 _("DW_AT_type missing from DW_TAG_subrange_type"));
6131       base_type
6132         = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6133                      0, NULL, cu->objfile);
6134     }
6135
6136   if (cu->language == language_fortran)
6137     { 
6138       /* FORTRAN implies a lower bound of 1, if not given.  */
6139       low = 1;
6140     }
6141
6142   /* FIXME: For variable sized arrays either of these could be
6143      a variable rather than a constant value.  We'll allow it,
6144      but we don't know how to handle it.  */
6145   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
6146   if (attr)
6147     low = dwarf2_get_attr_constant_value (attr, 0);
6148
6149   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
6150   if (attr)
6151     {       
6152       if (attr->form == DW_FORM_block1)
6153         {
6154           /* GCC encodes arrays with unspecified or dynamic length
6155              with a DW_FORM_block1 attribute.
6156              FIXME: GDB does not yet know how to handle dynamic
6157              arrays properly, treat them as arrays with unspecified
6158              length for now.
6159
6160              FIXME: jimb/2003-09-22: GDB does not really know
6161              how to handle arrays of unspecified length
6162              either; we just represent them as zero-length
6163              arrays.  Choose an appropriate upper bound given
6164              the lower bound we've computed above.  */
6165           high = low - 1;
6166         }
6167       else
6168         high = dwarf2_get_attr_constant_value (attr, 1);
6169     }
6170
6171   negative_mask = 
6172     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
6173   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
6174     low |= negative_mask;
6175   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
6176     high |= negative_mask;
6177
6178   range_type = create_range_type (NULL, base_type, low, high);
6179
6180   /* Mark arrays with dynamic length at least as an array of unspecified
6181      length.  GDB could check the boundary but before it gets implemented at
6182      least allow accessing the array elements.  */
6183   if (attr && attr->form == DW_FORM_block1)
6184     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
6185
6186   name = dwarf2_name (die, cu);
6187   if (name)
6188     TYPE_NAME (range_type) = name;
6189   
6190   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6191   if (attr)
6192     TYPE_LENGTH (range_type) = DW_UNSND (attr);
6193
6194   set_descriptive_type (range_type, die, cu);
6195
6196   return set_die_type (die, range_type, cu);
6197 }
6198   
6199 static struct type *
6200 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
6201 {
6202   struct type *type;
6203
6204   /* For now, we only support the C meaning of an unspecified type: void.  */
6205
6206   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
6207   TYPE_NAME (type) = dwarf2_name (die, cu);
6208
6209   return set_die_type (die, type, cu);
6210 }
6211
6212 /* Trivial hash function for die_info: the hash value of a DIE
6213    is its offset in .debug_info for this objfile.  */
6214
6215 static hashval_t
6216 die_hash (const void *item)
6217 {
6218   const struct die_info *die = item;
6219   return die->offset;
6220 }
6221
6222 /* Trivial comparison function for die_info structures: two DIEs
6223    are equal if they have the same offset.  */
6224
6225 static int
6226 die_eq (const void *item_lhs, const void *item_rhs)
6227 {
6228   const struct die_info *die_lhs = item_lhs;
6229   const struct die_info *die_rhs = item_rhs;
6230   return die_lhs->offset == die_rhs->offset;
6231 }
6232
6233 /* Read a whole compilation unit into a linked list of dies.  */
6234
6235 static struct die_info *
6236 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
6237 {
6238   struct die_reader_specs reader_specs;
6239
6240   gdb_assert (cu->die_hash == NULL);
6241   cu->die_hash
6242     = htab_create_alloc_ex (cu->header.length / 12,
6243                             die_hash,
6244                             die_eq,
6245                             NULL,
6246                             &cu->comp_unit_obstack,
6247                             hashtab_obstack_allocate,
6248                             dummy_obstack_deallocate);
6249
6250   init_cu_die_reader (&reader_specs, cu);
6251
6252   return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
6253 }
6254
6255 /* Main entry point for reading a DIE and all children.
6256    Read the DIE and dump it if requested.  */
6257
6258 static struct die_info *
6259 read_die_and_children (const struct die_reader_specs *reader,
6260                        gdb_byte *info_ptr,
6261                        gdb_byte **new_info_ptr,
6262                        struct die_info *parent)
6263 {
6264   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
6265                                                      new_info_ptr, parent);
6266
6267   if (dwarf2_die_debug)
6268     {
6269       fprintf_unfiltered (gdb_stdlog,
6270                           "\nRead die from %s of %s:\n",
6271                           reader->buffer == dwarf2_per_objfile->info.buffer
6272                           ? ".debug_info"
6273                           : reader->buffer == dwarf2_per_objfile->types.buffer
6274                           ? ".debug_types"
6275                           : "unknown section",
6276                           reader->abfd->filename);
6277       dump_die (result, dwarf2_die_debug);
6278     }
6279
6280   return result;
6281 }
6282
6283 /* Read a single die and all its descendents.  Set the die's sibling
6284    field to NULL; set other fields in the die correctly, and set all
6285    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
6286    location of the info_ptr after reading all of those dies.  PARENT
6287    is the parent of the die in question.  */
6288
6289 static struct die_info *
6290 read_die_and_children_1 (const struct die_reader_specs *reader,
6291                          gdb_byte *info_ptr,
6292                          gdb_byte **new_info_ptr,
6293                          struct die_info *parent)
6294 {
6295   struct die_info *die;
6296   gdb_byte *cur_ptr;
6297   int has_children;
6298
6299   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
6300   if (die == NULL)
6301     {
6302       *new_info_ptr = cur_ptr;
6303       return NULL;
6304     }
6305   store_in_ref_table (die, reader->cu);
6306
6307   if (has_children)
6308     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
6309   else
6310     {
6311       die->child = NULL;
6312       *new_info_ptr = cur_ptr;
6313     }
6314
6315   die->sibling = NULL;
6316   die->parent = parent;
6317   return die;
6318 }
6319
6320 /* Read a die, all of its descendents, and all of its siblings; set
6321    all of the fields of all of the dies correctly.  Arguments are as
6322    in read_die_and_children.  */
6323
6324 static struct die_info *
6325 read_die_and_siblings (const struct die_reader_specs *reader,
6326                        gdb_byte *info_ptr,
6327                        gdb_byte **new_info_ptr,
6328                        struct die_info *parent)
6329 {
6330   struct die_info *first_die, *last_sibling;
6331   gdb_byte *cur_ptr;
6332
6333   cur_ptr = info_ptr;
6334   first_die = last_sibling = NULL;
6335
6336   while (1)
6337     {
6338       struct die_info *die
6339         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
6340
6341       if (die == NULL)
6342         {
6343           *new_info_ptr = cur_ptr;
6344           return first_die;
6345         }
6346
6347       if (!first_die)
6348         first_die = die;
6349       else
6350         last_sibling->sibling = die;
6351
6352       last_sibling = die;
6353     }
6354 }
6355
6356 /* Read the die from the .debug_info section buffer.  Set DIEP to
6357    point to a newly allocated die with its information, except for its
6358    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
6359    whether the die has children or not.  */
6360
6361 static gdb_byte *
6362 read_full_die (const struct die_reader_specs *reader,
6363                struct die_info **diep, gdb_byte *info_ptr,
6364                int *has_children)
6365 {
6366   unsigned int abbrev_number, bytes_read, i, offset;
6367   struct abbrev_info *abbrev;
6368   struct die_info *die;
6369   struct dwarf2_cu *cu = reader->cu;
6370   bfd *abfd = reader->abfd;
6371
6372   offset = info_ptr - reader->buffer;
6373   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6374   info_ptr += bytes_read;
6375   if (!abbrev_number)
6376     {
6377       *diep = NULL;
6378       *has_children = 0;
6379       return info_ptr;
6380     }
6381
6382   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6383   if (!abbrev)
6384     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6385            abbrev_number,
6386            bfd_get_filename (abfd));
6387
6388   die = dwarf_alloc_die (cu, abbrev->num_attrs);
6389   die->offset = offset;
6390   die->tag = abbrev->tag;
6391   die->abbrev = abbrev_number;
6392
6393   die->num_attrs = abbrev->num_attrs;
6394
6395   for (i = 0; i < abbrev->num_attrs; ++i)
6396     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6397                                abfd, info_ptr, cu);
6398
6399   *diep = die;
6400   *has_children = abbrev->has_children;
6401   return info_ptr;
6402 }
6403
6404 /* In DWARF version 2, the description of the debugging information is
6405    stored in a separate .debug_abbrev section.  Before we read any
6406    dies from a section we read in all abbreviations and install them
6407    in a hash table.  This function also sets flags in CU describing
6408    the data found in the abbrev table.  */
6409
6410 static void
6411 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
6412 {
6413   struct comp_unit_head *cu_header = &cu->header;
6414   gdb_byte *abbrev_ptr;
6415   struct abbrev_info *cur_abbrev;
6416   unsigned int abbrev_number, bytes_read, abbrev_name;
6417   unsigned int abbrev_form, hash_number;
6418   struct attr_abbrev *cur_attrs;
6419   unsigned int allocated_attrs;
6420
6421   /* Initialize dwarf2 abbrevs */
6422   obstack_init (&cu->abbrev_obstack);
6423   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
6424                                       (ABBREV_HASH_SIZE
6425                                        * sizeof (struct abbrev_info *)));
6426   memset (cu->dwarf2_abbrevs, 0,
6427           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
6428
6429   dwarf2_read_section (dwarf2_per_objfile->objfile,
6430                        &dwarf2_per_objfile->abbrev);
6431   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
6432   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6433   abbrev_ptr += bytes_read;
6434
6435   allocated_attrs = ATTR_ALLOC_CHUNK;
6436   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6437   
6438   /* loop until we reach an abbrev number of 0 */
6439   while (abbrev_number)
6440     {
6441       cur_abbrev = dwarf_alloc_abbrev (cu);
6442
6443       /* read in abbrev header */
6444       cur_abbrev->number = abbrev_number;
6445       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6446       abbrev_ptr += bytes_read;
6447       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
6448       abbrev_ptr += 1;
6449
6450       if (cur_abbrev->tag == DW_TAG_namespace)
6451         cu->has_namespace_info = 1;
6452
6453       /* now read in declarations */
6454       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6455       abbrev_ptr += bytes_read;
6456       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6457       abbrev_ptr += bytes_read;
6458       while (abbrev_name)
6459         {
6460           if (cur_abbrev->num_attrs == allocated_attrs)
6461             {
6462               allocated_attrs += ATTR_ALLOC_CHUNK;
6463               cur_attrs
6464                 = xrealloc (cur_attrs, (allocated_attrs
6465                                         * sizeof (struct attr_abbrev)));
6466             }
6467
6468           /* Record whether this compilation unit might have
6469              inter-compilation-unit references.  If we don't know what form
6470              this attribute will have, then it might potentially be a
6471              DW_FORM_ref_addr, so we conservatively expect inter-CU
6472              references.  */
6473
6474           if (abbrev_form == DW_FORM_ref_addr
6475               || abbrev_form == DW_FORM_indirect)
6476             cu->has_form_ref_addr = 1;
6477
6478           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
6479           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
6480           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6481           abbrev_ptr += bytes_read;
6482           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6483           abbrev_ptr += bytes_read;
6484         }
6485
6486       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
6487                                          (cur_abbrev->num_attrs
6488                                           * sizeof (struct attr_abbrev)));
6489       memcpy (cur_abbrev->attrs, cur_attrs,
6490               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
6491
6492       hash_number = abbrev_number % ABBREV_HASH_SIZE;
6493       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
6494       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
6495
6496       /* Get next abbreviation.
6497          Under Irix6 the abbreviations for a compilation unit are not
6498          always properly terminated with an abbrev number of 0.
6499          Exit loop if we encounter an abbreviation which we have
6500          already read (which means we are about to read the abbreviations
6501          for the next compile unit) or if the end of the abbreviation
6502          table is reached.  */
6503       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
6504           >= dwarf2_per_objfile->abbrev.size)
6505         break;
6506       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6507       abbrev_ptr += bytes_read;
6508       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
6509         break;
6510     }
6511
6512   xfree (cur_attrs);
6513 }
6514
6515 /* Release the memory used by the abbrev table for a compilation unit.  */
6516
6517 static void
6518 dwarf2_free_abbrev_table (void *ptr_to_cu)
6519 {
6520   struct dwarf2_cu *cu = ptr_to_cu;
6521
6522   obstack_free (&cu->abbrev_obstack, NULL);
6523   cu->dwarf2_abbrevs = NULL;
6524 }
6525
6526 /* Lookup an abbrev_info structure in the abbrev hash table.  */
6527
6528 static struct abbrev_info *
6529 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
6530 {
6531   unsigned int hash_number;
6532   struct abbrev_info *abbrev;
6533
6534   hash_number = number % ABBREV_HASH_SIZE;
6535   abbrev = cu->dwarf2_abbrevs[hash_number];
6536
6537   while (abbrev)
6538     {
6539       if (abbrev->number == number)
6540         return abbrev;
6541       else
6542         abbrev = abbrev->next;
6543     }
6544   return NULL;
6545 }
6546
6547 /* Returns nonzero if TAG represents a type that we might generate a partial
6548    symbol for.  */
6549
6550 static int
6551 is_type_tag_for_partial (int tag)
6552 {
6553   switch (tag)
6554     {
6555 #if 0
6556     /* Some types that would be reasonable to generate partial symbols for,
6557        that we don't at present.  */
6558     case DW_TAG_array_type:
6559     case DW_TAG_file_type:
6560     case DW_TAG_ptr_to_member_type:
6561     case DW_TAG_set_type:
6562     case DW_TAG_string_type:
6563     case DW_TAG_subroutine_type:
6564 #endif
6565     case DW_TAG_base_type:
6566     case DW_TAG_class_type:
6567     case DW_TAG_interface_type:
6568     case DW_TAG_enumeration_type:
6569     case DW_TAG_structure_type:
6570     case DW_TAG_subrange_type:
6571     case DW_TAG_typedef:
6572     case DW_TAG_union_type:
6573       return 1;
6574     default:
6575       return 0;
6576     }
6577 }
6578
6579 /* Load all DIEs that are interesting for partial symbols into memory.  */
6580
6581 static struct partial_die_info *
6582 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
6583                    int building_psymtab, struct dwarf2_cu *cu)
6584 {
6585   struct partial_die_info *part_die;
6586   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
6587   struct abbrev_info *abbrev;
6588   unsigned int bytes_read;
6589   unsigned int load_all = 0;
6590
6591   int nesting_level = 1;
6592
6593   parent_die = NULL;
6594   last_die = NULL;
6595
6596   if (cu->per_cu && cu->per_cu->load_all_dies)
6597     load_all = 1;
6598
6599   cu->partial_dies
6600     = htab_create_alloc_ex (cu->header.length / 12,
6601                             partial_die_hash,
6602                             partial_die_eq,
6603                             NULL,
6604                             &cu->comp_unit_obstack,
6605                             hashtab_obstack_allocate,
6606                             dummy_obstack_deallocate);
6607
6608   part_die = obstack_alloc (&cu->comp_unit_obstack,
6609                             sizeof (struct partial_die_info));
6610
6611   while (1)
6612     {
6613       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6614
6615       /* A NULL abbrev means the end of a series of children.  */
6616       if (abbrev == NULL)
6617         {
6618           if (--nesting_level == 0)
6619             {
6620               /* PART_DIE was probably the last thing allocated on the
6621                  comp_unit_obstack, so we could call obstack_free
6622                  here.  We don't do that because the waste is small,
6623                  and will be cleaned up when we're done with this
6624                  compilation unit.  This way, we're also more robust
6625                  against other users of the comp_unit_obstack.  */
6626               return first_die;
6627             }
6628           info_ptr += bytes_read;
6629           last_die = parent_die;
6630           parent_die = parent_die->die_parent;
6631           continue;
6632         }
6633
6634       /* Check whether this DIE is interesting enough to save.  Normally
6635          we would not be interested in members here, but there may be
6636          later variables referencing them via DW_AT_specification (for
6637          static members).  */
6638       if (!load_all
6639           && !is_type_tag_for_partial (abbrev->tag)
6640           && abbrev->tag != DW_TAG_enumerator
6641           && abbrev->tag != DW_TAG_subprogram
6642           && abbrev->tag != DW_TAG_lexical_block
6643           && abbrev->tag != DW_TAG_variable
6644           && abbrev->tag != DW_TAG_namespace
6645           && abbrev->tag != DW_TAG_member)
6646         {
6647           /* Otherwise we skip to the next sibling, if any.  */
6648           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
6649           continue;
6650         }
6651
6652       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
6653                                    buffer, info_ptr, cu);
6654
6655       /* This two-pass algorithm for processing partial symbols has a
6656          high cost in cache pressure.  Thus, handle some simple cases
6657          here which cover the majority of C partial symbols.  DIEs
6658          which neither have specification tags in them, nor could have
6659          specification tags elsewhere pointing at them, can simply be
6660          processed and discarded.
6661
6662          This segment is also optional; scan_partial_symbols and
6663          add_partial_symbol will handle these DIEs if we chain
6664          them in normally.  When compilers which do not emit large
6665          quantities of duplicate debug information are more common,
6666          this code can probably be removed.  */
6667
6668       /* Any complete simple types at the top level (pretty much all
6669          of them, for a language without namespaces), can be processed
6670          directly.  */
6671       if (parent_die == NULL
6672           && part_die->has_specification == 0
6673           && part_die->is_declaration == 0
6674           && (part_die->tag == DW_TAG_typedef
6675               || part_die->tag == DW_TAG_base_type
6676               || part_die->tag == DW_TAG_subrange_type))
6677         {
6678           if (building_psymtab && part_die->name != NULL)
6679             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
6680                                  VAR_DOMAIN, LOC_TYPEDEF,
6681                                  &cu->objfile->static_psymbols,
6682                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
6683           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
6684           continue;
6685         }
6686
6687       /* If we're at the second level, and we're an enumerator, and
6688          our parent has no specification (meaning possibly lives in a
6689          namespace elsewhere), then we can add the partial symbol now
6690          instead of queueing it.  */
6691       if (part_die->tag == DW_TAG_enumerator
6692           && parent_die != NULL
6693           && parent_die->die_parent == NULL
6694           && parent_die->tag == DW_TAG_enumeration_type
6695           && parent_die->has_specification == 0)
6696         {
6697           if (part_die->name == NULL)
6698             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6699           else if (building_psymtab)
6700             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
6701                                  VAR_DOMAIN, LOC_CONST,
6702                                  (cu->language == language_cplus
6703                                   || cu->language == language_java)
6704                                  ? &cu->objfile->global_psymbols
6705                                  : &cu->objfile->static_psymbols,
6706                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
6707
6708           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
6709           continue;
6710         }
6711
6712       /* We'll save this DIE so link it in.  */
6713       part_die->die_parent = parent_die;
6714       part_die->die_sibling = NULL;
6715       part_die->die_child = NULL;
6716
6717       if (last_die && last_die == parent_die)
6718         last_die->die_child = part_die;
6719       else if (last_die)
6720         last_die->die_sibling = part_die;
6721
6722       last_die = part_die;
6723
6724       if (first_die == NULL)
6725         first_die = part_die;
6726
6727       /* Maybe add the DIE to the hash table.  Not all DIEs that we
6728          find interesting need to be in the hash table, because we
6729          also have the parent/sibling/child chains; only those that we
6730          might refer to by offset later during partial symbol reading.
6731
6732          For now this means things that might have be the target of a
6733          DW_AT_specification, DW_AT_abstract_origin, or
6734          DW_AT_extension.  DW_AT_extension will refer only to
6735          namespaces; DW_AT_abstract_origin refers to functions (and
6736          many things under the function DIE, but we do not recurse
6737          into function DIEs during partial symbol reading) and
6738          possibly variables as well; DW_AT_specification refers to
6739          declarations.  Declarations ought to have the DW_AT_declaration
6740          flag.  It happens that GCC forgets to put it in sometimes, but
6741          only for functions, not for types.
6742
6743          Adding more things than necessary to the hash table is harmless
6744          except for the performance cost.  Adding too few will result in
6745          wasted time in find_partial_die, when we reread the compilation
6746          unit with load_all_dies set.  */
6747
6748       if (load_all
6749           || abbrev->tag == DW_TAG_subprogram
6750           || abbrev->tag == DW_TAG_variable
6751           || abbrev->tag == DW_TAG_namespace
6752           || part_die->is_declaration)
6753         {
6754           void **slot;
6755
6756           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
6757                                            part_die->offset, INSERT);
6758           *slot = part_die;
6759         }
6760
6761       part_die = obstack_alloc (&cu->comp_unit_obstack,
6762                                 sizeof (struct partial_die_info));
6763
6764       /* For some DIEs we want to follow their children (if any).  For C
6765          we have no reason to follow the children of structures; for other
6766          languages we have to, both so that we can get at method physnames
6767          to infer fully qualified class names, and for DW_AT_specification.
6768
6769          For Ada, we need to scan the children of subprograms and lexical
6770          blocks as well because Ada allows the definition of nested
6771          entities that could be interesting for the debugger, such as
6772          nested subprograms for instance.  */
6773       if (last_die->has_children
6774           && (load_all
6775               || last_die->tag == DW_TAG_namespace
6776               || last_die->tag == DW_TAG_enumeration_type
6777               || (cu->language != language_c
6778                   && (last_die->tag == DW_TAG_class_type
6779                       || last_die->tag == DW_TAG_interface_type
6780                       || last_die->tag == DW_TAG_structure_type
6781                       || last_die->tag == DW_TAG_union_type))
6782               || (cu->language == language_ada
6783                   && (last_die->tag == DW_TAG_subprogram
6784                       || last_die->tag == DW_TAG_lexical_block))))
6785         {
6786           nesting_level++;
6787           parent_die = last_die;
6788           continue;
6789         }
6790
6791       /* Otherwise we skip to the next sibling, if any.  */
6792       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
6793
6794       /* Back to the top, do it again.  */
6795     }
6796 }
6797
6798 /* Read a minimal amount of information into the minimal die structure.  */
6799
6800 static gdb_byte *
6801 read_partial_die (struct partial_die_info *part_die,
6802                   struct abbrev_info *abbrev,
6803                   unsigned int abbrev_len, bfd *abfd,
6804                   gdb_byte *buffer, gdb_byte *info_ptr,
6805                   struct dwarf2_cu *cu)
6806 {
6807   unsigned int bytes_read, i;
6808   struct attribute attr;
6809   int has_low_pc_attr = 0;
6810   int has_high_pc_attr = 0;
6811
6812   memset (part_die, 0, sizeof (struct partial_die_info));
6813
6814   part_die->offset = info_ptr - buffer;
6815
6816   info_ptr += abbrev_len;
6817
6818   if (abbrev == NULL)
6819     return info_ptr;
6820
6821   part_die->tag = abbrev->tag;
6822   part_die->has_children = abbrev->has_children;
6823
6824   for (i = 0; i < abbrev->num_attrs; ++i)
6825     {
6826       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
6827
6828       /* Store the data if it is of an attribute we want to keep in a
6829          partial symbol table.  */
6830       switch (attr.name)
6831         {
6832         case DW_AT_name:
6833           switch (part_die->tag)
6834             {
6835             case DW_TAG_compile_unit:
6836             case DW_TAG_type_unit:
6837               /* Compilation units have a DW_AT_name that is a filename, not
6838                  a source language identifier.  */
6839             case DW_TAG_enumeration_type:
6840             case DW_TAG_enumerator:
6841               /* These tags always have simple identifiers already; no need
6842                  to canonicalize them.  */
6843               part_die->name = DW_STRING (&attr);
6844               break;
6845             default:
6846               part_die->name
6847                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
6848                                             &cu->objfile->objfile_obstack);
6849               break;
6850             }
6851           break;
6852         case DW_AT_MIPS_linkage_name:
6853           if (cu->language == language_ada)
6854             part_die->name = DW_STRING (&attr);
6855           break;
6856         case DW_AT_low_pc:
6857           has_low_pc_attr = 1;
6858           part_die->lowpc = DW_ADDR (&attr);
6859           break;
6860         case DW_AT_high_pc:
6861           has_high_pc_attr = 1;
6862           part_die->highpc = DW_ADDR (&attr);
6863           break;
6864         case DW_AT_location:
6865           /* Support the .debug_loc offsets */
6866           if (attr_form_is_block (&attr))
6867             {
6868                part_die->locdesc = DW_BLOCK (&attr);
6869             }
6870           else if (attr_form_is_section_offset (&attr))
6871             {
6872               dwarf2_complex_location_expr_complaint ();
6873             }
6874           else
6875             {
6876               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6877                                                      "partial symbol information");
6878             }
6879           break;
6880         case DW_AT_external:
6881           part_die->is_external = DW_UNSND (&attr);
6882           break;
6883         case DW_AT_declaration:
6884           part_die->is_declaration = DW_UNSND (&attr);
6885           break;
6886         case DW_AT_type:
6887           part_die->has_type = 1;
6888           break;
6889         case DW_AT_abstract_origin:
6890         case DW_AT_specification:
6891         case DW_AT_extension:
6892           part_die->has_specification = 1;
6893           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
6894           break;
6895         case DW_AT_sibling:
6896           /* Ignore absolute siblings, they might point outside of
6897              the current compile unit.  */
6898           if (attr.form == DW_FORM_ref_addr)
6899             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
6900           else
6901             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
6902           break;
6903         case DW_AT_byte_size:
6904           part_die->has_byte_size = 1;
6905           break;
6906         case DW_AT_calling_convention:
6907           /* DWARF doesn't provide a way to identify a program's source-level
6908              entry point.  DW_AT_calling_convention attributes are only meant
6909              to describe functions' calling conventions.
6910
6911              However, because it's a necessary piece of information in
6912              Fortran, and because DW_CC_program is the only piece of debugging
6913              information whose definition refers to a 'main program' at all,
6914              several compilers have begun marking Fortran main programs with
6915              DW_CC_program --- even when those functions use the standard
6916              calling conventions.
6917
6918              So until DWARF specifies a way to provide this information and
6919              compilers pick up the new representation, we'll support this
6920              practice.  */
6921           if (DW_UNSND (&attr) == DW_CC_program
6922               && cu->language == language_fortran)
6923             set_main_name (part_die->name);
6924           break;
6925         default:
6926           break;
6927         }
6928     }
6929
6930   /* When using the GNU linker, .gnu.linkonce. sections are used to
6931      eliminate duplicate copies of functions and vtables and such.
6932      The linker will arbitrarily choose one and discard the others.
6933      The AT_*_pc values for such functions refer to local labels in
6934      these sections.  If the section from that file was discarded, the
6935      labels are not in the output, so the relocs get a value of 0.
6936      If this is a discarded function, mark the pc bounds as invalid,
6937      so that GDB will ignore it.  */
6938   if (has_low_pc_attr && has_high_pc_attr
6939       && part_die->lowpc < part_die->highpc
6940       && (part_die->lowpc != 0
6941           || dwarf2_per_objfile->has_section_at_zero))
6942     part_die->has_pc_info = 1;
6943
6944   return info_ptr;
6945 }
6946
6947 /* Find a cached partial DIE at OFFSET in CU.  */
6948
6949 static struct partial_die_info *
6950 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
6951 {
6952   struct partial_die_info *lookup_die = NULL;
6953   struct partial_die_info part_die;
6954
6955   part_die.offset = offset;
6956   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6957
6958   return lookup_die;
6959 }
6960
6961 /* Find a partial DIE at OFFSET, which may or may not be in CU,
6962    except in the case of .debug_types DIEs which do not reference
6963    outside their CU (they do however referencing other types via
6964    DW_FORM_sig8).  */
6965
6966 static struct partial_die_info *
6967 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
6968 {
6969   struct dwarf2_per_cu_data *per_cu = NULL;
6970   struct partial_die_info *pd = NULL;
6971
6972   if (cu->per_cu->from_debug_types)
6973     {
6974       pd = find_partial_die_in_comp_unit (offset, cu);
6975       if (pd != NULL)
6976         return pd;
6977       goto not_found;
6978     }
6979
6980   if (offset_in_cu_p (&cu->header, offset))
6981     {
6982       pd = find_partial_die_in_comp_unit (offset, cu);
6983       if (pd != NULL)
6984         return pd;
6985     }
6986
6987   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6988
6989   if (per_cu->cu == NULL)
6990     {
6991       load_partial_comp_unit (per_cu, cu->objfile);
6992       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6993       dwarf2_per_objfile->read_in_chain = per_cu;
6994     }
6995
6996   per_cu->cu->last_used = 0;
6997   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6998
6999   if (pd == NULL && per_cu->load_all_dies == 0)
7000     {
7001       struct cleanup *back_to;
7002       struct partial_die_info comp_unit_die;
7003       struct abbrev_info *abbrev;
7004       unsigned int bytes_read;
7005       char *info_ptr;
7006
7007       per_cu->load_all_dies = 1;
7008
7009       /* Re-read the DIEs.  */
7010       back_to = make_cleanup (null_cleanup, 0);
7011       if (per_cu->cu->dwarf2_abbrevs == NULL)
7012         {
7013           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
7014           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
7015         }
7016       info_ptr = (dwarf2_per_objfile->info.buffer
7017                   + per_cu->cu->header.offset
7018                   + per_cu->cu->header.first_die_offset);
7019       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
7020       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
7021                                    per_cu->cu->objfile->obfd,
7022                                    dwarf2_per_objfile->info.buffer, info_ptr,
7023                                    per_cu->cu);
7024       if (comp_unit_die.has_children)
7025         load_partial_dies (per_cu->cu->objfile->obfd,
7026                            dwarf2_per_objfile->info.buffer, info_ptr,
7027                            0, per_cu->cu);
7028       do_cleanups (back_to);
7029
7030       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
7031     }
7032
7033  not_found:
7034
7035   if (pd == NULL)
7036     internal_error (__FILE__, __LINE__,
7037                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
7038                     offset, bfd_get_filename (cu->objfile->obfd));
7039   return pd;
7040 }
7041
7042 /* Adjust PART_DIE before generating a symbol for it.  This function
7043    may set the is_external flag or change the DIE's name.  */
7044
7045 static void
7046 fixup_partial_die (struct partial_die_info *part_die,
7047                    struct dwarf2_cu *cu)
7048 {
7049   /* If we found a reference attribute and the DIE has no name, try
7050      to find a name in the referred to DIE.  */
7051
7052   if (part_die->name == NULL && part_die->has_specification)
7053     {
7054       struct partial_die_info *spec_die;
7055
7056       spec_die = find_partial_die (part_die->spec_offset, cu);
7057
7058       fixup_partial_die (spec_die, cu);
7059
7060       if (spec_die->name)
7061         {
7062           part_die->name = spec_die->name;
7063
7064           /* Copy DW_AT_external attribute if it is set.  */
7065           if (spec_die->is_external)
7066             part_die->is_external = spec_die->is_external;
7067         }
7068     }
7069
7070   /* Set default names for some unnamed DIEs.  */
7071   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
7072                                  || part_die->tag == DW_TAG_class_type))
7073     part_die->name = "(anonymous class)";
7074
7075   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
7076     part_die->name = "(anonymous namespace)";
7077
7078   if (part_die->tag == DW_TAG_structure_type
7079       || part_die->tag == DW_TAG_class_type
7080       || part_die->tag == DW_TAG_union_type)
7081     guess_structure_name (part_die, cu);
7082 }
7083
7084 /* Read an attribute value described by an attribute form.  */
7085
7086 static gdb_byte *
7087 read_attribute_value (struct attribute *attr, unsigned form,
7088                       bfd *abfd, gdb_byte *info_ptr,
7089                       struct dwarf2_cu *cu)
7090 {
7091   struct comp_unit_head *cu_header = &cu->header;
7092   unsigned int bytes_read;
7093   struct dwarf_block *blk;
7094
7095   attr->form = form;
7096   switch (form)
7097     {
7098     case DW_FORM_ref_addr:
7099       if (cu->header.version == 2)
7100         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7101       else
7102         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7103       info_ptr += bytes_read;
7104       break;
7105     case DW_FORM_addr:
7106       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7107       info_ptr += bytes_read;
7108       break;
7109     case DW_FORM_block2:
7110       blk = dwarf_alloc_block (cu);
7111       blk->size = read_2_bytes (abfd, info_ptr);
7112       info_ptr += 2;
7113       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7114       info_ptr += blk->size;
7115       DW_BLOCK (attr) = blk;
7116       break;
7117     case DW_FORM_block4:
7118       blk = dwarf_alloc_block (cu);
7119       blk->size = read_4_bytes (abfd, info_ptr);
7120       info_ptr += 4;
7121       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7122       info_ptr += blk->size;
7123       DW_BLOCK (attr) = blk;
7124       break;
7125     case DW_FORM_data2:
7126       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
7127       info_ptr += 2;
7128       break;
7129     case DW_FORM_data4:
7130       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
7131       info_ptr += 4;
7132       break;
7133     case DW_FORM_data8:
7134       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
7135       info_ptr += 8;
7136       break;
7137     case DW_FORM_sec_offset:
7138       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7139       info_ptr += bytes_read;
7140       break;
7141     case DW_FORM_string:
7142       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
7143       DW_STRING_IS_CANONICAL (attr) = 0;
7144       info_ptr += bytes_read;
7145       break;
7146     case DW_FORM_strp:
7147       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
7148                                                &bytes_read);
7149       DW_STRING_IS_CANONICAL (attr) = 0;
7150       info_ptr += bytes_read;
7151       break;
7152     case DW_FORM_exprloc:
7153     case DW_FORM_block:
7154       blk = dwarf_alloc_block (cu);
7155       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7156       info_ptr += bytes_read;
7157       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7158       info_ptr += blk->size;
7159       DW_BLOCK (attr) = blk;
7160       break;
7161     case DW_FORM_block1:
7162       blk = dwarf_alloc_block (cu);
7163       blk->size = read_1_byte (abfd, info_ptr);
7164       info_ptr += 1;
7165       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7166       info_ptr += blk->size;
7167       DW_BLOCK (attr) = blk;
7168       break;
7169     case DW_FORM_data1:
7170       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7171       info_ptr += 1;
7172       break;
7173     case DW_FORM_flag:
7174       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7175       info_ptr += 1;
7176       break;
7177     case DW_FORM_flag_present:
7178       DW_UNSND (attr) = 1;
7179       break;
7180     case DW_FORM_sdata:
7181       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
7182       info_ptr += bytes_read;
7183       break;
7184     case DW_FORM_udata:
7185       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7186       info_ptr += bytes_read;
7187       break;
7188     case DW_FORM_ref1:
7189       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
7190       info_ptr += 1;
7191       break;
7192     case DW_FORM_ref2:
7193       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
7194       info_ptr += 2;
7195       break;
7196     case DW_FORM_ref4:
7197       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
7198       info_ptr += 4;
7199       break;
7200     case DW_FORM_ref8:
7201       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
7202       info_ptr += 8;
7203       break;
7204     case DW_FORM_sig8:
7205       /* Convert the signature to something we can record in DW_UNSND
7206          for later lookup.
7207          NOTE: This is NULL if the type wasn't found.  */
7208       DW_SIGNATURED_TYPE (attr) =
7209         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
7210       info_ptr += 8;
7211       break;
7212     case DW_FORM_ref_udata:
7213       DW_ADDR (attr) = (cu->header.offset
7214                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
7215       info_ptr += bytes_read;
7216       break;
7217     case DW_FORM_indirect:
7218       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7219       info_ptr += bytes_read;
7220       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
7221       break;
7222     default:
7223       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
7224              dwarf_form_name (form),
7225              bfd_get_filename (abfd));
7226     }
7227
7228   /* We have seen instances where the compiler tried to emit a byte
7229      size attribute of -1 which ended up being encoded as an unsigned
7230      0xffffffff.  Although 0xffffffff is technically a valid size value,
7231      an object of this size seems pretty unlikely so we can relatively
7232      safely treat these cases as if the size attribute was invalid and
7233      treat them as zero by default.  */
7234   if (attr->name == DW_AT_byte_size
7235       && form == DW_FORM_data4
7236       && DW_UNSND (attr) >= 0xffffffff)
7237     {
7238       complaint
7239         (&symfile_complaints,
7240          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
7241          hex_string (DW_UNSND (attr)));
7242       DW_UNSND (attr) = 0;
7243     }
7244
7245   return info_ptr;
7246 }
7247
7248 /* Read an attribute described by an abbreviated attribute.  */
7249
7250 static gdb_byte *
7251 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
7252                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
7253 {
7254   attr->name = abbrev->name;
7255   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
7256 }
7257
7258 /* read dwarf information from a buffer */
7259
7260 static unsigned int
7261 read_1_byte (bfd *abfd, gdb_byte *buf)
7262 {
7263   return bfd_get_8 (abfd, buf);
7264 }
7265
7266 static int
7267 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
7268 {
7269   return bfd_get_signed_8 (abfd, buf);
7270 }
7271
7272 static unsigned int
7273 read_2_bytes (bfd *abfd, gdb_byte *buf)
7274 {
7275   return bfd_get_16 (abfd, buf);
7276 }
7277
7278 static int
7279 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
7280 {
7281   return bfd_get_signed_16 (abfd, buf);
7282 }
7283
7284 static unsigned int
7285 read_4_bytes (bfd *abfd, gdb_byte *buf)
7286 {
7287   return bfd_get_32 (abfd, buf);
7288 }
7289
7290 static int
7291 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
7292 {
7293   return bfd_get_signed_32 (abfd, buf);
7294 }
7295
7296 static ULONGEST
7297 read_8_bytes (bfd *abfd, gdb_byte *buf)
7298 {
7299   return bfd_get_64 (abfd, buf);
7300 }
7301
7302 static CORE_ADDR
7303 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
7304               unsigned int *bytes_read)
7305 {
7306   struct comp_unit_head *cu_header = &cu->header;
7307   CORE_ADDR retval = 0;
7308
7309   if (cu_header->signed_addr_p)
7310     {
7311       switch (cu_header->addr_size)
7312         {
7313         case 2:
7314           retval = bfd_get_signed_16 (abfd, buf);
7315           break;
7316         case 4:
7317           retval = bfd_get_signed_32 (abfd, buf);
7318           break;
7319         case 8:
7320           retval = bfd_get_signed_64 (abfd, buf);
7321           break;
7322         default:
7323           internal_error (__FILE__, __LINE__,
7324                           _("read_address: bad switch, signed [in module %s]"),
7325                           bfd_get_filename (abfd));
7326         }
7327     }
7328   else
7329     {
7330       switch (cu_header->addr_size)
7331         {
7332         case 2:
7333           retval = bfd_get_16 (abfd, buf);
7334           break;
7335         case 4:
7336           retval = bfd_get_32 (abfd, buf);
7337           break;
7338         case 8:
7339           retval = bfd_get_64 (abfd, buf);
7340           break;
7341         default:
7342           internal_error (__FILE__, __LINE__,
7343                           _("read_address: bad switch, unsigned [in module %s]"),
7344                           bfd_get_filename (abfd));
7345         }
7346     }
7347
7348   *bytes_read = cu_header->addr_size;
7349   return retval;
7350 }
7351
7352 /* Read the initial length from a section.  The (draft) DWARF 3
7353    specification allows the initial length to take up either 4 bytes
7354    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
7355    bytes describe the length and all offsets will be 8 bytes in length
7356    instead of 4.
7357
7358    An older, non-standard 64-bit format is also handled by this
7359    function.  The older format in question stores the initial length
7360    as an 8-byte quantity without an escape value.  Lengths greater
7361    than 2^32 aren't very common which means that the initial 4 bytes
7362    is almost always zero.  Since a length value of zero doesn't make
7363    sense for the 32-bit format, this initial zero can be considered to
7364    be an escape value which indicates the presence of the older 64-bit
7365    format.  As written, the code can't detect (old format) lengths
7366    greater than 4GB.  If it becomes necessary to handle lengths
7367    somewhat larger than 4GB, we could allow other small values (such
7368    as the non-sensical values of 1, 2, and 3) to also be used as
7369    escape values indicating the presence of the old format.
7370
7371    The value returned via bytes_read should be used to increment the
7372    relevant pointer after calling read_initial_length().
7373
7374    [ Note:  read_initial_length() and read_offset() are based on the
7375      document entitled "DWARF Debugging Information Format", revision
7376      3, draft 8, dated November 19, 2001.  This document was obtained
7377      from:
7378
7379         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
7380      
7381      This document is only a draft and is subject to change.  (So beware.)
7382
7383      Details regarding the older, non-standard 64-bit format were
7384      determined empirically by examining 64-bit ELF files produced by
7385      the SGI toolchain on an IRIX 6.5 machine.
7386
7387      - Kevin, July 16, 2002
7388    ] */
7389
7390 static LONGEST
7391 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
7392 {
7393   LONGEST length = bfd_get_32 (abfd, buf);
7394
7395   if (length == 0xffffffff)
7396     {
7397       length = bfd_get_64 (abfd, buf + 4);
7398       *bytes_read = 12;
7399     }
7400   else if (length == 0)
7401     {
7402       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
7403       length = bfd_get_64 (abfd, buf);
7404       *bytes_read = 8;
7405     }
7406   else
7407     {
7408       *bytes_read = 4;
7409     }
7410
7411   return length;
7412 }
7413
7414 /* Cover function for read_initial_length.
7415    Returns the length of the object at BUF, and stores the size of the
7416    initial length in *BYTES_READ and stores the size that offsets will be in
7417    *OFFSET_SIZE.
7418    If the initial length size is not equivalent to that specified in
7419    CU_HEADER then issue a complaint.
7420    This is useful when reading non-comp-unit headers.  */
7421
7422 static LONGEST
7423 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
7424                                         const struct comp_unit_head *cu_header,
7425                                         unsigned int *bytes_read,
7426                                         unsigned int *offset_size)
7427 {
7428   LONGEST length = read_initial_length (abfd, buf, bytes_read);
7429
7430   gdb_assert (cu_header->initial_length_size == 4
7431               || cu_header->initial_length_size == 8
7432               || cu_header->initial_length_size == 12);
7433
7434   if (cu_header->initial_length_size != *bytes_read)
7435     complaint (&symfile_complaints,
7436                _("intermixed 32-bit and 64-bit DWARF sections"));
7437
7438   *offset_size = (*bytes_read == 4) ? 4 : 8;
7439   return length;
7440 }
7441
7442 /* Read an offset from the data stream.  The size of the offset is
7443    given by cu_header->offset_size.  */
7444
7445 static LONGEST
7446 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
7447              unsigned int *bytes_read)
7448 {
7449   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
7450   *bytes_read = cu_header->offset_size;
7451   return offset;
7452 }
7453
7454 /* Read an offset from the data stream.  */
7455
7456 static LONGEST
7457 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
7458 {
7459   LONGEST retval = 0;
7460
7461   switch (offset_size)
7462     {
7463     case 4:
7464       retval = bfd_get_32 (abfd, buf);
7465       break;
7466     case 8:
7467       retval = bfd_get_64 (abfd, buf);
7468       break;
7469     default:
7470       internal_error (__FILE__, __LINE__,
7471                       _("read_offset_1: bad switch [in module %s]"),
7472                       bfd_get_filename (abfd));
7473     }
7474
7475   return retval;
7476 }
7477
7478 static gdb_byte *
7479 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
7480 {
7481   /* If the size of a host char is 8 bits, we can return a pointer
7482      to the buffer, otherwise we have to copy the data to a buffer
7483      allocated on the temporary obstack.  */
7484   gdb_assert (HOST_CHAR_BIT == 8);
7485   return buf;
7486 }
7487
7488 static char *
7489 read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7490 {
7491   /* If the size of a host char is 8 bits, we can return a pointer
7492      to the string, otherwise we have to copy the string to a buffer
7493      allocated on the temporary obstack.  */
7494   gdb_assert (HOST_CHAR_BIT == 8);
7495   if (*buf == '\0')
7496     {
7497       *bytes_read_ptr = 1;
7498       return NULL;
7499     }
7500   *bytes_read_ptr = strlen ((char *) buf) + 1;
7501   return (char *) buf;
7502 }
7503
7504 static char *
7505 read_indirect_string (bfd *abfd, gdb_byte *buf,
7506                       const struct comp_unit_head *cu_header,
7507                       unsigned int *bytes_read_ptr)
7508 {
7509   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
7510
7511   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
7512   if (dwarf2_per_objfile->str.buffer == NULL)
7513     {
7514       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
7515                       bfd_get_filename (abfd));
7516       return NULL;
7517     }
7518   if (str_offset >= dwarf2_per_objfile->str.size)
7519     {
7520       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
7521                       bfd_get_filename (abfd));
7522       return NULL;
7523     }
7524   gdb_assert (HOST_CHAR_BIT == 8);
7525   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
7526     return NULL;
7527   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
7528 }
7529
7530 static unsigned long
7531 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7532 {
7533   unsigned long result;
7534   unsigned int num_read;
7535   int i, shift;
7536   unsigned char byte;
7537
7538   result = 0;
7539   shift = 0;
7540   num_read = 0;
7541   i = 0;
7542   while (1)
7543     {
7544       byte = bfd_get_8 (abfd, buf);
7545       buf++;
7546       num_read++;
7547       result |= ((unsigned long)(byte & 127) << shift);
7548       if ((byte & 128) == 0)
7549         {
7550           break;
7551         }
7552       shift += 7;
7553     }
7554   *bytes_read_ptr = num_read;
7555   return result;
7556 }
7557
7558 static long
7559 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7560 {
7561   long result;
7562   int i, shift, num_read;
7563   unsigned char byte;
7564
7565   result = 0;
7566   shift = 0;
7567   num_read = 0;
7568   i = 0;
7569   while (1)
7570     {
7571       byte = bfd_get_8 (abfd, buf);
7572       buf++;
7573       num_read++;
7574       result |= ((long)(byte & 127) << shift);
7575       shift += 7;
7576       if ((byte & 128) == 0)
7577         {
7578           break;
7579         }
7580     }
7581   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
7582     result |= -(((long)1) << shift);
7583   *bytes_read_ptr = num_read;
7584   return result;
7585 }
7586
7587 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
7588
7589 static gdb_byte *
7590 skip_leb128 (bfd *abfd, gdb_byte *buf)
7591 {
7592   int byte;
7593
7594   while (1)
7595     {
7596       byte = bfd_get_8 (abfd, buf);
7597       buf++;
7598       if ((byte & 128) == 0)
7599         return buf;
7600     }
7601 }
7602
7603 static void
7604 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
7605 {
7606   switch (lang)
7607     {
7608     case DW_LANG_C89:
7609     case DW_LANG_C99:
7610     case DW_LANG_C:
7611       cu->language = language_c;
7612       break;
7613     case DW_LANG_C_plus_plus:
7614       cu->language = language_cplus;
7615       break;
7616     case DW_LANG_Fortran77:
7617     case DW_LANG_Fortran90:
7618     case DW_LANG_Fortran95:
7619       cu->language = language_fortran;
7620       break;
7621     case DW_LANG_Mips_Assembler:
7622       cu->language = language_asm;
7623       break;
7624     case DW_LANG_Java:
7625       cu->language = language_java;
7626       break;
7627     case DW_LANG_Ada83:
7628     case DW_LANG_Ada95:
7629       cu->language = language_ada;
7630       break;
7631     case DW_LANG_Modula2:
7632       cu->language = language_m2;
7633       break;
7634     case DW_LANG_Pascal83:
7635       cu->language = language_pascal;
7636       break;
7637     case DW_LANG_ObjC:
7638       cu->language = language_objc;
7639       break;
7640     case DW_LANG_Cobol74:
7641     case DW_LANG_Cobol85:
7642     default:
7643       cu->language = language_minimal;
7644       break;
7645     }
7646   cu->language_defn = language_def (cu->language);
7647 }
7648
7649 /* Return the named attribute or NULL if not there.  */
7650
7651 static struct attribute *
7652 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
7653 {
7654   unsigned int i;
7655   struct attribute *spec = NULL;
7656
7657   for (i = 0; i < die->num_attrs; ++i)
7658     {
7659       if (die->attrs[i].name == name)
7660         return &die->attrs[i];
7661       if (die->attrs[i].name == DW_AT_specification
7662           || die->attrs[i].name == DW_AT_abstract_origin)
7663         spec = &die->attrs[i];
7664     }
7665
7666   if (spec)
7667     {
7668       die = follow_die_ref (die, spec, &cu);
7669       return dwarf2_attr (die, name, cu);
7670     }
7671
7672   return NULL;
7673 }
7674
7675 /* Return the named attribute or NULL if not there,
7676    but do not follow DW_AT_specification, etc.
7677    This is for use in contexts where we're reading .debug_types dies.
7678    Following DW_AT_specification, DW_AT_abstract_origin will take us
7679    back up the chain, and we want to go down.  */
7680
7681 static struct attribute *
7682 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
7683                        struct dwarf2_cu *cu)
7684 {
7685   unsigned int i;
7686
7687   for (i = 0; i < die->num_attrs; ++i)
7688     if (die->attrs[i].name == name)
7689       return &die->attrs[i];
7690
7691   return NULL;
7692 }
7693
7694 /* Return non-zero iff the attribute NAME is defined for the given DIE,
7695    and holds a non-zero value.  This function should only be used for
7696    DW_FORM_flag or DW_FORM_flag_present attributes.  */
7697
7698 static int
7699 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7700 {
7701   struct attribute *attr = dwarf2_attr (die, name, cu);
7702
7703   return (attr && DW_UNSND (attr));
7704 }
7705
7706 static int
7707 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
7708 {
7709   /* A DIE is a declaration if it has a DW_AT_declaration attribute
7710      which value is non-zero.  However, we have to be careful with
7711      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7712      (via dwarf2_flag_true_p) follows this attribute.  So we may
7713      end up accidently finding a declaration attribute that belongs
7714      to a different DIE referenced by the specification attribute,
7715      even though the given DIE does not have a declaration attribute.  */
7716   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7717           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
7718 }
7719
7720 /* Return the die giving the specification for DIE, if there is
7721    one.  *SPEC_CU is the CU containing DIE on input, and the CU
7722    containing the return value on output.  If there is no
7723    specification, but there is an abstract origin, that is
7724    returned.  */
7725
7726 static struct die_info *
7727 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
7728 {
7729   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7730                                              *spec_cu);
7731
7732   if (spec_attr == NULL)
7733     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7734
7735   if (spec_attr == NULL)
7736     return NULL;
7737   else
7738     return follow_die_ref (die, spec_attr, spec_cu);
7739 }
7740
7741 /* Free the line_header structure *LH, and any arrays and strings it
7742    refers to.  */
7743 static void
7744 free_line_header (struct line_header *lh)
7745 {
7746   if (lh->standard_opcode_lengths)
7747     xfree (lh->standard_opcode_lengths);
7748
7749   /* Remember that all the lh->file_names[i].name pointers are
7750      pointers into debug_line_buffer, and don't need to be freed.  */
7751   if (lh->file_names)
7752     xfree (lh->file_names);
7753
7754   /* Similarly for the include directory names.  */
7755   if (lh->include_dirs)
7756     xfree (lh->include_dirs);
7757
7758   xfree (lh);
7759 }
7760
7761
7762 /* Add an entry to LH's include directory table.  */
7763 static void
7764 add_include_dir (struct line_header *lh, char *include_dir)
7765 {
7766   /* Grow the array if necessary.  */
7767   if (lh->include_dirs_size == 0)
7768     {
7769       lh->include_dirs_size = 1; /* for testing */
7770       lh->include_dirs = xmalloc (lh->include_dirs_size
7771                                   * sizeof (*lh->include_dirs));
7772     }
7773   else if (lh->num_include_dirs >= lh->include_dirs_size)
7774     {
7775       lh->include_dirs_size *= 2;
7776       lh->include_dirs = xrealloc (lh->include_dirs,
7777                                    (lh->include_dirs_size
7778                                     * sizeof (*lh->include_dirs)));
7779     }
7780
7781   lh->include_dirs[lh->num_include_dirs++] = include_dir;
7782 }
7783  
7784
7785 /* Add an entry to LH's file name table.  */
7786 static void
7787 add_file_name (struct line_header *lh,
7788                char *name,
7789                unsigned int dir_index,
7790                unsigned int mod_time,
7791                unsigned int length)
7792 {
7793   struct file_entry *fe;
7794
7795   /* Grow the array if necessary.  */
7796   if (lh->file_names_size == 0)
7797     {
7798       lh->file_names_size = 1; /* for testing */
7799       lh->file_names = xmalloc (lh->file_names_size
7800                                 * sizeof (*lh->file_names));
7801     }
7802   else if (lh->num_file_names >= lh->file_names_size)
7803     {
7804       lh->file_names_size *= 2;
7805       lh->file_names = xrealloc (lh->file_names,
7806                                  (lh->file_names_size
7807                                   * sizeof (*lh->file_names)));
7808     }
7809
7810   fe = &lh->file_names[lh->num_file_names++];
7811   fe->name = name;
7812   fe->dir_index = dir_index;
7813   fe->mod_time = mod_time;
7814   fe->length = length;
7815   fe->included_p = 0;
7816   fe->symtab = NULL;
7817 }
7818  
7819
7820 /* Read the statement program header starting at OFFSET in
7821    .debug_line, according to the endianness of ABFD.  Return a pointer
7822    to a struct line_header, allocated using xmalloc.
7823
7824    NOTE: the strings in the include directory and file name tables of
7825    the returned object point into debug_line_buffer, and must not be
7826    freed.  */
7827 static struct line_header *
7828 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
7829                           struct dwarf2_cu *cu)
7830 {
7831   struct cleanup *back_to;
7832   struct line_header *lh;
7833   gdb_byte *line_ptr;
7834   unsigned int bytes_read, offset_size;
7835   int i;
7836   char *cur_dir, *cur_file;
7837
7838   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
7839   if (dwarf2_per_objfile->line.buffer == NULL)
7840     {
7841       complaint (&symfile_complaints, _("missing .debug_line section"));
7842       return 0;
7843     }
7844
7845   /* Make sure that at least there's room for the total_length field.
7846      That could be 12 bytes long, but we're just going to fudge that.  */
7847   if (offset + 4 >= dwarf2_per_objfile->line.size)
7848     {
7849       dwarf2_statement_list_fits_in_line_number_section_complaint ();
7850       return 0;
7851     }
7852
7853   lh = xmalloc (sizeof (*lh));
7854   memset (lh, 0, sizeof (*lh));
7855   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7856                           (void *) lh);
7857
7858   line_ptr = dwarf2_per_objfile->line.buffer + offset;
7859
7860   /* Read in the header.  */
7861   lh->total_length = 
7862     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7863                                             &bytes_read, &offset_size);
7864   line_ptr += bytes_read;
7865   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
7866                                      + dwarf2_per_objfile->line.size))
7867     {
7868       dwarf2_statement_list_fits_in_line_number_section_complaint ();
7869       return 0;
7870     }
7871   lh->statement_program_end = line_ptr + lh->total_length;
7872   lh->version = read_2_bytes (abfd, line_ptr);
7873   line_ptr += 2;
7874   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7875   line_ptr += offset_size;
7876   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7877   line_ptr += 1;
7878   if (lh->version >= 4)
7879     {
7880       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
7881       line_ptr += 1;
7882     }
7883   else
7884     lh->maximum_ops_per_instruction = 1;
7885
7886   if (lh->maximum_ops_per_instruction == 0)
7887     {
7888       lh->maximum_ops_per_instruction = 1;
7889       complaint (&symfile_complaints,
7890                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
7891     }
7892
7893   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7894   line_ptr += 1;
7895   lh->line_base = read_1_signed_byte (abfd, line_ptr);
7896   line_ptr += 1;
7897   lh->line_range = read_1_byte (abfd, line_ptr);
7898   line_ptr += 1;
7899   lh->opcode_base = read_1_byte (abfd, line_ptr);
7900   line_ptr += 1;
7901   lh->standard_opcode_lengths
7902     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
7903
7904   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
7905   for (i = 1; i < lh->opcode_base; ++i)
7906     {
7907       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7908       line_ptr += 1;
7909     }
7910
7911   /* Read directory table.  */
7912   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7913     {
7914       line_ptr += bytes_read;
7915       add_include_dir (lh, cur_dir);
7916     }
7917   line_ptr += bytes_read;
7918
7919   /* Read file name table.  */
7920   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7921     {
7922       unsigned int dir_index, mod_time, length;
7923
7924       line_ptr += bytes_read;
7925       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7926       line_ptr += bytes_read;
7927       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7928       line_ptr += bytes_read;
7929       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7930       line_ptr += bytes_read;
7931
7932       add_file_name (lh, cur_file, dir_index, mod_time, length);
7933     }
7934   line_ptr += bytes_read;
7935   lh->statement_program_start = line_ptr; 
7936
7937   if (line_ptr > (dwarf2_per_objfile->line.buffer
7938                   + dwarf2_per_objfile->line.size))
7939     complaint (&symfile_complaints,
7940                _("line number info header doesn't fit in `.debug_line' section"));
7941
7942   discard_cleanups (back_to);
7943   return lh;
7944 }
7945
7946 /* This function exists to work around a bug in certain compilers
7947    (particularly GCC 2.95), in which the first line number marker of a
7948    function does not show up until after the prologue, right before
7949    the second line number marker.  This function shifts ADDRESS down
7950    to the beginning of the function if necessary, and is called on
7951    addresses passed to record_line.  */
7952
7953 static CORE_ADDR
7954 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
7955 {
7956   struct function_range *fn;
7957
7958   /* Find the function_range containing address.  */
7959   if (!cu->first_fn)
7960     return address;
7961
7962   if (!cu->cached_fn)
7963     cu->cached_fn = cu->first_fn;
7964
7965   fn = cu->cached_fn;
7966   while (fn)
7967     if (fn->lowpc <= address && fn->highpc > address)
7968       goto found;
7969     else
7970       fn = fn->next;
7971
7972   fn = cu->first_fn;
7973   while (fn && fn != cu->cached_fn)
7974     if (fn->lowpc <= address && fn->highpc > address)
7975       goto found;
7976     else
7977       fn = fn->next;
7978
7979   return address;
7980
7981  found:
7982   if (fn->seen_line)
7983     return address;
7984   if (address != fn->lowpc)
7985     complaint (&symfile_complaints,
7986                _("misplaced first line number at 0x%lx for '%s'"),
7987                (unsigned long) address, fn->name);
7988   fn->seen_line = 1;
7989   return fn->lowpc;
7990 }
7991
7992 /* Decode the Line Number Program (LNP) for the given line_header
7993    structure and CU.  The actual information extracted and the type
7994    of structures created from the LNP depends on the value of PST.
7995
7996    1. If PST is NULL, then this procedure uses the data from the program
7997       to create all necessary symbol tables, and their linetables.
7998       The compilation directory of the file is passed in COMP_DIR,
7999       and must not be NULL.
8000    
8001    2. If PST is not NULL, this procedure reads the program to determine
8002       the list of files included by the unit represented by PST, and
8003       builds all the associated partial symbol tables.  In this case,
8004       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
8005       is not used to compute the full name of the symtab, and therefore
8006       omitting it when building the partial symtab does not introduce
8007       the potential for inconsistency - a partial symtab and its associated
8008       symbtab having a different fullname -).  */
8009
8010 static void
8011 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
8012                     struct dwarf2_cu *cu, struct partial_symtab *pst)
8013 {
8014   gdb_byte *line_ptr, *extended_end;
8015   gdb_byte *line_end;
8016   unsigned int bytes_read, extended_len;
8017   unsigned char op_code, extended_op, adj_opcode;
8018   CORE_ADDR baseaddr;
8019   struct objfile *objfile = cu->objfile;
8020   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8021   const int decode_for_pst_p = (pst != NULL);
8022   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
8023
8024   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8025
8026   line_ptr = lh->statement_program_start;
8027   line_end = lh->statement_program_end;
8028
8029   /* Read the statement sequences until there's nothing left.  */
8030   while (line_ptr < line_end)
8031     {
8032       /* state machine registers  */
8033       CORE_ADDR address = 0;
8034       unsigned int file = 1;
8035       unsigned int line = 1;
8036       unsigned int column = 0;
8037       int is_stmt = lh->default_is_stmt;
8038       int basic_block = 0;
8039       int end_sequence = 0;
8040       CORE_ADDR addr;
8041       unsigned char op_index = 0;
8042
8043       if (!decode_for_pst_p && lh->num_file_names >= file)
8044         {
8045           /* Start a subfile for the current file of the state machine.  */
8046           /* lh->include_dirs and lh->file_names are 0-based, but the
8047              directory and file name numbers in the statement program
8048              are 1-based.  */
8049           struct file_entry *fe = &lh->file_names[file - 1];
8050           char *dir = NULL;
8051
8052           if (fe->dir_index)
8053             dir = lh->include_dirs[fe->dir_index - 1];
8054
8055           dwarf2_start_subfile (fe->name, dir, comp_dir);
8056         }
8057
8058       /* Decode the table.  */
8059       while (!end_sequence)
8060         {
8061           op_code = read_1_byte (abfd, line_ptr);
8062           line_ptr += 1;
8063           if (line_ptr > line_end)
8064             {
8065               dwarf2_debug_line_missing_end_sequence_complaint ();
8066               break;
8067             }
8068
8069           if (op_code >= lh->opcode_base)
8070             {           
8071               /* Special operand.  */
8072               adj_opcode = op_code - lh->opcode_base;
8073               address += (((op_index + (adj_opcode / lh->line_range))
8074                            / lh->maximum_ops_per_instruction)
8075                           * lh->minimum_instruction_length);
8076               op_index = ((op_index + (adj_opcode / lh->line_range))
8077                           % lh->maximum_ops_per_instruction);
8078               line += lh->line_base + (adj_opcode % lh->line_range);
8079               if (lh->num_file_names < file || file == 0)
8080                 dwarf2_debug_line_missing_file_complaint ();
8081               /* For now we ignore lines not starting on an
8082                  instruction boundary.  */
8083               else if (op_index == 0)
8084                 {
8085                   lh->file_names[file - 1].included_p = 1;
8086                   if (!decode_for_pst_p && is_stmt)
8087                     {
8088                       if (last_subfile != current_subfile)
8089                         {
8090                           addr = gdbarch_addr_bits_remove (gdbarch, address);
8091                           if (last_subfile)
8092                             record_line (last_subfile, 0, addr);
8093                           last_subfile = current_subfile;
8094                         }
8095                       /* Append row to matrix using current values.  */
8096                       addr = check_cu_functions (address, cu);
8097                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
8098                       record_line (current_subfile, line, addr);
8099                     }
8100                 }
8101               basic_block = 0;
8102             }
8103           else switch (op_code)
8104             {
8105             case DW_LNS_extended_op:
8106               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8107               line_ptr += bytes_read;
8108               extended_end = line_ptr + extended_len;
8109               extended_op = read_1_byte (abfd, line_ptr);
8110               line_ptr += 1;
8111               switch (extended_op)
8112                 {
8113                 case DW_LNE_end_sequence:
8114                   end_sequence = 1;
8115                   break;
8116                 case DW_LNE_set_address:
8117                   address = read_address (abfd, line_ptr, cu, &bytes_read);
8118                   op_index = 0;
8119                   line_ptr += bytes_read;
8120                   address += baseaddr;
8121                   break;
8122                 case DW_LNE_define_file:
8123                   {
8124                     char *cur_file;
8125                     unsigned int dir_index, mod_time, length;
8126                     
8127                     cur_file = read_string (abfd, line_ptr, &bytes_read);
8128                     line_ptr += bytes_read;
8129                     dir_index =
8130                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8131                     line_ptr += bytes_read;
8132                     mod_time =
8133                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8134                     line_ptr += bytes_read;
8135                     length =
8136                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8137                     line_ptr += bytes_read;
8138                     add_file_name (lh, cur_file, dir_index, mod_time, length);
8139                   }
8140                   break;
8141                 case DW_LNE_set_discriminator:
8142                   /* The discriminator is not interesting to the debugger;
8143                      just ignore it.  */
8144                   line_ptr = extended_end;
8145                   break;
8146                 default:
8147                   complaint (&symfile_complaints,
8148                              _("mangled .debug_line section"));
8149                   return;
8150                 }
8151               /* Make sure that we parsed the extended op correctly.  If e.g.
8152                  we expected a different address size than the producer used,
8153                  we may have read the wrong number of bytes.  */
8154               if (line_ptr != extended_end)
8155                 {
8156                   complaint (&symfile_complaints,
8157                              _("mangled .debug_line section"));
8158                   return;
8159                 }
8160               break;
8161             case DW_LNS_copy:
8162               if (lh->num_file_names < file || file == 0)
8163                 dwarf2_debug_line_missing_file_complaint ();
8164               else
8165                 {
8166                   lh->file_names[file - 1].included_p = 1;
8167                   if (!decode_for_pst_p && is_stmt)
8168                     {
8169                       if (last_subfile != current_subfile)
8170                         {
8171                           addr = gdbarch_addr_bits_remove (gdbarch, address);
8172                           if (last_subfile)
8173                             record_line (last_subfile, 0, addr);
8174                           last_subfile = current_subfile;
8175                         }
8176                       addr = check_cu_functions (address, cu);
8177                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
8178                       record_line (current_subfile, line, addr);
8179                     }
8180                 }
8181               basic_block = 0;
8182               break;
8183             case DW_LNS_advance_pc:
8184               {
8185                 CORE_ADDR adjust
8186                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8187
8188                 address += (((op_index + adjust)
8189                              / lh->maximum_ops_per_instruction)
8190                             * lh->minimum_instruction_length);
8191                 op_index = ((op_index + adjust)
8192                             % lh->maximum_ops_per_instruction);
8193                 line_ptr += bytes_read;
8194               }
8195               break;
8196             case DW_LNS_advance_line:
8197               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
8198               line_ptr += bytes_read;
8199               break;
8200             case DW_LNS_set_file:
8201               {
8202                 /* The arrays lh->include_dirs and lh->file_names are
8203                    0-based, but the directory and file name numbers in
8204                    the statement program are 1-based.  */
8205                 struct file_entry *fe;
8206                 char *dir = NULL;
8207
8208                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8209                 line_ptr += bytes_read;
8210                 if (lh->num_file_names < file || file == 0)
8211                   dwarf2_debug_line_missing_file_complaint ();
8212                 else
8213                   {
8214                     fe = &lh->file_names[file - 1];
8215                     if (fe->dir_index)
8216                       dir = lh->include_dirs[fe->dir_index - 1];
8217                     if (!decode_for_pst_p)
8218                       {
8219                         last_subfile = current_subfile;
8220                         dwarf2_start_subfile (fe->name, dir, comp_dir);
8221                       }
8222                   }
8223               }
8224               break;
8225             case DW_LNS_set_column:
8226               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8227               line_ptr += bytes_read;
8228               break;
8229             case DW_LNS_negate_stmt:
8230               is_stmt = (!is_stmt);
8231               break;
8232             case DW_LNS_set_basic_block:
8233               basic_block = 1;
8234               break;
8235             /* Add to the address register of the state machine the
8236                address increment value corresponding to special opcode
8237                255.  I.e., this value is scaled by the minimum
8238                instruction length since special opcode 255 would have
8239                scaled the the increment.  */
8240             case DW_LNS_const_add_pc:
8241               {
8242                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
8243
8244                 address += (((op_index + adjust)
8245                              / lh->maximum_ops_per_instruction)
8246                             * lh->minimum_instruction_length);
8247                 op_index = ((op_index + adjust)
8248                             % lh->maximum_ops_per_instruction);
8249               }
8250               break;
8251             case DW_LNS_fixed_advance_pc:
8252               address += read_2_bytes (abfd, line_ptr);
8253               op_index = 0;
8254               line_ptr += 2;
8255               break;
8256             default:
8257               {
8258                 /* Unknown standard opcode, ignore it.  */
8259                 int i;
8260
8261                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
8262                   {
8263                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8264                     line_ptr += bytes_read;
8265                   }
8266               }
8267             }
8268         }
8269       if (lh->num_file_names < file || file == 0)
8270         dwarf2_debug_line_missing_file_complaint ();
8271       else
8272         {
8273           lh->file_names[file - 1].included_p = 1;
8274           if (!decode_for_pst_p)
8275             {
8276               addr = gdbarch_addr_bits_remove (gdbarch, address);
8277               record_line (current_subfile, 0, addr);
8278             }
8279         }
8280     }
8281
8282   if (decode_for_pst_p)
8283     {
8284       int file_index;
8285
8286       /* Now that we're done scanning the Line Header Program, we can
8287          create the psymtab of each included file.  */
8288       for (file_index = 0; file_index < lh->num_file_names; file_index++)
8289         if (lh->file_names[file_index].included_p == 1)
8290           {
8291             const struct file_entry fe = lh->file_names [file_index];
8292             char *include_name = fe.name;
8293             char *dir_name = NULL;
8294             char *pst_filename = pst->filename;
8295
8296             if (fe.dir_index)
8297               dir_name = lh->include_dirs[fe.dir_index - 1];
8298
8299             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
8300               {
8301                 include_name = concat (dir_name, SLASH_STRING,
8302                                        include_name, (char *)NULL);
8303                 make_cleanup (xfree, include_name);
8304               }
8305
8306             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
8307               {
8308                 pst_filename = concat (pst->dirname, SLASH_STRING,
8309                                        pst_filename, (char *)NULL);
8310                 make_cleanup (xfree, pst_filename);
8311               }
8312
8313             if (strcmp (include_name, pst_filename) != 0)
8314               dwarf2_create_include_psymtab (include_name, pst, objfile);
8315           }
8316     }
8317   else
8318     {
8319       /* Make sure a symtab is created for every file, even files
8320          which contain only variables (i.e. no code with associated
8321          line numbers).  */
8322
8323       int i;
8324       struct file_entry *fe;
8325
8326       for (i = 0; i < lh->num_file_names; i++)
8327         {
8328           char *dir = NULL;
8329           fe = &lh->file_names[i];
8330           if (fe->dir_index)
8331             dir = lh->include_dirs[fe->dir_index - 1];
8332           dwarf2_start_subfile (fe->name, dir, comp_dir);
8333
8334           /* Skip the main file; we don't need it, and it must be
8335              allocated last, so that it will show up before the
8336              non-primary symtabs in the objfile's symtab list.  */
8337           if (current_subfile == first_subfile)
8338             continue;
8339
8340           if (current_subfile->symtab == NULL)
8341             current_subfile->symtab = allocate_symtab (current_subfile->name,
8342                                                        cu->objfile);
8343           fe->symtab = current_subfile->symtab;
8344         }
8345     }
8346 }
8347
8348 /* Start a subfile for DWARF.  FILENAME is the name of the file and
8349    DIRNAME the name of the source directory which contains FILENAME
8350    or NULL if not known.  COMP_DIR is the compilation directory for the
8351    linetable's compilation unit or NULL if not known.
8352    This routine tries to keep line numbers from identical absolute and
8353    relative file names in a common subfile.
8354
8355    Using the `list' example from the GDB testsuite, which resides in
8356    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
8357    of /srcdir/list0.c yields the following debugging information for list0.c:
8358
8359    DW_AT_name:          /srcdir/list0.c
8360    DW_AT_comp_dir:              /compdir
8361    files.files[0].name: list0.h
8362    files.files[0].dir:  /srcdir
8363    files.files[1].name: list0.c
8364    files.files[1].dir:  /srcdir
8365
8366    The line number information for list0.c has to end up in a single
8367    subfile, so that `break /srcdir/list0.c:1' works as expected.
8368    start_subfile will ensure that this happens provided that we pass the
8369    concatenation of files.files[1].dir and files.files[1].name as the
8370    subfile's name.  */
8371
8372 static void
8373 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
8374 {
8375   char *fullname;
8376
8377   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
8378      `start_symtab' will always pass the contents of DW_AT_comp_dir as
8379      second argument to start_subfile.  To be consistent, we do the
8380      same here.  In order not to lose the line information directory,
8381      we concatenate it to the filename when it makes sense.
8382      Note that the Dwarf3 standard says (speaking of filenames in line
8383      information): ``The directory index is ignored for file names
8384      that represent full path names''.  Thus ignoring dirname in the
8385      `else' branch below isn't an issue.  */
8386
8387   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
8388     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
8389   else
8390     fullname = filename;
8391
8392   start_subfile (fullname, comp_dir);
8393
8394   if (fullname != filename)
8395     xfree (fullname);
8396 }
8397
8398 static void
8399 var_decode_location (struct attribute *attr, struct symbol *sym,
8400                      struct dwarf2_cu *cu)
8401 {
8402   struct objfile *objfile = cu->objfile;
8403   struct comp_unit_head *cu_header = &cu->header;
8404
8405   /* NOTE drow/2003-01-30: There used to be a comment and some special
8406      code here to turn a symbol with DW_AT_external and a
8407      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
8408      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
8409      with some versions of binutils) where shared libraries could have
8410      relocations against symbols in their debug information - the
8411      minimal symbol would have the right address, but the debug info
8412      would not.  It's no longer necessary, because we will explicitly
8413      apply relocations when we read in the debug information now.  */
8414
8415   /* A DW_AT_location attribute with no contents indicates that a
8416      variable has been optimized away.  */
8417   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
8418     {
8419       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8420       return;
8421     }
8422
8423   /* Handle one degenerate form of location expression specially, to
8424      preserve GDB's previous behavior when section offsets are
8425      specified.  If this is just a DW_OP_addr then mark this symbol
8426      as LOC_STATIC.  */
8427
8428   if (attr_form_is_block (attr)
8429       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
8430       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
8431     {
8432       unsigned int dummy;
8433
8434       SYMBOL_VALUE_ADDRESS (sym) =
8435         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
8436       SYMBOL_CLASS (sym) = LOC_STATIC;
8437       fixup_symbol_section (sym, objfile);
8438       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
8439                                               SYMBOL_SECTION (sym));
8440       return;
8441     }
8442
8443   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
8444      expression evaluator, and use LOC_COMPUTED only when necessary
8445      (i.e. when the value of a register or memory location is
8446      referenced, or a thread-local block, etc.).  Then again, it might
8447      not be worthwhile.  I'm assuming that it isn't unless performance
8448      or memory numbers show me otherwise.  */
8449
8450   dwarf2_symbol_mark_computed (attr, sym, cu);
8451   SYMBOL_CLASS (sym) = LOC_COMPUTED;
8452 }
8453
8454 /* Given a pointer to a DWARF information entry, figure out if we need
8455    to make a symbol table entry for it, and if so, create a new entry
8456    and return a pointer to it.
8457    If TYPE is NULL, determine symbol type from the die, otherwise
8458    used the passed type.  */
8459
8460 static struct symbol *
8461 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
8462 {
8463   struct objfile *objfile = cu->objfile;
8464   struct symbol *sym = NULL;
8465   char *name;
8466   struct attribute *attr = NULL;
8467   struct attribute *attr2 = NULL;
8468   CORE_ADDR baseaddr;
8469   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8470
8471   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8472
8473   name = dwarf2_name (die, cu);
8474   if (name)
8475     {
8476       const char *linkagename;
8477
8478       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
8479                                              sizeof (struct symbol));
8480       OBJSTAT (objfile, n_syms++);
8481       memset (sym, 0, sizeof (struct symbol));
8482
8483       /* Cache this symbol's name and the name's demangled form (if any).  */
8484       SYMBOL_LANGUAGE (sym) = cu->language;
8485       linkagename = dwarf2_physname (name, die, cu);
8486       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
8487
8488       /* Default assumptions.
8489          Use the passed type or decode it from the die.  */
8490       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8491       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8492       if (type != NULL)
8493         SYMBOL_TYPE (sym) = type;
8494       else
8495         SYMBOL_TYPE (sym) = die_type (die, cu);
8496       attr = dwarf2_attr (die,
8497                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
8498                           cu);
8499       if (attr)
8500         {
8501           SYMBOL_LINE (sym) = DW_UNSND (attr);
8502         }
8503
8504       attr = dwarf2_attr (die,
8505                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
8506                           cu);
8507       if (attr)
8508         {
8509           int file_index = DW_UNSND (attr);
8510           if (cu->line_header == NULL
8511               || file_index > cu->line_header->num_file_names)
8512             complaint (&symfile_complaints,
8513                        _("file index out of range"));
8514           else if (file_index > 0)
8515             {
8516               struct file_entry *fe;
8517               fe = &cu->line_header->file_names[file_index - 1];
8518               SYMBOL_SYMTAB (sym) = fe->symtab;
8519             }
8520         }
8521
8522       switch (die->tag)
8523         {
8524         case DW_TAG_label:
8525           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8526           if (attr)
8527             {
8528               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
8529             }
8530           SYMBOL_CLASS (sym) = LOC_LABEL;
8531           break;
8532         case DW_TAG_subprogram:
8533           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8534              finish_block.  */
8535           SYMBOL_CLASS (sym) = LOC_BLOCK;
8536           attr2 = dwarf2_attr (die, DW_AT_external, cu);
8537           if ((attr2 && (DW_UNSND (attr2) != 0))
8538               || cu->language == language_ada)
8539             {
8540               /* Subprograms marked external are stored as a global symbol.
8541                  Ada subprograms, whether marked external or not, are always
8542                  stored as a global symbol, because we want to be able to
8543                  access them globally.  For instance, we want to be able
8544                  to break on a nested subprogram without having to
8545                  specify the context.  */
8546               add_symbol_to_list (sym, &global_symbols);
8547             }
8548           else
8549             {
8550               add_symbol_to_list (sym, cu->list_in_scope);
8551             }
8552           break;
8553         case DW_TAG_inlined_subroutine:
8554           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8555              finish_block.  */
8556           SYMBOL_CLASS (sym) = LOC_BLOCK;
8557           SYMBOL_INLINED (sym) = 1;
8558           /* Do not add the symbol to any lists.  It will be found via
8559              BLOCK_FUNCTION from the blockvector.  */
8560           break;
8561         case DW_TAG_variable:
8562           /* Compilation with minimal debug info may result in variables
8563              with missing type entries. Change the misleading `void' type
8564              to something sensible.  */
8565           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
8566             SYMBOL_TYPE (sym)
8567               = objfile_type (objfile)->nodebug_data_symbol;
8568
8569           attr = dwarf2_attr (die, DW_AT_const_value, cu);
8570           if (attr)
8571             {
8572               dwarf2_const_value (attr, sym, cu);
8573               attr2 = dwarf2_attr (die, DW_AT_external, cu);
8574               if (attr2 && (DW_UNSND (attr2) != 0))
8575                 add_symbol_to_list (sym, &global_symbols);
8576               else
8577                 add_symbol_to_list (sym, cu->list_in_scope);
8578               break;
8579             }
8580           attr = dwarf2_attr (die, DW_AT_location, cu);
8581           if (attr)
8582             {
8583               var_decode_location (attr, sym, cu);
8584               attr2 = dwarf2_attr (die, DW_AT_external, cu);
8585               if (attr2 && (DW_UNSND (attr2) != 0))
8586                 {
8587                   struct pending **list_to_add;
8588
8589                   /* A variable with DW_AT_external is never static,
8590                      but it may be block-scoped.  */
8591                   list_to_add = (cu->list_in_scope == &file_symbols
8592                                  ? &global_symbols : cu->list_in_scope);
8593                   add_symbol_to_list (sym, list_to_add);
8594                 }
8595               else
8596                 add_symbol_to_list (sym, cu->list_in_scope);
8597             }
8598           else
8599             {
8600               /* We do not know the address of this symbol.
8601                  If it is an external symbol and we have type information
8602                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
8603                  The address of the variable will then be determined from
8604                  the minimal symbol table whenever the variable is
8605                  referenced.  */
8606               attr2 = dwarf2_attr (die, DW_AT_external, cu);
8607               if (attr2 && (DW_UNSND (attr2) != 0)
8608                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
8609                 {
8610                   struct pending **list_to_add;
8611
8612                   /* A variable with DW_AT_external is never static, but it
8613                      may be block-scoped.  */
8614                   list_to_add = (cu->list_in_scope == &file_symbols
8615                                  ? &global_symbols : cu->list_in_scope);
8616
8617                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
8618                   add_symbol_to_list (sym, list_to_add);
8619                 }
8620               else if (!die_is_declaration (die, cu))
8621                 {
8622                   /* Use the default LOC_OPTIMIZED_OUT class.  */
8623                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
8624                   add_symbol_to_list (sym, cu->list_in_scope);
8625                 }
8626             }
8627           break;
8628         case DW_TAG_formal_parameter:
8629           /* If we are inside a function, mark this as an argument.  If
8630              not, we might be looking at an argument to an inlined function
8631              when we do not have enough information to show inlined frames;
8632              pretend it's a local variable in that case so that the user can
8633              still see it.  */
8634           if (context_stack_depth > 0
8635               && context_stack[context_stack_depth - 1].name != NULL)
8636             SYMBOL_IS_ARGUMENT (sym) = 1;
8637           attr = dwarf2_attr (die, DW_AT_location, cu);
8638           if (attr)
8639             {
8640               var_decode_location (attr, sym, cu);
8641             }
8642           attr = dwarf2_attr (die, DW_AT_const_value, cu);
8643           if (attr)
8644             {
8645               dwarf2_const_value (attr, sym, cu);
8646             }
8647           add_symbol_to_list (sym, cu->list_in_scope);
8648           break;
8649         case DW_TAG_unspecified_parameters:
8650           /* From varargs functions; gdb doesn't seem to have any
8651              interest in this information, so just ignore it for now.
8652              (FIXME?) */
8653           break;
8654         case DW_TAG_class_type:
8655         case DW_TAG_interface_type:
8656         case DW_TAG_structure_type:
8657         case DW_TAG_union_type:
8658         case DW_TAG_set_type:
8659         case DW_TAG_enumeration_type:
8660           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8661           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8662
8663           /* Make sure that the symbol includes appropriate enclosing
8664              classes/namespaces in its name.  These are calculated in
8665              read_structure_type, and the correct name is saved in
8666              the type.  */
8667
8668           if (cu->language == language_cplus
8669               || cu->language == language_java)
8670             {
8671               struct type *type = SYMBOL_TYPE (sym);
8672               
8673               if (TYPE_TAG_NAME (type) != NULL)
8674                 {
8675                   /* FIXME: carlton/2003-11-10: Should this use
8676                      SYMBOL_SET_NAMES instead?  (The same problem also
8677                      arises further down in this function.)  */
8678                   /* The type's name is already allocated along with
8679                      this objfile, so we don't need to duplicate it
8680                      for the symbol.  */
8681                   SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
8682                 }
8683             }
8684
8685           {
8686             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
8687                really ever be static objects: otherwise, if you try
8688                to, say, break of a class's method and you're in a file
8689                which doesn't mention that class, it won't work unless
8690                the check for all static symbols in lookup_symbol_aux
8691                saves you.  See the OtherFileClass tests in
8692                gdb.c++/namespace.exp.  */
8693
8694             struct pending **list_to_add;
8695
8696             list_to_add = (cu->list_in_scope == &file_symbols
8697                            && (cu->language == language_cplus
8698                                || cu->language == language_java)
8699                            ? &global_symbols : cu->list_in_scope);
8700           
8701             add_symbol_to_list (sym, list_to_add);
8702
8703             /* The semantics of C++ state that "struct foo { ... }" also
8704                defines a typedef for "foo".  A Java class declaration also
8705                defines a typedef for the class.  */
8706             if (cu->language == language_cplus
8707                 || cu->language == language_java
8708                 || cu->language == language_ada)
8709               {
8710                 /* The symbol's name is already allocated along with
8711                    this objfile, so we don't need to duplicate it for
8712                    the type.  */
8713                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
8714                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
8715               }
8716           }
8717           break;
8718         case DW_TAG_typedef:
8719           SYMBOL_LINKAGE_NAME (sym)
8720             = (char *) dwarf2_full_name (name, die, cu);
8721           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8722           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8723           add_symbol_to_list (sym, cu->list_in_scope);
8724           break;
8725         case DW_TAG_base_type:
8726         case DW_TAG_subrange_type:
8727           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8728           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8729           add_symbol_to_list (sym, cu->list_in_scope);
8730           break;
8731         case DW_TAG_enumerator:
8732           SYMBOL_LINKAGE_NAME (sym)
8733             = (char *) dwarf2_full_name (name, die, cu);
8734           attr = dwarf2_attr (die, DW_AT_const_value, cu);
8735           if (attr)
8736             {
8737               dwarf2_const_value (attr, sym, cu);
8738             }
8739           {
8740             /* NOTE: carlton/2003-11-10: See comment above in the
8741                DW_TAG_class_type, etc. block.  */
8742
8743             struct pending **list_to_add;
8744
8745             list_to_add = (cu->list_in_scope == &file_symbols
8746                            && (cu->language == language_cplus
8747                                || cu->language == language_java)
8748                            ? &global_symbols : cu->list_in_scope);
8749           
8750             add_symbol_to_list (sym, list_to_add);
8751           }
8752           break;
8753         case DW_TAG_namespace:
8754           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8755           add_symbol_to_list (sym, &global_symbols);
8756           break;
8757         default:
8758           /* Not a tag we recognize.  Hopefully we aren't processing
8759              trash data, but since we must specifically ignore things
8760              we don't recognize, there is nothing else we should do at
8761              this point. */
8762           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
8763                      dwarf_tag_name (die->tag));
8764           break;
8765         }
8766
8767       /* For the benefit of old versions of GCC, check for anonymous
8768          namespaces based on the demangled name.  */
8769       if (!processing_has_namespace_info
8770           && cu->language == language_cplus)
8771         cp_scan_for_anonymous_namespaces (sym);
8772     }
8773   return (sym);
8774 }
8775
8776 /* Copy constant value from an attribute to a symbol.  */
8777
8778 static void
8779 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
8780                     struct dwarf2_cu *cu)
8781 {
8782   struct objfile *objfile = cu->objfile;
8783   struct comp_unit_head *cu_header = &cu->header;
8784   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
8785                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
8786   struct dwarf_block *blk;
8787
8788   switch (attr->form)
8789     {
8790     case DW_FORM_addr:
8791       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
8792         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
8793                                                       cu_header->addr_size,
8794                                                       TYPE_LENGTH (SYMBOL_TYPE
8795                                                                    (sym)));
8796       SYMBOL_VALUE_BYTES (sym) = 
8797         obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
8798       /* NOTE: cagney/2003-05-09: In-lined store_address call with
8799          it's body - store_unsigned_integer.  */
8800       store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
8801                               byte_order, DW_ADDR (attr));
8802       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8803       break;
8804     case DW_FORM_string:
8805     case DW_FORM_strp:
8806       /* DW_STRING is already allocated on the obstack, point directly
8807          to it.  */
8808       SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
8809       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8810       break;
8811     case DW_FORM_block1:
8812     case DW_FORM_block2:
8813     case DW_FORM_block4:
8814     case DW_FORM_block:
8815     case DW_FORM_exprloc:
8816       blk = DW_BLOCK (attr);
8817       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
8818         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
8819                                                       blk->size,
8820                                                       TYPE_LENGTH (SYMBOL_TYPE
8821                                                                    (sym)));
8822       SYMBOL_VALUE_BYTES (sym) =
8823         obstack_alloc (&objfile->objfile_obstack, blk->size);
8824       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
8825       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8826       break;
8827
8828       /* The DW_AT_const_value attributes are supposed to carry the
8829          symbol's value "represented as it would be on the target
8830          architecture."  By the time we get here, it's already been
8831          converted to host endianness, so we just need to sign- or
8832          zero-extend it as appropriate.  */
8833     case DW_FORM_data1:
8834       dwarf2_const_value_data (attr, sym, 8);
8835       break;
8836     case DW_FORM_data2:
8837       dwarf2_const_value_data (attr, sym, 16);
8838       break;
8839     case DW_FORM_data4:
8840       dwarf2_const_value_data (attr, sym, 32);
8841       break;
8842     case DW_FORM_data8:
8843       dwarf2_const_value_data (attr, sym, 64);
8844       break;
8845
8846     case DW_FORM_sdata:
8847       SYMBOL_VALUE (sym) = DW_SND (attr);
8848       SYMBOL_CLASS (sym) = LOC_CONST;
8849       break;
8850
8851     case DW_FORM_udata:
8852       SYMBOL_VALUE (sym) = DW_UNSND (attr);
8853       SYMBOL_CLASS (sym) = LOC_CONST;
8854       break;
8855
8856     default:
8857       complaint (&symfile_complaints,
8858                  _("unsupported const value attribute form: '%s'"),
8859                  dwarf_form_name (attr->form));
8860       SYMBOL_VALUE (sym) = 0;
8861       SYMBOL_CLASS (sym) = LOC_CONST;
8862       break;
8863     }
8864 }
8865
8866
8867 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
8868    or zero-extend it as appropriate for the symbol's type.  */
8869 static void
8870 dwarf2_const_value_data (struct attribute *attr,
8871                          struct symbol *sym,
8872                          int bits)
8873 {
8874   LONGEST l = DW_UNSND (attr);
8875
8876   if (bits < sizeof (l) * 8)
8877     {
8878       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
8879         l &= ((LONGEST) 1 << bits) - 1;
8880       else
8881         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
8882     }
8883
8884   SYMBOL_VALUE (sym) = l;
8885   SYMBOL_CLASS (sym) = LOC_CONST;
8886 }
8887
8888
8889 /* Return the type of the die in question using its DW_AT_type attribute.  */
8890
8891 static struct type *
8892 die_type (struct die_info *die, struct dwarf2_cu *cu)
8893 {
8894   struct type *type;
8895   struct attribute *type_attr;
8896   struct die_info *type_die;
8897
8898   type_attr = dwarf2_attr (die, DW_AT_type, cu);
8899   if (!type_attr)
8900     {
8901       /* A missing DW_AT_type represents a void type.  */
8902       return objfile_type (cu->objfile)->builtin_void;
8903     }
8904
8905   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
8906
8907   type = tag_type_to_type (type_die, cu);
8908   if (!type)
8909     {
8910       dump_die_for_error (type_die);
8911       error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
8912                       cu->objfile->name);
8913     }
8914   return type;
8915 }
8916
8917 /* True iff CU's producer generates GNAT Ada auxiliary information
8918    that allows to find parallel types through that information instead
8919    of having to do expensive parallel lookups by type name.  */
8920
8921 static int
8922 need_gnat_info (struct dwarf2_cu *cu)
8923 {
8924   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
8925      of GNAT produces this auxiliary information, without any indication
8926      that it is produced.  Part of enhancing the FSF version of GNAT
8927      to produce that information will be to put in place an indicator
8928      that we can use in order to determine whether the descriptive type
8929      info is available or not.  One suggestion that has been made is
8930      to use a new attribute, attached to the CU die.  For now, assume
8931      that the descriptive type info is not available.  */
8932   return 0;
8933 }
8934
8935
8936 /* Return the auxiliary type of the die in question using its
8937    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
8938    attribute is not present.  */
8939
8940 static struct type *
8941 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
8942 {
8943   struct type *type;
8944   struct attribute *type_attr;
8945   struct die_info *type_die;
8946
8947   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
8948   if (!type_attr)
8949     return NULL;
8950
8951   type_die = follow_die_ref (die, type_attr, &cu);
8952   type = tag_type_to_type (type_die, cu);
8953   if (!type)
8954     {
8955       dump_die_for_error (type_die);
8956       error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
8957                       cu->objfile->name);
8958     }
8959   return type;
8960 }
8961
8962 /* If DIE has a descriptive_type attribute, then set the TYPE's
8963    descriptive type accordingly.  */
8964
8965 static void
8966 set_descriptive_type (struct type *type, struct die_info *die,
8967                       struct dwarf2_cu *cu)
8968 {
8969   struct type *descriptive_type = die_descriptive_type (die, cu);
8970
8971   if (descriptive_type)
8972     {
8973       ALLOCATE_GNAT_AUX_TYPE (type);
8974       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
8975     }
8976 }
8977
8978 /* Return the containing type of the die in question using its
8979    DW_AT_containing_type attribute.  */
8980
8981 static struct type *
8982 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
8983 {
8984   struct type *type = NULL;
8985   struct attribute *type_attr;
8986   struct die_info *type_die = NULL;
8987
8988   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
8989   if (type_attr)
8990     {
8991       type_die = follow_die_ref_or_sig (die, type_attr, &cu);
8992       type = tag_type_to_type (type_die, cu);
8993     }
8994   if (!type)
8995     {
8996       if (type_die)
8997         dump_die_for_error (type_die);
8998       error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"), 
8999                       cu->objfile->name);
9000     }
9001   return type;
9002 }
9003
9004 static struct type *
9005 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
9006 {
9007   struct type *this_type;
9008
9009   this_type = read_type_die (die, cu);
9010   if (!this_type)
9011     {
9012       dump_die_for_error (die);
9013       error (_("Dwarf Error: Cannot find type of die [in module %s]"), 
9014              cu->objfile->name);
9015     }
9016   return this_type;
9017 }
9018
9019 static struct type *
9020 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
9021 {
9022   struct type *this_type;
9023
9024   this_type = get_die_type (die, cu);
9025   if (this_type)
9026     return this_type;
9027
9028   switch (die->tag)
9029     {
9030     case DW_TAG_class_type:
9031     case DW_TAG_interface_type:
9032     case DW_TAG_structure_type:
9033     case DW_TAG_union_type:
9034       this_type = read_structure_type (die, cu);
9035       break;
9036     case DW_TAG_enumeration_type:
9037       this_type = read_enumeration_type (die, cu);
9038       break;
9039     case DW_TAG_subprogram:
9040     case DW_TAG_subroutine_type:
9041     case DW_TAG_inlined_subroutine:
9042       this_type = read_subroutine_type (die, cu);
9043       break;
9044     case DW_TAG_array_type:
9045       this_type = read_array_type (die, cu);
9046       break;
9047     case DW_TAG_set_type:
9048       this_type = read_set_type (die, cu);
9049       break;
9050     case DW_TAG_pointer_type:
9051       this_type = read_tag_pointer_type (die, cu);
9052       break;
9053     case DW_TAG_ptr_to_member_type:
9054       this_type = read_tag_ptr_to_member_type (die, cu);
9055       break;
9056     case DW_TAG_reference_type:
9057       this_type = read_tag_reference_type (die, cu);
9058       break;
9059     case DW_TAG_const_type:
9060       this_type = read_tag_const_type (die, cu);
9061       break;
9062     case DW_TAG_volatile_type:
9063       this_type = read_tag_volatile_type (die, cu);
9064       break;
9065     case DW_TAG_string_type:
9066       this_type = read_tag_string_type (die, cu);
9067       break;
9068     case DW_TAG_typedef:
9069       this_type = read_typedef (die, cu);
9070       break;
9071     case DW_TAG_subrange_type:
9072       this_type = read_subrange_type (die, cu);
9073       break;
9074     case DW_TAG_base_type:
9075       this_type = read_base_type (die, cu);
9076       break;
9077     case DW_TAG_unspecified_type:
9078       this_type = read_unspecified_type (die, cu);
9079       break;
9080     case DW_TAG_namespace:
9081       this_type = read_namespace_type (die, cu);
9082       break;
9083     default:
9084       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
9085                  dwarf_tag_name (die->tag));
9086       break;
9087     }
9088
9089   return this_type;
9090 }
9091
9092 /* Return the name of the namespace/class that DIE is defined within,
9093    or "" if we can't tell.  The caller should not xfree the result.
9094
9095    For example, if we're within the method foo() in the following
9096    code:
9097
9098    namespace N {
9099      class C {
9100        void foo () {
9101        }
9102      };
9103    }
9104
9105    then determine_prefix on foo's die will return "N::C".  */
9106
9107 static char *
9108 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
9109 {
9110   struct die_info *parent, *spec_die;
9111   struct dwarf2_cu *spec_cu;
9112   struct type *parent_type;
9113
9114   if (cu->language != language_cplus
9115       && cu->language != language_java)
9116     return "";
9117
9118   /* We have to be careful in the presence of DW_AT_specification.
9119      For example, with GCC 3.4, given the code
9120
9121      namespace N {
9122        void foo() {
9123          // Definition of N::foo.
9124        }
9125      }
9126
9127      then we'll have a tree of DIEs like this:
9128
9129      1: DW_TAG_compile_unit
9130        2: DW_TAG_namespace        // N
9131          3: DW_TAG_subprogram     // declaration of N::foo
9132        4: DW_TAG_subprogram       // definition of N::foo
9133             DW_AT_specification   // refers to die #3
9134
9135      Thus, when processing die #4, we have to pretend that we're in
9136      the context of its DW_AT_specification, namely the contex of die
9137      #3.  */
9138   spec_cu = cu;
9139   spec_die = die_specification (die, &spec_cu);
9140   if (spec_die == NULL)
9141     parent = die->parent;
9142   else
9143     {
9144       parent = spec_die->parent;
9145       cu = spec_cu;
9146     }
9147
9148   if (parent == NULL)
9149     return "";
9150   else
9151     switch (parent->tag)
9152       {
9153       case DW_TAG_namespace:
9154         parent_type = read_type_die (parent, cu);
9155         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9156            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9157            Work around this problem here.  */
9158         if (cu->language == language_cplus
9159             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
9160           return "";
9161         /* We give a name to even anonymous namespaces.  */
9162         return TYPE_TAG_NAME (parent_type);
9163       case DW_TAG_class_type:
9164       case DW_TAG_interface_type:
9165       case DW_TAG_structure_type:
9166       case DW_TAG_union_type:
9167         parent_type = read_type_die (parent, cu);
9168         if (TYPE_TAG_NAME (parent_type) != NULL)
9169           return TYPE_TAG_NAME (parent_type);
9170         else
9171           /* An anonymous structure is only allowed non-static data
9172              members; no typedefs, no member functions, et cetera.
9173              So it does not need a prefix.  */
9174           return "";
9175       default:
9176         return determine_prefix (parent, cu);
9177       }
9178 }
9179
9180 /* Return a newly-allocated string formed by concatenating PREFIX and
9181    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
9182    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
9183    perform an obconcat, otherwise allocate storage for the result.  The CU argument
9184    is used to determine the language and hence, the appropriate separator.  */
9185
9186 #define MAX_SEP_LEN 2  /* sizeof ("::")  */
9187
9188 static char *
9189 typename_concat (struct obstack *obs, const char *prefix, const char *suffix, 
9190                  struct dwarf2_cu *cu)
9191 {
9192   char *sep;
9193
9194   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
9195     sep = "";
9196   else if (cu->language == language_java)
9197     sep = ".";
9198   else
9199     sep = "::";
9200
9201   if (prefix == NULL)
9202     prefix = "";
9203   if (suffix == NULL)
9204     suffix = "";
9205
9206   if (obs == NULL)
9207     {
9208       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9209       strcpy (retval, prefix);
9210       strcat (retval, sep);
9211       strcat (retval, suffix);
9212       return retval;
9213     }
9214   else
9215     {
9216       /* We have an obstack.  */
9217       return obconcat (obs, prefix, sep, suffix);
9218     }
9219 }
9220
9221 /* Return sibling of die, NULL if no sibling.  */
9222
9223 static struct die_info *
9224 sibling_die (struct die_info *die)
9225 {
9226   return die->sibling;
9227 }
9228
9229 /* Get name of a die, return NULL if not found.  */
9230
9231 static char *
9232 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
9233                           struct obstack *obstack)
9234 {
9235   if (name && cu->language == language_cplus)
9236     {
9237       char *canon_name = cp_canonicalize_string (name);
9238
9239       if (canon_name != NULL)
9240         {
9241           if (strcmp (canon_name, name) != 0)
9242             name = obsavestring (canon_name, strlen (canon_name),
9243                                  obstack);
9244           xfree (canon_name);
9245         }
9246     }
9247
9248   return name;
9249 }
9250
9251 /* Get name of a die, return NULL if not found.  */
9252
9253 static char *
9254 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9255 {
9256   struct attribute *attr;
9257
9258   attr = dwarf2_attr (die, DW_AT_name, cu);
9259   if (!attr || !DW_STRING (attr))
9260     return NULL;
9261
9262   switch (die->tag)
9263     {
9264     case DW_TAG_compile_unit:
9265       /* Compilation units have a DW_AT_name that is a filename, not
9266          a source language identifier.  */
9267     case DW_TAG_enumeration_type:
9268     case DW_TAG_enumerator:
9269       /* These tags always have simple identifiers already; no need
9270          to canonicalize them.  */
9271       return DW_STRING (attr);
9272
9273     case DW_TAG_subprogram:
9274       /* Java constructors will all be named "<init>", so return
9275          the class name when we see this special case.  */
9276       if (cu->language == language_java
9277           && DW_STRING (attr) != NULL
9278           && strcmp (DW_STRING (attr), "<init>") == 0)
9279         {
9280           struct dwarf2_cu *spec_cu = cu;
9281           struct die_info *spec_die;
9282
9283           /* GCJ will output '<init>' for Java constructor names.
9284              For this special case, return the name of the parent class.  */
9285
9286           /* GCJ may output suprogram DIEs with AT_specification set.
9287              If so, use the name of the specified DIE.  */
9288           spec_die = die_specification (die, &spec_cu);
9289           if (spec_die != NULL)
9290             return dwarf2_name (spec_die, spec_cu);
9291
9292           do
9293             {
9294               die = die->parent;
9295               if (die->tag == DW_TAG_class_type)
9296                 return dwarf2_name (die, cu);
9297             }
9298           while (die->tag != DW_TAG_compile_unit);
9299         }
9300       break;
9301
9302     case DW_TAG_class_type:
9303     case DW_TAG_interface_type:
9304     case DW_TAG_structure_type:
9305     case DW_TAG_union_type:
9306       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
9307          structures or unions.  These were of the form "._%d" in GCC 4.1,
9308          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
9309          and GCC 4.4.  We work around this problem by ignoring these.  */
9310       if (strncmp (DW_STRING (attr), "._", 2) == 0
9311           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
9312         return NULL;
9313       break;
9314
9315     default:
9316       break;
9317     }
9318
9319   if (!DW_STRING_IS_CANONICAL (attr))
9320     {
9321       DW_STRING (attr)
9322         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
9323                                     &cu->objfile->objfile_obstack);
9324       DW_STRING_IS_CANONICAL (attr) = 1;
9325     }
9326   return DW_STRING (attr);
9327 }
9328
9329 /* Return the die that this die in an extension of, or NULL if there
9330    is none.  *EXT_CU is the CU containing DIE on input, and the CU
9331    containing the return value on output.  */
9332
9333 static struct die_info *
9334 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9335 {
9336   struct attribute *attr;
9337
9338   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9339   if (attr == NULL)
9340     return NULL;
9341
9342   return follow_die_ref (die, attr, ext_cu);
9343 }
9344
9345 /* Convert a DIE tag into its string name.  */
9346
9347 static char *
9348 dwarf_tag_name (unsigned tag)
9349 {
9350   switch (tag)
9351     {
9352     case DW_TAG_padding:
9353       return "DW_TAG_padding";
9354     case DW_TAG_array_type:
9355       return "DW_TAG_array_type";
9356     case DW_TAG_class_type:
9357       return "DW_TAG_class_type";
9358     case DW_TAG_entry_point:
9359       return "DW_TAG_entry_point";
9360     case DW_TAG_enumeration_type:
9361       return "DW_TAG_enumeration_type";
9362     case DW_TAG_formal_parameter:
9363       return "DW_TAG_formal_parameter";
9364     case DW_TAG_imported_declaration:
9365       return "DW_TAG_imported_declaration";
9366     case DW_TAG_label:
9367       return "DW_TAG_label";
9368     case DW_TAG_lexical_block:
9369       return "DW_TAG_lexical_block";
9370     case DW_TAG_member:
9371       return "DW_TAG_member";
9372     case DW_TAG_pointer_type:
9373       return "DW_TAG_pointer_type";
9374     case DW_TAG_reference_type:
9375       return "DW_TAG_reference_type";
9376     case DW_TAG_compile_unit:
9377       return "DW_TAG_compile_unit";
9378     case DW_TAG_string_type:
9379       return "DW_TAG_string_type";
9380     case DW_TAG_structure_type:
9381       return "DW_TAG_structure_type";
9382     case DW_TAG_subroutine_type:
9383       return "DW_TAG_subroutine_type";
9384     case DW_TAG_typedef:
9385       return "DW_TAG_typedef";
9386     case DW_TAG_union_type:
9387       return "DW_TAG_union_type";
9388     case DW_TAG_unspecified_parameters:
9389       return "DW_TAG_unspecified_parameters";
9390     case DW_TAG_variant:
9391       return "DW_TAG_variant";
9392     case DW_TAG_common_block:
9393       return "DW_TAG_common_block";
9394     case DW_TAG_common_inclusion:
9395       return "DW_TAG_common_inclusion";
9396     case DW_TAG_inheritance:
9397       return "DW_TAG_inheritance";
9398     case DW_TAG_inlined_subroutine:
9399       return "DW_TAG_inlined_subroutine";
9400     case DW_TAG_module:
9401       return "DW_TAG_module";
9402     case DW_TAG_ptr_to_member_type:
9403       return "DW_TAG_ptr_to_member_type";
9404     case DW_TAG_set_type:
9405       return "DW_TAG_set_type";
9406     case DW_TAG_subrange_type:
9407       return "DW_TAG_subrange_type";
9408     case DW_TAG_with_stmt:
9409       return "DW_TAG_with_stmt";
9410     case DW_TAG_access_declaration:
9411       return "DW_TAG_access_declaration";
9412     case DW_TAG_base_type:
9413       return "DW_TAG_base_type";
9414     case DW_TAG_catch_block:
9415       return "DW_TAG_catch_block";
9416     case DW_TAG_const_type:
9417       return "DW_TAG_const_type";
9418     case DW_TAG_constant:
9419       return "DW_TAG_constant";
9420     case DW_TAG_enumerator:
9421       return "DW_TAG_enumerator";
9422     case DW_TAG_file_type:
9423       return "DW_TAG_file_type";
9424     case DW_TAG_friend:
9425       return "DW_TAG_friend";
9426     case DW_TAG_namelist:
9427       return "DW_TAG_namelist";
9428     case DW_TAG_namelist_item:
9429       return "DW_TAG_namelist_item";
9430     case DW_TAG_packed_type:
9431       return "DW_TAG_packed_type";
9432     case DW_TAG_subprogram:
9433       return "DW_TAG_subprogram";
9434     case DW_TAG_template_type_param:
9435       return "DW_TAG_template_type_param";
9436     case DW_TAG_template_value_param:
9437       return "DW_TAG_template_value_param";
9438     case DW_TAG_thrown_type:
9439       return "DW_TAG_thrown_type";
9440     case DW_TAG_try_block:
9441       return "DW_TAG_try_block";
9442     case DW_TAG_variant_part:
9443       return "DW_TAG_variant_part";
9444     case DW_TAG_variable:
9445       return "DW_TAG_variable";
9446     case DW_TAG_volatile_type:
9447       return "DW_TAG_volatile_type";
9448     case DW_TAG_dwarf_procedure:
9449       return "DW_TAG_dwarf_procedure";
9450     case DW_TAG_restrict_type:
9451       return "DW_TAG_restrict_type";
9452     case DW_TAG_interface_type:
9453       return "DW_TAG_interface_type";
9454     case DW_TAG_namespace:
9455       return "DW_TAG_namespace";
9456     case DW_TAG_imported_module:
9457       return "DW_TAG_imported_module";
9458     case DW_TAG_unspecified_type:
9459       return "DW_TAG_unspecified_type";
9460     case DW_TAG_partial_unit:
9461       return "DW_TAG_partial_unit";
9462     case DW_TAG_imported_unit:
9463       return "DW_TAG_imported_unit";
9464     case DW_TAG_condition:
9465       return "DW_TAG_condition";
9466     case DW_TAG_shared_type:
9467       return "DW_TAG_shared_type";
9468     case DW_TAG_type_unit:
9469       return "DW_TAG_type_unit";
9470     case DW_TAG_MIPS_loop:
9471       return "DW_TAG_MIPS_loop";
9472     case DW_TAG_HP_array_descriptor:
9473       return "DW_TAG_HP_array_descriptor";
9474     case DW_TAG_format_label:
9475       return "DW_TAG_format_label";
9476     case DW_TAG_function_template:
9477       return "DW_TAG_function_template";
9478     case DW_TAG_class_template:
9479       return "DW_TAG_class_template";
9480     case DW_TAG_GNU_BINCL:
9481       return "DW_TAG_GNU_BINCL";
9482     case DW_TAG_GNU_EINCL:
9483       return "DW_TAG_GNU_EINCL";
9484     case DW_TAG_upc_shared_type:
9485       return "DW_TAG_upc_shared_type";
9486     case DW_TAG_upc_strict_type:
9487       return "DW_TAG_upc_strict_type";
9488     case DW_TAG_upc_relaxed_type:
9489       return "DW_TAG_upc_relaxed_type";
9490     case DW_TAG_PGI_kanji_type:
9491       return "DW_TAG_PGI_kanji_type";
9492     case DW_TAG_PGI_interface_block:
9493       return "DW_TAG_PGI_interface_block";
9494     default:
9495       return "DW_TAG_<unknown>";
9496     }
9497 }
9498
9499 /* Convert a DWARF attribute code into its string name.  */
9500
9501 static char *
9502 dwarf_attr_name (unsigned attr)
9503 {
9504   switch (attr)
9505     {
9506     case DW_AT_sibling:
9507       return "DW_AT_sibling";
9508     case DW_AT_location:
9509       return "DW_AT_location";
9510     case DW_AT_name:
9511       return "DW_AT_name";
9512     case DW_AT_ordering:
9513       return "DW_AT_ordering";
9514     case DW_AT_subscr_data:
9515       return "DW_AT_subscr_data";
9516     case DW_AT_byte_size:
9517       return "DW_AT_byte_size";
9518     case DW_AT_bit_offset:
9519       return "DW_AT_bit_offset";
9520     case DW_AT_bit_size:
9521       return "DW_AT_bit_size";
9522     case DW_AT_element_list:
9523       return "DW_AT_element_list";
9524     case DW_AT_stmt_list:
9525       return "DW_AT_stmt_list";
9526     case DW_AT_low_pc:
9527       return "DW_AT_low_pc";
9528     case DW_AT_high_pc:
9529       return "DW_AT_high_pc";
9530     case DW_AT_language:
9531       return "DW_AT_language";
9532     case DW_AT_member:
9533       return "DW_AT_member";
9534     case DW_AT_discr:
9535       return "DW_AT_discr";
9536     case DW_AT_discr_value:
9537       return "DW_AT_discr_value";
9538     case DW_AT_visibility:
9539       return "DW_AT_visibility";
9540     case DW_AT_import:
9541       return "DW_AT_import";
9542     case DW_AT_string_length:
9543       return "DW_AT_string_length";
9544     case DW_AT_common_reference:
9545       return "DW_AT_common_reference";
9546     case DW_AT_comp_dir:
9547       return "DW_AT_comp_dir";
9548     case DW_AT_const_value:
9549       return "DW_AT_const_value";
9550     case DW_AT_containing_type:
9551       return "DW_AT_containing_type";
9552     case DW_AT_default_value:
9553       return "DW_AT_default_value";
9554     case DW_AT_inline:
9555       return "DW_AT_inline";
9556     case DW_AT_is_optional:
9557       return "DW_AT_is_optional";
9558     case DW_AT_lower_bound:
9559       return "DW_AT_lower_bound";
9560     case DW_AT_producer:
9561       return "DW_AT_producer";
9562     case DW_AT_prototyped:
9563       return "DW_AT_prototyped";
9564     case DW_AT_return_addr:
9565       return "DW_AT_return_addr";
9566     case DW_AT_start_scope:
9567       return "DW_AT_start_scope";
9568     case DW_AT_bit_stride:
9569       return "DW_AT_bit_stride";
9570     case DW_AT_upper_bound:
9571       return "DW_AT_upper_bound";
9572     case DW_AT_abstract_origin:
9573       return "DW_AT_abstract_origin";
9574     case DW_AT_accessibility:
9575       return "DW_AT_accessibility";
9576     case DW_AT_address_class:
9577       return "DW_AT_address_class";
9578     case DW_AT_artificial:
9579       return "DW_AT_artificial";
9580     case DW_AT_base_types:
9581       return "DW_AT_base_types";
9582     case DW_AT_calling_convention:
9583       return "DW_AT_calling_convention";
9584     case DW_AT_count:
9585       return "DW_AT_count";
9586     case DW_AT_data_member_location:
9587       return "DW_AT_data_member_location";
9588     case DW_AT_decl_column:
9589       return "DW_AT_decl_column";
9590     case DW_AT_decl_file:
9591       return "DW_AT_decl_file";
9592     case DW_AT_decl_line:
9593       return "DW_AT_decl_line";
9594     case DW_AT_declaration:
9595       return "DW_AT_declaration";
9596     case DW_AT_discr_list:
9597       return "DW_AT_discr_list";
9598     case DW_AT_encoding:
9599       return "DW_AT_encoding";
9600     case DW_AT_external:
9601       return "DW_AT_external";
9602     case DW_AT_frame_base:
9603       return "DW_AT_frame_base";
9604     case DW_AT_friend:
9605       return "DW_AT_friend";
9606     case DW_AT_identifier_case:
9607       return "DW_AT_identifier_case";
9608     case DW_AT_macro_info:
9609       return "DW_AT_macro_info";
9610     case DW_AT_namelist_items:
9611       return "DW_AT_namelist_items";
9612     case DW_AT_priority:
9613       return "DW_AT_priority";
9614     case DW_AT_segment:
9615       return "DW_AT_segment";
9616     case DW_AT_specification:
9617       return "DW_AT_specification";
9618     case DW_AT_static_link:
9619       return "DW_AT_static_link";
9620     case DW_AT_type:
9621       return "DW_AT_type";
9622     case DW_AT_use_location:
9623       return "DW_AT_use_location";
9624     case DW_AT_variable_parameter:
9625       return "DW_AT_variable_parameter";
9626     case DW_AT_virtuality:
9627       return "DW_AT_virtuality";
9628     case DW_AT_vtable_elem_location:
9629       return "DW_AT_vtable_elem_location";
9630     /* DWARF 3 values.  */
9631     case DW_AT_allocated:
9632       return "DW_AT_allocated";
9633     case DW_AT_associated:
9634       return "DW_AT_associated";
9635     case DW_AT_data_location:
9636       return "DW_AT_data_location";
9637     case DW_AT_byte_stride:
9638       return "DW_AT_byte_stride";
9639     case DW_AT_entry_pc:
9640       return "DW_AT_entry_pc";
9641     case DW_AT_use_UTF8:
9642       return "DW_AT_use_UTF8";
9643     case DW_AT_extension:
9644       return "DW_AT_extension";
9645     case DW_AT_ranges:
9646       return "DW_AT_ranges";
9647     case DW_AT_trampoline:
9648       return "DW_AT_trampoline";
9649     case DW_AT_call_column:
9650       return "DW_AT_call_column";
9651     case DW_AT_call_file:
9652       return "DW_AT_call_file";
9653     case DW_AT_call_line:
9654       return "DW_AT_call_line";
9655     case DW_AT_description:
9656       return "DW_AT_description";
9657     case DW_AT_binary_scale:
9658       return "DW_AT_binary_scale";
9659     case DW_AT_decimal_scale:
9660       return "DW_AT_decimal_scale";
9661     case DW_AT_small:
9662       return "DW_AT_small";
9663     case DW_AT_decimal_sign:
9664       return "DW_AT_decimal_sign";
9665     case DW_AT_digit_count:
9666       return "DW_AT_digit_count";
9667     case DW_AT_picture_string:
9668       return "DW_AT_picture_string";
9669     case DW_AT_mutable:
9670       return "DW_AT_mutable";
9671     case DW_AT_threads_scaled:
9672       return "DW_AT_threads_scaled";
9673     case DW_AT_explicit:
9674       return "DW_AT_explicit";
9675     case DW_AT_object_pointer:
9676       return "DW_AT_object_pointer";
9677     case DW_AT_endianity:
9678       return "DW_AT_endianity";
9679     case DW_AT_elemental:
9680       return "DW_AT_elemental";
9681     case DW_AT_pure:
9682       return "DW_AT_pure";
9683     case DW_AT_recursive:
9684       return "DW_AT_recursive";
9685     /* DWARF 4 values.  */
9686     case DW_AT_signature:
9687       return "DW_AT_signature";
9688     /* SGI/MIPS extensions.  */
9689 #ifdef MIPS /* collides with DW_AT_HP_block_index */
9690     case DW_AT_MIPS_fde:
9691       return "DW_AT_MIPS_fde";
9692 #endif
9693     case DW_AT_MIPS_loop_begin:
9694       return "DW_AT_MIPS_loop_begin";
9695     case DW_AT_MIPS_tail_loop_begin:
9696       return "DW_AT_MIPS_tail_loop_begin";
9697     case DW_AT_MIPS_epilog_begin:
9698       return "DW_AT_MIPS_epilog_begin";
9699     case DW_AT_MIPS_loop_unroll_factor:
9700       return "DW_AT_MIPS_loop_unroll_factor";
9701     case DW_AT_MIPS_software_pipeline_depth:
9702       return "DW_AT_MIPS_software_pipeline_depth";
9703     case DW_AT_MIPS_linkage_name:
9704       return "DW_AT_MIPS_linkage_name";
9705     case DW_AT_MIPS_stride:
9706       return "DW_AT_MIPS_stride";
9707     case DW_AT_MIPS_abstract_name:
9708       return "DW_AT_MIPS_abstract_name";
9709     case DW_AT_MIPS_clone_origin:
9710       return "DW_AT_MIPS_clone_origin";
9711     case DW_AT_MIPS_has_inlines:
9712       return "DW_AT_MIPS_has_inlines";
9713     /* HP extensions.  */
9714 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
9715     case DW_AT_HP_block_index:
9716       return "DW_AT_HP_block_index";
9717 #endif
9718     case DW_AT_HP_unmodifiable:
9719       return "DW_AT_HP_unmodifiable";
9720     case DW_AT_HP_actuals_stmt_list:
9721       return "DW_AT_HP_actuals_stmt_list";
9722     case DW_AT_HP_proc_per_section:
9723       return "DW_AT_HP_proc_per_section";
9724     case DW_AT_HP_raw_data_ptr:
9725       return "DW_AT_HP_raw_data_ptr";
9726     case DW_AT_HP_pass_by_reference:
9727       return "DW_AT_HP_pass_by_reference";
9728     case DW_AT_HP_opt_level:
9729       return "DW_AT_HP_opt_level";
9730     case DW_AT_HP_prof_version_id:
9731       return "DW_AT_HP_prof_version_id";
9732     case DW_AT_HP_opt_flags:
9733       return "DW_AT_HP_opt_flags";
9734     case DW_AT_HP_cold_region_low_pc:
9735       return "DW_AT_HP_cold_region_low_pc";
9736     case DW_AT_HP_cold_region_high_pc:
9737       return "DW_AT_HP_cold_region_high_pc";
9738     case DW_AT_HP_all_variables_modifiable:
9739       return "DW_AT_HP_all_variables_modifiable";
9740     case DW_AT_HP_linkage_name:
9741       return "DW_AT_HP_linkage_name";
9742     case DW_AT_HP_prof_flags:
9743       return "DW_AT_HP_prof_flags";
9744     /* GNU extensions.  */
9745     case DW_AT_sf_names:
9746       return "DW_AT_sf_names";
9747     case DW_AT_src_info:
9748       return "DW_AT_src_info";
9749     case DW_AT_mac_info:
9750       return "DW_AT_mac_info";
9751     case DW_AT_src_coords:
9752       return "DW_AT_src_coords";
9753     case DW_AT_body_begin:
9754       return "DW_AT_body_begin";
9755     case DW_AT_body_end:
9756       return "DW_AT_body_end";
9757     case DW_AT_GNU_vector:
9758       return "DW_AT_GNU_vector";
9759     /* VMS extensions.  */
9760     case DW_AT_VMS_rtnbeg_pd_address:
9761       return "DW_AT_VMS_rtnbeg_pd_address";
9762     /* UPC extension.  */
9763     case DW_AT_upc_threads_scaled:
9764       return "DW_AT_upc_threads_scaled";
9765     /* PGI (STMicroelectronics) extensions.  */
9766     case DW_AT_PGI_lbase:
9767       return "DW_AT_PGI_lbase";
9768     case DW_AT_PGI_soffset:
9769       return "DW_AT_PGI_soffset";
9770     case DW_AT_PGI_lstride:
9771       return "DW_AT_PGI_lstride";
9772     default:
9773       return "DW_AT_<unknown>";
9774     }
9775 }
9776
9777 /* Convert a DWARF value form code into its string name.  */
9778
9779 static char *
9780 dwarf_form_name (unsigned form)
9781 {
9782   switch (form)
9783     {
9784     case DW_FORM_addr:
9785       return "DW_FORM_addr";
9786     case DW_FORM_block2:
9787       return "DW_FORM_block2";
9788     case DW_FORM_block4:
9789       return "DW_FORM_block4";
9790     case DW_FORM_data2:
9791       return "DW_FORM_data2";
9792     case DW_FORM_data4:
9793       return "DW_FORM_data4";
9794     case DW_FORM_data8:
9795       return "DW_FORM_data8";
9796     case DW_FORM_string:
9797       return "DW_FORM_string";
9798     case DW_FORM_block:
9799       return "DW_FORM_block";
9800     case DW_FORM_block1:
9801       return "DW_FORM_block1";
9802     case DW_FORM_data1:
9803       return "DW_FORM_data1";
9804     case DW_FORM_flag:
9805       return "DW_FORM_flag";
9806     case DW_FORM_sdata:
9807       return "DW_FORM_sdata";
9808     case DW_FORM_strp:
9809       return "DW_FORM_strp";
9810     case DW_FORM_udata:
9811       return "DW_FORM_udata";
9812     case DW_FORM_ref_addr:
9813       return "DW_FORM_ref_addr";
9814     case DW_FORM_ref1:
9815       return "DW_FORM_ref1";
9816     case DW_FORM_ref2:
9817       return "DW_FORM_ref2";
9818     case DW_FORM_ref4:
9819       return "DW_FORM_ref4";
9820     case DW_FORM_ref8:
9821       return "DW_FORM_ref8";
9822     case DW_FORM_ref_udata:
9823       return "DW_FORM_ref_udata";
9824     case DW_FORM_indirect:
9825       return "DW_FORM_indirect";
9826     case DW_FORM_sec_offset:
9827       return "DW_FORM_sec_offset";
9828     case DW_FORM_exprloc:
9829       return "DW_FORM_exprloc";
9830     case DW_FORM_flag_present:
9831       return "DW_FORM_flag_present";
9832     case DW_FORM_sig8:
9833       return "DW_FORM_sig8";
9834     default:
9835       return "DW_FORM_<unknown>";
9836     }
9837 }
9838
9839 /* Convert a DWARF stack opcode into its string name.  */
9840
9841 static char *
9842 dwarf_stack_op_name (unsigned op)
9843 {
9844   switch (op)
9845     {
9846     case DW_OP_addr:
9847       return "DW_OP_addr";
9848     case DW_OP_deref:
9849       return "DW_OP_deref";
9850     case DW_OP_const1u:
9851       return "DW_OP_const1u";
9852     case DW_OP_const1s:
9853       return "DW_OP_const1s";
9854     case DW_OP_const2u:
9855       return "DW_OP_const2u";
9856     case DW_OP_const2s:
9857       return "DW_OP_const2s";
9858     case DW_OP_const4u:
9859       return "DW_OP_const4u";
9860     case DW_OP_const4s:
9861       return "DW_OP_const4s";
9862     case DW_OP_const8u:
9863       return "DW_OP_const8u";
9864     case DW_OP_const8s:
9865       return "DW_OP_const8s";
9866     case DW_OP_constu:
9867       return "DW_OP_constu";
9868     case DW_OP_consts:
9869       return "DW_OP_consts";
9870     case DW_OP_dup:
9871       return "DW_OP_dup";
9872     case DW_OP_drop:
9873       return "DW_OP_drop";
9874     case DW_OP_over:
9875       return "DW_OP_over";
9876     case DW_OP_pick:
9877       return "DW_OP_pick";
9878     case DW_OP_swap:
9879       return "DW_OP_swap";
9880     case DW_OP_rot:
9881       return "DW_OP_rot";
9882     case DW_OP_xderef:
9883       return "DW_OP_xderef";
9884     case DW_OP_abs:
9885       return "DW_OP_abs";
9886     case DW_OP_and:
9887       return "DW_OP_and";
9888     case DW_OP_div:
9889       return "DW_OP_div";
9890     case DW_OP_minus:
9891       return "DW_OP_minus";
9892     case DW_OP_mod:
9893       return "DW_OP_mod";
9894     case DW_OP_mul:
9895       return "DW_OP_mul";
9896     case DW_OP_neg:
9897       return "DW_OP_neg";
9898     case DW_OP_not:
9899       return "DW_OP_not";
9900     case DW_OP_or:
9901       return "DW_OP_or";
9902     case DW_OP_plus:
9903       return "DW_OP_plus";
9904     case DW_OP_plus_uconst:
9905       return "DW_OP_plus_uconst";
9906     case DW_OP_shl:
9907       return "DW_OP_shl";
9908     case DW_OP_shr:
9909       return "DW_OP_shr";
9910     case DW_OP_shra:
9911       return "DW_OP_shra";
9912     case DW_OP_xor:
9913       return "DW_OP_xor";
9914     case DW_OP_bra:
9915       return "DW_OP_bra";
9916     case DW_OP_eq:
9917       return "DW_OP_eq";
9918     case DW_OP_ge:
9919       return "DW_OP_ge";
9920     case DW_OP_gt:
9921       return "DW_OP_gt";
9922     case DW_OP_le:
9923       return "DW_OP_le";
9924     case DW_OP_lt:
9925       return "DW_OP_lt";
9926     case DW_OP_ne:
9927       return "DW_OP_ne";
9928     case DW_OP_skip:
9929       return "DW_OP_skip";
9930     case DW_OP_lit0:
9931       return "DW_OP_lit0";
9932     case DW_OP_lit1:
9933       return "DW_OP_lit1";
9934     case DW_OP_lit2:
9935       return "DW_OP_lit2";
9936     case DW_OP_lit3:
9937       return "DW_OP_lit3";
9938     case DW_OP_lit4:
9939       return "DW_OP_lit4";
9940     case DW_OP_lit5:
9941       return "DW_OP_lit5";
9942     case DW_OP_lit6:
9943       return "DW_OP_lit6";
9944     case DW_OP_lit7:
9945       return "DW_OP_lit7";
9946     case DW_OP_lit8:
9947       return "DW_OP_lit8";
9948     case DW_OP_lit9:
9949       return "DW_OP_lit9";
9950     case DW_OP_lit10:
9951       return "DW_OP_lit10";
9952     case DW_OP_lit11:
9953       return "DW_OP_lit11";
9954     case DW_OP_lit12:
9955       return "DW_OP_lit12";
9956     case DW_OP_lit13:
9957       return "DW_OP_lit13";
9958     case DW_OP_lit14:
9959       return "DW_OP_lit14";
9960     case DW_OP_lit15:
9961       return "DW_OP_lit15";
9962     case DW_OP_lit16:
9963       return "DW_OP_lit16";
9964     case DW_OP_lit17:
9965       return "DW_OP_lit17";
9966     case DW_OP_lit18:
9967       return "DW_OP_lit18";
9968     case DW_OP_lit19:
9969       return "DW_OP_lit19";
9970     case DW_OP_lit20:
9971       return "DW_OP_lit20";
9972     case DW_OP_lit21:
9973       return "DW_OP_lit21";
9974     case DW_OP_lit22:
9975       return "DW_OP_lit22";
9976     case DW_OP_lit23:
9977       return "DW_OP_lit23";
9978     case DW_OP_lit24:
9979       return "DW_OP_lit24";
9980     case DW_OP_lit25:
9981       return "DW_OP_lit25";
9982     case DW_OP_lit26:
9983       return "DW_OP_lit26";
9984     case DW_OP_lit27:
9985       return "DW_OP_lit27";
9986     case DW_OP_lit28:
9987       return "DW_OP_lit28";
9988     case DW_OP_lit29:
9989       return "DW_OP_lit29";
9990     case DW_OP_lit30:
9991       return "DW_OP_lit30";
9992     case DW_OP_lit31:
9993       return "DW_OP_lit31";
9994     case DW_OP_reg0:
9995       return "DW_OP_reg0";
9996     case DW_OP_reg1:
9997       return "DW_OP_reg1";
9998     case DW_OP_reg2:
9999       return "DW_OP_reg2";
10000     case DW_OP_reg3:
10001       return "DW_OP_reg3";
10002     case DW_OP_reg4:
10003       return "DW_OP_reg4";
10004     case DW_OP_reg5:
10005       return "DW_OP_reg5";
10006     case DW_OP_reg6:
10007       return "DW_OP_reg6";
10008     case DW_OP_reg7:
10009       return "DW_OP_reg7";
10010     case DW_OP_reg8:
10011       return "DW_OP_reg8";
10012     case DW_OP_reg9:
10013       return "DW_OP_reg9";
10014     case DW_OP_reg10:
10015       return "DW_OP_reg10";
10016     case DW_OP_reg11:
10017       return "DW_OP_reg11";
10018     case DW_OP_reg12:
10019       return "DW_OP_reg12";
10020     case DW_OP_reg13:
10021       return "DW_OP_reg13";
10022     case DW_OP_reg14:
10023       return "DW_OP_reg14";
10024     case DW_OP_reg15:
10025       return "DW_OP_reg15";
10026     case DW_OP_reg16:
10027       return "DW_OP_reg16";
10028     case DW_OP_reg17:
10029       return "DW_OP_reg17";
10030     case DW_OP_reg18:
10031       return "DW_OP_reg18";
10032     case DW_OP_reg19:
10033       return "DW_OP_reg19";
10034     case DW_OP_reg20:
10035       return "DW_OP_reg20";
10036     case DW_OP_reg21:
10037       return "DW_OP_reg21";
10038     case DW_OP_reg22:
10039       return "DW_OP_reg22";
10040     case DW_OP_reg23:
10041       return "DW_OP_reg23";
10042     case DW_OP_reg24:
10043       return "DW_OP_reg24";
10044     case DW_OP_reg25:
10045       return "DW_OP_reg25";
10046     case DW_OP_reg26:
10047       return "DW_OP_reg26";
10048     case DW_OP_reg27:
10049       return "DW_OP_reg27";
10050     case DW_OP_reg28:
10051       return "DW_OP_reg28";
10052     case DW_OP_reg29:
10053       return "DW_OP_reg29";
10054     case DW_OP_reg30:
10055       return "DW_OP_reg30";
10056     case DW_OP_reg31:
10057       return "DW_OP_reg31";
10058     case DW_OP_breg0:
10059       return "DW_OP_breg0";
10060     case DW_OP_breg1:
10061       return "DW_OP_breg1";
10062     case DW_OP_breg2:
10063       return "DW_OP_breg2";
10064     case DW_OP_breg3:
10065       return "DW_OP_breg3";
10066     case DW_OP_breg4:
10067       return "DW_OP_breg4";
10068     case DW_OP_breg5:
10069       return "DW_OP_breg5";
10070     case DW_OP_breg6:
10071       return "DW_OP_breg6";
10072     case DW_OP_breg7:
10073       return "DW_OP_breg7";
10074     case DW_OP_breg8:
10075       return "DW_OP_breg8";
10076     case DW_OP_breg9:
10077       return "DW_OP_breg9";
10078     case DW_OP_breg10:
10079       return "DW_OP_breg10";
10080     case DW_OP_breg11:
10081       return "DW_OP_breg11";
10082     case DW_OP_breg12:
10083       return "DW_OP_breg12";
10084     case DW_OP_breg13:
10085       return "DW_OP_breg13";
10086     case DW_OP_breg14:
10087       return "DW_OP_breg14";
10088     case DW_OP_breg15:
10089       return "DW_OP_breg15";
10090     case DW_OP_breg16:
10091       return "DW_OP_breg16";
10092     case DW_OP_breg17:
10093       return "DW_OP_breg17";
10094     case DW_OP_breg18:
10095       return "DW_OP_breg18";
10096     case DW_OP_breg19:
10097       return "DW_OP_breg19";
10098     case DW_OP_breg20:
10099       return "DW_OP_breg20";
10100     case DW_OP_breg21:
10101       return "DW_OP_breg21";
10102     case DW_OP_breg22:
10103       return "DW_OP_breg22";
10104     case DW_OP_breg23:
10105       return "DW_OP_breg23";
10106     case DW_OP_breg24:
10107       return "DW_OP_breg24";
10108     case DW_OP_breg25:
10109       return "DW_OP_breg25";
10110     case DW_OP_breg26:
10111       return "DW_OP_breg26";
10112     case DW_OP_breg27:
10113       return "DW_OP_breg27";
10114     case DW_OP_breg28:
10115       return "DW_OP_breg28";
10116     case DW_OP_breg29:
10117       return "DW_OP_breg29";
10118     case DW_OP_breg30:
10119       return "DW_OP_breg30";
10120     case DW_OP_breg31:
10121       return "DW_OP_breg31";
10122     case DW_OP_regx:
10123       return "DW_OP_regx";
10124     case DW_OP_fbreg:
10125       return "DW_OP_fbreg";
10126     case DW_OP_bregx:
10127       return "DW_OP_bregx";
10128     case DW_OP_piece:
10129       return "DW_OP_piece";
10130     case DW_OP_deref_size:
10131       return "DW_OP_deref_size";
10132     case DW_OP_xderef_size:
10133       return "DW_OP_xderef_size";
10134     case DW_OP_nop:
10135       return "DW_OP_nop";
10136     /* DWARF 3 extensions.  */
10137     case DW_OP_push_object_address:
10138       return "DW_OP_push_object_address";
10139     case DW_OP_call2:
10140       return "DW_OP_call2";
10141     case DW_OP_call4:
10142       return "DW_OP_call4";
10143     case DW_OP_call_ref:
10144       return "DW_OP_call_ref";
10145     /* GNU extensions.  */
10146     case DW_OP_form_tls_address:
10147       return "DW_OP_form_tls_address";
10148     case DW_OP_call_frame_cfa:
10149       return "DW_OP_call_frame_cfa";
10150     case DW_OP_bit_piece:
10151       return "DW_OP_bit_piece";
10152     case DW_OP_GNU_push_tls_address:
10153       return "DW_OP_GNU_push_tls_address";
10154     case DW_OP_GNU_uninit:
10155       return "DW_OP_GNU_uninit";
10156     /* HP extensions. */ 
10157     case DW_OP_HP_is_value:
10158       return "DW_OP_HP_is_value";
10159     case DW_OP_HP_fltconst4:
10160       return "DW_OP_HP_fltconst4";
10161     case DW_OP_HP_fltconst8:
10162       return "DW_OP_HP_fltconst8";
10163     case DW_OP_HP_mod_range:
10164       return "DW_OP_HP_mod_range";
10165     case DW_OP_HP_unmod_range:
10166       return "DW_OP_HP_unmod_range";
10167     case DW_OP_HP_tls:
10168       return "DW_OP_HP_tls";
10169     default:
10170       return "OP_<unknown>";
10171     }
10172 }
10173
10174 static char *
10175 dwarf_bool_name (unsigned mybool)
10176 {
10177   if (mybool)
10178     return "TRUE";
10179   else
10180     return "FALSE";
10181 }
10182
10183 /* Convert a DWARF type code into its string name.  */
10184
10185 static char *
10186 dwarf_type_encoding_name (unsigned enc)
10187 {
10188   switch (enc)
10189     {
10190     case DW_ATE_void:
10191       return "DW_ATE_void";
10192     case DW_ATE_address:
10193       return "DW_ATE_address";
10194     case DW_ATE_boolean:
10195       return "DW_ATE_boolean";
10196     case DW_ATE_complex_float:
10197       return "DW_ATE_complex_float";
10198     case DW_ATE_float:
10199       return "DW_ATE_float";
10200     case DW_ATE_signed:
10201       return "DW_ATE_signed";
10202     case DW_ATE_signed_char:
10203       return "DW_ATE_signed_char";
10204     case DW_ATE_unsigned:
10205       return "DW_ATE_unsigned";
10206     case DW_ATE_unsigned_char:
10207       return "DW_ATE_unsigned_char";
10208     /* DWARF 3.  */
10209     case DW_ATE_imaginary_float:
10210       return "DW_ATE_imaginary_float";
10211     case DW_ATE_packed_decimal:
10212       return "DW_ATE_packed_decimal";
10213     case DW_ATE_numeric_string:
10214       return "DW_ATE_numeric_string";
10215     case DW_ATE_edited:
10216       return "DW_ATE_edited";
10217     case DW_ATE_signed_fixed:
10218       return "DW_ATE_signed_fixed";
10219     case DW_ATE_unsigned_fixed:
10220       return "DW_ATE_unsigned_fixed";
10221     case DW_ATE_decimal_float:
10222       return "DW_ATE_decimal_float";
10223     /* HP extensions.  */
10224     case DW_ATE_HP_float80:
10225       return "DW_ATE_HP_float80";
10226     case DW_ATE_HP_complex_float80:
10227       return "DW_ATE_HP_complex_float80";
10228     case DW_ATE_HP_float128:
10229       return "DW_ATE_HP_float128";
10230     case DW_ATE_HP_complex_float128:
10231       return "DW_ATE_HP_complex_float128";
10232     case DW_ATE_HP_floathpintel:
10233       return "DW_ATE_HP_floathpintel";
10234     case DW_ATE_HP_imaginary_float80:
10235       return "DW_ATE_HP_imaginary_float80";
10236     case DW_ATE_HP_imaginary_float128:
10237       return "DW_ATE_HP_imaginary_float128";
10238     default:
10239       return "DW_ATE_<unknown>";
10240     }
10241 }
10242
10243 /* Convert a DWARF call frame info operation to its string name. */
10244
10245 #if 0
10246 static char *
10247 dwarf_cfi_name (unsigned cfi_opc)
10248 {
10249   switch (cfi_opc)
10250     {
10251     case DW_CFA_advance_loc:
10252       return "DW_CFA_advance_loc";
10253     case DW_CFA_offset:
10254       return "DW_CFA_offset";
10255     case DW_CFA_restore:
10256       return "DW_CFA_restore";
10257     case DW_CFA_nop:
10258       return "DW_CFA_nop";
10259     case DW_CFA_set_loc:
10260       return "DW_CFA_set_loc";
10261     case DW_CFA_advance_loc1:
10262       return "DW_CFA_advance_loc1";
10263     case DW_CFA_advance_loc2:
10264       return "DW_CFA_advance_loc2";
10265     case DW_CFA_advance_loc4:
10266       return "DW_CFA_advance_loc4";
10267     case DW_CFA_offset_extended:
10268       return "DW_CFA_offset_extended";
10269     case DW_CFA_restore_extended:
10270       return "DW_CFA_restore_extended";
10271     case DW_CFA_undefined:
10272       return "DW_CFA_undefined";
10273     case DW_CFA_same_value:
10274       return "DW_CFA_same_value";
10275     case DW_CFA_register:
10276       return "DW_CFA_register";
10277     case DW_CFA_remember_state:
10278       return "DW_CFA_remember_state";
10279     case DW_CFA_restore_state:
10280       return "DW_CFA_restore_state";
10281     case DW_CFA_def_cfa:
10282       return "DW_CFA_def_cfa";
10283     case DW_CFA_def_cfa_register:
10284       return "DW_CFA_def_cfa_register";
10285     case DW_CFA_def_cfa_offset:
10286       return "DW_CFA_def_cfa_offset";
10287     /* DWARF 3.  */
10288     case DW_CFA_def_cfa_expression:
10289       return "DW_CFA_def_cfa_expression";
10290     case DW_CFA_expression:
10291       return "DW_CFA_expression";
10292     case DW_CFA_offset_extended_sf:
10293       return "DW_CFA_offset_extended_sf";
10294     case DW_CFA_def_cfa_sf:
10295       return "DW_CFA_def_cfa_sf";
10296     case DW_CFA_def_cfa_offset_sf:
10297       return "DW_CFA_def_cfa_offset_sf";
10298     case DW_CFA_val_offset:
10299       return "DW_CFA_val_offset";
10300     case DW_CFA_val_offset_sf:
10301       return "DW_CFA_val_offset_sf";
10302     case DW_CFA_val_expression:
10303       return "DW_CFA_val_expression";
10304     /* SGI/MIPS specific.  */
10305     case DW_CFA_MIPS_advance_loc8:
10306       return "DW_CFA_MIPS_advance_loc8";
10307     /* GNU extensions.  */
10308     case DW_CFA_GNU_window_save:
10309       return "DW_CFA_GNU_window_save";
10310     case DW_CFA_GNU_args_size:
10311       return "DW_CFA_GNU_args_size";
10312     case DW_CFA_GNU_negative_offset_extended:
10313       return "DW_CFA_GNU_negative_offset_extended";
10314     default:
10315       return "DW_CFA_<unknown>";
10316     }
10317 }
10318 #endif
10319
10320 static void
10321 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
10322 {
10323   unsigned int i;
10324
10325   print_spaces (indent, f);
10326   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
10327            dwarf_tag_name (die->tag), die->abbrev, die->offset);
10328
10329   if (die->parent != NULL)
10330     {
10331       print_spaces (indent, f);
10332       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
10333                           die->parent->offset);
10334     }
10335
10336   print_spaces (indent, f);
10337   fprintf_unfiltered (f, "  has children: %s\n",
10338            dwarf_bool_name (die->child != NULL));
10339
10340   print_spaces (indent, f);
10341   fprintf_unfiltered (f, "  attributes:\n");
10342
10343   for (i = 0; i < die->num_attrs; ++i)
10344     {
10345       print_spaces (indent, f);
10346       fprintf_unfiltered (f, "    %s (%s) ",
10347                dwarf_attr_name (die->attrs[i].name),
10348                dwarf_form_name (die->attrs[i].form));
10349
10350       switch (die->attrs[i].form)
10351         {
10352         case DW_FORM_ref_addr:
10353         case DW_FORM_addr:
10354           fprintf_unfiltered (f, "address: ");
10355           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
10356           break;
10357         case DW_FORM_block2:
10358         case DW_FORM_block4:
10359         case DW_FORM_block:
10360         case DW_FORM_block1:
10361           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
10362           break;
10363         case DW_FORM_exprloc:
10364           fprintf_unfiltered (f, "expression: size %u",
10365                               DW_BLOCK (&die->attrs[i])->size);
10366           break;
10367         case DW_FORM_ref1:
10368         case DW_FORM_ref2:
10369         case DW_FORM_ref4:
10370           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10371                               (long) (DW_ADDR (&die->attrs[i])));
10372           break;
10373         case DW_FORM_data1:
10374         case DW_FORM_data2:
10375         case DW_FORM_data4:
10376         case DW_FORM_data8:
10377         case DW_FORM_udata:
10378         case DW_FORM_sdata:
10379           fprintf_unfiltered (f, "constant: %s",
10380                               pulongest (DW_UNSND (&die->attrs[i])));
10381           break;
10382         case DW_FORM_sec_offset:
10383           fprintf_unfiltered (f, "section offset: %s",
10384                               pulongest (DW_UNSND (&die->attrs[i])));
10385           break;
10386         case DW_FORM_sig8:
10387           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
10388             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
10389                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
10390           else
10391             fprintf_unfiltered (f, "signatured type, offset: unknown");
10392           break;
10393         case DW_FORM_string:
10394         case DW_FORM_strp:
10395           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
10396                    DW_STRING (&die->attrs[i])
10397                    ? DW_STRING (&die->attrs[i]) : "",
10398                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
10399           break;
10400         case DW_FORM_flag:
10401           if (DW_UNSND (&die->attrs[i]))
10402             fprintf_unfiltered (f, "flag: TRUE");
10403           else
10404             fprintf_unfiltered (f, "flag: FALSE");
10405           break;
10406         case DW_FORM_flag_present:
10407           fprintf_unfiltered (f, "flag: TRUE");
10408           break;
10409         case DW_FORM_indirect:
10410           /* the reader will have reduced the indirect form to
10411              the "base form" so this form should not occur */
10412           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
10413           break;
10414         default:
10415           fprintf_unfiltered (f, "unsupported attribute form: %d.",
10416                    die->attrs[i].form);
10417           break;
10418         }
10419       fprintf_unfiltered (f, "\n");
10420     }
10421 }
10422
10423 static void
10424 dump_die_for_error (struct die_info *die)
10425 {
10426   dump_die_shallow (gdb_stderr, 0, die);
10427 }
10428
10429 static void
10430 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
10431 {
10432   int indent = level * 4;
10433
10434   gdb_assert (die != NULL);
10435
10436   if (level >= max_level)
10437     return;
10438
10439   dump_die_shallow (f, indent, die);
10440
10441   if (die->child != NULL)
10442     {
10443       print_spaces (indent, f);
10444       fprintf_unfiltered (f, "  Children:");
10445       if (level + 1 < max_level)
10446         {
10447           fprintf_unfiltered (f, "\n");
10448           dump_die_1 (f, level + 1, max_level, die->child);
10449         }
10450       else
10451         {
10452           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
10453         }
10454     }
10455
10456   if (die->sibling != NULL && level > 0)
10457     {
10458       dump_die_1 (f, level, max_level, die->sibling);
10459     }
10460 }
10461
10462 /* This is called from the pdie macro in gdbinit.in.
10463    It's not static so gcc will keep a copy callable from gdb.  */
10464
10465 void
10466 dump_die (struct die_info *die, int max_level)
10467 {
10468   dump_die_1 (gdb_stdlog, 0, max_level, die);
10469 }
10470
10471 static void
10472 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
10473 {
10474   void **slot;
10475
10476   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
10477
10478   *slot = die;
10479 }
10480
10481 static int
10482 is_ref_attr (struct attribute *attr)
10483 {
10484   switch (attr->form)
10485     {
10486     case DW_FORM_ref_addr:
10487     case DW_FORM_ref1:
10488     case DW_FORM_ref2:
10489     case DW_FORM_ref4:
10490     case DW_FORM_ref8:
10491     case DW_FORM_ref_udata:
10492       return 1;
10493     default:
10494       return 0;
10495     }
10496 }
10497
10498 static unsigned int
10499 dwarf2_get_ref_die_offset (struct attribute *attr)
10500 {
10501   if (is_ref_attr (attr))
10502     return DW_ADDR (attr);
10503
10504   complaint (&symfile_complaints,
10505              _("unsupported die ref attribute form: '%s'"),
10506              dwarf_form_name (attr->form));
10507   return 0;
10508 }
10509
10510 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
10511  * the value held by the attribute is not constant.  */
10512
10513 static LONGEST
10514 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
10515 {
10516   if (attr->form == DW_FORM_sdata)
10517     return DW_SND (attr);
10518   else if (attr->form == DW_FORM_udata
10519            || attr->form == DW_FORM_data1
10520            || attr->form == DW_FORM_data2
10521            || attr->form == DW_FORM_data4
10522            || attr->form == DW_FORM_data8)
10523     return DW_UNSND (attr);
10524   else
10525     {
10526       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
10527                  dwarf_form_name (attr->form));
10528       return default_value;
10529     }
10530 }
10531
10532 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
10533    unit and add it to our queue.
10534    The result is non-zero if PER_CU was queued, otherwise the result is zero
10535    meaning either PER_CU is already queued or it is already loaded.  */
10536
10537 static int
10538 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
10539                        struct dwarf2_per_cu_data *per_cu)
10540 {
10541   /* Mark the dependence relation so that we don't flush PER_CU
10542      too early.  */
10543   dwarf2_add_dependence (this_cu, per_cu);
10544
10545   /* If it's already on the queue, we have nothing to do.  */
10546   if (per_cu->queued)
10547     return 0;
10548
10549   /* If the compilation unit is already loaded, just mark it as
10550      used.  */
10551   if (per_cu->cu != NULL)
10552     {
10553       per_cu->cu->last_used = 0;
10554       return 0;
10555     }
10556
10557   /* Add it to the queue.  */
10558   queue_comp_unit (per_cu, this_cu->objfile);
10559
10560   return 1;
10561 }
10562
10563 /* Follow reference or signature attribute ATTR of SRC_DIE.
10564    On entry *REF_CU is the CU of SRC_DIE.
10565    On exit *REF_CU is the CU of the result.  */
10566
10567 static struct die_info *
10568 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
10569                        struct dwarf2_cu **ref_cu)
10570 {
10571   struct die_info *die;
10572
10573   if (is_ref_attr (attr))
10574     die = follow_die_ref (src_die, attr, ref_cu);
10575   else if (attr->form == DW_FORM_sig8)
10576     die = follow_die_sig (src_die, attr, ref_cu);
10577   else
10578     {
10579       dump_die_for_error (src_die);
10580       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
10581              (*ref_cu)->objfile->name);
10582     }
10583
10584   return die;
10585 }
10586
10587 /* Follow reference attribute ATTR of SRC_DIE.
10588    On entry *REF_CU is the CU of SRC_DIE.
10589    On exit *REF_CU is the CU of the result.  */
10590
10591 static struct die_info *
10592 follow_die_ref (struct die_info *src_die, struct attribute *attr,
10593                 struct dwarf2_cu **ref_cu)
10594 {
10595   struct die_info *die;
10596   unsigned int offset;
10597   struct die_info temp_die;
10598   struct dwarf2_cu *target_cu, *cu = *ref_cu;
10599
10600   gdb_assert (cu->per_cu != NULL);
10601
10602   offset = dwarf2_get_ref_die_offset (attr);
10603
10604   if (cu->per_cu->from_debug_types)
10605     {
10606       /* .debug_types CUs cannot reference anything outside their CU.
10607          If they need to, they have to reference a signatured type via
10608          DW_FORM_sig8.  */
10609       if (! offset_in_cu_p (&cu->header, offset))
10610         goto not_found;
10611       target_cu = cu;
10612     }
10613   else if (! offset_in_cu_p (&cu->header, offset))
10614     {
10615       struct dwarf2_per_cu_data *per_cu;
10616       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
10617
10618       /* If necessary, add it to the queue and load its DIEs.  */
10619       if (maybe_queue_comp_unit (cu, per_cu))
10620         load_full_comp_unit (per_cu, cu->objfile);
10621
10622       target_cu = per_cu->cu;
10623     }
10624   else
10625     target_cu = cu;
10626
10627   *ref_cu = target_cu;
10628   temp_die.offset = offset;
10629   die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
10630   if (die)
10631     return die;
10632
10633  not_found:
10634
10635   error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
10636          "at 0x%x [in module %s]"),
10637          offset, src_die->offset, cu->objfile->name);
10638 }
10639
10640 /* Follow the signature attribute ATTR in SRC_DIE.
10641    On entry *REF_CU is the CU of SRC_DIE.
10642    On exit *REF_CU is the CU of the result.  */
10643
10644 static struct die_info *
10645 follow_die_sig (struct die_info *src_die, struct attribute *attr,
10646                 struct dwarf2_cu **ref_cu)
10647 {
10648   struct objfile *objfile = (*ref_cu)->objfile;
10649   struct die_info temp_die;
10650   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
10651   struct dwarf2_cu *sig_cu;
10652   struct die_info *die;
10653
10654   /* sig_type will be NULL if the signatured type is missing from
10655      the debug info.  */
10656   if (sig_type == NULL)
10657     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
10658              "at 0x%x [in module %s]"),
10659            src_die->offset, objfile->name);
10660
10661   /* If necessary, add it to the queue and load its DIEs.  */
10662
10663   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
10664     read_signatured_type (objfile, sig_type);
10665
10666   gdb_assert (sig_type->per_cu.cu != NULL);
10667
10668   sig_cu = sig_type->per_cu.cu;
10669   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
10670   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
10671   if (die)
10672     {
10673       *ref_cu = sig_cu;
10674       return die;
10675     }
10676
10677   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
10678          "at 0x%x [in module %s]"),
10679          sig_type->type_offset, src_die->offset, objfile->name);
10680 }
10681
10682 /* Given an offset of a signatured type, return its signatured_type.  */
10683
10684 static struct signatured_type *
10685 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
10686 {
10687   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
10688   unsigned int length, initial_length_size;
10689   unsigned int sig_offset;
10690   struct signatured_type find_entry, *type_sig;
10691
10692   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
10693   sig_offset = (initial_length_size
10694                 + 2 /*version*/
10695                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
10696                 + 1 /*address_size*/);
10697   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
10698   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
10699
10700   /* This is only used to lookup previously recorded types.
10701      If we didn't find it, it's our bug.  */
10702   gdb_assert (type_sig != NULL);
10703   gdb_assert (offset == type_sig->offset);
10704
10705   return type_sig;
10706 }
10707
10708 /* Read in signatured type at OFFSET and build its CU and die(s).  */
10709
10710 static void
10711 read_signatured_type_at_offset (struct objfile *objfile,
10712                                 unsigned int offset)
10713 {
10714   struct signatured_type *type_sig;
10715
10716   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
10717
10718   /* We have the section offset, but we need the signature to do the
10719      hash table lookup.  */
10720   type_sig = lookup_signatured_type_at_offset (objfile, offset);
10721
10722   gdb_assert (type_sig->per_cu.cu == NULL);
10723
10724   read_signatured_type (objfile, type_sig);
10725
10726   gdb_assert (type_sig->per_cu.cu != NULL);
10727 }
10728
10729 /* Read in a signatured type and build its CU and DIEs.  */
10730
10731 static void
10732 read_signatured_type (struct objfile *objfile,
10733                       struct signatured_type *type_sig)
10734 {
10735   gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
10736   struct die_reader_specs reader_specs;
10737   struct dwarf2_cu *cu;
10738   ULONGEST signature;
10739   struct cleanup *back_to, *free_cu_cleanup;
10740   struct attribute *attr;
10741
10742   gdb_assert (type_sig->per_cu.cu == NULL);
10743
10744   cu = xmalloc (sizeof (struct dwarf2_cu));
10745   memset (cu, 0, sizeof (struct dwarf2_cu));
10746   obstack_init (&cu->comp_unit_obstack);
10747   cu->objfile = objfile;
10748   type_sig->per_cu.cu = cu;
10749   cu->per_cu = &type_sig->per_cu;
10750
10751   /* If an error occurs while loading, release our storage.  */
10752   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
10753
10754   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
10755                                         types_ptr, objfile->obfd);
10756   gdb_assert (signature == type_sig->signature);
10757
10758   cu->die_hash
10759     = htab_create_alloc_ex (cu->header.length / 12,
10760                             die_hash,
10761                             die_eq,
10762                             NULL,
10763                             &cu->comp_unit_obstack,
10764                             hashtab_obstack_allocate,
10765                             dummy_obstack_deallocate);
10766
10767   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
10768   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
10769
10770   init_cu_die_reader (&reader_specs, cu);
10771
10772   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
10773                                     NULL /*parent*/);
10774
10775   /* We try not to read any attributes in this function, because not
10776      all objfiles needed for references have been loaded yet, and symbol
10777      table processing isn't initialized.  But we have to set the CU language,
10778      or we won't be able to build types correctly.  */
10779   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
10780   if (attr)
10781     set_cu_language (DW_UNSND (attr), cu);
10782   else
10783     set_cu_language (language_minimal, cu);
10784
10785   do_cleanups (back_to);
10786
10787   /* We've successfully allocated this compilation unit.  Let our caller
10788      clean it up when finished with it.  */
10789   discard_cleanups (free_cu_cleanup);
10790
10791   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
10792   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
10793 }
10794
10795 /* Decode simple location descriptions.
10796    Given a pointer to a dwarf block that defines a location, compute
10797    the location and return the value.
10798
10799    NOTE drow/2003-11-18: This function is called in two situations
10800    now: for the address of static or global variables (partial symbols
10801    only) and for offsets into structures which are expected to be
10802    (more or less) constant.  The partial symbol case should go away,
10803    and only the constant case should remain.  That will let this
10804    function complain more accurately.  A few special modes are allowed
10805    without complaint for global variables (for instance, global
10806    register values and thread-local values).
10807
10808    A location description containing no operations indicates that the
10809    object is optimized out.  The return value is 0 for that case.
10810    FIXME drow/2003-11-16: No callers check for this case any more; soon all
10811    callers will only want a very basic result and this can become a
10812    complaint.
10813
10814    Note that stack[0] is unused except as a default error return.
10815    Note that stack overflow is not yet handled.  */
10816
10817 static CORE_ADDR
10818 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
10819 {
10820   struct objfile *objfile = cu->objfile;
10821   struct comp_unit_head *cu_header = &cu->header;
10822   int i;
10823   int size = blk->size;
10824   gdb_byte *data = blk->data;
10825   CORE_ADDR stack[64];
10826   int stacki;
10827   unsigned int bytes_read, unsnd;
10828   gdb_byte op;
10829
10830   i = 0;
10831   stacki = 0;
10832   stack[stacki] = 0;
10833
10834   while (i < size)
10835     {
10836       op = data[i++];
10837       switch (op)
10838         {
10839         case DW_OP_lit0:
10840         case DW_OP_lit1:
10841         case DW_OP_lit2:
10842         case DW_OP_lit3:
10843         case DW_OP_lit4:
10844         case DW_OP_lit5:
10845         case DW_OP_lit6:
10846         case DW_OP_lit7:
10847         case DW_OP_lit8:
10848         case DW_OP_lit9:
10849         case DW_OP_lit10:
10850         case DW_OP_lit11:
10851         case DW_OP_lit12:
10852         case DW_OP_lit13:
10853         case DW_OP_lit14:
10854         case DW_OP_lit15:
10855         case DW_OP_lit16:
10856         case DW_OP_lit17:
10857         case DW_OP_lit18:
10858         case DW_OP_lit19:
10859         case DW_OP_lit20:
10860         case DW_OP_lit21:
10861         case DW_OP_lit22:
10862         case DW_OP_lit23:
10863         case DW_OP_lit24:
10864         case DW_OP_lit25:
10865         case DW_OP_lit26:
10866         case DW_OP_lit27:
10867         case DW_OP_lit28:
10868         case DW_OP_lit29:
10869         case DW_OP_lit30:
10870         case DW_OP_lit31:
10871           stack[++stacki] = op - DW_OP_lit0;
10872           break;
10873
10874         case DW_OP_reg0:
10875         case DW_OP_reg1:
10876         case DW_OP_reg2:
10877         case DW_OP_reg3:
10878         case DW_OP_reg4:
10879         case DW_OP_reg5:
10880         case DW_OP_reg6:
10881         case DW_OP_reg7:
10882         case DW_OP_reg8:
10883         case DW_OP_reg9:
10884         case DW_OP_reg10:
10885         case DW_OP_reg11:
10886         case DW_OP_reg12:
10887         case DW_OP_reg13:
10888         case DW_OP_reg14:
10889         case DW_OP_reg15:
10890         case DW_OP_reg16:
10891         case DW_OP_reg17:
10892         case DW_OP_reg18:
10893         case DW_OP_reg19:
10894         case DW_OP_reg20:
10895         case DW_OP_reg21:
10896         case DW_OP_reg22:
10897         case DW_OP_reg23:
10898         case DW_OP_reg24:
10899         case DW_OP_reg25:
10900         case DW_OP_reg26:
10901         case DW_OP_reg27:
10902         case DW_OP_reg28:
10903         case DW_OP_reg29:
10904         case DW_OP_reg30:
10905         case DW_OP_reg31:
10906           stack[++stacki] = op - DW_OP_reg0;
10907           if (i < size)
10908             dwarf2_complex_location_expr_complaint ();
10909           break;
10910
10911         case DW_OP_regx:
10912           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10913           i += bytes_read;
10914           stack[++stacki] = unsnd;
10915           if (i < size)
10916             dwarf2_complex_location_expr_complaint ();
10917           break;
10918
10919         case DW_OP_addr:
10920           stack[++stacki] = read_address (objfile->obfd, &data[i],
10921                                           cu, &bytes_read);
10922           i += bytes_read;
10923           break;
10924
10925         case DW_OP_const1u:
10926           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
10927           i += 1;
10928           break;
10929
10930         case DW_OP_const1s:
10931           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
10932           i += 1;
10933           break;
10934
10935         case DW_OP_const2u:
10936           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
10937           i += 2;
10938           break;
10939
10940         case DW_OP_const2s:
10941           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
10942           i += 2;
10943           break;
10944
10945         case DW_OP_const4u:
10946           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
10947           i += 4;
10948           break;
10949
10950         case DW_OP_const4s:
10951           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
10952           i += 4;
10953           break;
10954
10955         case DW_OP_constu:
10956           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
10957                                                   &bytes_read);
10958           i += bytes_read;
10959           break;
10960
10961         case DW_OP_consts:
10962           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
10963           i += bytes_read;
10964           break;
10965
10966         case DW_OP_dup:
10967           stack[stacki + 1] = stack[stacki];
10968           stacki++;
10969           break;
10970
10971         case DW_OP_plus:
10972           stack[stacki - 1] += stack[stacki];
10973           stacki--;
10974           break;
10975
10976         case DW_OP_plus_uconst:
10977           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10978           i += bytes_read;
10979           break;
10980
10981         case DW_OP_minus:
10982           stack[stacki - 1] -= stack[stacki];
10983           stacki--;
10984           break;
10985
10986         case DW_OP_deref:
10987           /* If we're not the last op, then we definitely can't encode
10988              this using GDB's address_class enum.  This is valid for partial
10989              global symbols, although the variable's address will be bogus
10990              in the psymtab.  */
10991           if (i < size)
10992             dwarf2_complex_location_expr_complaint ();
10993           break;
10994
10995         case DW_OP_GNU_push_tls_address:
10996           /* The top of the stack has the offset from the beginning
10997              of the thread control block at which the variable is located.  */
10998           /* Nothing should follow this operator, so the top of stack would
10999              be returned.  */
11000           /* This is valid for partial global symbols, but the variable's
11001              address will be bogus in the psymtab.  */
11002           if (i < size)
11003             dwarf2_complex_location_expr_complaint ();
11004           break;
11005
11006         case DW_OP_GNU_uninit:
11007           break;
11008
11009         default:
11010           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
11011                      dwarf_stack_op_name (op));
11012           return (stack[stacki]);
11013         }
11014     }
11015   return (stack[stacki]);
11016 }
11017
11018 /* memory allocation interface */
11019
11020 static struct dwarf_block *
11021 dwarf_alloc_block (struct dwarf2_cu *cu)
11022 {
11023   struct dwarf_block *blk;
11024
11025   blk = (struct dwarf_block *)
11026     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
11027   return (blk);
11028 }
11029
11030 static struct abbrev_info *
11031 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
11032 {
11033   struct abbrev_info *abbrev;
11034
11035   abbrev = (struct abbrev_info *)
11036     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
11037   memset (abbrev, 0, sizeof (struct abbrev_info));
11038   return (abbrev);
11039 }
11040
11041 static struct die_info *
11042 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
11043 {
11044   struct die_info *die;
11045   size_t size = sizeof (struct die_info);
11046
11047   if (num_attrs > 1)
11048     size += (num_attrs - 1) * sizeof (struct attribute);
11049
11050   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
11051   memset (die, 0, sizeof (struct die_info));
11052   return (die);
11053 }
11054
11055 \f
11056 /* Macro support.  */
11057
11058
11059 /* Return the full name of file number I in *LH's file name table.
11060    Use COMP_DIR as the name of the current directory of the
11061    compilation.  The result is allocated using xmalloc; the caller is
11062    responsible for freeing it.  */
11063 static char *
11064 file_full_name (int file, struct line_header *lh, const char *comp_dir)
11065 {
11066   /* Is the file number a valid index into the line header's file name
11067      table?  Remember that file numbers start with one, not zero.  */
11068   if (1 <= file && file <= lh->num_file_names)
11069     {
11070       struct file_entry *fe = &lh->file_names[file - 1];
11071   
11072       if (IS_ABSOLUTE_PATH (fe->name))
11073         return xstrdup (fe->name);
11074       else
11075         {
11076           const char *dir;
11077           int dir_len;
11078           char *full_name;
11079
11080           if (fe->dir_index)
11081             dir = lh->include_dirs[fe->dir_index - 1];
11082           else
11083             dir = comp_dir;
11084
11085           if (dir)
11086             {
11087               dir_len = strlen (dir);
11088               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
11089               strcpy (full_name, dir);
11090               full_name[dir_len] = '/';
11091               strcpy (full_name + dir_len + 1, fe->name);
11092               return full_name;
11093             }
11094           else
11095             return xstrdup (fe->name);
11096         }
11097     }
11098   else
11099     {
11100       /* The compiler produced a bogus file number.  We can at least
11101          record the macro definitions made in the file, even if we
11102          won't be able to find the file by name.  */
11103       char fake_name[80];
11104       sprintf (fake_name, "<bad macro file number %d>", file);
11105
11106       complaint (&symfile_complaints, 
11107                  _("bad file number in macro information (%d)"),
11108                  file);
11109
11110       return xstrdup (fake_name);
11111     }
11112 }
11113
11114
11115 static struct macro_source_file *
11116 macro_start_file (int file, int line,
11117                   struct macro_source_file *current_file,
11118                   const char *comp_dir,
11119                   struct line_header *lh, struct objfile *objfile)
11120 {
11121   /* The full name of this source file.  */
11122   char *full_name = file_full_name (file, lh, comp_dir);
11123
11124   /* We don't create a macro table for this compilation unit
11125      at all until we actually get a filename.  */
11126   if (! pending_macros)
11127     pending_macros = new_macro_table (&objfile->objfile_obstack,
11128                                       objfile->macro_cache);
11129
11130   if (! current_file)
11131     /* If we have no current file, then this must be the start_file
11132        directive for the compilation unit's main source file.  */
11133     current_file = macro_set_main (pending_macros, full_name);
11134   else
11135     current_file = macro_include (current_file, line, full_name);
11136
11137   xfree (full_name);
11138               
11139   return current_file;
11140 }
11141
11142
11143 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
11144    followed by a null byte.  */
11145 static char *
11146 copy_string (const char *buf, int len)
11147 {
11148   char *s = xmalloc (len + 1);
11149   memcpy (s, buf, len);
11150   s[len] = '\0';
11151
11152   return s;
11153 }
11154
11155
11156 static const char *
11157 consume_improper_spaces (const char *p, const char *body)
11158 {
11159   if (*p == ' ')
11160     {
11161       complaint (&symfile_complaints,
11162                  _("macro definition contains spaces in formal argument list:\n`%s'"),
11163                  body);
11164
11165       while (*p == ' ')
11166         p++;
11167     }
11168
11169   return p;
11170 }
11171
11172
11173 static void
11174 parse_macro_definition (struct macro_source_file *file, int line,
11175                         const char *body)
11176 {
11177   const char *p;
11178
11179   /* The body string takes one of two forms.  For object-like macro
11180      definitions, it should be:
11181
11182         <macro name> " " <definition>
11183
11184      For function-like macro definitions, it should be:
11185
11186         <macro name> "() " <definition>
11187      or
11188         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
11189
11190      Spaces may appear only where explicitly indicated, and in the
11191      <definition>.
11192
11193      The Dwarf 2 spec says that an object-like macro's name is always
11194      followed by a space, but versions of GCC around March 2002 omit
11195      the space when the macro's definition is the empty string. 
11196
11197      The Dwarf 2 spec says that there should be no spaces between the
11198      formal arguments in a function-like macro's formal argument list,
11199      but versions of GCC around March 2002 include spaces after the
11200      commas.  */
11201
11202
11203   /* Find the extent of the macro name.  The macro name is terminated
11204      by either a space or null character (for an object-like macro) or
11205      an opening paren (for a function-like macro).  */
11206   for (p = body; *p; p++)
11207     if (*p == ' ' || *p == '(')
11208       break;
11209
11210   if (*p == ' ' || *p == '\0')
11211     {
11212       /* It's an object-like macro.  */
11213       int name_len = p - body;
11214       char *name = copy_string (body, name_len);
11215       const char *replacement;
11216
11217       if (*p == ' ')
11218         replacement = body + name_len + 1;
11219       else
11220         {
11221           dwarf2_macro_malformed_definition_complaint (body);
11222           replacement = body + name_len;
11223         }
11224       
11225       macro_define_object (file, line, name, replacement);
11226
11227       xfree (name);
11228     }
11229   else if (*p == '(')
11230     {
11231       /* It's a function-like macro.  */
11232       char *name = copy_string (body, p - body);
11233       int argc = 0;
11234       int argv_size = 1;
11235       char **argv = xmalloc (argv_size * sizeof (*argv));
11236
11237       p++;
11238
11239       p = consume_improper_spaces (p, body);
11240
11241       /* Parse the formal argument list.  */
11242       while (*p && *p != ')')
11243         {
11244           /* Find the extent of the current argument name.  */
11245           const char *arg_start = p;
11246
11247           while (*p && *p != ',' && *p != ')' && *p != ' ')
11248             p++;
11249
11250           if (! *p || p == arg_start)
11251             dwarf2_macro_malformed_definition_complaint (body);
11252           else
11253             {
11254               /* Make sure argv has room for the new argument.  */
11255               if (argc >= argv_size)
11256                 {
11257                   argv_size *= 2;
11258                   argv = xrealloc (argv, argv_size * sizeof (*argv));
11259                 }
11260
11261               argv[argc++] = copy_string (arg_start, p - arg_start);
11262             }
11263
11264           p = consume_improper_spaces (p, body);
11265
11266           /* Consume the comma, if present.  */
11267           if (*p == ',')
11268             {
11269               p++;
11270
11271               p = consume_improper_spaces (p, body);
11272             }
11273         }
11274
11275       if (*p == ')')
11276         {
11277           p++;
11278
11279           if (*p == ' ')
11280             /* Perfectly formed definition, no complaints.  */
11281             macro_define_function (file, line, name,
11282                                    argc, (const char **) argv, 
11283                                    p + 1);
11284           else if (*p == '\0')
11285             {
11286               /* Complain, but do define it.  */
11287               dwarf2_macro_malformed_definition_complaint (body);
11288               macro_define_function (file, line, name,
11289                                      argc, (const char **) argv, 
11290                                      p);
11291             }
11292           else
11293             /* Just complain.  */
11294             dwarf2_macro_malformed_definition_complaint (body);
11295         }
11296       else
11297         /* Just complain.  */
11298         dwarf2_macro_malformed_definition_complaint (body);
11299
11300       xfree (name);
11301       {
11302         int i;
11303
11304         for (i = 0; i < argc; i++)
11305           xfree (argv[i]);
11306       }
11307       xfree (argv);
11308     }
11309   else
11310     dwarf2_macro_malformed_definition_complaint (body);
11311 }
11312
11313
11314 static void
11315 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
11316                      char *comp_dir, bfd *abfd,
11317                      struct dwarf2_cu *cu)
11318 {
11319   gdb_byte *mac_ptr, *mac_end;
11320   struct macro_source_file *current_file = 0;
11321   enum dwarf_macinfo_record_type macinfo_type;
11322   int at_commandline;
11323
11324   dwarf2_read_section (dwarf2_per_objfile->objfile,
11325                        &dwarf2_per_objfile->macinfo);
11326   if (dwarf2_per_objfile->macinfo.buffer == NULL)
11327     {
11328       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
11329       return;
11330     }
11331
11332   /* First pass: Find the name of the base filename.
11333      This filename is needed in order to process all macros whose definition
11334      (or undefinition) comes from the command line.  These macros are defined
11335      before the first DW_MACINFO_start_file entry, and yet still need to be
11336      associated to the base file.
11337
11338      To determine the base file name, we scan the macro definitions until we
11339      reach the first DW_MACINFO_start_file entry.  We then initialize
11340      CURRENT_FILE accordingly so that any macro definition found before the
11341      first DW_MACINFO_start_file can still be associated to the base file.  */
11342
11343   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11344   mac_end = dwarf2_per_objfile->macinfo.buffer
11345     + dwarf2_per_objfile->macinfo.size;
11346
11347   do
11348     {
11349       /* Do we at least have room for a macinfo type byte?  */
11350       if (mac_ptr >= mac_end)
11351         {
11352           /* Complaint is printed during the second pass as GDB will probably
11353              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
11354           break;
11355         }
11356
11357       macinfo_type = read_1_byte (abfd, mac_ptr);
11358       mac_ptr++;
11359
11360       switch (macinfo_type)
11361         {
11362           /* A zero macinfo type indicates the end of the macro
11363              information.  */
11364         case 0:
11365           break;
11366
11367         case DW_MACINFO_define:
11368         case DW_MACINFO_undef:
11369           /* Only skip the data by MAC_PTR.  */
11370           {
11371             unsigned int bytes_read;
11372
11373             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11374             mac_ptr += bytes_read;
11375             read_string (abfd, mac_ptr, &bytes_read);
11376             mac_ptr += bytes_read;
11377           }
11378           break;
11379
11380         case DW_MACINFO_start_file:
11381           {
11382             unsigned int bytes_read;
11383             int line, file;
11384
11385             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11386             mac_ptr += bytes_read;
11387             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11388             mac_ptr += bytes_read;
11389
11390             current_file = macro_start_file (file, line, current_file, comp_dir,
11391                                              lh, cu->objfile);
11392           }
11393           break;
11394
11395         case DW_MACINFO_end_file:
11396           /* No data to skip by MAC_PTR.  */
11397           break;
11398
11399         case DW_MACINFO_vendor_ext:
11400           /* Only skip the data by MAC_PTR.  */
11401           {
11402             unsigned int bytes_read;
11403
11404             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11405             mac_ptr += bytes_read;
11406             read_string (abfd, mac_ptr, &bytes_read);
11407             mac_ptr += bytes_read;
11408           }
11409           break;
11410
11411         default:
11412           break;
11413         }
11414     } while (macinfo_type != 0 && current_file == NULL);
11415
11416   /* Second pass: Process all entries.
11417
11418      Use the AT_COMMAND_LINE flag to determine whether we are still processing
11419      command-line macro definitions/undefinitions.  This flag is unset when we
11420      reach the first DW_MACINFO_start_file entry.  */
11421
11422   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11423
11424   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
11425      GDB is still reading the definitions from command line.  First
11426      DW_MACINFO_start_file will need to be ignored as it was already executed
11427      to create CURRENT_FILE for the main source holding also the command line
11428      definitions.  On first met DW_MACINFO_start_file this flag is reset to
11429      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
11430
11431   at_commandline = 1;
11432
11433   do
11434     {
11435       /* Do we at least have room for a macinfo type byte?  */
11436       if (mac_ptr >= mac_end)
11437         {
11438           dwarf2_macros_too_long_complaint ();
11439           break;
11440         }
11441
11442       macinfo_type = read_1_byte (abfd, mac_ptr);
11443       mac_ptr++;
11444
11445       switch (macinfo_type)
11446         {
11447           /* A zero macinfo type indicates the end of the macro
11448              information.  */
11449         case 0:
11450           break;
11451
11452         case DW_MACINFO_define:
11453         case DW_MACINFO_undef:
11454           {
11455             unsigned int bytes_read;
11456             int line;
11457             char *body;
11458
11459             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11460             mac_ptr += bytes_read;
11461             body = read_string (abfd, mac_ptr, &bytes_read);
11462             mac_ptr += bytes_read;
11463
11464             if (! current_file)
11465               {
11466                 /* DWARF violation as no main source is present.  */
11467                 complaint (&symfile_complaints,
11468                            _("debug info with no main source gives macro %s "
11469                              "on line %d: %s"),
11470                            macinfo_type == DW_MACINFO_define ? 
11471                              _("definition") : 
11472                                macinfo_type == DW_MACINFO_undef ?
11473                                  _("undefinition") :
11474                                  _("something-or-other"), line, body);
11475                 break;
11476               }
11477             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11478               complaint (&symfile_complaints,
11479                          _("debug info gives %s macro %s with %s line %d: %s"),
11480                          at_commandline ? _("command-line") : _("in-file"),
11481                          macinfo_type == DW_MACINFO_define ?
11482                            _("definition") : 
11483                              macinfo_type == DW_MACINFO_undef ?
11484                                _("undefinition") :
11485                                _("something-or-other"),
11486                          line == 0 ? _("zero") : _("non-zero"), line, body);
11487
11488             if (macinfo_type == DW_MACINFO_define)
11489               parse_macro_definition (current_file, line, body);
11490             else if (macinfo_type == DW_MACINFO_undef)
11491               macro_undef (current_file, line, body);
11492           }
11493           break;
11494
11495         case DW_MACINFO_start_file:
11496           {
11497             unsigned int bytes_read;
11498             int line, file;
11499
11500             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11501             mac_ptr += bytes_read;
11502             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11503             mac_ptr += bytes_read;
11504
11505             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11506               complaint (&symfile_complaints,
11507                          _("debug info gives source %d included "
11508                            "from %s at %s line %d"),
11509                          file, at_commandline ? _("command-line") : _("file"),
11510                          line == 0 ? _("zero") : _("non-zero"), line);
11511
11512             if (at_commandline)
11513               {
11514                 /* This DW_MACINFO_start_file was executed in the pass one.  */
11515                 at_commandline = 0;
11516               }
11517             else
11518               current_file = macro_start_file (file, line,
11519                                                current_file, comp_dir,
11520                                                lh, cu->objfile);
11521           }
11522           break;
11523
11524         case DW_MACINFO_end_file:
11525           if (! current_file)
11526             complaint (&symfile_complaints,
11527                        _("macro debug info has an unmatched `close_file' directive"));
11528           else
11529             {
11530               current_file = current_file->included_by;
11531               if (! current_file)
11532                 {
11533                   enum dwarf_macinfo_record_type next_type;
11534
11535                   /* GCC circa March 2002 doesn't produce the zero
11536                      type byte marking the end of the compilation
11537                      unit.  Complain if it's not there, but exit no
11538                      matter what.  */
11539
11540                   /* Do we at least have room for a macinfo type byte?  */
11541                   if (mac_ptr >= mac_end)
11542                     {
11543                       dwarf2_macros_too_long_complaint ();
11544                       return;
11545                     }
11546
11547                   /* We don't increment mac_ptr here, so this is just
11548                      a look-ahead.  */
11549                   next_type = read_1_byte (abfd, mac_ptr);
11550                   if (next_type != 0)
11551                     complaint (&symfile_complaints,
11552                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
11553
11554                   return;
11555                 }
11556             }
11557           break;
11558
11559         case DW_MACINFO_vendor_ext:
11560           {
11561             unsigned int bytes_read;
11562             int constant;
11563             char *string;
11564
11565             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11566             mac_ptr += bytes_read;
11567             string = read_string (abfd, mac_ptr, &bytes_read);
11568             mac_ptr += bytes_read;
11569
11570             /* We don't recognize any vendor extensions.  */
11571           }
11572           break;
11573         }
11574     } while (macinfo_type != 0);
11575 }
11576
11577 /* Check if the attribute's form is a DW_FORM_block*
11578    if so return true else false. */
11579 static int
11580 attr_form_is_block (struct attribute *attr)
11581 {
11582   return (attr == NULL ? 0 :
11583       attr->form == DW_FORM_block1
11584       || attr->form == DW_FORM_block2
11585       || attr->form == DW_FORM_block4
11586       || attr->form == DW_FORM_block
11587       || attr->form == DW_FORM_exprloc);
11588 }
11589
11590 /* Return non-zero if ATTR's value is a section offset --- classes
11591    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
11592    You may use DW_UNSND (attr) to retrieve such offsets.
11593
11594    Section 7.5.4, "Attribute Encodings", explains that no attribute
11595    may have a value that belongs to more than one of these classes; it
11596    would be ambiguous if we did, because we use the same forms for all
11597    of them.  */
11598 static int
11599 attr_form_is_section_offset (struct attribute *attr)
11600 {
11601   return (attr->form == DW_FORM_data4
11602           || attr->form == DW_FORM_data8
11603           || attr->form == DW_FORM_sec_offset);
11604 }
11605
11606
11607 /* Return non-zero if ATTR's value falls in the 'constant' class, or
11608    zero otherwise.  When this function returns true, you can apply
11609    dwarf2_get_attr_constant_value to it.
11610
11611    However, note that for some attributes you must check
11612    attr_form_is_section_offset before using this test.  DW_FORM_data4
11613    and DW_FORM_data8 are members of both the constant class, and of
11614    the classes that contain offsets into other debug sections
11615    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
11616    that, if an attribute's can be either a constant or one of the
11617    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
11618    taken as section offsets, not constants.  */
11619 static int
11620 attr_form_is_constant (struct attribute *attr)
11621 {
11622   switch (attr->form)
11623     {
11624     case DW_FORM_sdata:
11625     case DW_FORM_udata:
11626     case DW_FORM_data1:
11627     case DW_FORM_data2:
11628     case DW_FORM_data4:
11629     case DW_FORM_data8:
11630       return 1;
11631     default:
11632       return 0;
11633     }
11634 }
11635
11636 static void
11637 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
11638                              struct dwarf2_cu *cu)
11639 {
11640   if (attr_form_is_section_offset (attr)
11641       /* ".debug_loc" may not exist at all, or the offset may be outside
11642          the section.  If so, fall through to the complaint in the
11643          other branch.  */
11644       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
11645     {
11646       struct dwarf2_loclist_baton *baton;
11647
11648       baton = obstack_alloc (&cu->objfile->objfile_obstack,
11649                              sizeof (struct dwarf2_loclist_baton));
11650       baton->per_cu = cu->per_cu;
11651       gdb_assert (baton->per_cu);
11652
11653       dwarf2_read_section (dwarf2_per_objfile->objfile,
11654                            &dwarf2_per_objfile->loc);
11655
11656       /* We don't know how long the location list is, but make sure we
11657          don't run off the edge of the section.  */
11658       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
11659       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
11660       baton->base_address = cu->base_address;
11661       if (cu->base_known == 0)
11662         complaint (&symfile_complaints,
11663                    _("Location list used without specifying the CU base address."));
11664
11665       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
11666       SYMBOL_LOCATION_BATON (sym) = baton;
11667     }
11668   else
11669     {
11670       struct dwarf2_locexpr_baton *baton;
11671
11672       baton = obstack_alloc (&cu->objfile->objfile_obstack,
11673                              sizeof (struct dwarf2_locexpr_baton));
11674       baton->per_cu = cu->per_cu;
11675       gdb_assert (baton->per_cu);
11676
11677       if (attr_form_is_block (attr))
11678         {
11679           /* Note that we're just copying the block's data pointer
11680              here, not the actual data.  We're still pointing into the
11681              info_buffer for SYM's objfile; right now we never release
11682              that buffer, but when we do clean up properly this may
11683              need to change.  */
11684           baton->size = DW_BLOCK (attr)->size;
11685           baton->data = DW_BLOCK (attr)->data;
11686         }
11687       else
11688         {
11689           dwarf2_invalid_attrib_class_complaint ("location description",
11690                                                  SYMBOL_NATURAL_NAME (sym));
11691           baton->size = 0;
11692           baton->data = NULL;
11693         }
11694       
11695       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11696       SYMBOL_LOCATION_BATON (sym) = baton;
11697     }
11698 }
11699
11700 /* Return the OBJFILE associated with the compilation unit CU.  */
11701
11702 struct objfile *
11703 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
11704 {
11705   struct objfile *objfile = per_cu->psymtab->objfile;
11706
11707   /* Return the master objfile, so that we can report and look up the
11708      correct file containing this variable.  */
11709   if (objfile->separate_debug_objfile_backlink)
11710     objfile = objfile->separate_debug_objfile_backlink;
11711
11712   return objfile;
11713 }
11714
11715 /* Return the address size given in the compilation unit header for CU.  */
11716
11717 CORE_ADDR
11718 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
11719 {
11720   if (per_cu->cu)
11721     return per_cu->cu->header.addr_size;
11722   else
11723     {
11724       /* If the CU is not currently read in, we re-read its header.  */
11725       struct objfile *objfile = per_cu->psymtab->objfile;
11726       struct dwarf2_per_objfile *per_objfile
11727         = objfile_data (objfile, dwarf2_objfile_data_key);
11728       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
11729
11730       struct comp_unit_head cu_header;
11731       memset (&cu_header, 0, sizeof cu_header);
11732       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
11733       return cu_header.addr_size;
11734     }
11735 }
11736
11737 /* Locate the .debug_info compilation unit from CU's objfile which contains
11738    the DIE at OFFSET.  Raises an error on failure.  */
11739
11740 static struct dwarf2_per_cu_data *
11741 dwarf2_find_containing_comp_unit (unsigned int offset,
11742                                   struct objfile *objfile)
11743 {
11744   struct dwarf2_per_cu_data *this_cu;
11745   int low, high;
11746
11747   low = 0;
11748   high = dwarf2_per_objfile->n_comp_units - 1;
11749   while (high > low)
11750     {
11751       int mid = low + (high - low) / 2;
11752       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
11753         high = mid;
11754       else
11755         low = mid + 1;
11756     }
11757   gdb_assert (low == high);
11758   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
11759     {
11760       if (low == 0)
11761         error (_("Dwarf Error: could not find partial DIE containing "
11762                "offset 0x%lx [in module %s]"),
11763                (long) offset, bfd_get_filename (objfile->obfd));
11764
11765       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
11766       return dwarf2_per_objfile->all_comp_units[low-1];
11767     }
11768   else
11769     {
11770       this_cu = dwarf2_per_objfile->all_comp_units[low];
11771       if (low == dwarf2_per_objfile->n_comp_units - 1
11772           && offset >= this_cu->offset + this_cu->length)
11773         error (_("invalid dwarf2 offset %u"), offset);
11774       gdb_assert (offset < this_cu->offset + this_cu->length);
11775       return this_cu;
11776     }
11777 }
11778
11779 /* Locate the compilation unit from OBJFILE which is located at exactly
11780    OFFSET.  Raises an error on failure.  */
11781
11782 static struct dwarf2_per_cu_data *
11783 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
11784 {
11785   struct dwarf2_per_cu_data *this_cu;
11786   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11787   if (this_cu->offset != offset)
11788     error (_("no compilation unit with offset %u."), offset);
11789   return this_cu;
11790 }
11791
11792 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
11793
11794 static struct dwarf2_cu *
11795 alloc_one_comp_unit (struct objfile *objfile)
11796 {
11797   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
11798   cu->objfile = objfile;
11799   obstack_init (&cu->comp_unit_obstack);
11800   return cu;
11801 }
11802
11803 /* Release one cached compilation unit, CU.  We unlink it from the tree
11804    of compilation units, but we don't remove it from the read_in_chain;
11805    the caller is responsible for that.
11806    NOTE: DATA is a void * because this function is also used as a
11807    cleanup routine.  */
11808
11809 static void
11810 free_one_comp_unit (void *data)
11811 {
11812   struct dwarf2_cu *cu = data;
11813
11814   if (cu->per_cu != NULL)
11815     cu->per_cu->cu = NULL;
11816   cu->per_cu = NULL;
11817
11818   obstack_free (&cu->comp_unit_obstack, NULL);
11819
11820   xfree (cu);
11821 }
11822
11823 /* This cleanup function is passed the address of a dwarf2_cu on the stack
11824    when we're finished with it.  We can't free the pointer itself, but be
11825    sure to unlink it from the cache.  Also release any associated storage
11826    and perform cache maintenance.
11827
11828    Only used during partial symbol parsing.  */
11829
11830 static void
11831 free_stack_comp_unit (void *data)
11832 {
11833   struct dwarf2_cu *cu = data;
11834
11835   obstack_free (&cu->comp_unit_obstack, NULL);
11836   cu->partial_dies = NULL;
11837
11838   if (cu->per_cu != NULL)
11839     {
11840       /* This compilation unit is on the stack in our caller, so we
11841          should not xfree it.  Just unlink it.  */
11842       cu->per_cu->cu = NULL;
11843       cu->per_cu = NULL;
11844
11845       /* If we had a per-cu pointer, then we may have other compilation
11846          units loaded, so age them now.  */
11847       age_cached_comp_units ();
11848     }
11849 }
11850
11851 /* Free all cached compilation units.  */
11852
11853 static void
11854 free_cached_comp_units (void *data)
11855 {
11856   struct dwarf2_per_cu_data *per_cu, **last_chain;
11857
11858   per_cu = dwarf2_per_objfile->read_in_chain;
11859   last_chain = &dwarf2_per_objfile->read_in_chain;
11860   while (per_cu != NULL)
11861     {
11862       struct dwarf2_per_cu_data *next_cu;
11863
11864       next_cu = per_cu->cu->read_in_chain;
11865
11866       free_one_comp_unit (per_cu->cu);
11867       *last_chain = next_cu;
11868
11869       per_cu = next_cu;
11870     }
11871 }
11872
11873 /* Increase the age counter on each cached compilation unit, and free
11874    any that are too old.  */
11875
11876 static void
11877 age_cached_comp_units (void)
11878 {
11879   struct dwarf2_per_cu_data *per_cu, **last_chain;
11880
11881   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
11882   per_cu = dwarf2_per_objfile->read_in_chain;
11883   while (per_cu != NULL)
11884     {
11885       per_cu->cu->last_used ++;
11886       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
11887         dwarf2_mark (per_cu->cu);
11888       per_cu = per_cu->cu->read_in_chain;
11889     }
11890
11891   per_cu = dwarf2_per_objfile->read_in_chain;
11892   last_chain = &dwarf2_per_objfile->read_in_chain;
11893   while (per_cu != NULL)
11894     {
11895       struct dwarf2_per_cu_data *next_cu;
11896
11897       next_cu = per_cu->cu->read_in_chain;
11898
11899       if (!per_cu->cu->mark)
11900         {
11901           free_one_comp_unit (per_cu->cu);
11902           *last_chain = next_cu;
11903         }
11904       else
11905         last_chain = &per_cu->cu->read_in_chain;
11906
11907       per_cu = next_cu;
11908     }
11909 }
11910
11911 /* Remove a single compilation unit from the cache.  */
11912
11913 static void
11914 free_one_cached_comp_unit (void *target_cu)
11915 {
11916   struct dwarf2_per_cu_data *per_cu, **last_chain;
11917
11918   per_cu = dwarf2_per_objfile->read_in_chain;
11919   last_chain = &dwarf2_per_objfile->read_in_chain;
11920   while (per_cu != NULL)
11921     {
11922       struct dwarf2_per_cu_data *next_cu;
11923
11924       next_cu = per_cu->cu->read_in_chain;
11925
11926       if (per_cu->cu == target_cu)
11927         {
11928           free_one_comp_unit (per_cu->cu);
11929           *last_chain = next_cu;
11930           break;
11931         }
11932       else
11933         last_chain = &per_cu->cu->read_in_chain;
11934
11935       per_cu = next_cu;
11936     }
11937 }
11938
11939 /* Release all extra memory associated with OBJFILE.  */
11940
11941 void
11942 dwarf2_free_objfile (struct objfile *objfile)
11943 {
11944   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
11945
11946   if (dwarf2_per_objfile == NULL)
11947     return;
11948
11949   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
11950   free_cached_comp_units (NULL);
11951
11952   /* Everything else should be on the objfile obstack.  */
11953 }
11954
11955 /* A pair of DIE offset and GDB type pointer.  We store these
11956    in a hash table separate from the DIEs, and preserve them
11957    when the DIEs are flushed out of cache.  */
11958
11959 struct dwarf2_offset_and_type
11960 {
11961   unsigned int offset;
11962   struct type *type;
11963 };
11964
11965 /* Hash function for a dwarf2_offset_and_type.  */
11966
11967 static hashval_t
11968 offset_and_type_hash (const void *item)
11969 {
11970   const struct dwarf2_offset_and_type *ofs = item;
11971   return ofs->offset;
11972 }
11973
11974 /* Equality function for a dwarf2_offset_and_type.  */
11975
11976 static int
11977 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
11978 {
11979   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
11980   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
11981   return ofs_lhs->offset == ofs_rhs->offset;
11982 }
11983
11984 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
11985    table if necessary.  For convenience, return TYPE.  */
11986
11987 static struct type *
11988 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11989 {
11990   struct dwarf2_offset_and_type **slot, ofs;
11991
11992   /* For Ada types, make sure that the gnat-specific data is always
11993      initialized (if not already set).  There are a few types where
11994      we should not be doing so, because the type-specific area is
11995      already used to hold some other piece of info (eg: TYPE_CODE_FLT
11996      where the type-specific area is used to store the floatformat).
11997      But this is not a problem, because the gnat-specific information
11998      is actually not needed for these types.  */
11999   if (need_gnat_info (cu)
12000       && TYPE_CODE (type) != TYPE_CODE_FUNC
12001       && TYPE_CODE (type) != TYPE_CODE_FLT
12002       && !HAVE_GNAT_AUX_INFO (type))
12003     INIT_GNAT_SPECIFIC (type);
12004
12005   if (cu->type_hash == NULL)
12006     {
12007       gdb_assert (cu->per_cu != NULL);
12008       cu->per_cu->type_hash
12009         = htab_create_alloc_ex (cu->header.length / 24,
12010                                 offset_and_type_hash,
12011                                 offset_and_type_eq,
12012                                 NULL,
12013                                 &cu->objfile->objfile_obstack,
12014                                 hashtab_obstack_allocate,
12015                                 dummy_obstack_deallocate);
12016       cu->type_hash = cu->per_cu->type_hash;
12017     }
12018
12019   ofs.offset = die->offset;
12020   ofs.type = type;
12021   slot = (struct dwarf2_offset_and_type **)
12022     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
12023   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
12024   **slot = ofs;
12025   return type;
12026 }
12027
12028 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
12029    not have a saved type.  */
12030
12031 static struct type *
12032 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
12033 {
12034   struct dwarf2_offset_and_type *slot, ofs;
12035   htab_t type_hash = cu->type_hash;
12036
12037   if (type_hash == NULL)
12038     return NULL;
12039
12040   ofs.offset = die->offset;
12041   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
12042   if (slot)
12043     return slot->type;
12044   else
12045     return NULL;
12046 }
12047
12048 /* Add a dependence relationship from CU to REF_PER_CU.  */
12049
12050 static void
12051 dwarf2_add_dependence (struct dwarf2_cu *cu,
12052                        struct dwarf2_per_cu_data *ref_per_cu)
12053 {
12054   void **slot;
12055
12056   if (cu->dependencies == NULL)
12057     cu->dependencies
12058       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
12059                               NULL, &cu->comp_unit_obstack,
12060                               hashtab_obstack_allocate,
12061                               dummy_obstack_deallocate);
12062
12063   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
12064   if (*slot == NULL)
12065     *slot = ref_per_cu;
12066 }
12067
12068 /* Subroutine of dwarf2_mark to pass to htab_traverse.
12069    Set the mark field in every compilation unit in the
12070    cache that we must keep because we are keeping CU.  */
12071
12072 static int
12073 dwarf2_mark_helper (void **slot, void *data)
12074 {
12075   struct dwarf2_per_cu_data *per_cu;
12076
12077   per_cu = (struct dwarf2_per_cu_data *) *slot;
12078   if (per_cu->cu->mark)
12079     return 1;
12080   per_cu->cu->mark = 1;
12081
12082   if (per_cu->cu->dependencies != NULL)
12083     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
12084
12085   return 1;
12086 }
12087
12088 /* Set the mark field in CU and in every other compilation unit in the
12089    cache that we must keep because we are keeping CU.  */
12090
12091 static void
12092 dwarf2_mark (struct dwarf2_cu *cu)
12093 {
12094   if (cu->mark)
12095     return;
12096   cu->mark = 1;
12097   if (cu->dependencies != NULL)
12098     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
12099 }
12100
12101 static void
12102 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
12103 {
12104   while (per_cu)
12105     {
12106       per_cu->cu->mark = 0;
12107       per_cu = per_cu->cu->read_in_chain;
12108     }
12109 }
12110
12111 /* Trivial hash function for partial_die_info: the hash value of a DIE
12112    is its offset in .debug_info for this objfile.  */
12113
12114 static hashval_t
12115 partial_die_hash (const void *item)
12116 {
12117   const struct partial_die_info *part_die = item;
12118   return part_die->offset;
12119 }
12120
12121 /* Trivial comparison function for partial_die_info structures: two DIEs
12122    are equal if they have the same offset.  */
12123
12124 static int
12125 partial_die_eq (const void *item_lhs, const void *item_rhs)
12126 {
12127   const struct partial_die_info *part_die_lhs = item_lhs;
12128   const struct partial_die_info *part_die_rhs = item_rhs;
12129   return part_die_lhs->offset == part_die_rhs->offset;
12130 }
12131
12132 static struct cmd_list_element *set_dwarf2_cmdlist;
12133 static struct cmd_list_element *show_dwarf2_cmdlist;
12134
12135 static void
12136 set_dwarf2_cmd (char *args, int from_tty)
12137 {
12138   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
12139 }
12140
12141 static void
12142 show_dwarf2_cmd (char *args, int from_tty)
12143
12144   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
12145 }
12146
12147 /* If section described by INFO was mmapped, munmap it now.  */
12148
12149 static void
12150 munmap_section_buffer (struct dwarf2_section_info *info)
12151 {
12152   if (info->was_mmapped)
12153     {
12154 #ifdef HAVE_MMAP
12155       intptr_t begin = (intptr_t) info->buffer;
12156       intptr_t map_begin = begin & ~(pagesize - 1);
12157       size_t map_length = info->size + begin - map_begin;
12158       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
12159 #else
12160       /* Without HAVE_MMAP, we should never be here to begin with.  */
12161       gdb_assert (0);
12162 #endif
12163     }
12164 }
12165
12166 /* munmap debug sections for OBJFILE, if necessary.  */
12167
12168 static void
12169 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
12170 {
12171   struct dwarf2_per_objfile *data = d;
12172   munmap_section_buffer (&data->info);
12173   munmap_section_buffer (&data->abbrev);
12174   munmap_section_buffer (&data->line);
12175   munmap_section_buffer (&data->str);
12176   munmap_section_buffer (&data->macinfo);
12177   munmap_section_buffer (&data->ranges);
12178   munmap_section_buffer (&data->loc);
12179   munmap_section_buffer (&data->frame);
12180   munmap_section_buffer (&data->eh_frame);
12181 }
12182
12183 void _initialize_dwarf2_read (void);
12184
12185 void
12186 _initialize_dwarf2_read (void)
12187 {
12188   dwarf2_objfile_data_key
12189     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
12190
12191   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
12192 Set DWARF 2 specific variables.\n\
12193 Configure DWARF 2 variables such as the cache size"),
12194                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
12195                   0/*allow-unknown*/, &maintenance_set_cmdlist);
12196
12197   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
12198 Show DWARF 2 specific variables\n\
12199 Show DWARF 2 variables such as the cache size"),
12200                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
12201                   0/*allow-unknown*/, &maintenance_show_cmdlist);
12202
12203   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
12204                             &dwarf2_max_cache_age, _("\
12205 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
12206 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
12207 A higher limit means that cached compilation units will be stored\n\
12208 in memory longer, and more total memory will be used.  Zero disables\n\
12209 caching, which can slow down startup."),
12210                             NULL,
12211                             show_dwarf2_max_cache_age,
12212                             &set_dwarf2_cmdlist,
12213                             &show_dwarf2_cmdlist);
12214
12215   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
12216 Set debugging of the dwarf2 DIE reader."), _("\
12217 Show debugging of the dwarf2 DIE reader."), _("\
12218 When enabled (non-zero), DIEs are dumped after they are read in.\n\
12219 The value is the maximum depth to print."),
12220                             NULL,
12221                             NULL,
12222                             &setdebuglist, &showdebuglist);
12223 }
This page took 0.722616 seconds and 4 git commands to generate.