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