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