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