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