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