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